Trans.cc 16.7 KB
Newer Older
Bill Lorensen's avatar
Bill Lorensen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*=========================================================================

  Program:   Visualization Library
  Module:    Trans.cc
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

This file is part of the Visualization Library. No part of this file or its
contents may be copied, reproduced or altered in any way without the express
written consent of the authors.

Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994

=========================================================================*/
#include <stdlib.h>
#include <math.h>
#include "Trans.hh"
Will Schroeder's avatar
Will Schroeder committed
19
#include "vlMath.hh"
Bill Lorensen's avatar
Bill Lorensen committed
20

Bill Lorensen's avatar
Bill Lorensen committed
21
// Description:
Will Schroeder's avatar
Will Schroeder committed
22
23
24
25
// Constructs a transform. Sets the following defaults
// preMultiplyFlag = 1
// stackSize = 10
// creates an identity matrix as the top matrix on the stack.
Bill Lorensen's avatar
Bill Lorensen committed
26

Bill Lorensen's avatar
Bill Lorensen committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
vlTransform::vlTransform ()
{
  // pre multiply is on
  this->PreMultiplyFlag = 1;
  // create a reasonable size stack
  this->StackSize = 10;
  // allocate the stack
  this->Stack = new vlMatrix4x4 *[this->StackSize];
  // put a matrix on the top
  *this->Stack = new vlMatrix4x4;
  // initialize the bottom of the stack
  this->StackBottom = this->Stack;
  // initialize current matrix to identity
  this->Identity ();

42
43
44
  this->Point[0] = this->Point[1] = this->Point[2] = this->Point[3] = 0.0;
  this->Orientation[0] = this->Orientation[1] = this->Orientation[2] = 0.0;

Bill Lorensen's avatar
Bill Lorensen committed
45
46
47
  this->Modified ();
}

Bill Lorensen's avatar
Bill Lorensen committed
48
// Description:
Will Schroeder's avatar
Will Schroeder committed
49
// Copy constructor
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
vlTransform::vlTransform (const vlTransform& t)
{
  int i;
  vlMatrix4x4 *stack;

  this->PreMultiplyFlag = t.PreMultiplyFlag;
  this->StackSize = t.StackSize;
  this->Stack = new vlMatrix4x4 *[this->StackSize];

  // now copy each matrix in the stack
  for (stack = *this->Stack, i = 0; i < this->StackSize; i++)
    {
    this->Stack[i] = new vlMatrix4x4(*(t.Stack[i]));
    }

  this->StackBottom = this->Stack + (this->StackSize - 1);
}

Bill Lorensen's avatar
Bill Lorensen committed
68
// Description:
Will Schroeder's avatar
Will Schroeder committed
69
70
// Deletes the transformation on the top of the stack and sets the top 
// to the next transformation on the stack.
Bill Lorensen's avatar
Bill Lorensen committed
71

Bill Lorensen's avatar
Bill Lorensen committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
void vlTransform::Pop ()
{
  // if we're at the bottom of the stack, don't pop
  if (this->Stack == this->StackBottom) return;

  // free the stack matrix storage
  delete *this->Stack;
  *this->Stack = 0;

  // update the stack
  this->Stack--;

  this->Modified ();
}

Bill Lorensen's avatar
Bill Lorensen committed
87
// Description:
Will Schroeder's avatar
Will Schroeder committed
88
89
90
91
// Sets the internal state of the transform to
// post multiply. All matrix subsequent matrix
// operations will occur after those already represented
// in the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
92
93
94
95
96
97
98
99
void vlTransform::PostMultiply ()
{
  if (this->PreMultiplyFlag != 0) {
    this->PreMultiplyFlag = 0;
    this->Modified ();
  }
}

Bill Lorensen's avatar
Bill Lorensen committed
100
// Description:
Will Schroeder's avatar
Will Schroeder committed
101
102
103
104
// Sets the internal state of the transform to
// pre multiply. All matrix subsequent matrix
// operations will occur before those already represented
// in the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
105
106
107
108
109
110
111
112
void vlTransform::PreMultiply ()
{
  if (this->PreMultiplyFlag != 1) {
    this->PreMultiplyFlag = 1;
    this->Modified ();
  }
}

