aboutsummaryrefslogtreecommitdiff
path: root/tool_src/BspLib/Transform2.cpp
blob: 5d6a48b21d81dd55b1265783f45c04446766f715 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//-----------------------------------------------------------------------------
//	BSPLIB MODULE: Transform2.cpp
//
//  Copyright (c) 1997-1998 by Markus Hadwiger
//  All Rights Reserved.
//-----------------------------------------------------------------------------

// bsplib headers
#include "Transform2.h"


BSPLIB_NAMESPACE_BEGIN


// multiply two 3x3 matrices --------------------------------------------------
//
PRIVATE
void mat3x3_mul( double dest_matrix[][3], const double matrix1[][3], const double matrix2[][3] )
{
	//NOTE:
	// full homogeneous multiplication is performed. (that is, all 9
	// elements are calculated.) normally this isn't necessary as
	// most matrices are affine. but since this is no real-time
	// application more flexibility seems better.
/*
	for ( int i = 0; i < 3; i++ ) {
		for ( int j = 0; j < 3; j++ ) {
			dest_matrix[ i ][ j ] = 0.0;
			for ( int k = 0; k < 3; k++ )
				dest_matrix[ i ][ j ] += matrix1[ i ][ k ] * matrix2[ k ][ j ];
		}
	}
*/
	dest_matrix[ 0 ][ 0 ] = matrix1[ 0 ][ 0 ] * matrix2[ 0 ][ 0 ] +
							matrix1[ 0 ][ 1 ] * matrix2[ 1 ][ 0 ] +
							matrix1[ 0 ][ 2 ] * matrix2[ 2 ][ 0 ];
	dest_matrix[ 0 ][ 1 ] = matrix1[ 0 ][ 0 ] * matrix2[ 0 ][ 1 ] +
							matrix1[ 0 ][ 1 ] * matrix2[ 1 ][ 1 ] +
							matrix1[ 0 ][ 2 ] * matrix2[ 2 ][ 1 ];
	dest_matrix[ 0 ][ 2 ] = matrix1[ 0 ][ 0 ] * matrix2[ 0 ][ 2 ] +
							matrix1[ 0 ][ 1 ] * matrix2[ 1 ][ 2 ] +
							matrix1[ 0 ][ 2 ] * matrix2[ 2 ][ 2 ];

	dest_matrix[ 1 ][ 0 ] = matrix1[ 1 ][ 0 ] * matrix2[ 0 ][ 0 ] +
							matrix1[ 1 ][ 1 ] * matrix2[ 1 ][ 0 ] +
							matrix1[ 1 ][ 2 ] * matrix2[ 2 ][ 0 ];
	dest_matrix[ 1 ][ 1 ] = matrix1[ 1 ][ 0 ] * matrix2[ 0 ][ 1 ] +
							matrix1[ 1 ][ 1 ] * matrix2[ 1 ][ 1 ] +
							matrix1[ 1 ][ 2 ] * matrix2[ 2 ][ 1 ];
	dest_matrix[ 1 ][ 2 ] = matrix1[ 1 ][ 0 ] * matrix2[ 0 ][ 2 ] +
							matrix1[ 1 ][ 1 ] * matrix2[ 1 ][ 2 ] +
							matrix1[ 1 ][ 2 ] * matrix2[ 2 ][ 2 ];

	dest_matrix[ 2 ][ 0 ] = matrix1[ 2 ][ 0 ] * matrix2[ 0 ][ 0 ] +
							matrix1[ 2 ][ 1 ] * matrix2[ 1 ][ 0 ] +
							matrix1[ 2 ][ 2 ] * matrix2[ 2 ][ 0 ];
	dest_matrix[ 2 ][ 1 ] = matrix1[ 2 ][ 0 ] * matrix2[ 0 ][ 1 ] +
							matrix1[ 2 ][ 1 ] * matrix2[ 1 ][ 1 ] +
							matrix1[ 2 ][ 2 ] * matrix2[ 2 ][ 1 ];
	dest_matrix[ 2 ][ 2 ] = matrix1[ 2 ][ 0 ] * matrix2[ 0 ][ 2 ] +
							matrix1[ 2 ][ 1 ] * matrix2[ 1 ][ 2 ] +
							matrix1[ 2 ][ 2 ] * matrix2[ 2 ][ 2 ];
}


// calculate concatenation; don't overwrite object matrix ---------------------
//
Transform2 operator *( const Transform2& trafo1, const Transform2& trafo2 )
{
	Transform2 temp;
	mat3x3_mul( temp.m_matrix, trafo1.m_matrix, trafo2.m_matrix );
	return temp;
}


