Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / Ice / IcePoint.cpp
1 /*
2  *      ICE / OPCODE - Optimized Collision Detection
3  * http://www.codercorner.com/Opcode.htm
4  * 
5  * Copyright (c) 2001-2008 Pierre Terdiman,  pierre@codercorner.com
6
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose, 
10 including commercial applications, and to alter it and redistribute it freely, 
11 subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
16 */
17 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18 /**
19  *      Contains code for 3D vectors.
20  *      \file           IcePoint.cpp
21  *      \author         Pierre Terdiman
22  *      \date           April, 4, 2000
23  */
24 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25
26 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 /**
28  *      3D point.
29  *
30  *      The name is "Point" instead of "Vector" since a vector is N-dimensional, whereas a point is an implicit "vector of dimension 3".
31  *      So the choice was between "Point" and "Vector3", the first one looked better (IMHO).
32  *
33  *      Some people, then, use a typedef to handle both points & vectors using the same class: typedef Point Vector3;
34  *      This is bad since it opens the door to a lot of confusion while reading the code. I know it may sounds weird but check this out:
35  *
36  *      \code
37  *              Point P0,P1 = some 3D points;
38  *              Point Delta = P1 - P0;
39  *      \endcode
40  *
41  *      This compiles fine, although you should have written:
42  *
43  *      \code
44  *              Point P0,P1 = some 3D points;
45  *              Vector3 Delta = P1 - P0;
46  *      \endcode
47  *
48  *      Subtle things like this are not caught at compile-time, and when you find one in the code, you never know whether it's a mistake
49  *      from the author or something you don't get.
50  *
51  *      One way to handle it at compile-time would be to use different classes for Point & Vector3, only overloading operator "-" for vectors.
52  *      But then, you get a lot of redundant code in thoses classes, and basically it's really a lot of useless work.
53  *
54  *      Another way would be to use homogeneous points: w=1 for points, w=0 for vectors. That's why the HPoint class exists. Now, to store
55  *      your model's vertices and in most cases, you really want to use Points to save ram.
56  *
57  *      \class          Point
58  *      \author         Pierre Terdiman
59  *      \version        1.0
60  */
61 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62
63 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64 // Precompiled Header
65 #include "Stdafx.h"
66
67 using namespace Opcode;
68
69 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70 /**
71  *      Creates a positive unit random vector.
72  *      \return         Self-reference
73  */
74 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75 Point& Point::PositiveUnitRandomVector()
76 {
77         x = UnitRandomFloat();
78         y = UnitRandomFloat();
79         z = UnitRandomFloat();
80         Normalize();
81         return *this;
82 }
83
84 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85 /**
86  *      Creates a unit random vector.
87  *      \return         Self-reference
88  */
89 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90 Point& Point::UnitRandomVector()
91 {
92         x = UnitRandomFloat() - 0.5f;
93         y = UnitRandomFloat() - 0.5f;
94         z = UnitRandomFloat() - 0.5f;
95         Normalize();
96         return *this;
97 }
98
99 // Cast operator
100 // WARNING: not inlined
101 Point::operator HPoint() const  { return HPoint(x, y, z, 0.0f); }
102
103 Point& Point::Refract(const Point& eye, const Point& n, float refractindex, Point& refracted)
104 {
105         //      Point EyePt = eye position
106         //      Point p = current vertex
107         //      Point n = vertex normal
108         //      Point rv = refracted vector
109         //      Eye vector - doesn't need to be normalized
110         Point Env;
111         Env.x = eye.x - x;
112         Env.y = eye.y - y;
113         Env.z = eye.z - z;
114
115         float NDotE = n|Env;
116         float NDotN = n|n;
117         NDotE /= refractindex;
118
119         // Refracted vector
120         refracted = n*NDotE - Env*NDotN;
121
122         return *this;
123 }
124
125 Point& Point::ProjectToPlane(const Plane& p)
126 {
127         *this-= (p.d + (*this|p.n))*p.n;
128         return *this;
129 }
130
131 void Point::ProjectToScreen(float halfrenderwidth, float halfrenderheight, const Matrix4x4& mat, HPoint& projected) const
132 {
133         projected = HPoint(x, y, z, 1.0f) * mat;
134         projected.w = 1.0f / projected.w;
135
136         projected.x*=projected.w;
137         projected.y*=projected.w;
138         projected.z*=projected.w;
139
140         projected.x *= halfrenderwidth;         projected.x += halfrenderwidth;
141         projected.y *= -halfrenderheight;       projected.y += halfrenderheight;
142 }
143
144 void Point::SetNotUsed()
145 {
146         // We use a particular integer pattern : 0xffffffff everywhere. This is a NAN.
147         IR(x) = 0xffffffff;
148         IR(y) = 0xffffffff;
149         IR(z) = 0xffffffff;
150 }
151
152 BOOL Point::IsNotUsed() const
153 {
154         if(IR(x)!=0xffffffff)   return FALSE;
155         if(IR(y)!=0xffffffff)   return FALSE;
156         if(IR(z)!=0xffffffff)   return FALSE;
157         return TRUE;
158 }
159
160 Point& Point::Mult(const Matrix3x3& mat, const Point& a)
161 {
162         x = a.x * mat.m[0][0] + a.y * mat.m[0][1] + a.z * mat.m[0][2];
163         y = a.x * mat.m[1][0] + a.y * mat.m[1][1] + a.z * mat.m[1][2];
164         z = a.x * mat.m[2][0] + a.y * mat.m[2][1] + a.z * mat.m[2][2];
165         return *this;
166 }
167
168 Point& Point::Mult2(const Matrix3x3& mat1, const Point& a1, const Matrix3x3& mat2, const Point& a2)
169 {
170         x = a1.x * mat1.m[0][0] + a1.y * mat1.m[0][1] + a1.z * mat1.m[0][2] + a2.x * mat2.m[0][0] + a2.y * mat2.m[0][1] + a2.z * mat2.m[0][2];
171         y = a1.x * mat1.m[1][0] + a1.y * mat1.m[1][1] + a1.z * mat1.m[1][2] + a2.x * mat2.m[1][0] + a2.y * mat2.m[1][1] + a2.z * mat2.m[1][2];
172         z = a1.x * mat1.m[2][0] + a1.y * mat1.m[2][1] + a1.z * mat1.m[2][2] + a2.x * mat2.m[2][0] + a2.y * mat2.m[2][1] + a2.z * mat2.m[2][2];
173         return *this;
174 }
175
176 Point& Point::Mac(const Matrix3x3& mat, const Point& a)
177 {
178         x += a.x * mat.m[0][0] + a.y * mat.m[0][1] + a.z * mat.m[0][2];
179         y += a.x * mat.m[1][0] + a.y * mat.m[1][1] + a.z * mat.m[1][2];
180         z += a.x * mat.m[2][0] + a.y * mat.m[2][1] + a.z * mat.m[2][2];
181         return *this;
182 }
183
184 Point& Point::TransMult(const Matrix3x3& mat, const Point& a)
185 {
186         x = a.x * mat.m[0][0] + a.y * mat.m[1][0] + a.z * mat.m[2][0];
187         y = a.x * mat.m[0][1] + a.y * mat.m[1][1] + a.z * mat.m[2][1];
188         z = a.x * mat.m[0][2] + a.y * mat.m[1][2] + a.z * mat.m[2][2];
189         return *this;
190 }
191
192 Point& Point::Transform(const Point& r, const Matrix3x3& rotpos, const Point& linpos)
193 {
194         x = r.x * rotpos.m[0][0] + r.y * rotpos.m[0][1] + r.z * rotpos.m[0][2] + linpos.x;
195         y = r.x * rotpos.m[1][0] + r.y * rotpos.m[1][1] + r.z * rotpos.m[1][2] + linpos.y;
196         z = r.x * rotpos.m[2][0] + r.y * rotpos.m[2][1] + r.z * rotpos.m[2][2] + linpos.z;
197         return *this;
198 }
199
200 Point& Point::InvTransform(const Point& r, const Matrix3x3& rotpos, const Point& linpos)
201 {
202         float sx = r.x - linpos.x;
203         float sy = r.y - linpos.y;
204         float sz = r.z - linpos.z;
205         x = sx * rotpos.m[0][0] + sy * rotpos.m[1][0] + sz * rotpos.m[2][0];
206         y = sx * rotpos.m[0][1] + sy * rotpos.m[1][1] + sz * rotpos.m[2][1];
207         z = sx * rotpos.m[0][2] + sy * rotpos.m[1][2] + sz * rotpos.m[2][2];
208         return *this;
209 }