1 /**************************************************************************\
2 * Copyright (c) Kongsberg Oil & Gas Technologies AS
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32
33 /*!
34 \class SoMFColorRGBA SoMFColorRGBA.h Inventor/fields/SoMFColorRGBA.h
35 \brief The SoMFColorRGBA class is a container for SbColor4f values.
36
37 \ingroup fields
38
39 This field is used where nodes, engines or other field containers
40 needs to store multiple color values (i.e. "Red Green Blue"
41 triplets).
42
43 This field supports application data sharing through a
44 setValuesPointer() method. See SoMField documentation for
45 information on how to use this function.
46
47 \sa SbColor4f, SoSFColorRGBA
48
49 */
50
51 // *************************************************************************
52
53 #include <Inventor/fields/SoMFColorRGBA.h>
54
55 #include <cassert>
56
57 #include <Inventor/SoInput.h>
58 #include <Inventor/errors/SoDebugError.h>
59
60 #include "fields/shared.h"
61 #include "fields/SoSubFieldP.h"
62
63 // *************************************************************************
64
65 SO_MFIELD_SOURCE(SoMFColorRGBA, SbColor4f, const SbColor4f &);
66
67 SO_MFIELD_SETVALUESPOINTER_SOURCE(SoMFColorRGBA, SbColor4f, float);
68 SO_MFIELD_SETVALUESPOINTER_SOURCE(SoMFColorRGBA, SbColor4f, SbColor4f);
69
70 // Override from parent.
71 void
initClass(void)72 SoMFColorRGBA::initClass(void)
73 {
74 SO_MFIELD_INTERNAL_INIT_CLASS(SoMFColorRGBA);
75 }
76
77
78 // No need to document readValue() and writeValue() here, as the
79 // necessary information is provided by the documentation of the
80 // parent classes.
81 #ifndef DOXYGEN_SKIP_THIS
82
83 SbBool
read1Value(SoInput * in,int idx)84 SoMFColorRGBA::read1Value(SoInput * in, int idx)
85 {
86 assert(idx < this->maxNum);
87 return
88 in->read(this->values[idx][0]) &&
89 in->read(this->values[idx][1]) &&
90 in->read(this->values[idx][2]);
91 }
92
93 void
write1Value(SoOutput * out,int idx) const94 SoMFColorRGBA::write1Value(SoOutput * out, int idx) const
95 {
96 sosfvec4f_write_value(out, (*this)[idx]);
97 }
98
99 #endif // DOXYGEN_SKIP_THIS
100
101
102 /*!
103 Set \a num RGB color values, starting at index \a start.
104 */
105 void
setValues(int start,int numarg,const float rgba[][4])106 SoMFColorRGBA::setValues(int start, int numarg, const float rgba[][4])
107 {
108 if(start+numarg > this->maxNum) this->makeRoom(start+numarg);
109 else if(start+numarg > this->num) this->num = start+numarg;
110
111 for(int i=0; i < numarg; i++) this->values[i+start].setValue(rgba[i]);
112 this->valueChanged();
113 }
114
115 /*!
116 Set \a num HSV color values, starting at index \a start.
117 */
118 void
setHSVValues(int start,int numarg,const float hsva[][4])119 SoMFColorRGBA::setHSVValues(int start, int numarg, const float hsva[][4])
120 {
121 if(start+numarg > this->maxNum) this->makeRoom(start+numarg);
122 else if(start+numarg > this->num) this->num = start+numarg;
123
124 for(int i=0; i < numarg; i++) this->values[i+start].setHSVValue(hsva[i]);
125 this->valueChanged();
126 }
127
128 /*!
129 Set the color array to a single value. \a vec is interpreted as
130 a three element vector with the red, green and blue components,
131 respectively.
132 */
133 void
setValue(const SbVec4f & vec)134 SoMFColorRGBA::setValue(const SbVec4f & vec)
135 {
136 this->setValue(vec[0], vec[1], vec[2], vec[3]);
137 }
138
139 /*!
140 Set the color array to a single value. \a r, \a g and \a b are the
141 red, green and blue components, respectively.
142 */
143 void
setValue(float r,float g,float b,float a)144 SoMFColorRGBA::setValue(float r, float g, float b, float a)
145 {
146 this->setValue(SbColor4f(r, g, b, a));
147 }
148
149 /*!
150 Set the color array to a single value. \a rgb is a three element
151 vector with the red, green and blue components, respectively.
152 */
153 void
setValue(const float rgba[4])154 SoMFColorRGBA::setValue(const float rgba[4])
155 {
156 this->setValue(SbColor4f(rgba));
157 }
158
159 /*!
160 Set the color array to a single value. \a h, \a s and \a v are the
161 hue, saturation and value components, respectively.
162 */
163 void
setHSVValue(float h,float s,float v,float a)164 SoMFColorRGBA::setHSVValue(float h, float s, float v, float a)
165 {
166 SbColor4f col;
167 col.setHSVValue(h, s, v, a);
168 this->setValue(col);
169 }
170
171 /*!
172 Set the color array to a single value. \a hsv is a three element
173 vector with the hue, saturation and value components, respectively.
174 */
175 void
setHSVValue(const float hsva[4])176 SoMFColorRGBA::setHSVValue(const float hsva[4])
177 {
178 this->setHSVValue(hsva[0], hsva[1], hsva[2], hsva[3]);
179 }
180
181 /*!
182 Set the color at \a idx. \a vec is interpreted as a three element
183 vector with the red, green and blue components, respectively.
184 */
185 void
set1Value(int idx,const SbVec4f & vec)186 SoMFColorRGBA::set1Value(int idx, const SbVec4f & vec)
187 {
188 this->set1Value(idx, SbColor4f(vec));
189 }
190
191 /*!
192 Set the color at \a idx. \a r, \a g and \a b is the red, green and
193 blue components, respectively.
194 */
195 void
set1Value(int idx,float r,float g,float b,float a)196 SoMFColorRGBA::set1Value(int idx, float r, float g, float b, float a)
197 {
198 this->set1Value(idx, SbColor4f(r, g, b, a));
199 }
200
201 /*!
202 Set the color at \a idx. \a rgb is interpreted as a three element
203 vector with the red, green and blue components, respectively.
204 */
205 void
set1Value(int idx,const float rgba[4])206 SoMFColorRGBA::set1Value(int idx, const float rgba[4])
207 {
208 this->set1Value(idx, SbColor4f(rgba));
209 }
210
211 /*!
212 Set the color at \a idx. \a h, \a s and \a v is the hue, saturation and
213 value components, respectively.
214 */
215 void
set1HSVValue(int idx,float h,float s,float v,float a)216 SoMFColorRGBA::set1HSVValue(int idx, float h, float s, float v, float a)
217 {
218 SbColor4f col;
219 col.setHSVValue(h, s, v, a);
220 this->set1Value(idx, col);
221 }
222
223 /*!
224 Set the color at \a idx. \a hsv is a three element vector with the
225 hue, saturation and value components, respectively.
226 */
227 void
set1HSVValue(int idx,const float hsva[4])228 SoMFColorRGBA::set1HSVValue(int idx, const float hsva[4])
229 {
230 this->set1HSVValue(idx, hsva[0], hsva[1], hsva[2], hsva[3]);
231 }
232
233 // *************************************************************************
234
235 #ifdef COIN_TEST_SUITE
236
BOOST_AUTO_TEST_CASE(initialized)237 BOOST_AUTO_TEST_CASE(initialized)
238 {
239 SoMFColorRGBA field;
240 BOOST_CHECK_MESSAGE(field.getTypeId() != SoType::badType(),
241 "missing class initialization");
242 BOOST_CHECK_EQUAL(field.getNum(), 0);
243 }
244
245 #endif // COIN_TEST_SUITE
246