root/OpenSceneGraph/trunk/include/osg/Vec2f @ 10817

Revision 10817, 5.4 kB (checked in by robert, 5 years ago)

From Chris Hanson, " Adds support for Vec /= Vec and Vec *= Vec operators to Vec2/Vec3/Vec4 double and float
classes."

  • 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_VEC2F
15#define OSG_VEC2F 1
16
17#include <osg/Math>
18
19namespace 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
28class 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        /** Unary multiply by vector. */
95        inline Vec2f& operator *= (const Vec2f& rhs)
96        {
97            _v[0]*=rhs[0];
98            _v[1]*=rhs[1];
99            return *this;
100        }
101
102        /** Divide by scalar. */
103        inline const Vec2f operator / (value_type rhs) const
104        {
105            return Vec2f(_v[0]/rhs, _v[1]/rhs);
106        }
107
108        /** Unary divide by scalar. */
109        inline Vec2f& operator /= (value_type rhs)
110        {
111            _v[0]/=rhs;
112            _v[1]/=rhs;
113            return *this;
114        }
115
116        /** Unary divide by vector. */
117        inline Vec2f& operator /= (const Vec2f& rhs)
118        {
119            _v[0]/=rhs[0];
120            _v[1]/=rhs[1];
121            return *this;
122        }
123
124        /** Binary vector add. */
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 add. Slightly more efficient because no temporary
131          * intermediate object.
132        */
133        inline Vec2f& operator += (const Vec2f& rhs)
134        {
135            _v[0] += rhs._v[0];
136            _v[1] += rhs._v[1];
137            return *this;
138        }
139
140        /** Binary vector subtract. */
141        inline const Vec2f operator - (const Vec2f& rhs) const
142        {
143            return Vec2f(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
144        }
145
146        /** Unary vector subtract. */
147        inline Vec2f& operator -= (const Vec2f& rhs)
148        {
149            _v[0]-=rhs._v[0];
150            _v[1]-=rhs._v[1];
151            return *this;
152        }
153
154        /** Negation operator. Returns the negative of the Vec2f. */
155        inline const Vec2f operator - () const
156        {
157            return Vec2f (-_v[0], -_v[1]);
158        }
159
160        /** Length of the vector = sqrt( vec . vec ) */
161        inline value_type length() const
162        {
163            return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] );
164        }
165
166        /** Length squared of the vector = vec . vec */
167        inline value_type length2( void ) const
168        {
169            return _v[0]*_v[0] + _v[1]*_v[1];
170        }
171
172        /** Normalize the vector so that it has length unity.
173          * Returns the previous length of the vector.
174        */
175        inline value_type normalize()
176        {
177            value_type norm = Vec2f::length();
178            if (norm>0.0)
179            {
180                value_type inv = 1.0f/norm;
181                _v[0] *= inv;
182                _v[1] *= inv;
183            }
184            return( norm );
185        }
186       
187};    // end of class Vec2f
188
189}    // end of namespace osg
190#endif
191
Note: See TracBrowser for help on using the browser.