Code_TYMPAN  4.4.0
Industrial site acoustic simulation
mathlib.h
Go to the documentation of this file.
1 /* WARNING: I am an auto-generated header file, don't modify me !! */
2 /* Modify Tympan/models/common/mathlib.h instead */
3 /*
4  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 // #include "std_tools.hpp"
19 #include <cassert>
20 #include <math.h>
21 #include <stdlib.h>
22 #include <float.h>
23 #include <vector>
24 #include <boost/foreach.hpp>
25 
26 #include "3d.h"
27 
34 #ifndef __HMATHLIB__
35  #define __HMATHLIB__
36 
40 namespace core_mathlib
41 {
42 // Activation or not (if commented) of targeting operations
43 // #define _ALLOW_TARGETING_ XXX
44 
45 typedef float decimal;
46 
47 typedef unsigned int bitSet;
49  #ifndef EPSILON_4 // was BARELY_EPSILON before
50  #define EPSILON_4 (decimal)0.0001 // 10e-4
51  #endif
52 
53  #ifndef EPSILON_6
54  #define EPSILON_6 \
55  (decimal)0.000001 // 10e-6 /*!< Approximation when comparing 2 decimal */
56  #endif
57 
58  #ifndef EPSILON_15
59  #define EPSILON_15 \
60  (decimal)0.000000000000001 // 10e-15 /*!< Approximation when comparing 2
61  // decimal */
62  #endif
63 
64  #ifndef M_PI
65  #define M_PI (decimal)3.141592653589793238462643383279
66  #endif
67 
68  #define M_PIDIV2 (decimal)1.570796326794896619231321691639
70  #ifndef M_2PI
71  #define M_2PI (decimal)6.283185307179586476925286766559
72  #endif
73 
74  #ifndef M_4PI
75  #define M_4PI (decimal)12.566370614359172953850573533118
76  #endif
77 
78  #define M_PI2 (decimal)9.869604401089358618834490999876
79  #define M_PIDIV180 (decimal)0.01745329251994329576923690768488
80  #define M_180DIVPI (decimal)57.295779513082320876798154814105
82  #define DegToRad(a) a *= M_PIDIV180
83  #define RadToDeg(a) a *= M_180DIVPI
84  #define RADIANS(a) a* M_PIDIV180
85  #define DEGRES(a) a* M_180DIVPI
86  #define ABS(x) (fabs(x))
87 
88  #ifndef SIGN
89  #define SIGN(x) ((x) > 0 ? 1 : -1)
90  #endif
91 
92 class vec2;
93 class vec4;
94 class ivec2;
95 class ivec3;
96 class ivec4;
97 /*****************************************************************************/
98 /* */
99 /* vec3 */
100 /* */
101 /*****************************************************************************/
102 
107 template <typename base_t> class base_vec3
108 {
109 public:
110  base_vec3(void) : x(0), y(0), z(0) {}
111  base_vec3(const base_t& _x, const base_t& _y, const base_t& _z) : x(_x), y(_y), z(_z) {}
112  base_vec3(const base_t* _v) : x(_v[0]), y(_v[1]), z(_v[2]) {}
113  base_vec3(const vec2& _v, base_t _z);
114  base_vec3(const base_vec3& _v) : x(_v.x), y(_v.y), z(_v.z) {}
115  base_vec3(const base_vec3* _v) : x(_v->x), y(_v->y), z(_v->z) {}
116  base_vec3(const base_vec3& _v, const base_vec3& _w)
117  : x(_w.x - _v.x), y(_w.y - _v.y), z(_w.z - _v.z) {}
118 
119  base_vec3(const vec4& _v);
120  base_t get_x()
121  {
122  return this->x;
123  }
124  base_t get_y()
125  {
126  return this->y;
127  }
128  base_t get_z()
129  {
130  return this->z;
131  }
132 
133  bool operator==(const base_vec3& _v)
134  {
135  return (fabs(this->x - _v.x) < EPSILON_6 && fabs(this->y - _v.y) < EPSILON_6 &&
136  fabs(this->z - _v.z) < EPSILON_6);
137  }
138  bool operator!=(const base_vec3& _v)
139  {
140  return !(*this == _v);
141  }
142 
143  base_vec3& operator=(base_t _f)
144  {
145  this->x = _f;
146  this->y = _f;
147  this->z = _f;
148  return (*this);
149  }
150  const base_vec3 operator*(base_t _f) const
151  {
152  return base_vec3(this->x * _f, this->y * _f, this->z * _f);
153  }
154  const base_vec3 operator/(base_t _f) const
155  {
156  if (fabs(_f) < EPSILON_6)
157  {
158  return *this;
159  }
160  _f = 1.0f / _f;
161  return (*this) * _f;
162  }
163  base_vec3 operator/(const base_vec3& _v) const
164  {
165  return base_vec3(x / _v.x, y / _v.y, z / _v.z);
166  }
167  const base_vec3 operator+(const base_vec3& _v) const
168  {
169  return base_vec3(this->x + _v.x, this->y + _v.y, this->z + _v.z);
170  }
171  const base_vec3 operator-() const
172  {
173  return base_vec3(-this->x, -this->y, -this->z);
174  }
175  const base_vec3 operator-(const base_vec3& _v) const
176  {
177  return base_vec3(this->x - _v.x, this->y - _v.y, this->z - _v.z);
178  }
179 
180  base_vec3& operator*=(base_t _f)
181  {
182  return *this = *this * _f;
183  }
184  base_vec3& operator/=(base_t _f)
185  {
186  return *this = *this / _f;
187  }
189  {
190  return *this = *this + _v;
191  }
193  {
194  return *this = *this - _v;
195  }
196 
197  base_t operator*(const base_vec3& _v) const
198  {
199  return this->x * _v.x + this->y * _v.y + this->z * _v.z;
200  }
201  base_vec3 operator^(const base_vec3& _v) const
202  {
203  return base_vec3(this->y * _v.z - this->z * _v.y, this->z * _v.x - this->x * _v.z,
204  this->x * _v.y - this->y * _v.x);
205  }
206 
207  base_t operator*(const vec4& _v) const;
208 
209  operator base_t*()
210  {
211  return this->v;
212  }
213  operator const base_t*() const
214  {
215  return this->v;
216  }
217  base_t& operator[](int _i)
218  {
219  return this->v[_i];
220  }
221  const base_t& operator[](int _i) const
222  {
223  return this->v[_i];
224  }
225 
226  bool barelyEqual(const base_vec3& _v) const
227  {
228  return (fabs(this->x - _v.x) < 0.5 && fabs(this->y - _v.y) < 0.5 && fabs(this->z - _v.z) < 0.5);
229  }
230  void set(base_t _x, base_t _y, base_t _z)
231  {
232  this->x = _x;
233  this->y = _y;
234  this->z = _z;
235  }
236  void reset(void)
237  {
238  this->x = this->y = this->z = 0;
239  }
240  base_t length(void) const
241  {
242  return sqrtf(this->x * this->x + this->y * this->y + this->z * this->z);
243  }
244  base_t normalize(void)
245  {
246  base_t inv, l = this->length();
247  if (l < EPSILON_6)
248  {
249  return 0.0f;
250  }
251  inv = 1.0f / l;
252  this->x *= inv;
253  this->y *= inv;
254  this->z *= inv;
255  return l;
256  }
257  void cross(const base_vec3& v1, const base_vec3& v2)
258  {
259  this->x = v1.y * v2.z - v1.z * v2.y;
260  this->y = v1.z * v2.x - v1.x * v2.z;
261  this->z = v1.x * v2.y - v1.y * v2.x;
262  }
263  void cross(const base_vec3& v2)
264  {
265  base_t x = this->y * v2.z - this->z * v2.y;
266  base_t y = this->z * v2.x - this->x * v2.z;
267  this->z = this->x * v2.y - this->y * v2.x;
268  this->y = y;
269  this->x = x;
270  }
271  base_t cosinus(const base_vec3& ac)
272  {
273  return (this->x * ac.x + this->y * ac.y + this->z * ac.z) / (length() * ac.length());
274  }
275  base_t dot(const base_vec3& v) const
276  {
277  return ((this->x * v.x) + (this->y * v.y) + (this->z * v.z));
278  }
279  // this < _v
280  bool compare(const base_vec3& _v, base_t epsi = EPSILON_6)
281  {
282  return (fabs(this->x - _v.x) < epsi && fabs(this->y - _v.y) < epsi && fabs(this->z - _v.z) < epsi);
283  }
285  base_t angle(const base_vec3& v) const
286  {
287  base_t angle = acos(this->dot(v) / (this->length() * v.length()));
288  if (angle < EPSILON_6)
289  {
290  return 0;
291  }
292  return angle;
293  }
295  base_vec3 closestPointOnLine(const base_vec3& vA, const base_vec3& vB) const
296  {
297  return (((vB - vA) * this->projectionOnLine(vA, vB)) + vA);
298  }
301  {
302  base_t factor = this->projectionOnLine(vA, vB);
303  if (factor <= 0.0f)
304  {
305  return vA;
306  }
307  if (factor >= 1.0f)
308  {
309  return vB;
310  }
311  return (((vB - vA) * factor) + vA);
312  }
314  base_t projectionOnLine(const base_vec3& vA, const base_vec3& vB) const
315  {
316  base_vec3 v(vB - vA);
317  return v.dot(*this - vA) / v.dot(v);
318  }
320  base_vec3 lerp(base_vec3& u, base_vec3& v, base_t factor)
321  {
322  return ((u * (1 - factor)) + (v * factor));
323  }
324 
330  decimal distance(const base_vec3& a_vector) const
331  {
332  // compute distance between both points
333  decimal dx = x - a_vector.x;
334  decimal dy = y - a_vector.y;
335  decimal dz = z - a_vector.z;
336 
337  // return result
338  return (sqrt(dx * dx + dy * dy + dz * dz));
339  }
349  base_vec3 Rotation(const base_vec3& n, const base_t& angle) const
350  {
351  base_t m1 = cos(angle);
352  base_t m2 = 1 - m1;
353  base_t m3 = sin(angle);
354 
355  base_t m2nx = m2 * n.x;
356 
357  return base_vec3((m1 + m2nx * n.x) * this->x + (m2nx * n.y - m3 * n.z) * this->y +
358  (m2nx * n.z + m3 * n.y) * this->z,
359  (m2nx * n.y + m3 * n.z) * this->x + (m1 + m2 * n.y * n.y) * this->y +
360  (m2 * n.y * n.z - m3 * n.x) * this->z,
361  (m2nx * n.z - m3 * n.y) * this->x + (m2 * n.y * n.z + m3 * n.x) * this->y +
362  (m1 + m2 * n.z * n.z) * this->z);
363  }
364  union
365  {
366  struct
367  {
368  base_t x, y, z;
369  };
370  struct
371  {
372  base_t s, t, p;
373  };
374  struct
375  {
376  base_t r, g, b;
377  };
378  base_t v[3];
379  };
380 };
383 
384 inline std::vector<vec3> operator*(const std::vector<vec3>& _v, const decimal& _a)
385 {
386  std::vector<vec3> res;
387  BOOST_FOREACH (vec3 vec, _v)
388  {
389  res.push_back(vec * _a);
390  }
391  return res;
392 }
393 
394 inline std::vector<vec3> operator+(const std::vector<vec3>& _u, const std::vector<vec3>& _v)
395 {
396  assert(_u.size() == _v.size());
397  std::vector<vec3> res;
398  for (unsigned int i = 0; i < _v.size(); ++i)
399  {
400  res.push_back(_u[i] + _v[i]);
401  }
402  return res;
403 }
404 
405 // Version double
406 inline std::vector<dvec3> operator*(const std::vector<dvec3>& _v, const decimal& _a)
407 {
408  std::vector<dvec3> res;
409  BOOST_FOREACH (dvec3 vec, _v)
410  {
411  res.push_back(vec * _a);
412  }
413  return res;
414 }
415 
416 inline std::vector<dvec3> operator+(const std::vector<dvec3>& _u, const std::vector<dvec3>& _v)
417 {
418  assert(_u.size() == _v.size());
419  std::vector<dvec3> res;
420  for (unsigned int i = 0; i < _v.size(); ++i)
421  {
422  res.push_back(_u[i] + _v[i]);
423  }
424  return res;
425 }
426 
431 inline OPoint3D vec3toOPoint3D(const vec3& _v)
432 {
433  return OPoint3D(static_cast<double>(_v.x), static_cast<double>(_v.y), static_cast<double>(_v.z));
434 }
435 
440 inline vec3 OPoint3Dtovec3(const OPoint3D& _p)
441 {
442  return vec3(static_cast<float>(_p._x), static_cast<float>(_p._y), static_cast<float>(_p._z));
443 }
444 
449 inline OVector3D vec3toOVector3D(const vec3& _v)
450 {
451  return OVector3D(static_cast<double>(_v.x), static_cast<double>(_v.y), static_cast<double>(_v.z));
452 }
453 
458 inline vec3 OVector3Dtovec3(const OVector3D& _v)
459 {
460  return vec3(static_cast<float>(_v._x), static_cast<float>(_v._y), static_cast<float>(_v._z));
461 }
462 
463 /*****************************************************************************/
464 /* */
465 /* vec2 */
466 /* */
467 /*****************************************************************************/
472 class vec2
473 {
474 public:
475  vec2(void) : x(0), y(0) {}
476  vec2(decimal _x, decimal _y) : x(_x), y(_y) {}
477  vec2(const decimal* _v) : x(_v[0]), y(_v[1]) {}
478  vec2(const vec2& _v) : x(_v.x), y(_v.y) {}
479  vec2(const vec2& _v, const vec2& _w) : x(_w.x - _v.x), y(_w.y - _v.y) {}
480  vec2(const vec3& _v);
481  vec2(const vec4& _v);
482 
483  int operator==(const vec2& _v)
484  {
485  return (fabs(this->x - _v.x) < EPSILON_6 && fabs(this->y - _v.y) < EPSILON_6);
486  }
487 
488  int operator!=(const vec2& _v)
489  {
490  return !(*this == _v);
491  }
492 
494  {
495  this->x = _f;
496  this->y = _f;
497  return (*this);
498  }
499  const vec2 operator*(decimal _f) const
500  {
501  return vec2(this->x * _f, this->y * _f);
502  }
503  const vec2 operator/(decimal _f) const
504  {
505  if (fabs(_f) < EPSILON_6)
506  {
507  return *this;
508  }
509  _f = 1.0f / _f;
510  return (*this) * _f;
511  }
512  const vec2 operator+(const vec2& _v) const
513  {
514  return vec2(this->x + _v.x, this->y + _v.y);
515  }
516  const vec2 operator-() const
517  {
518  return vec2(-this->x, -this->y);
519  }
520  const vec2 operator-(const vec2& _v) const
521  {
522  return vec2(this->x - _v.x, this->y - _v.y);
523  }
524 
525  decimal operator^(const vec2& _v) const
526  {
527  return this->x * _v.y - this->y * _v.x;
528  } // produit mixte
529 
531  {
532  return *this = *this * _f;
533  }
535  {
536  return *this = *this / _f;
537  }
538  vec2& operator+=(const vec2& _v)
539  {
540  return *this = *this + _v;
541  }
542  vec2& operator-=(const vec2& _v)
543  {
544  return *this = *this - _v;
545  }
546 
547  decimal operator*(const vec2& _v) const
548  {
549  return this->x * _v.x + this->y * _v.y;
550  }
551 
552  operator decimal*()
553  {
554  return this->v;
555  }
556  operator const decimal*() const
557  {
558  return this->v;
559  }
560  decimal& operator[](int _i)
561  {
562  return this->v[_i];
563  }
564  const decimal& operator[](int _i) const
565  {
566  return this->v[_i];
567  }
568 
569  void set(decimal _x, decimal _y)
570  {
571  this->x = _x;
572  this->y = _y;
573  }
574  void reset(void)
575  {
576  this->x = this->y = 0;
577  }
578  decimal length(void) const
579  {
580  return sqrtf(this->x * this->x + this->y * this->y);
581  }
582  decimal normalize(void)
583  {
584  decimal inv, l = this->length();
585  if (l < EPSILON_6)
586  {
587  return 0.0f;
588  }
589  inv = 1.0f / l;
590  this->x *= inv;
591  this->y *= inv;
592  return l;
593  }
595  {
596  return (this->x * v.y - this->y * v.x);
597  }
598  decimal dot(const vec2& v)
599  {
600  return ((this->x * v.x) + (this->y * v.y));
601  }
602  bool compare(const vec2& _v, decimal epsi = EPSILON_6)
603  {
604  return (fabs(this->x - _v.x) < epsi && fabs(this->y - _v.y) < epsi);
605  }
607  vec2 closestPointOnLine(const vec2& vA, const vec2& vB)
608  {
609  return (((vB - vA) * this->projectionOnLine(vA, vB)) + vA);
610  }
612  vec2 closestPointOnSegment(const vec2& vA, const vec2& vB)
613  {
614  decimal factor = this->projectionOnLine(vA, vB);
615  if (factor <= 0.0f)
616  {
617  return vA;
618  }
619  if (factor >= 1.0f)
620  {
621  return vB;
622  }
623  return (((vB - vA) * factor) + vA);
624  }
626  decimal projectionOnLine(const vec2& vA, const vec2& vB)
627  {
628  vec2 v(vB - vA);
629  return v.dot(*this - vA) / v.dot(v);
630  }
632  vec2 lerp(vec2& u, vec2& v, decimal factor)
633  {
634  return ((u * (1 - factor)) + (v * factor));
635  }
637  {
638  return (decimal)atan2(this->y, this->x);
639  }
640  decimal angle(const vec2& v)
641  {
642  return (decimal)atan2(v.y - this->y, v.x - this->x);
643  }
644 
645  union
646  {
647  struct
648  {
650  };
651  struct
652  {
654  };
655  decimal v[2];
656  };
657 };
658 
659 inline decimal area(const vec2& A, const vec2& B, const vec2& C)
660 {
661  return vec2(A, B) ^ vec2(A, C) * 0.5;
662 }
663 
664 inline void Cross(const vec3& v1, const vec3& v2, vec3& vout)
665 {
666  vout.x = v1.y * v2.z - v1.z * v2.y;
667  vout.y = v1.z * v2.x - v1.x * v2.z;
668  vout.z = v1.x * v2.y - v1.y * v2.x;
669 }
670 inline vec3 Cross_r(const vec3& v1, const vec3& v2)
671 {
672  return vec3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
673 }
674 template <typename base_t>
675 inline base_vec3<base_t>::base_vec3(const vec2& _v, base_t _z) : x(_v.x), y(_v.y), z(_z)
676 {
677 }
678 inline vec2::vec2(const vec3& _v)
679 {
680  this->x = _v.x;
681  this->y = _v.y;
682 }
683 
684 // This calculates a vector between 2 points and returns the result
685 inline void Vector(const vec3& vp1, const vec3& vp2, vec3& vout)
686 {
687 
688  vout.x = vp1.x - vp2.x;
689  vout.y = vp1.y - vp2.y;
690  vout.z = vp1.z - vp2.z;
691 }
692 inline vec3 Vector_r(const vec3& vp1, const vec3& vp2)
693 {
694  return vec3(vp1.x - vp2.x, vp1.y - vp2.y, vp1.z - vp2.z);
695 }
699 inline decimal Determinant(const vec3& vp1, const vec3& vp2, const vec3& vp3)
700 {
701  return vp1.x * vp2.y * vp3.z + vp1.y * vp2.z * vp3.x + vp1.z * vp2.x * vp3.y - vp1.x * vp2.z * vp3.y -
702  vp1.y * vp2.x * vp3.z - vp1.z * vp2.y * vp3.x;
703 }
704 
705 /*****************************************************************************/
706 /* */
707 /* vec4 */
708 /* */
709 /*****************************************************************************/
710 
711 inline vec3 FaceNormal(const vec3& vp1, const vec3& vp2, const vec3& vp3)
712 {
713 
714  vec3 vret = Cross_r(Vector_r(vp1, vp2), Vector_r(vp2, vp3));
715  vret.normalize();
716  return vret;
717 }
722 class vec4
723 {
724 public:
725  vec4(void) : x(0), y(0), z(0), w(1) {}
726  vec4(decimal _x, decimal _y, decimal _z, decimal _w) : x(_x), y(_y), z(_z), w(_w) {}
727  vec4(const decimal* _v) : x(_v[0]), y(_v[1]), z(_v[2]), w(_v[3]) {}
728  vec4(const vec3& _v) : x(_v.x), y(_v.y), z(_v.z), w(1) {}
729  vec4(const vec3& _v, decimal _w) : x(_v.x), y(_v.y), z(_v.z), w(_w) {}
730  vec4(const vec4& _v) : x(_v.x), y(_v.y), z(_v.z), w(_v.w) {}
731 
732  int operator==(const vec4& _v)
733  {
734  return (fabs(this->x - _v.x) < EPSILON_6 && fabs(this->y - _v.y) < EPSILON_6 &&
735  fabs(this->z - _v.z) < EPSILON_6 && fabs(this->w - _v.w) < EPSILON_6);
736  }
737  int operator!=(const vec4& _v)
738  {
739  return !(*this == _v);
740  }
741 
743  {
744  this->x = _f;
745  this->y = _f;
746  this->z = _f;
747  this->w = _f;
748  return (*this);
749  }
750  const vec4 operator*(decimal _f) const
751  {
752  return vec4(this->x * _f, this->y * _f, this->z * _f, this->w * _f);
753  }
754  const vec4 operator/(decimal _f) const
755  {
756  if (fabs(_f) < EPSILON_6)
757  {
758  return *this;
759  }
760  _f = 1.0f / _f;
761  return (*this) * _f;
762  }
763  const vec4 operator+(const vec4& _v) const
764  {
765  return vec4(this->x + _v.x, this->y + _v.y, this->z + _v.z, this->w + _v.w);
766  }
767  const vec4 operator-() const
768  {
769  return vec4(-x, -y, -z, -w);
770  }
771  const vec4 operator-(const vec4& _v) const
772  {
773  return vec4(this->x - _v.x, this->y - _v.y, this->z - _v.z, this->w - _v.w);
774  }
775 
777  {
778  return *this = *this * _f;
779  }
781  {
782  return *this = *this / _f;
783  }
784  vec4& operator+=(const vec4& _v)
785  {
786  return *this = *this + _v;
787  }
788  vec4& operator-=(const vec4& _v)
789  {
790  return *this = *this - _v;
791  }
792 
793  decimal operator*(const vec3& _v) const
794  {
795  return this->x * _v.x + this->y * _v.y + this->z * _v.z + this->w;
796  }
797  decimal operator*(const vec4& _v) const
798  {
799  return this->x * _v.x + this->y * _v.y + this->z * _v.z + this->w * _v.w;
800  }
801 
802  operator decimal*()
803  {
804  return this->v;
805  }
806  operator const decimal*() const
807  {
808  return this->v;
809  }
810  // decimal &operator[](int _i) { return this->v[_i]; }
811  // const decimal &operator[](int _i) const { return this->v[_i]; }
812 
813  void set(decimal _x, decimal _y, decimal _z, decimal _w)
814  {
815  this->x = _x;
816  this->y = _y;
817  this->z = _z;
818  this->w = _w;
819  }
820  void reset(void)
821  {
822  this->x = this->y = this->z = this->w = 0;
823  }
824  bool compare(const vec4& _v, decimal epsi = EPSILON_6)
825  {
826  return (fabs(this->x - _v.x) < epsi && fabs(this->y - _v.y) < epsi && fabs(this->z - _v.z) < epsi &&
827  fabs(this->w - _v.w) < epsi);
828  }
829 
830  union
831  {
832  struct
833  {
834  decimal x, y, z, w;
835  };
836  struct
837  {
838  decimal s, t, p, q;
839  };
840  struct
841  {
842  decimal r, g, b, a;
843  };
844  struct
845  {
847  };
848  decimal v[4];
849  };
850 };
859 inline decimal Determinant(const vec4& vp1, const vec4& vp2, const vec4& vp3, const vec4& vp4)
860 {
861  return -(vp1.w * (vp2.x * (vp3.y * vp4.z - vp4.y * vp3.z) - vp3.x * (vp2.y * vp4.z - vp4.y * vp2.z) +
862  vp4.x * (vp2.y * vp3.z - vp3.y * vp2.z)) -
863  vp2.w * (vp1.x * (vp3.y * vp4.z - vp4.y * vp3.z) - vp3.x * (vp1.y * vp4.z - vp4.y * vp1.z) +
864  vp4.x * (vp1.y * vp3.z - vp3.y * vp1.z)) +
865  vp3.w * (vp1.x * (vp2.y * vp4.z - vp4.y * vp2.z) - vp2.x * (vp1.y * vp4.z - vp4.y * vp1.z) +
866  vp4.x * (vp1.y * vp2.z - vp2.y * vp1.z)) -
867  vp4.w * (vp1.x * (vp2.y * vp3.z - vp3.y * vp2.z) - vp2.x * (vp1.y * vp3.z - vp3.y * vp1.z) +
868  vp3.x * (vp1.y * vp2.z - vp2.y * vp1.z)));
869 }
878 inline decimal Determinant(const vec3& vp1, const vec3& vp2, const vec3& vp3, const vec3& vp4)
879 {
880  return vp1.x * (vp2.y * (vp3.z - vp4.z) - vp3.y * (vp2.z - vp4.z) + vp4.y * (vp2.z - vp3.z)) -
881  vp2.x * (vp1.y * (vp3.z - vp4.z) - vp3.y * (vp1.z - vp4.z) + vp4.y * (vp1.z - vp3.z)) +
882  vp3.x * (vp1.y * (vp2.z - vp4.z) - vp2.y * (vp1.z - vp4.z) + vp4.y * (vp1.z - vp2.z)) -
883  vp4.x * (vp1.y * (vp2.z - vp3.z) - vp2.y * (vp1.z - vp3.z) + vp3.y * (vp1.z - vp2.z));
884 }
885 
886 template <typename base_t> inline base_vec3<base_t>::base_vec3(const vec4& _v)
887 {
888  this->x = _v.x;
889  this->y = _v.y;
890  this->z = _v.z;
891 }
892 template <typename base_t> inline base_t base_vec3<base_t>::operator*(const vec4& _v) const
893 {
894  return this->x * _v.x + this->y * _v.y + this->z * _v.z + _v.w;
895 }
896 
897 inline vec2::vec2(const vec4& _v)
898 {
899  this->x = _v.x;
900  this->y = _v.y;
901 }
902 inline bool colinear(const vec3& A, const vec3& B, const vec3& C, const decimal& aproximation)
903 {
904  vec3 AB = B - A;
905  vec3 AC = C - A;
906  decimal diff = (AB / AB.length() - AC / AC.length()).length();
907  return diff < aproximation;
908 }
909 /*****************************************************************************/
910 /* */
911 /* ivec2 */
912 /* */
913 /*****************************************************************************/
914 
919 class ivec2
920 {
921 public:
922  ivec2(void) : a(0), b(0) {}
923  ivec2(long _a, long _b) : a(_a), b(_b) {}
924  ivec2(const long* iv) : a(iv[0]), b(iv[1]) {}
925  ivec2(const ivec2& iv) : a(iv.a), b(iv.b) {}
926 
927  int operator==(const ivec2& iv)
928  {
929  return ((this->a == iv.a) && (this->b == iv.b));
930  }
931  int operator!=(const ivec2& iv)
932  {
933  return !(*this == iv);
934  }
935 
936  ivec2& operator=(long _i)
937  {
938  this->x = _i;
939  this->y = _i;
940  return (*this);
941  }
942  const ivec2 operator*(long _i) const
943  {
944  return ivec2(this->a * _i, this->b * _i);
945  }
946  const ivec2 operator/(long _i) const
947  {
948  return ivec2(this->a / _i, this->b / _i);
949  }
950  const ivec2 operator+(const ivec2& iv) const
951  {
952  return ivec2(this->a + iv.a, this->b + iv.b);
953  }
954  const ivec2 operator-() const
955  {
956  return ivec2(-this->a, -this->b);
957  }
958  const ivec2 operator-(const ivec2& iv) const
959  {
960  return ivec2(this->a - iv.a, this->b - iv.b);
961  }
962 
963  ivec2& operator*=(long _i)
964  {
965  return *this = *this * _i;
966  }
967  ivec2& operator/=(long _i)
968  {
969  return *this = *this / _i;
970  }
971  ivec2& operator+=(const ivec2& iv)
972  {
973  return *this = *this + iv;
974  }
975  ivec2& operator-=(const ivec2& iv)
976  {
977  return *this = *this - iv;
978  }
979 
980  long operator*(const ivec2& iv) const
981  {
982  return this->a * iv.a + this->b * iv.b;
983  }
984 
985  operator long*()
986  {
987  return this->i;
988  }
989  operator const long*() const
990  {
991  return this->i;
992  }
993  // long &operator[](int _i) { return this->i[_i]; }
994  // const long &operator[](int _i) const { return this->i[_i]; }
995 
996  void set(long _a, long _b)
997  {
998  this->a = _a;
999  this->b = _b;
1000  }
1001  void reset(void)
1002  {
1003  this->a = this->b = 0;
1004  }
1005  void swap(ivec2& iv)
1006  {
1007  long tmp = a;
1008  a = iv.a;
1009  iv.a = tmp;
1010  tmp = b;
1011  b = iv.b;
1012  iv.b = tmp;
1013  }
1014  void swap(ivec2* iv)
1015  {
1016  this->swap(*iv);
1017  }
1018 
1019  union
1020  {
1021  struct
1022  {
1023  long a, b;
1024  };
1025  struct
1026  {
1027  long x, y;
1028  };
1029  long i[2];
1030  };
1031 };
1032 
1033 /*****************************************************************************/
1034 /* */
1035 /* ivec3 */
1036 /* */
1037 /*****************************************************************************/
1038 
1043 class ivec3
1044 {
1045 public:
1046  ivec3(void) : a(0), b(0), c(0) {}
1047  ivec3(long _a, long _b, long _c) : a(_a), b(_b), c(_c) {}
1048  ivec3(const long* iv) : a(iv[0]), b(iv[1]), c(iv[2]) {}
1049  ivec3(const ivec3& iv) : a(iv.a), b(iv.b), c(iv.c) {}
1050  ivec3(const ivec4& iv);
1051 
1052  int operator==(const ivec3& iv)
1053  {
1054  return ((this->a == iv.a) && (this->b == iv.b) && (this->c == iv.c));
1055  }
1056  int operator!=(const ivec3& iv)
1057  {
1058  return !(*this == iv);
1059  }
1060 
1061  ivec3& operator=(long _i)
1062  {
1063  this->x = _i;
1064  this->y = _i;
1065  this->z = _i;
1066  return (*this);
1067  }
1069  {
1070  this->a = (long)iv[0];
1071  this->b = (long)iv[1];
1072  this->c = (long)iv[2];
1073  return (*this);
1074  }
1075  const ivec3 operator*(long _i) const
1076  {
1077  return ivec3(this->a * _i, this->b * _i, this->c * _i);
1078  }
1079  const ivec3 operator/(long _i) const
1080  {
1081  return ivec3(this->a / _i, this->b / _i, this->c / _i);
1082  }
1083  const ivec3 operator+(const ivec3& iv) const
1084  {
1085  return ivec3(this->a + iv.a, this->b + iv.b, this->c + iv.c);
1086  }
1087  const ivec3 operator-() const
1088  {
1089  return ivec3(-this->a, -this->b, -this->c);
1090  }
1091  const ivec3 operator-(const ivec3& iv) const
1092  {
1093  return ivec3(this->a - iv.a, this->b - iv.b, this->c - iv.c);
1094  }
1095 
1096  ivec3& operator*=(long _i)
1097  {
1098  return *this = *this * _i;
1099  }
1100  ivec3& operator/=(long _i)
1101  {
1102  return *this = *this / _i;
1103  }
1104  ivec3& operator+=(const ivec3& iv)
1105  {
1106  return *this = *this + iv;
1107  }
1108  ivec3& operator-=(const ivec3& iv)
1109  {
1110  return *this = *this - iv;
1111  }
1112 
1113  long operator*(const ivec3& iv) const
1114  {
1115  return this->a * iv.a + this->b * iv.b + this->c * iv.c;
1116  }
1117  long operator*(const ivec4& iv) const;
1118 
1119  operator long*()
1120  {
1121  return this->i;
1122  }
1123  operator const long*() const
1124  {
1125  return this->i;
1126  }
1127  // long &operator[](int _i) { return this->i[_i]; }
1128  // const long &operator[](int _i) const { return this->i[_i]; }
1129 
1130  void set(long _a, long _b, long _c)
1131  {
1132  this->a = _a;
1133  this->b = _b;
1134  this->c = _c;
1135  }
1136  void set(int tab[3])
1137  {
1138  this->a = tab[0];
1139  this->b = tab[1];
1140  this->c = tab[2];
1141  }
1142  void reset(void)
1143  {
1144  this->a = this->b = this->c = 0;
1145  }
1146  void swap(ivec3& iv)
1147  {
1148  long tmp = a;
1149  a = iv.a;
1150  iv.a = tmp;
1151  tmp = b;
1152  b = iv.b;
1153  iv.b = tmp;
1154  tmp = c;
1155  c = iv.c;
1156  iv.c = tmp;
1157  }
1158  void swap(ivec3* iv)
1159  {
1160  this->swap(*iv);
1161  }
1162 
1163  union
1164  {
1165  struct
1166  {
1167  long a, b, c;
1168  };
1169  struct
1170  {
1171  long x, y, z;
1172  };
1173  struct
1174  {
1175  long red, green, blue;
1176  };
1177  long i[3];
1178  };
1179 };
1180 
1181 /*****************************************************************************/
1182 /* */
1183 /* ivec4 */
1184 /* */
1185 /*****************************************************************************/
1186 
1191 class ivec4
1192 {
1193 public:
1194  ivec4(void) : a(0), b(0), c(0), d(1) {}
1195  ivec4(long _a, long _b, long _c, long _d) : a(_a), b(_b), c(_c), d(_d) {}
1196  ivec4(const long* iv) : a(iv[0]), b(iv[1]), c(iv[2]), d(iv[3]) {}
1197  ivec4(const ivec3& iv) : a(iv.a), b(iv.b), c(iv.c), d(1) {}
1198  ivec4(const ivec3& iv, long _d) : a(iv.a), b(iv.b), c(iv.c), d(_d) {}
1199  ivec4(const ivec4& iv) : a(iv.a), b(iv.b), c(iv.c), d(iv.d) {}
1200 
1201  int operator==(const ivec4& iv)
1202  {
1203  return ((this->a == iv.a) && (this->b == iv.b) && (this->c == iv.c) && (this->d == iv.d));
1204  }
1205  int operator!=(const ivec4& iv)
1206  {
1207  return !(*this == iv);
1208  }
1209 
1210  ivec4& operator=(long _i)
1211  {
1212  this->x = _i;
1213  this->y = _i;
1214  this->z = _i;
1215  this->w = _i;
1216  return (*this);
1217  }
1218  const ivec4 operator*(long _i) const
1219  {
1220  return ivec4(this->a * _i, this->b * _i, this->c * _i, this->d * _i);
1221  }
1222  const ivec4 operator/(long _i) const
1223  {
1224  return ivec4(this->a / _i, this->b / _i, this->c / _i, this->d / _i);
1225  }
1226  const ivec4 operator+(const ivec4& iv) const
1227  {
1228  return ivec4(this->a + iv.a, this->b + iv.b, this->c + iv.c, this->d + iv.d);
1229  }
1230  const ivec4 operator-() const
1231  {
1232  return ivec4(-this->a, -this->b, -this->c, -this->d);
1233  }
1234  const ivec4 operator-(const ivec4& iv) const
1235  {
1236  return ivec4(this->a - iv.a, this->b - iv.b, this->c - iv.c, this->d - iv.d);
1237  }
1238 
1239  ivec4& operator*=(long _i)
1240  {
1241  return *this = *this * _i;
1242  }
1243  ivec4& operator/=(long _i)
1244  {
1245  return *this = *this / _i;
1246  }
1247  ivec4& operator+=(const ivec4& iv)
1248  {
1249  return *this = *this + iv;
1250  }
1251  ivec4& operator-=(const ivec4& iv)
1252  {
1253  return *this = *this - iv;
1254  }
1255 
1256  long operator*(const ivec3& iv) const
1257  {
1258  return this->a * iv.a + this->b * iv.b + this->c * iv.c + this->d;
1259  }
1260  long operator*(const ivec4& iv) const
1261  {
1262  return this->a * iv.a + this->b * iv.b + this->c * iv.c + this->d * iv.d;
1263  }
1264 
1265  operator long*()
1266  {
1267  return this->i;
1268  }
1269  operator const long*() const
1270  {
1271  return this->i;
1272  }
1273  // long &operator[](int _i) { return this->i[_i]; }
1274  // const long &operator[](int _i) const { return this->i[_i]; }
1275 
1276  void set(long _a, long _b, long _c, long _d)
1277  {
1278  this->a = _a;
1279  this->b = _b;
1280  this->c = _c;
1281  this->d = _d;
1282  }
1283  void reset(void)
1284  {
1285  this->a = this->b = this->c = this->d = 0;
1286  }
1287  void swap(ivec4& iv)
1288  {
1289  long tmp = a;
1290  a = iv.a;
1291  iv.a = tmp;
1292  tmp = b;
1293  b = iv.b;
1294  iv.b = tmp;
1295  tmp = c;
1296  c = iv.c;
1297  iv.c = tmp;
1298  tmp = d;
1299  d = iv.d;
1300  iv.d = tmp;
1301  }
1302  void swap(ivec4* iv)
1303  {
1304  this->swap(*iv);
1305  }
1306 
1307  union
1308  {
1309  struct
1310  {
1311  long a, b, c, d;
1312  };
1313  struct
1314  {
1315  long x, y, z, w;
1316  };
1317  struct
1318  {
1319  long red, green, blue, alpha;
1320  };
1321  long i[4];
1322  };
1323 };
1324 
1325 inline ivec3::ivec3(const ivec4& iv)
1326 {
1327  this->a = iv.a;
1328  this->b = iv.b;
1329  this->c = iv.c;
1330 }
1331 
1332 inline long ivec3::operator*(const ivec4& iv) const
1333 {
1334  return this->a * iv.a + this->b * iv.b + this->c * iv.c + iv.d;
1335 }
1336 
1338 {
1339  A -= D;
1340  B -= D;
1341  C -= D;
1342  return fabs((A.dot(Cross_r(B, C))) / 6.f);
1343 }
1347 inline vec3 GetGTriangle(const vec3& A, const vec3& B, const vec3& C)
1348 {
1349  vec3 I = (B + C) / 2;
1350  vec3 AG = (I - A) * (2.f / 3.f);
1351  return A + AG;
1352 }
1353 inline vec3 GetGTetra(const vec3& A, const vec3& B, const vec3& C, const vec3& D)
1354 {
1355  return (A + B + C + D) / 4;
1356 }
1357 inline decimal GetAireTriangle(const vec3& a, const vec3& b, const vec3& c)
1358 {
1359  vec3 ab;
1360  vec3 ac;
1361  Vector(a, b, ab);
1362  Vector(a, c, ac);
1363  ab.cross(ac);
1364  return .5f * ab.length();
1365 }
1366 
1367 inline float Clamp(float val, float low, float high)
1368 {
1369  if (val < low)
1370  {
1371  return low;
1372  }
1373  else if (val > high)
1374  {
1375  return high;
1376  }
1377  else
1378  {
1379  return val;
1380  }
1381 }
1382 
1383 inline int Clamp(int val, int low, int high)
1384 {
1385  if (val < low)
1386  {
1387  return low;
1388  }
1389  else if (val > high)
1390  {
1391  return high;
1392  }
1393  else
1394  {
1395  return val;
1396  }
1397 }
1398 
1399 inline int Floor2Int(float val)
1400 {
1401  return (int)floorf(val);
1402 }
1403 
1404 inline int Round2Int(float val)
1405 {
1406  return Floor2Int(val + 0.5f);
1407 }
1408 /*
1409  * @param ecart Au maximum 2*PI au minimum 0
1410  * @return Vrai si Si ecart==0 ou ecart>0.1
1411  */
1412 /*inline bool DotIsInVertex(const vec3& dot,const vec3& va,const vec3& vb,const vec3& vc,decimal* ecart=NULL)
1413 { //retourne vrai si le point est dans un triangle, le total a la fin correspond a environ 2PI si c'est le cas
1414  decimal totangle=0;
1415  // calcul des vecteurs des cotes
1416  vec3 vda(dot-va);
1417  vec3 vdb(dot-vb);
1418  vec3 vdc(dot-vc);
1419  //Calcul de la somme des angles sur le plan xy
1420  totangle+=vda.angle(vdb);
1421  totangle+=vda.angle(vdc);
1422  totangle+=vdb.angle(vdc);
1423  if(ecart)
1424  *ecart=fabs(M_2PI-totangle);
1425  if(int(totangle*10)==int(M_2PI*10) || !st_isfinite(totangle))
1426  return true;
1427  else
1428  return false;
1429 }*/
1430 /*
1431  * Retourne vrai si le point dotTest est dans le tetraedre forme par les sommets v1,v2,v3,v4
1432  * @ref http://steve.hollasch.net/cgindex/geometry/ptintet.html
1433  */
1434 /*static bool DotInTetra(const vec3& dotTest,const vec3& v1,const vec3& v2,const vec3& v3,const vec3& v4)
1435 {
1436  int sd0=SIGN(Determinant(v1,v2,v3,v4));
1437  if(SIGN(Determinant(dotTest,v2,v3,v4))!=sd0)
1438  return false;
1439  if(SIGN(Determinant(v1,dotTest,v3,v4))!=sd0)
1440  return false;
1441  if(SIGN(Determinant(v1,v2,dotTest,v4))!=sd0)
1442  return false;
1443  if(SIGN(Determinant(v1,v2,v3,dotTest))!=sd0)
1444  return false;
1445  return true;
1446 }*/
1447 
1460 inline bool LineLineIntersect(const vec3& p1, const vec3& p2, const vec3& p3, const vec3& p4, vec3* pa,
1461  vec3* pb, decimal* mua, decimal* mub)
1462 {
1463  vec3 w, v, u;
1464  decimal vw, uv, uw, vv, uu;
1465  decimal numer, denom;
1466 
1467  // compute the vector u defined by the first segment
1468  u.x = p2.x - p1.x;
1469  u.y = p2.y - p1.y;
1470  u.z = p2.z - p1.z;
1471 
1472  // p1 and p2 coincide, the first segment is reduced to a point.
1473  if (fabs(u.x) < EPSILON_6 && fabs(u.y) < EPSILON_6 && fabs(u.z) < EPSILON_6)
1474  {
1475  return false;
1476  }
1477 
1478  // compute the vector v defined by the second segment
1479  v.x = p4.x - p3.x;
1480  v.y = p4.y - p3.y;
1481  v.z = p4.z - p3.z;
1482 
1483  // p4 and p3 coincide, the second segment is reduced to a point.
1484  if (fabs(v.x) < EPSILON_6 && fabs(v.y) < EPSILON_6 && fabs(v.z) < EPSILON_6)
1485  {
1486  return false;
1487  }
1488 
1489  // Idea: Find pa and pb such that (pa-pb)*u=(pa-pb)*v=0 (1) (because the shortest segment between two
1490  // lines is perpendicular to both of them)
1491  // With pa=p1+mua*u and pb=p3+mub*v (2) (pa is at mua times u along the line p1p2)
1492  //
1493  // Expanding (1) with (2) we get:
1494  // (w + mua*u - mub*v)*u=0 and (w + mua*u - mub*v)*v=0
1495  //
1496  // which can be further expanded into:
1497  // wu+mua*uu-mub*uv=0 and wv+mua*uv-mub*vv=0
1498  //
1499  // Solving for mua : mua = (uv*vw - vv*uw)/(uu*vv - uv*uv)
1500  //
1501  // Back-substituting gives mub = (vw + mua*uv)/vv = (uu*vw - uv*uw)/(uu*vv - uv*uv)
1502 
1503  // Compute the vector w formed by the first vertices of both segments
1504  w.x = p1.x - p3.x;
1505  w.y = p1.y - p3.y;
1506  w.z = p1.z - p3.z;
1507 
1508  // Pre-compute u*u, v*v, u*v, u*w and v*w
1509  vw = v * w;
1510  uv = u * v;
1511  uw = u * w;
1512  vv = v * v;
1513  uu = u * u;
1514 
1515  // Compute the denominator
1516  denom = uu * vv - uv * uv;
1517 
1518  // If the denominator is 0, then the two equations are dependant and therefore the two lines are parallel
1519  if (fabs(denom) < EPSILON_6)
1520  {
1521  return false;
1522  }
1523 
1524  // Compute the numerator of mua
1525  numer = uv * vw - vv * uw;
1526 
1527  // Compute mua and mub
1528  *mua = numer / denom;
1529  *mub = (vw + uv * (*mua)) / vv;
1530 
1531  pa->x = p1.x + *mua * u.x;
1532  pa->y = p1.y + *mua * u.y;
1533  pa->z = p1.z + *mua * u.z;
1534  pb->x = p3.x + *mub * v.x;
1535  pb->y = p3.y + *mub * v.y;
1536  pb->z = p3.z + *mub * v.z;
1537 
1538  return true;
1539 }
1540 
1541 inline bool pointInPolygone(const vec2& p, const std::vector<vec2>& points)
1542 {
1543  size_t i, j;
1544  bool c = false;
1545  for (i = 0, j = points.size() - 1; i < (int)points.size(); j = i++)
1546  {
1547  if ((((points.at(i).y <= p.y) && (p.y < points.at(j).y)) ||
1548  ((points.at(j).y <= p.y) && (p.y < points.at(i).y))) &&
1549  (p.x <
1550  (points.at(j).x - points.at(i).x) * (p.y - points.at(i).y) / (points.at(j).y - points.at(i).y) +
1551  points.at(i).x))
1552  {
1553  c = !c;
1554  }
1555  }
1556  return c;
1557 }
1558 
1559 // const Vector& P, decimal* pfSParam, decimal* pfTParam
1578  const vec3& vc, // Triangle
1579  const vec3& P, decimal* pfSParam, decimal* pfTParam)
1580 {
1581  //-------------------------------------------------------------------
1582  // 2 edges of the triangle
1583  //-------------------------------------------------------------------
1584  vec3 E0 = (vb - va);
1585  vec3 E1 = (vc - va);
1586 
1587  vec3 kDiff = (va - P);
1588  decimal fA00 = E0 * E0;
1589  decimal fA01 = E0 * E1;
1590  decimal fA11 = E1 * E1;
1591  decimal fB0 = kDiff * E0;
1592  decimal fB1 = kDiff * E1;
1593  decimal fC = kDiff * kDiff;
1594  decimal fDet = (decimal)fabs(fA00 * fA11 - fA01 * fA01);
1595  decimal fS = fA01 * fB1 - fA11 * fB0;
1596  decimal fT = fA01 * fB0 - fA00 * fB1;
1597  decimal fSqrDist;
1598 
1599  if (fabs(fDet) < 0.00000001f)
1600  {
1601  return 100000000.0f;
1602  }
1603 
1604  if (fS + fT <= fDet)
1605  {
1606  if (fS < (decimal)0.0)
1607  {
1608  if (fT < (decimal)0.0) // region 4
1609  {
1610  if (fB0 < (decimal)0.0)
1611  {
1612  fT = (decimal)0.0;
1613  if (-fB0 >= fA00)
1614  {
1615  fS = (decimal)1.0;
1616  fSqrDist = fA00 + ((decimal)2.0) * fB0 + fC;
1617  }
1618  else
1619  {
1620  fS = -fB0 / fA00;
1621  fSqrDist = fB0 * fS + fC;
1622  }
1623  }
1624  else
1625  {
1626  fS = (decimal)0.0;
1627  if (fB1 >= (decimal)0.0)
1628  {
1629  fT = (decimal)0.0;
1630  fSqrDist = fC;
1631  }
1632  else if (-fB1 >= fA11)
1633  {
1634  fT = (decimal)1.0;
1635  fSqrDist = fA11 + ((decimal)2.0) * fB1 + fC;
1636  }
1637  else
1638  {
1639  fT = -fB1 / fA11;
1640  fSqrDist = fB1 * fT + fC;
1641  }
1642  }
1643  }
1644  else // region 3
1645  {
1646  fS = (decimal)0.0;
1647  if (fB1 >= (decimal)0.0)
1648  {
1649  fT = (decimal)0.0;
1650  fSqrDist = fC;
1651  }
1652  else if (-fB1 >= fA11)
1653  {
1654  fT = (decimal)1.0;
1655  fSqrDist = fA11 + ((decimal)2.0) * fB1 + fC;
1656  }
1657  else
1658  {
1659  fT = -fB1 / fA11;
1660  fSqrDist = fB1 * fT + fC;
1661  }
1662  }
1663  }
1664  else if (fT < (decimal)0.0) // region 5
1665  {
1666  fT = (decimal)0.0;
1667  if (fB0 >= (decimal)0.0)
1668  {
1669  fS = (decimal)0.0;
1670  fSqrDist = fC;
1671  }
1672  else if (-fB0 >= fA00)
1673  {
1674  fS = (decimal)1.0;
1675  fSqrDist = fA00 + ((decimal)2.0) * fB0 + fC;
1676  }
1677  else
1678  {
1679  fS = -fB0 / fA00;
1680  fSqrDist = fB0 * fS + fC;
1681  }
1682  }
1683  else // region 0
1684  {
1685  // minimum at interior point
1686  decimal fInvDet = ((decimal)1.0) / fDet;
1687  fS *= fInvDet;
1688  fT *= fInvDet;
1689  fSqrDist = fS * (fA00 * fS + fA01 * fT + ((decimal)2.0) * fB0) +
1690  fT * (fA01 * fS + fA11 * fT + ((decimal)2.0) * fB1) + fC;
1691  }
1692  }
1693  else
1694  {
1695  decimal fTmp0, fTmp1, fNumer, fDenom;
1696 
1697  if (fS < (decimal)0.0) // region 2
1698  {
1699  fTmp0 = fA01 + fB0;
1700  fTmp1 = fA11 + fB1;
1701  if (fTmp1 > fTmp0)
1702  {
1703  fNumer = fTmp1 - fTmp0;
1704  fDenom = fA00 - 2.0f * fA01 + fA11;
1705  if (fNumer >= fDenom)
1706  {
1707  fS = (decimal)1.0;
1708  fT = (decimal)0.0;
1709  fSqrDist = fA00 + ((decimal)2.0) * fB0 + fC;
1710  }
1711  else
1712  {
1713  fS = fNumer / fDenom;
1714  fT = (decimal)1.0 - fS;
1715  fSqrDist = fS * (fA00 * fS + fA01 * fT + 2.0f * fB0) +
1716  fT * (fA01 * fS + fA11 * fT + ((decimal)2.0) * fB1) + fC;
1717  }
1718  }
1719  else
1720  {
1721  fS = (decimal)0.0;
1722  if (fTmp1 <= (decimal)0.0)
1723  {
1724  fT = (decimal)1.0;
1725  fSqrDist = fA11 + ((decimal)2.0) * fB1 + fC;
1726  }
1727  else if (fB1 >= (decimal)0.0)
1728  {
1729  fT = (decimal)0.0;
1730  fSqrDist = fC;
1731  }
1732  else
1733  {
1734  fT = -fB1 / fA11;
1735  fSqrDist = fB1 * fT + fC;
1736  }
1737  }
1738  }
1739  else if (fT < (decimal)0.0) // region 6
1740  {
1741  fTmp0 = fA01 + fB1;
1742  fTmp1 = fA00 + fB0;
1743  if (fTmp1 > fTmp0)
1744  {
1745  fNumer = fTmp1 - fTmp0;
1746  fDenom = fA00 - ((decimal)2.0) * fA01 + fA11;
1747  if (fNumer >= fDenom)
1748  {
1749  fT = (decimal)1.0;
1750  fS = (decimal)0.0;
1751  fSqrDist = fA11 + ((decimal)2.0) * fB1 + fC;
1752  }
1753  else
1754  {
1755  fT = fNumer / fDenom;
1756  fS = (decimal)1.0 - fT;
1757  fSqrDist = fS * (fA00 * fS + fA01 * fT + ((decimal)2.0) * fB0) +
1758  fT * (fA01 * fS + fA11 * fT + ((decimal)2.0) * fB1) + fC;
1759  }
1760  }
1761  else
1762  {
1763  fT = (decimal)0.0;
1764  if (fTmp1 <= (decimal)0.0)
1765  {
1766  fS = (decimal)1.0;
1767  fSqrDist = fA00 + ((decimal)2.0) * fB0 + fC;
1768  }
1769  else if (fB0 >= (decimal)0.0)
1770  {
1771  fS = (decimal)0.0;
1772  fSqrDist = fC;
1773  }
1774  else
1775  {
1776  fS = -fB0 / fA00;
1777  fSqrDist = fB0 * fS + fC;
1778  }
1779  }
1780  }
1781  else // region 1
1782  {
1783  fNumer = fA11 + fB1 - fA01 - fB0;
1784  if (fNumer <= (decimal)0.0)
1785  {
1786  fS = (decimal)0.0;
1787  fT = (decimal)1.0;
1788  fSqrDist = fA11 + ((decimal)2.0) * fB1 + fC;
1789  }
1790  else
1791  {
1792  fDenom = fA00 - 2.0f * fA01 + fA11;
1793  if (fNumer >= fDenom)
1794  {
1795  fS = (decimal)1.0;
1796  fT = (decimal)0.0;
1797  fSqrDist = fA00 + ((decimal)2.0) * fB0 + fC;
1798  }
1799  else
1800  {
1801  fS = fNumer / fDenom;
1802  fT = (decimal)1.0 - fS;
1803  fSqrDist = fS * (fA00 * fS + fA01 * fT + ((decimal)2.0) * fB0) +
1804  fT * (fA01 * fS + fA11 * fT + ((decimal)2.0) * fB1) + fC;
1805  }
1806  }
1807  }
1808  }
1809 
1810  if (pfSParam)
1811  {
1812  *pfSParam = fS;
1813  }
1814 
1815  if (pfTParam)
1816  {
1817  *pfTParam = fT;
1818  }
1819 
1820  return (decimal)fabs(fSqrDist);
1821 }
1822 
1823 /*
1824  * \brief binary operation
1825  */
1826 
1831 inline unsigned int buildBitSet(const unsigned short& length)
1832 {
1833  unsigned int res = 1;
1834  for (unsigned short i = 0; i < length - 1; i++, res++)
1835  {
1836  res = res << 1;
1837  }
1838  return res;
1839 }
1840 
1841 /*
1842  * \fn sequence buildComplementaryBitSet(const unsigned& int length, const unsigned int& bitSet )
1843  * \brief build the bit sequence representing complementary of bitset, taking account of is real useful length
1844  */
1845 inline unsigned int buildComplementaryBitSet(const unsigned int& length, const unsigned int& bitSet)
1846 {
1847  return (bitSet ^ buildBitSet(length));
1848 }
1849 
1850 }; // namespace core_mathlib
1851 using namespace core_mathlib;
1852 #endif // __HMATHLIB__
All base classes related to 3D manipulation.
#define EPSILON_6
Definition: 3d.h:39
NxReal c
Definition: NxVec3.cpp:317
double _y
y coordinate of OCoord3D
Definition: 3d.h:283
double _z
z coordinate of OCoord3D
Definition: 3d.h:284
double _x
x coordinate of OCoord3D
Definition: 3d.h:282
The 3D point class.
Definition: 3d.h:487
The 3D vector class.
Definition: 3d.h:298
3D vector Vector defined with 3 float numbers
Definition: mathlib.h:108
const base_vec3 operator*(base_t _f) const
Definition: mathlib.h:150
base_vec3(const base_t &_x, const base_t &_y, const base_t &_z)
Definition: mathlib.h:111
base_t length(void) const
Definition: mathlib.h:240
const base_vec3 operator/(base_t _f) const
Definition: mathlib.h:154
const base_vec3 operator+(const base_vec3 &_v) const
Definition: mathlib.h:167
base_vec3 & operator/=(base_t _f)
Definition: mathlib.h:184
base_vec3 operator^(const base_vec3 &_v) const
cross product
Definition: mathlib.h:201
void set(base_t _x, base_t _y, base_t _z)
Definition: mathlib.h:230
decimal distance(const base_vec3 &a_vector) const
Compute the distance between two points pointed by *this and a_vector.
Definition: mathlib.h:330
bool compare(const base_vec3 &_v, base_t epsi=EPSILON_6)
Definition: mathlib.h:280
base_t normalize(void)
Definition: mathlib.h:244
base_vec3 & operator=(base_t _f)
Definition: mathlib.h:143
base_vec3 lerp(base_vec3 &u, base_vec3 &v, base_t factor)
Linear interpolation function between 2 vectors.
Definition: mathlib.h:320
base_vec3(const base_vec3 *_v)
Definition: mathlib.h:115
base_vec3(const base_t *_v)
Definition: mathlib.h:112
bool barelyEqual(const base_vec3 &_v) const
Definition: mathlib.h:226
base_t operator*(const base_vec3 &_v) const
Definition: mathlib.h:197
void cross(const base_vec3 &v2)
Definition: mathlib.h:263
const base_vec3 operator-(const base_vec3 &_v) const
Definition: mathlib.h:175
void cross(const base_vec3 &v1, const base_vec3 &v2)
Definition: mathlib.h:257
base_vec3(const base_vec3 &_v)
Definition: mathlib.h:114
base_t projectionOnLine(const base_vec3 &vA, const base_vec3 &vB) const
Return the projection factor of *this on the line passing by vA and vB.
Definition: mathlib.h:314
base_t angle(const base_vec3 &v) const
Return the angle in radians between *this and v.
Definition: mathlib.h:285
bool operator==(const base_vec3 &_v)
Definition: mathlib.h:133
base_vec3 operator/(const base_vec3 &_v) const
Definition: mathlib.h:163
base_t dot(const base_vec3 &v) const
Scalar product.
Definition: mathlib.h:275
base_vec3 & operator+=(const base_vec3 &_v)
Definition: mathlib.h:188
base_vec3 & operator*=(base_t _f)
Definition: mathlib.h:180
base_vec3 Rotation(const base_vec3 &n, const base_t &angle) const
Vector rotation.
Definition: mathlib.h:349
base_t cosinus(const base_vec3 &ac)
Definition: mathlib.h:271
base_vec3 & operator-=(const base_vec3 &_v)
Definition: mathlib.h:192
base_t & operator[](int _i)
Definition: mathlib.h:217
const base_t & operator[](int _i) const
Definition: mathlib.h:221
base_vec3(const base_vec3 &_v, const base_vec3 &_w)
ab vector coordinates
Definition: mathlib.h:116
base_vec3 closestPointOnSegment(const base_vec3 &vA, const base_vec3 &vB) const
Return the coordinates of the nearest point of *this on the segment [vA,vB].
Definition: mathlib.h:300
bool operator!=(const base_vec3 &_v)
Definition: mathlib.h:138
const base_vec3 operator-() const
Definition: mathlib.h:171
base_vec3 closestPointOnLine(const base_vec3 &vA, const base_vec3 &vB) const
Return the coordinates of the nearest point of *this on the line passing by vA and vB.
Definition: mathlib.h:295
2D Vector Vector defined with 2 integers
Definition: mathlib.h:920
ivec2 & operator=(long _i)
Definition: mathlib.h:936
ivec2 & operator+=(const ivec2 &iv)
Definition: mathlib.h:971
const ivec2 operator/(long _i) const
Definition: mathlib.h:946
const ivec2 operator+(const ivec2 &iv) const
Definition: mathlib.h:950
ivec2 & operator*=(long _i)
Definition: mathlib.h:963
long operator*(const ivec2 &iv) const
Definition: mathlib.h:980
const ivec2 operator-() const
Definition: mathlib.h:954
const ivec2 operator*(long _i) const
Definition: mathlib.h:942
ivec2(long _a, long _b)
Definition: mathlib.h:923
void set(long _a, long _b)
Definition: mathlib.h:996
void reset(void)
Definition: mathlib.h:1001
ivec2 & operator/=(long _i)
Definition: mathlib.h:967
void swap(ivec2 *iv)
Definition: mathlib.h:1014
const ivec2 operator-(const ivec2 &iv) const
Definition: mathlib.h:958
void swap(ivec2 &iv)
Definition: mathlib.h:1005
int operator!=(const ivec2 &iv)
Definition: mathlib.h:931
int operator==(const ivec2 &iv)
Definition: mathlib.h:927
ivec2(const ivec2 &iv)
Definition: mathlib.h:925
ivec2(const long *iv)
Definition: mathlib.h:924
ivec2 & operator-=(const ivec2 &iv)
Definition: mathlib.h:975
3D Vector Vector defined with 3 integers
Definition: mathlib.h:1044
void swap(ivec3 *iv)
Definition: mathlib.h:1158
ivec3 & operator-=(const ivec3 &iv)
Definition: mathlib.h:1108
ivec3 & operator=(vec3 &iv)
Definition: mathlib.h:1068
ivec3 & operator+=(const ivec3 &iv)
Definition: mathlib.h:1104
const ivec3 operator/(long _i) const
Definition: mathlib.h:1079
const ivec3 operator-() const
Definition: mathlib.h:1087
ivec3 & operator=(long _i)
Definition: mathlib.h:1061
ivec3(long _a, long _b, long _c)
Definition: mathlib.h:1047
void swap(ivec3 &iv)
Definition: mathlib.h:1146
const ivec3 operator+(const ivec3 &iv) const
Definition: mathlib.h:1083
const ivec3 operator*(long _i) const
Definition: mathlib.h:1075
ivec3(const long *iv)
Definition: mathlib.h:1048
long operator*(const ivec3 &iv) const
Definition: mathlib.h:1113
ivec3 & operator/=(long _i)
Definition: mathlib.h:1100
const ivec3 operator-(const ivec3 &iv) const
Definition: mathlib.h:1091
ivec3(const ivec3 &iv)
Definition: mathlib.h:1049
void set(int tab[3])
Definition: mathlib.h:1136
ivec3 & operator*=(long _i)
Definition: mathlib.h:1096
void reset(void)
Definition: mathlib.h:1142
void set(long _a, long _b, long _c)
Definition: mathlib.h:1130
int operator==(const ivec3 &iv)
Definition: mathlib.h:1052
int operator!=(const ivec3 &iv)
Definition: mathlib.h:1056
4D Vector Vector defined with 4 integers
Definition: mathlib.h:1192
long operator*(const ivec4 &iv) const
Definition: mathlib.h:1260
int operator!=(const ivec4 &iv)
Definition: mathlib.h:1205
void reset(void)
Definition: mathlib.h:1283
int operator==(const ivec4 &iv)
Definition: mathlib.h:1201
const ivec4 operator*(long _i) const
Definition: mathlib.h:1218
ivec4(const long *iv)
Definition: mathlib.h:1196
void set(long _a, long _b, long _c, long _d)
Definition: mathlib.h:1276
ivec4(long _a, long _b, long _c, long _d)
Definition: mathlib.h:1195
ivec4 & operator=(long _i)
Definition: mathlib.h:1210
void swap(ivec4 *iv)
Definition: mathlib.h:1302
long operator*(const ivec3 &iv) const
Definition: mathlib.h:1256
void swap(ivec4 &iv)
Definition: mathlib.h:1287
const ivec4 operator-() const
Definition: mathlib.h:1230
const ivec4 operator/(long _i) const
Definition: mathlib.h:1222
const ivec4 operator-(const ivec4 &iv) const
Definition: mathlib.h:1234
ivec4 & operator/=(long _i)
Definition: mathlib.h:1243
ivec4 & operator+=(const ivec4 &iv)
Definition: mathlib.h:1247
ivec4(const ivec4 &iv)
Definition: mathlib.h:1199
ivec4 & operator-=(const ivec4 &iv)
Definition: mathlib.h:1251
ivec4(const ivec3 &iv, long _d)
Definition: mathlib.h:1198
const ivec4 operator+(const ivec4 &iv) const
Definition: mathlib.h:1226
ivec4(const ivec3 &iv)
Definition: mathlib.h:1197
ivec4 & operator*=(long _i)
Definition: mathlib.h:1239
2D Vector Vector defined with 2 float numbers
Definition: mathlib.h:473
vec2(decimal _x, decimal _y)
Definition: mathlib.h:476
int operator==(const vec2 &_v)
Definition: mathlib.h:483
void set(decimal _x, decimal _y)
Definition: mathlib.h:569
vec2 & operator*=(decimal _f)
Definition: mathlib.h:530
vec2 & operator=(decimal _f)
Definition: mathlib.h:493
decimal normalize(void)
Definition: mathlib.h:582
const vec2 operator-(const vec2 &_v) const
Definition: mathlib.h:520
vec2 & operator-=(const vec2 &_v)
Definition: mathlib.h:542
int operator!=(const vec2 &_v)
Definition: mathlib.h:488
const vec2 operator/(decimal _f) const
Definition: mathlib.h:503
decimal length(void) const
Definition: mathlib.h:578
decimal angle(void)
Definition: mathlib.h:636
vec2 & operator/=(decimal _f)
Definition: mathlib.h:534
void reset(void)
Definition: mathlib.h:574
decimal det(vec2 &v)
2D determinant
Definition: mathlib.h:594
const vec2 operator+(const vec2 &_v) const
Definition: mathlib.h:512
vec2 lerp(vec2 &u, vec2 &v, decimal factor)
Linear interpolation function between 2 vectors.
Definition: mathlib.h:632
decimal dot(const vec2 &v)
Scalar product.
Definition: mathlib.h:598
vec2(const decimal *_v)
Definition: mathlib.h:477
decimal & operator[](int _i)
Definition: mathlib.h:560
decimal v[2]
Definition: mathlib.h:655
vec2(const vec2 &_v, const vec2 &_w)
Definition: mathlib.h:479
bool compare(const vec2 &_v, decimal epsi=EPSILON_6)
Definition: mathlib.h:602
vec2(const vec2 &_v)
Definition: mathlib.h:478
decimal operator^(const vec2 &_v) const
Definition: mathlib.h:525
vec2 closestPointOnSegment(const vec2 &vA, const vec2 &vB)
Return the coordinates of the nearest point of *this on the segment vA,vB.
Definition: mathlib.h:612
const vec2 operator*(decimal _f) const
Definition: mathlib.h:499
decimal operator*(const vec2 &_v) const
Definition: mathlib.h:547
decimal angle(const vec2 &v)
Definition: mathlib.h:640
vec2 closestPointOnLine(const vec2 &vA, const vec2 &vB)
Return the coordinates of the nearest point of *this on the line passing by vA and vB.
Definition: mathlib.h:607
decimal projectionOnLine(const vec2 &vA, const vec2 &vB)
Return the projection factor of *this on the line passing by vA and vB.
Definition: mathlib.h:626
const vec2 operator-() const
Definition: mathlib.h:516
const decimal & operator[](int _i) const
Definition: mathlib.h:564
vec2 & operator+=(const vec2 &_v)
Definition: mathlib.h:538
4D Vector Vector defined with 4 float numbers
Definition: mathlib.h:723
const vec4 operator*(decimal _f) const
Definition: mathlib.h:750
decimal v[4]
Definition: mathlib.h:848
vec4(const decimal *_v)
Definition: mathlib.h:727
vec4 & operator*=(decimal _f)
Definition: mathlib.h:776
void reset(void)
Definition: mathlib.h:820
bool compare(const vec4 &_v, decimal epsi=EPSILON_6)
Definition: mathlib.h:824
const vec4 operator/(decimal _f) const
Definition: mathlib.h:754
vec4(const vec3 &_v)
Definition: mathlib.h:728
vec4 & operator/=(decimal _f)
Definition: mathlib.h:780
vec4(decimal _x, decimal _y, decimal _z, decimal _w)
Definition: mathlib.h:726
decimal operator*(const vec4 &_v) const
Definition: mathlib.h:797
const vec4 operator+(const vec4 &_v) const
Definition: mathlib.h:763
vec4 & operator+=(const vec4 &_v)
Definition: mathlib.h:784
const vec4 operator-(const vec4 &_v) const
Definition: mathlib.h:771
decimal operator*(const vec3 &_v) const
Definition: mathlib.h:793
const vec4 operator-() const
Definition: mathlib.h:767
vec4(const vec3 &_v, decimal _w)
Definition: mathlib.h:729
void set(decimal _x, decimal _y, decimal _z, decimal _w)
Definition: mathlib.h:813
vec4 & operator=(decimal _f)
Definition: mathlib.h:742
vec4(const vec4 &_v)
Definition: mathlib.h:730
int operator==(const vec4 &_v)
Definition: mathlib.h:732
vec4 & operator-=(const vec4 &_v)
Definition: mathlib.h:788
int operator!=(const vec4 &_v)
Definition: mathlib.h:737
Math functions.
Definition: mathlib.h:41
unsigned int buildComplementaryBitSet(const unsigned int &length, const unsigned int &bitSet)
Definition: mathlib.h:1845
bool LineLineIntersect(const vec3 &p1, const vec3 &p2, const vec3 &p3, const vec3 &p4, vec3 *pa, vec3 *pb, decimal *mua, decimal *mub)
Calculate the segment between the lines (p1,p2) and (p3,p4)
Definition: mathlib.h:1460
float decimal
Definition: mathlib.h:45
void Cross(const vec3 &v1, const vec3 &v2, vec3 &vout)
Definition: mathlib.h:664
unsigned int buildBitSet(const unsigned short &length)
Definition: mathlib.h:1831
vec3 GetGTriangle(const vec3 &A, const vec3 &B, const vec3 &C)
Definition: mathlib.h:1347
int Round2Int(float val)
Definition: mathlib.h:1404
decimal GetAireTriangle(const vec3 &a, const vec3 &b, const vec3 &c)
Definition: mathlib.h:1357
base_vec3< double > dvec3
Definition: mathlib.h:382
int Floor2Int(float val)
Definition: mathlib.h:1399
bool colinear(const vec3 &A, const vec3 &B, const vec3 &C, const decimal &aproximation)
Definition: mathlib.h:902
vec3 GetGTetra(const vec3 &A, const vec3 &B, const vec3 &C, const vec3 &D)
Definition: mathlib.h:1353
std::vector< vec3 > operator+(const std::vector< vec3 > &_u, const std::vector< vec3 > &_v)
Definition: mathlib.h:394
decimal ClosestDistanceBetweenDotAndTriangle(const vec3 &va, const vec3 &vb, const vec3 &vc, const vec3 &P, decimal *pfSParam, decimal *pfTParam)
Definition: mathlib.h:1577
float Clamp(float val, float low, float high)
Definition: mathlib.h:1367
void Vector(const vec3 &vp1, const vec3 &vp2, vec3 &vout)
Definition: mathlib.h:685
vec3 OPoint3Dtovec3(const OPoint3D &_p)
Converts a OPoint3D to vec3.
Definition: mathlib.h:440
OPoint3D vec3toOPoint3D(const vec3 &_v)
Converts a vec3 to OPoint3D.
Definition: mathlib.h:431
decimal Determinant(const vec3 &vp1, const vec3 &vp2, const vec3 &vp3)
Definition: mathlib.h:699
std::vector< vec3 > operator*(const std::vector< vec3 > &_v, const decimal &_a)
Definition: mathlib.h:384
decimal CalcTetraVolume(vec3 A, vec3 B, vec3 C, vec3 D)
Definition: mathlib.h:1337
vec3 Vector_r(const vec3 &vp1, const vec3 &vp2)
Definition: mathlib.h:692
vec3 FaceNormal(const vec3 &vp1, const vec3 &vp2, const vec3 &vp3)
Definition: mathlib.h:711
vec3 Cross_r(const vec3 &v1, const vec3 &v2)
Definition: mathlib.h:670
OVector3D vec3toOVector3D(const vec3 &_v)
Converts a vec3 to OVector3D.
Definition: mathlib.h:449
bool pointInPolygone(const vec2 &p, const std::vector< vec2 > &points)
Definition: mathlib.h:1541
base_vec3< decimal > vec3
Definition: mathlib.h:381
decimal area(const vec2 &A, const vec2 &B, const vec2 &C)
Definition: mathlib.h:659
unsigned int bitSet
Definition: mathlib.h:47
vec3 OVector3Dtovec3(const OVector3D &_v)
Converts a OVector3D to vec3.
Definition: mathlib.h:458