rpp_vecmat.cpp
Go to the documentation of this file.
1 /* ========================================================================
2  * PROJECT: ARToolKitPlus
3  * ========================================================================
4  * This work is based on the original ARToolKit developed by
5  * Hirokazu Kato
6  * Mark Billinghurst
7  * HITLab, University of Washington, Seattle
8  * http://www.hitl.washington.edu/artoolkit/
9  *
10  * Copyright of the derived and new portions of this work
11  * (C) 2006 Graz University of Technology
12  *
13  * This framework is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This framework is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this framework; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * For further information please contact
28  * Dieter Schmalstieg
29  * <schmalstieg@icg.tu-graz.ac.at>
30  * Graz University of Technology,
31  * Institut for Computer Graphics and Vision,
32  * Inffeldgasse 16a, 8010 Graz, Austria.
33  * ========================================================================
34  ** @author Thomas Pintaric
35  *
36  * $Id: rpp_vecmat.cpp 162 2006-04-19 21:28:10Z grabner $
37  * @file
38  * ======================================================================== */
39 
40 
41 #ifndef _NO_LIBRPP_
42 
43 #include <cstdlib>
44 #include <cstdio>
45 #include "rpp_vecmat.h"
46 #include "math.h"
47 #include "assert.h"
48 
49 
50 namespace rpp {
51 
52 
53 int svdcmp( double **a, int m,int n, double *w, double **v);
54 int quintic(double [], double [], double [], int*, double);
55 int quartic(double[], double[], double[], int* );
56 int cubic(double[], double[], int*);
57 
58 
59 
60 #ifdef _WIN32_WCE
61  real_t _sin(real_t a) { return(real_t(::sin(a))); }
62  real_t _cos(real_t a) { return(real_t(::cos(a))); }
63  real_t _atan2(real_t a, real_t b) { return(real_t(::atan2(a,b))); }
64  real_t _abs(real_t a) { return((a>0?a:-a)); }
65  real_t _acos(real_t a) { return(real_t(::acos(a))); }
66  real_t _sqrt(real_t a) { return(real_t(::sqrt(a))); }
67  real_t _pow(real_t a, real_t b) { return(real_t(::pow(a,b))); }
68 #else
69  real_t _sin(real_t a) { return(::sin(a)); }
70  real_t _cos(real_t a) { return(::cos(a)); }
71  real_t _atan2(real_t a, real_t b) { return(::atan2(a,b)); }
72  real_t _abs(real_t a) { return((a>0?a:-a)); }
73  real_t _acos(real_t a) { return(::acos(a)); }
74  real_t _sqrt(real_t a) { return(::sqrt(a)); }
75  real_t _pow(real_t a, real_t b) { return(::pow(a,b)); }
76  /*
77  real_t _sin(real_t a) { return(::sinf(a)); }
78  real_t _cos(real_t a) { return(::cosf(a)); }
79  real_t _atan2(real_t a, real_t b) { return(::atan2f(a,b)); }
80  real_t _abs(real_t a) { return((a>0?a:-a)); }
81  real_t _acos(real_t a) { return(::acosf(a)); }
82  real_t _sqrt(real_t a) { return(::sqrtf(a)); }
83  real_t _pow(real_t a, real_t b) { return(::powf(a,b)); }
84  */
85 #endif
86 
87 // ---------------------------------------------------------------------------
88 
89 void mat33_from_double_pptr(mat33_t &mat, double** m_ptr)
90 {
91  for(int m=0; m<3; m++)
92  for(int n=0; n<3; n++)
93  mat.m[m][n] = (real_t) m_ptr[m][n];
94 }
95 
96 
97 void mat33_from_float_pptr(mat33_t &mat, float** m_ptr)
98 {
99  for(int m=0; m<3; m++)
100  for(int n=0; n<3; n++)
101  mat.m[m][n] = (real_t) m_ptr[m][n];
102 }
103 
104 
106 {
107  double **M = (double**) malloc(3*sizeof(double*));
108  for(int i=0; i<3; i++) M[i] = (double*) malloc(3*sizeof(double));
109  for(int m=0; m<3; m++)
110  for(int n=0; n<3; n++)
111  M[m][n] = (double) mat.m[m][n];
112  return(M);
113 }
114 
115 
117 {
118  float **M = (float**) malloc(3*sizeof(float*));
119  for(int i=0; i<3; i++) M[i] = (float*) malloc(3*sizeof(float));
120  for(int m=0; m<3; m++)
121  for(int n=0; n<3; n++)
122  M[m][n] = (float) mat.m[m][n];
123  return(M);
124 }
125 
126 
127 void free_double_pptr(double*** m_ptr)
128 {
129  for(int i=0; i<3; i++) free((*m_ptr)[i]);
130  free(*m_ptr);
131 }
132 
133 void free_float_pptr(float*** m_ptr)
134 {
135  for(int i=0; i<3; i++) free((*m_ptr)[i]);
136  free(*m_ptr);
137 }
138 
139 void vec3_from_double_ptr(vec3_t &vec, double* v_ptr)
140 {
141  vec.v[0] = (real_t)v_ptr[0];
142  vec.v[1] = (real_t)v_ptr[1];
143  vec.v[2] = (real_t)v_ptr[2];
144 }
145 
146 void vec3_from_float_ptr(vec3_t &vec, float* v_ptr)
147 {
148  vec.v[0] = (real_t)v_ptr[0];
149  vec.v[1] = (real_t)v_ptr[1];
150  vec.v[2] = (real_t)v_ptr[2];
151 }
152 
153 double* vec3_to_double_ptr(const vec3_t &vec)
154 {
155  double* v = (double*) malloc(3* sizeof(double));
156  v[0] = (double) vec.v[0];
157  v[1] = (double) vec.v[1];
158  v[2] = (double) vec.v[2];
159  return(v);
160 }
161 
162 float* vec3_to_float_ptr(const vec3_t &vec)
163 {
164  float* v = (float*) malloc(3* sizeof(float));
165  v[0] = (float) vec.v[0];
166  v[1] = (float) vec.v[1];
167  v[2] = (float) vec.v[2];
168  return(v);
169 }
170 
171 
172 void free_double_ptr(double** v_ptr)
173 {
174  free(*v_ptr);
175 }
176 
177 void free_float_ptr(float** v_ptr)
178 {
179  free(*v_ptr);
180 }
181 
182 
183 
185  const real_t m00, const real_t m01, const real_t m02,
186  const real_t m10, const real_t m11, const real_t m12,
187  const real_t m20, const real_t m21, const real_t m22)
188 {
189  m.m[0][0] = m00; m.m[0][1] = m01; m.m[0][2] = m02;
190  m.m[1][0] = m10; m.m[1][1] = m11; m.m[1][2] = m12;
191  m.m[2][0] = m20; m.m[2][1] = m21; m.m[2][2] = m22;
192 }
193 
194 
195 void _dbg_quat_print(const quat_t &q, char* name)
196 {
197  printf("%s: s = [ %.4f ] v = [ ",name,q.s);
198  for(unsigned int c=0; c<3; c++)
199  {
200  // double d = (double)(q.v.v[c]);
201  printf("%.4f ",q.v.v[c]);
202  }
203  printf("]\n");
204 }
205 
206 void _dbg_mat33_print(const mat33_t &m, char* name)
207 {
208  printf("%s:\n",name);
209  for(unsigned int r=0; r<3; r++)
210  {
211  printf("[ ");
212  for(unsigned int c=0; c<3; c++)
213  {
214  double d = (double)(m.m[r][c]);
215  printf("%.4f ",d);
216  }
217  printf("]\n");
218  }
219 }
220 
221 void _dbg_mat33_array_print(const mat33_array &m, char* name)
222 {
223  for(unsigned int i=0; i<m.size(); i++)
224  {
225  printf("%s.at(%i):\n",name,i);
226  for(unsigned int r=0; r<3; r++)
227  {
228  printf("[ ");
229  for(unsigned int c=0; c<3; c++)
230  {
231  double d = (double)(m.at(i).m[r][c]);
232  printf("%.4f ",d);
233  }
234  printf("]\n");
235  }
236  }
237 }
238 
239 void _dbg_vec3_print(const vec3_t &v, char* name)
240 {
241  printf("%s: [ ",name);
242  for(unsigned int c=0; c<3; c++)
243  {
244  double d = (double)(v.v[c]);
245  printf("%.4f ",d);
246  }
247  printf("]\n");
248 }
249 
250 void _dbg_vec3_array_print(const vec3_array &v, char* name)
251 {
252  for(unsigned int i=0; i<v.size(); i++)
253  {
254  printf("%s.at(%i): [ ",name,i);
255  for(unsigned int c=0; c<3; c++)
256  {
257  double d = (double)(v.at(i).v[c]);
258  printf("%.4f ",d);
259  }
260  printf("]\n");
261  }
262 }
263 
264 
265 bool _dbg_load_vec3_array(vec3_array &va, char* filename)
266 {
267  FILE *fp = fopen( filename, "r" );
268  if( fp == NULL ) return(false);
269 
270  int n;
271  int lineno = 0;
272  va.clear();
273 
274  while(!feof(fp))
275  {
276  ++lineno;
277  double vx,vy,vz;
278  vec3_t v;
279  n = fscanf(fp, "%lf%lf%lf\n", &vx,&vy,&vz);
280  if((n!=3) || ferror(fp))
281  {
282  printf("file i/o error: %s (line %i)",filename,lineno);
283  fclose(fp);
284  return(lineno > 1);
285  }
286  v.v[0] = (real_t)vx;
287  v.v[1] = (real_t)vy;
288  v.v[2] = (real_t)vz;
289  va.push_back(v);
290  }
291 
292  fclose(fp);
293  return(true);
294 }
295 
296 
297 void vec3_assign(vec3_t &v, const real_t x, const real_t y, const real_t z)
298 {
299  v.v[0] = x;
300  v.v[1] = y;
301  v.v[2] = z;
302 }
303 
305 {
306  v.v[0] = 0;
307  v.v[1] = 0;
308  v.v[2] = 0;
309 }
310 
311 void vec3_copy(vec3_t &a, const vec3_t &b)
312 {
313  a.v[0] = b.v[0];
314  a.v[1] = b.v[1];
315  a.v[2] = b.v[2];
316 }
317 
318 
319 void vec3_array_sum(vec3_t &v_sum2, const vec3_array &va)
320 {
321  vec3_clear(v_sum2);
322  for(vec3_array_const_iter iter = va.begin();
323  iter != va.end(); iter++)
324  {
325  v_sum2.v[0] += (*iter).v[0];
326  v_sum2.v[1] += (*iter).v[1];
327  v_sum2.v[2] += (*iter).v[2];
328  }
329 }
330 
331 void vec3_array_sum(scalar_array &v_sum1, const vec3_array &va)
332 {
333  v_sum1.clear();
334  v_sum1.resize(va.size());
335 
336  for(unsigned int i=0; i<va.size(); i++)
337  v_sum1.at(i) = (va[i].v[0] + va[i].v[1] + va[i].v[2]);
338 }
339 
341 {
342  for(vec3_array_iter iter = va.begin();
343  iter != va.end(); iter++)
344  {
345  (*iter).v[0] *= (*iter).v[0];
346  (*iter).v[1] *= (*iter).v[1];
347  (*iter).v[2] *= (*iter).v[2];
348  }
349 }
350 
351 
352 
353 
354 void vec3_div(vec3_t &va, const real_t n)
355 {
356  va.v[0] /= n;
357  va.v[1] /= n;
358  va.v[2] /= n;
359 }
360 
361 void vec3_div(vec3_t &va, const vec3_t &vb)
362 {
363  va.v[0] /= vb.v[0];
364  va.v[1] /= vb.v[1];
365  va.v[2] /= vb.v[2];
366 }
367 
368 void vec3_mult(vec3_t &va, const real_t n)
369 {
370  va.v[0] *= n;
371  va.v[1] *= n;
372  va.v[2] *= n;
373 }
374 
375 void vec3_mult(vec3_t &va, const vec3_t &vb)
376 {
377  va.v[0] *= vb.v[0];
378  va.v[1] *= vb.v[1];
379  va.v[2] *= vb.v[2];
380 }
381 
382 void vec3_add(vec3_t &va, const real_t f)
383 {
384  va.v[0] += f;
385  va.v[1] += f;
386  va.v[2] += f;
387 }
388 
389 void vec3_add(vec3_t &va, const vec3_t &vb)
390 {
391  va.v[0] += vb.v[0];
392  va.v[1] += vb.v[1];
393  va.v[2] += vb.v[2];
394 }
395 
396 void vec3_add(vec3_t &va, const vec3_t &vb, const vec3_t &vc)
397 {
398  va.v[0] = vb.v[0] + vc.v[0];
399  va.v[1] = vb.v[1] + vc.v[1];
400  va.v[2] = vb.v[2] + vc.v[2];
401 }
402 
403 void vec3_sub(vec3_t &va, const real_t f)
404 {
405  va.v[0] -= f;
406  va.v[1] -= f;
407  va.v[2] -= f;
408 }
409 
410 void vec3_sub(vec3_t &va, const vec3_t &vb)
411 {
412  va.v[0] -= vb.v[0];
413  va.v[1] -= vb.v[1];
414  va.v[2] -= vb.v[2];
415 }
416 
417 void vec3_sub(vec3_t &va, const vec3_t &vb, const vec3_t &vc)
418 {
419  va.v[0] = vb.v[0] - vc.v[0];
420  va.v[1] = vb.v[1] - vc.v[1];
421  va.v[2] = vb.v[2] - vc.v[2];
422 }
423 
424 real_t vec3_dot(const vec3_t &va, const vec3_t &vb)
425 {
426  return(va.v[0]*vb.v[0]+va.v[1]*vb.v[1]+va.v[2]*vb.v[2]);
427 }
428 
429 void vec3_cross(vec3_t &va, const vec3_t &vb, const vec3_t &vc)
430 {
431  va.v[0] = (vb.v[1] * vc.v[2] - vc.v[1] * vb.v[2]);
432  va.v[1] = (vb.v[2] * vc.v[0] - vc.v[2] * vb.v[0]);
433  va.v[2] = (vb.v[0] * vc.v[1] - vc.v[0] * vb.v[1]);
434 }
435 
437 {
438  return(_sqrt(v.v[0]*v.v[0] + v.v[1]*v.v[1] + v.v[2]*v.v[2]));
439 }
440 
442 {
443  return(v.v[0] + v.v[1] + v.v[2]);
444 }
445 
446 void vec3_array_add(vec3_array &va, const vec3_t &a)
447 {
448  for(vec3_array_iter iter = va.begin();
449  iter != va.end(); iter++)
450  {
451  (*iter).v[0] += a.v[0];
452  (*iter).v[1] += a.v[1];
453  (*iter).v[2] += a.v[2];
454  }
455 }
456 
457 void vec3_array_sub(vec3_array &va, const vec3_t &a)
458 {
459  for(vec3_array_iter iter = va.begin();
460  iter != va.end(); iter++)
461  {
462  (*iter).v[0] -= a.v[0];
463  (*iter).v[1] -= a.v[1];
464  (*iter).v[2] -= a.v[2];
465  }
466 }
467 
468 void vec3_array_set(vec3_array &va, const vec3_t &a, const bool mask[3])
469 {
470  for(vec3_array_iter iter = va.begin();
471  iter != va.end(); iter++)
472  {
473  if(mask[0]) (*iter).v[0] = a.v[0];
474  if(mask[1]) (*iter).v[1] = a.v[1];
475  if(mask[2]) (*iter).v[2] = a.v[2];
476  }
477 }
478 
480 {
481  assert(va.size() == c.size());
482  for(unsigned int i=0; i<va.size(); i++)
483  {
484  va[i].v[0] *= c[i];
485  va[i].v[1] *= c[i];
486  va[i].v[2] *= c[i];
487  }
488 }
489 
490 void vec3_array_mean(vec3_t &v_mean, const vec3_array &va)
491 {
492  vec3_array_sum(v_mean, va);
493  real_t l = (real_t) va.size();
494  vec3_div(v_mean, l);
495 }
496 
497 
498 void vec3_mul_vec3trans(mat33_t &m, const vec3_t &va, const vec3_t &vb)
499 {
500  m.m[0][0] = va.v[0] * vb.v[0];
501  m.m[0][1] = va.v[0] * vb.v[1];
502  m.m[0][2] = va.v[0] * vb.v[2];
503  m.m[1][0] = va.v[1] * vb.v[0];
504  m.m[1][1] = va.v[1] * vb.v[1];
505  m.m[1][2] = va.v[1] * vb.v[2];
506  m.m[2][0] = va.v[2] * vb.v[0];
507  m.m[2][1] = va.v[2] * vb.v[1];
508  m.m[2][2] = va.v[2] * vb.v[2];
509 }
510 
511 real_t vec3trans_mul_vec3(const vec3_t &va, const vec3_t &vb)
512 {
513  return(va.v[0] * vb.v[0] + va.v[1] * vb.v[1] + va.v[2] * vb.v[2]);
514 }
515 
516 
518 {
519  for(unsigned int r=0; r<3; r++)
520  for(unsigned int c=0; c<3; c++)
521  {
522  m.m[r][c] = 0;
523  }
524 }
525 
526 void mat33_copy(mat33_t &md, const mat33_t &ms)
527 {
528  for(unsigned int r=0; r<3; r++)
529  for(unsigned int c=0; c<3; c++)
530  {
531  md.m[r][c] = ms.m[r][c];
532  }
533 }
534 
535 
536 void mat33_to_col_vec3(vec3_t &c0, vec3_t &c1, vec3_t &c2, const mat33_t &m)
537 {
538  for(unsigned int r=0; r<3; r++)
539  {
540  c0.v[r] = m.m[r][0];
541  c1.v[r] = m.m[r][1];
542  c2.v[r] = m.m[r][2];
543  }
544 }
545 
546 
547 void mat33_div(mat33_t &m, const real_t f)
548 {
549  m.m[0][0] /= f;
550  m.m[0][1] /= f;
551  m.m[0][2] /= f;
552  m.m[1][0] /= f;
553  m.m[1][1] /= f;
554  m.m[1][2] /= f;
555  m.m[2][0] /= f;
556  m.m[2][1] /= f;
557  m.m[2][2] /= f;
558 }
559 
561 {
562  m.m[0][0] = 1;
563  m.m[0][1] = 0;
564  m.m[0][2] = 0;
565  m.m[1][0] = 0;
566  m.m[1][1] = 1;
567  m.m[1][2] = 0;
568  m.m[2][0] = 0;
569  m.m[2][1] = 0;
570  m.m[2][2] = 1;
571 }
572 
574 {
575  real_t sum(0.0f);
576  for(unsigned int r=0; r<3; r++)
577  for(unsigned int c=0; c<3; c++)
578  {
579  sum += m.m[r][c];
580  }
581  return(sum);
582 }
583 
584 
585 bool mat33_all_zeros(const mat33_t &m)
586 {
587  for(unsigned int r=0; r<3; r++)
588  for(unsigned int c=0; c<3; c++)
589  {
590  if(m.m[r][c] != 0) return(false);
591  }
592  return(true);
593 }
594 
596 {
597  for(unsigned int r=0; r<3; r++)
598  for(unsigned int c=0; c<3; c++)
599  {
600  m.m[r][c] = 0;
601  }
602 }
603 
604 
606 {
607  mat33_clear(s);
608  for(mat33_array_const_iter iter = ma.begin();
609  iter != ma.end(); iter++)
610  {
611  for(unsigned int c=0; c<3; c++)
612  {
613  s.m[0][c] += (*iter).m[0][c];
614  s.m[1][c] += (*iter).m[1][c];
615  s.m[2][c] += (*iter).m[2][c];
616  }
617  }
618 }
619 
620 
621 void mat33_sub(mat33_t &mr, const mat33_t &ma, const mat33_t &mb)
622 {
623  for(unsigned int r=0; r<3; r++)
624  for(unsigned int c=0; c<3; c++)
625  {
626  mr.m[r][c] = ma.m[r][c]-mb.m[r][c];
627  }
628 }
629 
630 void mat33_sub(mat33_t &ma, const mat33_t &mb)
631 {
632  for(unsigned int r=0; r<3; r++)
633  for(unsigned int c=0; c<3; c++)
634  {
635  ma.m[r][c] -= mb.m[r][c];
636  }
637 }
638 
639 void mat33_add(mat33_t &mr, const mat33_t &ma, const mat33_t &mb)
640 {
641  for(unsigned int r=0; r<3; r++)
642  for(unsigned int c=0; c<3; c++)
643  {
644  mr.m[r][c] = ma.m[r][c]+mb.m[r][c];
645  }
646 }
647 
648 void mat33_add(mat33_t &ma, const mat33_t &mb)
649 {
650  for(unsigned int r=0; r<3; r++)
651  for(unsigned int c=0; c<3; c++)
652  {
653  ma.m[r][c] += mb.m[r][c];
654  }
655 }
656 
657 
659 {
660  real_t determinant = a.m[0][0]*a.m[1][1]*a.m[2][2] + a.m[0][1]*a.m[1][2]*a.m[2][0] +
661  a.m[0][2]*a.m[1][0]*a.m[2][1] - a.m[2][0]*a.m[1][1]*a.m[0][2] -
662  a.m[2][1]*a.m[1][2]*a.m[0][0] - a.m[2][2]*a.m[1][0]*a.m[0][1];
663  return(determinant);
664 }
665 
666 void mat33_inv(mat33_t &mi, const mat33_t &ma)
667 {
668  real_t determinant = mat33_det(ma);
669  mi.m[0][0] = (ma.m[1][1]*ma.m[2][2] - ma.m[1][2]*ma.m[2][1])/determinant;
670  mi.m[0][1] = (ma.m[0][2]*ma.m[2][1] - ma.m[0][1]*ma.m[2][2])/determinant;
671  mi.m[0][2] = (ma.m[0][1]*ma.m[1][2] - ma.m[0][2]*ma.m[1][1])/determinant;
672 
673  mi.m[1][0] = (ma.m[1][2]*ma.m[2][0] - ma.m[1][0]*ma.m[2][2])/determinant;
674  mi.m[1][1] = (ma.m[0][0]*ma.m[2][2] - ma.m[0][2]*ma.m[2][0])/determinant;
675  mi.m[1][2] = (ma.m[0][2]*ma.m[1][0] - ma.m[0][0]*ma.m[1][2])/determinant;
676 
677  mi.m[2][0] = (ma.m[1][0]*ma.m[2][1] - ma.m[1][1]*ma.m[2][0])/determinant;
678  mi.m[2][1] = (ma.m[0][1]*ma.m[2][0] - ma.m[0][0]*ma.m[2][1])/determinant;
679  mi.m[2][2] = (ma.m[0][0]*ma.m[1][1] - ma.m[0][1]*ma.m[1][0])/determinant;
680 }
681 
682 void mat33_mult(mat33_t &m0, const mat33_t &m1, const mat33_t &m2)
683 {
684  m0.m[0][0] = m1.m[0][0]*m2.m[0][0] + m1.m[0][1]*m2.m[1][0] + m1.m[0][2]*m2.m[2][0];
685  m0.m[0][1] = m1.m[0][0]*m2.m[0][1] + m1.m[0][1]*m2.m[1][1] + m1.m[0][2]*m2.m[2][1];
686  m0.m[0][2] = m1.m[0][0]*m2.m[0][2] + m1.m[0][1]*m2.m[1][2] + m1.m[0][2]*m2.m[2][2];
687  m0.m[1][0] = m1.m[1][0]*m2.m[0][0] + m1.m[1][1]*m2.m[1][0] + m1.m[1][2]*m2.m[2][0];
688  m0.m[1][1] = m1.m[1][0]*m2.m[0][1] + m1.m[1][1]*m2.m[1][1] + m1.m[1][2]*m2.m[2][1];
689  m0.m[1][2] = m1.m[1][0]*m2.m[0][2] + m1.m[1][1]*m2.m[1][2] + m1.m[1][2]*m2.m[2][2];
690  m0.m[2][0] = m1.m[2][0]*m2.m[0][0] + m1.m[2][1]*m2.m[1][0] + m1.m[2][2]*m2.m[2][0];
691  m0.m[2][1] = m1.m[2][0]*m2.m[0][1] + m1.m[2][1]*m2.m[1][1] + m1.m[2][2]*m2.m[2][1];
692  m0.m[2][2] = m1.m[2][0]*m2.m[0][2] + m1.m[2][1]*m2.m[1][2] + m1.m[2][2]*m2.m[2][2];
693 }
694 
695 void mat33_mult(mat33_t &mr, const real_t n)
696 {
697  for(unsigned int r=0; r<3; r++)
698  for(unsigned int c=0; c<3; c++)
699  {
700  mr.m[r][c] *= n;
701  }
702 }
703 
704 void mat33_transpose(mat33_t &t, const mat33_t m)
705 {
706  for(unsigned int r=0; r<3; r++)
707  for(unsigned int c=0; c<3; c++)
708  {
709  t.m[r][c] = m.m[c][r];
710  }
711 }
712 
713 void vec3_mult(vec3_t &v0, const mat33_t &m1, const vec3_t &v2)
714 {
715  v0.v[0] = m1.m[0][0]*v2.v[0] + m1.m[0][1]*v2.v[1] + m1.m[0][2]*v2.v[2];
716  v0.v[1] = m1.m[1][0]*v2.v[0] + m1.m[1][1]*v2.v[1] + m1.m[1][2]*v2.v[2];
717  v0.v[2] = m1.m[2][0]*v2.v[0] + m1.m[2][1]*v2.v[1] + m1.m[2][2]*v2.v[2];
718 }
719 
720 void vec3_array_mult(vec3_array &va, const mat33_t &m, const vec3_array &vb)
721 {
722  va.clear();
723  va.resize(vb.size());
724  for(unsigned int i=0; i<vb.size(); i++)
725  {
726  vec3_mult(va.at(i),m,vb.at(i));
727  }
728 }
729 
730 void mat33_svd2(mat33_t &u, mat33_t &s, mat33_t &v, const mat33_t &m)
731 {
732  mat33_clear(u);
733  mat33_clear(v);
734 
735  double** m_ptr = mat33_to_double_pptr(m);
736  double** v_ptr = mat33_to_double_pptr(v);
737 
738  vec3_t q;
739  vec3_clear(q);
740  double* q_ptr = vec3_to_double_ptr(q);
741 
742  /*int ret =*/ svdcmp(m_ptr, 3, 3, q_ptr, v_ptr);
743 
744  mat33_from_double_pptr(u,m_ptr);
745  mat33_from_double_pptr(v,v_ptr);
746  vec3_from_double_ptr(q,q_ptr);
747 
748  mat33_clear(s);
749  s.m[0][0] = (real_t)q_ptr[0];
750  s.m[1][1] = (real_t)q_ptr[1];
751  s.m[2][2] = (real_t)q_ptr[2];
752 
753  free_double_pptr(&m_ptr);
754  free_double_pptr(&v_ptr);
755  free_double_ptr(&q_ptr);
756 }
757 
758 void quat_mult(quat_t &q, const real_t s)
759 {
760  vec3_mult(q.v,s);
761  q.s *= s;
762 }
763 
765 {
766  const real_t f_vn = vec3_norm(q.v);
767  return(_sqrt((f_vn*f_vn) + (q.s*q.s)));
768 
769 }
770 
771 void mat33_from_quat(mat33_t &m, const quat_t &q)
772 {
773  const real_t a = q.s;
774  const real_t b = q.v.v[0];
775  const real_t c = q.v.v[1];
776  const real_t d = q.v.v[2];
777 
778  m.m[0][0] = (a*a)+(b*b)-(c*c)-(d*d);
779  m.m[0][1] = real_t(2.0f)*(b*c-a*d);
780  m.m[0][2] = real_t(2.0f)*(b*d+a*c);
781 
782  m.m[1][0] = real_t(2.0f)*(b*c+a*d);
783  m.m[1][1] = (a*a)+(c*c)-(b*b)-(d*d);
784  m.m[1][2] = real_t(2.0f)*(c*d-a*b);
785 
786  m.m[2][0] = real_t(2.0f)*(b*d-a*c);
787  m.m[2][1] = real_t(2.0f)*(c*d+a*b);
788  m.m[2][2] = (a*a)+(d*d)-(b*b)-(c*c);
789 }
790 
791 // ===========================================================================================
792 void normRv(vec3_t &n, const vec3_t &v)
793 {
794  vec3_t _v1;
795  vec3_copy(_v1,v);
796  vec3_mult(_v1,_v1);
797  real_t l = 1.0f / _sqrt(_v1.v[0] + _v1.v[1] + _v1.v[2]);
798  vec3_copy(n,v);
799  vec3_mult(n,l);
800 }
801 
802 // ===========================================================================================
803 void normRv(vec3_array &normR_v, const vec3_array &v)
804 {
805  normR_v.assign(v.begin(),v.end());
806  vec3_array_pow2(normR_v);
807  scalar_array _l;
808  vec3_array_sum(_l,normR_v);
809 
810  for(scalar_array::iterator iter = _l.begin();
811  iter != _l.end(); iter++)
812  {
813  (*iter) = 1.0f / _sqrt(*iter);
814  }
815 
816  normR_v.assign(v.begin(),v.end());
817  vec3_array_mult(normR_v,_l);
818 }
819 
820 // ===========================================================================================
821 int solve_polynomial(scalar_array &r_sol, const scalar_array &coefficients)
822 {
823  if(coefficients.size() != 5) return(0);
824  r_sol.clear();
825 
826  double dd[5] = {(double)coefficients[0],
827  (double)coefficients[1],
828  (double)coefficients[2],
829  (double)coefficients[3],
830  (double)coefficients[4] };
831 
832  double sol[4] = {0,0,0,0};
833  double soli[4] = {0,0,0,0};
834  int n_sol = 0;
835  quartic(dd, sol, soli, &n_sol);
836 
837  if(n_sol <= 0) return(0); // assert(false);
838 
839  r_sol.resize(n_sol);
840  for(int i=0; i<n_sol; i++) r_sol[i] = (real_t)sol[i];
841  return(n_sol);
842 }
843 // ===========================================================================================
844 
846 {
847  for(unsigned int i=0; i<sa.size(); i++) sa.at(i) = _pow(sa[i],f);
848 }
849 
851 {
852  for(unsigned int i=0; i<sa.size(); i++) sa.at(i) = - sa.at(i);
853 }
854 
856  const real_t f,
857  const unsigned int sz)
858 {
859  sa.clear();
860  sa.resize(sz);
861  for(unsigned int i=0; i<sz; i++) sa.at(i) = f;
862 }
863 
864 
866 {
867  assert(sa.size() == sb.size());
868  for(unsigned int i=0; i<(unsigned int)sa.size(); i++)
869  sa.at(i) = sa.at(i) + sb.at(i);
870 }
871 
873 {
874  for(unsigned int i=0; i<(unsigned int)sa.size(); i++) sa.at(i) = 0.;
875 }
876 
878  const scalar_array &sb,
879  const scalar_array &sc)
880 {
881  assert(sb.size() == sc.size());
882  sa.clear();
883  sa.resize(sb.size());
884  for(unsigned int i=0; i<(unsigned int)sb.size(); i++)
885  sa[i] = _atan2(sb[i],sc[i]);
886 }
887 
888 void _dbg_scalar_array_print(const scalar_array &sa, char* name)
889 {
890  for(unsigned int i=0; i<sa.size(); i++)
891  printf("%s.at(%i): [ %e ]\n",name,i,sa[i]);
892 }
893 
895 {
896  for(unsigned int i=0; i<(unsigned int)sa.size(); i++)
897  {
898  sa.at(i) /= f;
899  }
900 }
901 
903 {
904  assert(sa.size() == sb.size());
905  for(unsigned int i=0; i<(unsigned int)sa.size(); i++)
906  sa[i] /= sb[i];
907 }
908 
910 {
911  for(unsigned int i=0; i<(unsigned int)sa.size(); i++)
912  {
913  sa.at(i) *= f;
914  }
915 }
916 
918 {
919  for(unsigned int i=0; i<(unsigned int)sa.size(); i++)
920  {
921  sa.at(i) += f;
922  }
923 }
924 
926 {
927  for(unsigned int i=0; i<(unsigned int)sa.size(); i++)
928  {
929  sa.at(i) -= f;
930  }
931 }
932 
934 {
935  for(unsigned int r=0; r<3; r++)
936  for(unsigned int c=0; c<3; c++)
937  {
938  m.m[r][c] *= m.m[r][c];
939  }
940 }
941 
942 
943 void _dbg_vec3_fprint(void* fp, const vec3_t &v, char* name)
944 {
945  fprintf((FILE*)fp,"%s: [ ",name);
946  for(unsigned int c=0; c<3; c++)
947  {
948  fprintf((FILE*)fp,"%.4f ",v.v[c]);
949  }
950  fprintf((FILE*)fp,"]\n");
951 }
952 
953 void _dbg_mat33_fprint(void* fp, const mat33_t &m, char* name)
954 {
955  fprintf((FILE*)fp,"%s:\n",name);
956  for(unsigned int r=0; r<3; r++)
957  {
958  fprintf((FILE*)fp,"[ ");
959  for(unsigned int c=0; c<3; c++)
960  {
961  fprintf((FILE*)fp,"%.4f ",m.m[r][c]);
962  }
963  fprintf((FILE*)fp,"]\n");
964  }
965 }
966 
967 
968 } // namespace rpp
969 
970 
971 #endif //_NO_LIBRPP_
d
vec3_t v
Definition: rpp_types.h:99
real_t vec3trans_mul_vec3(const vec3_t &va, const vec3_t &vb)
Definition: rpp_vecmat.cpp:511
void scalar_array_clear(scalar_array &sa)
Definition: rpp_vecmat.cpp:872
std::vector< vec3_t >::const_iterator vec3_array_const_iter
Definition: rpp_types.h:73
void mat33_eye(mat33_t &m)
Definition: rpp_vecmat.cpp:560
real_t mat33_det(const mat33_t &a)
Definition: rpp_vecmat.cpp:658
real_t _sin(real_t a)
Definition: rpp_vecmat.cpp:69
void mat33_from_float_pptr(mat33_t &mat, float **m_ptr)
Definition: rpp_vecmat.cpp:97
void mat33_svd2(mat33_t &u, mat33_t &s, mat33_t &v, const mat33_t &m)
Definition: rpp_vecmat.cpp:730
void free_float_ptr(float **v_ptr)
Definition: rpp_vecmat.cpp:177
f
void _dbg_mat33_print(const mat33_t &m, char *name)
Definition: rpp_vecmat.cpp:206
void mat33_to_col_vec3(vec3_t &c0, vec3_t &c1, vec3_t &c2, const mat33_t &m)
Definition: rpp_vecmat.cpp:536
void normRv(vec3_t &n, const vec3_t &v)
Definition: rpp_vecmat.cpp:792
void mat33_div(mat33_t &m, const real_t f)
Definition: rpp_vecmat.cpp:547
float ** mat33_to_float_pptr(const mat33_t &mat)
Definition: rpp_vecmat.cpp:116
static int free(ARMat *m)
bool _dbg_load_vec3_array(vec3_array &va, char *filename)
Definition: rpp_vecmat.cpp:265
void vec3_array_sub(vec3_array &va, const vec3_t &a)
Definition: rpp_vecmat.cpp:457
void mat33_mult(mat33_t &m0, const mat33_t &m1, const mat33_t &m2)
Definition: rpp_vecmat.cpp:682
Definition: rpp.cpp:55
std::vector< mat33_t > mat33_array
Definition: rpp_types.h:77
void scalar_array_negate(scalar_array &sa)
Definition: rpp_vecmat.cpp:850
real_t vec3_dot(const vec3_t &va, const vec3_t &vb)
Definition: rpp_vecmat.cpp:424
void scalar_array_mult(scalar_array &sa, real_t f)
Definition: rpp_vecmat.cpp:909
void free_double_ptr(double **v_ptr)
Definition: rpp_vecmat.cpp:172
void vec3_from_double_ptr(vec3_t &vec, double *v_ptr)
Definition: rpp_vecmat.cpp:139
bool mat33_all_zeros(const mat33_t &m)
Definition: rpp_vecmat.cpp:585
void free_double_pptr(double ***m_ptr)
Definition: rpp_vecmat.cpp:127
std::vector< vec3_t > vec3_array
Definition: rpp_types.h:71
void quat_mult(quat_t &q, const real_t s)
Definition: rpp_vecmat.cpp:758
void _dbg_vec3_fprint(void *fp, const vec3_t &v, char *name)
Definition: rpp_vecmat.cpp:943
void vec3_array_pow2(vec3_array &va)
Definition: rpp_vecmat.cpp:340
int quartic(double[], double[], double[], int *)
double * vec3_to_double_ptr(const vec3_t &vec)
Definition: rpp_vecmat.cpp:153
void mat33_clear(mat33_t &m)
Definition: rpp_vecmat.cpp:517
double ** mat33_to_double_pptr(const mat33_t &mat)
Definition: rpp_vecmat.cpp:105
float * vec3_to_float_ptr(const vec3_t &vec)
Definition: rpp_vecmat.cpp:162
void vec3_cross(vec3_t &va, const vec3_t &vb, const vec3_t &vc)
Definition: rpp_vecmat.cpp:429
void _dbg_mat33_array_print(const mat33_array &m, char *name)
Definition: rpp_vecmat.cpp:221
void mat33_assign(mat33_t &m, const real_t m00, const real_t m01, const real_t m02, const real_t m10, const real_t m11, const real_t m12, const real_t m20, const real_t m21, const real_t m22)
Definition: rpp_vecmat.cpp:184
real_t _cos(real_t a)
Definition: rpp_vecmat.cpp:70
void mat33_from_double_pptr(mat33_t &mat, double **m_ptr)
Definition: rpp_vecmat.cpp:89
std::vector< vec3_t >::iterator vec3_array_iter
Definition: rpp_types.h:72
void vec3_array_mult(vec3_array &va, const scalar_array &c)
Definition: rpp_vecmat.cpp:479
std::vector< real_t > scalar_array
Definition: rpp_types.h:81
real_t _pow(real_t a, real_t b)
Definition: rpp_vecmat.cpp:75
void _dbg_scalar_array_print(const scalar_array &sa, char *name)
Definition: rpp_vecmat.cpp:888
void scalar_array_atan2(scalar_array &sa, const scalar_array &sb, const scalar_array &sc)
Definition: rpp_vecmat.cpp:877
void mat33_from_quat(mat33_t &m, const quat_t &q)
Definition: rpp_vecmat.cpp:771
void scalar_array_add(scalar_array &sa, const scalar_array &sb)
Definition: rpp_vecmat.cpp:865
void mat33_sub(mat33_t &mr, const mat33_t &ma, const mat33_t &mb)
Definition: rpp_vecmat.cpp:621
void _dbg_vec3_print(const vec3_t &v, char *name)
Definition: rpp_vecmat.cpp:239
void vec3_from_float_ptr(vec3_t &vec, float *v_ptr)
Definition: rpp_vecmat.cpp:146
real_t vec3_norm(const vec3_t &v)
Definition: rpp_vecmat.cpp:436
real_t _sqrt(real_t a)
Definition: rpp_vecmat.cpp:74
void scalar_array_assign(scalar_array &sa, const real_t f, const unsigned int sz)
Definition: rpp_vecmat.cpp:855
int quintic(double[], double[], double[], int *, double)
int cubic(double[], double[], int *)
void mat33_set_all_zeros(mat33_t &m)
Definition: rpp_vecmat.cpp:595
void vec3_array_add(vec3_array &va, const vec3_t &a)
Definition: rpp_vecmat.cpp:446
double real_t
Definition: rpp_types.h:64
real_t vec3_sum(const vec3_t &v)
Definition: rpp_vecmat.cpp:441
void vec3_add(vec3_t &va, const real_t f)
Definition: rpp_vecmat.cpp:382
void mat33_inv(mat33_t &mi, const mat33_t &ma)
Definition: rpp_vecmat.cpp:666
int solve_polynomial(scalar_array &r_sol, const scalar_array &coefficients)
Definition: rpp_vecmat.cpp:821
void scalar_array_pow(scalar_array &sa, const real_t f)
Definition: rpp_vecmat.cpp:845
#define NULL
Definition: PocketKnife.h:38
void mat33_array_sum(mat33_t &s, const mat33_array &ma)
Definition: rpp_vecmat.cpp:605
real_t quat_norm(const quat_t &q)
Definition: rpp_vecmat.cpp:764
void vec3_mul_vec3trans(mat33_t &m, const vec3_t &va, const vec3_t &vb)
Definition: rpp_vecmat.cpp:498
int svdcmp(double **a, int m, int n, double *w, double **v)
Definition: rpp_svd.cpp:61
void vec3_array_mean(vec3_t &v_mean, const vec3_array &va)
Definition: rpp_vecmat.cpp:490
void vec3_clear(vec3_t &v)
Definition: rpp_vecmat.cpp:304
void free_float_pptr(float ***m_ptr)
Definition: rpp_vecmat.cpp:133
real_t _abs(real_t a)
Definition: rpp_vecmat.cpp:72
void vec3_sub(vec3_t &va, const real_t f)
Definition: rpp_vecmat.cpp:403
void vec3_array_sum(vec3_t &v_sum2, const vec3_array &va)
Definition: rpp_vecmat.cpp:319
void scalar_array_sub(scalar_array &sa, real_t f)
Definition: rpp_vecmat.cpp:925
void mat33_copy(mat33_t &md, const mat33_t &ms)
Definition: rpp_vecmat.cpp:526
void _dbg_quat_print(const quat_t &q, char *name)
Definition: rpp_vecmat.cpp:195
void _dbg_vec3_array_print(const vec3_array &v, char *name)
Definition: rpp_vecmat.cpp:250
void vec3_div(vec3_t &va, const real_t n)
Definition: rpp_vecmat.cpp:354
void mat33_pow2(mat33_t &m)
Definition: rpp_vecmat.cpp:933
real_t _atan2(real_t a, real_t b)
Definition: rpp_vecmat.cpp:71
void mat33_transpose(mat33_t &t, const mat33_t m)
Definition: rpp_vecmat.cpp:704
ARFloat mat[3][4]
std::vector< mat33_t >::const_iterator mat33_array_const_iter
Definition: rpp_types.h:79
void _dbg_mat33_fprint(void *fp, const mat33_t &m, char *name)
Definition: rpp_vecmat.cpp:953
void mat33_add(mat33_t &mr, const mat33_t &ma, const mat33_t &mb)
Definition: rpp_vecmat.cpp:639
real_t mat33_sum(const mat33_t &m)
Definition: rpp_vecmat.cpp:573
void vec3_array_set(vec3_array &va, const vec3_t &a, const bool mask[3])
Definition: rpp_vecmat.cpp:468
void vec3_copy(vec3_t &a, const vec3_t &b)
Definition: rpp_vecmat.cpp:311
void scalar_array_div(scalar_array &sa, real_t f)
Definition: rpp_vecmat.cpp:894
real_t _acos(real_t a)
Definition: rpp_vecmat.cpp:73
void vec3_assign(vec3_t &v, const real_t x, const real_t y, const real_t z)
Definition: rpp_vecmat.cpp:297
real_t s
Definition: rpp_types.h:100
void vec3_mult(vec3_t &va, const real_t n)
Definition: rpp_vecmat.cpp:368


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun Sep 4 2016 03:24:33