| 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_VEC3S |
|---|
| 15 | #define OSG_VEC3S 1 |
|---|
| 16 | |
|---|
| 17 | namespace osg { |
|---|
| 18 | |
|---|
| 19 | class Vec3s |
|---|
| 20 | { |
|---|
| 21 | public: |
|---|
| 22 | |
|---|
| 23 | /** Data type of vector components.*/ |
|---|
| 24 | typedef short value_type; |
|---|
| 25 | |
|---|
| 26 | /** Number of vector components. */ |
|---|
| 27 | enum { num_components = 3 }; |
|---|
| 28 | |
|---|
| 29 | value_type _v[3]; |
|---|
| 30 | |
|---|
| 31 | /** Constructor that sets all components of the vector to zero */ |
|---|
| 32 | Vec3s() { _v[0]=0; _v[1]=0; _v[2]=0; } |
|---|
| 33 | |
|---|
| 34 | Vec3s(value_type r, value_type g, value_type b) { _v[0]=r; _v[1]=g; _v[2]=b; } |
|---|
| 35 | |
|---|
| 36 | inline bool operator == (const Vec3s& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; } |
|---|
| 37 | inline bool operator != (const Vec3s& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; } |
|---|
| 38 | inline bool operator < (const Vec3s& v) const |
|---|
| 39 | { |
|---|
| 40 | if (_v[0]<v._v[0]) return true; |
|---|
| 41 | else if (_v[0]>v._v[0]) return false; |
|---|
| 42 | else if (_v[1]<v._v[1]) return true; |
|---|
| 43 | else if (_v[1]>v._v[1]) return false; |
|---|
| 44 | else return (_v[2]<v._v[2]); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | inline value_type* ptr() { return _v; } |
|---|
| 48 | inline const value_type* ptr() const { return _v; } |
|---|
| 49 | |
|---|
| 50 | inline void set(value_type r, value_type g, value_type b) |
|---|
| 51 | { |
|---|
| 52 | _v[0]=r; _v[1]=g; _v[2]=b; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | inline void set( const Vec3s& rhs) |
|---|
| 56 | { |
|---|
| 57 | _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2]; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | inline value_type& operator [] (unsigned int i) { return _v[i]; } |
|---|
| 61 | inline value_type operator [] (unsigned int i) const { return _v[i]; } |
|---|
| 62 | |
|---|
| 63 | inline value_type& x() { return _v[0]; } |
|---|
| 64 | inline value_type& y() { return _v[1]; } |
|---|
| 65 | inline value_type& z() { return _v[2]; } |
|---|
| 66 | |
|---|
| 67 | inline value_type x() const { return _v[0]; } |
|---|
| 68 | inline value_type y() const { return _v[1]; } |
|---|
| 69 | inline value_type z() const { return _v[2]; } |
|---|
| 70 | |
|---|
| 71 | inline value_type& r() { return _v[0]; } |
|---|
| 72 | inline value_type& g() { return _v[1]; } |
|---|
| 73 | inline value_type& b() { return _v[2]; } |
|---|
| 74 | |
|---|
| 75 | inline value_type r() const { return _v[0]; } |
|---|
| 76 | inline value_type g() const { return _v[1]; } |
|---|
| 77 | inline value_type b() const { return _v[2]; } |
|---|
| 78 | |
|---|
| 79 | /** Multiply by scalar. */ |
|---|
| 80 | inline Vec3s operator * (value_type rhs) const |
|---|
| 81 | { |
|---|
| 82 | return Vec3s(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** Unary multiply by scalar. */ |
|---|
| 86 | inline Vec3s& operator *= (value_type rhs) |
|---|
| 87 | { |
|---|
| 88 | _v[0]*=rhs; |
|---|
| 89 | _v[1]*=rhs; |
|---|
| 90 | _v[2]*=rhs; |
|---|
| 91 | return *this; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** Divide by scalar. */ |
|---|
| 95 | inline Vec3s operator / (value_type rhs) const |
|---|
| 96 | { |
|---|
| 97 | return Vec3s(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** Unary divide by scalar. */ |
|---|
| 101 | inline Vec3s& operator /= (value_type rhs) |
|---|
| 102 | { |
|---|
| 103 | _v[0]/=rhs; |
|---|
| 104 | _v[1]/=rhs; |
|---|
| 105 | _v[2]/=rhs; |
|---|
| 106 | return *this; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** Binary vector multiply. */ |
|---|
| 110 | inline Vec3s operator * (const Vec3s& rhs) const |
|---|
| 111 | { |
|---|
| 112 | return Vec3s(_v[0]*rhs._v[0], _v[1]*rhs._v[1], _v[2]*rhs._v[2]); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** Binary vector add. */ |
|---|
| 116 | inline Vec3s operator + (const Vec3s& rhs) const |
|---|
| 117 | { |
|---|
| 118 | return Vec3s(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** Unary vector add. Slightly more efficient because no temporary |
|---|
| 122 | * intermediate object. |
|---|
| 123 | */ |
|---|
| 124 | inline Vec3s& operator += (const Vec3s& rhs) |
|---|
| 125 | { |
|---|
| 126 | _v[0] += rhs._v[0]; |
|---|
| 127 | _v[1] += rhs._v[1]; |
|---|
| 128 | _v[2] += rhs._v[2]; |
|---|
| 129 | return *this; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** Binary vector subtract. */ |
|---|
| 133 | inline Vec3s operator - (const Vec3s& rhs) const |
|---|
| 134 | { |
|---|
| 135 | return Vec3s(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | /** Unary vector subtract. */ |
|---|
| 139 | inline Vec3s& operator -= (const Vec3s& rhs) |
|---|
| 140 | { |
|---|
| 141 | _v[0]-=rhs._v[0]; |
|---|
| 142 | _v[1]-=rhs._v[1]; |
|---|
| 143 | _v[2]-=rhs._v[2]; |
|---|
| 144 | return *this; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** Negation operator. Returns the negative of the Vec3s. */ |
|---|
| 148 | inline Vec3s operator - () const |
|---|
| 149 | { |
|---|
| 150 | return Vec3s (-_v[0], -_v[1], -_v[2]); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | }; // end of class Vec3s |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | /** multiply by vector components. */ |
|---|
| 157 | inline Vec3s componentMultiply(const Vec3s& lhs, const Vec3s& rhs) |
|---|
| 158 | { |
|---|
| 159 | return Vec3s(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** divide rhs components by rhs vector components. */ |
|---|
| 163 | inline Vec3s componentDivide(const Vec3s& lhs, const Vec3s& rhs) |
|---|
| 164 | { |
|---|
| 165 | return Vec3s(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | } // end of namespace osg |
|---|
| 169 | |
|---|
| 170 | #endif |
|---|
| 171 | |
|---|