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