// calculate concatenation as new object matrix -------------------------------
//
Transform2& Transform2::Concat( const Transform2& cattrafo )
{
	Transform2 temp;
	mat3x3_mul( temp.m_matrix, (const double(*)[3]) m_matrix, cattrafo.m_matrix );
	memcpy( m_matrix, temp.m_matrix, sizeof( m_matrix ) );
	return *this;
}


// reversed concatenation -----------------------------------------------------
//
Transform2& Transform2::ConcatR( const Transform2& cattrafo )
{
	Transform2 temp;
	mat3x3_mul( temp.m_matrix, cattrafo.m_matrix, (const double(*)[3]) m_matrix );
	memcpy( m_matrix, temp.m_matrix, sizeof( m_matrix ) );
	return *this;
}


// transform Vector2 by matrix (homogeneous component is also evaluated!) -----
//
Vector2 Transform2::TransformVector2( const Vector2& vec ) const
{
	Vector2 temp;
	temp.setX( m_matrix[ 0 ][ 0 ] * vec.getX() + m_matrix[ 1 ][ 0 ] * vec.getY() + m_matrix[ 2 ][ 0 ] * vec.getW() );
	temp.setY( m_matrix[ 0 ][ 1 ] * vec.getX() + m_matrix[ 1 ][ 1 ] * vec.getY() + m_matrix[ 2 ][ 1 ] * vec.getW() );
	temp.setW( m_matrix[ 0 ][ 2 ] * vec.getX() + m_matrix[ 1 ][ 2 ] * vec.getY() + m_matrix[ 2 ][ 2 ] * vec.getW() );
	return temp;
}


// calc determinant of matrix -------------------------------------------------
//
double Transform2::Determinant()
{
	// calc determinant using rule of sarrus
	double pos;
	pos  = m_matrix[ 0 ][ 0 ] * m_matrix[ 1 ][ 1 ] * m_matrix[ 2 ][ 2 ];
	pos += m_matrix[ 0 ][ 1 ] * m_matrix[ 1 ][ 2 ] * m_matrix[ 2 ][ 0 ];
	pos += m_matrix[ 0 ][ 2 ] * m_matrix[ 1 ][ 0 ] * m_matrix[ 2 ][ 1 ];
	double neg;
	neg  = m_matrix[ 0 ][ 0 ] * m_matrix[ 1 ][ 2 ] * m_matrix[ 2 ][ 1 ];
	neg += m_matrix[ 0 ][ 1 ] * m_matrix[ 1 ][ 0 ] * m_matrix[ 2 ][ 2 ];
	neg += m_matrix[ 0 ][ 2 ] * m_matrix[ 1 ][ 1 ] * m_matrix[ 2 ][ 0 ];

	return ( pos - neg );
}


// calc matrix inverse --------------------------------------------------------
//
int Transform2::Inverse( Transform2& inverse )
{
	// calculate determinant
	double det = Determinant();
	// check for singularity
	if ( fabs( det ) < EPS_DENOM_ZERO )
		return 0;

	// calculate inverse
	double detinv = 1 / det;
	inverse.m_matrix[ 0 ][ 0 ] = ( m_matrix[ 1 ][ 1 ] * m_matrix[ 2 ][ 2 ] - m_matrix[ 1 ][ 2 ] * m_matrix[ 2 ][ 1 ] ) * detinv;
	inverse.m_matrix[ 0 ][ 1 ] = ( m_matrix[ 0 ][ 2 ] * m_matrix[ 2 ][ 1 ] - m_matrix[ 0 ][ 1 ] * m_matrix[ 2 ][ 2 ] ) * detinv;
	inverse.m_matrix[ 0 ][ 2 ] = ( m_matrix[ 0 ][ 1 ] * m_matrix[ 1 ][ 2 ] - m_matrix[ 0 ][ 2 ] * m_matrix[ 1 ][ 1 ] ) * detinv;
	inverse.m_matrix[ 1 ][ 0 ] = ( m_matrix[ 1 ][ 2 ] * m_matrix[ 2 ][ 0 ] - m_matrix[ 1 ][ 0 ] * m_matrix[ 2 ][ 2 ] ) * detinv;
	inverse.m_matrix[ 1 ][ 1 ] = ( m_matrix[ 0 ][ 0 ] * m_matrix[ 2 ][ 2 ] - m_matrix[ 0 ][ 2 ] * m_matrix[ 2 ][ 0 ] ) * detinv;
	inverse.m_matrix[ 1 ][ 2 ] = ( m_matrix[ 0 ][ 2 ] * m_matrix[ 1 ][ 0 ] - m_matrix[ 0 ][ 0 ] * m_matrix[ 1 ][ 2 ] ) * detinv;
	inverse.m_matrix[ 2 ][ 0 ] = ( m_matrix[ 1 ][ 0 ] * m_matrix[ 2 ][ 1 ] - m_matrix[ 1 ][ 1 ] * m_matrix[ 2 ][ 0 ] ) * detinv;
	inverse.m_matrix[ 2 ][ 1 ] = ( m_matrix[ 0 ][ 1 ] * m_matrix[ 2 ][ 0 ] - m_matrix[ 0 ][ 0 ] * m_matrix[ 2 ][ 1 ] ) * detinv;
	inverse.m_matrix[ 2 ][ 2 ] = ( m_matrix[ 0 ][ 0 ] * m_matrix[ 1 ][ 1 ] - m_matrix[ 0 ][ 1 ] * m_matrix[ 1 ][ 0 ] ) * detinv;

	return 1;
}


