source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/vrippack-0.31/src/linear/Matrix3f.cc @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 6.7 KB
Line 
1/*
2
3Brian Curless
4
5Computer Graphics Laboratory
6Stanford University
7
8---------------------------------------------------------------------
9
10Copyright (1997) The Board of Trustees of the Leland Stanford Junior
11University. Except for commercial resale, lease, license or other
12commercial transactions, permission is hereby given to use, copy,
13modify this software for academic purposes only.  No part of this
14software or any derivatives thereof may be used in the production of
15computer models for resale or for use in a commercial
16product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND
17CONCERNING THIS SOFTWARE.  No support is implied or provided.
18
19*/
20
21
22#include "defines.h"
23#include "Matrix3f.h"
24#include <math.h>
25#include <unistd.h>
26#include <stdio.h>
27
28/* #include "NR.h" */
29
30void
31Matrix3f::setValue(float a[][3])
32{
33    m[0][0] = a[0][0];  m[0][1] = a[0][1];  m[0][2] = a[0][2];
34    m[1][0] = a[1][0];  m[1][1] = a[1][1];  m[1][2] = a[1][2];
35    m[2][0] = a[2][0];  m[2][1] = a[2][1];  m[2][2] = a[2][2];
36}
37
38void
39Matrix3f::setValue(const Matrix3f &a)
40{
41    m[0][0] = a.m[0][0];  m[0][1] = a.m[0][1];  m[0][2] = a.m[0][2];
42    m[1][0] = a.m[1][0];  m[1][1] = a.m[1][1];  m[1][2] = a.m[1][2];
43    m[2][0] = a.m[2][0];  m[2][1] = a.m[2][1];  m[2][2] = a.m[2][2];
44}
45
46void
47Matrix3f::setValue(float *a0, float *a1, float *a2)
48{
49    m[0][0] = a0[0];  m[0][1] = a0[1];  m[0][2] = a0[2];
50    m[1][0] = a1[0];  m[1][1] = a1[1];  m[1][2] = a1[2];
51    m[2][0] = a2[0];  m[2][1] = a2[1];  m[2][2] = a2[2];
52}
53
54void
55Matrix3f::setValue(Vec3f a0, Vec3f a1, Vec3f a2)
56{
57    m[0][0] = a0[0];  m[0][1] = a0[1];  m[0][2] = a0[2];
58    m[1][0] = a1[0];  m[1][1] = a1[1];  m[1][2] = a1[2];
59    m[2][0] = a2[0];  m[2][1] = a2[1];  m[2][2] = a2[2];
60}
61
62void
63Matrix3f::setValue(float a00, float a01, float a02,
64                  float a10, float a11, float a12,
65                  float a20, float a21, float a22)
66{
67    m[0][0] = a00;  m[0][1] = a01;  m[0][2] = a02;
68    m[1][0] = a10;  m[1][1] = a11;  m[1][2] = a12;
69    m[2][0] = a20;  m[2][1] = a21;  m[2][2] = a22;
70}
71
72
73void
74Matrix3f::makeIdentity() 
75{
76    setValue(1,0,0,
77             0,1,0,
78             0,0,1);
79}
80
81
82Vec3f
83Matrix3f::mult(Vec3f& vec)
84{
85    return Vec3f(m[0][0] * vec[0] + m[0][1] * vec[1] + m[0][2] * vec[2],
86                 m[1][0] * vec[0] + m[1][1] * vec[1] + m[1][2] * vec[2],
87                 m[2][0] * vec[0] + m[2][1] * vec[1] + m[2][2] * vec[2]);
88}
89
90void 
91Matrix3f::setScale(float s)
92{
93    setScale(s, s, s);
94}
95
96
97void 
98Matrix3f::setScale(float sx, float sy, float sz)
99{
100    m[0][0] = sx; m[0][1] = 0; m[0][2] = 0;
101    m[1][0] = 0; m[1][1] = sy; m[1][2] = 0;
102    m[2][0] = 0; m[2][1] = 0; m[2][2] = sz;
103}
104
105
106void 
107Matrix3f::setScale(const Vec3f &s)
108{
109    setScale(s[0], s[1], s[2]);
110}
111
112
113Matrix3f & 
114Matrix3f::multLeft(const Matrix3f &a)
115{
116    Matrix3f temp;
117
118    temp.clear();
119    for (int i = 0; i < 3; i++)
120        for (int j = 0; j < 3; j++)
121            for (int k = 0; k < 3; k++)
122                temp.m[i][j] += a.m[i][k] * m[k][j];
123
124    setValue(temp);   
125
126    return *this;
127}
128
129Matrix3f&
130Matrix3f::operator+=(const Matrix3f &a)
131{
132    m[0][0] += a.m[0][0];  m[0][1] += a.m[0][1];  m[0][2] += a.m[0][2];
133    m[1][0] += a.m[1][0];  m[1][1] += a.m[1][1];  m[1][2] += a.m[1][2];
134    m[2][0] += a.m[2][0];  m[2][1] += a.m[2][1];  m[2][2] += a.m[2][2];
135    return *this;
136}
137
138
139Matrix3f&
140Matrix3f::operator*=(float a)
141{
142    m[0][0] *= a;  m[0][1] *= a;  m[0][2] *= a;
143    m[1][0] *= a;  m[1][1] *= a;  m[1][2] *= a;
144    m[2][0] *= a;  m[2][1] *= a;  m[2][2] *= a;
145    return *this;
146}
147
148
149int
150Matrix3f::operator!=(const Matrix3f &a)
151{
152    if (m[0][0] != a.m[0][0]) return TRUE;
153    if (m[0][1] != a.m[0][1]) return TRUE;
154    if (m[0][2] != a.m[0][2]) return TRUE;
155    if (m[1][0] != a.m[1][0]) return TRUE;
156    if (m[1][1] != a.m[1][1]) return TRUE;
157    if (m[1][2] != a.m[1][2]) return TRUE;
158    if (m[2][0] != a.m[2][0]) return TRUE;
159    if (m[2][1] != a.m[2][1]) return TRUE;
160    if (m[2][2] != a.m[2][2]) return TRUE;
161
162    return FALSE;
163}
164
165int
166Matrix3f::operator==(const Matrix3f &a)
167{
168    if (m[0][0] != a.m[0][0]) return FALSE;
169    if (m[0][1] != a.m[0][1]) return FALSE;
170    if (m[0][2] != a.m[0][2]) return FALSE;
171    if (m[1][0] != a.m[1][0]) return FALSE;
172    if (m[1][1] != a.m[1][1]) return FALSE;
173    if (m[1][2] != a.m[1][2]) return FALSE;
174    if (m[2][0] != a.m[2][0]) return FALSE;
175    if (m[2][1] != a.m[2][1]) return FALSE;
176    if (m[2][2] != a.m[2][2]) return FALSE;
177
178    return TRUE;
179}
180
181
182float
183Matrix3f::determinant()
184{
185    return 
186        m[0][0] * m[1][1] * m[2][2] +
187        m[0][1] * m[1][2] * m[2][0] +
188        m[0][2] * m[1][0] * m[2][1] -
189        m[0][2] * m[1][1] * m[2][0] - 
190        m[0][1] * m[1][0] * m[2][2] - 
191        m[0][0] * m[1][2] * m[2][1];
192}
193
194
195Matrix3f
196Matrix3f::inverse() const 
197{
198   Matrix3f result;
199   int i, j, k;
200   double temp;
201   double bigm[6][3];
202   /*   Declare identity matrix   */
203
204   result.makeIdentity();
205   for (i = 0; i < 3; i++) {
206      for (j = 0;  j < 3;  j++) {
207         bigm[i][j] = m[i][j];
208         bigm[i+3][j] = result.m[i][j];
209      }
210   }
211
212   /*   Work across by columns   */
213   for (i = 0;  i < 3;  i++) {
214      for (j = i;  (bigm[i][j] == 0.0) && (j < 3);  j++)
215         ;
216      if (j == 3) {
217         fprintf (stderr, "error:  cannot do inverse matrix\n");
218         exit (2);
219      } 
220      else if (i != j) {
221         for (k = 0;  k < 6;  k++) {
222            temp = bigm[k][i];   
223            bigm[k][i] = bigm[k][j];   
224            bigm[k][j] = temp;
225         }
226      }
227     
228      /*   Divide original row   */
229     
230      for (j = 5;  j >= i;  j--)
231         bigm[j][i] /= bigm[i][i];
232     
233      /*   Subtract other rows   */
234     
235      for (j = 0;  j < 3;  j++)
236         if (i != j)
237            for (k = 5;  k >= i;  k--)
238               bigm[k][j] -= bigm[k][i] * bigm[i][j];
239   }
240   
241   for (i = 0;  i < 3;  i++)
242      for (j = 0;  j < 3;  j++)
243         result.m[i][j] = bigm[i+3][j];
244
245   return result;
246}
247
248
249void
250Matrix3f::map(const Vec3f vin[3], const Vec3f vout[3])
251{
252/*
253    Vec3f x0, x1, x2;
254    Matrix3f A;
255    float *nrmat[3], d, x0[3], x1[3], x2[3];
256    int indx[3];
257
258    A.setValue(vin[0], vin[1], vin[2]);
259    toNR(A, nrmat);
260    ludcmp(nrmat-1, 3, indx-1, &d);
261
262    x0.setValue(vout[0][0], vout[1][0], vout[2][0]);
263    lubksb(nrmat-1, 3, indx-1, &x0[0] - 1);
264
265    x1.setValue(vout[0][1], vout[1][1], vout[2][1]);   
266    lubksb(nrmat-1, 3, indx-1, &x1[0] - 1);
267
268    x2.setValue(vout[0][2], vout[1][2], vout[2][2]);   
269    lubksb(nrmat-1, 3, indx-1, &x2[0] - 1);
270
271    setValue(x0, x1, x2);
272*/
273}
274
275
276/*
277
278Matrix3f
279Matrix3f::inverse() const
280{
281    Matrix3f temp, result;
282    int i;
283
284    temp.setValue(*this);
285    float *nrmat[3];
286
287    toNR(temp, nrmat);
288
289    // See NRC book
290    float d, col[3];
291    int indx[3];
292    ludcmp(nrmat-1, 3, indx-1, &d);
293    for (int j = 0; j < 3; j++) {
294        for (i = 0; i < 3; i++) col[i] = 0;
295        col[j] = 1.0;
296        lubksb(nrmat-1, 3, indx-1, col-1);
297        for(i = 0; i < 3; i++) result.m[i][j] = col[i];
298    }
299
300    return Matrix3f(result);
301}
302
303
304void
305Matrix3f::toNR(Matrix3f &in, float **nrmat) const
306{
307    nrmat[0] = in.m[0] - 1;
308    nrmat[1] = in.m[1] - 1;
309    nrmat[2] = in.m[2] - 1;
310}
311*/
312
Note: See TracBrowser for help on using the repository browser.