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.
23 /** Data type of vector components.*/
24 typedef short value_type;
26 /** Number of vector components. */
27 enum { num_components = 3 };
31 /** Constructor that sets all components of the vector to zero */
32 Vec3s() { _v[0]=0; _v[1]=0; _v[2]=0; }
34 Vec3s(value_type r, value_type g, value_type b) { _v[0]=r; _v[1]=g; _v[2]=b; }
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
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]);
47 inline value_type* ptr() { return _v; }
48 inline const value_type* ptr() const { return _v; }
50 inline void set(value_type r, value_type g, value_type b)
52 _v[0]=r; _v[1]=g; _v[2]=b;
55 inline void set( const Vec3s& rhs)
57 _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
60 inline value_type& operator [] (unsigned int i) { return _v[i]; }
61 inline value_type operator [] (unsigned int i) const { return _v[i]; }
63 inline value_type& x() { return _v[0]; }
64 inline value_type& y() { return _v[1]; }
65 inline value_type& z() { return _v[2]; }
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]; }
71 inline value_type& r() { return _v[0]; }
72 inline value_type& g() { return _v[1]; }
73 inline value_type& b() { return _v[2]; }
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]; }
79 /** Multiply by scalar. */
80 inline Vec3s operator * (value_type rhs) const
82 return Vec3s(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
85 /** Unary multiply by scalar. */
86 inline Vec3s& operator *= (value_type rhs)
94 /** Divide by scalar. */
95 inline Vec3s operator / (value_type rhs) const
97 return Vec3s(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
100 /** Unary divide by scalar. */
101 inline Vec3s& operator /= (value_type rhs)
109 /** Binary vector multiply. */
110 inline Vec3s operator * (const Vec3s& rhs) const
112 return Vec3s(_v[0]*rhs._v[0], _v[1]*rhs._v[1], _v[2]*rhs._v[2]);
115 /** Binary vector add. */
116 inline Vec3s operator + (const Vec3s& rhs) const
118 return Vec3s(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
121 /** Unary vector add. Slightly more efficient because no temporary
122 * intermediate object.
124 inline Vec3s& operator += (const Vec3s& rhs)
132 /** Binary vector subtract. */
133 inline Vec3s operator - (const Vec3s& rhs) const
135 return Vec3s(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
138 /** Unary vector subtract. */
139 inline Vec3s& operator -= (const Vec3s& rhs)
147 /** Negation operator. Returns the negative of the Vec3s. */
148 inline Vec3s operator - () const
150 return Vec3s (-_v[0], -_v[1], -_v[2]);
153}; // end of class Vec3s
156/** multiply by vector components. */
157inline Vec3s componentMultiply(const Vec3s& lhs, const Vec3s& rhs)
159 return Vec3s(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
162/** divide rhs components by rhs vector components. */
163inline Vec3s componentDivide(const Vec3s& lhs, const Vec3s& rhs)
165 return Vec3s(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
168} // end of namespace osg