1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module des.math.linear.view.transform;
26 
27 public import des.math.linear.vector;
28 public import des.math.linear.matrix;
29 
30 interface Transform
31 {
32     @property mat4 matrix() const;
33 
34     protected final static mat4 getMatrix( const(Transform) tr )
35     {
36         if( tr !is null )
37             return tr.matrix;
38         return mat4.diag(1);
39     }
40 }
41 
42 class SimpleTransform : Transform
43 {
44 protected:
45     mat4 mtr;
46 
47 public:
48     @property
49     {
50         mat4 matrix() const { return mtr; }
51         void matrix( in mat4 m ) { mtr = m; }
52     }
53 }
54 
55 class TransformList : Transform
56 {
57     Transform[] list;
58     enum Order { DIRECT, REVERSE }
59     Order order = Order.DIRECT;
60 
61     @property mat4 matrix() const
62     {
63         mat4 buf;
64         if( order == Order.DIRECT )
65             foreach( tr; list )
66                 buf *= tr.matrix;
67         else
68             foreach_reverse( tr; list )
69                 buf *= tr.matrix;
70         return buf;
71     }
72 }
73 
74 class CachedTransform : Transform
75 {
76 protected:
77     mat4 mtr;
78     Transform transform_source;
79 
80 public:
81 
82     this( Transform ntr ) { setTransform( ntr ); }
83 
84     void setTransform( Transform ntr )
85     {
86         transform_source = ntr;
87         recalc();
88     }
89 
90     void recalc()
91     {
92         if( transform_source !is null )
93             mtr = transform_source.matrix;
94         else mtr = mat4.diag(1);
95     }
96 
97     @property mat4 matrix() const { return mtr; }
98 }