1 module des.space.node;
2 
3 public import des.math.linear.vector;
4 public import des.math.linear.matrix;
5 public import des.space.transform;
6 
7 import des.util.arch.tree;
8 import des.util.testsuite;
9 
10 ///
11 interface SpaceNode : Transform, TNode!(SpaceNode,"space")
12 {
13     /++ past code
14      + ---
15      + mixin spaceTNodeHelper!(true,true,true);
16      + ---
17      + 
18      + if `with_matrix_field` add `protected mat4 self_mtr` field
19      + and realization for `matrix` and `offset` properties (with setters)
20      +/
21     mixin template SpaceNodeHelper(bool with_matrix_field=true)
22     {
23         mixin spaceTNodeHelper!(true,true);
24 
25         static if( with_matrix_field )
26         {
27             protected mat4 self_mtr;
28 
29             public @property
30             {
31                 mat4 matrix() const { return self_mtr; }
32                 mat4 matrix( in mat4 m ) { self_mtr = m; return m; }
33 
34                 vec3 offset() const { return vec3( self_mtr.col(3).data[0..3] ); }
35                 vec3 offset( in vec3 o ) { self_mtr.setCol( 3, vec4( o, matrix[3][3] ) ); return o; }
36             }
37         }
38     }
39 
40     const @property
41     {
42         /// local to parent transform
43         mat4 matrix();
44 
45         /// in parent system
46         vec3 offset();
47 
48         final
49         {
50             /// e1
51             vec3 baseX() { return vec3( matrix.col(0).data[0 .. 3] ); }
52             /// e2
53             vec3 baseY() { return vec3( matrix.col(1).data[0 .. 3] ); }
54             /// e3
55             vec3 baseZ() { return vec3( matrix.col(2).data[0 .. 3] ); }
56         }
57     }
58 }
59 
60 final class DimmyNode : SpaceNode
61 {
62     mixin SpaceNodeHelper;
63     this( SpaceNode par = null ) { spaceParent = par; }
64 }
65 
66 unittest
67 {
68     auto tsn = new DimmyNode;
69 
70     assertEq( tsn.baseX, vec3( 1,0,0 ) );
71     assertEq( tsn.baseY, vec3( 0,1,0 ) );
72     assertEq( tsn.baseZ, vec3( 0,0,1 ) );
73     assertEq( tsn.offset, vec3( 0,0,0 ) );
74 
75     tsn.offset = vec3( 1, 2, 3 );
76 
77     assertEq( vec4( tsn.matrix.col(3) ), vec4(1,2,3,1) );
78     tsn.matrix = mat4.init;
79     assertEq( tsn.offset, vec3(0,0,0) );
80 }