Debian Codegraph @

lib/database/src/so/elements/SoCoordinateElement.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 | SoCoordinateElement
47 |
48 | Author(s) : Paul S. Strauss
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/SoCoordinateElement.h>
55 #include <Inventor/errors/SoDebugError.h>
56
57
58 SO_ELEMENT_SOURCE(SoCoordinateElement);
59
60 SbVec3f *SoCoordinateElement::defaultCoord3 = NULL;
61
62 ////////////////////////////////////////////////////////////////////////
63 //
64 // Description:
65 // Destructor.
66 //
67 // Use: private
68
69 SoCoordinateElement::~SoCoordinateElement()
70 //
71 ////////////////////////////////////////////////////////////////////////
72 {
73 }
74
75 ////////////////////////////////////////////////////////////////////////
76 //
77 // Description:
78 // Initializes element.
79 //
80 // Use: public
81
82 void
83 SoCoordinateElement::init(SoState *state)
84 //
85 ////////////////////////////////////////////////////////////////////////
86 {
87 SoReplacedElement::init(state);
88
89 // Initialize default coordinate storage if not already done
90 if (defaultCoord3 == NULL) {
91 defaultCoord3 = new SbVec3f;
92 *defaultCoord3 = getDefault3();
93 }
94
95 // Assume 3D until told otherwise
96 coordsAre3D = TRUE;
97 coords3 = defaultCoord3;
98 numCoords = 1;
99 }
100
101 ////////////////////////////////////////////////////////////////////////
102 //
103 // Description:
104 // Sets the 3-D coordinates in element accessed from state.
105 //
106 // Use: public
107
108 void
109 SoCoordinateElement::set3(SoState *state, SoNode *node,
110 int32_t numCoords, const SbVec3f *coords)
111 //
112 ////////////////////////////////////////////////////////////////////////
113 {
114 SoCoordinateElement *elt;
115
116 // Get an instance we can change (pushing if necessary)
117 elt = (SoCoordinateElement *) getElement(state, classStackIndex, node);
118
119 if (elt != NULL) {
120 elt->numCoords = numCoords;
121 elt->coords3 = coords;
122 elt->coordsAre3D = TRUE;
123 }
124 }
125
126 ////////////////////////////////////////////////////////////////////////
127 //
128 // Description:
129 // Sets the 4-D coordinates in element accessed from state.
130 //
131 // Use: public
132
133 void
134 SoCoordinateElement::set4(SoState *state, SoNode *node,
135 int32_t numCoords, const SbVec4f *coords)
136 //
137 ////////////////////////////////////////////////////////////////////////
138 {
139 SoCoordinateElement *elt;
140
141 // Get an instance we can change (pushing if necessary)
142 elt = (SoCoordinateElement *) getElement(state, classStackIndex, node);
143
144 if (elt != NULL) {
145 elt->numCoords = numCoords;
146 elt->coords4 = coords;
147 elt->coordsAre3D = FALSE;
148 }
149 }
150
151
152 ////////////////////////////////////////////////////////////////////////
153 //
154 // Description:
155 // Returns the indexed coordinate from an element as an SbVec3f.
156 //
157 // Use: public
158
159 const SbVec3f &
160 SoCoordinateElement::get3(int index) const
161 //
162 ////////////////////////////////////////////////////////////////////////
163 {
164 #ifdef DEBUG
165 if (index < 0 || index >= numCoords)
166 SoDebugError::post("SoCoordinateElement::get3",
167 "Index (%d) is out of range 0 - %d",
168 index, numCoords - 1);
169 #endif /* DEBUG */
170
171 if (coordsAre3D)
172 return coords3[index];
173
174 // Convert from 4-D if necessary
175 else {
176 // Cast the const away...
177 SoCoordinateElement *elt = (SoCoordinateElement *) this;
178 const SbVec4f &c4 = coords4[index];
179
180 // If we can't do the projection, or we don't have to
181 if (c4[3] == 0.0 || c4[3] == 1.0) {
182 elt->convert3[0] = c4[0];
183 elt->convert3[1] = c4[1];
184 elt->convert3[2] = c4[2];
185 }
186
187 else {
188 elt->convert3[0] = c4[0] / c4[3];
189 elt->convert3[1] = c4[1] / c4[3];
190 elt->convert3[2] = c4[2] / c4[3];
191 }
192
193 return convert3;
194 }
195 }
196
197 ////////////////////////////////////////////////////////////////////////
198 //
199 // Description:
200 // Returns the indexed coordinate from an element as an SbVec4f.
201 //
202 // Use: public
203
204 const SbVec4f &
205 SoCoordinateElement::get4(int index) const
206 //
207 ////////////////////////////////////////////////////////////////////////
208 {
209 #ifdef DEBUG
210 if (index < 0 || index >= numCoords)
211 SoDebugError::post("SoCoordinateElement::get4",
212 "Index (%d) is out of range 0 - %d",
213 index, numCoords - 1);
214 #endif /* DEBUG */
215
216 // Convert from 3-D if necessary
217 if (coordsAre3D) {
218 // Cast the const away...
219 SoCoordinateElement *elt = (SoCoordinateElement *) this;
220 const SbVec3f &c3 = coords3[index];
221
222 elt->convert4[0] = c3[0];
223 elt->convert4[1] = c3[1];
224 elt->convert4[2] = c3[2];
225 elt->convert4[3] = 1.0;
226
227 return convert4;
228 }
229
230 else
231 return coords4[index];
232 }
233
234 ////////////////////////////////////////////////////////////////////////
235 //
236 // Description:
237 // Prints element for debugging.
238 //
239 // Use: public
240 //
241 ////////////////////////////////////////////////////////////////////////
242
243 #ifdef DEBUG
244 void
245 SoCoordinateElement::print(FILE *fp) const
246 {
247 SoReplacedElement::print(fp);
248 }
249 #else /* DEBUG */
250 void
251 SoCoordinateElement::print(FILE *) const
252 {
253 }
254 #endif /* DEBUG */