Bill Lorensen's avatar
Bill Lorensen committed
113
// Description:
Will Schroeder's avatar
Will Schroeder committed
114
115
// Pushes the current transformation matrix onto the
// transformation stack.
Bill Lorensen's avatar
Bill Lorensen committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
void vlTransform::Push ()
{
  vlMatrix4x4 ctm;

  ctm = **this->Stack;
  this->Stack++;
  if ((this->Stack - this->StackBottom) > this->StackSize) {
    this->Stack--;
    return;
  }
  // allocate a new matrix on the stack

  *this->Stack = new vlMatrix4x4;

  // set the new matrix to the previous top of stack matrix
  **this->Stack = ctm;

  this->Modified ();
}

Bill Lorensen's avatar
Bill Lorensen committed
136
// Description:
Will Schroeder's avatar
Will Schroeder committed
137
138
// Creates an x rotation matrix andn concatenates it with 
// the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
139
140
141
void vlTransform::RotateX ( float angle)
{
  vlMatrix4x4 ctm;
Will Schroeder's avatar
Will Schroeder committed
142
143
144
  vlMath math;
  float radians = angle * math.DegreesToRadians();
  float cosAngle, sinAngle;
Bill Lorensen's avatar
Bill Lorensen committed
145
146

  if (angle != 0.0) {
Will Schroeder's avatar
Will Schroeder committed
147
148
    cosAngle = cos (radians);
    sinAngle = sin (radians);
Bill Lorensen's avatar
Bill Lorensen committed
149
150
151
152

    ctm = 0.0;

    ctm.Element[0][0] = 1.0;
Will Schroeder's avatar
Will Schroeder committed
153
154
155
156
    ctm.Element[1][1] =  cosAngle;
    ctm.Element[2][1] = -sinAngle;
    ctm.Element[1][2] =  sinAngle;
    ctm.Element[2][2] =  cosAngle;
Bill Lorensen's avatar
Bill Lorensen committed
157
158
159
160
161
162
163
    ctm.Element[3][3] = 1.0;

    // concatenate with current transformation matrix
    this->Concatenate (ctm);
  }
}

Bill Lorensen's avatar
Bill Lorensen committed
164
// Description:
Will Schroeder's avatar
Will Schroeder committed
165
// Rotate about y-axis
Bill Lorensen's avatar
Bill Lorensen committed
166
167
168
169
170
void vlTransform::RotateY ( float angle)
  //  Creates a y rotation matrix and concatenates it with 
  //  the current transformation matrix.
{
  vlMatrix4x4 ctm;
Will Schroeder's avatar
Will Schroeder committed
171
172
173
  vlMath math;
  float radians = angle * math.DegreesToRadians();
  float cosAngle, sinAngle;
Bill Lorensen's avatar
Bill Lorensen committed
174
175

  if (angle != 0.0) {
Will Schroeder's avatar
Will Schroeder committed
176
177
    cosAngle = cos (radians);
    sinAngle = sin (radians);
Bill Lorensen's avatar
Bill Lorensen committed
178
179
180

    ctm = 0.0;

Will Schroeder's avatar
Will Schroeder committed
181
    ctm.Element[0][0] = cosAngle;
Bill Lorensen's avatar
Bill Lorensen committed
182
    ctm.Element[1][1] = 1.0;
Will Schroeder's avatar
Will Schroeder committed
183
184
185
    ctm.Element[2][0] = sinAngle;
    ctm.Element[0][2] = -sinAngle;
    ctm.Element[2][2] = cosAngle;
Bill Lorensen's avatar
Bill Lorensen committed
186
187
188
189
190
191
192
    ctm.Element[3][3] = 1.0;

    // concatenate with current transformation matrix
    this->Concatenate (ctm);
  }
}

