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

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

Added original make3d

File size: 6.4 KB
Line 
1/*
2 * vect:
3 *      Functions to support operations on vectors and matrices.
4 *
5 * Original code from:
6 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
7 *
8 * Much mucking with by:
9 * Gavin Bell
10 */
11#include <stdio.h>
12#include "vect.h"
13
14float *
15vnew()
16{
17        register float *v;
18
19        v = (float *) malloc(sizeof(float)*3);
20        return v;
21}
22
23float *
24vclone(const float *v)
25{
26        register float *c;
27
28        c = vnew();
29        vcopy(v, c);
30        return c;
31}
32
33void
34vcopy(const float *v1, float *v2)
35{
36        register int i;
37        for (i = 0 ; i < 3 ; i++)
38                v2[i] = v1[i];
39}
40
41void
42vprint(const float *v)
43{
44        printf("x: %f y: %f z: %f\n",v[0],v[1],v[2]);
45}
46
47void
48vset(float *v, float x, float y, float z)
49{
50        v[0] = x;
51        v[1] = y;
52        v[2] = z;
53}
54
55void
56vzero(float *v)
57{
58        v[0] = 0.0;
59        v[1] = 0.0;
60        v[2] = 0.0;
61}
62
63void
64vnormal(float *v)
65{
66        vscale(v,1.0/vlength(v));
67}
68
69float
70vlength(const float *v)
71{
72        return sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
73}
74
75void
76vscale(float *v, float div)
77{
78        v[0] *= div;
79        v[1] *= div;
80        v[2] *= div;
81}
82
83void
84vmult(const float *src1, const float *src2, float *dst)
85{
86        dst[0] = src1[0] * src2[0];
87        dst[1] = src1[1] * src2[1];
88        dst[2] = src1[2] * src2[2];
89}
90
91void
92vadd(const float *src1, const float *src2, float *dst)
93{
94        dst[0] = src1[0] + src2[0];
95        dst[1] = src1[1] + src2[1];
96        dst[2] = src1[2] + src2[2];
97}
98
99void
100vsub(const float *src1, const float *src2, float *dst)
101{
102        dst[0] = src1[0] - src2[0];
103        dst[1] = src1[1] - src2[1];
104        dst[2] = src1[2] - src2[2];
105}
106
107void
108vhalf(const float *v1, const float *v2, float *half)
109{
110        float len;
111
112        vadd(v2,v1,half);
113        len = vlength(half);
114        if(len>0.0001)
115                vscale(half,1.0/len);
116        else
117                vcopy(v1, half);
118}
119
120float
121vdot(const float *v1, const float *v2)
122{
123        return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
124}
125
126void
127vcross(const float *v1, const float *v2, float *cross)
128{
129        float temp[3];
130
131        temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
132        temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
133        temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
134        vcopy(temp, cross);
135}
136
137void
138vdirection(const float *v1, float *dir)
139{
140        vcopy(v1, dir);
141        vnormal(dir);
142}
143
144void
145vreflect(const float *in, const float *mirror, float *out)
146{
147        float temp[3];
148
149        vcopy(mirror, temp);
150        vscale(temp,vdot(mirror,in));
151        vsub(temp,in,out);
152        vadd(temp,out,out);
153}
154
155void
156vmultmatrix(const Matrix m1, const Matrix m2, Matrix prod)
157{
158        register int row, col;
159        Matrix temp;
160
161        for(row=0 ; row<4 ; row++) 
162                for(col=0 ; col<4 ; col++)
163                        temp[row][col] = m1[row][0] * m2[0][col]
164                                                   + m1[row][1] * m2[1][col]
165                                                   + m1[row][2] * m2[2][col]
166                                                   + m1[row][3] * m2[3][col];
167        for(row=0 ; row<4 ; row++) 
168                for(col=0 ; col<4 ; col++)
169                prod[row][col] = temp[row][col];
170}
171
172void
173vtransform(const float *v, const Matrix mat, float *vt)
174{
175        float t[3];
176
177        t[0] = v[0]*mat[0][0] + v[1]*mat[1][0] + v[2]*mat[2][0] + mat[3][0];
178        t[1] = v[0]*mat[0][1] + v[1]*mat[1][1] + v[2]*mat[2][1] + mat[3][1];
179        t[2] = v[0]*mat[0][2] + v[1]*mat[1][2] + v[2]*mat[2][2] + mat[3][2];
180        vcopy(t, vt);
181}
182void
183vtransform4(const float *v, const Matrix mat, float *vt)
184{
185        float t[3];
186
187        t[0] = v[0]*mat[0][0] + v[1]*mat[1][0] + v[2]*mat[2][0] + mat[3][0];
188        t[1] = v[0]*mat[0][1] + v[1]*mat[1][1] + v[2]*mat[2][1] + mat[3][1];
189        t[2] = v[0]*mat[0][2] + v[1]*mat[1][2] + v[2]*mat[2][2] + mat[3][2];
190        vcopy(t, vt);
191        t[3] = v[0]*mat[0][3] + v[1]*mat[1][3] + v[2]*mat[2][3] + mat[3][3];
192        vt[3] = t[3];
193}
194
195Matrix idmatrix =
196{
197        { 1.0, 0.0, 0.0, 0.0,},
198        { 0.0, 1.0, 0.0, 0.0,},
199        { 0.0, 0.0, 1.0, 0.0,},
200        { 0.0, 0.0, 0.0, 1.0,},
201};
202
203void
204mcopy(const Matrix m1, Matrix m2)
205{
206        int i, j;
207        for (i = 0; i < 4; i++)
208                for (j = 0; j < 4; j++)
209                        m2[i][j] = m1[i][j];
210}
211
212void
213minvert(const Matrix mat, Matrix result)
214{
215        int i, j, k;
216        double temp;
217        double m[8][4];
218        /*   Declare identity matrix   */
219
220        mcopy(idmatrix, result);
221        for (i = 0;  i < 4;  i++) {
222                for (j = 0;  j < 4;  j++) {
223                        m[i][j] = mat[i][j];
224                        m[i+4][j] = result[i][j];
225                }
226        }
227
228        /*   Work across by columns   */
229
230        for (i = 0;  i < 4;  i++) {
231                for (j = i;  (m[i][j] == 0.0) && (j < 4);  j++)
232                                ;
233                if (j == 4) {
234                        fprintf (stderr, "error:  cannot do inverse matrix\n");
235                        exit (2);
236                } 
237                else if (i != j) {
238                        for (k = 0;  k < 8;  k++) {
239                                temp = m[k][i];   
240                                m[k][i] = m[k][j];   
241                                m[k][j] = temp;
242                        }
243                }
244
245                /*   Divide original row   */
246
247                for (j = 7;  j >= i;  j--)
248                        m[j][i] /= m[i][i];
249
250                /*   Subtract other rows   */
251
252                for (j = 0;  j < 4;  j++)
253                        if (i != j)
254                                for (k = 7;  k >= i;  k--)
255                                        m[k][j] -= m[k][i] * m[i][j];
256        }
257
258        for (i = 0;  i < 4;  i++)
259                for (j = 0;  j < 4;  j++)
260                                result[i][j] = m[i+4][j];
261}
262
263/*
264 * Get combined Model/View/Projection matrix, in any mmode
265 */
266void
267vgetmatrix(Matrix m)
268{
269        long mm;
270
271        mm = getmmode();
272
273        if (mm == MSINGLE)
274        {
275                getmatrix(m);
276        }
277        else
278        {
279                Matrix mp, mv;
280
281                mmode(MPROJECTION);
282                getmatrix(mp);
283                mmode(MVIEWING);
284                getmatrix(mv);
285
286                pushmatrix();   /* Multiply them together */
287                loadmatrix(mp);
288                multmatrix(mv);
289                getmatrix(m);
290                popmatrix();
291
292                mmode(mm);      /* Back into the mode we started in */
293        }
294}
295
296/*
297 * Gaussian Elimination with Scaled Column Pivoting
298 *
299 * copied out of the book by        Wade Olsen
300 *                                  Silicon Graphics
301 *                                  Feb. 12, 1990
302 */
303
304void
305linsolve(       
306        const float *eqs[],     /* System of eq's to solve */
307        int             n,              /* of size inmat[n][n+1] */
308        float   *x              /* Result float *or of size x[n] */
309)
310{
311        int             i, j, p;
312
313        float **a;
314
315        /* Allocate space to work in */
316        /* (avoid modifying the equations passed) */
317        a = (float **)malloc(sizeof(float *)*n);
318        for (i = 0; i < n; i++)
319        {
320                a[i] = (float *)malloc(sizeof(float)*(n+1));
321                bcopy(eqs[i], a[i], sizeof(float)*(n+1));
322        }
323
324
325        if (n == 1)
326        {               /* The simple single variable case */
327                x[0] = a[0][1] / a[0][0];
328                return;
329        }
330                                /* Gausian elimination process */       
331        for (i = 0; i < n -1; i++)
332        {
333
334                                /* find non-zero pivot row */
335                p = i;
336                while (a[p][i] == 0.0)
337                {
338                        p++;
339                        if (p == n)
340                        {
341                                printf("linsolv:  No unique solution exists.\n");
342                                exit(1);
343                        }
344                }
345                                /* row swap */
346                if (p != i)
347                {
348                        float *swap;
349
350                        swap = a[i];
351                        a[i] = a[p];
352                        a[p] = swap;
353                }
354                                /* row subtractions */
355                for (j = i + 1; j < n; j++)
356                {
357                        float mult      = a[j][i] / a[i][i];
358
359                        int     k;
360                        for (k = i + 1; k < n + 1; k++)
361                                a[j][k] -= mult * a[i][k];
362                }
363        }
364
365        if (a[n-1][n-1] == 0.0)
366        {
367                printf("linsolv:  No unique solution exists.\n");
368                exit(1);
369        }
370
371                                        /* backwards substitution */
372        x[n-1] = a[n-1][n] / a[n-1][n-1];
373        for (i = n -2; i > -1; i--)
374        {
375                float sum = a[i][n];
376
377                for (j = i + 1; j < n; j++)
378                        sum -= a[i][j] * x[j];
379
380                x[i] = sum / a[i][i];
381        }
382
383        /* Free working space */
384        for (i = 0; i < n; i++)
385        {
386                free(a[i]);
387        }
388        free(a);
389}
Note: See TracBrowser for help on using the repository browser.