ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/g3affine.c
Revision: 2.1
Committed: Wed Aug 12 23:07:59 2015 UTC (8 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Added Jan Wienold's evalglare to distribution

File Contents

# Content
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "g3affine.h"
5 #include "gbasic.h"
6 #include "g3sphere.h"
7
8 static g3AVec g3av_zero = {0.0,0.0,0.0,0.0};
9
10 static g3ATrans g3at_unit = {{1.0,0.0,0.0,0.0},
11 {0.0,1.0,0.0,0.0},
12 {0.0,0.0,1.0,0.0},
13 {0.0,0.0,0.0,1.0}};
14
15 static g3ATrans g3at_zero = {{0.0,0.0,0.0,0.0},
16 {0.0,0.0,0.0,0.0},
17 {0.0,0.0,0.0,0.0},
18 {0.0,0.0,0.0,0.0}};
19
20 void g3at_tounit(g3ATrans t)
21 {
22 g3at_copy(t,g3at_unit);
23 }
24
25 static int nr_gaussj(g3Float a[4][4],g3Float b[4])
26 {
27 int *indxc,*indxr,*ipiv;
28 int i,icol,irow,j,k,l,ll;
29 g3Float big,dum,pivinv;
30
31 indxc = (int*) calloc(4,sizeof(int));
32 indxr = (int*) calloc(4,sizeof(int));
33 ipiv = (int*) calloc(4,sizeof(int));
34
35 icol = irow = 0;
36 for (j=0;j<4;j++)
37 ipiv[j]=0;
38 for (i=0;i<4;i++) {
39 big=0.0;
40 for (j=0;j<4;j++)
41 if (ipiv[j] != 1)
42 for (k=0;k<4;k++) {
43 if (ipiv[k] == 0) {
44 if (fabs(a[j][k]) >= big) {
45 big = fabs(a[j][k]);
46 irow = j;
47 icol = k;
48 }
49 } else if (ipiv[k] > 1) {
50 return 0;
51 }
52 }
53 ++(ipiv[icol]);
54 if (irow != icol) {
55 for (l=0;l<4;l++)
56 GB_SWAP(a[irow][l],a[icol][l])
57 GB_SWAP(b[irow],b[icol])
58 }
59 indxr[i] = irow;
60 indxc[i] = icol;
61 if (a[icol][icol] == 0.0)
62 return 0;
63 pivinv = 1.0/a[icol][icol];
64 a[icol][icol] = 1.0;
65 for (l=0;l<4;l++)
66 a[icol][l] *= pivinv;
67 b[icol] *= pivinv;
68 for (ll=0;ll<4;ll++) {
69 if (ll != icol) {
70 dum = a[ll][icol];
71 a[ll][icol] = 0.0;
72 for (l=0;l<4;l++)
73 a[ll][l] -= a[icol][l]*dum;
74 b[ll] -= b[icol]*dum;
75 }
76 }
77 }
78 for (l=3;l>=0;l--) {
79 if (indxr[l] != indxc[l])
80 for (k=0;k<4;k++)
81 GB_SWAP(a[k][indxr[l]],a[k][indxc[l]]);
82 }
83 free(ipiv);
84 free(indxr);
85 free(indxc);
86 return 1;
87 }
88
89 void g3at_translate(g3ATrans t,g3Vec tv)
90 {
91 int i;
92 g3at_copy(t,g3at_unit);
93 for(i=0;i<3;i++)
94 t[i][3] += tv[i];
95 }
96
97 void g3at_add_trans(g3ATrans t,g3Vec tv)
98 {
99 g3ATrans tt;
100
101 g3at_translate(tt,tv);
102 g3at_comb_to(t,tt,t);
103 }
104
105 void g3at_prepend_trans(g3ATrans t,g3Vec tv)
106 {
107 g3ATrans tt;
108
109 g3at_translate(tt,tv);
110 g3at_comb(t,tt);
111 }
112
113 void g3at_rotate(g3ATrans t,g3Vec rnorm,g3Float ang)
114 {
115 g3Vec axe;
116 int aid,i;
117
118 axe = g3v_create();
119 g3at_copy(t,g3at_unit);
120
121 for(aid=0;aid<3;aid++) {
122 g3v_zero(axe);
123 axe[aid] = 1.0;
124 g3v_rotate(axe,axe,rnorm,ang);
125 for(i=0;i<3;i++)
126 t[i][aid] = axe[i];
127 }
128 g3v_free(axe);
129 }
130
131 void g3at_add_rot(g3ATrans t,g3Vec rnorm,g3Float ang)
132 {
133 g3ATrans rt;
134
135 g3at_rotate(rt,rnorm,ang);
136 g3at_comb_to(t,rt,t);
137 }
138
139 void g3at_prepend_rot(g3ATrans t,g3Vec rnorm,g3Float ang)
140 {
141 g3ATrans rt;
142
143 g3at_rotate(rt,rnorm,ang);
144 g3at_comb(t,rt);
145 }
146
147 void g3at_btrans(g3ATrans t,g3Vec xv,g3Vec yv,g3Vec zv)
148 {
149 int i;
150
151 g3at_copy(t,g3at_unit);
152 for(i=0;i<3;i++) {
153 t[i][0] = xv[i];
154 t[i][1] = yv[i];
155 t[i][2] = zv[i];
156 }
157 }
158
159 int g3at_inverse(g3ATrans t)
160 {
161 g3Float v[] = {0,0,0,0};
162
163 if (!nr_gaussj(t,v)) {
164 fprintf(stderr,"g3at_inverse: singular matrix\n");
165 return 0;
166 }
167 return 1;
168 }
169
170 void g3at_apply_h(g3ATrans t,g3AVec v)
171 {
172 int i,j;
173 g3AVec vt;
174
175 g3av_copy(vt,v);
176 g3av_copy(v,g3av_zero);
177 for(i=0;i<4;i++)
178 for(j=0;j<4;j++)
179 v[i] += t[i][j]*vt[j];
180 g3av_tonorm(v);
181 }
182
183 void g3av_print(g3AVec vh,FILE* outf)
184 {
185 int i;
186
187 for(i=0;i<4;i++)
188 fprintf(outf,"%f ",vh[i]);
189 }
190
191 void g3at_apply(g3ATrans t,g3Vec v)
192 {
193 g3AVec res;
194
195 g3av_vectohom(res,v);
196 g3at_apply_h(t,res);
197 g3v_set(v,res[0],res[1],res[2]);
198 }
199
200 void g3at_iapply(g3Vec v,g3ATrans t)
201 {
202 g3AVec res;
203
204 g3av_copy(res,v);
205 g3at_iapply_h(res,t);
206 g3v_set(v,res[0],res[1],res[2]);
207 }
208
209 void g3at_iapply_h(g3Vec v,g3ATrans t)
210 {
211 int i,j;
212 g3AVec vt;
213
214 g3av_copy(vt,v);
215 g3av_copy(v,g3av_zero);
216 for(i=0;i<4;i++)
217 for(j=0;j<4;j++)
218 v[i] += t[j][i]*vt[j];
219 g3av_tonorm(v);
220 }
221
222 void g3at_comb(g3ATrans t,g3ATrans tf)
223 {
224 g3ATrans res;
225
226 g3at_comb_to(res,t,tf);
227 g3at_copy(t,res);
228 }
229
230 void g3at_comb_to(g3ATrans res,g3ATrans t,g3ATrans tf)
231 {
232 int i,j,k;
233 g3ATrans temp;
234 g3at_copy(temp,g3at_zero);
235 for(i=0;i<4;i++)
236 for(j=0;j<4;j++)
237 for(k=0;k<4;k++)
238 temp[i][j] += t[i][k]*tf[k][j];
239 g3at_copy(res,temp);
240 }
241
242
243 void g3av_vectohom(g3AVec res,g3Vec v)
244 {
245 int i;
246
247 for(i=0;i<3;i++)
248 res[i] = v[i];
249 res[3] = 1.0;
250 }
251
252 void g3av_homtovec(g3Vec res,g3AVec h)
253 {
254 int i;
255
256 g3av_tonorm(h);
257 for(i=0;i<3;i++)
258 res[i] = h[i];
259 }
260
261 int g3av_tonorm(g3AVec v)
262 {
263 int i;
264
265 if (gb_epseq(v[3],0.0))
266 return 0;
267 for(i=0;i<3;i++)
268 v[i] /= v[3];
269 v[3] = 1.0;
270 return 1;
271 }
272
273 void g3av_copy(g3AVec v,g3AVec vsrc)
274 {
275 int i;
276
277 for(i=0;i<4;i++)
278 v[i] = vsrc[i];
279 }
280
281 void g3at_print(g3ATrans t,FILE* outf)
282 {
283 int i,j;
284
285 for(i=0;i<4;i++) {
286 for(j=0;j<4;j++)
287 fprintf(outf,"%f ",t[i][j]);
288 fprintf(outf,"\n");
289 }
290 }
291
292 void g3at_copy(g3ATrans t,g3ATrans tsrc)
293 {
294 int i,j;
295
296 for(i=0;i<4;i++)
297 for(j=0;j<4;j++)
298 t[i][j] = tsrc[i][j];
299 }
300
301 int g3at_ctrans(g3ATrans t,g3Vec vdir,g3Vec vup)
302 {
303 g3Vec x,y,z;
304
305 x = g3v_create();
306 y = g3v_create();
307 z = g3v_create();
308 g3v_copy(z,vdir);
309 g3v_copy(x,vup);
310 g3v_normalize(z);
311 g3v_normalize(x);
312 g3v_cross(y,z,x);
313 if (!g3v_normalize(y)) {
314 fprintf(stderr,"g3at_sph_ctrans: parallel\n");
315 g3v_free(x);
316 g3v_free(y);
317 g3v_free(z);
318 return 0;
319 }
320 g3v_cross(x,y,z);
321 if (!g3v_normalize(x)) {
322 fprintf(stderr,"g3at_sph_ctrans: parallel\n");
323 g3v_free(x);
324 g3v_free(y);
325 g3v_free(z);
326 return 0;
327 }
328 g3at_btrans(t,x,y,z);
329 g3v_free(x);
330 g3v_free(y);
331 g3v_free(z);
332 return 1;
333 }
334
335 int g3at_sph_ctrans(g3ATrans t,g3Vec vdir,g3Vec vup)
336 {
337 g3Vec x,z;
338 int res;
339
340 x = g3v_create();
341 z = g3v_create();
342 g3v_copy(z,vdir);
343 g3v_copy(x,vup);
344 z[G3S_RAD] = x[G3S_RAD] = 1.0;
345 g3s_sphtocc(x,x);
346 g3s_sphtocc(z,z);
347 res = g3at_ctrans(t,z,x);
348 g3v_free(x);
349 g3v_free(z);
350
351 return res;
352 }
353
354 #ifdef GL_CONVERSIONS
355 void g3at_get_openGL(g3ATrans t,GLdouble* glm)
356 {
357 int r,c;
358
359 for(r=0;r<4;r++)
360 for(c=0;c<4;c++)
361 glm[c+(r*4)] = t[c][r];
362 }
363
364 void g3at_from_openGL(g3ATrans t,GLdouble* glm)
365 {
366 int r,c;
367
368 for(r=0;r<4;r++)
369 for(c=0;c<4;c++)
370 t[c][r] = glm[c+(r*4)];
371 }
372 #endif
373
374 #ifdef G3AFFINETEST
375
376
377 int main(int argc,char** argv)
378 {
379 g3Vec v,iv,x,y,z;
380 g3ATrans t,it;
381
382 g3at_copy(t,g3at_unit);
383 v = g3v_create();
384 iv = g3v_create();
385 x = g3v_create();
386 y = g3v_create();
387 z = g3v_create();
388 g3v_set(z,1,0,0);
389 g3v_set(x,atof(argv[1]),atof(argv[2]),atof(argv[3]));
390 //g3v_set(y,atof(argv[4]),atof(argv[5]),atof(argv[6]));
391 //g3v_set(z,atof(argv[7]),atof(argv[8]),atof(argv[9]));
392 g3at_tounit(t);
393 g3at_rotate(t,z,DEG2RAD(-90));
394 g3v_set(z,0,0,1);
395 g3at_rotate(it,z,DEG2RAD(180));
396 g3at_comb_to(t,it,t);
397 g3at_apply(t,x);
398 g3v_print(x,stdout);
399 printf("\n");
400 //g3v_print(y,stdout);
401 //printf("\n");
402 exit(19);
403 g3v_set(z,1,DEG2RAD(atof(argv[1])),DEG2RAD(atof(argv[2])));
404 g3v_set(x,1,DEG2RAD(atof(argv[3])),DEG2RAD(atof(argv[4])));
405 g3at_sph_ctrans(t,z,x);
406 g3at_copy(it,t);
407 g3at_inverse(it);
408 g3at_print(t,stderr);
409 fprintf(stderr,"\n");
410 g3v_set(v,1,DEG2RAD(atof(argv[5])),DEG2RAD(atof(argv[6])));
411 g3s_sphtocc(v,v);
412 g3v_copy(iv,v);
413 g3at_apply(it,iv);
414 g3at_apply(t,v);
415 g3s_cctosph(v,v);
416 g3s_cctosph(iv,iv);
417 g3s_sphwrap(v);
418 g3s_sphwrap(iv);
419 fprintf(stderr,"tt %f %f\n",RAD2DEG(v[1]),RAD2DEG(v[2]));
420 fprintf(stderr,"ise %f %f\n",RAD2DEG(iv[1]),RAD2DEG(iv[2]));
421 return EXIT_SUCCESS;
422 }
423
424 #endif