Bill Lorensen's avatar
Bill Lorensen committed
193
// Description:
Will Schroeder's avatar
Will Schroeder committed
194
// Rotate about y-axis
Bill Lorensen's avatar
Bill Lorensen committed
195
196
197
198
199
void vlTransform::RotateZ (float angle)
  //  Creates a z rotation matrix and concatenates it with 
  //  the current transformation matrix.
{
  vlMatrix4x4 ctm;
Will Schroeder's avatar
Will Schroeder committed
200
201
202
  vlMath math;
  float radians = angle * math.DegreesToRadians();
  float cosAngle, sinAngle;
Bill Lorensen's avatar
Bill Lorensen committed
203
204

  if (angle != 0.0) {
Will Schroeder's avatar
Will Schroeder committed
205
206
    cosAngle = cos (radians);
    sinAngle = sin (radians);
Bill Lorensen's avatar
Bill Lorensen committed
207
208
209

    ctm = 0.0;

Will Schroeder's avatar
Will Schroeder committed
210
211
212
213
    ctm.Element[0][0] =  cosAngle;
    ctm.Element[1][0] = -sinAngle;
    ctm.Element[0][1] =  sinAngle;
    ctm.Element[1][1] =  cosAngle;
Bill Lorensen's avatar
Bill Lorensen committed
214
215
216
217
218
219
220
221
    ctm.Element[2][2] = 1.0;
    ctm.Element[3][3] = 1.0;

    // concatenate with current transformation matrix
    this->Concatenate (ctm);
  }
}

Bill Lorensen's avatar
Bill Lorensen committed
222
// Description:
Will Schroeder's avatar
Will Schroeder committed
223
224
225
// Creates a matrix that rotates angle degrees about an axis
// through the origin and x, y, z. Then concatenates
// this matrix with the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
226
227
228
229
230
231
void vlTransform::RotateWXYZ ( float angle, float x, float y, float z)
{
  vlMatrix4x4 ctm;
  float   radians;
  float   w;
  float   quat[4];
Will Schroeder's avatar
Will Schroeder committed
232
233
234
  float   sinAngle;
  float   cosAngle;
  vlMath math;
Bill Lorensen's avatar
Bill Lorensen committed
235
236
237
238
239
240
241
242

  // build a rotation matrix and concatenate it
  quat[0] = angle;
  quat[1] = x;
  quat[2] = y;
  quat[3] = z;

  // convert degrees to radians
Will Schroeder's avatar
Will Schroeder committed
243
  radians = - quat[0] * math.DegreesToRadians() / 2;
Bill Lorensen's avatar
Bill Lorensen committed
244

Will Schroeder's avatar
Will Schroeder committed
245
246
  cosAngle = cos (radians);
  sinAngle = sin (radians);
Bill Lorensen's avatar
Bill Lorensen committed
247
248

  // normalize x, y, z
Will Schroeder's avatar
Will Schroeder committed
249
  if ( math.Normalize(quat+1) == 0.0 )
250
    {
Will Schroeder's avatar
Will Schroeder committed
251
    vlErrorMacro(<<"Trying to rotate around zero-length axis");
Bill Lorensen's avatar
Bill Lorensen committed
252
    return;
253
    }
Bill Lorensen's avatar
Bill Lorensen committed
254

Will Schroeder's avatar
Will Schroeder committed
255
  w = cosAngle;
256
257
258
  x = quat[1];
  y = quat[2];
  z = quat[3];
Bill Lorensen's avatar
Bill Lorensen committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

  // matrix calculation is taken from Ken Shoemake's
  // "Animation Rotation with Quaternion Curves",
  // Comput. Graphics, vol. 19, No. 3 , p. 253

  ctm.Element[0][0] = 1 - 2 * y * y - 2 * z * z;
  ctm.Element[1][1] = 1 - 2 * x * x - 2 * z * z;
  ctm.Element[2][2] = 1 - 2 * x * x - 2 * y * y;
  ctm.Element[1][0] =  2 * x * y + 2 * w * z;
  ctm.Element[2][0] =  2 * x * z - 2 * w * y;
  ctm.Element[0][1] =  2 * x * y - 2 * w * z;
  ctm.Element[2][1] =  2 * y * z + 2 * w * x;
  ctm.Element[0][2] =  2 * x * z + 2 * w * y;
  ctm.Element[1][2] =  2 * y * z - 2 * w * x;

  // concatenate with current transformation matrix
  this->Concatenate (ctm);
}

