Debian Codegraph @

src/Gui/Inventor/SoAxisCrossKit.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2010 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 <FCConfig.h>
24
25 #if defined(FC_OS_WIN32)
26 # include <windows.h>
27 #endif
28
29 #ifdef FC_OS_MACOSX
30 # include <OpenGL/gl.h>
31 #else
32 # include <GL/gl.h>
33 #endif
34
35 #include <sstream>
36
37 #include <Inventor/actions/SoGetBoundingBoxAction.h>
38 #include <Inventor/actions/SoGLRenderAction.h>
39 #include <Inventor/bundles/SoMaterialBundle.h>
40 #include <Inventor/bundles/SoTextureCoordinateBundle.h>
41 #include <Inventor/elements/SoLazyElement.h>
42 #include <Inventor/elements/SoModelMatrixElement.h>
43 #include <Inventor/elements/SoViewportRegionElement.h>
44 #include <Inventor/elements/SoViewVolumeElement.h>
45 #include <Inventor/nodekits/SoShapeKit.h>
46 #include <Inventor/nodes/SoBaseColor.h>
47 #include <Inventor/nodes/SoCone.h>
48 #include <Inventor/nodes/SoCoordinate3.h>
49 #include <Inventor/nodes/SoCube.h>
50 #include <Inventor/nodes/SoFontStyle.h>
51 #include <Inventor/nodes/SoLineSet.h>
52 #include <Inventor/nodes/SoScale.h>
53 #include <Inventor/nodes/SoSeparator.h>
54 #include <Inventor/nodes/SoText2.h>
55 #include <Inventor/nodes/SoTranslation.h>
56
57 #include <Base/Color.h>
58 #include <Gui/ViewParams.h>
59
60 #include "SoAxisCrossKit.h"
61 #include "SoDevicePixelRatioElement.h"
62
63 using namespace Gui;
64
65 SO_KIT_SOURCE(SoShapeScale)
66
67 // Constructor.
68 SoShapeScale::SoShapeScale()
69 {
70 SO_KIT_CONSTRUCTOR(SoShapeScale);
71
72 SO_KIT_ADD_FIELD(active, (true));
73 SO_KIT_ADD_FIELD(scaleFactor, (1.0f));
74
75 SO_KIT_ADD_CATALOG_ENTRY(topSeparator, SoSeparator, false, this, "", false);
76 SO_KIT_ADD_CATALOG_ABSTRACT_ENTRY(shape, SoNode, SoCube, true, topSeparator, "", true);
77 SO_KIT_ADD_CATALOG_ENTRY(scale, SoScale, false, topSeparator, shape, false);
78
79 SO_KIT_INIT_INSTANCE();
80 }
81
82 // Destructor.
83 SoShapeScale::~SoShapeScale() = default;
84
85 void SoShapeScale::initClass()
86 {
87 SO_KIT_INIT_CLASS(SoShapeScale, SoBaseKit, "BaseKit");
88 }
89
90 void SoShapeScale::GLRender(SoGLRenderAction* action)
91 {
92 auto* scale = static_cast<SoScale*>(this->getAnyPart(SbName("scale"), true));
93 if (!this->active.getValue()) {
94 SbVec3f v(1.0f, 1.0f, 1.0f);
95 if (scale->scaleFactor.getValue() != v) {
96 scale->scaleFactor = v;
97 }
98 }
99 else {
100 SoState* state = action->getState();
101 const SbViewportRegion& vp = SoViewportRegionElement::get(state);
102 const SbViewVolume& vv = SoViewVolumeElement::get(state);
103
104 SbVec3f center(0.0f, 0.0f, 0.0f);
105 float nsize = this->scaleFactor.getValue() / float(vp.getViewportSizePixels()[0]);
106 SoModelMatrixElement::get(state).multVecMatrix(center, center); // world coords
107 float sf = vv.getWorldToScreenScale(center, nsize);
108
109 sf *= SoDevicePixelRatioElement::get(state);
110
111 SbVec3f v(sf, sf, sf);
112 if (scale->scaleFactor.getValue() != v) {
113 scale->scaleFactor = v;
114 }
115 }
116
117 inherited::GLRender(action);
118 }
119
120 // --------------------------------------------------------------
121
122 SO_KIT_SOURCE(SoAxisCrossKit)
123
124 void SoAxisCrossKit::initClass()
125 {
126 SO_KIT_INIT_CLASS(SoAxisCrossKit, SoBaseKit, "BaseKit");
127 }
128
129 SoAxisCrossKit::SoAxisCrossKit()
130 {
131 SO_KIT_CONSTRUCTOR(SoAxisCrossKit);
132
133 // Add the parts to the catalog...
134 SO_KIT_ADD_CATALOG_ENTRY(xAxis, SoShapeKit, true, this, "", true);
135 SO_KIT_ADD_CATALOG_ENTRY(xHead, SoShapeKit, true, this, "", true);
136 SO_KIT_ADD_CATALOG_ENTRY(yAxis, SoShapeKit, true, this, "", true);
137 SO_KIT_ADD_CATALOG_ENTRY(yHead, SoShapeKit, true, this, "", true);
138 SO_KIT_ADD_CATALOG_ENTRY(zAxis, SoShapeKit, true, this, "", true);
139 SO_KIT_ADD_CATALOG_ENTRY(zHead, SoShapeKit, true, this, "", true);
140
141 SO_KIT_INIT_INSTANCE();
142
143 createAxes();
144 }
145
146 SoAxisCrossKit::~SoAxisCrossKit() = default;
147
148 // This kit is made up entirely of SoShapeKits.
149 // Since SoShapeKits do not affect state, neither does this.
150 SbBool SoAxisCrossKit::affectsState() const
151 {
152 return false;
153 }
154
155 void SoAxisCrossKit::addWriteReference(SoOutput* /*out*/, SbBool /*isfromfield*/)
156 {
157 // this node should not be written out to a file
158 }
159
160 void SoAxisCrossKit::getBoundingBox(SoGetBoundingBoxAction* action)
161 {
162 inherited::getBoundingBox(action);
163 action->resetCenter();
164 action->setCenter(SbVec3f(0, 0, 0), false);
165 }
166
167 // Set up parts for default configuration of the jumping jack
168 void SoAxisCrossKit::createAxes()
169 {
170 // Create the heads.
171 auto head = new SoCone;
172 head->bottomRadius.setValue(5);
173 head->height.setValue(10);
174 setPart("xHead.shape", head);
175 setPart("yHead.shape", head);
176 setPart("zHead.shape", head);
177
178 // Create the axes.
179 auto coords = new SoCoordinate3;
180 coords->point.set1Value(0, SbVec3f(0, 0, 0));
181 coords->point.set1Value(1, SbVec3f(90, 0, 0));
182 setPart("xAxis.coordinate3", coords);
183 setPart("yAxis.coordinate3", coords);
184 setPart("zAxis.coordinate3", coords);
185
186 auto shape = new SoLineSet;
187 setPart("xAxis.shape", shape);
188 setPart("yAxis.shape", shape);
189 setPart("zAxis.shape", shape);
190
191 // Place the axes and heads
192 set("yAxis.transform", "rotation 0 0 1 1.5707999");
193 set("zAxis.transform", "rotation 0 1 0 -1.5707999");
194
195 set("xHead.transform", "translation 95 0 0");
196 set("xHead.transform", "scaleFactor 0.5 1.5 0.5");
197 set("xHead.transform", "rotation 0 0 -1 1.5707999");
198
199 set("yHead.transform", "translation 0 95 0");
200 set("yHead.transform", "scaleFactor 0.5 1.5 0.5");
201 set("yHead.transform", "rotation 0 0 1 0");
202
203 set("zHead.transform", "translation 0 0 95");
204 set("zHead.transform", "scaleFactor 0.5 1.5 0.5");
205 set("zHead.transform", "rotation 1 0 0 1.5707999");
206
207 // Set colors & styles
208 set("xAxis.appearance.lightModel", "model BASE_COLOR");
209 set("xHead.appearance.lightModel", "model BASE_COLOR");
210 set("yAxis.appearance.lightModel", "model BASE_COLOR");
211 set("yHead.appearance.lightModel", "model BASE_COLOR");
212 set("zAxis.appearance.lightModel", "model BASE_COLOR");
213 set("zHead.appearance.lightModel", "model BASE_COLOR");
214 set("xAxis.appearance.drawStyle", "lineWidth 1");
215 set("yAxis.appearance.drawStyle", "lineWidth 1");
216 set("zAxis.appearance.drawStyle", "lineWidth 1");
217
218 unsigned long colorLong;
219 Base::Color color;
220 std::stringstream parameterstring;
221
222 colorLong = Gui::ViewParams::instance()->getAxisXColor();
223 color = Base::Color(static_cast<uint32_t>(colorLong));
224 parameterstring << "diffuseColor " << color.r << " " << color.g << " " << color.b;
225 set("xAxis.appearance.material", parameterstring.str().c_str());
226 set("xHead.appearance.material", parameterstring.str().c_str());
227
228 colorLong = Gui::ViewParams::instance()->getAxisYColor();
229 color = Base::Color(static_cast<uint32_t>(colorLong));
230 parameterstring << "diffuseColor " << color.r << " " << color.g << " " << color.b;
231 set("yAxis.appearance.material", parameterstring.str().c_str());
232 set("yHead.appearance.material", parameterstring.str().c_str());
233
234 colorLong = Gui::ViewParams::instance()->getAxisZColor();
235 color = Base::Color(static_cast<uint32_t>(colorLong));
236 parameterstring << "diffuseColor " << color.r << " " << color.g << " " << color.b;
237 set("zAxis.appearance.material", parameterstring.str().c_str());
238 set("zHead.appearance.material", parameterstring.str().c_str());
239
240 // Make unpickable
241 set("xAxis.pickStyle", "style UNPICKABLE");
242 set("xHead.pickStyle", "style UNPICKABLE");
243 set("yAxis.pickStyle", "style UNPICKABLE");
244 set("yHead.pickStyle", "style UNPICKABLE");
245 set("zAxis.pickStyle", "style UNPICKABLE");
246 set("zHead.pickStyle", "style UNPICKABLE");
247 }
248
249 // --------------------------------------------------------------
250
251 SO_NODE_SOURCE(SoRegPoint)
252
253 void SoRegPoint::initClass()
254 {
255 SO_NODE_INIT_CLASS(SoRegPoint, SoShape, "Shape");
256 }
257
258 SoRegPoint::SoRegPoint()
259 {
260 SO_NODE_CONSTRUCTOR(SoRegPoint);
261
262 SO_NODE_ADD_FIELD(base, (SbVec3f(0, 0, 0)));
263 SO_NODE_ADD_FIELD(normal, (SbVec3f(1, 1, 1)));
264 SO_NODE_ADD_FIELD(length, (3.0));
265 SO_NODE_ADD_FIELD(color, (1.0f, 0.447059f, 0.337255f));
266 SO_NODE_ADD_FIELD(text, (""));
267
268 root = new SoSeparator();
269 root->ref();
270
271 // translation
272 auto move = new SoTranslation();
273 move->translation.setValue(base.getValue() + normal.getValue() * length.getValue());
274 root->addChild(move);
275
276 // sub-group
277 auto col = new SoBaseColor();
278 col->rgb.setValue(this->color.getValue());
279
280 auto font = new SoFontStyle;
281 font->size = 14;
282
283 auto sub = new SoSeparator();
284 sub->addChild(col);
285 sub->addChild(font);
286 sub->addChild(new SoText2());
287 root->addChild(sub);
288 }
289
290 SoRegPoint::~SoRegPoint()
291 {
292 root->unref();
293 }
294
295 /**
296 * Renders the probe with text label and a bullet at the base point.
297 */
298 void SoRegPoint::GLRender(SoGLRenderAction* action)
299 {
300 if (shouldGLRender(action)) {
301 SoState* state = action->getState();
302 state->push();
303 SoMaterialBundle mb(action);
304 SoTextureCoordinateBundle tb(action, true, false);
305 SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
306 mb.sendFirst(); // make sure we have the correct material
307
308 SbVec3f p1 = base.getValue();
309 SbVec3f p2 = p1 + normal.getValue() * length.getValue();
310
311 glLineWidth(1.0f);
312 glColor3fv(color.getValue().getValue());
313 glBegin(GL_LINE_STRIP);
314 glVertex3d(p1[0], p1[1], p1[2]);
315 glVertex3d(p2[0], p2[1], p2[2]);
316 glEnd();
317 glPointSize(5.0f);
318 glBegin(GL_POINTS);
319 glVertex3fv(p1.getValue());
320 glEnd();
321 glPointSize(2.0f);
322 glBegin(GL_POINTS);
323 glVertex3fv(p2.getValue());
324 glEnd();
325
326 root->GLRender(action);
327 state->pop();
328 }
329 }
330
331 void SoRegPoint::generatePrimitives(SoAction* /*action*/)
332 {}
333
334 /**
335 * Sets the bounding box of the probe to \a box and its center to \a center.
336 */
337 void SoRegPoint::computeBBox(SoAction* action, SbBox3f& box, SbVec3f& center)
338 {
339 root->doAction(action);
340 if (action->getTypeId().isDerivedFrom(SoGetBoundingBoxAction::getClassTypeId())) {
341 static_cast<SoGetBoundingBoxAction*>(action)->resetCenter();
342 }
343
344 SbVec3f p1 = base.getValue();
345 SbVec3f p2 = p1 + normal.getValue() * length.getValue();
346
347 box.extendBy(p1);
348 box.extendBy(p2);
349
350 center = box.getCenter();
351 }
352
353 void SoRegPoint::notify(SoNotList* node)
354 {
355 SoField* f = node->getLastField();
356 if (f == &this->base || f == &this->normal || f == &this->length) {
357 auto move = static_cast<SoTranslation*>(root->getChild(0));
358 move->translation.setValue(base.getValue() + normal.getValue() * length.getValue());
359 }
360 else if (f == &this->color) {
361 auto sub = static_cast<SoSeparator*>(root->getChild(1));
362 auto col = static_cast<SoBaseColor*>(sub->getChild(0));
363 col->rgb = this->color.getValue();
364 }
365 else if (f == &this->text) {
366 auto sub = static_cast<SoSeparator*>(root->getChild(1));
367 auto label = static_cast<SoText2*>(sub->getChild(2));
368 label->string = this->text.getValue();
369 }
370
371 SoShape::notify(node);
372 }