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 unsigned byte pair.
25 /** Data type of vector components.*/
26 typedef unsigned char value_type;
28 /** Number of vector components. */
29 enum { num_components = 2 };
31 /** Vec member variable. */
34 /** Constructor that sets all components of the vector to zero */
35 Vec2ub() { _v[0]=0; _v[1]=0; }
37 Vec2ub(value_type r, value_type g) { _v[0]=r; _v[1]=g; }
39 inline bool operator == (const Vec2ub& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
40 inline bool operator != (const Vec2ub& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
41 inline bool operator < (const Vec2ub& v) const
43 if (_v[0]<v._v[0]) return true;
44 else if (_v[0]>v._v[0]) return false;
45 else return (_v[1]<v._v[1]);
48 inline value_type* ptr() { return _v; }
49 inline const value_type* ptr() const { return _v; }
51 inline void set( value_type x, value_type y)
56 inline void set( const Vec2ub& rhs)
58 _v[0]=rhs._v[0]; _v[1]=rhs._v[1];
61 inline value_type& operator [] (int i) { return _v[i]; }
62 inline value_type operator [] (int i) const { return _v[i]; }
64 inline value_type& x() { return _v[0]; }
65 inline value_type& y() { return _v[1]; }
67 inline value_type x() const { return _v[0]; }
68 inline value_type y() const { return _v[1]; }
70 inline value_type& r() { return _v[0]; }
71 inline value_type& g() { return _v[1]; }
73 inline value_type r() const { return _v[0]; }
74 inline value_type g() const { return _v[1]; }
76 /** Multiply by scalar. */
77 inline Vec2ub operator * (float rhs) const
84 /** Unary multiply by scalar. */
85 inline Vec2ub& operator *= (float rhs)
87 _v[0]=(value_type)((float)_v[0]*rhs);
88 _v[1]=(value_type)((float)_v[1]*rhs);
92 /** Divide by scalar. */
93 inline Vec2ub operator / (float rhs) const
100 /** Unary divide by scalar. */
101 inline Vec2ub& operator /= (float rhs)
103 float div = 1.0f/rhs;
108 /** Binary vector add. */
109 inline Vec2ub operator + (const Vec2ub& rhs) const
111 return Vec2ub(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
114 /** Unary vector add. Slightly more efficient because no temporary
115 * intermediate object.
117 inline Vec2ub& operator += (const Vec2ub& rhs)
124 /** Binary vector subtract. */
125 inline Vec2ub operator - (const Vec2ub& rhs) const
127 return Vec2ub(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
130 /** Unary vector subtract. */
131 inline Vec2ub& operator -= (const Vec2ub& rhs)
138}; // end of class Vec2ub
140/** multiply by vector components. */
141inline Vec2ub componentMultiply(const Vec2ub& lhs, const Vec2ub& rhs)
143 return Vec2ub(lhs[0]*rhs[0], lhs[1]*rhs[1]);
146/** divide rhs components by rhs vector components. */
147inline Vec2ub componentDivide(const Vec2ub& lhs, const Vec2ub& rhs)
149 return Vec2ub(lhs[0]/rhs[0], lhs[1]/rhs[1]);
152} // end of namespace osg