Bill Lorensen's avatar
Bill Lorensen committed
278
// Description:
Will Schroeder's avatar
Will Schroeder committed
279
// Scale in x, y, z directions using current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
280
281
282
283
284
285
286
287
288
void vlTransform::Scale ( float x, float y, float z)
{
  vlMatrix4x4 ctm;

  if (x != 1.0 || y != 1.0 || z != 1.0) {
    ctm = 0.0;

    ctm.Element[0][0] = x;
    if (ctm.Element[0][0] == 0.0) {
Will Schroeder's avatar
Will Schroeder committed
289
      vlErrorMacro(<< "scale: x scale is 0.0, reset to 1.0\n");
Bill Lorensen's avatar
Bill Lorensen committed
290
291
292
293
294
      ctm.Element[0][0] = 1.0;
    }

    ctm.Element[1][1] = y;
    if (ctm.Element[1][1] == 0.0) {
Will Schroeder's avatar
Will Schroeder committed
295
      vlErrorMacro(<<  "scale: y scale is 0.0, reset to 1.0\n");
Bill Lorensen's avatar
Bill Lorensen committed
296
297
298
299
300
      ctm.Element[1][1] = 1.0;
    }

    ctm.Element[2][2] = z;
    if (ctm.Element[2][2] == 0.0) {
Will Schroeder's avatar
Will Schroeder committed
301
      vlErrorMacro(<< "scale: z scale is 0.0, reset to 1.0\n");
Bill Lorensen's avatar
Bill Lorensen committed
302
303
304
305
306
307
308
309
310
311
      ctm.Element[2][2] = 1.0;
    }

    ctm.Element[3][3] = 1.0;

    // concatenate with current transformation matrix
    this->Concatenate (ctm);
  }
}

Bill Lorensen's avatar
Bill Lorensen committed
312
// Description:
Will Schroeder's avatar
Will Schroeder committed
313
// Translate in x, y, z directions using current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
void vlTransform::Translate ( float x, float y, float z)
{
  vlMatrix4x4 ctm;

  if (x != 0.0 || y != 0.0 || z != 0.0) {
    ctm = 0.0;

    ctm.Element[0][0] = 1.0;
    ctm.Element[1][1] = 1.0;
    ctm.Element[2][2] = 1.0;
    ctm.Element[3][3] = 1.0;

    ctm.Element[0][3] = x;
    ctm.Element[1][3] = y;
    ctm.Element[2][3] = z;

    // concatenate with current transformation matrix
    this->Concatenate (ctm);
  }
}

Bill Lorensen's avatar
Bill Lorensen committed
335
// Description:
Will Schroeder's avatar
Will Schroeder committed
336
// Obtain transpose of current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
337
338
339
340
341
342
343
344
345
346
347
348
349
void vlTransform::GetTranspose (vlMatrix4x4& (transpose))
{
  vlMatrix4x4 temp;
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      temp.Element[j][i] = (**this->Stack).Element[i][j];
    }
  }    
  transpose = temp;
}

Bill Lorensen's avatar
Bill Lorensen committed
350
// Description:
Will Schroeder's avatar
Will Schroeder committed
351
// Invert current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
352
353
354
355
356
357
358
void vlTransform::Inverse ()
{
  (**this->Stack).Invert (**this->Stack, **this->Stack);

  this->Modified ();
}

Bill Lorensen's avatar
Bill Lorensen committed
359
// Description:
Will Schroeder's avatar
Will Schroeder committed
360
// Return inverse of current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
361
362
363
364
365
void vlTransform::GetInverse ( vlMatrix4x4& inverse)
{
  inverse.Invert (**this->Stack, inverse);
}

