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