*Backed out changeset 3be01fb200a5
--HG-- branch : trunk
This commit is contained in:
@@ -1,37 +1,49 @@
|
||||
/**
|
||||
@file Vector3.h
|
||||
|
||||
3D vector class
|
||||
|
||||
@maintainer Morgan McGuire, [email protected]
|
||||
|
||||
@created 2001-06-02
|
||||
@edited 2005-08-23
|
||||
Copyright 2000-2006, Morgan McGuire.
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
#ifndef G3D_VECTOR3_H
|
||||
#define G3D_VECTOR3_H
|
||||
|
||||
#include "G3D/platform.h"
|
||||
#include "G3D/g3dmath.h"
|
||||
#include "G3D/Vector2.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace G3D {
|
||||
|
||||
class Vector2;
|
||||
class Vector3;
|
||||
class Vector4;
|
||||
|
||||
/**
|
||||
<B>Swizzles</B>
|
||||
Vector classes have swizzle operators, e.g. <CODE>v.xy()</CODE>, that
|
||||
allow selection of arbitrary sub-fields. These cannot be used as write
|
||||
masks. Examples
|
||||
|
||||
<PRE>
|
||||
Vector3 v(1, 2, 3);
|
||||
Vector3 j;
|
||||
Vector2 b;
|
||||
|
||||
b = v.xz();
|
||||
j = b.xx();
|
||||
</PRE>
|
||||
|
||||
|
||||
<B>Warning</B>
|
||||
|
||||
Do not subclass-- this implementation makes assumptions about the
|
||||
memory layout.
|
||||
*/
|
||||
@@ -42,20 +54,25 @@ private:
|
||||
Note that if used for a collision or ray reflection you
|
||||
must negate the resulting vector to get a direction pointing
|
||||
<I>away</I> from the collision.
|
||||
|
||||
<PRE>
|
||||
V' N V
|
||||
|
||||
r ^ -,
|
||||
\ | /
|
||||
\|/
|
||||
</PRE>
|
||||
|
||||
See also Vector3::reflectionDirection
|
||||
*/
|
||||
Vector3 reflectAbout(const Vector3& normal) const;
|
||||
|
||||
// Hidden operators
|
||||
bool operator<(const Vector3&) const;
|
||||
bool operator>(const Vector3&) const;
|
||||
bool operator<=(const Vector3&) const;
|
||||
bool operator>=(const Vector3&) const;
|
||||
|
||||
public:
|
||||
// construction
|
||||
Vector3();
|
||||
@@ -65,8 +82,10 @@ public:
|
||||
Vector3(double coordinate[3]);
|
||||
Vector3(const Vector3& rkVector);
|
||||
Vector3(const class Vector3int16& v);
|
||||
|
||||
// coordinates
|
||||
float x, y, z;
|
||||
|
||||
// access vector V as V[0] = V.x, V[1] = V.y, V[2] = V.z
|
||||
//
|
||||
// WARNING. These member functions rely on
|
||||
@@ -74,18 +93,23 @@ public:
|
||||
// (2) the data packed in a 3*sizeof(float) memory block
|
||||
const float& operator[] (int i) const;
|
||||
float& operator[] (int i);
|
||||
|
||||
inline operator float* () {
|
||||
return (float*)this;
|
||||
}
|
||||
|
||||
operator const float* () const {
|
||||
return (float*)this;
|
||||
}
|
||||
|
||||
enum Axis {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, DETECT_AXIS=-1};
|
||||
|
||||
/**
|
||||
Returns the largest dimension. Particularly convenient for determining
|
||||
which plane to project a triangle onto for point-in-polygon tests.
|
||||
*/
|
||||
Axis primaryAxis() const;
|
||||
|
||||
// assignment and comparison
|
||||
Vector3& operator= (const Vector3& rkVector);
|
||||
bool operator== (const Vector3& rkVector) const;
|
||||
@@ -93,12 +117,16 @@ public:
|
||||
unsigned int hashCode() const;
|
||||
bool fuzzyEq(const Vector3& other) const;
|
||||
bool fuzzyNe(const Vector3& other) const;
|
||||
|
||||
/** Returns true if this vector has finite length. */
|
||||
bool isFinite() const;
|
||||
|
||||
/** Returns true if this vector has length ~= 0 */
|
||||
bool isZero() const;
|
||||
|
||||
/** Returns true if this vector has length ~= 1 */
|
||||
bool isUnit() const;
|
||||
|
||||
// arithmetic operations
|
||||
Vector3 operator+ (const Vector3& v) const;
|
||||
Vector3 operator- (const Vector3& v) const;
|
||||
@@ -107,6 +135,7 @@ public:
|
||||
Vector3 operator* (const Vector3& v) const;
|
||||
Vector3 operator/ (const Vector3& v) const;
|
||||
Vector3 operator- () const;
|
||||
|
||||
// arithmetic updates
|
||||
Vector3& operator+= (const Vector3& v);
|
||||
Vector3& operator-= (const Vector3& v);
|
||||
@@ -114,24 +143,30 @@ public:
|
||||
Vector3& operator/= (float s);
|
||||
Vector3& operator*= (const Vector3& v);
|
||||
Vector3& operator/= (const Vector3& v);
|
||||
|
||||
/** @deprecated Use magnitude */
|
||||
float G3D_DEPRECATED length() const;
|
||||
|
||||
float magnitude() const;
|
||||
|
||||
/**
|
||||
The result is a nan vector if the length is almost zero.
|
||||
*/
|
||||
Vector3 direction() const;
|
||||
|
||||
/**
|
||||
Potentially less accurate but faster than direction().
|
||||
Only works if System::hasSSE is true.
|
||||
*/
|
||||
Vector3 fastDirection() const;
|
||||
|
||||
|
||||
/**
|
||||
See also G3D::Ray::reflect.
|
||||
The length is 1.
|
||||
<PRE>
|
||||
V' N V
|
||||
|
||||
r ^ /
|
||||
\ | /
|
||||
\|'-
|
||||
@@ -139,6 +174,7 @@ public:
|
||||
*/
|
||||
Vector3 reflectionDirection(const Vector3& normal) const;
|
||||
|
||||
|
||||
/**
|
||||
Returns Vector3::zero() if the length is nearly zero, otherwise
|
||||
returns a unit vector.
|
||||
@@ -153,6 +189,7 @@ public:
|
||||
return *this * (1.0f / mag);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the direction of a refracted ray,
|
||||
where iExit is the index of refraction for the
|
||||
@@ -160,14 +197,19 @@ public:
|
||||
for the new material. Like Vector3::reflectionDirection,
|
||||
the result has length 1 and is
|
||||
pointed <I>away</I> from the intersection.
|
||||
|
||||
Returns Vector3::zero() in the case of total internal refraction.
|
||||
|
||||
@param iOutside The index of refraction (eta) outside
|
||||
(on the <I>positive</I> normal side) of the surface.
|
||||
|
||||
@param iInside The index of refraction (eta) inside
|
||||
(on the <I>negative</I> normal side) of the surface.
|
||||
|
||||
See also G3D::Ray::refract.
|
||||
<PRE>
|
||||
N V
|
||||
|
||||
^ /
|
||||
| /
|
||||
|'-
|
||||
@@ -179,28 +221,37 @@ public:
|
||||
const Vector3& normal,
|
||||
float iInside,
|
||||
float iOutside) const;
|
||||
|
||||
/** Synonym for direction */
|
||||
inline Vector3 unit() const {
|
||||
return direction();
|
||||
}
|
||||
|
||||
/** Returns a normalized vector. May be computed with lower precision than unit */
|
||||
inline Vector3 fastUnit() const {
|
||||
return fastDirection();
|
||||
}
|
||||
|
||||
/** @deprecated Use squaredMagnitude */
|
||||
float G3D_DEPRECATED squaredLength() const;
|
||||
|
||||
float squaredMagnitude () const;
|
||||
|
||||
/** @deprecated Use squaredMagnitude */
|
||||
inline float G3D_DEPRECATED norm() const {
|
||||
return squaredMagnitude();
|
||||
}
|
||||
|
||||
float dot(const Vector3& rkVector) const;
|
||||
|
||||
float G3D_DEPRECATED unitize(float fTolerance = 1e-06);
|
||||
|
||||
/** Cross product. Note that two cross products in a row
|
||||
can be computed more cheaply: v1 x (v2 x v3) = (v1 dot v3) v2 - (v1 dot v2) v3.
|
||||
*/
|
||||
Vector3 cross(const Vector3& rkVector) const;
|
||||
Vector3 unitCross (const Vector3& rkVector) const;
|
||||
|
||||
/**
|
||||
Returns a matrix such that v.cross() * w = v.cross(w).
|
||||
<PRE>
|
||||
@@ -210,53 +261,67 @@ public:
|
||||
</PRE>
|
||||
*/
|
||||
class Matrix3 cross() const;
|
||||
|
||||
Vector3 min(const Vector3 &v) const;
|
||||
Vector3 max(const Vector3 &v) const;
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
inline Vector3 clamp(const Vector3& low, const Vector3& high) const {
|
||||
return Vector3(
|
||||
G3D::clamp(x, low.x, high.x),
|
||||
G3D::clamp(y, low.y, high.y),
|
||||
G3D::clamp(z, low.z, high.z));
|
||||
}
|
||||
|
||||
inline Vector3 clamp(float low, float high) const {
|
||||
return Vector3(
|
||||
G3D::clamp(x, low, high),
|
||||
G3D::clamp(y, low, high),
|
||||
G3D::clamp(z, low, high));
|
||||
}
|
||||
|
||||
/**
|
||||
Linear interpolation
|
||||
*/
|
||||
inline Vector3 lerp(const Vector3& v, float alpha) const {
|
||||
return (*this) + (v - *this) * alpha;
|
||||
}
|
||||
|
||||
/** Gram-Schmidt orthonormalization. */
|
||||
static void orthonormalize (Vector3 akVector[3]);
|
||||
|
||||
/** Random unit vector, uniformly distributed */
|
||||
static Vector3 random();
|
||||
|
||||
/** Random unit vector, distributed
|
||||
so that the probability of V is proportional
|
||||
to max(V dot Normal, 0).
|
||||
|
||||
@cite Henrik Wann Jensen, Realistic Image Synthesis using Photon Mapping eqn 2.24
|
||||
*/
|
||||
static Vector3 cosRandom(const Vector3& normal);
|
||||
|
||||
|
||||
/**
|
||||
Random vector distributed over the hemisphere about normal.
|
||||
*/
|
||||
static Vector3 hemiRandom(const Vector3& normal);
|
||||
|
||||
// Input W must be initialize to a nonzero vector, output is {U,V,W}
|
||||
// an orthonormal basis. A hint is provided about whether or not W
|
||||
// is already unit length.
|
||||
static void generateOrthonormalBasis (Vector3& rkU, Vector3& rkV,
|
||||
Vector3& rkW, bool bUnitLengthW = true);
|
||||
|
||||
inline float sum() const {
|
||||
return x + y + z;
|
||||
}
|
||||
|
||||
inline float average() const {
|
||||
return sum() / 3.0f;
|
||||
}
|
||||
|
||||
// Special values.
|
||||
inline static const Vector3& zero() { static Vector3 v(0, 0, 0); return v; }
|
||||
inline static const Vector3& one() { static Vector3 v(1, 1, 1); return v; }
|
||||
@@ -269,6 +334,7 @@ public:
|
||||
inline static const Vector3& minFinite(){ static Vector3 v(-FLT_MAX, -FLT_MAX, -FLT_MAX); return v; }
|
||||
/** Largest representable vector */
|
||||
inline static const Vector3& maxFinite(){ static Vector3 v(FLT_MAX, FLT_MAX, FLT_MAX); return v; }
|
||||
|
||||
// Deprecated. See Matrix3::identity() for details.
|
||||
/** @deprecated Use Vector3::zero() */
|
||||
static const Vector3 ZERO;
|
||||
@@ -284,7 +350,9 @@ public:
|
||||
static const Vector3 INF3;
|
||||
/** @deprecated Use Vector3::nan() */
|
||||
static const Vector3 NAN3;
|
||||
|
||||
// 2-char swizzles
|
||||
|
||||
Vector2 xx() const;
|
||||
Vector2 yx() const;
|
||||
Vector2 zx() const;
|
||||
@@ -294,7 +362,9 @@ public:
|
||||
Vector2 xz() const;
|
||||
Vector2 yz() const;
|
||||
Vector2 zz() const;
|
||||
|
||||
// 3-char swizzles
|
||||
|
||||
Vector3 xxx() const;
|
||||
Vector3 yxx() const;
|
||||
Vector3 zxx() const;
|
||||
@@ -322,7 +392,9 @@ public:
|
||||
Vector3 xzz() const;
|
||||
Vector3 yzz() const;
|
||||
Vector3 zzz() const;
|
||||
|
||||
// 4-char swizzles
|
||||
|
||||
Vector4 xxxx() const;
|
||||
Vector4 yxxx() const;
|
||||
Vector4 zxxx() const;
|
||||
@@ -404,21 +476,30 @@ public:
|
||||
Vector4 xzzz() const;
|
||||
Vector4 yzzz() const;
|
||||
Vector4 zzzz() const;
|
||||
|
||||
/** A value that can be passed to ignore a parameter. Never look at the result of dummy. */
|
||||
static Vector3 dummy;
|
||||
};
|
||||
|
||||
inline G3D::Vector3 operator*(float s, const G3D::Vector3& v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
inline G3D::Vector3 operator*(double s, const G3D::Vector3& v) {
|
||||
return v * (float)s;
|
||||
}
|
||||
|
||||
inline G3D::Vector3 operator*(int s, const G3D::Vector3& v) {
|
||||
return v * (float)s;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Vector3&);
|
||||
|
||||
}
|
||||
|
||||
unsigned int hashCode(const G3D::Vector3& v);
|
||||
|
||||
#include "Vector3.inl"
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user