-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOgreMath.cpp
958 lines (848 loc) · 31.8 KB
/
OgreMath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2014 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
//#include "OgreStableHeaders.h"
#include "OgreMath.h"
#include "OgreVector2.h"
#include "OgreVector3.h"
#include "OgreVector4.h"
#include "OgreRay.h"
#include "OgreSphere.h"
#include "OgreAxisAlignedBox.h"
#include "OgrePlane.h"
const Real Math::POS_INFINITY = std::numeric_limits<Real>::infinity();
const Real Math::NEG_INFINITY = -std::numeric_limits<Real>::infinity();
const Real Math::PI = Real( 4.0 * atan( 1.0 ) );
const Real Math::TWO_PI = Real( 2.0 * PI );
const Real Math::HALF_PI = Real( 0.5 * PI );
const Real Math::fDeg2Rad = PI / Real(180.0);
const Real Math::fRad2Deg = Real(180.0) / PI;
const Real Math::LOG2 = log(Real(2.0));
int Math::mTrigTableSize;
Math::AngleUnit Math::msAngleUnit;
Real Math::mTrigTableFactor;
Real *Math::mSinTable = NULL;
Real *Math::mTanTable = NULL;
Math::RandomValueProvider* Math::mRandProvider = NULL;
//-----------------------------------------------------------------------
Math::Math( unsigned int trigTableSize )
{
msAngleUnit = AU_DEGREE;
// mTrigTableSize = trigTableSize;
// mTrigTableFactor = mTrigTableSize / Math::TWO_PI;
// mSinTable = OGRE_ALLOC_T(Real, mTrigTableSize, MEMCATEGORY_GENERAL);
// mTanTable = OGRE_ALLOC_T(Real, mTrigTableSize, MEMCATEGORY_GENERAL);
// buildTrigTables();
}
//-----------------------------------------------------------------------
Math::~Math()
{
// OGRE_FREE(mSinTable, MEMCATEGORY_GENERAL);
// OGRE_FREE(mTanTable, MEMCATEGORY_GENERAL);
}
/*
//-----------------------------------------------------------------------
void Math::buildTrigTables(void)
{
// Build trig lookup tables
// Could get away with building only PI sized Sin table but simpler this
// way. Who cares, it'll ony use an extra 8k of memory anyway and I like
// simplicity.
Real angle;
for (int i = 0; i < mTrigTableSize; ++i)
{
// angle = Math::TWO_PI * i / mTrigTableSize;
// mSinTable[i] = sin(angle);
// mTanTable[i] = tan(angle);
}
}
//-----------------------------------------------------------------------
Real Math::SinTable (Real fValue)
{
// Convert range to index values, wrap if required
int idx;
if (fValue >= 0)
{
idx = int(fValue * mTrigTableFactor) % mTrigTableSize;
}
else
{
idx = mTrigTableSize - (int(-fValue * mTrigTableFactor) % mTrigTableSize) - 1;
}
return mSinTable[idx];
}
//-----------------------------------------------------------------------
Real Math::TanTable (Real fValue)
{
// Convert range to index values, wrap if required
int idx = int(fValue *= mTrigTableFactor) % mTrigTableSize;
return mTanTable[idx];
}*/
//-----------------------------------------------------------------------
Radian Math::ACos (Real fValue)
{
if ( -1.0 < fValue )
{
if ( fValue < 1.0 )
return Radian(std::acos(fValue));
else
return Radian(0.0);
}
else
{
return Radian(PI);
}
}
//-----------------------------------------------------------------------
Radian Math::ASin (Real fValue)
{
if ( -1.0 < fValue )
{
if ( fValue < 1.0 )
return Radian(std::asin(fValue));
else
return Radian(HALF_PI);
}
else
{
return Radian(-HALF_PI);
}
}
//-----------------------------------------------------------------------
Real Math::Sign (Real fValue)
{
if ( fValue > 0.0 )
return 1.0;
if ( fValue < 0.0 )
return -1.0;
return 0.0;
}
//-----------------------------------------------------------------------
Real Math::UnitRandom ()
{
if (mRandProvider)
return mRandProvider->getRandomUnit();
else return Real(rand()) / RAND_MAX;
}
//-----------------------------------------------------------------------
void Math::SetRandomValueProvider(RandomValueProvider* provider)
{
mRandProvider = provider;
}
//-----------------------------------------------------------------------
void Math::setAngleUnit(Math::AngleUnit unit)
{
msAngleUnit = unit;
}
//-----------------------------------------------------------------------
Math::AngleUnit Math::getAngleUnit(void)
{
return msAngleUnit;
}
//-----------------------------------------------------------------------
Real Math::AngleUnitsToRadians(Real angleunits)
{
if (msAngleUnit == AU_DEGREE)
return angleunits * fDeg2Rad;
else
return angleunits;
}
//-----------------------------------------------------------------------
Real Math::RadiansToAngleUnits(Real radians)
{
if (msAngleUnit == AU_DEGREE)
return radians * fRad2Deg;
else
return radians;
}
//-----------------------------------------------------------------------
Real Math::AngleUnitsToDegrees(Real angleunits)
{
if (msAngleUnit == AU_RADIAN)
return angleunits * fRad2Deg;
else
return angleunits;
}
//-----------------------------------------------------------------------
Real Math::DegreesToAngleUnits(Real degrees)
{
if (msAngleUnit == AU_RADIAN)
return degrees * fDeg2Rad;
else
return degrees;
}
//-----------------------------------------------------------------------
bool Math::pointInTri2D(const Vector2& p, const Vector2& a,
const Vector2& b, const Vector2& c)
{
// Winding must be consistent from all edges for point to be inside
Vector2 v1, v2;
Real dot[3];
bool zeroDot[3];
v1 = b - a;
v2 = p - a;
// Note we don't care about normalisation here since sign is all we need
// It means we don't have to worry about magnitude of cross products either
dot[0] = v1.crossProduct(v2);
zeroDot[0] = Math::RealEqual(dot[0], 0.0f, 1e-3);
v1 = c - b;
v2 = p - b;
dot[1] = v1.crossProduct(v2);
zeroDot[1] = Math::RealEqual(dot[1], 0.0f, 1e-3);
// Compare signs (ignore colinear / coincident points)
if(!zeroDot[0] && !zeroDot[1]
&& Math::Sign(dot[0]) != Math::Sign(dot[1]))
{
return false;
}
v1 = a - c;
v2 = p - c;
dot[2] = v1.crossProduct(v2);
zeroDot[2] = Math::RealEqual(dot[2], 0.0f, 1e-3);
// Compare signs (ignore colinear / coincident points)
if((!zeroDot[0] && !zeroDot[2]
&& Math::Sign(dot[0]) != Math::Sign(dot[2])) ||
(!zeroDot[1] && !zeroDot[2]
&& Math::Sign(dot[1]) != Math::Sign(dot[2])))
{
return false;
}
return true;
}
//-----------------------------------------------------------------------
bool Math::pointInTri3D(const Vector3& p, const Vector3& a,
const Vector3& b, const Vector3& c, const Vector3& normal)
{
// Winding must be consistent from all edges for point to be inside
Vector3 v1, v2;
Real dot[3];
bool zeroDot[3];
v1 = b - a;
v2 = p - a;
// Note we don't care about normalisation here since sign is all we need
// It means we don't have to worry about magnitude of cross products either
dot[0] = v1.crossProduct(v2).dotProduct(normal);
zeroDot[0] = Math::RealEqual(dot[0], 0.0f, 1e-3);
v1 = c - b;
v2 = p - b;
dot[1] = v1.crossProduct(v2).dotProduct(normal);
zeroDot[1] = Math::RealEqual(dot[1], 0.0f, 1e-3);
// Compare signs (ignore colinear / coincident points)
if(!zeroDot[0] && !zeroDot[1]
&& Math::Sign(dot[0]) != Math::Sign(dot[1]))
{
return false;
}
v1 = a - c;
v2 = p - c;
dot[2] = v1.crossProduct(v2).dotProduct(normal);
zeroDot[2] = Math::RealEqual(dot[2], 0.0f, 1e-3);
// Compare signs (ignore colinear / coincident points)
if((!zeroDot[0] && !zeroDot[2]
&& Math::Sign(dot[0]) != Math::Sign(dot[2])) ||
(!zeroDot[1] && !zeroDot[2]
&& Math::Sign(dot[1]) != Math::Sign(dot[2])))
{
return false;
}
return true;
}
//-----------------------------------------------------------------------
std::pair<bool, Real> Math::intersects(const Ray& ray, const Plane& plane)
{
Real denom = plane.normal.dotProduct(ray.getDirection());
if (Math::Abs(denom) < std::numeric_limits<Real>::epsilon())
{
// Parallel
return std::pair<bool, Real>(false, (Real)0);
}
else
{
Real nom = plane.normal.dotProduct(ray.getOrigin()) + plane.d;
Real t = -(nom/denom);
return std::pair<bool, Real>(t >= 0, (Real)t);
}
}
//-----------------------------------------------------------------------
std::pair<bool, Real> Math::intersects(const Ray& ray,
const std::vector<Plane>& planes, bool normalIsOutside)
{
std::list<Plane> planesList;
for (std::vector<Plane>::const_iterator i = planes.begin(); i != planes.end(); ++i)
{
planesList.push_back(*i);
}
return intersects(ray, planesList, normalIsOutside);
}
//-----------------------------------------------------------------------
std::pair<bool, Real> Math::intersects(const Ray& ray,
const std::list<Plane>& planes, bool normalIsOutside)
{
std::list<Plane>::const_iterator planeit, planeitend;
planeitend = planes.end();
bool allInside = true;
std::pair<bool, Real> ret;
std::pair<bool, Real> end;
ret.first = false;
ret.second = 0.0f;
end.first = false;
end.second = 0;
// derive side
// NB we don't pass directly since that would require Plane::Side in
// interface, which results in recursive includes since Math is so fundamental
Plane::Side outside = normalIsOutside ? Plane::POSITIVE_SIDE : Plane::NEGATIVE_SIDE;
for (planeit = planes.begin(); planeit != planeitend; ++planeit)
{
const Plane& plane = *planeit;
// is origin outside?
if (plane.getSide(ray.getOrigin()) == outside)
{
allInside = false;
// Test single plane
std::pair<bool, Real> planeRes =
ray.intersects(plane);
if (planeRes.first)
{
// Ok, we intersected
ret.first = true;
// Use the most distant result since convex volume
ret.second = max(ret.second, planeRes.second);
}
else
{
ret.first =false;
ret.second=0.0f;
return ret;
}
}
else
{
std::pair<bool, Real> planeRes =
ray.intersects(plane);
if (planeRes.first)
{
if( !end.first )
{
end.first = true;
end.second = planeRes.second;
}
else
{
end.second = min( planeRes.second, end.second );
}
}
}
}
if (allInside)
{
// Intersecting at 0 distance since inside the volume!
ret.first = true;
ret.second = 0.0f;
return ret;
}
if( end.first )
{
if( end.second < ret.second )
{
ret.first = false;
return ret;
}
}
return ret;
}
//-----------------------------------------------------------------------
std::pair<bool, Real> Math::intersects(const Ray& ray, const Sphere& sphere,
bool discardInside)
{
const Vector3& raydir = ray.getDirection();
// Adjust ray origin relative to sphere center
const Vector3& rayorig = ray.getOrigin() - sphere.getCenter();
Real radius = sphere.getRadius();
// Check origin inside first
if (rayorig.squaredLength() <= radius*radius && discardInside)
{
return std::pair<bool, Real>(true, (Real)0);
}
// Mmm, quadratics
// Build coeffs which can be used with std quadratic solver
// ie t = (-b +/- sqrt(b*b + 4ac)) / 2a
Real a = raydir.dotProduct(raydir);
Real b = 2 * rayorig.dotProduct(raydir);
Real c = rayorig.dotProduct(rayorig) - radius*radius;
// Calc determinant
Real d = (b*b) - (4 * a * c);
if (d < 0)
{
// No intersection
return std::pair<bool, Real>(false, (Real)0);
}
else
{
// BTW, if d=0 there is one intersection, if d > 0 there are 2
// But we only want the closest one, so that's ok, just use the
// '-' version of the solver
Real t = ( -b - Math::Sqrt(d) ) / (2 * a);
if (t < 0)
t = ( -b + Math::Sqrt(d) ) / (2 * a);
return std::pair<bool, Real>(true, (Real)t);
}
}
//-----------------------------------------------------------------------
std::pair<bool, Real> Math::intersects(const Ray& ray, const AxisAlignedBox& box)
{
if (box.isNull()) return std::pair<bool, Real>(false, (Real)0);
if (box.isInfinite()) return std::pair<bool, Real>(true, (Real)0);
Real lowt = 0.0f;
Real t;
bool hit = false;
Vector3 hitpoint;
const Vector3& min = box.getMinimum();
const Vector3& max = box.getMaximum();
const Vector3& rayorig = ray.getOrigin();
const Vector3& raydir = ray.getDirection();
// Check origin inside first
if ( rayorig > min && rayorig < max )
{
return std::pair<bool, Real>(true, (Real)0);
}
// Check each face in turn, only check closest 3
// Min x
if (rayorig.x <= min.x && raydir.x > 0)
{
t = (min.x - rayorig.x) / raydir.x;
// Substitute t back into ray and check bounds and dist
hitpoint = rayorig + raydir * t;
if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
hitpoint.z >= min.z && hitpoint.z <= max.z &&
(!hit || t < lowt))
{
hit = true;
lowt = t;
}
}
// Max x
if (rayorig.x >= max.x && raydir.x < 0)
{
t = (max.x - rayorig.x) / raydir.x;
// Substitute t back into ray and check bounds and dist
hitpoint = rayorig + raydir * t;
if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
hitpoint.z >= min.z && hitpoint.z <= max.z &&
(!hit || t < lowt))
{
hit = true;
lowt = t;
}
}
// Min y
if (rayorig.y <= min.y && raydir.y > 0)
{
t = (min.y - rayorig.y) / raydir.y;
// Substitute t back into ray and check bounds and dist
hitpoint = rayorig + raydir * t;
if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
hitpoint.z >= min.z && hitpoint.z <= max.z &&
(!hit || t < lowt))
{
hit = true;
lowt = t;
}
}
// Max y
if (rayorig.y >= max.y && raydir.y < 0)
{
t = (max.y - rayorig.y) / raydir.y;
// Substitute t back into ray and check bounds and dist
hitpoint = rayorig + raydir * t;
if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
hitpoint.z >= min.z && hitpoint.z <= max.z &&
(!hit || t < lowt))
{
hit = true;
lowt = t;
}
}
// Min z
if (rayorig.z <= min.z && raydir.z > 0)
{
t = (min.z - rayorig.z) / raydir.z;
// Substitute t back into ray and check bounds and dist
hitpoint = rayorig + raydir * t;
if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
hitpoint.y >= min.y && hitpoint.y <= max.y &&
(!hit || t < lowt))
{
hit = true;
lowt = t;
}
}
// Max z
if (rayorig.z >= max.z && raydir.z < 0)
{
t = (max.z - rayorig.z) / raydir.z;
// Substitute t back into ray and check bounds and dist
hitpoint = rayorig + raydir * t;
if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
hitpoint.y >= min.y && hitpoint.y <= max.y &&
(!hit || t < lowt))
{
hit = true;
lowt = t;
}
}
return std::pair<bool, Real>(hit, (Real)lowt);
}
//-----------------------------------------------------------------------
bool Math::intersects(const Ray& ray, const AxisAlignedBox& box,
Real* d1, Real* d2)
{
if (box.isNull())
return false;
if (box.isInfinite())
{
if (d1) *d1 = 0;
if (d2) *d2 = Math::POS_INFINITY;
return true;
}
const Vector3& min = box.getMinimum();
const Vector3& max = box.getMaximum();
const Vector3& rayorig = ray.getOrigin();
const Vector3& raydir = ray.getDirection();
Vector3 absDir;
absDir[0] = Math::Abs(raydir[0]);
absDir[1] = Math::Abs(raydir[1]);
absDir[2] = Math::Abs(raydir[2]);
// Sort the axis, ensure check minimise floating error axis first
int imax = 0, imid = 1, imin = 2;
if (absDir[0] < absDir[2])
{
imax = 2;
imin = 0;
}
if (absDir[1] < absDir[imin])
{
imid = imin;
imin = 1;
}
else if (absDir[1] > absDir[imax])
{
imid = imax;
imax = 1;
}
Real start = 0, end = Math::POS_INFINITY;
#define _CALC_AXIS(i) \
do { \
Real denom = 1 / raydir[i]; \
Real newstart = (min[i] - rayorig[i]) * denom; \
Real newend = (max[i] - rayorig[i]) * denom; \
if (newstart > newend) std::swap(newstart, newend); \
if (newstart > end || newend < start) return false; \
if (newstart > start) start = newstart; \
if (newend < end) end = newend; \
} while(0)
// Check each axis in turn
_CALC_AXIS(imax);
if (absDir[imid] < std::numeric_limits<Real>::epsilon())
{
// Parallel with middle and minimise axis, check bounds only
if (rayorig[imid] < min[imid] || rayorig[imid] > max[imid] ||
rayorig[imin] < min[imin] || rayorig[imin] > max[imin])
return false;
}
else
{
_CALC_AXIS(imid);
if (absDir[imin] < std::numeric_limits<Real>::epsilon())
{
// Parallel with minimise axis, check bounds only
if (rayorig[imin] < min[imin] || rayorig[imin] > max[imin])
return false;
}
else
{
_CALC_AXIS(imin);
}
}
#undef _CALC_AXIS
if (d1) *d1 = start;
if (d2) *d2 = end;
return true;
}
//-----------------------------------------------------------------------
std::pair<bool, Real> Math::intersects(const Ray& ray, const Vector3& a,
const Vector3& b, const Vector3& c, const Vector3& normal,
bool positiveSide, bool negativeSide)
{
//
// Calculate intersection with plane.
//
Real t;
{
Real denom = normal.dotProduct(ray.getDirection());
// Check intersect side
if (denom > + std::numeric_limits<Real>::epsilon())
{
if (!negativeSide)
return std::pair<bool, Real>(false, (Real)0);
}
else if (denom < - std::numeric_limits<Real>::epsilon())
{
if (!positiveSide)
return std::pair<bool, Real>(false, (Real)0);
}
else
{
// Parallel or triangle area is close to zero when
// the plane normal not normalised.
return std::pair<bool, Real>(false, (Real)0);
}
t = normal.dotProduct(a - ray.getOrigin()) / denom;
if (t < 0)
{
// Intersection is behind origin
return std::pair<bool, Real>(false, (Real)0);
}
}
//
// Calculate the largest area projection plane in X, Y or Z.
//
size_t i0, i1;
{
Real n0 = Math::Abs(normal[0]);
Real n1 = Math::Abs(normal[1]);
Real n2 = Math::Abs(normal[2]);
i0 = 1; i1 = 2;
if (n1 > n2)
{
if (n1 > n0) i0 = 0;
}
else
{
if (n2 > n0) i1 = 0;
}
}
//
// Check the intersection point is inside the triangle.
//
{
Real u1 = b[i0] - a[i0];
Real v1 = b[i1] - a[i1];
Real u2 = c[i0] - a[i0];
Real v2 = c[i1] - a[i1];
Real u0 = t * ray.getDirection()[i0] + ray.getOrigin()[i0] - a[i0];
Real v0 = t * ray.getDirection()[i1] + ray.getOrigin()[i1] - a[i1];
Real alpha = u0 * v2 - u2 * v0;
Real beta = u1 * v0 - u0 * v1;
Real area = u1 * v2 - u2 * v1;
// epsilon to avoid float precision error
const Real EPSILON = 1e-6f;
Real tolerance = - EPSILON * area;
if (area > 0)
{
if (alpha < tolerance || beta < tolerance || alpha+beta > area-tolerance)
return std::pair<bool, Real>(false, (Real)0);
}
else
{
if (alpha > tolerance || beta > tolerance || alpha+beta < area-tolerance)
return std::pair<bool, Real>(false, (Real)0);
}
}
return std::pair<bool, Real>(true, (Real)t);
}
//-----------------------------------------------------------------------
std::pair<bool, Real> Math::intersects(const Ray& ray, const Vector3& a,
const Vector3& b, const Vector3& c,
bool positiveSide, bool negativeSide)
{
Vector3 normal = calculateBasicFaceNormalWithoutNormalize(a, b, c);
return intersects(ray, a, b, c, normal, positiveSide, negativeSide);
}
//-----------------------------------------------------------------------
bool Math::intersects(const Sphere& sphere, const AxisAlignedBox& box)
{
if (box.isNull()) return false;
if (box.isInfinite()) return true;
// Use splitting planes
const Vector3& center = sphere.getCenter();
Real radius = sphere.getRadius();
const Vector3& min = box.getMinimum();
const Vector3& max = box.getMaximum();
// Arvo's algorithm
Real s, d = 0;
for (int i = 0; i < 3; ++i)
{
if (center.ptr()[i] < min.ptr()[i])
{
s = center.ptr()[i] - min.ptr()[i];
d += s * s;
}
else if(center.ptr()[i] > max.ptr()[i])
{
s = center.ptr()[i] - max.ptr()[i];
d += s * s;
}
}
return d <= radius * radius;
}
//-----------------------------------------------------------------------
bool Math::intersects(const Plane& plane, const AxisAlignedBox& box)
{
return (plane.getSide(box) == Plane::BOTH_SIDE);
}
//-----------------------------------------------------------------------
bool Math::intersects(const Sphere& sphere, const Plane& plane)
{
return (
Math::Abs(plane.getDistance(sphere.getCenter()))
<= sphere.getRadius() );
}
//-----------------------------------------------------------------------
Vector3 Math::calculateTangentSpaceVector(
const Vector3& position1, const Vector3& position2, const Vector3& position3,
Real u1, Real v1, Real u2, Real v2, Real u3, Real v3)
{
//side0 is the vector along one side of the triangle of vertices passed in,
//and side1 is the vector along another side. Taking the cross product of these returns the normal.
Vector3 side0 = position1 - position2;
Vector3 side1 = position3 - position1;
//Calculate face normal
Vector3 normal = side1.crossProduct(side0);
normal.normalise();
//Now we use a formula to calculate the tangent.
Real deltaV0 = v1 - v2;
Real deltaV1 = v3 - v1;
Vector3 tangent = deltaV1 * side0 - deltaV0 * side1;
tangent.normalise();
//Calculate binormal
Real deltaU0 = u1 - u2;
Real deltaU1 = u3 - u1;
Vector3 binormal = deltaU1 * side0 - deltaU0 * side1;
binormal.normalise();
//Now, we take the cross product of the tangents to get a vector which
//should point in the same direction as our normal calculated above.
//If it points in the opposite direction (the dot product between the normals is less than zero),
//then we need to reverse the s and t tangents.
//This is because the triangle has been mirrored when going from tangent space to object space.
//reverse tangents if necessary
Vector3 tangentCross = tangent.crossProduct(binormal);
if (tangentCross.dotProduct(normal) < 0.0f)
{
tangent = -tangent;
binormal = -binormal;
}
return tangent;
}
//-----------------------------------------------------------------------
Matrix4 Math::buildReflectionMatrix(const Plane& p)
{
return Matrix4(
-2 * p.normal.x * p.normal.x + 1, -2 * p.normal.x * p.normal.y, -2 * p.normal.x * p.normal.z, -2 * p.normal.x * p.d,
-2 * p.normal.y * p.normal.x, -2 * p.normal.y * p.normal.y + 1, -2 * p.normal.y * p.normal.z, -2 * p.normal.y * p.d,
-2 * p.normal.z * p.normal.x, -2 * p.normal.z * p.normal.y, -2 * p.normal.z * p.normal.z + 1, -2 * p.normal.z * p.d,
0, 0, 0, 1);
}
//-----------------------------------------------------------------------
Vector4 Math::calculateFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3)
{
Vector3 normal = calculateBasicFaceNormal(v1, v2, v3);
// Now set up the w (distance of tri from origin
return Vector4(normal.x, normal.y, normal.z, -(normal.dotProduct(v1)));
}
//-----------------------------------------------------------------------
Vector3 Math::calculateBasicFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3)
{
Vector3 normal = (v2 - v1).crossProduct(v3 - v1);
normal.normalise();
return normal;
}
//-----------------------------------------------------------------------
Vector4 Math::calculateFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3)
{
Vector3 normal = calculateBasicFaceNormalWithoutNormalize(v1, v2, v3);
// Now set up the w (distance of tri from origin)
return Vector4(normal.x, normal.y, normal.z, -(normal.dotProduct(v1)));
}
//-----------------------------------------------------------------------
Vector3 Math::calculateBasicFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3)
{
Vector3 normal = (v2 - v1).crossProduct(v3 - v1);
return normal;
}
//-----------------------------------------------------------------------
Real Math::gaussianDistribution(Real x, Real offset, Real scale)
{
Real nom = Math::Exp(
-Math::Sqr(x - offset) / (2 * Math::Sqr(scale)));
Real denom = scale * Math::Sqrt(2 * Math::PI);
return nom / denom;
}
//---------------------------------------------------------------------
Matrix4 Math::makeViewMatrix(const Vector3& position, const Quaternion& orientation,
const Matrix4* reflectMatrix)
{
Matrix4 viewMatrix;
// View matrix is:
//
// [ Lx Uy Dz Tx ]
// [ Lx Uy Dz Ty ]
// [ Lx Uy Dz Tz ]
// [ 0 0 0 1 ]
//
// Where T = -(Transposed(Rot) * Pos)
// This is most efficiently done using 3x3 Matrices
Matrix3 rot;
orientation.ToRotationMatrix(rot);
// Make the translation relative to new axes
Matrix3 rotT = rot.Transpose();
Vector3 trans = -rotT * position;
// Make final matrix
viewMatrix = Matrix4::IDENTITY;
viewMatrix = rotT; // fills upper 3x3
viewMatrix[0][3] = trans.x;
viewMatrix[1][3] = trans.y;
viewMatrix[2][3] = trans.z;
// Deal with reflections
if (reflectMatrix)
{
viewMatrix = viewMatrix * (*reflectMatrix);
}
return viewMatrix;
}
//---------------------------------------------------------------------
Real Math::boundingRadiusFromAABB(const AxisAlignedBox& aabb)
{
const Vector3& max = aabb.getMaximum();
const Vector3& min = aabb.getMinimum();
Vector3 magnitude = max;
magnitude.makeCeil(-max);
magnitude.makeCeil(min);
magnitude.makeCeil(-min);
return magnitude.length();
}
Real Math::boundingRadiusFromAABBCentered(const AxisAlignedBox& aabb)
{
const Vector3& max = aabb.getMaximum();
const Vector3& min = aabb.getMinimum();
return ((min - max) * 0.5f).length();
}