1 | #ifndef _BBCUTIL_H |
---|
2 | #define _BBCUTIL_H |
---|
3 |
|
---|
4 | #include <BBCPrerequisites.h>
|
---|
5 |
|
---|
6 | namespace BBC {
|
---|
7 |
|
---|
8 | const Ogre::Real EPSILON_INTERSECT = 0.00000001;
|
---|
9 |
|
---|
10 | class _BBCExport Util
|
---|
11 | {
|
---|
12 | public:
|
---|
13 | static Ogre::String getBaseName(Ogre::String fileName)
|
---|
14 | {
|
---|
15 | Ogre::String baseName;
|
---|
16 | size_t pos = fileName.find_last_of(".");
|
---|
17 | baseName = fileName.substr(0, pos);
|
---|
18 | return baseName;
|
---|
19 | }
|
---|
20 |
|
---|
21 | static Ogre::String getExtensionName(Ogre::String fileName)
|
---|
22 | {
|
---|
23 | Ogre::String ext;
|
---|
24 | size_t pos = fileName.find_last_of(".");
|
---|
25 | ext = fileName.substr(pos+1);
|
---|
26 | return ext;
|
---|
27 | }
|
---|
28 |
|
---|
29 | static unsigned int nextPowerOf2(unsigned int nPlanes) |
---|
30 | { |
---|
31 | unsigned int depth = 1;
|
---|
32 | while (depth < nPlanes)
|
---|
33 | {
|
---|
34 | depth = depth << 1;
|
---|
35 | }
|
---|
36 | return depth; |
---|
37 | }
|
---|
38 |
|
---|
39 | // Calculate the line segment PaPb that is the shortest route between
|
---|
40 | // two lines P1P2 and P3P4. Calculate also the values of mua and mub where
|
---|
41 | // Pa = P1 + mua (P2 - P1)
|
---|
42 | // Pb = P3 + mub (P4 - P3)
|
---|
43 | // Return false if no solution exists.
|
---|
44 |
|
---|
45 | static int lineLineIntersect(
|
---|
46 | Ogre::Vector3 p1, Ogre::Vector3 p2, Ogre::Vector3 p3, Ogre::Vector3 p4, Ogre::Vector3 *pa, Ogre::Vector3 *pb,
|
---|
47 | Ogre::Real *mua, Ogre::Real *mub)
|
---|
48 | {
|
---|
49 | Ogre::Vector3 p13,p43,p21;
|
---|
50 | Ogre::Real d1343,d4321,d1321,d4343,d2121;
|
---|
51 | Ogre::Real numer,denom;
|
---|
52 |
|
---|
53 | p13.x = p1.x - p3.x;
|
---|
54 | p13.y = p1.y - p3.y;
|
---|
55 | p13.z = p1.z - p3.z;
|
---|
56 | p43.x = p4.x - p3.x;
|
---|
57 | p43.y = p4.y - p3.y;
|
---|
58 | p43.z = p4.z - p3.z;
|
---|
59 | if (Ogre::Math::Abs(p43.x) < EPSILON_INTERSECT && Ogre::Math::Abs(p43.y) < EPSILON_INTERSECT && Ogre::Math::Abs(p43.z) < EPSILON_INTERSECT)
|
---|
60 | {
|
---|
61 | return(false);
|
---|
62 | }
|
---|
63 | p21.x = p2.x - p1.x;
|
---|
64 | p21.y = p2.y - p1.y;
|
---|
65 | p21.z = p2.z - p1.z;
|
---|
66 | if (Ogre::Math::Abs(p21.x) < EPSILON_INTERSECT && Ogre::Math::Abs(p21.y) < EPSILON_INTERSECT && Ogre::Math::Abs(p21.z) < EPSILON_INTERSECT)
|
---|
67 | {
|
---|
68 | return(false);
|
---|
69 | }
|
---|
70 |
|
---|
71 | d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
|
---|
72 | d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
|
---|
73 | d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
|
---|
74 | d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
|
---|
75 | d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
|
---|
76 |
|
---|
77 | denom = d2121 * d4343 - d4321 * d4321;
|
---|
78 | if (Ogre::Math::Abs(denom) < EPSILON_INTERSECT)
|
---|
79 | {
|
---|
80 | return(false);
|
---|
81 | }
|
---|
82 | numer = d1343 * d4321 - d1321 * d4343;
|
---|
83 |
|
---|
84 | *mua = numer / denom;
|
---|
85 | *mub = (d1343 + d4321 * (*mua)) / d4343;
|
---|
86 |
|
---|
87 | pa->x = p1.x + *mua * p21.x;
|
---|
88 | pa->y = p1.y + *mua * p21.y;
|
---|
89 | pa->z = p1.z + *mua * p21.z;
|
---|
90 | pb->x = p3.x + *mub * p43.x;
|
---|
91 | pb->y = p3.y + *mub * p43.y;
|
---|
92 | pb->z = p3.z + *mub * p43.z;
|
---|
93 |
|
---|
94 | return(true);
|
---|
95 | } |
---|
96 | }; |
---|
97 |
|
---|
98 | /*
|
---|
99 | extern "C" _BBCExport Ogre::Real fround(Ogre::Real n, unsigned d)
|
---|
100 | {
|
---|
101 | return floor(n * pow((Ogre::Real)10.,(int) d) + .5) / pow((Ogre::Real)10.,(int) d);
|
---|
102 | }
|
---|
103 |
|
---|
104 | extern "C" _BBCExport int iround(Ogre::Real x)
|
---|
105 | {
|
---|
106 | return (int)floor(x + 0.5);
|
---|
107 | } |
---|
108 | */ |
---|
109 | } |
---|
110 | |
---|
111 | #endif |
---|