root/OpenSceneGraph/trunk/include/osg/Vec2d @ 13041

Revision 13041, 5.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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_VEC2D
15#define OSG_VEC2D 1
16
17#include <osg/Vec2f>
18
19namespace osg {
20
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
26*/
27
28class Vec2d
29{
30    public:
31
32        /** Data type of vector components.*/
33        typedef double value_type;
34
35        /** Number of vector components. */
36        enum { num_components = 2 };
37
38        value_type _v[2];
39
40        /** Constructor that sets all components of the vector to zero */
41        Vec2d() {_v[0]=0.0; _v[1]=0.0;}
42
43        Vec2d(value_type x,value_type y) { _v[0]=x; _v[1]=y; }
44
45        inline Vec2d(const Vec2f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; }
46
47        inline operator Vec2f() const { return Vec2f(static_cast<float>(_v[0]),static_cast<float>(_v[1]));}
48
49
50        inline bool operator == (const Vec2d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
51
52        inline bool operator != (const Vec2d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
53
54        inline bool operator <  (const Vec2d& v) const
55        {
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]);
59        }
60
61        inline value_type* ptr() { return _v; }
62        inline const value_type* ptr() const { return _v; }
63
64        inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; }
65
66        inline value_type& operator [] (int i) { return _v[i]; }
67        inline value_type operator [] (int i) const { return _v[i]; }
68
69        inline value_type& x() { return _v[0]; }
70        inline value_type& y() { return _v[1]; }
71
72        inline value_type x() const { return _v[0]; }
73        inline value_type y() const { return _v[1]; }
74
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]); }
79
80        /** Dot product. */
81        inline value_type operator * (const Vec2d& rhs) const
82        {
83            return _v[0]*rhs._v[0]+_v[1]*rhs._v[1];
84        }
85
86        /** Multiply by scalar. */
87        inline const Vec2d operator * (value_type rhs) const
88        {
89            return Vec2d(_v[0]*rhs, _v[1]*rhs);
90        }
91
92        /** Unary multiply by scalar. */
93        inline Vec2d& operator *= (value_type rhs)
94        {
95            _v[0]*=rhs;
96            _v[1]*=rhs;
97            return *this;
98        }
99
100        /** Divide by scalar. */
101        inline const Vec2d operator / (value_type rhs) const
102        {
103            return Vec2d(_v[0]/rhs, _v[1]/rhs);
104        }
105
106        /** Unary divide by scalar. */
107        inline Vec2d& operator /= (value_type rhs)
108        {
109            _v[0]/=rhs;
110            _v[1]/=rhs;
111            return *this;
112        }
113
114        /** Binary vector add. */
115        inline const Vec2d operator + (const Vec2d& rhs) const
116        {
117            return Vec2d(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
118        }
119
120        /** Unary vector add. Slightly more efficient because no temporary
121          * intermediate object.
122        */
123        inline Vec2d& operator += (const Vec2d& rhs)
124        {
125            _v[0] += rhs._v[0];
126            _v[1] += rhs._v[1];
127            return *this;
128        }
129
130        /** Binary vector subtract. */
131        inline const Vec2d operator - (const Vec2d& rhs) const
132        {
133            return Vec2d(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
134        }
135
136        /** Unary vector subtract. */
137        inline Vec2d& operator -= (const Vec2d& rhs)
138        {
139            _v[0]-=rhs._v[0];
140            _v[1]-=rhs._v[1];
141            return *this;
142        }
143
144        /** Negation operator. Returns the negative of the Vec2d. */
145        inline const Vec2d operator - () const
146        {
147            return Vec2d (-_v[0], -_v[1]);
148        }
149
150        /** Length of the vector = sqrt( vec . vec ) */
151        inline value_type length() const
152        {
153            return sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
154        }
155
156        /** Length squared of the vector = vec . vec */
157        inline value_type length2( void ) const
158        {
159            return _v[0]*_v[0] + _v[1]*_v[1];
160        }
161
162        /** Normalize the vector so that it has length unity.
163          * Returns the previous length of the vector.
164        */
165        inline value_type normalize()
166        {
167            value_type norm = Vec2d::length();
168            if (norm>0.0)
169            {
170                value_type inv = 1.0/norm;
171                _v[0] *= inv;
172                _v[1] *= inv;
173            }
174            return( norm );
175        }
176
177};    // end of class Vec2d
178
179
180/** multiply by vector components. */
181inline Vec2d componentMultiply(const Vec2d& lhs, const Vec2d& rhs)
182{
183    return Vec2d(lhs[0]*rhs[0], lhs[1]*rhs[1]);
184}
185
186/** divide rhs components by rhs vector components. */
187inline Vec2d componentDivide(const Vec2d& lhs, const Vec2d& rhs)
188{
189    return Vec2d(lhs[0]/rhs[0], lhs[1]/rhs[1]);
190}
191
192}    // end of namespace osg
193#endif
Note: See TracBrowser for help on using the browser.