- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# FMath/FMVector3.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2005-2007 Feeling Software Inc.
    00003     Portions of the code are:
    00004     Copyright (C) 2005-2007 Sony Computer Entertainment America
    00005
    00006     MIT License: http://www.opensource.org/licenses/mit-license.php
    00007 */
    00008
    00013 #ifndef _FM_VECTOR3_H_
    00014 #define _FM_VECTOR3_H_
    00015
    00016 class FMVector4;
    00017
    00026 class FCOLLADA_EXPORT
    00027 ALIGN_STRUCT(16)
    00028 FMVector3
    00029 {
    00030 public:
    00031     float x;
    00032     float y;
    00033     float z;
    00034 private:
    00035     float w;    // For alignment purposes.
    00036
    00037 public:
    00041     #ifndef _DEBUG
    00042     inline FMVector3() {}
    00043     #else
    00044     inline FMVector3() { x = 123456789.0f; y = 123456789.0f; z = 123456789.0f; }
    00045     #endif
    00046
    00051     inline FMVector3(float _x, float _y, float _z) { x = _x; y = _y; z = _z; }
    00052
    00055     inline FMVector3(const FMVector3& rhs) { x = rhs.x; y = rhs.y; z = rhs.z; }
    00056
    00060     FMVector3(const FMVector4& vect4);
    00061
    00068     FMVector3(const float* source, uint32 startIndex = 0);
    00069     FMVector3(const double* source, uint32 startIndex = 0);
    00073     inline float LengthSquared() const { return x * x + y * y + z * z; }
    00074
    00077     inline float Length() const { return sqrtf(x * x + y * y + z * z); }
    00078
    00080     inline void NormalizeIt() { float l = Length(); if (l > 0.0f) { x /= l; y /= l; z /= l; } else { x = y = 0; z = 1; }}
    00081
    00084     inline FMVector3 Normalize() const { float l = Length(); return (l > 0.0f) ? FMVector3(x / l, y / l, z / l) : FMVector3::XAxis; }
    00085
    00088     inline void Project(const FMVector3& unto) { (*this) = Projected(unto); }
    00089
    00093     inline FMVector3 Projected(const FMVector3& unto) const;
    00094
    00097     inline operator float*() { return &x; }
    00098
    00101     inline operator const float*() const { return &x; }
    00102
    00107     inline void Set(float x, float y, float z) { this->x = x; this->y = y, this->z = z; }
    00108
    00115     inline FMVector3& operator =(const float* v) { x = *v; y = *(v + 1); z = *(v + 2); return *this; }
    00116
    00120     inline FMVector3& operator =(const FMVector3& rhs) { x=rhs.x; y=rhs.y; z=rhs.z; return *this; }
    00121
    00126     inline void ComponentMinimum(const FMVector3& min) { if (x < min.x) x = min.x; if (y < min.y) y = min.y; if (z < min.z) z = min.z; }
    00127
    00130     inline float ComponentMinimum() const { return min(fabsf(x), min(fabsf(y), fabsf(z))); }
    00131
    00136     inline void ComponentMaximum(const FMVector3& max) { if (x > max.x) x = max.x; if (y > max.y) y = max.y; if (z > max.z) z = max.z; }
    00137
    00140     inline float ComponentMaximum() const { return max(fabsf(x), max(fabsf(y), fabsf(z))); }
    00141
    00149     inline void ComponentClamp(const FMVector3& min, const FMVector3& max) { ComponentMinimum(min); ComponentMaximum(max); }
    00150
    00153     inline float ComponentAverage() const { return (fabsf(x) + fabsf(y) + fabsf(z)) / 3.0f; }
    00154
    00155 public:
    00156     static const FMVector3 XAxis;
    00157     static const FMVector3 YAxis;
    00158     static const FMVector3 ZAxis;
    00159     static const FMVector3 Origin;
    00160     static const FMVector3 Zero;
    00161     static const FMVector3 One;
    00162 };
    00163
    00168 inline FMVector3 operator +(const FMVector3& a, const FMVector3& b) { return FMVector3(a.x + b.x, a.y + b.y, a.z + b.z); }
    00169
    00174 inline FMVector3 operator -(const FMVector3& a, const FMVector3& b) { return FMVector3(a.x - b.x, a.y - b.y, a.z - b.z); }
    00175
    00180 inline FMVector3 operator +(const FMVector3& a) { return FMVector3(+a.x, +a.y, +a.z); }
    00181
    00186 inline FMVector3 operator -(const FMVector3& a) { return FMVector3(-a.x, -a.y, -a.z); }
    00187
    00192 inline float operator *(const FMVector3& a, const FMVector3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
    00193
    00198 inline FMVector3 operator *(const FMVector3& a, float b) { return FMVector3(a.x * b, a.y * b, a.z * b); }
    00199
    00204 inline FMVector3 operator *(float a, const FMVector3& b) { return FMVector3(a * b.x, a * b.y, a * b.z); }
    00205
    00210 inline FMVector3 operator /(const FMVector3& a, float b) { return FMVector3(a.x / b, a.y / b, a.z / b); }
    00211
    00216 inline FMVector3 operator ^(const FMVector3& a, const FMVector3& b) { return FMVector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); }
    00217
    00222 inline FMVector3& operator +=(FMVector3& b, const FMVector3& a) { b.x += a.x; b.y += a.y; b.z += a.z; return b; }
    00223
    00228 inline FMVector3& operator -=(FMVector3& b, const FMVector3& a) { b.x -= a.x; b.y -= a.y; b.z -= a.z; return b; }
    00229
    00234 inline FMVector3& operator *=(FMVector3& b, float a) { b.x *= a; b.y *= a; b.z *= a; return b; }
    00235
    00240 inline FMVector3& operator /=(FMVector3& b, float a) { b.x /= a; b.y /= a; b.z /= a; return b; }
    00241
    00246 inline bool IsEquivalent(const FMVector3& p, const FMVector3& q) { return IsEquivalent(p.x, q.x) && IsEquivalent(p.y, q.y) && IsEquivalent(p.z, q.z); }
    00247 inline bool operator == (const FMVector3& p, const FMVector3& q) { return IsEquivalent(p, q); }
    00249 // Already documented above.
    00250 inline FMVector3 FMVector3::Projected(const FMVector3& unto) const { return ((*this) * unto) / unto.LengthSquared() * unto; }
    00251
    00253 typedef fm::vector<FMVector3> FMVector3List;
    00254
    00255 #endif // _FM_VECTOR3_H_