forked from openscad/scad-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformations.scad
43 lines (37 loc) · 1 KB
/
transformations.scad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use <se3.scad>
use <linalg.scad>
use <lists.scad>
/*!
Creates a rotation matrix
xyz = euler angles = rz * ry * rx
axis = rotation_axis * rotation_angle
*/
function rotation(xyz=undef, axis=undef) =
xyz != undef && axis != undef ? undef :
xyz == undef ? se3_exp([0,0,0,axis[0],axis[1],axis[2]]) :
len(xyz) == undef ? rotation(axis=[0,0,xyz]) :
(len(xyz) >= 3 ? rotation(axis=[0,0,xyz[2]]) : identity4()) *
(len(xyz) >= 2 ? rotation(axis=[0,xyz[1],0]) : identity4()) *
(len(xyz) >= 1 ? rotation(axis=[xyz[0],0,0]) : identity4());
/*!
Creates a scaling matrix
*/
function scaling(v) = [
[v[0],0,0,0],
[0,v[1],0,0],
[0,0,v[2],0],
[0,0,0,1],
];
/*!
Creates a translation matrix
*/
function translation(v) = [
[1,0,0,v[0]],
[0,1,0,v[1]],
[0,0,1,v[2]],
[0,0,0,1],
];
// Convert between cartesian and homogenous coordinates
function project(x) = subarray(x,end=len(x)-1) / x[len(x)-1];
function transform(m, list) = [for (p=list) project(m * vec4(p))];
function to_3d(list) = [ for(v = list) vec3(v) ];