Bill Lorensen's avatar
Bill Lorensen committed
366
// Description:
Will Schroeder's avatar
Will Schroeder committed
367
// Get the x, y, z orientation angles from transformation matrix.
Ken Martin's avatar
Ken Martin committed
368
float *vlTransform::GetOrientation ()
Bill Lorensen's avatar
Bill Lorensen committed
369
370
{
#define AXIS_EPSILON .01
Will Schroeder's avatar
Will Schroeder committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  float	scale_x, scale_y, scale_z;
  vlMatrix4x4  temp;
  float   x,y,z;
  float   d;
  float   d1;
  float   d2;
  float   dot;
  float   alpha;
  float   phi;
  float   theta;
  float   cos_phi, sin_phi;
  float   cos_theta, sin_theta;
  float   cos_alpha, sin_alpha;
  float   x2, y2, z2;
  float   x3, y3, z3;
  float   x3p, y3p;
  vlMath math;
Bill Lorensen's avatar
Bill Lorensen committed
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

  // copy the matrix into local storage

  temp = **this->Stack;

  // get scale factors

  this->GetScale (scale_x, scale_y, scale_z);

  // first rotate about y axis

  x2 = temp.Element[2][0] / scale_x;
  y2 = temp.Element[2][1] / scale_y;
  z2 = temp.Element[2][2] / scale_z;

  x3 = temp.Element[1][0] / scale_x;
  y3 = temp.Element[1][1] / scale_y;
  z3 = temp.Element[1][2] / scale_z;

  dot = x2 * x2 + z2 * z2;
  d1 = sqrt (dot);

  if (d1 < AXIS_EPSILON) {
    cos_theta = 1.0;
    sin_theta = 0.0;
  }
  else {
    cos_theta = z2 / d1;
    sin_theta = x2 / d1;
  }

  theta = atan2 (sin_theta, cos_theta);

Will Schroeder's avatar
Will Schroeder committed
421
  y = theta / math.DegreesToRadians();
Bill Lorensen's avatar
Bill Lorensen committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

  // now rotate about x axis

  dot = x2 * x2 + y2 * y2 + z2 * z2;
  d = sqrt (dot);

  if (d < AXIS_EPSILON) {
    sin_phi = 0.0;
    cos_phi = 1.0;
  }
  else if (d1 < AXIS_EPSILON) {
    sin_phi = y2 / d;
    cos_phi = z2 / d;
  }
  else {
    sin_phi = y2 / d;
    cos_phi = ( x2 * x2 + z2 * z2) / (d1 * d);
  }

  phi = atan2 (sin_phi, cos_phi);

Will Schroeder's avatar
Will Schroeder committed
443
  x = - phi / math.DegreesToRadians();
Bill Lorensen's avatar
Bill Lorensen committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462

  // finally, rotate about z

  x3p = x3 * cos_theta - z3 * sin_theta;
  y3p = - sin_phi * sin_theta * x3 + cos_phi * y3 - sin_phi * cos_theta * z3;
  dot = x3p * x3p + y3p * y3p;

  d2 = sqrt (dot);
  if (d2 < AXIS_EPSILON) {
    cos_alpha = 1.0;
    sin_alpha = 0.0;
  }
  else {
    cos_alpha = y3p / d2;
    sin_alpha = x3p / d2;
  }

  alpha = atan2 (sin_alpha, cos_alpha);

Will Schroeder's avatar
Will Schroeder committed
463
  z = - alpha / math.DegreesToRadians();
Ken Martin's avatar
Ken Martin committed
464
465
466
467
468
469

  this->Orientation[0] = x;
  this->Orientation[1] = y;
  this->Orientation[2] = z;

  return this->Orientation;
Bill Lorensen's avatar
Bill Lorensen committed
470
471
}

Bill Lorensen's avatar
Bill Lorensen committed
472
// Description:
Will Schroeder's avatar
Will Schroeder committed
473
// Return the x, y, z positions from the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
474
475
476
477
478
479
480
void vlTransform::GetPosition (float & x,float & y,float & z)
{
	x = (**this->Stack).Element[0][3];
	y = (**this->Stack).Element[1][3];
	z = (**this->Stack).Element[2][3];
}

