openscenegraph
Vec2ub
Go to the documentation of this file.
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_VEC2UB
15#define OSG_VEC2UB 1
16
17namespace osg {
18
19/** General purpose unsigned byte pair.
20*/
21class Vec2ub
22{
23 public:
24
25 /** Data type of vector components.*/
26 typedef unsigned char value_type;
27
28 /** Number of vector components. */
29 enum { num_components = 2 };
30
31 /** Vec member variable. */
32 value_type _v[2];
33
34 /** Constructor that sets all components of the vector to zero */
35 Vec2ub() { _v[0]=0; _v[1]=0; }
36
37 Vec2ub(value_type r, value_type g) { _v[0]=r; _v[1]=g; }
38
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
42 {
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]);
46 }
47
48 inline value_type* ptr() { return _v; }
49 inline const value_type* ptr() const { return _v; }
50
51 inline void set( value_type x, value_type y)
52 {
53 _v[0]=x; _v[1]=y;
54 }
55
56 inline void set( const Vec2ub& rhs)
57 {
58 _v[0]=rhs._v[0]; _v[1]=rhs._v[1];
59 }
60
61 inline value_type& operator [] (int i) { return _v[i]; }
62 inline value_type operator [] (int i) const { return _v[i]; }
63
64 inline value_type& x() { return _v[0]; }
65 inline value_type& y() { return _v[1]; }
66
67 inline value_type x() const { return _v[0]; }
68 inline value_type y() const { return _v[1]; }
69
70 inline value_type& r() { return _v[0]; }
71 inline value_type& g() { return _v[1]; }
72
73 inline value_type r() const { return _v[0]; }
74 inline value_type g() const { return _v[1]; }
75
76 /** Multiply by scalar. */
77 inline Vec2ub operator * (float rhs) const
78 {
79 Vec2ub col(*this);
80 col *= rhs;
81 return col;
82 }
83
84 /** Unary multiply by scalar. */
85 inline Vec2ub& operator *= (float rhs)
86 {
87 _v[0]=(value_type)((float)_v[0]*rhs);
88 _v[1]=(value_type)((float)_v[1]*rhs);
89 return *this;
90 }
91
92 /** Divide by scalar. */
93 inline Vec2ub operator / (float rhs) const
94 {
95 Vec2ub col(*this);
96 col /= rhs;
97 return col;
98 }
99
100 /** Unary divide by scalar. */
101 inline Vec2ub& operator /= (float rhs)
102 {
103 float div = 1.0f/rhs;
104 *this *= div;
105 return *this;
106 }
107
108 /** Binary vector add. */
109 inline Vec2ub operator + (const Vec2ub& rhs) const
110 {
111 return Vec2ub(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
112 }
113
114 /** Unary vector add. Slightly more efficient because no temporary
115 * intermediate object.
116 */
117 inline Vec2ub& operator += (const Vec2ub& rhs)
118 {
119 _v[0] += rhs._v[0];
120 _v[1] += rhs._v[1];
121 return *this;
122 }
123
124 /** Binary vector subtract. */
125 inline Vec2ub operator - (const Vec2ub& rhs) const
126 {
127 return Vec2ub(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
128 }
129
130 /** Unary vector subtract. */
131 inline Vec2ub& operator -= (const Vec2ub& rhs)
132 {
133 _v[0]-=rhs._v[0];
134 _v[1]-=rhs._v[1];
135 return *this;
136 }
137
138}; // end of class Vec2ub
139
140/** multiply by vector components. */
141inline Vec2ub componentMultiply(const Vec2ub& lhs, const Vec2ub& rhs)
142{
143 return Vec2ub(lhs[0]*rhs[0], lhs[1]*rhs[1]);
144}
145
146/** divide rhs components by rhs vector components. */
147inline Vec2ub componentDivide(const Vec2ub& lhs, const Vec2ub& rhs)
148{
149 return Vec2ub(lhs[0]/rhs[0], lhs[1]/rhs[1]);
150}
151
152} // end of namespace osg
153
154#endif