[774] | 1 | #ifndef GFXMATH_VEC4_INCLUDED // -*- C++ -*-
|
---|
| 2 | #define GFXMATH_VEC4_INCLUDED
|
---|
| 3 |
|
---|
| 4 | /************************************************************************
|
---|
| 5 |
|
---|
| 6 | 4D Vector class.
|
---|
| 7 |
|
---|
| 8 | $Id: Vec4.h,v 1.10 1997/03/17 22:52:27 garland Exp $
|
---|
| 9 |
|
---|
| 10 | ************************************************************************/
|
---|
| 11 |
|
---|
[1025] | 12 | namespace simplif
|
---|
[774] | 13 | {
|
---|
| 14 | class Vec4 {
|
---|
| 15 | private:
|
---|
| 16 | real elt[4];
|
---|
| 17 |
|
---|
| 18 | protected:
|
---|
| 19 | inline void copy(const Vec4& v);
|
---|
| 20 |
|
---|
| 21 | public:
|
---|
| 22 | //
|
---|
| 23 | // Standard constructors
|
---|
| 24 | //
|
---|
| 25 | Vec4(real x=0, real y=0, real z=0, real w=0) {
|
---|
| 26 | elt[0]=x; elt[1]=y; elt[2]=z; elt[3]=w;
|
---|
| 27 | }
|
---|
| 28 | #ifdef GFXMATH_VEC3_INCLUDED
|
---|
| 29 | Vec4(const Vec3& v,real w) {elt[0]=v[0];elt[1]=v[1];elt[2]=v[2];elt[3]=w;}
|
---|
| 30 | #endif
|
---|
| 31 | Vec4(const Vec4& v) { copy(v); }
|
---|
| 32 | Vec4(const real *v) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=v[2]; elt[3]=v[3]; }
|
---|
| 33 |
|
---|
| 34 | //
|
---|
| 35 | // Access methods
|
---|
| 36 | //
|
---|
| 37 | #ifdef SAFETY
|
---|
| 38 | real& operator()(int i) { assert(i>=0 && i<4); return elt[i]; }
|
---|
| 39 | real operator()(int i) const { assert(i>=0 && i<4); return elt[i]; }
|
---|
| 40 | #else
|
---|
| 41 | real& operator()(int i) { return elt[i]; }
|
---|
| 42 | real operator()(int i) const { return elt[i]; }
|
---|
| 43 | #endif
|
---|
| 44 | real& operator[](int i) { return elt[i]; }
|
---|
| 45 | const real& operator[](int i) const { return elt[i]; }
|
---|
| 46 |
|
---|
| 47 | real *raw() { return elt; }
|
---|
| 48 | const real *raw() const { return elt; }
|
---|
| 49 |
|
---|
| 50 | //
|
---|
| 51 | // Comparison methods
|
---|
| 52 | //
|
---|
| 53 | inline bool operator==(const Vec4&) const;
|
---|
| 54 | inline bool operator!=(const Vec4&) const;
|
---|
| 55 |
|
---|
| 56 | //
|
---|
| 57 | // Assignment and in-place arithmetic methods
|
---|
| 58 | //
|
---|
| 59 | inline void set(real x, real y, real z, real w){
|
---|
| 60 | elt[0]=x; elt[1]=y; elt[2]=z; elt[3]=w;
|
---|
| 61 | }
|
---|
| 62 | inline Vec4& operator=(const Vec4& v);
|
---|
| 63 | inline Vec4& operator+=(const Vec4& v);
|
---|
| 64 | inline Vec4& operator-=(const Vec4& v);
|
---|
| 65 | inline Vec4& operator*=(real s);
|
---|
| 66 | inline Vec4& operator/=(real s);
|
---|
| 67 |
|
---|
| 68 | //
|
---|
| 69 | // Binary arithmetic methods
|
---|
| 70 | //
|
---|
| 71 | inline Vec4 operator+(const Vec4& v) const;
|
---|
| 72 | inline Vec4 operator-(const Vec4& v) const;
|
---|
| 73 | inline Vec4 operator-() const;
|
---|
| 74 |
|
---|
| 75 | inline Vec4 operator*(real s) const;
|
---|
| 76 | inline Vec4 operator/(real s) const;
|
---|
| 77 | inline real operator*(const Vec4& v) const;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | ////////////////////////////////////////////////////////////////////////
|
---|
| 83 | //
|
---|
| 84 | // Method definitions
|
---|
| 85 | //
|
---|
| 86 |
|
---|
| 87 | inline void Vec4::copy(const Vec4& v)
|
---|
| 88 | {
|
---|
| 89 | elt[0]=v.elt[0]; elt[1]=v.elt[1]; elt[2]=v.elt[2]; elt[3]=v.elt[3];
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | inline bool Vec4::operator==(const Vec4& v) const
|
---|
| 93 | {
|
---|
| 94 | real dx=elt[X]-v[X], dy=elt[Y]-v[Y], dz=elt[Z]-v[Z], dw=elt[W]-v[W];
|
---|
| 95 | return (dx*dx + dy*dy + dz*dz + dw*dw) < FEQ_EPS2;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | inline bool Vec4::operator!=(const Vec4& v) const
|
---|
| 99 | {
|
---|
| 100 | real dx=elt[X]-v[X], dy=elt[Y]-v[Y], dz=elt[Z]-v[Z], dw=elt[W]-v[W];
|
---|
| 101 | return (dx*dx + dy*dy + dz*dz + dw*dw) > FEQ_EPS2;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | inline Vec4& Vec4::operator=(const Vec4& v)
|
---|
| 105 | {
|
---|
| 106 | copy(v);
|
---|
| 107 | return *this;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | inline Vec4& Vec4::operator+=(const Vec4& v)
|
---|
| 111 | {
|
---|
| 112 | elt[0] += v[0]; elt[1] += v[1]; elt[2] += v[2]; elt[3] += v[3];
|
---|
| 113 | return *this;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | inline Vec4& Vec4::operator-=(const Vec4& v)
|
---|
| 117 | {
|
---|
| 118 | elt[0] -= v[0]; elt[1] -= v[1]; elt[2] -= v[2]; elt[3] -= v[3];
|
---|
| 119 | return *this;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | inline Vec4& Vec4::operator*=(real s)
|
---|
| 123 | {
|
---|
| 124 | elt[0] *= s; elt[1] *= s; elt[2] *= s; elt[3] *= s;
|
---|
| 125 | return *this;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | inline Vec4& Vec4::operator/=(real s)
|
---|
| 129 | {
|
---|
| 130 | elt[0] /= s; elt[1] /= s; elt[2] /= s; elt[3] /= s;
|
---|
| 131 | return *this;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | inline Vec4 Vec4::operator+(const Vec4& v) const
|
---|
| 135 | {
|
---|
| 136 | return Vec4(elt[0]+v[0], elt[1]+v[1], elt[2]+v[2], elt[3]+v[3]);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | inline Vec4 Vec4::operator-(const Vec4& v) const
|
---|
| 140 | {
|
---|
| 141 | return Vec4(elt[0]-v[0], elt[1]-v[1], elt[2]-v[2], elt[3]-v[3]);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | inline Vec4 Vec4::operator-() const
|
---|
| 145 | {
|
---|
| 146 | return Vec4(-elt[0], -elt[1], -elt[2], -elt[3]);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | inline Vec4 Vec4::operator*(real s) const
|
---|
| 150 | {
|
---|
| 151 | return Vec4(elt[0]*s, elt[1]*s, elt[2]*s, elt[3]*s);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | inline Vec4 Vec4::operator/(real s) const
|
---|
| 155 | {
|
---|
| 156 | return Vec4(elt[0]/s, elt[1]/s, elt[2]/s, elt[3]/s);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | inline real Vec4::operator*(const Vec4& v) const
|
---|
| 160 | {
|
---|
| 161 | return elt[0]*v[0] + elt[1]*v[1] + elt[2]*v[2] + elt[3]*v[3];
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | // Make scalar multiplication commutative
|
---|
| 165 | inline Vec4 operator*(real s, const Vec4& v) { return v*s; }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | ////////////////////////////////////////////////////////////////////////
|
---|
| 170 | //
|
---|
| 171 | // Primitive function definitions
|
---|
| 172 | //
|
---|
| 173 |
|
---|
| 174 | //
|
---|
| 175 | // Code adapted from VecLib4d.c in Graphics Gems V
|
---|
| 176 | inline Vec4 cross(const Vec4& a, const Vec4& b, const Vec4& c)
|
---|
| 177 | {
|
---|
| 178 | Vec4 result;
|
---|
| 179 |
|
---|
| 180 | real d1 = (b[Z] * c[W]) - (b[W] * c[Z]);
|
---|
| 181 | real d2 = (b[Y] * c[W]) - (b[W] * c[Y]);
|
---|
| 182 | real d3 = (b[Y] * c[Z]) - (b[Z] * c[Y]);
|
---|
| 183 | real d4 = (b[X] * c[W]) - (b[W] * c[X]);
|
---|
| 184 | real d5 = (b[X] * c[Z]) - (b[Z] * c[X]);
|
---|
| 185 | real d6 = (b[X] * c[Y]) - (b[Y] * c[X]);
|
---|
| 186 |
|
---|
| 187 | result[X] = - a[Y] * d1 + a[Z] * d2 - a[W] * d3;
|
---|
| 188 | result[Y] = a[X] * d1 - a[Z] * d4 + a[W] * d5;
|
---|
| 189 | result[Z] = - a[X] * d2 + a[Y] * d4 - a[W] * d6;
|
---|
| 190 | result[W] = a[X] * d3 - a[Y] * d5 + a[Z] * d6;
|
---|
| 191 |
|
---|
| 192 | return result;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | inline real norm(const Vec4& v)
|
---|
| 196 | {
|
---|
| 197 | return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | inline real norm2(const Vec4& v)
|
---|
| 201 | {
|
---|
| 202 | return v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3];
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | inline real length(const Vec4& v) { return norm(v); }
|
---|
| 206 |
|
---|
| 207 | inline real unitize(Vec4& v)
|
---|
| 208 | {
|
---|
| 209 | real l=norm2(v);
|
---|
| 210 | if( l!=1.0 && l!=0.0 )
|
---|
| 211 | {
|
---|
| 212 | l = sqrt(l);
|
---|
| 213 | v /= l;
|
---|
| 214 | }
|
---|
| 215 | return l;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 | ////////////////////////////////////////////////////////////////////////
|
---|
| 220 | //
|
---|
| 221 | // Misc. function definitions
|
---|
| 222 | //
|
---|
| 223 |
|
---|
| 224 | /* inline ostream& operator<<(ostream& out, zVec4& v)
|
---|
| 225 | {
|
---|
| 226 | return
|
---|
| 227 | out << "[" << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << "]";
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | inline istream& operator>>(istream& in, Vec4& v)
|
---|
| 231 | {
|
---|
| 232 | return in >> "[" >> v[0] >> v[1] >> v[2] >> v[3] >> "]";
|
---|
| 233 | }
|
---|
| 234 | */
|
---|
| 235 | #ifdef GFXGL_INCLUDED
|
---|
| 236 | inline void glV(const Vec4& v) { glVertex(v[X], v[Y], v[Z], v[W]); }
|
---|
| 237 | inline void glC(const Vec4& v) { glColor(v[X], v[Y], v[Z], v[W]); }
|
---|
| 238 | #endif
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | // GFXMATH_VEC4_INCLUDED
|
---|
| 242 | #endif
|
---|