1 | #pragma once
|
---|
2 |
|
---|
3 | #include "Vector.hpp"
|
---|
4 | #include "Float.h"
|
---|
5 | #include <math.h>
|
---|
6 |
|
---|
7 | typedef float m3x3Type[3][3];
|
---|
8 |
|
---|
9 | /*!
|
---|
10 | \brief 3D linear transformation + translation class.
|
---|
11 | Used by the ray-tracing system to store entity modelling transformations. Class Transformed is an Intersecable
|
---|
12 | that refers to an Intersectable and contains a Transformation.
|
---|
13 | */
|
---|
14 | class Transformation {
|
---|
15 | public:
|
---|
16 | float m[3][3];
|
---|
17 | float t[3];
|
---|
18 |
|
---|
19 | Transformation(std::istream& isc)
|
---|
20 | {
|
---|
21 | for(int i=0;i < 3;i++)
|
---|
22 | for(int j=0;j < 3;j++)
|
---|
23 | isc >> m[i][j];
|
---|
24 | for(int ti=0;ti < 3;ti++)
|
---|
25 | isc >> t[ti];
|
---|
26 | }
|
---|
27 |
|
---|
28 | Transformation()
|
---|
29 | {
|
---|
30 | m[0][0] = m[1][1] = m[2][2] = 1.0f;
|
---|
31 | m[0][1] = m[0][2] = m [1][0] = m[1][2] = m[2][0] = m[2][1] = 0.0f;
|
---|
32 | t[0] = t[1] = t[2] = 0.0f;
|
---|
33 | }
|
---|
34 |
|
---|
35 | inline void fill (float m00, float m01, float m02,
|
---|
36 | float m10, float m11, float m12,
|
---|
37 | float m20, float m21, float m22,
|
---|
38 | float t0, float t1, float t2){
|
---|
39 | /* m[0][0] = m00;
|
---|
40 | m[0][1] = m01;
|
---|
41 | m[0][2] = m02;
|
---|
42 |
|
---|
43 | m[1][0] = m10;
|
---|
44 | m[1][1] = m11;
|
---|
45 | m[1][2] = m12;
|
---|
46 |
|
---|
47 | m[2][0] = m20;
|
---|
48 | m[2][1] = m21;
|
---|
49 | m[2][2] = m22;*/
|
---|
50 |
|
---|
51 | m[0][0] = m00;
|
---|
52 | m[1][0] = m01;
|
---|
53 | m[2][0] = m02;
|
---|
54 |
|
---|
55 | m[0][1] = m10;
|
---|
56 | m[1][1] = m11;
|
---|
57 | m[2][1] = m12;
|
---|
58 |
|
---|
59 | m[0][2] = m20;
|
---|
60 | m[1][2] = m21;
|
---|
61 | m[2][2] = m22;
|
---|
62 |
|
---|
63 | t[0] = t0;
|
---|
64 | t[1] = t1;
|
---|
65 | t[2] = t2;
|
---|
66 | }
|
---|
67 |
|
---|
68 | inline void setInvert (Transformation& mA);
|
---|
69 | inline void transformPoint (const Vector& vIn, Vector& vOut) const;
|
---|
70 | inline void transformDirection (const Vector& vIn, Vector& vOut) const;
|
---|
71 | inline void transformPointTransposed (const Vector& vIn, Vector& vOut) const;
|
---|
72 | inline void transformDirectionTransposed (const Vector& vIn, Vector& vOut) const;
|
---|
73 | inline void rotateX(float angle);
|
---|
74 | inline void rotateY(float angle);
|
---|
75 | inline void rotateZ(float angle);
|
---|
76 | inline void scale(float factor);
|
---|
77 | };
|
---|
78 |
|
---|
79 |
|
---|
80 | void Transformation::setInvert (Transformation& mA)
|
---|
81 | {
|
---|
82 | m3x3Type& A = mA.m;
|
---|
83 |
|
---|
84 | // generated by maple C(A_inv,optimized);
|
---|
85 | float t4 = A[0][0]*A[1][1];
|
---|
86 | float t6 = A[0][0]*A[1][2];
|
---|
87 | float t8 = A[0][1]*A[1][0];
|
---|
88 | float t10 = A[0][2]*A[1][0];
|
---|
89 | float t12 = A[0][1]*A[2][0];
|
---|
90 | float t14 = A[0][2]*A[2][0];
|
---|
91 | float t17 = 1/(t4*A[2][2]-t6*A[2][1]-t8*A[2][2]+t10*A[2][1]+t12*A[1][2]-t14*A[1][1]);
|
---|
92 |
|
---|
93 | // assert (!_isnan (t17));
|
---|
94 |
|
---|
95 | m[0][0] = (A[1][1]*A[2][2]-A[1][2]*A[2][1])*t17;
|
---|
96 | m[0][1] = -(A[0][1]*A[2][2]-A[0][2]*A[2][1])*t17;
|
---|
97 | m[0][2] = -(-A[0][1]*A[1][2]+A[0][2]*A[1][1])*t17;
|
---|
98 | m[1][0] = -(A[1][0]*A[2][2]-A[1][2]*A[2][0])*t17;
|
---|
99 | m[1][1] = (A[0][0]*A[2][2]-t14)*t17;
|
---|
100 | m[1][2] = -(t6-t10)*t17;
|
---|
101 | m[2][0] = -(-A[1][0]*A[2][1]+A[1][1]*A[2][0])*t17;
|
---|
102 | m[2][1] = -(A[0][0]*A[2][1]-t12)*t17;
|
---|
103 | m[2][2] = (t4-t8)*t17;
|
---|
104 |
|
---|
105 | t[0] = -(mA.t[0] * m[0][0] + mA.t[1] * m[0][1] + mA.t[2] * m[0][2]);
|
---|
106 | t[1] = -(mA.t[0] * m[1][0] + mA.t[1] * m[1][1] + mA.t[2] * m[1][2]);
|
---|
107 | t[2] = -(mA.t[0] * m[2][0] + mA.t[1] * m[2][1] + mA.t[2] * m[2][2]);
|
---|
108 |
|
---|
109 | }
|
---|
110 |
|
---|
111 | //! vOut = Matrix * vIn, matrix is on the left side
|
---|
112 | inline void Transformation::transformPoint (const Vector& vIn, Vector& vOut) const
|
---|
113 | {
|
---|
114 | vOut.x = vIn.x * m[0][0] + vIn.y * m[0][1] + vIn.z * m[0][2] + t[0];
|
---|
115 | vOut.y = vIn.x * m[1][0] + vIn.y * m[1][1] + vIn.z * m[1][2] + t[1];
|
---|
116 | vOut.z = vIn.x * m[2][0] + vIn.y * m[2][1] + vIn.z * m[2][2] + t[2];
|
---|
117 | }
|
---|
118 |
|
---|
119 | //! vOut = vIn * Matrix, matrix is on the right side
|
---|
120 | inline void Transformation::transformPointTransposed (const Vector& vIn, Vector& vOut) const
|
---|
121 | {
|
---|
122 | vOut.x = vIn.x * m[0][0] + vIn.y * m[1][0] + vIn.z * m[2][0] + t[0];
|
---|
123 | vOut.y = vIn.x * m[0][1] + vIn.y * m[1][1] + vIn.z * m[2][1] + t[1];
|
---|
124 | vOut.z = vIn.x * m[0][2] + vIn.y * m[1][2] + vIn.z * m[2][2] + t[2];
|
---|
125 | }
|
---|
126 |
|
---|
127 | //! vOut = Matrix * vIn, matrix is on the left side
|
---|
128 | inline void Transformation::transformDirection (const Vector& vIn, Vector& vOut) const
|
---|
129 | {
|
---|
130 | vOut.x = vIn.x * m[0][0] + vIn.y * m[0][1] + vIn.z * m[0][2];
|
---|
131 | vOut.y = vIn.x * m[1][0] + vIn.y * m[1][1] + vIn.z * m[1][2];
|
---|
132 | vOut.z = vIn.x * m[2][0] + vIn.y * m[2][1] + vIn.z * m[2][2];
|
---|
133 | }
|
---|
134 |
|
---|
135 | //! vOut = vIn * Matrix, matrix is on the right side
|
---|
136 | inline void Transformation::transformDirectionTransposed (const Vector& vIn, Vector& vOut) const
|
---|
137 | {
|
---|
138 | vOut.x = vIn.x * m[0][0] + vIn.y * m[1][0] + vIn.z * m[2][0];
|
---|
139 | vOut.y = vIn.x * m[0][1] + vIn.y * m[1][1] + vIn.z * m[2][1];
|
---|
140 | vOut.z = vIn.x * m[0][2] + vIn.y * m[1][2] + vIn.z * m[2][2];
|
---|
141 | }
|
---|
142 |
|
---|
143 | inline void Transformation::rotateZ(float angle)
|
---|
144 | {
|
---|
145 | float cosx = cosf(angle);
|
---|
146 | float sinx = sinf(angle);
|
---|
147 |
|
---|
148 | float tmp = m[0][0];
|
---|
149 | m[0][0] = cosx * tmp - sinx * m[1][0];
|
---|
150 | m[1][0] = sinx * tmp + cosx * m[1][0];
|
---|
151 |
|
---|
152 | tmp = m[0][1];
|
---|
153 | m[0][1] = cosx * tmp - sinx * m[1][1];
|
---|
154 | m[1][1] = sinx * tmp + cosx * m[1][1];
|
---|
155 |
|
---|
156 | tmp = m[0][2];
|
---|
157 | m[0][2] = cosx * tmp - sinx * m[1][2];
|
---|
158 | m[1][2] = sinx * tmp + cosx * m[1][2];
|
---|
159 |
|
---|
160 | tmp = t[0];
|
---|
161 | t[0] = cosx * tmp - sinx * t[1];
|
---|
162 | t[1] = sinx * tmp + cosx * t[1];
|
---|
163 | }
|
---|
164 |
|
---|
165 | inline void Transformation::rotateY(float angle)
|
---|
166 | {
|
---|
167 | float cosx = cosf(angle);
|
---|
168 | float sinx = sinf(angle);
|
---|
169 |
|
---|
170 | float tmp = m[0][0];
|
---|
171 | m[0][0] = cosx * tmp - sinx * m[2][0];
|
---|
172 | m[2][0] = sinx * tmp + cosx * m[2][0];
|
---|
173 |
|
---|
174 | tmp = m[0][2];
|
---|
175 | m[0][2] = cosx * tmp - sinx * m[2][2];
|
---|
176 | m[2][2] = sinx * tmp + cosx * m[2][2];
|
---|
177 |
|
---|
178 | tmp = m[0][1];
|
---|
179 | m[0][1] = cosx * tmp - sinx * m[2][1];
|
---|
180 | m[2][1] = sinx * tmp + cosx * m[2][1];
|
---|
181 |
|
---|
182 | tmp = t[0];
|
---|
183 | t[0] = cosx * tmp - sinx * t[2];
|
---|
184 | t[2] = sinx * tmp + cosx * t[2];
|
---|
185 | }
|
---|
186 |
|
---|
187 | inline void Transformation::rotateX(float angle)
|
---|
188 | {
|
---|
189 | float cosx = cosf(angle);
|
---|
190 | float sinx = sinf(angle);
|
---|
191 |
|
---|
192 | float tmp = m[1][1];
|
---|
193 | m[1][1] = cosx * tmp - sinx * m[2][1];
|
---|
194 | m[2][1] = sinx * tmp + cosx * m[2][1];
|
---|
195 |
|
---|
196 | tmp = m[1][2];
|
---|
197 | m[1][2] = cosx * tmp - sinx * m[2][2];
|
---|
198 | m[2][2] = sinx * tmp + cosx * m[2][2];
|
---|
199 |
|
---|
200 | tmp = m[1][0];
|
---|
201 | m[1][0] = cosx * tmp - sinx * m[2][0];
|
---|
202 | m[2][0] = sinx * tmp + cosx * m[2][0];
|
---|
203 |
|
---|
204 | tmp = t[1];
|
---|
205 | t[1] = cosx * tmp - sinx * t[2];
|
---|
206 | t[2] = sinx * tmp + cosx * t[2];
|
---|
207 | }
|
---|
208 |
|
---|
209 | inline void Transformation::scale(float factor)
|
---|
210 | {
|
---|
211 |
|
---|
212 | for(int i=0; i<3; i++)
|
---|
213 | {
|
---|
214 | for(int j=0; j<3; j++)
|
---|
215 | m[i][j] *= factor;
|
---|
216 | t[i] *= factor;
|
---|
217 | }
|
---|
218 |
|
---|
219 | } |
---|