Debian Codegraph @

src/Gui/Inventor/Draggers/SoLinearDraggerGeometry.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 <Inventor/nodes/SoBaseColor.h>
25 #include <Inventor/nodes/SoCone.h>
26 #include <Inventor/nodes/SoCylinder.h>
27 #include <Inventor/nodes/SoLightModel.h>
28 #include <Inventor/nodes/SoPickStyle.h>
29 #include <Inventor/nodes/SoTranslation.h>
30
31 #include "SoLinearDraggerGeometry.h"
32
33 using namespace Gui;
34
35 SO_KIT_SOURCE(SoLinearGeometryKit)
36
37 void SoLinearGeometryKit::initClass()
38 {
39 SO_KIT_INIT_CLASS(SoLinearGeometryKit, SoBaseKit, "BaseKit");
40 }
41
42 SoLinearGeometryKit::SoLinearGeometryKit()
43 {
44 SO_KIT_CONSTRUCTOR(SoLinearGeometryKit);
45
46 SO_KIT_ADD_FIELD(tipPosition, (0.0, 0.0, 0.0));
47
48 SO_KIT_INIT_INSTANCE();
49 }
50
51 SO_KIT_SOURCE(SoArrowGeometry)
52
53 void SoArrowGeometry::initClass()
54 {
55 SO_KIT_INIT_CLASS(SoArrowGeometry, SoLinearGeometryKit, "LinearGeometryKit");
56 }
57
58 SoArrowGeometry::SoArrowGeometry()
59 {
60 SO_KIT_CONSTRUCTOR(SoArrowGeometry);
61 SO_KIT_ADD_CATALOG_ENTRY(separator, SoSeparator, false, this, "", false);
62 SO_KIT_ADD_CATALOG_ENTRY(lightModel, SoLightModel, false, separator, "", false);
63 SO_KIT_ADD_CATALOG_ENTRY(pickStyle, SoPickStyle, false, separator, "", false);
64 SO_KIT_ADD_CATALOG_ENTRY(arrowBody, SoCylinder, false, separator, "", true);
65 SO_KIT_ADD_CATALOG_ENTRY(arrowTip, SoCone, false, separator, "", true);
66
67 SO_KIT_ADD_CATALOG_ENTRY(_arrowBodyTranslation, SoTranslation, false, separator, arrowBody, false);
68 SO_KIT_ADD_CATALOG_ENTRY(_arrowTipTranslation, SoTranslation, false, separator, arrowTip, false);
69
70 SO_KIT_ADD_FIELD(coneBottomRadius, (0.8f));
71 SO_KIT_ADD_FIELD(coneHeight, (2.5f));
72 SO_KIT_ADD_FIELD(cylinderHeight, (10.0f));
73 SO_KIT_ADD_FIELD(cylinderRadius, (0.1f));
74
75 SO_KIT_INIT_INSTANCE();
76
77 auto arrowBody = SO_GET_ANY_PART(this, "arrowBody", SoCylinder);
78 arrowBody->height.connectFrom(&cylinderHeight);
79 arrowBody->radius.connectFrom(&cylinderRadius);
80
81 auto arrowTip = SO_GET_ANY_PART(this, "arrowTip", SoCone);
82 arrowTip->height.connectFrom(&coneHeight);
83 arrowTip->bottomRadius.connectFrom(&coneBottomRadius);
84
85 auto lightModel = SO_GET_ANY_PART(this, "lightModel", SoLightModel);
86 lightModel->model = SoLightModel::BASE_COLOR;
87
88 auto pickStyle = SO_GET_ANY_PART(this, "pickStyle", SoPickStyle);
89 pickStyle->style = SoPickStyle::SHAPE_ON_TOP;
90
91 // forces the notify method to get called so that the initial translations and tipPostion are set
92 cylinderHeight.touch();
93 }
94
95 void SoArrowGeometry::notify(SoNotList* notList)
96 {
97 SoField* lastField = notList->getLastField();
98
99 if (lastField == &cylinderHeight) {
100 auto translation = SO_GET_ANY_PART(this, "_arrowBodyTranslation", SoTranslation);
101 translation->translation = SbVec3f(0, cylinderHeight.getValue() / 2.0f, 0);
102 }
103
104 if (lastField == &coneHeight || lastField == &cylinderHeight) {
105 auto translation = SO_GET_ANY_PART(this, "_arrowTipTranslation", SoTranslation);
106 translation->translation
107 = SbVec3f(0, (cylinderHeight.getValue() + coneHeight.getValue()) / 2.0f, 0);
108
109 tipPosition = {0, cylinderHeight.getValue() + 1.5f * coneHeight.getValue(), 0};
110 }
111 }
112
113 SO_KIT_SOURCE(SoLinearGeometryBaseKit)
114
115 void SoLinearGeometryBaseKit::initClass()
116 {
117 SO_KIT_INIT_CLASS(SoLinearGeometryBaseKit, SoBaseKit, "BaseKit");
118 }
119
120 SoLinearGeometryBaseKit::SoLinearGeometryBaseKit()
121 {
122 SO_KIT_CONSTRUCTOR(SoLinearGeometryBaseKit);
123
124 SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0));
125 SO_KIT_ADD_FIELD(geometryScale, (1.0, 1.0, 1.0));
126 SO_KIT_ADD_FIELD(active, (false));
127
128 SO_KIT_INIT_INSTANCE();
129 }
130
131 SO_KIT_SOURCE(SoArrowBase)
132
133 void SoArrowBase::initClass()
134 {
135 SO_KIT_INIT_CLASS(SoArrowBase, SoLinearGeometryBaseKit, "LinearGeometryBaseKit");
136 }
137
138 SoArrowBase::SoArrowBase()
139 {
140 SO_KIT_CONSTRUCTOR(SoArrowBase);
141 SO_KIT_ADD_CATALOG_ENTRY(separator, SoSeparator, false, this, "", false);
142 SO_KIT_ADD_CATALOG_ENTRY(lightModel, SoLightModel, false, separator, "", false);
143 SO_KIT_ADD_CATALOG_ENTRY(pickStyle, SoPickStyle, false, separator, "", false);
144 SO_KIT_ADD_CATALOG_ENTRY(baseColor, SoBaseColor, false, separator, "", true);
145 SO_KIT_ADD_CATALOG_ENTRY(cylinder, SoCylinder, false, separator, "", true);
146
147 SO_KIT_ADD_CATALOG_ENTRY(_cylinderTranslation, SoTranslation, false, separator, cylinder, false);
148
149 SO_KIT_ADD_FIELD(cylinderHeight, (1.0));
150 SO_KIT_ADD_FIELD(cylinderRadius, (0.15));
151 SO_KIT_ADD_FIELD(color, (0, 0, 1));
152
153 SO_KIT_INIT_INSTANCE();
154
155 auto lightModel = SO_GET_ANY_PART(this, "lightModel", SoLightModel);
156 lightModel->model = SoLightModel::BASE_COLOR;
157
158 auto pickStyle = SO_GET_ANY_PART(this, "pickStyle", SoPickStyle);
159 pickStyle->style = SoPickStyle::UNPICKABLE;
160
161 // We don't want to change the color of the base geometry when the dragger is
162 // dragged so the active field from SoLinearGeometryBaseKit is unused
163 // If that is desired then just add a toggle switch with a colour after the
164 // baseColor node in the graph and it will work as expected
165 auto baseColor = SO_GET_ANY_PART(this, "baseColor", SoBaseColor);
166 baseColor->rgb.connectFrom(&color);
167
168 // Forces the cylinder dimensions to be computed
169 cylinderHeight.touch();
170 cylinderRadius.touch();
171 }
172
173 void SoArrowBase::notify(SoNotList* notList)
174 {
175 SoField* lastField = notList->getLastField();
176
177 if (lastField == &cylinderHeight || lastField == &translation) {
178 auto cylinder = SO_GET_ANY_PART(this, "cylinder", SoCylinder);
179 cylinder->height = cylinderHeight.getValue() * translation.getValue()[1];
180
181 auto cylinderTranslation = SO_GET_ANY_PART(this, "_cylinderTranslation", SoTranslation);
182 cylinderTranslation->translation = {0, cylinder->height.getValue() / 2, 0};
183 }
184 else if (lastField == &cylinderRadius || lastField == &geometryScale) {
185 auto cylinder = SO_GET_ANY_PART(this, "cylinder", SoCylinder);
186 assert(
187 geometryScale.getValue()[0] == geometryScale.getValue()[1]
188 && geometryScale.getValue()[1] == geometryScale.getValue()[2]
189 && "Camera scale should be equal along all three axes"
190 );
191 cylinder->radius = cylinderRadius.getValue() * geometryScale.getValue()[0];
192 }
193 }