Debian Codegraph @

src/Gui/Inventor/Draggers/SoPlanarDragger.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Thomas Anderson <blobfish[at]gmx.com> *
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 <cassert>
24 #include <numbers>
25
26 #include <Inventor/SbRotation.h>
27 #include <Inventor/actions/SoGLRenderAction.h>
28 #include <Inventor/engines/SoComposeVec3f.h>
29 #include <Inventor/nodes/SoLightModel.h>
30 #include <Inventor/nodes/SoDrawStyle.h>
31 #include <Inventor/nodes/SoBaseColor.h>
32 #include <Inventor/nodes/SoCone.h>
33 #include <Inventor/nodes/SoCoordinate3.h>
34 #include <Inventor/nodes/SoCylinder.h>
35 #include <Inventor/nodes/SoCube.h>
36 #include <Inventor/nodes/SoGroup.h>
37 #include <Inventor/nodes/SoLineSet.h>
38 #include <Inventor/nodes/SoOrthographicCamera.h>
39 #include <Inventor/nodes/SoPerspectiveCamera.h>
40 #include <Inventor/nodes/SoPickStyle.h>
41 #include <Inventor/nodes/SoRotation.h>
42 #include <Inventor/nodes/SoSeparator.h>
43 #include <Inventor/nodes/SoSphere.h>
44 #include <Inventor/nodes/SoSwitch.h>
45 #include <Inventor/nodes/SoTranslation.h>
46 #include <Inventor/nodes/SoText2.h>
47 #include <Inventor/nodes/SoAnnotation.h>
48 #include <Inventor/nodes/SoFontStyle.h>
49
50 #include <Base/Quantity.h>
51
52 #include "SoPlanarDragger.h"
53
54 #include "MainWindow.h"
55 #include "SoFCDB.h"
56
57 #include <SoTextLabel.h>
58
59 using namespace Gui;
60
61 SO_KIT_SOURCE(SoPlanarDragger)
62
63 void SoPlanarDragger::initClass()
64 {
65 SO_KIT_INIT_CLASS(SoPlanarDragger, SoDragger, "Dragger");
66 }
67
68 SoPlanarDragger::SoPlanarDragger()
69 {
70 SO_KIT_CONSTRUCTOR(SoPlanarDragger);
71 #if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
72 this->ref();
73 #endif
74
75 SO_KIT_ADD_CATALOG_ENTRY(planarTranslatorSwitch, SoSwitch, TRUE, geomSeparator, "", TRUE);
76 SO_KIT_ADD_CATALOG_ENTRY(planarTranslator, SoSeparator, TRUE, planarTranslatorSwitch, "", TRUE);
77 SO_KIT_ADD_CATALOG_ENTRY(planarTranslatorActive, SoSeparator, TRUE, planarTranslatorSwitch, "", TRUE);
78
79 if (SO_KIT_IS_FIRST_INSTANCE()) {
80 buildFirstInstance();
81 }
82
83 SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0));
84 SO_KIT_ADD_FIELD(translationIncrement, (1.0));
85 SO_KIT_ADD_FIELD(translationIncrementXCount, (0));
86 SO_KIT_ADD_FIELD(translationIncrementYCount, (0));
87 SO_KIT_ADD_FIELD(autoScaleResult, (1.0));
88
89 SO_KIT_INIT_INSTANCE();
90
91 // initialize default parts.
92 // first is from 'SO_KIT_CATALOG_ENTRY_HEADER' macro
93 // second is unique name from buildFirstInstance().
94 this->setPartAsDefault("planarTranslator", "CSysDynamics_TPlanarDragger_Translator");
95 this->setPartAsDefault("planarTranslatorActive", "CSysDynamics_TPlanarDragger_TranslatorActive");
96
97 SoSwitch* sw = SO_GET_ANY_PART(this, "planarTranslatorSwitch", SoSwitch);
98 SoInteractionKit::setSwitchValue(sw, 0);
99
100 this->addStartCallback(&SoPlanarDragger::startCB);
101 this->addMotionCallback(&SoPlanarDragger::motionCB);
102 this->addFinishCallback(&SoPlanarDragger::finishCB);
103
104 addValueChangedCallback(&SoPlanarDragger::valueChangedCB);
105
106 fieldSensor.setFunction(&SoPlanarDragger::fieldSensorCB);
107 fieldSensor.setData(this);
108 fieldSensor.setPriority(0);
109
110 this->setUpConnections(TRUE, TRUE);
111 }
112
113 SoPlanarDragger::~SoPlanarDragger()
114 {
115 fieldSensor.setData(nullptr);
116 fieldSensor.detach();
117
118 this->removeStartCallback(&SoPlanarDragger::startCB);
119 this->removeMotionCallback(&SoPlanarDragger::motionCB);
120 this->removeFinishCallback(&SoPlanarDragger::finishCB);
121 removeValueChangedCallback(&SoPlanarDragger::valueChangedCB);
122 }
123
124 void SoPlanarDragger::buildFirstInstance()
125 {
126 SoGroup* geometryGroup = buildGeometry();
127
128 auto localTranslator = new SoSeparator();
129 localTranslator->setName("CSysDynamics_TPlanarDragger_Translator");
130 localTranslator->addChild(geometryGroup);
131 SoFCDB::getStorage()->addChild(localTranslator);
132
133 auto localTranslatorActive = new SoSeparator();
134 localTranslatorActive->setName("CSysDynamics_TPlanarDragger_TranslatorActive");
135 auto colorActive = new SoBaseColor();
136 colorActive->rgb.setValue(1.0, 1.0, 0.0);
137 localTranslatorActive->addChild(colorActive);
138 localTranslatorActive->addChild(geometryGroup);
139 SoFCDB::getStorage()->addChild(localTranslatorActive);
140 }
141
142 SoGroup* SoPlanarDragger::buildGeometry()
143 {
144 auto root = new SoGroup();
145
146 float cubeWidthHeight = 2.0;
147 float cubeDepth = 0.1f;
148
149 auto translation = new SoTranslation();
150 translation->translation.setValue(cubeWidthHeight + 0.15, cubeWidthHeight + 0.15, 0.0);
151 root->addChild(translation);
152
153 auto pickStyle = new SoPickStyle();
154 pickStyle->style.setValue(SoPickStyle::SHAPE_ON_TOP);
155 pickStyle->setOverride(TRUE);
156 root->addChild(pickStyle);
157
158 auto lightModel = new SoLightModel();
159 lightModel->model = SoLightModel::BASE_COLOR;
160 root->addChild(lightModel);
161
162 auto cube = new SoCube();
163 cube->width.setValue(cubeWidthHeight);
164 cube->height.setValue(cubeWidthHeight);
165 cube->depth.setValue(cubeDepth);
166 root->addChild(cube);
167
168 return root;
169 }
170
171 void SoPlanarDragger::startCB(void*, SoDragger* d)
172 {
173 auto sudoThis = static_cast<SoPlanarDragger*>(d);
174 assert(sudoThis);
175 sudoThis->dragStart();
176 }
177
178 void SoPlanarDragger::motionCB(void*, SoDragger* d)
179 {
180 auto sudoThis = static_cast<SoPlanarDragger*>(d);
181 assert(sudoThis);
182 sudoThis->drag();
183 }
184
185 void SoPlanarDragger::finishCB(void*, SoDragger* d)
186 {
187 auto sudoThis = static_cast<SoPlanarDragger*>(d);
188 assert(sudoThis);
189 sudoThis->dragFinish();
190 }
191
192 void SoPlanarDragger::fieldSensorCB(void* f, SoSensor*)
193 {
194 auto sudoThis = static_cast<SoPlanarDragger*>(f);
195
196 if (!f) {
197 return;
198 }
199
200 SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft
201 sudoThis->workFieldsIntoTransform(matrix);
202 sudoThis->setMotionMatrix(matrix);
203 }
204
205 void SoPlanarDragger::valueChangedCB(void*, SoDragger* d)
206 {
207 auto sudoThis = dynamic_cast<SoPlanarDragger*>(d);
208 assert(sudoThis);
209 SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft
210
211 // all this just to get the translation?
212 SbVec3f trans, scaleDummy;
213 SbRotation rotationDummy, scaleOrientationDummy;
214 matrix.getTransform(trans, rotationDummy, scaleDummy, scaleOrientationDummy);
215
216 sudoThis->fieldSensor.detach();
217 if (sudoThis->translation.getValue() != trans) {
218 sudoThis->translation = trans;
219 }
220 sudoThis->fieldSensor.attach(&sudoThis->translation);
221 }
222
223 void SoPlanarDragger::dragStart()
224 {
225 SoSwitch* sw;
226 sw = SO_GET_ANY_PART(this, "planarTranslatorSwitch", SoSwitch);
227 SoInteractionKit::setSwitchValue(sw, 1);
228
229 projector.setViewVolume(this->getViewVolume());
230 projector.setWorkingSpace(this->getLocalToWorldMatrix());
231 projector.setPlane(SbPlane(SbVec3f(0.0, 0.0, 0.0), SbVec3f(1.0, 0.0, 0.0), SbVec3f(0.0, 1.0, 0.0)));
232 SbVec3f hitPoint = projector.project(getNormalizedLocaterPosition());
233
234 SbMatrix localToWorld = getLocalToWorldMatrix();
235 localToWorld.multVecMatrix(hitPoint, hitPoint);
236 setStartingPoint((hitPoint));
237
238 translationIncrementXCount.setValue(0);
239 translationIncrementYCount.setValue(0);
240 }
241
242 void SoPlanarDragger::drag()
243 {
244 projector.setViewVolume(this->getViewVolume());
245 projector.setWorkingSpace(this->getLocalToWorldMatrix());
246
247 SbVec3f hitPoint = projector.project(getNormalizedLocaterPosition());
248 SbVec3f startingPoint = getLocalStartingPoint();
249 SbVec3f localMovement = hitPoint - startingPoint;
250
251 // scale the increment to match local space.
252 float scaledIncrement = static_cast<float>(translationIncrement.getValue())
253 / autoScaleResult.getValue();
254
255 localMovement = roundTranslation(localMovement, scaledIncrement);
256 // when the movement vector is null either the appendTranslation or
257 // the setMotionMatrix doesn't work. either way it stops translating
258 // back to its initial starting point.
259 if (localMovement.equals(SbVec3f(0.0, 0.0, 0.0), 0.00001f)) {
260 setMotionMatrix(getStartMotionMatrix());
261 // don't know why I need the following but if I don't have it
262 // it won't return to original position.
263 this->valueChanged();
264 }
265 else {
266 setMotionMatrix(appendTranslation(getStartMotionMatrix(), localMovement));
267 }
268
269 Base::Quantity quantityX(
270 static_cast<double>(translationIncrementXCount.getValue()) * translationIncrement.getValue(),
271 Base::Unit::Length
272 );
273 Base::Quantity quantityY(
274 static_cast<double>(translationIncrementYCount.getValue()) * translationIncrement.getValue(),
275 Base::Unit::Length
276 );
277
278 QString message = QStringLiteral("%1 %2, %3")
279 .arg(
280 QObject::tr("Translation XY:"),
281 QString::fromStdString(quantityX.getUserString()),
282 QString::fromStdString(quantityY.getUserString())
283 );
284 getMainWindow()->showMessage(message, 3000);
285 }
286
287 void SoPlanarDragger::dragFinish()
288 {
289 SoSwitch* sw;
290 sw = SO_GET_ANY_PART(this, "planarTranslatorSwitch", SoSwitch);
291 SoInteractionKit::setSwitchValue(sw, 0);
292 }
293
294 SbBool SoPlanarDragger::setUpConnections(SbBool onoff, SbBool doitalways)
295 {
296 if (!doitalways && this->connectionsSetUp == onoff) {
297 return onoff;
298 }
299
300 SbBool oldval = this->connectionsSetUp;
301
302 if (onoff) {
303 inherited::setUpConnections(onoff, doitalways);
304 SoPlanarDragger::fieldSensorCB(this, nullptr);
305 if (this->fieldSensor.getAttachedField() != &this->translation) {
306 this->fieldSensor.attach(&this->translation);
307 }
308 }
309 else {
310 if (this->fieldSensor.getAttachedField()) {
311 this->fieldSensor.detach();
312 }
313 inherited::setUpConnections(onoff, doitalways);
314 }
315 this->connectionsSetUp = onoff;
316 return oldval;
317 }
318
319 SbVec3f SoPlanarDragger::roundTranslation(const SbVec3f& vecIn, float incrementIn)
320 {
321 int xCount = 0;
322 float xValue = vecIn[0];
323
324 if (fabs(xValue) > (incrementIn / 2.0)) {
325 xCount = static_cast<int>(xValue / incrementIn);
326 float remainder = fmod(xValue, incrementIn);
327 if (remainder >= (incrementIn / 2.0)) {
328 xCount++;
329 }
330 }
331
332 translationIncrementXCount.setValue(xCount);
333
334 int yCount = 0;
335 float yValue = vecIn[1];
336
337 if (fabs(yValue) > (incrementIn / 2.0)) {
338 yCount = static_cast<int>(yValue / incrementIn);
339 float remainder = fmod(yValue, incrementIn);
340 if (remainder >= (incrementIn / 2.0)) {
341 yCount++;
342 }
343 }
344
345 translationIncrementYCount.setValue(yCount);
346
347 SbVec3f out;
348 out[0] = static_cast<float>(xCount) * incrementIn;
349 out[1] = static_cast<float>(yCount) * incrementIn;
350 out[2] = 0.0;
351
352 return out;
353 }