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 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif // HAVE_CONFIG_H
36 
37 #ifdef HAVE_VRML97
38 
39 /*!
40   \class SoVRMLColorInterpolator SoVRMLColorInterpolator.h Inventor/VRMLnodes/SoVRMLColorInterpolator.h
41   \brief The SoVRMLColorInterpolator class is used to interpolate color values.
42 
43   \ingroup VRMLnodes
44 
45   \WEB3DCOPYRIGHT
46 
47   \verbatim
48   ColorInterpolator {
49     eventIn      SFFloat set_fraction        # (-inf, inf)
50     exposedField MFFloat key           []    # (-inf, inf)
51     exposedField MFColor keyValue      []    # [0,1]
52     eventOut     SFColor value_changed
53   }
54   \endverbatim
55 
56   This node interpolates among a list of MFColor key values to produce
57   an SFColor (RGB) value_changed event. The number of colours in the
58   keyValue field shall be equal to the number of keyframes in the key
59   field. The keyValue field and value_changed events are defined in
60   RGB colour space. A linear interpolation using the value of
61   set_fraction as input is performed in HSV space (see
62   http://www.web3d.org/documents/specifications/14772/V2.0/part1/bibliography.html#[FOLE]
63   for description of RGB and HSV colour spaces). The results are undefined
64   when interpolating between two consecutive keys with complementary
65   hues.
66 
67   4.6.8, Interpolator nodes
68   (<http://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.6.8>),
69   contains a detailed discussion of interpolators.
70 
71 */
72 
73 
74 /*!
75   \var SoMFColor SoVRMLColorInterpolator::keyValue
76   The color values.
77 */
78 
79 /*!
80   \var SoEngineOutput SoVRMLColorInterpolator::value_changed
81   The eventOut color.
82 */
83 
84 #include <Inventor/VRMLnodes/SoVRMLColorInterpolator.h>
85 
86 #include <Inventor/VRMLnodes/SoVRMLMacros.h>
87 
88 #include "engines/SoSubNodeEngineP.h"
89 
90 SO_NODEENGINE_SOURCE(SoVRMLColorInterpolator);
91 
92 // doc in parent
93 void
initClass(void)94 SoVRMLColorInterpolator::initClass(void) // static
95 {
96   SO_NODEENGINE_INTERNAL_INIT_CLASS(SoVRMLColorInterpolator);
97 }
98 
99 /*!
100   Constructor.
101 */
SoVRMLColorInterpolator(void)102 SoVRMLColorInterpolator::SoVRMLColorInterpolator(void)
103 {
104   SO_NODEENGINE_INTERNAL_CONSTRUCTOR(SoVRMLColorInterpolator);
105 
106   SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(keyValue);
107   SO_NODEENGINE_ADD_OUTPUT(value_changed, SoSFColor);
108 }
109 
110 /*!
111   Destructor.
112 */
~SoVRMLColorInterpolator()113 SoVRMLColorInterpolator::~SoVRMLColorInterpolator() // virtual, protected
114 {
115 }
116 
117 // doc in parent
118 void
evaluate(void)119 SoVRMLColorInterpolator::evaluate(void)
120 {
121   float interp;
122   int idx = this->getKeyValueIndex(interp, this->keyValue.getNum());
123   if (idx < 0) return;
124 
125   const SbColor * v = this->keyValue.getValues(0);
126 
127   SbColor v0 = v[idx];
128   if (interp > 0.0f) {
129     SbColor v1 = v[idx+1];
130     v0 = v0 + (v1-v0)*interp;
131   }
132   SO_ENGINE_OUTPUT(value_changed, SoSFColor, setValue(v0));
133 }
134 
135 #endif // HAVE_VRML97
136