1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
21/** General purpose double pair, uses include representation of
22 * texture coordinates.
23 * No support yet added for double * Vec2d - is it necessary?
24 * Need to define a non-member non-friend operator* etc.
25 * BTW: Vec2d * double is okay
32 /** Data type of vector components.*/
33 typedef double value_type;
35 /** Number of vector components. */
36 enum { num_components = 2 };
40 /** Constructor that sets all components of the vector to zero */
41 Vec2d() {_v[0]=0.0; _v[1]=0.0;}
43 Vec2d(value_type x,value_type y) { _v[0]=x; _v[1]=y; }
45 inline Vec2d(const Vec2f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; }
47 inline operator Vec2f() const { return Vec2f(static_cast<float>(_v[0]),static_cast<float>(_v[1]));}
50 inline bool operator == (const Vec2d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
52 inline bool operator != (const Vec2d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
54 inline bool operator < (const Vec2d& v) const
56 if (_v[0]<v._v[0]) return true;
57 else if (_v[0]>v._v[0]) return false;
58 else return (_v[1]<v._v[1]);
61 inline value_type* ptr() { return _v; }
62 inline const value_type* ptr() const { return _v; }
64 inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; }
66 inline value_type& operator [] (int i) { return _v[i]; }
67 inline value_type operator [] (int i) const { return _v[i]; }
69 inline value_type& x() { return _v[0]; }
70 inline value_type& y() { return _v[1]; }
72 inline value_type x() const { return _v[0]; }
73 inline value_type y() const { return _v[1]; }
75 /** Returns true if all components have values that are not NaN. */
76 inline bool valid() const { return !isNaN(); }
77 /** Returns true if at least one component has value NaN. */
78 inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); }
81 inline value_type operator * (const Vec2d& rhs) const
83 return _v[0]*rhs._v[0]+_v[1]*rhs._v[1];
86 /** Multiply by scalar. */
87 inline const Vec2d operator * (value_type rhs) const
89 return Vec2d(_v[0]*rhs, _v[1]*rhs);
92 /** Unary multiply by scalar. */
93 inline Vec2d& operator *= (value_type rhs)
100 /** Divide by scalar. */
101 inline const Vec2d operator / (value_type rhs) const
103 return Vec2d(_v[0]/rhs, _v[1]/rhs);
106 /** Unary divide by scalar. */
107 inline Vec2d& operator /= (value_type rhs)
114 /** Binary vector add. */
115 inline const Vec2d operator + (const Vec2d& rhs) const
117 return Vec2d(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
120 /** Unary vector add. Slightly more efficient because no temporary
121 * intermediate object.
123 inline Vec2d& operator += (const Vec2d& rhs)
130 /** Binary vector subtract. */
131 inline const Vec2d operator - (const Vec2d& rhs) const
133 return Vec2d(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
136 /** Unary vector subtract. */
137 inline Vec2d& operator -= (const Vec2d& rhs)
144 /** Negation operator. Returns the negative of the Vec2d. */
145 inline const Vec2d operator - () const
147 return Vec2d (-_v[0], -_v[1]);
150 /** Length of the vector = sqrt( vec . vec ) */
151 inline value_type length() const
153 return sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
156 /** Length squared of the vector = vec . vec */
157 inline value_type length2( void ) const
159 return _v[0]*_v[0] + _v[1]*_v[1];
162 /** Normalize the vector so that it has length unity.
163 * Returns the previous length of the vector.
165 inline value_type normalize()
167 value_type norm = Vec2d::length();
170 value_type inv = 1.0/norm;
177}; // end of class Vec2d
180/** multiply by vector components. */
181inline Vec2d componentMultiply(const Vec2d& lhs, const Vec2d& rhs)
183 return Vec2d(lhs[0]*rhs[0], lhs[1]*rhs[1]);
186/** divide rhs components by rhs vector components. */
187inline Vec2d componentDivide(const Vec2d& lhs, const Vec2d& rhs)
189 return Vec2d(lhs[0]/rhs[0], lhs[1]/rhs[1]);
192} // end of namespace osg