// fetch translation part of matrix -------------------------------------------
//
Vector2 Transform2::FetchTranslation() const
{
	// return translation vector
	return Vector2( m_matrix[ 2 ][ 0 ], m_matrix[ 2 ][ 1 ] );
}


// extract translation part of matrix; set to NULL translation afterwards -----
//
Vector2 Transform2::ExtractTranslation()
{
	// create translation vector
	Vector2 temp( m_matrix[ 2 ][ 0 ], m_matrix[ 2 ][ 1 ] );
	// zero translation part of matrix
	m_matrix[ 2 ][ 0 ] = 0.0;
	m_matrix[ 2 ][ 1 ] = 0.0;
	return temp;
}


// create rotation matrix -----------------------------------------------------
//
Transform2&	Transform2::LoadRotation( double angle )
{
	// init matrix
	LoadIdentity();

	// set rotated basis
	m_matrix[ 0 ][ 0 ] = cos( angle );
	m_matrix[ 0 ][ 1 ] = sin( angle );
	m_matrix[ 1 ][ 0 ] = -m_matrix[ 0 ][ 1 ];
	m_matrix[ 1 ][ 1 ] = m_matrix[ 0 ][ 0 ];

	return *this;
}


// create scale matrix --------------------------------------------------------
//
Transform2& Transform2::LoadScale( double x, double y )
{
	LoadIdentity();
	m_matrix[ 0 ][ 0 ] = x;
	m_matrix[ 1 ][ 1 ] = y;
	return *this;
}


// create translation matrix --------------------------------------------------
//
Transform2& Transform2::LoadTranslation( double x, double y )
{
	LoadIdentity();
	m_matrix[ 2 ][ 0 ] = x;
	m_matrix[ 2 ][ 1 ] = y;
	return *this;
}


// rotate around arbitrary axis -----------------------------------------------
//
Transform2& Transform2::Rotate( double angle )
{
	Transform2 temp;
	temp.LoadRotation( angle );
	Concat( temp );
	return *this;
}


// rotate around arbitrary axis (reversed) ------------------------------------
//
Transform2& Transform2::RotateR( double angle )
{
	Transform2 temp;
	temp.LoadRotation( angle );
	ConcatR( temp );
	return *this;
}


// apply scale factors --------------------------------------------------------
//
Transform2& Transform2::Scale( double x, double y )
{
	Transform2 temp;
	temp.LoadScale( x, y );
	Concat( temp );
	return *this;
}


// apply scale factors (reversed) ---------------------------------------------
//
Transform2& Transform2::ScaleR( double x, double y )
{
	Transform2 temp;
	temp.LoadScale( x, y );
	ConcatR( temp );
	return *this;
}


// apply translation ----------------------------------------------------------
//
Transform2& Transform2::Translate( double x, double y )
{
	m_matrix[ 2 ][ 0 ] += x;
	m_matrix[ 2 ][ 1 ] += y;
	return *this;
}


// apply translation (reversed) -----------------------------------------------
//
Transform2& Transform2::TranslateR( double x, double y )
{
	Transform2 temp;
	temp.LoadTranslation( x, y );
	ConcatR( temp );
	return *this;
}


BSPLIB_NAMESPACE_END

//-----------------------------------------------------------------------------