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 /*! \file SbClip.h */
34 #include <Inventor/SbClip.h>
35 #include <Inventor/SbVec2f.h>
36 #include <Inventor/SbPlane.h>
37 #include <cstdio>
38 #include <cassert>
39
40 /*!
41 \class SbClip SbClip.h include/Inventor/base/SbClip.h
42 \brief The SbClip class is a generic polygon clipper class.
43
44 \ingroup base
45
46 It is used by first adding all vertices in the polygon, and then
47 clipping against any number of planes. If you need to supply
48 additional information per vertex (e.g. texture coordinates), you
49 should supply a callback in the constructor, and a pointer to your
50 vertex structure in addVertex(). For every new vertex created, the
51 callback is called with the line being clipped, including the
52 pointers to your vertex structures and the position of the new
53 (clipped against some plane) vertex. You should then create a new
54 vertex structure, calculate your data (e.g. a new texture
55 coordinate) and return a pointer to this structure.
56
57 \COIN_CLASS_EXTENSION
58
59 \since Coin 2.0
60 */
61 // FIXME: a small usage example would do wonders for the class
62 // doc. 20020120 mortene.
63
64 /*!
65 \typedef void * SbClipCallback(const SbVec3f & v0, void * vdata0,
66 const SbVec3f & v1, void * vdata1,
67 const SbVec3f & newvertex,
68 void * userdata)
69
70 The type definition for the callback function when additional data handling per vertex
71 is required.
72 */
73
74 /*!
75 A constructor. Supply a callback if you need to handle additional
76 data per vertex.
77 */
SbClip(SbClipCallback * callback,void * userdata)78 SbClip::SbClip(SbClipCallback * callback, void * userdata)
79 : callback(callback),
80 cbdata(userdata),
81 curr(0)
82 {
83 }
84
85 /*!
86 Adds a polygon vertex. \a vdata could be a pointer to your
87 vertex structure.
88 */
89 void
addVertex(const SbVec3f & v,void * vdata)90 SbClip::addVertex(const SbVec3f &v, void * vdata)
91 {
92 this->array[this->curr].append(SbClipData(v, vdata));
93 }
94
95 /*!
96 Resets the clipper. This should be called before adding any vertices
97 when reusing an SbClip instance.
98 */
99 void
reset(void)100 SbClip::reset(void)
101 {
102 this->array[0].truncate(0);
103 this->array[1].truncate(0);
104 }
105
106 //
107 // private method, inline only inside this class
108 //
109 inline void
outputVertex(const SbVec3f & v,void * data)110 SbClip::outputVertex(const SbVec3f & v, void * data)
111 {
112 this->array[this->curr ^ 1].append(SbClipData(v, data));
113 }
114
115 /*!
116 Clip polygon against \a plane. This might change the number of
117 vertices in the polygon. For each time a new vertex is created, the
118 callback supplied in the constructor (if != NULL) is called with the
119 line being clipped and the new vertex calculated. The callback
120 should return a new void pointer to be stored by the clipper.
121 */
122 void
clip(const SbPlane & plane)123 SbClip::clip(const SbPlane & plane)
124 {
125 int n = this->array[this->curr].getLength();
126
127 if (n == 0) return;
128
129 // create loop
130 SbClipData dummy = this->array[this->curr][0];
131 this->array[this->curr].append(dummy);
132
133 const SbVec3f & planeN = plane.getNormal();
134
135 for (int i = 0; i < n; i++) {
136 SbVec3f v0, v1;
137 void * data0, *data1;
138 this->array[this->curr][i].get(v0, data0);
139 this->array[this->curr][i+1].get(v1, data1);
140
141 float d0 = plane.getDistance(v0);
142 float d1 = plane.getDistance(v1);
143
144 if (d0 >= 0.0f && d1 < 0.0f) { // exit plane
145 SbVec3f dir = v1-v0;
146 // we know that v0 != v1 since we got here
147 (void) dir.normalize();
148 float dot = dir.dot(planeN);
149 SbVec3f newvertex = v0 - dir * (d0/dot);
150 void * newdata = NULL;
151 if (this->callback) {
152 newdata = this->callback(v0, data0, v1, data1, newvertex, this->cbdata);
153 }
154 outputVertex(newvertex, newdata);
155 }
156 else if (d0 < 0.0f && d1 >= 0.0f) { // enter plane
157 SbVec3f dir = v1-v0;
158 // we know that v0 != v1 since we got here
159 (void) dir.normalize();
160 float dot = dir.dot(planeN);
161 SbVec3f newvertex = v0 - dir * (d0/dot);
162 void * newdata = NULL;
163 if (this->callback) {
164 newdata = this->callback(v0, data0, v1, data1, newvertex, this->cbdata);
165 }
166 outputVertex(newvertex, newdata);
167 outputVertex(v1, data1);
168 }
169 else if (d0 >= 0.0f && d1 >= 0.0f) { // in plane
170 outputVertex(v1, data1);
171 }
172 }
173 this->array[this->curr].truncate(0);
174 this->curr ^= 1;
175 }
176
177 /*!
178 Returns the number of vertices in the polygon.
179 \sa SbClip::getVertex()
180 */
181 int
getNumVertices(void) const182 SbClip::getNumVertices(void) const
183 {
184 return this->array[this->curr].getLength();
185 }
186
187 /*!
188 Returns the vertex at index \a idx.
189 \sa SbClip::getNumVertices()
190 */
191 void
getVertex(const int idx,SbVec3f & v,void ** vdata) const192 SbClip::getVertex(const int idx, SbVec3f & v, void ** vdata) const
193 {
194 v = this->array[this->curr][idx].vertex;
195 if (vdata) *vdata = this->array[this->curr][idx].data;
196 }
197
198 /*!
199 Return the vertex data at index \a idx.
200 */
201 void *
getVertexData(const int idx) const202 SbClip::getVertexData(const int idx) const
203 {
204 return this->array[this->curr][idx].data;
205 }
206