Mat4x4.cc 6.43 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*=========================================================================

  Program:   Visualization Library
  Module:    Mat4x4.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 "Mat4x4.hh"

vlMatrix4x4::vlMatrix4x4 ()
{
}
void vlMatrix4x4::operator= (float element)
{
  int i,j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      this->Element[i][j] = element;
    }
  }
  this->Modified ();
}
Ken Martin's avatar
Ken Martin committed
35

Ken Martin's avatar
Ken Martin committed
36
void vlMatrix4x4::VectorMultiply(float in[4],float result[4])
Ken Martin's avatar
Ken Martin committed
37
38
{
  int i;
Ken Martin's avatar
Ken Martin committed
39
40
41
42
  float v1 = in[0];
  float v2 = in[1];
  float v3 = in[2];
  float v4 = in[3];
Ken Martin's avatar
Ken Martin committed
43
44
45
46
  
  for (i = 0; i < 4; i++)
    {
    result[i] = 
Ken Martin's avatar
Ken Martin committed
47
48
49
50
      v1 * this->Element[0][i] +
      v2 * this->Element[1][i] +
      v3 * this->Element[2][i] +
      v4 * this->Element[3][i];
Ken Martin's avatar
Ken Martin committed
51
52
53
54
    }
  
}

Bill Lorensen's avatar
Bill Lorensen committed
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
void vlMatrix4x4::Invert (vlMatrix4x4 in,vlMatrix4x4 & out)
{
// Matrix Inversion
// by Richard Carling
// from "Graphics Gems", Academic Press, 1990

#define SMALL_NUMBER	1.e-9

// inverse( original_matrix, inverse_matrix )
// calculate the inverse of a 4x4 matrix

//     -1     
//     A  = ___1__ adjoint A
//         det A
//

  int i, j;
  double det;

  // calculate the 4x4 determinent
  // if the determinent is zero, 
  // then the inverse matrix is not unique.

  det = in.Determinant(in);
  if ( fabs( det ) < SMALL_NUMBER) {
Will Schroeder's avatar
Will Schroeder committed
80
    vlErrorMacro(<< "Singular matrix, no inverse! Determinant= " << det <<"\n");
Bill Lorensen's avatar
Bill Lorensen committed
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
    det = 0.0;
    return;
  }

  // calculate the adjoint matrix

  this->Adjoint( in, out );

  // scale the adjoint matrix to get the inverse

  for (i=0; i<4; i++)
    for(j=0; j<4; j++)
      out.Element[i][j] = out.Element[i][j] / det;
}
/*
 * double = det2x2( double a, double b, double c, double d )
 * 
 * calculate the determinent of a 2x2 matrix.
 */

static double det2x2( double a, double b, double c, double d)
{
    double ans;
    ans = a * d - b * c;
    return ans;
}

/*
 * double = det3x3(  a1, a2, a3, b1, b2, b3, c1, c2, c3 )
 * 
 * calculate the determinent of a 3x3 matrix
 * in the form
 *
 *     | a1,  b1,  c1 |
 *     | a2,  b2,  c2 |
 *     | a3,  b3,  c3 |
 */

static double det3x3( double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3 )
{
    double ans;

    ans = a1 * det2x2( b2, b3, c2, c3 )
        - b1 * det2x2( a2, a3, c2, c3 )
        + c1 * det2x2( a2, a3, b2, b3 );
    return ans;
}

float vlMatrix4x4::Determinant (vlMatrix4x4 & in)
/*
 * double = det4x4( matrix )
 * 
 * calculate the determinent of a 4x4 matrix.
 */
{
  double ans;
  double a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4;
  double det3x3();

  // assign to individual variable names to aid selecting
  //  correct elements

  a1 = in.Element[0][0]; b1 = in.Element[0][1]; 
  c1 = in.Element[0][2]; d1 = in.Element[0][3];

  a2 = in.Element[1][0]; b2 = in.Element[1][1]; 
  c2 = in.Element[1][2]; d2 = in.Element[1][3];

  a3 = in.Element[2][0]; b3 = in.Element[2][1]; 
  c3 = in.Element[2][2]; d3 = in.Element[2][3];

  a4 = in.Element[3][0]; b4 = in.Element[3][1]; 
  c4 = in.Element[3][2]; d4 = in.Element[3][3];

  ans = a1 * det3x3( b2, b3, b4, c2, c3, c4, d2, d3, d4)
      - b1 * det3x3( a2, a3, a4, c2, c3, c4, d2, d3, d4)
      + c1 * det3x3( a2, a3, a4, b2, b3, b4, d2, d3, d4)
      - d1 * det3x3( a2, a3, a4, b2, b3, b4, c2, c3, c4);
  return ans;
}

