2 * ICE / OPCODE - Optimized Collision Detection
3 * http://www.codercorner.com/Opcode.htm
5 * Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
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:
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 * Contains code for 3D vectors.
21 * \author Pierre Terdiman
22 * \date April, 4, 2000
24 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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).
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:
37 * Point P0,P1 = some 3D points;
38 * Point Delta = P1 - P0;
41 * This compiles fine, although you should have written:
44 * Point P0,P1 = some 3D points;
45 * Vector3 Delta = P1 - P0;
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.
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.
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.
58 * \author Pierre Terdiman
61 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67 using namespace Opcode;
69 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
71 * Creates a positive unit random vector.
72 * \return Self-reference
74 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75 Point& Point::PositiveUnitRandomVector()
77 x = UnitRandomFloat();
78 y = UnitRandomFloat();
79 z = UnitRandomFloat();
84 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
86 * Creates a unit random vector.
87 * \return Self-reference
89 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90 Point& Point::UnitRandomVector()
92 x = UnitRandomFloat() - 0.5f;
93 y = UnitRandomFloat() - 0.5f;
94 z = UnitRandomFloat() - 0.5f;
100 // WARNING: not inlined
101 Point::operator HPoint() const { return HPoint(x, y, z, 0.0f); }
103 Point& Point::Refract(const Point& eye, const Point& n, float refractindex, Point& refracted)
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
117 NDotE /= refractindex;
120 refracted = n*NDotE - Env*NDotN;
125 Point& Point::ProjectToPlane(const Plane& p)
127 *this-= (p.d + (*this|p.n))*p.n;
131 void Point::ProjectToScreen(float halfrenderwidth, float halfrenderheight, const Matrix4x4& mat, HPoint& projected) const
133 projected = HPoint(x, y, z, 1.0f) * mat;
134 projected.w = 1.0f / projected.w;
136 projected.x*=projected.w;
137 projected.y*=projected.w;
138 projected.z*=projected.w;
140 projected.x *= halfrenderwidth; projected.x += halfrenderwidth;
141 projected.y *= -halfrenderheight; projected.y += halfrenderheight;
144 void Point::SetNotUsed()
146 // We use a particular integer pattern : 0xffffffff everywhere. This is a NAN.
152 BOOL Point::IsNotUsed() const
154 if(IR(x)!=0xffffffff) return FALSE;
155 if(IR(y)!=0xffffffff) return FALSE;
156 if(IR(z)!=0xffffffff) return FALSE;
160 Point& Point::Mult(const Matrix3x3& mat, const Point& a)
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];
168 Point& Point::Mult2(const Matrix3x3& mat1, const Point& a1, const Matrix3x3& mat2, const Point& a2)
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];
176 Point& Point::Mac(const Matrix3x3& mat, const Point& a)
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];
184 Point& Point::TransMult(const Matrix3x3& mat, const Point& a)
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];
192 Point& Point::Transform(const Point& r, const Matrix3x3& rotpos, const Point& linpos)
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;
200 Point& Point::InvTransform(const Point& r, const Matrix3x3& rotpos, const Point& linpos)
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];