| 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_VEC2F |
|---|
| 15 | #define OSG_VEC2F 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Math> |
|---|
| 18 | |
|---|
| 19 | namespace osg { |
|---|
| 20 | |
|---|
| 21 | /** General purpose float pair. Uses include representation of |
|---|
| 22 | * texture coordinates. |
|---|
| 23 | * No support yet added for float * Vec2f - is it necessary? |
|---|
| 24 | * Need to define a non-member non-friend operator* etc. |
|---|
| 25 | * BTW: Vec2f * float is okay |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | class Vec2f |
|---|
| 29 | { |
|---|
| 30 | public: |
|---|
| 31 | |
|---|
| 32 | /** Type of Vec class.*/ |
|---|
| 33 | typedef float value_type; |
|---|
| 34 | |
|---|
| 35 | /** Number of vector components. */ |
|---|
| 36 | enum { num_components = 2 }; |
|---|
| 37 | |
|---|
| 38 | /** Vec member varaible. */ |
|---|
| 39 | value_type _v[2]; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | Vec2f() {_v[0]=0.0; _v[1]=0.0;} |
|---|
| 43 | Vec2f(value_type x,value_type y) { _v[0]=x; _v[1]=y; } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | inline bool operator == (const Vec2f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; } |
|---|
| 47 | |
|---|
| 48 | inline bool operator != (const Vec2f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; } |
|---|
| 49 | |
|---|
| 50 | inline bool operator < (const Vec2f& v) const |
|---|
| 51 | { |
|---|
| 52 | if (_v[0]<v._v[0]) return true; |
|---|
| 53 | else if (_v[0]>v._v[0]) return false; |
|---|
| 54 | else return (_v[1]<v._v[1]); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | inline value_type * ptr() { return _v; } |
|---|
| 58 | inline const value_type * ptr() const { return _v; } |
|---|
| 59 | |
|---|
| 60 | inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; } |
|---|
| 61 | |
|---|
| 62 | inline value_type & operator [] (int i) { return _v[i]; } |
|---|
| 63 | inline value_type operator [] (int i) const { return _v[i]; } |
|---|
| 64 | |
|---|
| 65 | inline value_type & x() { return _v[0]; } |
|---|
| 66 | inline value_type & y() { return _v[1]; } |
|---|
| 67 | |
|---|
| 68 | inline value_type x() const { return _v[0]; } |
|---|
| 69 | inline value_type y() const { return _v[1]; } |
|---|
| 70 | |
|---|
| 71 | inline bool valid() const { return !isNaN(); } |
|---|
| 72 | inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); } |
|---|
| 73 | |
|---|
| 74 | /** Dot product. */ |
|---|
| 75 | inline value_type operator * (const Vec2f& rhs) const |
|---|
| 76 | { |
|---|
| 77 | return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** Multiply by scalar. */ |
|---|
| 81 | inline const Vec2f operator * (value_type rhs) const |
|---|
| 82 | { |
|---|
| 83 | return Vec2f(_v[0]*rhs, _v[1]*rhs); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /** Unary multiply by scalar. */ |
|---|
| 87 | inline Vec2f& operator *= (value_type rhs) |
|---|
| 88 | { |
|---|
| 89 | _v[0]*=rhs; |
|---|
| 90 | _v[1]*=rhs; |
|---|
| 91 | return *this; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** Divide by scalar. */ |
|---|
| 95 | inline const Vec2f operator / (value_type rhs) const |
|---|
| 96 | { |
|---|
| 97 | return Vec2f(_v[0]/rhs, _v[1]/rhs); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** Unary divide by scalar. */ |
|---|
| 101 | inline Vec2f& operator /= (value_type rhs) |
|---|
| 102 | { |
|---|
| 103 | _v[0]/=rhs; |
|---|
| 104 | _v[1]/=rhs; |
|---|
| 105 | return *this; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** Binary vector add. */ |
|---|
| 109 | inline const Vec2f operator + (const Vec2f& rhs) const |
|---|
| 110 | { |
|---|
| 111 | return Vec2f(_v[0]+rhs._v[0], _v[1]+rhs._v[1]); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** Unary vector add. Slightly more efficient because no temporary |
|---|
| 115 | * intermediate object. |
|---|
| 116 | */ |
|---|
| 117 | inline Vec2f& operator += (const Vec2f& rhs) |
|---|
| 118 | { |
|---|
| 119 | _v[0] += rhs._v[0]; |
|---|
| 120 | _v[1] += rhs._v[1]; |
|---|
| 121 | return *this; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** Binary vector subtract. */ |
|---|
| 125 | inline const Vec2f operator - (const Vec2f& rhs) const |
|---|
| 126 | { |
|---|
| 127 | return Vec2f(_v[0]-rhs._v[0], _v[1]-rhs._v[1]); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** Unary vector subtract. */ |
|---|
| 131 | inline Vec2f& operator -= (const Vec2f& rhs) |
|---|
| 132 | { |
|---|
| 133 | _v[0]-=rhs._v[0]; |
|---|
| 134 | _v[1]-=rhs._v[1]; |
|---|
| 135 | return *this; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | /** Negation operator. Returns the negative of the Vec2f. */ |
|---|
| 139 | inline const Vec2f operator - () const |
|---|
| 140 | { |
|---|
| 141 | return Vec2f (-_v[0], -_v[1]); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /** Length of the vector = sqrt( vec . vec ) */ |
|---|
| 145 | inline value_type length() const |
|---|
| 146 | { |
|---|
| 147 | return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] ); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** Length squared of the vector = vec . vec */ |
|---|
| 151 | inline value_type length2( void ) const |
|---|
| 152 | { |
|---|
| 153 | return _v[0]*_v[0] + _v[1]*_v[1]; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** Normalize the vector so that it has length unity. |
|---|
| 157 | * Returns the previous length of the vector. |
|---|
| 158 | */ |
|---|
| 159 | inline value_type normalize() |
|---|
| 160 | { |
|---|
| 161 | value_type norm = Vec2f::length(); |
|---|
| 162 | if (norm>0.0) |
|---|
| 163 | { |
|---|
| 164 | value_type inv = 1.0f/norm; |
|---|
| 165 | _v[0] *= inv; |
|---|
| 166 | _v[1] *= inv; |
|---|
| 167 | } |
|---|
| 168 | return( norm ); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | }; // end of class Vec2f |
|---|
| 172 | |
|---|
| 173 | } // end of namespace osg |
|---|
| 174 | #endif |
|---|
| 175 | |
|---|