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 | t[0] = t0;
|
---|
52 | t[1] = t1;
|
---|
53 | t[2] = t2;
|
---|
54 | }
|
---|
55 |
|
---|
56 | inline void setInvert (Transformation& mA);
|
---|
57 | inline void transformPoint (const Vector& vIn, Vector& vOut) const;
|
---|
58 | inline void transformDirection (const Vector& vIn, Vector& vOut) const;
|
---|
59 | inline void transformPointTransposed (const Vector& vIn, Vector& vOut) const;
|
---|
60 | inline void transformDirectionTransposed (const Vector& vIn, Vector& vOut) const;
|
---|
61 | inline void rotateX(float angle);
|
---|
62 | inline void rotateY(float angle);
|
---|
63 | inline void rotateZ(float angle);
|
---|
64 | inline void scale(float factor);
|
---|
65 | };
|
---|
66 |
|
---|
67 |
|
---|
68 | void Transformation::setInvert (Transformation& mA)
|
---|
69 | {
|
---|
70 | m3x3Type& A = mA.m;
|
---|
71 |
|
---|
72 | // generated by maple C(A_inv,optimized);
|
---|
73 | float t4 = A[0][0]*A[1][1];
|
---|
74 | float t6 = A[0][0]*A[1][2];
|
---|
75 | float t8 = A[0][1]*A[1][0];
|
---|
76 | float t10 = A[0][2]*A[1][0];
|
---|
77 | float t12 = A[0][1]*A[2][0];
|
---|
78 | float t14 = A[0][2]*A[2][0];
|
---|
79 | 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]);
|
---|
80 |
|
---|
81 | // assert (!_isnan (t17));
|
---|
82 |
|
---|
83 | m[0][0] = (A[1][1]*A[2][2]-A[1][2]*A[2][1])*t17;
|
---|
84 | m[0][1] = -(A[0][1]*A[2][2]-A[0][2]*A[2][1])*t17;
|
---|
85 | m[0][2] = -(-A[0][1]*A[1][2]+A[0][2]*A[1][1])*t17;
|
---|
86 | m[1][0] = -(A[1][0]*A[2][2]-A[1][2]*A[2][0])*t17;
|
---|
87 | m[1][1] = (A[0][0]*A[2][2]-t14)*t17;
|
---|
88 | m[1][2] = -(t6-t10)*t17;
|
---|
89 | m[2][0] = -(-A[1][0]*A[2][1]+A[1][1]*A[2][0])*t17;
|
---|
90 | m[2][1] = -(A[0][0]*A[2][1]-t12)*t17;
|
---|
91 | m[2][2] = (t4-t8)*t17;
|
---|
92 |
|
---|
93 | t[0] = -(mA.t[0] * m[0][0] + mA.t[1] * m[0][1] + mA.t[2] * m[0][2]);
|
---|
94 | t[1] = -(mA.t[0] * m[1][0] + mA.t[1] * m[1][1] + mA.t[2] * m[1][2]);
|
---|
95 | t[2] = -(mA.t[0] * m[2][0] + mA.t[1] * m[2][1] + mA.t[2] * m[2][2]);
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | //! vOut = Matrix * vIn, matrix is on the left side
|
---|
100 | inline void Transformation::transformPoint (const Vector& vIn, Vector& vOut) const
|
---|
101 | {
|
---|
102 | vOut.x = vIn.x * m[0][0] + vIn.y * m[0][1] + vIn.z * m[0][2] + t[0];
|
---|
103 | vOut.y = vIn.x * m[1][0] + vIn.y * m[1][1] + vIn.z * m[1][2] + t[1];
|
---|
104 | vOut.z = vIn.x * m[2][0] + vIn.y * m[2][1] + vIn.z * m[2][2] + t[2];
|
---|
105 | }
|
---|
106 |
|
---|
107 | //! vOut = vIn * Matrix, matrix is on the right side
|
---|
108 | inline void Transformation::transformPointTransposed (const Vector& vIn, Vector& vOut) const
|
---|
109 | {
|
---|
110 | vOut.x = vIn.x * m[0][0] + vIn.y * m[1][0] + vIn.z * m[2][0] + t[0];
|
---|
111 | vOut.y = vIn.x * m[0][1] + vIn.y * m[1][1] + vIn.z * m[2][1] + t[1];
|
---|
112 | vOut.z = vIn.x * m[0][2] + vIn.y * m[1][2] + vIn.z * m[2][2] + t[2];
|
---|
113 | }
|
---|
114 |
|
---|
115 | //! vOut = Matrix * vIn, matrix is on the left side
|
---|
116 | inline void Transformation::transformDirection (const Vector& vIn, Vector& vOut) const
|
---|
117 | {
|
---|
118 | vOut.x = vIn.x * m[0][0] + vIn.y * m[0][1] + vIn.z * m[0][2];
|
---|
119 | vOut.y = vIn.x * m[1][0] + vIn.y * m[1][1] + vIn.z * m[1][2];
|
---|
120 | vOut.z = vIn.x * m[2][0] + vIn.y * m[2][1] + vIn.z * m[2][2];
|
---|
121 | }
|
---|
122 |
|
---|
123 | //! vOut = vIn * Matrix, matrix is on the right side
|
---|
124 | inline void Transformation::transformDirectionTransposed (const Vector& vIn, Vector& vOut) const
|
---|
125 | {
|
---|
126 | vOut.x = vIn.x * m[0][0] + vIn.y * m[1][0] + vIn.z * m[2][0];
|
---|
127 | vOut.y = vIn.x * m[0][1] + vIn.y * m[1][1] + vIn.z * m[2][1];
|
---|
128 | vOut.z = vIn.x * m[0][2] + vIn.y * m[1][2] + vIn.z * m[2][2];
|
---|
129 | }
|
---|
130 |
|
---|
131 | inline void Transformation::rotateZ(float angle)
|
---|
132 | {
|
---|
133 | float cosx = cosf(angle);
|
---|
134 | float sinx = sinf(angle);
|
---|
135 |
|
---|
136 | float tmp = m[0][0];
|
---|
137 | m[0][0] = cosx * tmp - sinx * m[1][0];
|
---|
138 | m[1][0] = sinx * tmp + cosx * m[1][0];
|
---|
139 |
|
---|
140 | tmp = m[0][1];
|
---|
141 | m[0][1] = cosx * tmp - sinx * m[1][1];
|
---|
142 | m[1][1] = sinx * tmp + cosx * m[1][1];
|
---|
143 |
|
---|
144 | tmp = m[0][2];
|
---|
145 | m[0][2] = cosx * tmp - sinx * m[1][2];
|
---|
146 | m[1][2] = sinx * tmp + cosx * m[1][2];
|
---|
147 |
|
---|
148 | tmp = t[0];
|
---|
149 | t[0] = cosx * tmp - sinx * t[1];
|
---|
150 | t[1] = sinx * tmp + cosx * t[1];
|
---|
151 | }
|
---|
152 |
|
---|
153 | inline void Transformation::rotateY(float angle)
|
---|
154 | {
|
---|
155 | float cosx = cosf(angle);
|
---|
156 | float sinx = sinf(angle);
|
---|
157 |
|
---|
158 | float tmp = m[0][0];
|
---|
159 | m[0][0] = cosx * tmp - sinx * m[2][0];
|
---|
160 | m[2][0] = sinx * tmp + cosx * m[2][0];
|
---|
161 |
|
---|
162 | tmp = m[0][2];
|
---|
163 | m[0][2] = cosx * tmp - sinx * m[2][2];
|
---|
164 | m[2][2] = sinx * tmp + cosx * m[2][2];
|
---|
165 |
|
---|
166 | tmp = m[0][1];
|
---|
167 | m[0][1] = cosx * tmp - sinx * m[2][1];
|
---|
168 | m[2][1] = sinx * tmp + cosx * m[2][1];
|
---|
169 |
|
---|
170 | tmp = t[0];
|
---|
171 | t[0] = cosx * tmp - sinx * t[2];
|
---|
172 | t[2] = sinx * tmp + cosx * t[2];
|
---|
173 | }
|
---|
174 |
|
---|
175 | inline void Transformation::rotateX(float angle)
|
---|
176 | {
|
---|
177 | float cosx = cosf(angle);
|
---|
178 | float sinx = sinf(angle);
|
---|
179 |
|
---|
180 | float tmp = m[1][1];
|
---|
181 | m[1][1] = cosx * tmp - sinx * m[2][1];
|
---|
182 | m[2][1] = sinx * tmp + cosx * m[2][1];
|
---|
183 |
|
---|
184 | tmp = m[1][2];
|
---|
185 | m[1][2] = cosx * tmp - sinx * m[2][2];
|
---|
186 | m[2][2] = sinx * tmp + cosx * m[2][2];
|
---|
187 |
|
---|
188 | tmp = m[1][0];
|
---|
189 | m[1][0] = cosx * tmp - sinx * m[2][0];
|
---|
190 | m[2][0] = sinx * tmp + cosx * m[2][0];
|
---|
191 |
|
---|
192 | tmp = t[1];
|
---|
193 | t[1] = cosx * tmp - sinx * t[2];
|
---|
194 | t[2] = sinx * tmp + cosx * t[2];
|
---|
195 | }
|
---|
196 |
|
---|
197 | inline void Transformation::scale(float factor)
|
---|
198 | {
|
---|
199 |
|
---|
200 | for(int i=0; i<3; i++)
|
---|
201 | {
|
---|
202 | for(int j=0; j<3; j++)
|
---|
203 | m[i][j] *= factor;
|
---|
204 | t[i] *= factor;
|
---|
205 | }
|
---|
206 |
|
---|
207 | } |
---|