Bill Lorensen's avatar
Bill Lorensen committed
481
// Description:
Will Schroeder's avatar
Will Schroeder committed
482
// Return the x, y, z scale factors of the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
void vlTransform::GetScale ( float & x, float & y, float & z)
{
  int	i;
  float	scale[3];
  vlMatrix4x4 temp;

  // copy the matrix into local storage

  temp = **this->Stack;

  // find scale factors

  for (i = 0; i < 3; i++) {
    scale[i] = sqrt (temp.Element[i][0] * temp.Element[i][0] +
	temp.Element[i][1] * temp.Element[i][1] +
	temp.Element[i][2] * temp.Element[i][2]);
  }
  x = scale[0];
  y = scale[1];
  z = scale[2];
}

Bill Lorensen's avatar
Bill Lorensen committed
505
// Description:
Will Schroeder's avatar
Will Schroeder committed
506
// Returns the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
507
508
509
510
511
vlMatrix4x4 & vlTransform::GetMatrix ()
{
  return **this->Stack;;
}

Will Schroeder's avatar
Will Schroeder committed
512
513
514
515
516
517
518
// Description:
// Set the matrix directly.
void vlTransform::SetMatrix(vlMatrix4x4& m)
{
  **this->Stack = m;
}

Bill Lorensen's avatar
Bill Lorensen committed
519
// Description:
Will Schroeder's avatar
Will Schroeder committed
520
// Creates an identity matrix and makes it the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
521
522
523
void vlTransform::Identity ()
{
  vlMatrix4x4 ctm;
524
  int i;
Bill Lorensen's avatar
Bill Lorensen committed
525
526
527
528
529
530
531
532
533

  ctm = 0.0;

  for (i = 0; i < 4; i++) {
    ctm.Element[i][i] = 1.0;
  }
  **this->Stack = ctm;
}

Bill Lorensen's avatar
Bill Lorensen committed
534
// Description:
Will Schroeder's avatar
Will Schroeder committed
535
// Concatenates input matrix with the current transformation matrix.
Will Schroeder's avatar
Will Schroeder committed
536
// The resulting matrix becomes the new current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
537

Bill Lorensen's avatar
Bill Lorensen committed
538
539
540
541
542
543
544
545
546
547
548
void vlTransform::Concatenate (vlMatrix4x4 & matrix)
{
  if (this->PreMultiplyFlag) {
    this->Multiply4x4 (**this->Stack, matrix, **this->Stack);
  }
  else {
    this->Multiply4x4 (matrix, **this->Stack, **this->Stack);
  }
  this->Modified ();
}

Bill Lorensen's avatar
Bill Lorensen committed
549
// Description:
Will Schroeder's avatar
Will Schroeder committed
550
// Multiplies matrices a and b and stores result in c.
Bill Lorensen's avatar
Bill Lorensen committed
551

Bill Lorensen's avatar
Bill Lorensen committed
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
void vlTransform::Multiply4x4 ( vlMatrix4x4 & a, vlMatrix4x4 & b, vlMatrix4x4 & c)
{
  int i, j, k;
  vlMatrix4x4 result;

  result = 0.0;
  for (i = 0; i < 4; i++) {
    for (k = 0; k < 4; k++) {
      for (j = 0; j < 4; j++) {
        result.Element[i][k] += a.Element[i][j] * b.Element[j][k];
      }
    }
  }
  c = result;
}

Bill Lorensen's avatar
Bill Lorensen committed
568
// Description:
Will Schroeder's avatar
Will Schroeder committed
569
// Transposes the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
570

Bill Lorensen's avatar
Bill Lorensen committed
571
572
573
574
575
void vlTransform::Transpose ()
{
  this->GetTranspose (**this->Stack);
}

Bill Lorensen's avatar
Bill Lorensen committed
576
// Description:
Will Schroeder's avatar
Will Schroeder committed
577
// Returns the current transformation matrix.
Bill Lorensen's avatar
Bill Lorensen committed
578

