Debian Codegraph @

lib/database/src/so/nodes/SoLinearProfile.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision $
44 |
45 | Classes:
46 | SoLinearProfile
47 |
48 | Author(s) : Thad Beier, Dave Immel
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/elements/SoProfileCoordinateElement.h>
55 #include <Inventor/nodes/SoLinearProfile.h>
56
57 SO_NODE_SOURCE(SoLinearProfile);
58
59 ////////////////////////////////////////////////////////////////////////
60 //
61 // Description:
62 // Constructor
63 //
64 // Use: public
65
66 SoLinearProfile::SoLinearProfile()
67 //
68 ////////////////////////////////////////////////////////////////////////
69 {
70 SO_NODE_CONSTRUCTOR(SoLinearProfile);
71 isBuiltIn = TRUE;
72 }
73
74 ////////////////////////////////////////////////////////////////////////
75 //
76 // Description:
77 // Destructor (necessary since inline destructor is too complex)
78 //
79 // Use: private
80
81 SoLinearProfile::~SoLinearProfile()
82 //
83 ////////////////////////////////////////////////////////////////////////
84 {
85 }
86
87 ////////////////////////////////////////////////////////////////////////
88 //
89 // Description:
90 // Static helper routine that figures out how many points will
91 // really be used, taking into account USE_REST_OF_VERTICES
92 //
93 // Use: internal
94
95 static int
96 getNumPoints(const SoMFInt32 &index, int numCoords)
97 //
98 ////////////////////////////////////////////////////////////////////////
99 {
100 int numIndices = index.getNum();
101 if (index[numIndices-1] != SO_LINEAR_PROFILE_USE_REST_OF_VERTICES)
102 return numIndices;
103
104 int lastIndex = (numIndices == 1 ? -1 : index[numIndices-2]+1);
105
106 // Return regular indices plus number of coordinates left:
107 return (numIndices-1) + (numCoords-1 - lastIndex);
108 }
109
110 ////////////////////////////////////////////////////////////////////////
111 //
112 // Description:
113 // Static helper routine that returns the correct index,
114 // taking into account USE_REST_OF_VERTICES
115 //
116 // Use: internal
117
118 static int
119 getIndex(int i, const SoMFInt32 &index, int numCoords)
120 //
121 ////////////////////////////////////////////////////////////////////////
122 {
123 int numIndices = index.getNum();
124 int result;
125
126 if (index[numIndices-1] != SO_LINEAR_PROFILE_USE_REST_OF_VERTICES) {
127 result = index[i];
128 } else {
129 if (i < numIndices-1) result = i;
130 else {
131 int lastIndex = (numIndices == 1 ? -1 : index[numIndices-2]+1);
132
133 result = lastIndex+1 + i-(numIndices-1);
134 }
135 }
136
137 #ifdef DEBUG
138 if (result < 0 || result > numCoords) {
139 SoDebugError::post("SoLinearProfile", "index[%d]=%d out of "
140 "range (0,%d)", i, result, 0, numCoords);
141 }
142 #endif
143 return result;
144 }
145
146 ////////////////////////////////////////////////////////////////////////
147 //
148 // Description:
149 // Returns a list of points used in making a piecewise linear trim
150 // curve. numKnots and knotVector will be returned as 0 and NULL.
151 //
152 // Use: extender
153
154 void
155 SoLinearProfile::getTrimCurve(SoState *state, int32_t &numPoints, float *&points,
156 int &floatsPerVec,
157 int32_t &numKnots, float *&knotVector)
158 //
159 ////////////////////////////////////////////////////////////////////////
160 {
161 const SoProfileCoordinateElement *pce;
162 int i;
163
164 pce = SoProfileCoordinateElement::getInstance(state);
165
166 numPoints = getNumPoints(index, pce->getNum());
167
168 if (pce->is2D()) {
169 floatsPerVec = 2;
170 points = new float[numPoints * 2];
171
172 for (i = 0; i < numPoints; i++) {
173 const SbVec2f &t = pce->get2(getIndex(i, index, pce->getNum()));
174 points[i*2] = t[0];
175 points[i*2+1] = t[1];
176 }
177 }
178 else {
179 floatsPerVec = 3;
180 points = new float[numPoints * 3];
181
182 for (i = 0; i < numPoints; i++) {
183 const SbVec3f &t = pce->get3(getIndex(i, index, pce->getNum()));
184 points[i*3] = t[0];
185 points[i*3+1] = t[1];
186 points[i*3+2] = t[2];
187 }
188 }
189 numKnots = 0;
190 knotVector = NULL;
191 }
192
193 ////////////////////////////////////////////////////////////////////////
194 //
195 // Description:
196 // Returns the vertices of the profile. These will just be the
197 // profile coordinates. If the coordinates are rational, divide
198 // out the third component. Space will be allocated to store the
199 // vertices. The calling routine is responsible for freeing this
200 // space.
201 //
202 // Use: extender
203
204 void
205 SoLinearProfile::getVertices(SoState *state,
206 int32_t &nVertices, SbVec2f *&vertices)
207 //
208 ////////////////////////////////////////////////////////////////////////
209 {
210 const SoProfileCoordinateElement *pce;
211 int i;
212
213 pce = SoProfileCoordinateElement::getInstance(state);
214
215 nVertices = getNumPoints(index, pce->getNum());
216 if (nVertices > 0) {
217 vertices = new SbVec2f[nVertices];
218
219 for (i = 0; i < nVertices; i++)
220 vertices[i] = pce->get2(getIndex(i, index, pce->getNum()));
221 } else {
222 vertices = NULL;
223 }
224 }