Debian Codegraph @

src/Gui/Inventor/SoFCBoundingBox.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <sstream>
24
25 #include <Inventor/SbBox.h>
26 #include <Inventor/actions/SoGLRenderAction.h>
27 #include <Inventor/elements/SoLazyElement.h>
28 #include <Inventor/misc/SoState.h>
29 #include <Inventor/nodes/SoText2.h>
30 #include <Inventor/nodes/SoTransform.h>
31
32 #include "SoFCBoundingBox.h"
33
34
35 using namespace Gui;
36
37 SO_NODE_SOURCE(SoFCBoundingBox)
38
39 // vertices used to create a box
40 static const int32_t bBoxVerts[8][3]
41 = {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}};
42
43 // indexes used to create the edges
44 static const int32_t bBoxEdges[36] = {0, 1, -1, 1, 2, -1, 2, 3, -1, 3, 0, -1, 4, 5, -1, 5, 6, -1,
45 6, 7, -1, 7, 4, -1, 0, 4, -1, 1, 5, -1, 2, 6, -1, 3, 7, -1};
46
47 void SoFCBoundingBox::initClass()
48 {
49 SO_NODE_INIT_CLASS(SoFCBoundingBox, SoShape, "Shape");
50 }
51
52 SoFCBoundingBox::SoFCBoundingBox()
53 {
54 SO_NODE_CONSTRUCTOR(SoFCBoundingBox);
55
56 SO_NODE_ADD_FIELD(minBounds, (-1.0, -1.0, -1.0));
57 SO_NODE_ADD_FIELD(maxBounds, (1.0, 1.0, 1.0));
58 SO_NODE_ADD_FIELD(coordsOn, (true));
59 SO_NODE_ADD_FIELD(dimensionsOn, (true));
60
61 root = new SoSeparator();
62 auto bboxSep = new SoSeparator();
63
64 bboxCoords = new SoCoordinate3();
65 bboxCoords->point.setNum(8);
66 bboxSep->addChild(bboxCoords);
67 root->addChild(bboxSep);
68
69 // the lines of the box
70 bboxLines = new SoIndexedLineSet();
71 bboxLines->coordIndex.setNum(36);
72 bboxLines->coordIndex.setValues(0, 36, bBoxEdges);
73 bboxSep->addChild(bboxLines);
74
75
76 // create the text nodes, including a transform for each vertice offset
77 textSep = new SoSeparator();
78 for (int i = 0; i < 8; i++) {
79 auto temp = new SoSeparator();
80 auto trans = new SoTransform();
81 temp->addChild(trans);
82 auto text = new SoText2();
83 text->justification.setValue(SoText2::CENTER);
84 temp->addChild(text);
85 textSep->addChild(temp);
86 }
87
88 // create the text nodes, including a transform for each dimension
89 dimSep = new SoSeparator();
90 for (int i = 0; i < 3; i++) {
91 auto temp = new SoSeparator();
92 auto trans = new SoTransform();
93 temp->addChild(trans);
94 auto text = new SoText2();
95 text->justification.setValue(SoText2::CENTER);
96 temp->addChild(text);
97 dimSep->addChild(temp);
98 }
99
100 root->addChild(textSep);
101 root->addChild(dimSep);
102 root->ref();
103 }
104
105 SoFCBoundingBox::~SoFCBoundingBox()
106 {
107 root->unref();
108 }
109
110 void SoFCBoundingBox::GLRender(SoGLRenderAction* action)
111 {
112 SbVec3f corner[2], ctr, *vptr;
113 SbBool coord, dimension;
114
115 // grab the current state
116 // SoState *state = action->getState();
117
118 if (!shouldGLRender(action)) {
119 return;
120 }
121
122 // get the latest values from the fields
123 corner[0] = minBounds.getValue();
124 corner[1] = maxBounds.getValue();
125 coord = coordsOn.getValue();
126 dimension = dimensionsOn.getValue();
127
128 // set the coordinates for the LineSet to point to
129 vptr = bboxCoords->point.startEditing();
130 for (int i = 0; i < 8; i++) {
131 for (int j = 0; j < 3; j++) {
132 vptr[i][j] = corner[bBoxVerts[i][j]][j];
133 }
134 }
135
136 // if coord is true then set the text nodes
137 if (coord) {
138 ctr = (corner[1] - corner[0]) / 2.0f;
139 for (int i = 0; i < 8; i++) {
140 // create the string for the text
141 std::stringstream str;
142 str.precision(2);
143 str.setf(std::ios::fixed | std::ios::showpoint);
144 str << "(" << vptr[i][0] << "," << vptr[i][1] << "," << vptr[i][2] << ")";
145
146 SoSeparator* sep = static_cast<SoSeparator*>(textSep->getChild(i));
147 SoTransform* trans = static_cast<SoTransform*>(sep->getChild(0));
148
149 trans->translation.setValue(vptr[i].getValue());
150 SoText2* t = static_cast<SoText2*>(sep->getChild(1));
151 t->string.setValue(str.str().c_str());
152 }
153
154 textSep->ref();
155 if (root->findChild(textSep) < 0) {
156 root->addChild(textSep);
157 }
158 }
159 else {
160 if (root->findChild(textSep) >= 0) {
161 root->removeChild(textSep);
162 }
163 }
164
165 // if dimension is true then set the text nodes
166 if (dimension) {
167 ctr = (corner[1] - corner[0]) / 2.0f;
168 for (int i = 0; i < 3; i++) {
169 // create the string for the text
170 std::stringstream str;
171 str.precision(2);
172 str.setf(std::ios::fixed | std::ios::showpoint);
173 str << (2.0f * ctr[i]);
174
175 SoSeparator* sep = static_cast<SoSeparator*>(dimSep->getChild(i));
176 SoTransform* trans = static_cast<SoTransform*>(sep->getChild(0));
177
178 SbVec3f point = corner[0];
179 point[i] += ctr[i];
180 trans->translation.setValue(point.getValue());
181 SoText2* t = static_cast<SoText2*>(sep->getChild(1));
182 t->string.setValue(str.str().c_str());
183 }
184
185 dimSep->ref();
186 if (root->findChild(dimSep) < 0) {
187 root->addChild(dimSep);
188 }
189 }
190 else {
191 if (root->findChild(dimSep) >= 0) {
192 root->removeChild(dimSep);
193 }
194 }
195
196 bboxCoords->point.finishEditing();
197
198 // Avoid shading
199 SoState* state = action->getState();
200 state->push();
201 SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
202 root->GLRender(action);
203 state->pop();
204 }
205
206 void SoFCBoundingBox::generatePrimitives(SoAction* /*action*/)
207 {}
208
209 void SoFCBoundingBox::computeBBox(SoAction* /*action*/, SbBox3f& box, SbVec3f& center)
210 {
211 center = (minBounds.getValue() + maxBounds.getValue()) / 2.0f;
212 box.setBounds(minBounds.getValue(), maxBounds.getValue());
213 }
214
215 void SoFCBoundingBox::finish()
216 {
217 atexit_cleanup();
218 }
219
220 // ---------------------------------------------------------------
221
222 SO_NODE_SOURCE(SoSkipBoundingGroup)
223
224 /*!
225 Constructor.
226 */
227 SoSkipBoundingGroup::SoSkipBoundingGroup()
228 {
229 SO_NODE_CONSTRUCTOR(SoSkipBoundingGroup);
230
231 SO_NODE_ADD_FIELD(mode, (INCLUDE_BBOX));
232
233 SO_NODE_DEFINE_ENUM_VALUE(Modes, INCLUDE_BBOX);
234 SO_NODE_DEFINE_ENUM_VALUE(Modes, EXCLUDE_BBOX);
235 SO_NODE_SET_SF_ENUM_TYPE(mode, Modes);
236 }
237
238 /*!
239 Destructor.
240 */
241 SoSkipBoundingGroup::~SoSkipBoundingGroup() = default;
242
243 void SoSkipBoundingGroup::initClass()
244 {
245 SO_NODE_INIT_CLASS(SoSkipBoundingGroup, SoGroup, "Group");
246 }
247
248 void SoSkipBoundingGroup::finish()
249 {
250 atexit_cleanup();
251 }
252
253 void SoSkipBoundingGroup::getBoundingBox(SoGetBoundingBoxAction* action)
254 {
255 if (mode.getValue() == INCLUDE_BBOX) {
256 inherited::getBoundingBox(action);
257 }
258 }