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 SoMFColor SoMFColor.h Inventor/fields/SoMFColor.h
35   \brief The SoMFColor class is a container for SbColor 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 SbColor, SoSFColor
48 
49 */
50 
51 // *************************************************************************
52 
53 #include <Inventor/fields/SoMFColor.h>
54 
55 #include <cassert>
56 
57 #include <Inventor/SoInput.h>
58 #include <Inventor/errors/SoDebugError.h>
59 
60 #include "fields/SoSubFieldP.h"
61 #include "fields/shared.h"
62 
63 // *************************************************************************
64 
65 SO_MFIELD_SOURCE(SoMFColor, SbColor, const SbColor &);
66 
67 SO_MFIELD_SETVALUESPOINTER_SOURCE(SoMFColor, SbColor, float);
68 SO_MFIELD_SETVALUESPOINTER_SOURCE(SoMFColor, SbColor, SbColor);
69 
70 // Override from parent.
71 void
initClass(void)72 SoMFColor::initClass(void)
73 {
74   SO_MFIELD_INTERNAL_INIT_CLASS(SoMFColor);
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 SoMFColor::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 SoMFColor::write1Value(SoOutput * out, int idx) const
95 {
96   sosfvec3f_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 rgb[][3])106 SoMFColor::setValues(int start, int numarg, const float rgb[][3])
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(rgb[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 hsv[][3])119 SoMFColor::setHSVValues(int start, int numarg, const float hsv[][3])
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(hsv[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 SbVec3f & vec)134 SoMFColor::setValue(const SbVec3f & vec)
135 {
136   this->setValue(vec[0], vec[1], vec[2]);
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)144 SoMFColor::setValue(float r, float g, float b)
145 {
146   this->setValue(SbColor(r, g, b));
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 rgb[3])154 SoMFColor::setValue(const float rgb[3])
155 {
156   this->setValue(SbColor(rgb));
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)164 SoMFColor::setHSVValue(float h, float s, float v)
165 {
166   SbColor col;
167   col.setHSVValue(h, s, v);
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 hsv[3])176 SoMFColor::setHSVValue(const float hsv[3])
177 {
178   this->setHSVValue(hsv[0], hsv[1], hsv[2]);
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 SbVec3f & vec)186 SoMFColor::set1Value(int idx, const SbVec3f & vec)
187 {
188   this->set1Value(idx, SbColor(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)196 SoMFColor::set1Value(int idx, float r, float g, float b)
197 {
198   this->set1Value(idx, SbColor(r, g, b));
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 rgb[3])206 SoMFColor::set1Value(int idx, const float rgb[3])
207 {
208   this->set1Value(idx, SbColor(rgb));
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)216 SoMFColor::set1HSVValue(int idx, float h, float s, float v)
217 {
218   SbColor col;
219   col.setHSVValue(h, s, v);
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 hsv[3])228 SoMFColor::set1HSVValue(int idx, const float hsv[3])
229 {
230   this->set1HSVValue(idx, hsv[0], hsv[1], hsv[2]);
231 }
232 
233 // *************************************************************************
234 
235 #ifdef COIN_TEST_SUITE
236 
BOOST_AUTO_TEST_CASE(initialized)237 BOOST_AUTO_TEST_CASE(initialized)
238 {
239   SoMFColor 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