void vlMatrix4x4::Adjoint (vlMatrix4x4 & in,vlMatrix4x4 & out)
{
// 
//   adjoint( original_matrix, inverse_matrix )
// 
//     calculate the adjoint of a 4x4 matrix
//
//      Let  a   denote the minor determinant of matrix A obtained by
//           ij
//
//      deleting the ith row and jth column from A.
//
//                    i+j
//     Let  b   = (-1)    a
//          ij            ji
//
//    The matrix B = (b  ) is the adjoint of A
//                     ij
///

  double a1, a2, a3, a4, b1, b2, b3, b4;
  double c1, c2, c3, c4, d1, d2, d3, d4;
  double det3x3();

  // assign to individual variable names to aid
  // selecting correct values

  a1 = in.Element[0][0]; b1 = in.Element[0][1]; 
  c1 = in.Element[0][2]; d1 = in.Element[0][3];

  a2 = in.Element[1][0]; b2 = in.Element[1][1]; 
  c2 = in.Element[1][2]; d2 = in.Element[1][3];

  a3 = in.Element[2][0]; b3 = in.Element[2][1];
  c3 = in.Element[2][2]; d3 = in.Element[2][3];

  a4 = in.Element[3][0]; b4 = in.Element[3][1]; 
  c4 = in.Element[3][2]; d4 = in.Element[3][3];


  // row column labeling reversed since we transpose rows & columns

  out.Element[0][0]  =   det3x3( b2, b3, b4, c2, c3, c4, d2, d3, d4);
  out.Element[1][0]  = - det3x3( a2, a3, a4, c2, c3, c4, d2, d3, d4);
  out.Element[2][0]  =   det3x3( a2, a3, a4, b2, b3, b4, d2, d3, d4);
  out.Element[3][0]  = - det3x3( a2, a3, a4, b2, b3, b4, c2, c3, c4);
        
  out.Element[0][1]  = - det3x3( b1, b3, b4, c1, c3, c4, d1, d3, d4);
  out.Element[1][1]  =   det3x3( a1, a3, a4, c1, c3, c4, d1, d3, d4);
  out.Element[2][1]  = - det3x3( a1, a3, a4, b1, b3, b4, d1, d3, d4);
  out.Element[3][1]  =   det3x3( a1, a3, a4, b1, b3, b4, c1, c3, c4);
        
  out.Element[0][2]  =   det3x3( b1, b2, b4, c1, c2, c4, d1, d2, d4);
  out.Element[1][2]  = - det3x3( a1, a2, a4, c1, c2, c4, d1, d2, d4);
  out.Element[2][2]  =   det3x3( a1, a2, a4, b1, b2, b4, d1, d2, d4);
  out.Element[3][2]  = - det3x3( a1, a2, a4, b1, b2, b4, c1, c2, c4);
        
  out.Element[0][3]  = - det3x3( b1, b2, b3, c1, c2, c3, d1, d2, d3);
  out.Element[1][3]  =   det3x3( a1, a2, a3, c1, c2, c3, d1, d2, d3);
  out.Element[2][3]  = - det3x3( a1, a2, a3, b1, b2, b3, d1, d2, d3);
  out.Element[3][3]  =   det3x3( a1, a2, a3, b1, b2, b3, c1, c2, c3);
}

void vlMatrix4x4::operator= (vlMatrix4x4& source)
{
  int i, j;

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

void vlMatrix4x4::PrintSelf (ostream& os, vlIndent indent)
{
  int i, j;

  if (this->ShouldIPrint (vlMatrix4x4::GetClassName()))
    {
	  vlObject::PrintSelf(os, indent);

	  os << indent << "Elements:\n";
          for (i = 0; i < 4; i++) {
	    cout << indent << indent;
            for (j = 0; j < 4; j++) {
              cout << this->Element[i][j] << " ";
            }
	    cout << "\n";
          }
    }
}