Debian Codegraph @

src/Gui/Inventor/SoFCPlacementIndicatorKit.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2024 Kacper Donat <[email protected]> *
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 <sstream>
25
26 #include <Inventor/nodes/SoBaseColor.h>
27 #include <Inventor/nodes/SoComplexity.h>
28 #include <Inventor/nodes/SoCone.h>
29 #include <Inventor/nodes/SoCylinder.h>
30 #include <Inventor/nodes/SoDrawStyle.h>
31 #include <Inventor/nodes/SoLightModel.h>
32 #include <Inventor/nodes/SoMaterial.h>
33 #include <Inventor/nodes/SoRotation.h>
34 #include <Inventor/nodes/SoSeparator.h>
35 #include <Inventor/nodes/SoSphere.h>
36 #include <Inventor/nodes/SoTransform.h>
37 #include <Inventor/nodes/SoTranslation.h>
38 #include <Inventor/nodes/SoFontStyle.h>
39
40 #include "SoFCPlacementIndicatorKit.h"
41
42 #include "So3DAnnotation.h"
43 #include "SoAxisCrossKit.h"
44
45 #include <SoTextLabel.h>
46 #include <Utilities.h>
47 #include <ViewParams.h>
48 #include <ViewProvider.h>
49 #include <Base/Color.h>
50 #include <Base/Placement.h>
51 #include <Base/Vector3D.h>
52
53 namespace Gui
54 {
55
56 SO_KIT_SOURCE(SoFCPlacementIndicatorKit);
57
58 SoFCPlacementIndicatorKit::SoFCPlacementIndicatorKit()
59 {
60 SO_KIT_CONSTRUCTOR(SoFCPlacementIndicatorKit);
61
62 SO_KIT_ADD_CATALOG_ENTRY(root, SoShapeScale, false, this, "", true);
63
64 SO_KIT_INIT_INSTANCE();
65
66 SO_NODE_ADD_FIELD(coloredAxis, (true));
67 SO_NODE_ADD_FIELD(scaleFactor, (ViewParams::instance()->getPlacementIndicatorScale()));
68 SO_NODE_ADD_FIELD(axisLength, (axisLengthDefault));
69 SO_NODE_ADD_FIELD(parts, (AxisCross));
70 SO_NODE_ADD_FIELD(axes, (AllAxes));
71
72 SO_NODE_DEFINE_ENUM_VALUE(Part, Axes);
73 SO_NODE_DEFINE_ENUM_VALUE(Part, ArrowHeads);
74 SO_NODE_DEFINE_ENUM_VALUE(Part, Labels);
75 SO_NODE_DEFINE_ENUM_VALUE(Part, AxisCross);
76 SO_NODE_DEFINE_ENUM_VALUE(Part, PlaneIndicator);
77 SO_NODE_DEFINE_ENUM_VALUE(Part, OriginIndicator);
78 SO_NODE_DEFINE_ENUM_VALUE(Part, AllParts);
79 SO_NODE_SET_SF_ENUM_TYPE(parts, Part);
80
81 SO_NODE_DEFINE_ENUM_VALUE(Axes, X);
82 SO_NODE_DEFINE_ENUM_VALUE(Axes, Y);
83 SO_NODE_DEFINE_ENUM_VALUE(Axes, Z);
84 SO_NODE_DEFINE_ENUM_VALUE(Axes, AllAxes);
85 SO_NODE_SET_SF_ENUM_TYPE(axes, Axes);
86
87 auto root = SO_GET_ANY_PART(this, "root", SoShapeScale);
88 root->scaleFactor.connectFrom(&scaleFactor);
89
90 recomputeGeometry();
91 }
92
93 void SoFCPlacementIndicatorKit::initClass()
94 {
95 SO_KIT_INIT_CLASS(SoFCPlacementIndicatorKit, SoBaseKit, "BaseKit");
96 }
97
98 void SoFCPlacementIndicatorKit::notify(SoNotList* l)
99 {
100 SoField* field = l->getLastField();
101
102 if (field == &parts || field == &axes || field == &axisLength) {
103 // certainly this is not the fastest way to recompute the geometry as it does recreate
104 // everything from the scratch. It is however very easy to implement and this node should
105 // not really change too often so the performance penalty is better than making code that
106 // is harder to maintain.
107 recomputeGeometry();
108 return;
109 }
110
111 SoBaseKit::notify(l);
112 }
113
114 void SoFCPlacementIndicatorKit::recomputeGeometry()
115 {
116 auto root = SO_GET_ANY_PART(this, "root", SoShapeScale);
117 root->setPart("shape", createGeometry());
118 }
119
120 SoSeparator* SoFCPlacementIndicatorKit::createOriginIndicator()
121 {
122 const uint32_t originColor = ViewParams::instance()->getOriginColor();
123
124 auto sep = new SoSeparator();
125
126 auto pcBaseColor = new SoBaseColor();
127 pcBaseColor->rgb.setValue(Base::Color::fromPackedRGBA<SbColor>(originColor));
128
129 auto pcSphere = new SoSphere();
130 // the factor aligns radius of sphere and arrow head visually, without it, it looks too small
131 constexpr float visualAdjustmentFactor = 1.2F;
132 pcSphere->radius = arrowHeadRadius * visualAdjustmentFactor;
133
134 sep->addChild(pcBaseColor);
135 sep->addChild(pcSphere);
136
137 return sep;
138 }
139
140 SoSeparator* SoFCPlacementIndicatorKit::createGeometry()
141 {
142 const uint32_t neutralColor = ViewParams::instance()->getNeutralColor();
143
144 auto sep = new SoSeparator();
145
146 auto pcBaseColor = new SoBaseColor();
147 pcBaseColor->rgb.setValue(Base::Color::fromPackedRGBA<SbColor>(neutralColor));
148
149 auto pcLightModel = new SoLightModel();
150 pcLightModel->model = SoLightModel::BASE_COLOR;
151
152 sep->addChild(pcBaseColor);
153 sep->addChild(pcLightModel);
154
155 if (parts.getValue() & PlaneIndicator) {
156 sep->addChild(createPlaneIndicator());
157 }
158
159 if (parts.getValue() & OriginIndicator) {
160 sep->addChild(createOriginIndicator());
161 }
162
163 if (parts.getValue() & Axes) {
164 sep->addChild(createAxes());
165 }
166
167 return sep;
168 }
169
170 SoSeparator* SoFCPlacementIndicatorKit::createAxes()
171 {
172 const auto cylinderOffset = axisLength.getValue() / 2.F;
173
174 const auto createAxis = [&](const char* label,
175 Base::Vector3d axis,
176 uint32_t packedColor,
177 const double offset) {
178 Base::Color axisColor(packedColor);
179 Base::Rotation rotation(Base::Vector3d::UnitY, axis);
180
181 auto sep = new SoSeparator;
182
183 auto pcTranslate = new SoTransform();
184 pcTranslate->translation.setValue(Base::convertTo<SbVec3f>((cylinderOffset + offset) * axis));
185 pcTranslate->rotation.setValue(Base::convertTo<SbRotation>(rotation));
186
187 auto pcArrowShaft = new SoCylinder();
188 pcArrowShaft->radius = axisThickness / 2.F;
189 pcArrowShaft->height = axisLength;
190
191 if (coloredAxis.getValue()) {
192 auto pcBaseColor = new SoBaseColor();
193 pcBaseColor->rgb.setValue(Base::convertTo<SbColor>(axisColor));
194
195 sep->addChild(pcBaseColor);
196 }
197
198 sep->addChild(pcTranslate);
199 sep->addChild(pcArrowShaft);
200
201 if (parts.getValue() & ArrowHeads) {
202 auto pcArrowHeadTranslation = new SoTranslation();
203 pcArrowHeadTranslation->translation.setValue(0.0, cylinderOffset, 0.0);
204
205 auto pcArrowHead = new SoCone();
206 pcArrowHead->bottomRadius = arrowHeadRadius;
207 pcArrowHead->height = arrowHeadHeight;
208
209 auto pcArrowHeadSeparator = new SoSeparator();
210 pcArrowHeadSeparator->addChild(pcArrowHeadTranslation);
211 pcArrowHeadSeparator->addChild(pcArrowHead);
212
213 sep->addChild(pcArrowHeadSeparator);
214 }
215
216 if (parts.getValue() & Labels) {
217 auto pcLabelSeparator = new SoSeparator();
218
219 auto pcLabelTranslation = new SoTranslation();
220 pcLabelTranslation->translation.setValue(0.0, cylinderOffset + labelOffset, 0.0);
221 pcLabelSeparator->addChild(pcLabelTranslation);
222
223 auto pcAxisLabel = new SoFrameLabel();
224 pcAxisLabel->string.setValue(label);
225 pcAxisLabel->textColor.setValue(1.0, 1.0, 1.0);
226 pcAxisLabel->horAlignment = SoImage::CENTER;
227 pcAxisLabel->vertAlignment = SoImage::HALF;
228 pcAxisLabel->border = false;
229 pcAxisLabel->frame = false;
230 pcAxisLabel->textUseBaseColor = true;
231 pcAxisLabel->size = labelFontSize;
232
233 pcLabelSeparator->addChild(pcAxisLabel);
234
235 sep->addChild(pcLabelSeparator);
236 }
237
238 return sep;
239 };
240
241 double additionalAxisMargin = (parts.getValue() & OriginIndicator) ? axisThickness * 4 : 0;
242 double xyOffset = (parts.getValue() & PlaneIndicator)
243 ? planeIndicatorRadius + planeIndicatorMargin
244 : axisMargin + additionalAxisMargin;
245
246 auto sep = new SoSeparator;
247
248 if (axes.getValue() & X) {
249 sep->addChild(
250 createAxis("X", Base::Vector3d::UnitX, ViewParams::instance()->getAxisXColor(), xyOffset)
251 );
252 }
253
254 if (axes.getValue() & Y) {
255 sep->addChild(
256 createAxis("Y", Base::Vector3d::UnitY, ViewParams::instance()->getAxisYColor(), xyOffset)
257 );
258 }
259
260 if (axes.getValue() & Z) {
261 double zOffset = (parts.getValue() & PlaneIndicator) ? planeIndicatorMargin
262 : axisMargin + additionalAxisMargin;
263
264 sep->addChild(
265 createAxis("Z", Base::Vector3d::UnitZ, ViewParams::instance()->getAxisZColor(), zOffset)
266 );
267 }
268
269 return sep;
270 }
271
272 SoSeparator* SoFCPlacementIndicatorKit::createPlaneIndicator()
273 {
274 // cylinders are aligned with Y axis for some reason
275 auto rotation = Base::Rotation(Base::Vector3d::UnitY, Base::Vector3d::UnitZ);
276
277 auto pcRotation = new SoRotation();
278 pcRotation->rotation = Base::convertTo<SbRotation>(rotation);
279
280 auto pcComplexity = new SoComplexity();
281 pcComplexity->value = 1.0f;
282
283 auto pcCylinder = new SoCylinder();
284 pcCylinder->height = 0.f;
285 pcCylinder->radius = planeIndicatorRadius;
286 pcCylinder->parts = SoCylinder::TOP;
287
288 const auto gray = Base::Color(0.75f, 0.75f, 0.75f);
289
290 auto pcMaterial = new SoMaterial();
291 pcMaterial->diffuseColor.setValue(Base::convertTo<SbColor>(gray));
292 pcMaterial->ambientColor.connectFrom(&pcMaterial->diffuseColor);
293 pcMaterial->transparency = planeIndicatorTransparency;
294
295 auto sep = new SoSeparator;
296
297 sep->addChild(pcRotation);
298 sep->addChild(pcMaterial);
299 sep->addChild(pcComplexity);
300 sep->addChild(pcCylinder);
301
302 return sep;
303 }
304
305 SoFCPlacementIndicatorKit::~SoFCPlacementIndicatorKit() = default;
306
307 } // namespace Gui