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