| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_VEC4F |
|---|
| 15 | #define OSG_VEC4F 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Vec3f> |
|---|
| 18 | |
|---|
| 19 | namespace osg { |
|---|
| 20 | |
|---|
| 21 | /** General purpose float quad. Uses include representation |
|---|
| 22 | * of color coordinates. |
|---|
| 23 | * No support yet added for float * Vec4f - is it necessary? |
|---|
| 24 | * Need to define a non-member non-friend operator* etc. |
|---|
| 25 | * Vec4f * float is okay |
|---|
| 26 | */ |
|---|
| 27 | class Vec4f |
|---|
| 28 | { |
|---|
| 29 | public: |
|---|
| 30 | |
|---|
| 31 | /** Type of Vec class.*/ |
|---|
| 32 | typedef float value_type; |
|---|
| 33 | |
|---|
| 34 | /** Number of vector components. */ |
|---|
| 35 | enum { num_components = 4 }; |
|---|
| 36 | |
|---|
| 37 | /** Vec member variable. */ |
|---|
| 38 | value_type _v[4]; |
|---|
| 39 | |
|---|
| 40 | // Methods are defined here so that they are implicitly inlined |
|---|
| 41 | |
|---|
| 42 | Vec4f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f; _v[3]=0.0f;} |
|---|
| 43 | |
|---|
| 44 | Vec4f(value_type x, value_type y, value_type z, value_type w) |
|---|
| 45 | { |
|---|
| 46 | _v[0]=x; |
|---|
| 47 | _v[1]=y; |
|---|
| 48 | _v[2]=z; |
|---|
| 49 | _v[3]=w; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | Vec4f(const Vec3f& v3,value_type w) |
|---|
| 53 | { |
|---|
| 54 | _v[0]=v3[0]; |
|---|
| 55 | _v[1]=v3[1]; |
|---|
| 56 | _v[2]=v3[2]; |
|---|
| 57 | _v[3]=w; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | inline bool operator == (const Vec4f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; } |
|---|
| 61 | |
|---|
| 62 | inline bool operator != (const Vec4f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; } |
|---|
| 63 | |
|---|
| 64 | inline bool operator < (const Vec4f& v) const |
|---|
| 65 | { |
|---|
| 66 | if (_v[0]<v._v[0]) return true; |
|---|
| 67 | else if (_v[0]>v._v[0]) return false; |
|---|
| 68 | else if (_v[1]<v._v[1]) return true; |
|---|
| 69 | else if (_v[1]>v._v[1]) return false; |
|---|
| 70 | else if (_v[2]<v._v[2]) return true; |
|---|
| 71 | else if (_v[2]>v._v[2]) return false; |
|---|
| 72 | else return (_v[3]<v._v[3]); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | inline value_type* ptr() { return _v; } |
|---|
| 76 | inline const value_type* ptr() const { return _v; } |
|---|
| 77 | |
|---|
| 78 | inline void set( value_type x, value_type y, value_type z, value_type w) |
|---|
| 79 | { |
|---|
| 80 | _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | inline value_type& operator [] (unsigned int i) { return _v[i]; } |
|---|
| 84 | inline value_type operator [] (unsigned int i) const { return _v[i]; } |
|---|
| 85 | |
|---|
| 86 | inline value_type& x() { return _v[0]; } |
|---|
| 87 | inline value_type& y() { return _v[1]; } |
|---|
| 88 | inline value_type& z() { return _v[2]; } |
|---|
| 89 | inline value_type& w() { return _v[3]; } |
|---|
| 90 | |
|---|
| 91 | inline value_type x() const { return _v[0]; } |
|---|
| 92 | inline value_type y() const { return _v[1]; } |
|---|
| 93 | inline value_type z() const { return _v[2]; } |
|---|
| 94 | inline value_type w() const { return _v[3]; } |
|---|
| 95 | |
|---|
| 96 | inline value_type& r() { return _v[0]; } |
|---|
| 97 | inline value_type& g() { return _v[1]; } |
|---|
| 98 | inline value_type& b() { return _v[2]; } |
|---|
| 99 | inline value_type& a() { return _v[3]; } |
|---|
| 100 | |
|---|
| 101 | inline value_type r() const { return _v[0]; } |
|---|
| 102 | inline value_type g() const { return _v[1]; } |
|---|
| 103 | inline value_type b() const { return _v[2]; } |
|---|
| 104 | inline value_type a() const { return _v[3]; } |
|---|
| 105 | |
|---|
| 106 | inline unsigned int asABGR() const |
|---|
| 107 | { |
|---|
| 108 | return (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f)<<24 | |
|---|
| 109 | (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<16 | |
|---|
| 110 | (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<8 | |
|---|
| 111 | (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | inline unsigned int asRGBA() const |
|---|
| 115 | { |
|---|
| 116 | return (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f)<<24 | |
|---|
| 117 | (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<16 | |
|---|
| 118 | (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<8 | |
|---|
| 119 | (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | inline bool valid() const { return !isNaN(); } |
|---|
| 123 | inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]) || osg::isNaN(_v[3]); } |
|---|
| 124 | |
|---|
| 125 | /** Dot product. */ |
|---|
| 126 | inline value_type operator * (const Vec4f& rhs) const |
|---|
| 127 | { |
|---|
| 128 | return _v[0]*rhs._v[0]+ |
|---|
| 129 | _v[1]*rhs._v[1]+ |
|---|
| 130 | _v[2]*rhs._v[2]+ |
|---|
| 131 | _v[3]*rhs._v[3] ; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** Multiply by scalar. */ |
|---|
| 135 | inline Vec4f operator * (value_type rhs) const |
|---|
| 136 | { |
|---|
| 137 | return Vec4f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** Unary multiply by scalar. */ |
|---|
| 141 | inline Vec4f& operator *= (value_type rhs) |
|---|
| 142 | { |
|---|
| 143 | _v[0]*=rhs; |
|---|
| 144 | _v[1]*=rhs; |
|---|
| 145 | _v[2]*=rhs; |
|---|
| 146 | _v[3]*=rhs; |
|---|
| 147 | return *this; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** Unary multiply by vector. */ |
|---|
| 151 | inline Vec4f& operator *= (const Vec4f& rhs) |
|---|
| 152 | { |
|---|
| 153 | _v[0]*=rhs[0]; |
|---|
| 154 | _v[1]*=rhs[1]; |
|---|
| 155 | _v[2]*=rhs[2]; |
|---|
| 156 | _v[3]*=rhs[3]; |
|---|
| 157 | return *this; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /** Divide by scalar. */ |
|---|
| 161 | inline Vec4f operator / (value_type rhs) const |
|---|
| 162 | { |
|---|
| 163 | return Vec4f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /** Unary divide by scalar. */ |
|---|
| 167 | inline Vec4f& operator /= (value_type rhs) |
|---|
| 168 | { |
|---|
| 169 | _v[0]/=rhs; |
|---|
| 170 | _v[1]/=rhs; |
|---|
| 171 | _v[2]/=rhs; |
|---|
| 172 | _v[3]/=rhs; |
|---|
| 173 | return *this; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** Unary divide by vector. */ |
|---|
| 177 | inline Vec4f& operator /= (const Vec4f& rhs) |
|---|
| 178 | { |
|---|
| 179 | _v[0]/=rhs[0]; |
|---|
| 180 | _v[1]/=rhs[1]; |
|---|
| 181 | _v[2]/=rhs[2]; |
|---|
| 182 | _v[3]/=rhs[3]; |
|---|
| 183 | return *this; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /** Binary vector add. */ |
|---|
| 187 | inline Vec4f operator + (const Vec4f& rhs) const |
|---|
| 188 | { |
|---|
| 189 | return Vec4f(_v[0]+rhs._v[0], _v[1]+rhs._v[1], |
|---|
| 190 | _v[2]+rhs._v[2], _v[3]+rhs._v[3]); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /** Unary vector add. Slightly more efficient because no temporary |
|---|
| 194 | * intermediate object. |
|---|
| 195 | */ |
|---|
| 196 | inline Vec4f& operator += (const Vec4f& rhs) |
|---|
| 197 | { |
|---|
| 198 | _v[0] += rhs._v[0]; |
|---|
| 199 | _v[1] += rhs._v[1]; |
|---|
| 200 | _v[2] += rhs._v[2]; |
|---|
| 201 | _v[3] += rhs._v[3]; |
|---|
| 202 | return *this; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** Binary vector subtract. */ |
|---|
| 206 | inline Vec4f operator - (const Vec4f& rhs) const |
|---|
| 207 | { |
|---|
| 208 | return Vec4f(_v[0]-rhs._v[0], _v[1]-rhs._v[1], |
|---|
| 209 | _v[2]-rhs._v[2], _v[3]-rhs._v[3] ); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | /** Unary vector subtract. */ |
|---|
| 213 | inline Vec4f& operator -= (const Vec4f& rhs) |
|---|
| 214 | { |
|---|
| 215 | _v[0]-=rhs._v[0]; |
|---|
| 216 | _v[1]-=rhs._v[1]; |
|---|
| 217 | _v[2]-=rhs._v[2]; |
|---|
| 218 | _v[3]-=rhs._v[3]; |
|---|
| 219 | return *this; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /** Negation operator. Returns the negative of the Vec4f. */ |
|---|
| 223 | inline const Vec4f operator - () const |
|---|
| 224 | { |
|---|
| 225 | return Vec4f (-_v[0], -_v[1], -_v[2], -_v[3]); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /** Length of the vector = sqrt( vec . vec ) */ |
|---|
| 229 | inline value_type length() const |
|---|
| 230 | { |
|---|
| 231 | return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | /** Length squared of the vector = vec . vec */ |
|---|
| 235 | inline value_type length2() const |
|---|
| 236 | { |
|---|
| 237 | return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /** Normalize the vector so that it has length unity. |
|---|
| 241 | * Returns the previous length of the vector. |
|---|
| 242 | */ |
|---|
| 243 | inline value_type normalize() |
|---|
| 244 | { |
|---|
| 245 | value_type norm = Vec4f::length(); |
|---|
| 246 | if (norm>0.0f) |
|---|
| 247 | { |
|---|
| 248 | value_type inv = 1.0f/norm; |
|---|
| 249 | _v[0] *= inv; |
|---|
| 250 | _v[1] *= inv; |
|---|
| 251 | _v[2] *= inv; |
|---|
| 252 | _v[3] *= inv; |
|---|
| 253 | } |
|---|
| 254 | return( norm ); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | }; // end of class Vec4f |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | /** Compute the dot product of a (Vec3,1.0) and a Vec4f. */ |
|---|
| 262 | inline Vec4f::value_type operator * (const Vec3f& lhs,const Vec4f& rhs) |
|---|
| 263 | { |
|---|
| 264 | return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3]; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | /** Compute the dot product of a Vec4f and a (Vec3,1.0). */ |
|---|
| 268 | inline Vec4f::value_type operator * (const Vec4f& lhs,const Vec3f& rhs) |
|---|
| 269 | { |
|---|
| 270 | return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3]; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | } // end of namespace osg |
|---|
| 274 | |
|---|
| 275 | #endif |
|---|
| 276 | |
|---|