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

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

Added original make3d

File size: 12.3 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 "Matrix4f.h"
23#include "defines.h"
24#include <math.h>
25#include <unistd.h>
26#include <stdio.h>
27
28/* #include "NR.h" */
29
30void
31Matrix4f::setValue(float *a)
32{
33    m[0][0] = a[0];   m[0][1] = a[1];   m[0][2] = a[2];   m[0][3] = a[3];
34    m[1][0] = a[4];   m[1][1] = a[5];   m[1][2] = a[6];   m[1][3] = a[7];
35    m[2][0] = a[8];   m[2][1] = a[9];   m[2][2] = a[10];  m[2][3] = a[11];
36    m[3][0] = a[12];  m[3][1] = a[13];  m[3][2] = a[14];  m[3][3] = a[15];
37}
38
39void
40Matrix4f::getValue(float *a)
41{
42    a[0] = m[0][0];   a[1] = m[0][1];   a[2] = m[0][2];   a[3] = m[0][3];
43    a[4] = m[1][0];   a[5] = m[1][1];   a[6] = m[1][2];   a[7] = m[1][3];
44    a[8] = m[2][0];   a[9] = m[2][1];   a[10] = m[2][2];  a[11] = m[2][3];
45    a[12] = m[3][0];  a[13] = m[3][1];  a[14] = m[3][2];  a[15] = m[3][3];
46}
47
48void
49Matrix4f::setValue(float a[][4])
50{
51    m[0][0] = a[0][0];  m[0][1] = a[0][1];  m[0][2] = a[0][2];
52    m[0][3] = a[0][3];
53
54    m[1][0] = a[1][0];  m[1][1] = a[1][1];  m[1][2] = a[1][2];
55    m[1][3] = a[1][3];
56
57    m[2][0] = a[2][0];  m[2][1] = a[2][1];  m[2][2] = a[2][2];
58    m[2][3] = a[2][3];
59
60    m[3][0] = a[3][0];  m[3][1] = a[3][1];  m[3][2] = a[3][2];
61    m[3][3] = a[3][3];
62}
63
64void
65Matrix4f::setValue(const Matrix4f &a)
66{
67    m[0][0] = a.m[0][0];  m[0][1] = a.m[0][1];  m[0][2] = a.m[0][2];
68    m[0][3] = a.m[0][3];
69
70    m[1][0] = a.m[1][0];  m[1][1] = a.m[1][1];  m[1][2] = a.m[1][2];
71    m[1][3] = a.m[1][3];
72
73    m[2][0] = a.m[2][0];  m[2][1] = a.m[2][1];  m[2][2] = a.m[2][2];
74    m[2][3] = a.m[2][3];
75
76    m[3][0] = a.m[3][0];  m[3][1] = a.m[3][1];  m[3][2] = a.m[3][2];
77    m[3][3] = a.m[3][3];
78}
79
80void
81Matrix4f::setValue(float *a0, float *a1, float *a2, float *a3)
82{
83    m[0][0] = a0[0];  m[0][1] = a0[1];  m[0][2] = a0[2];  m[0][3] = a0[3];
84    m[1][0] = a1[0];  m[1][1] = a1[1];  m[1][2] = a1[2];  m[1][3] = a1[3];
85    m[2][0] = a2[0];  m[2][1] = a2[1];  m[2][2] = a2[2];  m[2][3] = a2[3];
86    m[3][0] = a3[0];  m[3][1] = a3[1];  m[3][2] = a3[2];  m[3][3] = a3[3];
87}
88
89void
90Matrix4f::setValue(float a00, float a01, float a02, float a03,
91                   float a10, float a11, float a12, float a13,
92                   float a20, float a21, float a22, float a23,
93                   float a30, float a31, float a32, float a33)
94{
95    m[0][0] = a00;  m[0][1] = a01;  m[0][2] = a02;  m[0][3] = a03;
96    m[1][0] = a10;  m[1][1] = a11;  m[1][2] = a12;  m[1][3] = a13;
97    m[2][0] = a20;  m[2][1] = a21;  m[2][2] = a22;  m[2][3] = a23;
98    m[3][0] = a30;  m[3][1] = a31;  m[3][2] = a32;  m[3][3] = a33;
99}
100
101
102void
103Matrix4f::multVec(const Vec3f &src, Vec3f &dst)
104{
105    dst.setValue(m[0][0] * src[0] + m[0][1] * src[1] 
106                 + m[0][2] * src[2] + m[0][3],
107                 m[1][0] * src[0] + m[1][1] * src[1] 
108                 + m[1][2] * src[2] + m[1][3],
109                 m[2][0] * src[0] + m[2][1] * src[1] 
110                 + m[2][2] * src[2] + m[2][3]);
111}
112
113void
114Matrix4f::multVecHomog(const Vec3f &src, Vec3f &dst)
115{
116    float w;
117
118    dst.setValue(m[0][0] * src[0] + m[0][1] * src[1] 
119                 + m[0][2] * src[2] + m[0][3],
120                 m[1][0] * src[0] + m[1][1] * src[1] 
121                 + m[1][2] * src[2] + m[1][3],
122                 m[2][0] * src[0] + m[2][1] * src[1] 
123                 + m[2][2] * src[2] + m[2][3]);
124
125    w = m[3][0]*src[0] + m[3][1]*src[1] + m[3][2]*src[2] + m[3][3]; 
126
127    dst /= w;
128}
129
130void
131Matrix4f::multDir(const Vec3f& src, Vec3f &dst)
132{
133    dst.setValue(m[0][0] * src[0] + m[0][1] * src[1] + m[0][2] * src[2],
134                 m[1][0] * src[0] + m[1][1] * src[1] + m[1][2] * src[2],
135                 m[2][0] * src[0] + m[2][1] * src[1] + m[2][2] * src[2]);
136}
137
138
139void
140Matrix4f::makeIdentity() 
141{
142    setValue(1,0,0,0,
143             0,1,0,0,
144             0,0,1,0,
145             0,0,0,1);
146}
147
148
149Matrix4f &
150Matrix4f::multRight(const Matrix4f &a)
151{
152    Matrix4f temp;
153
154    temp.clear();
155    for (int i = 0; i < 4; i++)
156        for (int j = 0; j < 4; j++)
157            for (int k = 0; k < 4; k++)
158                temp.m[i][j] += m[i][k] * a.m[k][j];
159
160    setValue(temp);
161
162    return *this;
163}
164
165
166Matrix4f & 
167Matrix4f::multLeft(const Matrix4f &a)
168{
169    Matrix4f temp;
170
171    temp.clear();
172    for (int i = 0; i < 4; i++)
173        for (int j = 0; j < 4; j++)
174            for (int k = 0; k < 4; k++)
175                temp.m[i][j] += a.m[i][k] * m[k][j];
176
177    setValue(temp);   
178
179    return *this;
180}
181
182
183Matrix4f & 
184Matrix4f::multLeft(const Matrix3f &a)
185{
186    Matrix4f temp;
187    int i;
188
189    temp.clear();
190    for (i = 0; i < 3; i++)
191        for (int j = 0; j < 3; j++)
192            for (int k = 0; k < 3; k++)
193                temp.m[i][j] += a.m[i][k] * m[k][j];
194   
195    for (i = 0; i < 4; i++) {
196        temp.m[3][i] = m[3][i];
197        temp.m[i][3] = m[i][3];
198    }
199
200    setValue(temp);   
201
202    return *this;
203}
204
205
206void 
207Matrix4f::setScale(float s)
208{
209    setScale(s, s, s);
210}
211
212
213void 
214Matrix4f::setScale(float sx, float sy, float sz)
215{
216    m[0][0] = sx; m[0][1] = 0; m[0][2] = 0;
217    m[1][0] = 0; m[1][1] = sy; m[1][2] = 0;
218    m[2][0] = 0; m[2][1] = 0; m[2][2] = sz;
219}
220
221
222void 
223Matrix4f::setScale(const Vec3f &s)
224{
225    setScale(s[0], s[1], s[2]);
226}
227
228
229void 
230Matrix4f::scale(float s)
231{
232    setScale(s, s, s);
233}
234
235
236void 
237Matrix4f::scale(float sx, float sy, float sz)
238{
239    m[0][0] *= sx; m[0][1] *= sx; m[0][2] *= sx; m[0][3] *= sx;
240    m[1][0] *= sy; m[1][1] *= sy; m[1][2] *= sy; m[1][3] *= sy;
241    m[2][0] *= sz; m[2][1] *= sz; m[2][2] *= sz; m[2][3] *= sz;
242}
243
244
245void 
246Matrix4f::scale(const Vec3f &s)
247{
248    scale(s[0], s[1], s[2]);
249}
250
251
252void 
253Matrix4f::rotateX(float theta) 
254{
255    Matrix4f temp;
256    temp.makeIdentity();
257    temp.setRotateX(theta);
258    multLeft(temp);
259}
260
261void 
262Matrix4f::rotateY(float theta) 
263{
264    Matrix4f temp;
265    temp.makeIdentity();
266    temp.setRotateY(theta);
267    multLeft(temp);
268}
269
270void 
271Matrix4f::rotateZ(float theta) 
272{
273    Matrix4f temp;
274    temp.makeIdentity();
275    temp.setRotateZ(theta);
276    multLeft(temp);
277}
278
279
280void 
281Matrix4f::setRotateX(float theta) 
282{
283    m[0][0] = 1;            m[0][1] = 0;            m[0][2] = 0;
284    m[1][0] = 0;            m[1][1] = cos(theta);   m[1][2] = -sin(theta);
285    m[2][0] = 0;            m[2][1] = sin(theta);   m[2][2] = cos(theta);
286}
287
288void 
289Matrix4f::setRotateY(float theta) 
290{
291    m[0][0] = cos(theta);   m[0][1] = 0;            m[0][2] = sin(theta);
292    m[1][0] = 0;            m[1][1] = 1;            m[1][2] = 0;
293    m[2][0] = -sin(theta);  m[2][1] = 0;            m[2][2] = cos(theta);
294}
295
296void 
297Matrix4f::setRotateZ(float theta) 
298{
299    m[0][0] = cos(theta);   m[0][1] = -sin(theta);  m[0][2] = 0;
300    m[1][0] = sin(theta);   m[1][1] = cos(theta);   m[1][2] = 0;
301    m[2][0] = 0;            m[2][1] = 0;            m[2][2] = 1;
302}
303
304void 
305Matrix4f::setTranslate(Vec3f t) 
306{
307    setTranslate(t[0], t[1], t[2]);
308}
309
310
311void 
312Matrix4f::setTranslate(float tx, float ty, float tz) 
313{
314    m[0][3] = tx;
315    m[1][3] = ty;
316    m[2][3] = tz;
317}
318
319
320void 
321Matrix4f::translate(Vec3f t) 
322{
323    translate(t[0], t[1], t[2]);
324}
325
326
327void 
328Matrix4f::translate(float tx, float ty, float tz) 
329{
330    m[0][3] += tx;
331    m[1][3] += ty;
332    m[2][3] += tz;
333}
334
335
336
337Matrix4f
338Matrix4f::inverse() const 
339{
340   Matrix4f result;
341   int i, j, k;
342   double temp;
343   double bigm[8][4];
344   /*   Declare identity matrix   */
345
346   result.makeIdentity();
347   for (i = 0; i < 4; i++) {
348      for (j = 0;  j < 4;  j++) {
349         bigm[i][j] = m[i][j];
350         bigm[i+4][j] = result.m[i][j];
351      }
352   }
353
354   /*   Work across by columns   */
355   for (i = 0;  i < 4;  i++) {
356      for (j = i;  (bigm[i][j] == 0.0) && (j < 4);  j++)
357         ;
358      if (j == 4) {
359         fprintf (stderr, "error:  cannot do inverse matrix\n");
360         exit (2);
361      } 
362      else if (i != j) {
363         for (k = 0;  k < 8;  k++) {
364            temp = bigm[k][i];   
365            bigm[k][i] = bigm[k][j];   
366            bigm[k][j] = temp;
367         }
368      }
369     
370      /*   Divide original row   */     
371      for (j = 7;  j >= i;  j--)
372         bigm[j][i] /= bigm[i][i];
373     
374      /*   Subtract other rows   */     
375      for (j = 0;  j < 4;  j++)
376         if (i != j)
377            for (k = 7;  k >= i;  k--)
378               bigm[k][j] -= bigm[k][i] * bigm[i][j];
379   }
380   
381   for (i = 0;  i < 4;  i++)
382      for (j = 0;  j < 4;  j++)
383         result.m[i][j] = bigm[i+4][j];
384
385   return result;
386}
387
388
389void
390Matrix4f::transpose()
391{
392    float temp;
393
394    SWAP(m[0][1], m[1][0], temp);
395    SWAP(m[0][2], m[2][0], temp);
396    SWAP(m[0][3], m[3][0], temp);
397    SWAP(m[1][2], m[2][1], temp);
398    SWAP(m[1][3], m[3][1], temp);
399    SWAP(m[2][3], m[3][2], temp);
400}
401
402
403int
404Matrix4f::operator!=(const Matrix4f &a)
405{
406    if (m[0][0] != a.m[0][0]) return TRUE;
407    if (m[0][1] != a.m[0][1]) return TRUE;
408    if (m[0][2] != a.m[0][2]) return TRUE;
409    if (m[0][3] != a.m[0][3]) return TRUE;
410    if (m[1][0] != a.m[1][0]) return TRUE;
411    if (m[1][1] != a.m[1][1]) return TRUE;
412    if (m[1][2] != a.m[1][2]) return TRUE;
413    if (m[1][3] != a.m[1][3]) return TRUE;
414    if (m[2][0] != a.m[2][0]) return TRUE;
415    if (m[2][1] != a.m[2][1]) return TRUE;
416    if (m[2][2] != a.m[2][2]) return TRUE;
417    if (m[2][3] != a.m[2][3]) return TRUE;
418    if (m[3][0] != a.m[3][0]) return TRUE;
419    if (m[3][1] != a.m[3][1]) return TRUE;
420    if (m[3][2] != a.m[3][2]) return TRUE;
421    if (m[3][3] != a.m[3][3]) return TRUE;
422
423    return FALSE;
424}
425
426int
427Matrix4f::operator==(const Matrix4f &a)
428{
429    if (m[0][0] != a.m[0][0]) return FALSE;
430    if (m[0][1] != a.m[0][1]) return FALSE;
431    if (m[0][2] != a.m[0][2]) return FALSE;
432    if (m[0][3] != a.m[0][3]) return FALSE;
433    if (m[1][0] != a.m[1][0]) return FALSE;
434    if (m[1][1] != a.m[1][1]) return FALSE;
435    if (m[1][2] != a.m[1][2]) return FALSE;
436    if (m[1][3] != a.m[1][3]) return FALSE;
437    if (m[2][0] != a.m[2][0]) return FALSE;
438    if (m[2][1] != a.m[2][1]) return FALSE;
439    if (m[2][2] != a.m[2][2]) return FALSE;
440    if (m[2][3] != a.m[2][3]) return FALSE;
441    if (m[3][0] != a.m[3][0]) return FALSE;
442    if (m[3][1] != a.m[3][1]) return FALSE;
443    if (m[3][2] != a.m[3][2]) return FALSE;
444    if (m[3][3] != a.m[3][3]) return FALSE;
445
446    return TRUE;
447}
448
449void
450Matrix4f::print()
451{
452    for (int i=0; i<4; i++) {
453        for (int j=0; j<4; j++) {
454            printf("%f\t", m[i][j]);
455        }
456        printf("\n");
457    }
458}
459
460void
461Matrix4f::read(FILE *fp)
462{
463   float a0, a1, a2, a3;
464
465   fscanf(fp, "%f %f %f %f", &a0, &a1, &a2, &a3);
466   this->setElem(0,0,a0);
467   this->setElem(0,1,a1);
468   this->setElem(0,2,a2);
469   this->setElem(0,3,a3);
470
471   fscanf(fp, "%f %f %f %f", &a0, &a1, &a2, &a3);
472   this->setElem(1,0,a0);
473   this->setElem(1,1,a1);
474   this->setElem(1,2,a2);
475   this->setElem(1,3,a3);
476
477   fscanf(fp, "%f %f %f %f", &a0, &a1, &a2, &a3);
478   this->setElem(2,0,a0);
479   this->setElem(2,1,a1);
480   this->setElem(2,2,a2);
481   this->setElem(2,3,a3);
482
483   fscanf(fp, "%f %f %f %f", &a0, &a1, &a2, &a3);
484   this->setElem(3,0,a0);
485   this->setElem(3,1,a1);
486   this->setElem(3,2,a2);
487   this->setElem(3,3,a3);
488}
489
490void
491Matrix4f::write(FILE *fp)
492{
493   float a0, a1, a2, a3;
494
495   a0 = this->elem(0,0);
496   a1 = this->elem(0,1);
497   a2 = this->elem(0,2);
498   a3 = this->elem(0,3);
499   fprintf(fp, "%f %f %f %f\n", a0, a1, a2, a3);
500
501   a0 = this->elem(1,0);
502   a1 = this->elem(1,1);
503   a2 = this->elem(1,2);
504   a3 = this->elem(1,3);
505   fprintf(fp, "%f %f %f %f\n", a0, a1, a2, a3);
506
507   a0 = this->elem(2,0);
508   a1 = this->elem(2,1);
509   a2 = this->elem(2,2);
510   a3 = this->elem(2,3);
511   fprintf(fp, "%f %f %f %f\n", a0, a1, a2, a3);
512
513   a0 = this->elem(3,0);
514   a1 = this->elem(3,1);
515   a2 = this->elem(3,2);
516   a3 = this->elem(3,3);
517   fprintf(fp, "%f %f %f %f\n", a0, a1, a2, a3);
518
519}
520
521
522/*
523Matrix4f
524Matrix4f::inverse() const
525{
526    Matrix4f temp, result;
527    int i;
528
529    temp.setValue(*this);
530    float *nrmat[4];
531
532    toNR(temp, nrmat);
533
534    // See NRC book
535    float d, col[4];
536    int indx[4];
537    ludcmp(nrmat-1, 4, indx-1, &d);
538    for (int j = 0; j < 4; j++) {
539        for (i = 0; i < 4; i++) col[i] = 0;
540        col[j] = 1.0;
541        lubksb(nrmat-1, 4, indx-1, col-1);
542        for(i = 0; i < 4; i++) result.m[i][j] = col[i];
543    }
544
545    return Matrix4f(result);
546}
547
548
549void
550Matrix4f::toNR(Matrix4f &in, float **nrmat) const
551{
552    nrmat[0] = in.m[0] - 1;
553    nrmat[1] = in.m[1] - 1;
554    nrmat[2] = in.m[2] - 1;
555    nrmat[3] = in.m[3] - 1;
556}
557
558
559void
560Matrix4f::freeNR(float **in) const
561{
562    free(in);
563}
564
565
566Matrix4f &
567Matrix4f::operator *=(const Matrix4f &m)
568{
569}
570
571
572friend Matrix4f
573Matrix4f::operator *(const Matrix4f &m1, const Matrix4f &m2)
574{
575}
576*/
Note: See TracBrowser for help on using the repository browser.