Bill Lorensen's avatar
Bill Lorensen committed
579
580
581
582
583
void vlTransform::GetMatrix (vlMatrix4x4 & ctm)
{
  ctm = **this->Stack;
}

Bill Lorensen's avatar
Bill Lorensen committed
584
// Description:
Will Schroeder's avatar
Will Schroeder committed
585
// Destructor. Deletes all matrices on the stack and the stack
Bill Lorensen's avatar
Bill Lorensen committed
586

Bill Lorensen's avatar
Bill Lorensen committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
vlTransform::~vlTransform ()
{
  // delete all matrices on the stack

  while (this->Stack != this->StackBottom) this->Pop ();

  // delete the bottom matrix
  delete *this->Stack;

  // delet the stack itself
  delete this->Stack;
}

void vlTransform::PrintSelf (ostream& os, vlIndent indent)
{
602
        vlObject::PrintSelf(os, indent);
Bill Lorensen's avatar
Bill Lorensen committed
603

604
        os << indent << "Current Transformation:" << "\n";
Bill Lorensen's avatar
Bill Lorensen committed
605

606
        (**this->Stack).PrintSelf (os, indent.GetNextIndent());
Bill Lorensen's avatar
Bill Lorensen committed
607
608
}

Bill Lorensen's avatar
Bill Lorensen committed
609
// Description:
Will Schroeder's avatar
Will Schroeder committed
610
611
// Returns point transformed by the current transformation matrix. Point is
// expressed in homogeneous coordinates.
Will Schroeder's avatar
Will Schroeder committed
612
float *vlTransform::GetPoint()
613
{
Ken Martin's avatar
Ken Martin committed
614
615
616
  if (this->PreMultiplyFlag)
    {
    this->Stack[0]->Transpose();
Will Schroeder's avatar
Will Schroeder committed
617
    this->Stack[0]->PointMultiply(this->Point,this->Point);
Ken Martin's avatar
Ken Martin committed
618
619
620
621
    this->Stack[0]->Transpose();
    }
  else
    {
Will Schroeder's avatar
Will Schroeder committed
622
    this->Stack[0]->PointMultiply(this->Point,this->Point);
Ken Martin's avatar
Ken Martin committed
623
    }
Will Schroeder's avatar
Will Schroeder committed
624
625
626
  return this->Point;
}

Will Schroeder's avatar
Will Schroeder committed
627
628
629
630
631
632
void vlTransform::GetPoint(float p[4])
{
  float *x=this->vlTransform::GetPoint();
  for (int i=0; i<4; i++) p[i] = x[i];
}

Will Schroeder's avatar
Will Schroeder committed
633
// Description:
Will Schroeder's avatar
Will Schroeder committed
634
635
// Multiplies list of points (inPts) by current transformation matrix.
// Transformed points are appended to output list (outPts).
Will Schroeder's avatar
Will Schroeder committed
636
637
void vlTransform::MultiplyPoints(vlPoints *inPts, vlPoints *outPts)
{
Will Schroeder's avatar
Will Schroeder committed
638
  float newX[3];
Will Schroeder's avatar
Will Schroeder committed
639
640
641
642
643
644
645
  float *x;
  int ptId, i;
  int numPts = inPts->GetNumberOfPoints();

  for (ptId=0; ptId < numPts; ptId++)
    {
    x = inPts->GetPoint(ptId);
Will Schroeder's avatar
Will Schroeder committed
646
647
648
649
650
651
652
    for (i=0; i<3; i++)
      {
      newX[i] = (**this->Stack).Element[i][0] * x[0] +
                (**this->Stack).Element[i][1] * x[1] +
                (**this->Stack).Element[i][2] * x[2] +
                (**this->Stack).Element[i][3];
      }
Will Schroeder's avatar
Will Schroeder committed
653

Will Schroeder's avatar
Will Schroeder committed
654
    outPts->InsertNextPoint(newX);
Will Schroeder's avatar
Will Schroeder committed
655
656
657
    }
}

