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 SoClipPlaneElement Inventor/elements/SoClipPlaneElement.h
35 \brief The SoClipPlaneElement class is used to manage the clip plane stack.
36
37 \ingroup elements
38 */
39
40 #include <Inventor/elements/SoClipPlaneElement.h>
41 #include <Inventor/elements/SoModelMatrixElement.h>
42 #include <Inventor/nodes/SoNode.h>
43
44 #include "SbBasicP.h"
45
46 //
47 // constructor for the internal class
48 //
so_plane_data(const SbPlane & planeref,const SbMatrix & matrix)49 SoClipPlaneElement::so_plane_data::so_plane_data(const SbPlane &planeref, const SbMatrix &matrix)
50 {
51 this->plane = this->wcPlane = planeref;
52 this->wcPlane.transform(matrix);
53 }
54
55 /*!
56 \fn SoClipPlaneElement::planes
57 List of currently active planes.
58 */
59
60 /*!
61 \fn SoClipPlaneElement::startIndex
62 Index of first clip plane in this element. Used to disable clip planes
63 in SoGLClipPlaneElement::pop().
64 */
65
66 SO_ELEMENT_SOURCE(SoClipPlaneElement);
67
68 // doc from parent
69 void
initClass(void)70 SoClipPlaneElement::initClass(void)
71 {
72 SO_ELEMENT_INIT_CLASS(SoClipPlaneElement, inherited);
73 }
74
75 /*!
76 The destructor.
77 */
~SoClipPlaneElement()78 SoClipPlaneElement::~SoClipPlaneElement()
79 {
80 }
81
82 /*!
83 Adds \a plane as an active plane. Calls addToElt() to do the job.
84 */
85 void
add(SoState * const state,SoNode * const node,const SbPlane & plane)86 SoClipPlaneElement::add(SoState * const state,
87 SoNode * const node,
88 const SbPlane & plane)
89 {
90 SoClipPlaneElement * element =
91 coin_safe_cast<SoClipPlaneElement * >
92 (
93 SoElement::getElement(state, classStackIndex)
94 );
95
96 if (element) {
97 element->addToElt(plane, SoModelMatrixElement::get(state));
98 if (node) element->addNodeId(node);
99 }
100 }
101
102 /*!
103 Returns the current (top-of-stack) element.
104 */
105 const SoClipPlaneElement *
getInstance(SoState * const state)106 SoClipPlaneElement::getInstance(SoState * const state)
107 {
108 return coin_assert_cast<const SoClipPlaneElement *>
109 (
110 SoElement::getConstElement(state, classStackIndex)
111 );
112 }
113
114 /*!
115 Returns the current number of active clipping planes.
116 */
117 int
getNum() const118 SoClipPlaneElement::getNum() const
119 {
120 return this->planes.getLength();
121 }
122
123 /*!
124 Returns the \a index'th plane.
125 */
126 const SbPlane &
get(const int index,const SbBool inworldspace) const127 SoClipPlaneElement::get(const int index,
128 const SbBool inworldspace) const
129 {
130 assert(index >= 0 && index < this->planes.getLength());
131 if (inworldspace) return this->planes.getArrayPtr()[index].wcPlane;
132 return this->planes.getArrayPtr()[index].plane;
133 }
134
135 /*!
136 This method adds the clipping plane, \a plane, to an instance.
137 \a modelmatrix is the current model matrix.
138 */
139 void
addToElt(const SbPlane & plane,const SbMatrix & modelMatrix)140 SoClipPlaneElement::addToElt(const SbPlane &plane,
141 const SbMatrix &modelMatrix)
142 {
143 SoClipPlaneElement::so_plane_data data(plane, modelMatrix);
144 this->planes.append(data);
145 }
146
147 // doc from parent
148 void
init(SoState * state)149 SoClipPlaneElement::init(SoState * state)
150 {
151 inherited::init(state);
152 this->planes.truncate(0);
153 this->startIndex = 0;
154 }
155
156 // Documented in superclass. Overridden to copy planes into the new
157 // top of stack, since planes are accumulated. Also copies accumulated
158 // node ids.
159 void
push(SoState * state)160 SoClipPlaneElement::push(SoState * state)
161 {
162 inherited::push(state);
163
164 SoClipPlaneElement * const prev =
165 coin_assert_cast<SoClipPlaneElement *>(this->getNextInStack());
166
167 this->planes.truncate(0);
168 for (int i = 0; i < prev->planes.getLength(); i++) {
169 this->planes.append(prev->planes[i]);
170 }
171 this->startIndex = prev->planes.getLength();
172 this->copyNodeIds(prev);
173 }
174