Menu

[487b0c]: / src / transformation.cpp  Maximize  Restore  History

Download this file

424 lines (333 with data), 11.9 kB

  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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*! \file transformation.cpp
* \brief Affine transformation
*/
/* Copyright (c) 2010-2012 Taneli Kalvas. All rights reserved.
*
* You can redistribute this software and/or modify it under the terms
* of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library (file "COPYING" included in the package);
* if not, write to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* If you have questions about your rights to use or distribute this
* software, please contact Berkeley Lab's Technology Transfer
* Department at TTD@lbl.gov. Other questions, comments and bug
* reports should be sent directly to the author via email at
* taneli.kalvas@jyu.fi.
*
* NOTICE. This software was developed under partial funding from the
* U.S. Department of Energy. As such, the U.S. Government has been
* granted for itself and others acting on its behalf a paid-up,
* nonexclusive, irrevocable, worldwide license in the Software to
* reproduce, prepare derivative works, and perform publicly and
* display publicly. Beginning five (5) years after the date
* permission to assert copyright is obtained from the U.S. Department
* of Energy, and subject to any subsequent five (5) year renewals,
* the U.S. Government is granted for itself and others acting on its
* behalf a paid-up, nonexclusive, irrevocable, worldwide license in
* the Software to reproduce, prepare derivative works, distribute
* copies to the public, perform publicly and display publicly, and to
* permit others to do so.
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include "transformation.hpp"
Transformation::Transformation()
{
x[0] = x[5] = x[10] = x[15] = 1.0;
x[1] = x[2] = x[3] = x[4] = x[6] = x[7] = x[8] = x[9]
= x[11] = x[12] = x[13] = x[14] = 0.0;
}
Transformation::Transformation( double x11, double x12, double x13, double x14,
double x21, double x22, double x23, double x24,
double x31, double x32, double x33, double x34,
double x41, double x42, double x43, double x44 )
{
x[0] = x11;
x[1] = x12;
x[2] = x13;
x[3] = x14;
x[4] = x21;
x[5] = x22;
x[6] = x23;
x[7] = x24;
x[8] = x31;
x[9] = x32;
x[10] = x33;
x[11] = x34;
x[12] = x41;
x[13] = x42;
x[14] = x43;
x[15] = x44;
}
Transformation::Transformation( const Transformation &m )
{
memcpy( x, m.x, 16*sizeof(double) );
}
Transformation::Transformation( std::istream &is )
{
for( int i = 0; i < 16; i++ )
x[i] = read_double( is );
}
Transformation::~Transformation()
{
}
double Transformation::determinant( void ) const
{
return( + x[3] * x[6] * x[9] * x[12]
- x[2] * x[7] * x[9] * x[12]
- x[3] * x[5] * x[10] * x[12]
+ x[1] * x[7] * x[10] * x[12]
+ x[2] * x[5] * x[11] * x[12]
- x[1] * x[6] * x[11] * x[12]
- x[3] * x[6] * x[8] * x[13]
+ x[2] * x[7] * x[8] * x[13]
+ x[3] * x[4] * x[10] * x[13]
- x[0] * x[7] * x[10] * x[13]
- x[2] * x[4] * x[11] * x[13]
+ x[0] * x[6] * x[11] * x[13]
+ x[3] * x[5] * x[8] * x[14]
- x[1] * x[7] * x[8] * x[14]
- x[3] * x[4] * x[9] * x[14]
+ x[0] * x[7] * x[9] * x[14]
+ x[1] * x[4] * x[11] * x[14]
- x[0] * x[5] * x[11] * x[14]
- x[2] * x[5] * x[8] * x[15]
+ x[1] * x[6] * x[8] * x[15]
+ x[2] * x[4] * x[9] * x[15]
- x[0] * x[6] * x[9] * x[15]
- x[1] * x[4] * x[10] * x[15]
+ x[0] * x[5] * x[10] * x[15] );
}
Transformation Transformation::inverse( void ) const
{
double idet = determinant();
if( idet == 0.0 )
throw( Error( ERROR_LOCATION, "can't invert matrix: zero determinant" ) );
idet = 1.0/idet;
Transformation ret( x[6]*x[11]*x[13] - x[7]*x[10]*x[13] + x[7]*x[9]*x[14] -
x[5]*x[11]*x[14] - x[6]*x[9]*x[15] + x[5]*x[10]*x[15],
x[3]*x[10]*x[13] - x[2]*x[11]*x[13] - x[3]*x[9]*x[14] +
x[1]*x[11]*x[14] + x[2]*x[9]*x[15] - x[1]*x[10]*x[15],
x[2]*x[7]*x[13] - x[3]*x[6]*x[13] + x[3]*x[5]*x[14] -
x[1]*x[7]*x[14] - x[2]*x[5]*x[15] + x[1]*x[6]*x[15],
x[3]*x[6]*x[9] - x[2]*x[7]*x[9] - x[3]*x[5]*x[10] +
x[1]*x[7]*x[10] + x[2]*x[5]*x[11] - x[1]*x[6]*x[11],
x[7]*x[10]*x[12] - x[6]*x[11]*x[12] - x[7]*x[8]*x[14] +
x[4]*x[11]*x[14] + x[6]*x[8]*x[15] - x[4]*x[10]*x[15],
x[2]*x[11]*x[12] - x[3]*x[10]*x[12] + x[3]*x[8]*x[14] -
x[0]*x[11]*x[14] - x[2]*x[8]*x[15] + x[0]*x[10]*x[15],
x[3]*x[6]*x[12] - x[2]*x[7]*x[12] - x[3]*x[4]*x[14] +
x[0]*x[7]*x[14] + x[2]*x[4]*x[15] - x[0]*x[6]*x[15],
x[2]*x[7]*x[8] - x[3]*x[6]*x[8] + x[3]*x[4]*x[10] -
x[0]*x[7]*x[10] - x[2]*x[4]*x[11] + x[0]*x[6]*x[11],
x[5]*x[11]*x[12] - x[7]*x[9]*x[12] + x[7]*x[8]*x[13] -
x[4]*x[11]*x[13] - x[5]*x[8]*x[15] + x[4]*x[9]*x[15],
x[3]*x[9]*x[12] - x[1]*x[11]*x[12] - x[3]*x[8]*x[13] +
x[0]*x[11]*x[13] + x[1]*x[8]*x[15] - x[0]*x[9]*x[15],
x[1]*x[7]*x[12] - x[3]*x[5]*x[12] + x[3]*x[4]*x[13] -
x[0]*x[7]*x[13] - x[1]*x[4]*x[15] + x[0]*x[5]*x[15],
x[3]*x[5]*x[8] - x[1]*x[7]*x[8] - x[3]*x[4]*x[9] +
x[0]*x[7]*x[9] + x[1]*x[4]*x[11] - x[0]*x[5]*x[11],
x[6]*x[9]*x[12] - x[5]*x[10]*x[12] - x[6]*x[8]*x[13] +
x[4]*x[10]*x[13] + x[5]*x[8]*x[14] - x[4]*x[9]*x[14],
x[1]*x[10]*x[12] - x[2]*x[9]*x[12] + x[2]*x[8]*x[13] -
x[0]*x[10]*x[13] - x[1]*x[8]*x[14] + x[0]*x[9]*x[14],
x[2]*x[5]*x[12] - x[1]*x[6]*x[12] - x[2]*x[4]*x[13] +
x[0]*x[6]*x[13] + x[1]*x[4]*x[14] - x[0]*x[5]*x[14],
x[1]*x[6]*x[8] - x[2]*x[5]*x[8] + x[2]*x[4]*x[9] -
x[0]*x[6]*x[9] - x[1]*x[4]*x[10] + x[0]*x[5]*x[10] );
ret *= idet;
return( ret );
}
Transformation Transformation::transpose( void ) const
{
Transformation ret( x[0], x[4], x[8], x[12],
x[1], x[5], x[9], x[13],
x[2], x[6], x[10], x[14],
x[3], x[7], x[11], x[15] );
return( ret );
}
const Transformation &Transformation::operator*=( double s )
{
for( size_t i = 0; i < 16; i++ )
x[i] *= s;
return( *this );
}
Transformation Transformation::operator*( const Transformation &m ) const
{
Transformation r;
r[ 0] = x[ 0]*m[ 0] + x[ 1]*m[ 4] + x[ 2]*m[ 8] + x[ 3]*m[12];
r[ 1] = x[ 0]*m[ 1] + x[ 1]*m[ 5] + x[ 2]*m[ 9] + x[ 3]*m[13];
r[ 2] = x[ 0]*m[ 2] + x[ 1]*m[ 6] + x[ 2]*m[10] + x[ 3]*m[14];
r[ 3] = x[ 0]*m[ 3] + x[ 1]*m[ 7] + x[ 2]*m[11] + x[ 3]*m[15];
r[ 4] = x[ 4]*m[ 0] + x[ 5]*m[ 4] + x[ 6]*m[ 8] + x[ 7]*m[12];
r[ 5] = x[ 4]*m[ 1] + x[ 5]*m[ 5] + x[ 6]*m[ 9] + x[ 7]*m[13];
r[ 6] = x[ 4]*m[ 2] + x[ 5]*m[ 6] + x[ 6]*m[10] + x[ 7]*m[14];
r[ 7] = x[ 4]*m[ 3] + x[ 5]*m[ 7] + x[ 6]*m[11] + x[ 7]*m[15];
r[ 8] = x[ 8]*m[ 0] + x[ 9]*m[ 4] + x[10]*m[ 8] + x[11]*m[12];
r[ 9] = x[ 8]*m[ 1] + x[ 9]*m[ 5] + x[10]*m[ 9] + x[11]*m[13];
r[10] = x[ 8]*m[ 2] + x[ 9]*m[ 6] + x[10]*m[10] + x[11]*m[14];
r[11] = x[ 8]*m[ 3] + x[ 9]*m[ 7] + x[10]*m[11] + x[11]*m[15];
r[12] = x[12]*m[ 0] + x[13]*m[ 4] + x[14]*m[ 8] + x[15]*m[12];
r[13] = x[12]*m[ 1] + x[13]*m[ 5] + x[14]*m[ 9] + x[15]*m[13];
r[14] = x[12]*m[ 2] + x[13]*m[ 6] + x[14]*m[10] + x[15]*m[14];
r[15] = x[12]*m[ 3] + x[13]*m[ 7] + x[14]*m[11] + x[15]*m[15];
return( r );
}
Vec4D Transformation::operator*( const Vec4D &v ) const
{
Vec4D r;
r[0] = x[0]*v[0] + x[1]*v[1] + x[2]*v[2] + x[3]*v[3];
r[1] = x[4]*v[0] + x[5]*v[1] + x[6]*v[2] + x[7]*v[3];
r[2] = x[8]*v[0] + x[9]*v[1] + x[10]*v[2] + x[11]*v[3];
r[3] = x[12]*v[0] + x[13]*v[1] + x[14]*v[2] + x[15]*v[3];
return( r );
}
Vec4D Transformation::operator%( const Vec4D &v ) const
{
Vec4D r;
r[0] = x[0]*v[0] + x[4]*v[1] + x[8]*v[2] + x[12]*v[3];
r[1] = x[1]*v[0] + x[5]*v[1] + x[9]*v[2] + x[13]*v[3];
r[2] = x[2]*v[0] + x[6]*v[1] + x[10]*v[2] + x[14]*v[3];
r[3] = x[3]*v[0] + x[7]*v[1] + x[11]*v[2] + x[15]*v[3];
return( r );
}
Vec3D Transformation::inv_transform_point( const Vec3D &xin ) const
{
Transformation t = this->inverse();
Vec4D r = t * Vec4D( xin[0], xin[1], xin[2], 1.0 );
return( Vec3D( r[0], r[1], r[2] ) );
}
Vec3D Transformation::inv_transform_vector( const Vec3D &xin ) const
{
Transformation t = this->inverse();
Vec4D r = t * Vec4D( xin[0], xin[1], xin[2], 0.0 );
return( Vec3D( r[0], r[1], r[2] ) );
}
void Transformation::reset( void )
{
Transformation t1 = unity();
*this = t1;
}
void Transformation::translate( const Vec3D &d )
{
*this = translation( d ) * (*this);
}
void Transformation::translate_before( const Vec3D &d )
{
*this = (*this) * translation( d );
}
void Transformation::scale( const Vec3D &s )
{
*this = scaling( s ) * (*this);
}
void Transformation::scale_before( const Vec3D &s )
{
*this = (*this) * scaling( s );
}
void Transformation::rotate_x( double a )
{
*this = rotation_x( a ) * (*this);
}
void Transformation::rotate_x_before( double a )
{
*this = (*this) * rotation_x( a );
}
void Transformation::rotate_y( double a )
{
*this = rotation_y( a ) * (*this);
}
void Transformation::rotate_y_before( double a )
{
*this = (*this) * rotation_y( a );
}
void Transformation::rotate_z( double a )
{
*this = rotation_z( a ) * (*this);
}
void Transformation::rotate_z_before( double a )
{
*this = (*this) * rotation_z( a );
}
Transformation Transformation::unity( void )
{
return( Transformation( 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 ) );
}
Transformation Transformation::translation( const Vec3D &d )
{
return( Transformation( 1, 0, 0, d[0],
0, 1, 0, d[1],
0, 0, 1, d[2],
0, 0, 0, 1 ) );
}
Transformation Transformation::scaling( const Vec3D &s )
{
return( Transformation( s[0], 0, 0, 0,
0, s[1], 0, 0,
0, 0, s[2], 0,
0, 0, 0, 1 ) );
}
Transformation Transformation::rotation_x( double a )
{
return( Transformation( 1, 0, 0, 0,
0, cos(a), -sin(a), 0,
0, sin(a), cos(a), 0,
0, 0, 0, 1 ) );
}
Transformation Transformation::rotation_y( double a )
{
return( Transformation( cos(a), 0, sin(a), 0,
0, 1, 0, 0,
-sin(a), 0, cos(a), 0,
0, 0, 0, 1 ) );
}
Transformation Transformation::rotation_z( double a )
{
return( Transformation( cos(a), -sin(a), 0, 0,
sin(a), cos(a), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 ) );
}
void Transformation::save( const std::string &filename ) const
{
std::ofstream os( filename.c_str(), std::ios_base::binary );
if( !os.good() )
throw( Error( ERROR_LOCATION, "couldn\'t open file \'" + filename + "\' for writing" ) );
save( os );
os.close();
}
void Transformation::save( std::ostream &os ) const
{
for( int i = 0; i < 16; i++ )
write_double( os, x[i] );
}
std::ostream &operator<<( std::ostream &os, const Transformation &t )
{
for( size_t i = 0; i < 4; i++ ) {
for( size_t j = 0; j < 4; j++ ) {
os << std::setw(12) << to_string(t.x[4*i+j]).substr(0,12) << " ";
}
os << "\n";
}
return( os );
}
void Transformation::debug_print( std::ostream &os ) const
{
std::cout << "**Transformation\n";
os << "x = \n";
os << *this << "\n";
}