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.
19/** General purpose float triple.
20 * Uses include representation of color coordinates.
21 * No support yet added for float * Vec3ub - is it necessary?
22 * Need to define a non-member non-friend operator* etc.
23 * Vec3ub * float is okay
29 /** Data type of vector components.*/
30 typedef unsigned char value_type;
32 /** Number of vector components. */
33 enum { num_components = 3 };
37 /** Constructor that sets all components of the vector to zero */
38 Vec3ub() { _v[0]=0; _v[1]=0; _v[2]=0; }
40 Vec3ub(value_type r, value_type g, value_type b) { _v[0]=r; _v[1]=g; _v[2]=b; }
42 inline bool operator == (const Vec3ub& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; }
44 inline bool operator != (const Vec3ub& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
46 inline bool operator < (const Vec3ub& v) const
48 if (_v[0]<v._v[0]) return true;
49 else if (_v[0]>v._v[0]) return false;
50 else if (_v[1]<v._v[1]) return true;
51 else if (_v[1]>v._v[1]) return false;
52 else return (_v[2]<v._v[2]);
55 inline value_type* ptr() { return _v; }
56 inline const value_type* ptr() const { return _v; }
58 inline void set(value_type r, value_type g, value_type b)
60 _v[0]=r; _v[1]=g; _v[2]=b;
63 inline void set( const Vec3ub& rhs)
65 _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
68 inline value_type& operator [] (unsigned int i) { return _v[i]; }
69 inline value_type operator [] (unsigned int i) const { return _v[i]; }
71 inline value_type& x() { return _v[0]; }
72 inline value_type& y() { return _v[1]; }
73 inline value_type& z() { return _v[2]; }
75 inline value_type x() const { return _v[0]; }
76 inline value_type y() const { return _v[1]; }
77 inline value_type z() const { return _v[2]; }
79 inline value_type& r() { return _v[0]; }
80 inline value_type& g() { return _v[1]; }
81 inline value_type& b() { return _v[2]; }
83 inline value_type r() const { return _v[0]; }
84 inline value_type g() const { return _v[1]; }
85 inline value_type b() const { return _v[2]; }
87 /** Multiply by scalar. */
88 inline Vec3ub operator * (float rhs) const
95 /** Unary multiply by scalar. */
96 inline Vec3ub& operator *= (float rhs)
98 _v[0]=(value_type)((float)_v[0]*rhs);
99 _v[1]=(value_type)((float)_v[1]*rhs);
100 _v[2]=(value_type)((float)_v[2]*rhs);
104 /** Divide by scalar. */
105 inline Vec3ub operator / (float rhs) const
112 /** Unary divide by scalar. */
113 inline Vec3ub& operator /= (float rhs)
115 float div = 1.0f/rhs;
120 /** Binary vector add. */
121 inline Vec3ub operator + (const Vec3ub& rhs) const
123 return Vec3ub(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
127 /** Unary vector add. Slightly more efficient because no temporary
128 * intermediate object.
130 inline Vec3ub& operator += (const Vec3ub& rhs)
138 /** Binary vector subtract. */
139 inline Vec3ub operator - (const Vec3ub& rhs) const
141 return Vec3ub(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
145 /** Unary vector subtract. */
146 inline Vec3ub& operator -= (const Vec3ub& rhs)
154}; // end of class Vec3ub
156/** multiply by vector components. */
157inline Vec3ub componentMultiply(const Vec3ub& lhs, const Vec3ub& rhs)
159 return Vec3ub(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
162/** divide rhs components by rhs vector components. */
163inline Vec3ub componentDivide(const Vec3ub& lhs, const Vec3ub& rhs)
165 return Vec3ub(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
168} // end of namespace osg