Debian Codegraph @

src/Gui/Inventor/Draggers/SoRotationDraggerGeometry.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2025 Sayantan Deb <sayantandebin[at]gmail.com> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24 #include <algorithm>
25 #include <cmath>
26 #include <numbers>
27
28 #include <Inventor/nodes/SoBaseColor.h>
29 #include <Inventor/nodes/SoCone.h>
30 #include <Inventor/nodes/SoCoordinate3.h>
31 #include <Inventor/nodes/SoCylinder.h>
32 #include <Inventor/nodes/SoDrawStyle.h>
33 #include <Inventor/nodes/SoLightModel.h>
34 #include <Inventor/nodes/SoLineSet.h>
35 #include <Inventor/nodes/SoPickStyle.h>
36 #include <Inventor/nodes/SoSphere.h>
37 #include <Inventor/nodes/SoTransform.h>
38 #include <Inventor/nodes/SoTranslation.h>
39
40 #include "SoRotationDraggerGeometry.h"
41
42 #include <Gui/Inventor/SoToggleSwitch.h>
43
44 using namespace Gui;
45
46 SO_KIT_SOURCE(SoRotatorGeometryKit)
47
48 void SoRotatorGeometryKit::initClass()
49 {
50 SO_KIT_INIT_CLASS(SoRotatorGeometryKit, SoBaseKit, "BaseKit");
51 }
52
53 SoRotatorGeometryKit::SoRotatorGeometryKit()
54 {
55 SO_KIT_CONSTRUCTOR(SoRotatorGeometryKit);
56
57 SO_KIT_ADD_FIELD(pivotPosition, (0.0, 0.0, 0.0));
58 SO_KIT_ADD_FIELD(geometryScale, (1.0, 1.0, 1.0));
59
60 SO_KIT_INIT_INSTANCE();
61 }
62
63 SO_KIT_SOURCE(SoRotatorGeometry)
64
65 void SoRotatorGeometry::initClass()
66 {
67 SO_KIT_INIT_CLASS(SoRotatorGeometry, SoRotatorGeometryKit, "RotatorGeometryKit");
68 }
69
70 SoRotatorGeometry::SoRotatorGeometry()
71 {
72 SO_KIT_CONSTRUCTOR(SoRotatorGeometry);
73 SO_KIT_ADD_CATALOG_ENTRY(separator, SoSeparator, false, this, "", false);
74 SO_KIT_ADD_CATALOG_ENTRY(lightModel, SoLightModel, false, separator, "", false);
75 SO_KIT_ADD_CATALOG_ENTRY(pickStyle, SoPickStyle, false, separator, "", false);
76 SO_KIT_ADD_CATALOG_ENTRY(drawStyle, SoDrawStyle, false, separator, "", false);
77 SO_KIT_ADD_CATALOG_ENTRY(arcCoords, SoCoordinate3, false, separator, "", true);
78 SO_KIT_ADD_CATALOG_ENTRY(arc, SoLineSet, false, separator, "", true);
79 SO_KIT_ADD_CATALOG_ENTRY(pivotSeparator, SoSeparator, false, separator, "", true);
80 SO_KIT_ADD_CATALOG_ENTRY(rotorPivot, SoSphere, false, pivotSeparator, "", true);
81
82 SO_KIT_ADD_CATALOG_ENTRY(_rotorPivotTranslation, SoTranslation, false, pivotSeparator, rotorPivot, false);
83
84 SO_KIT_ADD_FIELD(arcRadius, (8.0f));
85 SO_KIT_ADD_FIELD(arcAngle, (std::numbers::pi_v<float> / 2.0f));
86 SO_KIT_ADD_FIELD(sphereRadius, (0.8f));
87 SO_KIT_ADD_FIELD(arcThickness, (4.0f));
88
89 SO_KIT_INIT_INSTANCE();
90
91 auto lightModel = SO_GET_ANY_PART(this, "lightModel", SoLightModel);
92 lightModel->model = SoLightModel::BASE_COLOR;
93
94 auto pickStyle = SO_GET_ANY_PART(this, "pickStyle", SoPickStyle);
95 pickStyle->style = SoPickStyle::SHAPE_ON_TOP;
96
97 auto rotorPivot = SO_GET_ANY_PART(this, "rotorPivot", SoSphere);
98 rotorPivot->radius.connectFrom(&sphereRadius);
99
100 auto drawStyle = SO_GET_ANY_PART(this, "drawStyle", SoDrawStyle);
101 drawStyle->lineWidth.connectFrom(&arcThickness);
102
103 auto translation = SO_GET_ANY_PART(this, "_rotorPivotTranslation", SoTranslation);
104 pivotPosition.connectFrom(&translation->translation);
105
106 auto arc = SO_GET_ANY_PART(this, "arc", SoLineSet);
107 arc->numVertices = segments + 1;
108
109 // forces the notify method to get called so that the initial translations and other values are set
110 arcRadius.touch();
111 }
112
113 void SoRotatorGeometry::notify(SoNotList* notList)
114 {
115 SoField* lastField = notList->getLastField();
116
117 if (lastField == &arcRadius || lastField == &arcAngle) {
118 float angle = arcAngle.getValue();
119 float radius = arcRadius.getValue();
120
121 auto coordinates = SO_GET_ANY_PART(this, "arcCoords", SoCoordinate3);
122 float angleIncrement = angle / static_cast<float>(segments);
123 SbRotation rotation(SbVec3f(0.0, 0.0, 1.0), angleIncrement);
124 SbVec3f point(radius, 0.0, 0.0);
125 for (int index = 0; index <= segments; ++index) {
126 coordinates->point.set1Value(index, point);
127 rotation.multVec(point, point);
128 }
129
130 auto translation = SO_GET_ANY_PART(this, "_rotorPivotTranslation", SoTranslation);
131 translation->translation = {radius * cos(angle / 2.0f), radius * sin(angle / 2.0f), 0};
132 }
133 }
134
135 SO_KIT_SOURCE(SoRotatorGeometry2)
136
137 void SoRotatorGeometry2::initClass()
138 {
139 SO_KIT_INIT_CLASS(SoRotatorGeometry2, SoRotatorGeometry, "SoRotatorGeometry");
140 }
141
142 SoRotatorGeometry2::SoRotatorGeometry2()
143 {
144 SO_KIT_CONSTRUCTOR(SoRotatorGeometry2);
145
146 SO_KIT_ADD_CATALOG_ENTRY(leftArrowSwitch, SoToggleSwitch, true, separator, "", true);
147 SO_KIT_ADD_CATALOG_ENTRY(leftArrowSeparator, SoSeparator, true, leftArrowSwitch, "", true);
148 SO_KIT_ADD_CATALOG_ENTRY(leftArrowTransform, SoTransform, true, leftArrowSeparator, "", true);
149 SO_KIT_ADD_CATALOG_ENTRY(leftArrow, SoCone, true, leftArrowSeparator, "", true);
150
151 SO_KIT_ADD_CATALOG_ENTRY(rightArrowSwitch, SoToggleSwitch, true, separator, "", true);
152 SO_KIT_ADD_CATALOG_ENTRY(rightArrowSeparator, SoSeparator, true, rightArrowSwitch, "", true);
153 SO_KIT_ADD_CATALOG_ENTRY(rightArrowTransform, SoTransform, true, rightArrowSeparator, "", true);
154 SO_KIT_ADD_CATALOG_ENTRY(rightArrow, SoCone, true, rightArrowSeparator, "", true);
155
156 SO_KIT_ADD_FIELD(coneBottomRadius, (0.8));
157 SO_KIT_ADD_FIELD(coneHeight, (2.5));
158 SO_KIT_ADD_FIELD(leftArrowVisible, (true));
159 SO_KIT_ADD_FIELD(rightArrowVisible, (true));
160
161 SO_KIT_INIT_INSTANCE();
162
163 auto arrowHead = SO_GET_ANY_PART(this, "leftArrow", SoCone);
164 arrowHead->bottomRadius.connectFrom(&coneBottomRadius);
165 arrowHead->height.connectFrom(&coneHeight);
166
167 arrowHead = SO_GET_ANY_PART(this, "rightArrow", SoCone);
168 arrowHead->bottomRadius.connectFrom(&coneBottomRadius);
169 arrowHead->height.connectFrom(&coneHeight);
170
171 auto sw = SO_GET_ANY_PART(this, "leftArrowSwitch", SoToggleSwitch);
172 sw->on.connectFrom(&leftArrowVisible);
173
174 sw = SO_GET_ANY_PART(this, "rightArrowSwitch", SoToggleSwitch);
175 sw->on.connectFrom(&rightArrowVisible);
176
177 // forces the notify method to get called so that the initial transforms are set
178 arcAngle.touch();
179 }
180
181 void SoRotatorGeometry2::notify(SoNotList* notList)
182 {
183 SoField* lastField = notList->getLastField();
184
185 inherited::notify(notList);
186
187 if (lastField == &arcAngle || lastField == &arcRadius) {
188 auto angle = arcAngle.getValue();
189 auto radius = arcRadius.getValue();
190
191 auto transform = SO_GET_ANY_PART(this, "leftArrowTransform", SoTransform);
192 transform->translation = SbVec3f {radius, 0, 0};
193 transform->rotation = SbRotation({0, 0, 1}, std::numbers::pi_v<float>);
194
195 transform = SO_GET_ANY_PART(this, "rightArrowTransform", SoTransform);
196 transform->translation = SbVec3f {radius * std::cos(angle), radius * std::sin(angle), 0};
197 transform->rotation = SbRotation({0, 0, 1}, angle);
198 }
199 }
200
201 void SoRotatorGeometry2::toggleArrowVisibility()
202 {
203 leftArrowVisible = !leftArrowVisible.getValue();
204 rightArrowVisible = !rightArrowVisible.getValue();
205 }
206
207 SO_KIT_SOURCE(SoRotatorArrow)
208
209 void SoRotatorArrow::initClass()
210 {
211 SO_KIT_INIT_CLASS(SoRotatorArrow, SoRotatorGeometryKit, "RotatorGeometryKit");
212 }
213
214 SoRotatorArrow::SoRotatorArrow()
215 {
216 SO_KIT_CONSTRUCTOR(SoRotatorArrow);
217 SO_KIT_ADD_CATALOG_ENTRY(separator, SoSeparator, false, this, "", false);
218 SO_KIT_ADD_CATALOG_ENTRY(lightModel, SoLightModel, false, separator, "", false);
219 SO_KIT_ADD_CATALOG_ENTRY(pickStyle, SoPickStyle, false, separator, "", false);
220 SO_KIT_ADD_CATALOG_ENTRY(arrowBody, SoCylinder, false, separator, "", true);
221 SO_KIT_ADD_CATALOG_ENTRY(arrowTip, SoCone, false, separator, "", true);
222
223 SO_KIT_ADD_CATALOG_ENTRY(_arrowTransform, SoTransform, false, separator, arrowBody, false);
224 SO_KIT_ADD_CATALOG_ENTRY(_arrowBodyTranslation, SoTranslation, false, separator, arrowBody, false);
225 SO_KIT_ADD_CATALOG_ENTRY(_arrowTipTranslation, SoTranslation, false, separator, arrowTip, false);
226
227 SO_KIT_ADD_FIELD(coneBottomRadius, (0.8));
228 SO_KIT_ADD_FIELD(coneHeight, (2.5));
229 SO_KIT_ADD_FIELD(cylinderHeight, (3.0));
230 SO_KIT_ADD_FIELD(cylinderRadius, (0.2));
231 SO_KIT_ADD_FIELD(radius, (8.0));
232 SO_KIT_ADD_FIELD(minRadius, (8.0));
233
234 SO_KIT_INIT_INSTANCE();
235
236 auto lightModel = SO_GET_ANY_PART(this, "lightModel", SoLightModel);
237 lightModel->model = SoLightModel::BASE_COLOR;
238
239 auto pickStyle = SO_GET_ANY_PART(this, "pickStyle", SoPickStyle);
240 pickStyle->style = SoPickStyle::SHAPE_ON_TOP;
241
242 auto arrowBody = SO_GET_ANY_PART(this, "arrowBody", SoCylinder);
243 arrowBody->height.connectFrom(&cylinderHeight);
244 arrowBody->radius.connectFrom(&cylinderRadius);
245
246 auto arrowTip = SO_GET_ANY_PART(this, "arrowTip", SoCone);
247 arrowTip->height.connectFrom(&coneHeight);
248 arrowTip->bottomRadius.connectFrom(&coneBottomRadius);
249
250 auto transform = SO_GET_ANY_PART(this, "_arrowTransform", SoTransform);
251 transform->rotation = SbRotation({1, 0, 0}, std::numbers::pi)
252 * SbRotation({0, 0, 1}, std::numbers::pi / 2);
253
254 // forces the notify method to get called so that the initial dimensions are set
255 cylinderHeight.touch();
256 radius.touch();
257 }
258
259 void SoRotatorArrow::flipArrow()
260 {
261 auto transform = SO_GET_ANY_PART(this, "_arrowTransform", SoTransform);
262 transform->rotation = transform->rotation.getValue() * SbRotation({0, 0, 1}, std::numbers::pi);
263 }
264
265 void SoRotatorArrow::notify(SoNotList* notList)
266 {
267 SoField* lastField = notList->getLastField();
268
269 if (lastField == &cylinderHeight) {
270 auto translation = SO_GET_ANY_PART(this, "_arrowBodyTranslation", SoTranslation);
271 translation->translation = SbVec3f(0, cylinderHeight.getValue() / 2.0f, 0);
272 }
273 else if (lastField == &radius || lastField == &minRadius || lastField == &geometryScale) {
274 auto transform = SO_GET_ANY_PART(this, "_arrowTransform", SoTransform);
275 // The geometry nodes are scaled from the parent dragger but in this case the translation
276 // itself shouldn't be scaled
277 pivotPosition = transform->translation = SbVec3f(
278 0,
279 std::max(minRadius.getValue(), radius.getValue() / geometryScale.getValue()[1]),
280 0
281 );
282 }
283
284 if (lastField == &coneHeight || lastField == &cylinderHeight) {
285 auto translation = SO_GET_ANY_PART(this, "_arrowTipTranslation", SoTranslation);
286 translation->translation
287 = SbVec3f(0, (cylinderHeight.getValue() + coneHeight.getValue()) / 2.0f, 0);
288 }
289 }
290
291 SO_KIT_SOURCE(SoRotatorGeometryBaseKit)
292
293 void SoRotatorGeometryBaseKit::initClass()
294 {
295 SO_KIT_INIT_CLASS(SoRotatorGeometryBaseKit, SoBaseKit, "BaseKit");
296 }
297
298 SoRotatorGeometryBaseKit::SoRotatorGeometryBaseKit()
299 {
300 SO_KIT_CONSTRUCTOR(SoRotatorGeometryBaseKit);
301
302 SO_KIT_ADD_FIELD(rotation, ({0.0, 0.0, 0.0}, 0.0));
303 SO_KIT_ADD_FIELD(geometryScale, (1.0, 1.0, 1.0));
304 SO_KIT_ADD_FIELD(active, (false));
305
306 SO_KIT_INIT_INSTANCE();
307 }
308
309 SO_KIT_SOURCE(SoRotatorBase)
310
311 void SoRotatorBase::initClass()
312 {
313 SO_KIT_INIT_CLASS(SoRotatorBase, SoRotatorGeometryBaseKit, "RotatorGeometryBaseKit");
314 }
315
316 SoRotatorBase::SoRotatorBase()
317 {
318 SO_KIT_CONSTRUCTOR(SoRotatorBase);
319 SO_KIT_ADD_CATALOG_ENTRY(separator, SoSeparator, false, this, "", false);
320 SO_KIT_ADD_CATALOG_ENTRY(lightModel, SoLightModel, false, separator, "", false);
321 SO_KIT_ADD_CATALOG_ENTRY(pickStyle, SoPickStyle, false, separator, "", false);
322 SO_KIT_ADD_CATALOG_ENTRY(baseColor, SoBaseColor, false, separator, "", true);
323 SO_KIT_ADD_CATALOG_ENTRY(drawStyle, SoDrawStyle, false, separator, "", false);
324 SO_KIT_ADD_CATALOG_ENTRY(arcCoords, SoCoordinate3, false, separator, "", true);
325 SO_KIT_ADD_CATALOG_ENTRY(arc, SoLineSet, false, separator, "", true);
326
327 SO_KIT_ADD_FIELD(minArcRadius, (8.0));
328 SO_KIT_ADD_FIELD(arcRadius, (8.0));
329 SO_KIT_ADD_FIELD(arcThickness, (2.0));
330 SO_KIT_ADD_FIELD(color, (0.214, 0.560, 0.930));
331
332 SO_KIT_INIT_INSTANCE();
333
334 auto lightModel = SO_GET_ANY_PART(this, "lightModel", SoLightModel);
335 lightModel->model = SoLightModel::BASE_COLOR;
336
337 auto pickStyle = SO_GET_ANY_PART(this, "pickStyle", SoPickStyle);
338 pickStyle->style = SoPickStyle::UNPICKABLE;
339
340 // We don't want to change the color of the base geometry when the dragger is
341 // dragged so the active field from SoRotatorGeometryBaseKit is unused
342 // If that is desired then just add a toggle switch with a colour after the
343 // baseColor node in the graph and it will work as expected
344 auto baseColor = SO_GET_ANY_PART(this, "baseColor", SoBaseColor);
345 baseColor->rgb.connectFrom(&color);
346
347 auto drawStyle = SO_GET_ANY_PART(this, "drawStyle", SoDrawStyle);
348 drawStyle->lineWidth.connectFrom(&arcThickness);
349
350 auto arc = SO_GET_ANY_PART(this, "arc", SoLineSet);
351 arc->numVertices = segments + 3;
352
353 // forces the notify method to get called so that the initial translations and other values are set
354 arcRadius.touch();
355 }
356
357 void SoRotatorBase::notify(SoNotList* notList)
358 {
359 SoField* lastField = notList->getLastField();
360
361 if (lastField == &arcRadius || lastField == &minArcRadius || lastField == &rotation
362 || lastField == &geometryScale) {
363 SbVec3f axis;
364 float angle;
365 rotation.getValue(axis, angle);
366 float radius
367 = std::max(minArcRadius.getValue() * geometryScale.getValue()[0], arcRadius.getValue());
368
369 auto coordinates = SO_GET_ANY_PART(this, "arcCoords", SoCoordinate3);
370 float angleIncrement = angle / static_cast<float>(segments);
371 SbRotation rotation(axis, angleIncrement);
372 SbVec3f point(0.0, radius, 0.0);
373 coordinates->point.set1Value(0, {0.0, 0.0, 0.0});
374 for (int index = 0; index <= segments; ++index) {
375 coordinates->point.set1Value(index + 1, point);
376 rotation.multVec(point, point);
377 }
378 coordinates->point.set1Value(segments + 2, {0.0, 0.0, 0.0});
379 }
380 }