openscenegraph
Vec2b
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_VEC2B
15#define OSG_VEC2B 1
16
17namespace osg {
18
19/** General purpose float triple.
20 * Uses include representation of color coordinates.
21 * No support yet added for float * Vec2b - is it necessary?
22 * Need to define a non-member non-friend operator* etc.
23 * Vec2b * float is okay
24*/
25class Vec2b
26{
27 public:
28
29 // Methods are defined here so that they are implicitly inlined
30
31 /** Data type of vector components.*/
32 typedef signed char value_type;
33
34 /** Number of vector components. */
35 enum { num_components = 2 };
36
37 /** Vec member variable. */
38 value_type _v[2];
39
40 /** Constructor that sets all components of the vector to zero */
41 Vec2b() { _v[0]=0; _v[1]=0; }
42
43 Vec2b(value_type r, value_type g)
44 {
45 _v[0]=r; _v[1]=g;
46 }
47
48 inline bool operator == (const Vec2b& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
49
50 inline bool operator != (const Vec2b& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
51
52 inline bool operator < (const Vec2b& v) const
53 {
54 if (_v[0]<v._v[0]) return true;
55 else if (_v[0]>v._v[0]) return false;
56 else return (_v[1]<v._v[1]);
57 }
58
59 inline value_type* ptr() { return _v; }
60 inline const value_type* ptr() const { return _v; }
61
62 inline void set( value_type x, value_type y)
63 {
64 _v[0]=x; _v[1]=y;
65 }
66
67 inline void set( const Vec2b& rhs)
68 {
69 _v[0]=rhs._v[0]; _v[1]=rhs._v[1];
70 }
71
72 inline value_type& operator [] (int i) { return _v[i]; }
73 inline value_type operator [] (int i) const { return _v[i]; }
74
75 inline value_type& x() { return _v[0]; }
76 inline value_type& y() { return _v[1]; }
77
78 inline value_type x() const { return _v[0]; }
79 inline value_type y() const { return _v[1]; }
80
81 inline value_type& r() { return _v[0]; }
82 inline value_type& g() { return _v[1]; }
83
84 inline value_type r() const { return _v[0]; }
85 inline value_type g() const { return _v[1]; }
86
87 /** Multiply by scalar. */
88 inline Vec2b operator * (float rhs) const
89 {
90 Vec2b col(*this);
91 col *= rhs;
92 return col;
93 }
94
95 /** Unary multiply by scalar. */
96 inline Vec2b& operator *= (float rhs)
97 {
98 _v[0]=(value_type)((float)_v[0]*rhs);
99 _v[1]=(value_type)((float)_v[1]*rhs);
100 return *this;
101 }
102
103 /** Divide by scalar. */
104 inline Vec2b operator / (float rhs) const
105 {
106 Vec2b col(*this);
107 col /= rhs;
108 return col;
109 }
110
111 /** Unary divide by scalar. */
112 inline Vec2b& operator /= (float rhs)
113 {
114 float div = 1.0f/rhs;
115 *this *= div;
116 return *this;
117 }
118
119 /** Binary vector add. */
120 inline Vec2b operator + (const Vec2b& rhs) const
121 {
122 return Vec2b(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
123 }
124
125 /** Unary vector add. Slightly more efficient because no temporary
126 * intermediate object.
127 */
128 inline Vec2b& operator += (const Vec2b& rhs)
129 {
130 _v[0] += rhs._v[0];
131 _v[1] += rhs._v[1];
132 return *this;
133 }
134
135 /** Binary vector subtract. */
136 inline Vec2b operator - (const Vec2b& rhs) const
137 {
138 return Vec2b(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
139 }
140
141 /** Unary vector subtract. */
142 inline Vec2b& operator -= (const Vec2b& rhs)
143 {
144 _v[0]-=rhs._v[0];
145 _v[1]-=rhs._v[1];
146 return *this;
147 }
148
149}; // end of class Vec2b
150
151
152
153} // end of namespace osg
154
155#endif