Will Schroeder's avatar
Will Schroeder committed
658
// Description:
Will Schroeder's avatar
Will Schroeder committed
659
660
// Multiplies list of vectors (inVectors) by current transformation matrix. 
// Transformed vectors are appended to output list (outVectors).
Will Schroeder's avatar
Will Schroeder committed
661
662
663
// Special multiplication since these are vectors. Multiplies vectors
// by the transposed inverse of the matrix, ignoring the translational
// components.
Will Schroeder's avatar
Will Schroeder committed
664

Will Schroeder's avatar
Will Schroeder committed
665
666
void vlTransform::MultiplyVectors(vlVectors *inVectors, vlVectors *outVectors)
{
Will Schroeder's avatar
Will Schroeder committed
667
  float newV[3];
Will Schroeder's avatar
Will Schroeder committed
668
669
670
  float *v;
  int ptId, i;
  int numVectors = inVectors->GetNumberOfVectors();
Will Schroeder's avatar
Will Schroeder committed
671
  vlMath math;
Will Schroeder's avatar
Will Schroeder committed
672
673
674
675
676
677
678
679

  this->Push();
  this->Inverse();
  this->Transpose();

  for (ptId=0; ptId < numVectors; ptId++)
    {
    v = inVectors->GetVector(ptId);
Will Schroeder's avatar
Will Schroeder committed
680
681
682
683
684
685
    for (i=0; i<3; i++)
      {
      newV[i] = (**this->Stack).Element[i][0] * v[0] +
                (**this->Stack).Element[i][1] * v[1] +
                (**this->Stack).Element[i][2] * v[2];
      }
Will Schroeder's avatar
Will Schroeder committed
686

Will Schroeder's avatar
Will Schroeder committed
687
    math.Normalize(newV);
Will Schroeder's avatar
Will Schroeder committed
688
    outVectors->InsertNextVector(newV);
Will Schroeder's avatar
Will Schroeder committed
689
690
691
692
    }
  this->Pop();
}

Will Schroeder's avatar
Will Schroeder committed
693
// Description:
Will Schroeder's avatar
Will Schroeder committed
694
695
// Multiplies list of normals (inNormals) by current transformation matrix.
// Transformed normals are appended to output list (outNormals).
Will Schroeder's avatar
Will Schroeder committed
696
697
698
// Special multiplication since these are vectors. Multiplies vectors
// by the transposed inverse of the matrix, ignoring the translational
// components.
Will Schroeder's avatar
Will Schroeder committed
699

Will Schroeder's avatar
Will Schroeder committed
700
701
void vlTransform::MultiplyNormals(vlNormals *inNormals, vlNormals *outNormals)
{
Will Schroeder's avatar
Will Schroeder committed
702
  float newN[3];
Will Schroeder's avatar
Will Schroeder committed
703
704
705
  float *n;
  int ptId, i;
  int numNormals = inNormals->GetNumberOfNormals();
Will Schroeder's avatar
Will Schroeder committed
706
  vlMath math;
Will Schroeder's avatar
Will Schroeder committed
707
708
709
710
711
712
713
714

  this->Push();
  this->Inverse();
  this->Transpose();

  for (ptId=0; ptId < numNormals; ptId++)
    {
    n = inNormals->GetNormal(ptId);
Will Schroeder's avatar
Will Schroeder committed
715
716
717
718
719
720
    for (i=0; i<3; i++)
      {
      newN[i] = (**this->Stack).Element[i][0] * n[0] +
                (**this->Stack).Element[i][1] * n[1] +
                (**this->Stack).Element[i][2] * n[2];
      }
Will Schroeder's avatar
Will Schroeder committed
721

Will Schroeder's avatar
Will Schroeder committed
722
    math.Normalize(newN);
Will Schroeder's avatar
Will Schroeder committed
723
    outNormals->InsertNextNormal(newN);
Will Schroeder's avatar
Will Schroeder committed
724
725
    }
  this->Pop();
726
}