transformations.scad 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use <se3.scad>
  2. use <linalg.scad>
  3. use <lists.scad>
  4. /*!
  5. Creates a rotation matrix
  6. xyz = euler angles = rz * ry * rx
  7. axis = rotation_axis * rotation_angle
  8. */
  9. function rotation(xyz=undef, axis=undef) =
  10. xyz != undef && axis != undef ? undef :
  11. xyz == undef ? se3_exp([0,0,0,axis[0],axis[1],axis[2]]) :
  12. len(xyz) == undef ? rotation(axis=[0,0,xyz]) :
  13. (len(xyz) >= 3 ? rotation(axis=[0,0,xyz[2]]) : identity4()) *
  14. (len(xyz) >= 2 ? rotation(axis=[0,xyz[1],0]) : identity4()) *
  15. (len(xyz) >= 1 ? rotation(axis=[xyz[0],0,0]) : identity4());
  16. /*!
  17. Creates a scaling matrix
  18. */
  19. function scaling(v) = [
  20. [v[0],0,0,0],
  21. [0,v[1],0,0],
  22. [0,0,v[2],0],
  23. [0,0,0,1],
  24. ];
  25. /*!
  26. Creates a translation matrix
  27. */
  28. function translation(v) = [
  29. [1,0,0,v[0]],
  30. [0,1,0,v[1]],
  31. [0,0,1,v[2]],
  32. [0,0,0,1],
  33. ];
  34. // Convert between cartesian and homogenous coordinates
  35. function project(x) = subarray(x,end=len(x)-1) / x[len(x)-1];
  36. function transform(m, list) = [for (p=list) project(m * vec4(p))];
  37. function to_3d(list) = [ for(v = list) vec3(v) ];