Debian Codegraph @

src/Gui/SoDatumLabel.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2011-2012 Luke Parry <[email protected]> *
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
24 #include <FCConfig.h>
25
26 #ifdef FC_OS_WIN32
27 # include <windows.h>
28 # undef min
29 # undef max
30 #endif
31 #ifdef FC_OS_MACOSX
32 # include <OpenGL/gl.h>
33 #else
34 # include <GL/gl.h>
35 #endif
36
37 #include <algorithm>
38 #include <cmath>
39 #include <limits>
40 #include <numbers>
41 #include <QFontMetrics>
42 #include <QPainter>
43
44 #include <Inventor/SoPrimitiveVertex.h>
45 #include <Inventor/actions/SoGLRenderAction.h>
46 #include <Inventor/elements/SoFocalDistanceElement.h>
47 #include <Inventor/elements/SoViewportRegionElement.h>
48 #include <Inventor/elements/SoViewVolumeElement.h>
49 #include <Inventor/misc/SoState.h>
50
51 #include <Base/Tools.h>
52
53 #include <Gui/BitmapFactory.h>
54 #include <Gui/Tools.h>
55
56 #include "SoDatumLabel.h"
57
58 // NOLINTBEGIN(readability-magic-numbers,cppcoreguidelines-pro-bounds-pointer-arithmetic)
59 constexpr const float ZCONSTR {0.006F};
60 // Z offset for arrowheads and text to render them ON TOP of geometry lines.
61 // Geometry lines are at Z ~0.005-0.008, so this offset ensures arrowheads
62 // and text selection primitives are above them and remain selectable.
63 constexpr const float ZARROW_TEXT_OFFSET {0.010F};
64
65 using namespace Gui;
66
67 // ------------------------------------------------------
68
69
70 namespace
71 {
72
73 void glVertex(const SbVec3f& pt)
74 {
75 glVertex3f(pt[0], pt[1], pt[2]);
76 }
77
78 void glVertexes(const std::vector<SbVec3f>& pts)
79 {
80 for (auto pt : pts) {
81 glVertex3f(pt[0], pt[1], pt[2]);
82 }
83 }
84
85 void glDrawLine(const SbVec3f& p1, const SbVec3f& p2)
86 {
87 glBegin(GL_LINES);
88 glVertexes({p1, p2});
89 glEnd();
90 }
91
92 void glDrawArc(
93 const SbVec3f& center,
94 float radius,
95 float startAngle = 0.,
96 float endAngle = 2.0 * std::numbers::pi,
97 int countSegments = 0
98 )
99 {
100 float range = endAngle - startAngle;
101
102 if (countSegments == 0) {
103 countSegments = std::max(6, abs(int(25.0 * range / std::numbers::pi)));
104 }
105
106 float segment = range / (countSegments - 1);
107
108 glBegin(GL_LINE_STRIP);
109 for (int i = 0; i < countSegments; i++) {
110 float theta = startAngle + segment * i;
111 SbVec3f v1 = center + radius * SbVec3f(cos(theta), sin(theta), 0);
112 glVertex(v1);
113 }
114 glEnd();
115 }
116
117 void glDrawArrow(const SbVec3f& base, const SbVec3f& dir, float width, float length)
118 {
119 // Calculate arrowhead points
120 SbVec3f normal(dir[1], -dir[0], 0);
121 SbVec3f arrowLeft = base - length * dir + width * normal;
122 SbVec3f arrowRight = base - length * dir - width * normal;
123
124 // Draw arrowheads at elevated Z to render ON TOP of geometry lines
125 glBegin(GL_TRIANGLES);
126 glVertex3f(base[0], base[1], ZARROW_TEXT_OFFSET);
127 glVertex3f(arrowLeft[0], arrowLeft[1], ZARROW_TEXT_OFFSET);
128 glVertex3f(arrowRight[0], arrowRight[1], ZARROW_TEXT_OFFSET);
129 glEnd();
130 }
131
132 } // namespace
133
134
135 SO_NODE_SOURCE(SoDatumLabel)
136
137 void SoDatumLabel::initClass()
138 {
139 SO_NODE_INIT_CLASS(SoDatumLabel, SoShape, "Shape");
140 }
141
142 // NOLINTNEXTLINE
143 SoDatumLabel::SoDatumLabel()
144 {
145 SO_NODE_CONSTRUCTOR(SoDatumLabel);
146 SO_NODE_ADD_FIELD(string, (""));
147 SO_NODE_ADD_FIELD(textColor, (SbVec3f(1.0F, 1.0F, 1.0F)));
148 SO_NODE_ADD_FIELD(pnts, (SbVec3f(.0F, .0F, .0F)));
149 SO_NODE_ADD_FIELD(norm, (SbVec3f(.0F, .0F, 1.F)));
150 SO_NODE_ADD_FIELD(strikethrough, (false));
151
152 SO_NODE_ADD_FIELD(name, ("Helvetica"));
153 SO_NODE_ADD_FIELD(size, (10.F));
154 SO_NODE_ADD_FIELD(lineWidth, (2.F));
155 SO_NODE_ADD_FIELD(sampling, (2.F));
156
157 SO_NODE_ADD_FIELD(datumtype, (SoDatumLabel::DISTANCE));
158
159 SO_NODE_DEFINE_ENUM_VALUE(Type, DISTANCE);
160 SO_NODE_DEFINE_ENUM_VALUE(Type, DISTANCEX);
161 SO_NODE_DEFINE_ENUM_VALUE(Type, DISTANCEY);
162 SO_NODE_DEFINE_ENUM_VALUE(Type, ANGLE);
163 SO_NODE_DEFINE_ENUM_VALUE(Type, RADIUS);
164 SO_NODE_DEFINE_ENUM_VALUE(Type, DIAMETER);
165 SO_NODE_DEFINE_ENUM_VALUE(Type, ARCLENGTH);
166 SO_NODE_SET_SF_ENUM_TYPE(datumtype, Type);
167
168 SO_NODE_ADD_FIELD(param1, (0.F));
169 SO_NODE_ADD_FIELD(param2, (0.F));
170 SO_NODE_ADD_FIELD(param4, (0.F));
171 SO_NODE_ADD_FIELD(param5, (0.F));
172 SO_NODE_ADD_FIELD(param6, (0.F));
173 SO_NODE_ADD_FIELD(param7, (0.F));
174 SO_NODE_ADD_FIELD(param8, (0.F));
175
176 useAntialiasing = true;
177
178 this->imgWidth = 0;
179 this->imgHeight = 0;
180 this->glimagevalid = false;
181 }
182
183 void SoDatumLabel::drawImage()
184 {
185 const SbString* s = string.getValues(0);
186 int num = string.getNum();
187 if (num == 0) {
188 this->image = SoSFImage();
189 return;
190 }
191
192 QFont font(QString::fromLatin1(name.getValue(), -1), size.getValue());
193 QFontMetrics fm(font);
194 QString str = QString::fromUtf8(s[0].getString());
195 QRect rect = fm.boundingRect(str);
196
197 int w = Gui::QtTools::horizontalAdvance(fm, str);
198 int h = fm.height();
199
200 // No Valid text
201 if (!w) {
202 this->image = SoSFImage();
203 return;
204 }
205
206 const SbColor& t = textColor.getValue();
207 QColor front;
208 front.setRgbF(t[0], t[1], t[2]);
209
210 QImage image(w * sampling.getValue(), h * sampling.getValue(), QImage::Format_ARGB32_Premultiplied);
211 image.setDevicePixelRatio(sampling.getValue());
212 image.fill(0x00000000);
213
214 QPainter painter(&image);
215 if (useAntialiasing) {
216 painter.setRenderHint(QPainter::Antialiasing);
217 }
218
219 painter.setPen(QPen(front, 2));
220 painter.setFont(font);
221 painter.drawText(0, fm.ascent() + rect.y(), w, rect.height(), Qt::AlignLeft, str);
222 if (strikethrough.getValue()) {
223 int strikepos = fm.ascent() - fm.strikeOutPos();
224 painter.drawLine(0, strikepos, w, strikepos);
225 }
226 painter.end();
227
228 Gui::BitmapFactory().convert(image, this->image);
229 }
230
231 namespace Gui
232 {
233 // helper class to determine the bounding box of a datum label
234 class DatumLabelBox
235 {
236 public:
237 DatumLabelBox(float scale, SoDatumLabel* label)
238 : scale {scale}
239 , label {label}
240 {}
241 void computeBBox(SbBox3f& box, SbVec3f& center) const
242 {
243 std::vector<SbVec3f> corners;
244 if (label->datumtype.getValue() == SoDatumLabel::DISTANCE
245 || label->datumtype.getValue() == SoDatumLabel::DISTANCEX
246 || label->datumtype.getValue() == SoDatumLabel::DISTANCEY) {
247 corners = computeDistanceBBox();
248 }
249 else if (label->datumtype.getValue() == SoDatumLabel::RADIUS
250 || label->datumtype.getValue() == SoDatumLabel::DIAMETER) {
251 corners = computeRadiusDiameterBBox();
252 }
253 else if (label->datumtype.getValue() == SoDatumLabel::ANGLE) {
254 corners = computeAngleBBox();
255 }
256 else if (label->datumtype.getValue() == SoDatumLabel::SYMMETRIC) {
257 corners = computeSymmetricBBox();
258 }
259 else if (label->datumtype.getValue() == SoDatumLabel::ARCLENGTH) {
260 corners = computeArcLengthBBox();
261 }
262
263 getBBox(corners, box, center);
264 }
265
266 private:
267 void getBBox(const std::vector<SbVec3f>& corners, SbBox3f& box, SbVec3f& center) const
268 {
269 constexpr float floatMax = std::numeric_limits<float>::max();
270 if (corners.size() > 1) {
271 float minX = floatMax;
272 float minY = floatMax;
273 float maxX = -floatMax;
274 float maxY = -floatMax;
275 for (SbVec3f it : corners) {
276 minX = (it[0] < minX) ? it[0] : minX;
277 minY = (it[1] < minY) ? it[1] : minY;
278 maxX = (it[0] > maxX) ? it[0] : maxX;
279 maxY = (it[1] > maxY) ? it[1] : maxY;
280 }
281
282 // Store the bounding box
283 box.setBounds(SbVec3f(minX, minY, 0.0F), SbVec3f(maxX, maxY, 0.0F));
284 center = box.getCenter();
285 }
286 }
287 std::vector<SbVec3f> computeDistanceBBox() const
288 {
289 SbVec2s imgsize;
290 int nc {};
291 int srcw = 1;
292 int srch = 1;
293
294 const unsigned char* dataptr = label->image.getValue(imgsize, nc);
295 if (dataptr) {
296 srcw = imgsize[0];
297 srch = imgsize[1];
298 }
299
300 float aspectRatio = (float)srcw / (float)srch;
301 float imgHeight = scale * (float)(srch);
302 float imgWidth = aspectRatio * imgHeight;
303
304 // get the points stored in the pnt field
305 const SbVec3f* points = label->pnts.getValues(0);
306 if (label->pnts.getNum() < 2) {
307 return {};
308 }
309
310 // use the shared geometry calculation for consistency
311 SoDatumLabel::DistanceGeometry geom = label->calculateDistanceGeometry(points);
312
313 std::vector<SbVec3f> corners;
314 float margin = imgHeight / 4.0F;
315
316 // include main points and extension line endpoints
317 corners.push_back(geom.p1);
318 corners.push_back(geom.p2);
319 corners.push_back(geom.perp1);
320 corners.push_back(geom.perp2);
321
322 // include text label area
323 corners.push_back(
324 geom.textOffset + geom.dir * (imgWidth / 2.0F + margin) + geom.normal * (srch + margin)
325 );
326 corners.push_back(
327 geom.textOffset - geom.dir * (imgWidth / 2.0F + margin) + geom.normal * (srch + margin)
328 );
329 corners.push_back(
330 geom.textOffset + geom.dir * (imgWidth / 2.0F + margin) - geom.normal * margin
331 );
332 corners.push_back(
333 geom.textOffset - geom.dir * (imgWidth / 2.0F + margin) - geom.normal * margin
334 );
335
336 // include arrow head positions for better selection
337 // arrows are positioned at dimension line endpoints (par1, par4)
338 corners.push_back(geom.par1);
339 corners.push_back(geom.par4);
340 corners.push_back(geom.ar1);
341 corners.push_back(geom.ar2);
342 corners.push_back(geom.ar3);
343 corners.push_back(geom.ar4);
344
345 return corners;
346 }
347
348 std::vector<SbVec3f> computeRadiusDiameterBBox() const
349 {
350 // get the points stored in the pnt field
351 const SbVec3f* points = label->pnts.getValues(0);
352 if (label->pnts.getNum() < 2) {
353 return {};
354 }
355
356 // use the shared geometry calculation for consistency
357 SoDatumLabel::DiameterGeometry geom = label->calculateDiameterGeometry(points);
358
359 std::vector<SbVec3f> corners;
360
361 // include main points and line segment points around text
362 corners.push_back(geom.p1);
363 corners.push_back(geom.p2);
364 corners.push_back(geom.pnt1);
365 corners.push_back(geom.pnt2);
366
367 // include arrow head positions for better selection
368 // first arrow head at p2
369 corners.push_back(geom.ar0);
370 corners.push_back(geom.ar1);
371 corners.push_back(geom.ar2);
372
373 // second arrow head for diameter (if applicable)
374 if (geom.isDiameter) {
375 corners.push_back(geom.ar0_1);
376 corners.push_back(geom.ar1_1);
377 corners.push_back(geom.ar2_1);
378 }
379
380 // sample points along the arc helper
381 constexpr int numArcSamples = 6;
382
383 const auto includeArcHelper = [&corners, &geom](float startAngle, float range) {
384 if (range != 0.0) {
385 for (int i = 0; i <= numArcSamples; i++) {
386 float t = static_cast<float>(i) / static_cast<float>(numArcSamples);
387 float angle = startAngle + t * range;
388 SbVec3f arcPoint = geom.center
389 + SbVec3f(geom.radius * cos(angle), geom.radius * sin(angle), 0);
390 corners.push_back(arcPoint);
391 }
392 }
393 };
394
395 includeArcHelper(geom.startAngle, geom.startRange);
396 includeArcHelper(geom.endAngle, geom.endRange);
397
398 return corners;
399 }
400
401 std::vector<SbVec3f> computeAngleBBox() const
402 {
403 SbVec2s imgsize;
404 int nc {};
405 int srcw = 1;
406 int srch = 1;
407
408 const unsigned char* dataptr = label->image.getValue(imgsize, nc);
409 if (dataptr) {
410 srcw = imgsize[0];
411 srch = imgsize[1];
412 }
413
414 float aspectRatio = (float)srcw / (float)srch;
415 float imgHeight = scale * (float)(srch);
416 float imgWidth = aspectRatio * imgHeight;
417
418 // get the points stored in the pnt field
419 const SbVec3f* points = label->pnts.getValues(0);
420 if (label->pnts.getNum() < 1) {
421 return {};
422 }
423
424 // use the shared geometry calculation for consistency
425 SoDatumLabel::AngleGeometry geom = label->calculateAngleGeometry(points);
426
427 std::vector<SbVec3f> corners;
428
429 // include extension line endpoints
430 corners.push_back(geom.pnt1);
431 corners.push_back(geom.pnt2);
432 corners.push_back(geom.pnt3);
433 corners.push_back(geom.pnt4);
434
435 // include text label area
436 SbVec3f img1 = SbVec3f(-imgWidth / 2.0F, -imgHeight / 2, 0.0F);
437 SbVec3f img2 = SbVec3f(-imgWidth / 2.0F, imgHeight / 2, 0.0F);
438 SbVec3f img3 = SbVec3f(imgWidth / 2.0F, -imgHeight / 2, 0.0F);
439 SbVec3f img4 = SbVec3f(imgWidth / 2.0F, imgHeight / 2, 0.0F);
440
441 img1 += geom.textOffset;
442 img2 += geom.textOffset;
443 img3 += geom.textOffset;
444 img4 += geom.textOffset;
445
446 corners.push_back(img1);
447 corners.push_back(img2);
448 corners.push_back(img3);
449 corners.push_back(img4);
450
451 // include arrow head positions for better selection
452 corners.push_back(geom.startArrowBase);
453 corners.push_back(geom.endArrowBase);
454
455 // include arrow tips (base + direction * length)
456 corners.push_back(geom.startArrowBase + geom.dirStart * geom.arrowLength);
457 corners.push_back(geom.endArrowBase + geom.dirEnd * geom.arrowLength);
458
459 return corners;
460 }
461
462 std::vector<SbVec3f> computeSymmetricBBox() const
463 {
464 // get the points stored in the pnt field
465 const SbVec3f* points = label->pnts.getValues(0);
466 if (label->pnts.getNum() < 2) {
467 return {};
468 }
469
470 // use shared geometry calculation
471 SoDatumLabel::SymmetricGeometry geom = label->calculateSymmetricGeometry(points);
472
473 // include all visual elements in bounding box
474 std::vector<SbVec3f> corners;
475
476 // main points (existing)
477 corners.push_back(geom.p1);
478 corners.push_back(geom.p2);
479
480 // first arrow triangle points
481 corners.push_back(geom.ar0); // arrow tip
482 corners.push_back(geom.ar1); // arrow base point 1
483 corners.push_back(geom.ar2); // arrow base point 2
484
485 // second arrow triangle points
486 corners.push_back(geom.ar3); // arrow tip
487 corners.push_back(geom.ar4); // arrow base point 1
488 corners.push_back(geom.ar5); // arrow base point 2
489
490 return corners;
491 }
492
493 std::vector<SbVec3f> computeArcLengthBBox() const
494 {
495 // get the points stored in the pnt field
496 const SbVec3f* points = label->pnts.getValues(0);
497 if (label->pnts.getNum() < 3) {
498 return {};
499 }
500
501 // use shared geometry calculation
502 SoDatumLabel::ArcLengthGeometry geom = label->calculateArcLengthGeometry(points);
503
504 // get text area for existing text coverage
505 SbVec2s imgsize;
506 int nc {};
507 int srcw = 1;
508 int srch = 1;
509
510 const unsigned char* dataptr = label->image.getValue(imgsize, nc);
511 if (dataptr) {
512 srcw = imgsize[0];
513 srch = imgsize[1];
514 }
515
516 float aspectRatio = (float)srcw / (float)srch;
517 float imgHeight = scale * (float)(srch);
518 float imgWidth = aspectRatio * imgHeight;
519
520 // text orientation
521 SbVec3f dir = (geom.p2 - geom.p1);
522 dir.normalize();
523 SbVec3f normal = SbVec3f(-dir[1], dir[0], 0);
524
525 // include all visual elements in bounding box
526 std::vector<SbVec3f> corners;
527
528 // text area (existing coverage)
529 float margin = imgHeight / 4.0F;
530 corners.push_back(
531 geom.textOffset + dir * (imgWidth / 2.0F + margin) - normal * (imgHeight / 2.0F + margin)
532 );
533 corners.push_back(
534 geom.textOffset - dir * (imgWidth / 2.0F + margin) - normal * (imgHeight / 2.0F + margin)
535 );
536 corners.push_back(
537 geom.textOffset + dir * (imgWidth / 2.0F + margin) + normal * (imgHeight / 2.0F + margin)
538 );
539 corners.push_back(
540 geom.textOffset - dir * (imgWidth / 2.0F + margin) + normal * (imgHeight / 2.0F + margin)
541 );
542
543 // extension line endpoints
544 corners.push_back(geom.pnt1); // start point
545 corners.push_back(geom.pnt2); // extension end 1
546 corners.push_back(geom.pnt3); // end point
547 corners.push_back(geom.pnt4); // extension end 2
548
549 // arc sample points (8 points along the curve for better coverage)
550 int numSamples = 8;
551 for (int i = 0; i < numSamples; i++) {
552 float t = (float)i / (numSamples - 1);
553 float angle = geom.startangle + t * (geom.endangle - geom.startangle);
554 SbVec3f arcPoint = geom.arcCenter
555 + SbVec3f(geom.arcRadius * cos(angle), geom.arcRadius * sin(angle), 0);
556 corners.push_back(arcPoint);
557 }
558
559 // arrow head tips (base + direction * length)
560 float arrowLength = geom.margin * 2;
561 SbVec3f startArrowTip = geom.pnt2 + geom.dirStart * arrowLength;
562 SbVec3f endArrowTip = geom.pnt4 + geom.dirEnd * arrowLength;
563 corners.push_back(startArrowTip);
564 corners.push_back(endArrowTip);
565
566 return corners;
567 }
568
569 private:
570 float scale;
571 SoDatumLabel* label;
572 };
573 } // namespace Gui
574
575 void SoDatumLabel::computeBBox(SoAction* action, SbBox3f& box, SbVec3f& center)
576 {
577 SoState* state = action->getState();
578 float scale = getScaleFactor(state);
579
580 Gui::DatumLabelBox datumBox(scale, this);
581 datumBox.computeBBox(box, center);
582 }
583
584 SbVec3f SoDatumLabel::getLabelTextCenter()
585 {
586 // Get the points stored
587 int numPts = this->pnts.getNum();
588 if (numPts < 2) {
589 return {};
590 }
591
592 const SbVec3f* points = this->pnts.getValues(0);
593 SbVec3f p1 = points[0];
594 SbVec3f p2 = points[1];
595
596 if (datumtype.getValue() == SoDatumLabel::DISTANCE
597 || datumtype.getValue() == SoDatumLabel::DISTANCEX
598 || datumtype.getValue() == SoDatumLabel::DISTANCEY) {
599 return getLabelTextCenterDistance(p1, p2);
600 }
601 if (datumtype.getValue() == SoDatumLabel::RADIUS
602 || datumtype.getValue() == SoDatumLabel::DIAMETER) {
603 return getLabelTextCenterDiameter(p1, p2);
604 }
605 if (datumtype.getValue() == SoDatumLabel::ANGLE) {
606 return getLabelTextCenterAngle(p1);
607 }
608 if (datumtype.getValue() == SoDatumLabel::ARCLENGTH) {
609 if (numPts >= 3) {
610 SbVec3f p3 = points[2];
611 return getLabelTextCenterArcLength(p1, p2, p3);
612 }
613 }
614
615 return p1;
616 }
617
618 SbVec3f SoDatumLabel::getLabelTextCenterDistance(const SbVec3f& p1, const SbVec3f& p2)
619 {
620 float length = param1.getValue();
621 float length2 = param2.getValue();
622
623 SbVec3f dir;
624 SbVec3f normal;
625
626 constexpr float floatEpsilon = std::numeric_limits<float>::epsilon();
627 if (datumtype.getValue() == SoDatumLabel::DISTANCE) {
628 dir = (p2 - p1);
629 }
630 else if (datumtype.getValue() == SoDatumLabel::DISTANCEX) {
631 dir = SbVec3f((p2[0] - p1[0] >= floatEpsilon) ? 1 : -1, 0, 0);
632 }
633 else if (datumtype.getValue() == SoDatumLabel::DISTANCEY) {
634 dir = SbVec3f(0, (p2[1] - p1[1] >= floatEpsilon) ? 1 : -1, 0);
635 }
636
637 dir.normalize();
638 normal = SbVec3f(-dir[1], dir[0], 0);
639
640 float normproj12 = (p2 - p1).dot(normal);
641 SbVec3f p1_ = p1 + normproj12 * normal;
642
643 SbVec3f midpos = (p1_ + p2) / 2;
644
645 SbVec3f textCenter = midpos + normal * length + dir * length2;
646 return textCenter;
647 }
648
649 SbVec3f SoDatumLabel::getLabelTextCenterDiameter(const SbVec3f& p1, const SbVec3f& p2)
650 {
651 SbVec3f dir = (p2 - p1);
652 dir.normalize();
653
654 float length = this->param1.getValue();
655 SbVec3f textCenter = p2 + length * dir;
656 return textCenter;
657 }
658
659 SbVec3f SoDatumLabel::getLabelTextCenterAngle(const SbVec3f& p0)
660 {
661 // Load the Parameters
662 float length = param1.getValue();
663 float startangle = param2.getValue();
664 float range = param3.getValue();
665 float len2 = 2.0F * length;
666
667 // Useful Information
668 // v0 - vector for text position
669 // p0 - vector for angle intersect
670 SbVec3f v0(cos(startangle + range / 2), sin(startangle + range / 2), 0);
671
672 SbVec3f textCenter = p0 + v0 * len2;
673 return textCenter;
674 }
675
676 SbVec3f SoDatumLabel::getLabelTextCenterArcLength(
677 const SbVec3f& ctr,
678 const SbVec3f& p1,
679 const SbVec3f& p2
680 ) const
681 {
682 float length = this->param1.getValue();
683
684 // Angles calculations
685 SbVec3f vc1 = (p1 - ctr);
686 SbVec3f vc2 = (p2 - ctr);
687
688 float startangle = atan2f(vc1[1], vc1[0]);
689 float endangle = atan2f(vc2[1], vc2[0]);
690
691 if (endangle < startangle) {
692 endangle += 2.F * std::numbers::pi_v<float>;
693 }
694
695 // Text location
696 SbVec3f vm = (p1 + p2) / 2 - ctr;
697 vm.normalize();
698
699 SbVec3f textCenter;
700 if (endangle - startangle <= std::numbers::pi) {
701 textCenter = ctr + vm * (length + this->imgHeight);
702 }
703 else {
704 textCenter = ctr - vm * (length + 2. * this->imgHeight);
705 }
706 return textCenter;
707 }
708
709
710 void SoDatumLabel::generateDistancePrimitives(SoAction* action, const SbVec3f& p1, const SbVec3f& p2)
711 {
712 SbVec3f points[2] = {p1, p2};
713
714 DistanceGeometry geom = calculateDistanceGeometry(points);
715
716 // generate selectable primitive for txt label at elevated Z for selection above geometry
717 SbVec3f img1 = SbVec3f(-this->imgWidth / 2, -this->imgHeight / 2, ZARROW_TEXT_OFFSET);
718 SbVec3f img2 = SbVec3f(-this->imgWidth / 2, this->imgHeight / 2, ZARROW_TEXT_OFFSET);
719 SbVec3f img3 = SbVec3f(this->imgWidth / 2, -this->imgHeight / 2, ZARROW_TEXT_OFFSET);
720 SbVec3f img4 = SbVec3f(this->imgWidth / 2, this->imgHeight / 2, ZARROW_TEXT_OFFSET);
721
722 float s = sin(geom.angle);
723 float c = cos(geom.angle);
724
725 img1 = SbVec3f((img1[0] * c) - (img1[1] * s), (img1[0] * s) + (img1[1] * c), ZARROW_TEXT_OFFSET);
726 img2 = SbVec3f((img2[0] * c) - (img2[1] * s), (img2[0] * s) + (img2[1] * c), ZARROW_TEXT_OFFSET);
727 img3 = SbVec3f((img3[0] * c) - (img3[1] * s), (img3[0] * s) + (img3[1] * c), ZARROW_TEXT_OFFSET);
728 img4 = SbVec3f((img4[0] * c) - (img4[1] * s), (img4[0] * s) + (img4[1] * c), ZARROW_TEXT_OFFSET);
729
730 img1 += geom.textOffset;
731 img2 += geom.textOffset;
732 img3 += geom.textOffset;
733 img4 += geom.textOffset;
734
735 // text label selection primitive
736 SoPrimitiveVertex pv;
737 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
738
739 this->beginShape(action, TRIANGLE_STRIP);
740 pv.setPoint(img1);
741 shapeVertex(&pv);
742 pv.setPoint(img2);
743 shapeVertex(&pv);
744 pv.setPoint(img3);
745 shapeVertex(&pv);
746 pv.setPoint(img4);
747 shapeVertex(&pv);
748 this->endShape();
749
750 // beginning of generation of selectable primitives for lines
751 float lineWidth = geom.margin * 0.8f; // adjust the width for selection
752
753 // ext lines
754 generateLineSelectionPrimitive(action, geom.p1, geom.perp1, lineWidth);
755 generateLineSelectionPrimitive(action, geom.p2, geom.perp2, lineWidth);
756
757 // dim lines
758 generateLineSelectionPrimitive(action, geom.par1, geom.par2, lineWidth);
759 generateLineSelectionPrimitive(action, geom.par3, geom.par4, lineWidth);
760
761 // begin generation of selectable primitives for arrow-heads at elevated Z
762 this->beginShape(action, TRIANGLES);
763 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
764
765 // 1st arrow-head
766 pv.setPoint(SbVec3f(geom.par1[0], geom.par1[1], ZARROW_TEXT_OFFSET));
767 shapeVertex(&pv);
768 pv.setPoint(SbVec3f(geom.ar1[0], geom.ar1[1], ZARROW_TEXT_OFFSET));
769 shapeVertex(&pv);
770 pv.setPoint(SbVec3f(geom.ar2[0], geom.ar2[1], ZARROW_TEXT_OFFSET));
771 shapeVertex(&pv);
772
773 // 2nd arrow-head
774 pv.setPoint(SbVec3f(geom.par4[0], geom.par4[1], ZARROW_TEXT_OFFSET));
775 shapeVertex(&pv);
776 pv.setPoint(SbVec3f(geom.ar3[0], geom.ar3[1], ZARROW_TEXT_OFFSET));
777 shapeVertex(&pv);
778 pv.setPoint(SbVec3f(geom.ar4[0], geom.ar4[1], ZARROW_TEXT_OFFSET));
779 shapeVertex(&pv);
780
781 this->endShape();
782 }
783
784 void SoDatumLabel::generateDiameterPrimitives(SoAction* action, const SbVec3f& p1, const SbVec3f& p2)
785 {
786 SbVec3f points[2] = {p1, p2};
787 DiameterGeometry geom = calculateDiameterGeometry(points);
788
789 // generate selectable primitive for text label at elevated Z for selection above geometry
790 SbVec3f img1 = SbVec3f(-this->imgWidth / 2, -this->imgHeight / 2, ZARROW_TEXT_OFFSET);
791 SbVec3f img2 = SbVec3f(-this->imgWidth / 2, this->imgHeight / 2, ZARROW_TEXT_OFFSET);
792 SbVec3f img3 = SbVec3f(this->imgWidth / 2, -this->imgHeight / 2, ZARROW_TEXT_OFFSET);
793 SbVec3f img4 = SbVec3f(this->imgWidth / 2, this->imgHeight / 2, ZARROW_TEXT_OFFSET);
794
795 float s = sin(geom.angle);
796 float c = cos(geom.angle);
797
798 img1 = SbVec3f((img1[0] * c) - (img1[1] * s), (img1[0] * s) + (img1[1] * c), ZARROW_TEXT_OFFSET);
799 img2 = SbVec3f((img2[0] * c) - (img2[1] * s), (img2[0] * s) + (img2[1] * c), ZARROW_TEXT_OFFSET);
800 img3 = SbVec3f((img3[0] * c) - (img3[1] * s), (img3[0] * s) + (img3[1] * c), ZARROW_TEXT_OFFSET);
801 img4 = SbVec3f((img4[0] * c) - (img4[1] * s), (img4[0] * s) + (img4[1] * c), ZARROW_TEXT_OFFSET);
802
803 img1 += geom.textOffset;
804 img2 += geom.textOffset;
805 img3 += geom.textOffset;
806 img4 += geom.textOffset;
807
808 // txt label selection primitive
809 SoPrimitiveVertex pv;
810 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
811
812 this->beginShape(action, TRIANGLE_STRIP);
813 pv.setPoint(img1);
814 shapeVertex(&pv);
815 pv.setPoint(img2);
816 shapeVertex(&pv);
817 pv.setPoint(img3);
818 shapeVertex(&pv);
819 pv.setPoint(img4);
820 shapeVertex(&pv);
821 this->endShape();
822
823 // generate selectable primitives for dimension lines
824 float lineWidth = geom.margin * 0.8f;
825
826 // main dimension lines (from center/start to text area and from text area to end)
827 generateLineSelectionPrimitive(action, geom.p1, geom.pnt1, lineWidth);
828 generateLineSelectionPrimitive(action, geom.pnt2, geom.p2, lineWidth);
829
830 // Generate selectable primitives for arrow heads at elevated Z
831 this->beginShape(action, TRIANGLES);
832 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
833
834 // first arrow-head
835 pv.setPoint(SbVec3f(geom.ar0[0], geom.ar0[1], ZARROW_TEXT_OFFSET));
836 shapeVertex(&pv);
837 pv.setPoint(SbVec3f(geom.ar1[0], geom.ar1[1], ZARROW_TEXT_OFFSET));
838 shapeVertex(&pv);
839 pv.setPoint(SbVec3f(geom.ar2[0], geom.ar2[1], ZARROW_TEXT_OFFSET));
840 shapeVertex(&pv);
841
842 // second arrow-head but only for diameter
843 if (geom.isDiameter) {
844 pv.setPoint(SbVec3f(geom.ar0_1[0], geom.ar0_1[1], ZARROW_TEXT_OFFSET));
845 shapeVertex(&pv);
846 pv.setPoint(SbVec3f(geom.ar1_1[0], geom.ar1_1[1], ZARROW_TEXT_OFFSET));
847 shapeVertex(&pv);
848 pv.setPoint(SbVec3f(geom.ar2_1[0], geom.ar2_1[1], ZARROW_TEXT_OFFSET));
849 shapeVertex(&pv);
850 }
851
852 this->endShape();
853
854 const auto generateSelectablePrimitiveForArcHelper = [&, this](float startAngle, float range) {
855 if (range != 0.0) {
856 int countSegments = std::max(6, abs(int(50.0 * range / (2 * std::numbers::pi))));
857 double segment = range / (countSegments - 1);
858
859 // create selectable line segments for the arc
860 for (int i = 0; i < countSegments - 1; i++) {
861 double theta1 = startAngle + segment * i;
862 double theta2 = startAngle + segment * (i + 1);
863 SbVec3f v1 = geom.center
864 + SbVec3f(geom.radius * cos(theta1), geom.radius * sin(theta1), 0);
865 SbVec3f v2 = geom.center
866 + SbVec3f(geom.radius * cos(theta2), geom.radius * sin(theta2), 0);
867 generateLineSelectionPrimitive(action, v1, v2, lineWidth * 0.5f);
868 }
869 }
870 };
871
872 generateSelectablePrimitiveForArcHelper(geom.startAngle, geom.startRange);
873 generateSelectablePrimitiveForArcHelper(geom.endAngle, geom.endRange);
874 }
875
876 void SoDatumLabel::generateAnglePrimitives(SoAction* action, const SbVec3f& p0)
877 {
878 // use shared geometry calculation
879 SbVec3f points[1] = {p0};
880 AngleGeometry geom = calculateAngleGeometry(points);
881
882 // generate selectable primitive for text label at elevated Z for selection above geometry
883 SbVec3f img1 = SbVec3f(-this->imgWidth / 2, -this->imgHeight / 2, ZARROW_TEXT_OFFSET);
884 SbVec3f img2 = SbVec3f(-this->imgWidth / 2, this->imgHeight / 2, ZARROW_TEXT_OFFSET);
885 SbVec3f img3 = SbVec3f(this->imgWidth / 2, -this->imgHeight / 2, ZARROW_TEXT_OFFSET);
886 SbVec3f img4 = SbVec3f(this->imgWidth / 2, this->imgHeight / 2, ZARROW_TEXT_OFFSET);
887
888 img1 += geom.textOffset;
889 img2 += geom.textOffset;
890 img3 += geom.textOffset;
891 img4 += geom.textOffset;
892
893 // text label selection primitive
894 SoPrimitiveVertex pv;
895 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
896
897 this->beginShape(action, TRIANGLE_STRIP);
898 pv.setPoint(img1);
899 shapeVertex(&pv);
900 pv.setPoint(img2);
901 shapeVertex(&pv);
902 pv.setPoint(img3);
903 shapeVertex(&pv);
904 pv.setPoint(img4);
905 shapeVertex(&pv);
906 this->endShape();
907
908 // generate selectable primitives for dimension lines
909 float lineWidth = geom.margin * 0.8f;
910
911 // extension lines
912 generateLineSelectionPrimitive(action, geom.pnt1, geom.pnt2, lineWidth);
913 generateLineSelectionPrimitive(action, geom.pnt3, geom.pnt4, lineWidth);
914
915 // generate selectable primitives for arc segments
916 float arcWidth = geom.margin * 0.6f;
917
918 // arc before text
919 generateArcSelectionPrimitive(
920 action,
921 geom.p0,
922 geom.r,
923 geom.startangle,
924 geom.startangle + geom.range / 2.0 - geom.textMargin,
925 arcWidth
926 );
927
928 // arc after text
929 generateArcSelectionPrimitive(
930 action,
931 geom.p0,
932 geom.r,
933 geom.startangle + geom.range / 2.0 + geom.textMargin,
934 geom.endangle,
935 arcWidth
936 );
937
938 // generate selectable primitives for arrow heads
939 generateArrowSelectionPrimitive(
940 action,
941 geom.startArrowBase,
942 geom.dirStart,
943 geom.arrowWidth,
944 geom.arrowLength
945 );
946 generateArrowSelectionPrimitive(
947 action,
948 geom.endArrowBase,
949 geom.dirEnd,
950 geom.arrowWidth,
951 geom.arrowLength
952 );
953 }
954
955 void SoDatumLabel::generateSymmetricPrimitives(SoAction* action, const SbVec3f& p1, const SbVec3f& p2)
956 {
957 // use shared geometry calculation
958 SbVec3f points[2] = {p1, p2};
959 SymmetricGeometry geom = calculateSymmetricGeometry(points);
960
961 // generate selectable primitives for lines
962 float lineWidth = geom.margin * 0.8f;
963
964 // lines from endpoints to arrow tips
965 generateLineSelectionPrimitive(action, geom.p1, geom.ar0, lineWidth);
966 generateLineSelectionPrimitive(action, geom.p2, geom.ar3, lineWidth);
967
968 // generate selectable primitives for arrow heads as triangles at elevated Z
969 SoPrimitiveVertex pv;
970 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
971
972 this->beginShape(action, TRIANGLES);
973
974 // first arrow
975 pv.setPoint(SbVec3f(geom.ar0[0], geom.ar0[1], ZARROW_TEXT_OFFSET));
976 shapeVertex(&pv);
977 pv.setPoint(SbVec3f(geom.ar1[0], geom.ar1[1], ZARROW_TEXT_OFFSET));
978 shapeVertex(&pv);
979 pv.setPoint(SbVec3f(geom.ar2[0], geom.ar2[1], ZARROW_TEXT_OFFSET));
980 shapeVertex(&pv);
981
982 // second arrow
983 pv.setPoint(SbVec3f(geom.ar3[0], geom.ar3[1], ZARROW_TEXT_OFFSET));
984 shapeVertex(&pv);
985 pv.setPoint(SbVec3f(geom.ar4[0], geom.ar4[1], ZARROW_TEXT_OFFSET));
986 shapeVertex(&pv);
987 pv.setPoint(SbVec3f(geom.ar5[0], geom.ar5[1], ZARROW_TEXT_OFFSET));
988 shapeVertex(&pv);
989
990 this->endShape();
991 }
992
993 void SoDatumLabel::generateArcLengthPrimitives(
994 SoAction* action,
995 const SbVec3f& ctr,
996 const SbVec3f& p1,
997 const SbVec3f& p2
998 )
999 {
1000 // use shared geometry calculation
1001 SbVec3f points[3] = {ctr, p1, p2};
1002 ArcLengthGeometry geom = calculateArcLengthGeometry(points);
1003
1004 // generate selectable primitive for text label at elevated Z for selection above geometry
1005 SbVec3f img1 = SbVec3f(-this->imgWidth / 2, -this->imgHeight / 2, ZARROW_TEXT_OFFSET);
1006 SbVec3f img2 = SbVec3f(-this->imgWidth / 2, this->imgHeight / 2, ZARROW_TEXT_OFFSET);
1007 SbVec3f img3 = SbVec3f(this->imgWidth / 2, -this->imgHeight / 2, ZARROW_TEXT_OFFSET);
1008 SbVec3f img4 = SbVec3f(this->imgWidth / 2, this->imgHeight / 2, ZARROW_TEXT_OFFSET);
1009
1010 float s = sin(geom.angle);
1011 float c = cos(geom.angle);
1012
1013 img1 = SbVec3f((img1[0] * c) - (img1[1] * s), (img1[0] * s) + (img1[1] * c), ZARROW_TEXT_OFFSET);
1014 img2 = SbVec3f((img2[0] * c) - (img2[1] * s), (img2[0] * s) + (img2[1] * c), ZARROW_TEXT_OFFSET);
1015 img3 = SbVec3f((img3[0] * c) - (img3[1] * s), (img3[0] * s) + (img3[1] * c), ZARROW_TEXT_OFFSET);
1016 img4 = SbVec3f((img4[0] * c) - (img4[1] * s), (img4[0] * s) + (img4[1] * c), ZARROW_TEXT_OFFSET);
1017
1018 img1 += geom.textOffset;
1019 img2 += geom.textOffset;
1020 img3 += geom.textOffset;
1021 img4 += geom.textOffset;
1022
1023 // text label selection primitive
1024 SoPrimitiveVertex pv;
1025 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
1026
1027 this->beginShape(action, TRIANGLE_STRIP);
1028 pv.setPoint(img1);
1029 shapeVertex(&pv);
1030 pv.setPoint(img2);
1031 shapeVertex(&pv);
1032 pv.setPoint(img3);
1033 shapeVertex(&pv);
1034 pv.setPoint(img4);
1035 shapeVertex(&pv);
1036 this->endShape();
1037
1038 // generate selectable primitives for lines
1039 float lineWidth = geom.margin * 0.8f;
1040
1041 // extension lines
1042 generateLineSelectionPrimitive(action, geom.pnt1, geom.pnt2, lineWidth);
1043 generateLineSelectionPrimitive(action, geom.pnt3, geom.pnt4, lineWidth);
1044
1045 // generate selectable primitive for arc
1046 generateArcSelectionPrimitive(
1047 action,
1048 geom.arcCenter,
1049 geom.arcRadius,
1050 geom.startangle,
1051 geom.endangle,
1052 lineWidth
1053 );
1054
1055 // generate selectable primitives for arrow heads
1056 float arrowLength = geom.margin * 2;
1057 float arrowWidth = geom.margin * 0.5F;
1058
1059 generateArrowSelectionPrimitive(action, geom.pnt2, geom.dirStart, arrowWidth, arrowLength);
1060 generateArrowSelectionPrimitive(action, geom.pnt4, geom.dirEnd, arrowWidth, arrowLength);
1061 }
1062
1063 void SoDatumLabel::generatePrimitives(SoAction* action)
1064 {
1065 // Initialisation check (needs something more sensible) prevents an infinite loop bug
1066 constexpr float floatEpsilon = std::numeric_limits<float>::epsilon();
1067 if (this->imgHeight <= floatEpsilon || this->imgWidth <= floatEpsilon) {
1068 return;
1069 }
1070
1071 int numPts = this->pnts.getNum();
1072 if (numPts < 2) {
1073 return;
1074 }
1075
1076 // Get the points stored
1077 const SbVec3f* points = this->pnts.getValues(0);
1078 SbVec3f p1 = points[0];
1079 SbVec3f p2 = points[1];
1080
1081 // Change the offset and bounding box parameters depending on Datum Type
1082 if (this->datumtype.getValue() == DISTANCE || this->datumtype.getValue() == DISTANCEX
1083 || this->datumtype.getValue() == DISTANCEY) {
1084
1085 generateDistancePrimitives(action, p1, p2);
1086 }
1087 else if (this->datumtype.getValue() == RADIUS || this->datumtype.getValue() == DIAMETER) {
1088
1089 generateDiameterPrimitives(action, p1, p2);
1090 }
1091 else if (this->datumtype.getValue() == ANGLE) {
1092
1093 generateAnglePrimitives(action, p1);
1094 }
1095 else if (this->datumtype.getValue() == SYMMETRIC) {
1096
1097 generateSymmetricPrimitives(action, p1, p2);
1098 }
1099 else if (this->datumtype.getValue() == ARCLENGTH) {
1100
1101 if (numPts >= 3) {
1102 SbVec3f p3 = points[2];
1103 generateArcLengthPrimitives(action, p1, p2, p3);
1104 }
1105 }
1106 }
1107
1108 void SoDatumLabel::notify(SoNotList* l)
1109 {
1110 SoField* f = l->getLastField();
1111 if (f == &this->string) {
1112 this->glimagevalid = false;
1113 }
1114 else if (f == &this->textColor) {
1115 this->glimagevalid = false;
1116 }
1117 else if (f == &this->name) {
1118 this->glimagevalid = false;
1119 }
1120 else if (f == &this->size) {
1121 this->glimagevalid = false;
1122 }
1123 else if (f == &this->image) {
1124 this->glimagevalid = false;
1125 }
1126 inherited::notify(l);
1127 }
1128
1129 float SoDatumLabel::getScaleFactor(SoState* state) const
1130 {
1131 /**Remark from Stefan Tröger:
1132 * The scale calculation is based on knowledge of SbViewVolume::getWorldToScreenScale
1133 * implementation internals. The factor returned from this function is calculated from the view
1134 * frustums nearplane width, height is not taken into account, and hence we divide it with the
1135 * viewport width to get the exact pixel scale factor. This is not documented and therefore may
1136 * change on later coin versions!
1137 */
1138 const SbViewVolume& vv = SoViewVolumeElement::get(state);
1139 // As reference use the center point the camera is looking at on the focal plane
1140 // because then independent of the camera we get a constant scale factor when panning.
1141 // If we used (0,0,0) instead then the scale factor would change heavily in perspective
1142 // rendering mode. See #0002921 and #0002922.
1143 // It's important to use the distance to the focal plane an not near or far plane because
1144 // depending on additionally displayed objects they may change heavily and thus impact the
1145 // scale factor. See #7082 and #7860.
1146 float focal = SoFocalDistanceElement::get(state);
1147 SbVec3f center = vv.getSightPoint(focal);
1148 float scale = vv.getWorldToScreenScale(center, 1.F);
1149 const SbViewportRegion& vp = SoViewportRegionElement::get(state);
1150 SbVec2s vp_size = vp.getViewportSizePixels();
1151 scale /= float(vp_size[0]);
1152
1153 return scale;
1154 }
1155
1156 void SoDatumLabel::GLRender(SoGLRenderAction* action)
1157 {
1158 SoState* state = action->getState();
1159
1160 if (!shouldGLRender(action)) {
1161 return;
1162 }
1163 if (action->handleTransparency(true)) {
1164 return;
1165 }
1166
1167 const float scale = getScaleFactor(state);
1168 bool hasText = hasDatumText();
1169
1170 int srcw = 1;
1171 int srch = 1;
1172
1173 if (hasText) {
1174 getDimension(scale, srcw, srch);
1175 }
1176
1177 if (this->datumtype.getValue() == SYMMETRIC) {
1178 this->imgHeight = scale * 25.0F;
1179 this->imgWidth = scale * 25.0F;
1180 }
1181
1182 // Get the points stored in the pnt field
1183 const SbVec3f* points = this->pnts.getValues(0);
1184
1185 state->push();
1186
1187 // Set General OpenGL Properties
1188 glPushAttrib(GL_ENABLE_BIT | GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1189 glDisable(GL_LIGHTING);
1190 glDisable(GL_CULL_FACE);
1191
1192 // Enable depth testing so constraint lines use the sketch-local Z carried by their
1193 // input points. That keeps them above coplanar model geometry while still rendering
1194 // below sketch elements in the normal scene.
1195 glEnable(GL_DEPTH_TEST);
1196
1197 // Enable Anti-alias
1198 if (action->isSmoothing()) {
1199 glEnable(GL_LINE_SMOOTH);
1200 glEnable(GL_BLEND);
1201 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1202 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
1203 }
1204
1205 // Position for Datum Text Label
1206 float angle = 0;
1207
1208 // Get the colour
1209 const SbColor& t = textColor.getValue();
1210
1211 // Set GL Properties
1212 glLineWidth(this->lineWidth.getValue());
1213 glColor3f(t[0], t[1], t[2]);
1214
1215 SbVec3f textOffset;
1216
1217 if (this->datumtype.getValue() == DISTANCE || this->datumtype.getValue() == DISTANCEX
1218 || this->datumtype.getValue() == DISTANCEY) {
1219 drawDistance(points, angle, textOffset);
1220 }
1221 else if (this->datumtype.getValue() == RADIUS || this->datumtype.getValue() == DIAMETER) {
1222 drawRadiusOrDiameter(points, angle, textOffset);
1223 }
1224 else if (this->datumtype.getValue() == ANGLE) {
1225 drawAngle(points, angle, textOffset);
1226 }
1227 else if (this->datumtype.getValue() == SYMMETRIC) {
1228 drawSymmetric(points);
1229 }
1230 else if (this->datumtype.getValue() == ARCLENGTH) {
1231 drawArcLength(points, angle, textOffset);
1232 }
1233
1234 if (hasText) {
1235 drawText(state, srcw, srch, angle, textOffset);
1236 }
1237
1238 glPopAttrib();
1239 state->pop();
1240 }
1241
1242 bool SoDatumLabel::hasDatumText() const
1243 {
1244 const SbString* s = string.getValues(0);
1245 return (s->getLength() > 0);
1246 }
1247
1248 void SoDatumLabel::getDimension(float scale, int& srcw, int& srch)
1249 {
1250 SbVec2s imgsize;
1251 int nc {};
1252
1253 if (!this->glimagevalid) {
1254 drawImage();
1255 this->glimagevalid = true;
1256 }
1257
1258 const unsigned char* dataptr = this->image.getValue(imgsize, nc);
1259 if (!dataptr) { // no image
1260 return;
1261 }
1262
1263 srcw = imgsize[0];
1264 srch = imgsize[1];
1265
1266 float aspectRatio = (float)srcw / (float)srch;
1267 this->imgHeight = scale * (float)(srch) / sampling.getValue();
1268 this->imgWidth = aspectRatio * (float)this->imgHeight;
1269 }
1270
1271 void SoDatumLabel::drawDistance(const SbVec3f* points, float& angle, SbVec3f& textOffset)
1272 {
1273 SoDatumLabel::DistanceGeometry geom = this->calculateDistanceGeometry(points);
1274
1275 angle = geom.angle;
1276 textOffset = geom.textOffset;
1277
1278 // Get the colour
1279 const SbColor& t = textColor.getValue();
1280
1281 // Set GL Properties
1282 glLineWidth(this->lineWidth.getValue());
1283 glColor3f(t[0], t[1], t[2]);
1284
1285 // Perp Lines
1286 glBegin(GL_LINES);
1287 if (this->param1.getValue() != 0.) {
1288 glVertex(geom.p1);
1289 glVertex(geom.perp1);
1290
1291 glVertex(geom.p2);
1292 glVertex(geom.perp2);
1293 }
1294
1295 glVertex(geom.par1);
1296 glVertex(geom.par2);
1297
1298 glVertex(geom.par3);
1299 glVertex(geom.par4);
1300 glEnd();
1301
1302 // Draw the arrowheads at elevated Z to render ON TOP of geometry lines
1303 glBegin(GL_TRIANGLES);
1304 glVertex3f(geom.par1[0], geom.par1[1], ZARROW_TEXT_OFFSET);
1305 glVertex3f(geom.ar1[0], geom.ar1[1], ZARROW_TEXT_OFFSET);
1306 glVertex3f(geom.ar2[0], geom.ar2[1], ZARROW_TEXT_OFFSET);
1307
1308 glVertex3f(geom.par4[0], geom.par4[1], ZARROW_TEXT_OFFSET);
1309 glVertex3f(geom.ar3[0], geom.ar3[1], ZARROW_TEXT_OFFSET);
1310 glVertex3f(geom.ar4[0], geom.ar4[1], ZARROW_TEXT_OFFSET);
1311 glEnd();
1312
1313 if (this->datumtype.getValue() == DISTANCE) {
1314 drawDistance(points);
1315 }
1316 }
1317
1318 void SoDatumLabel::drawDistance(const SbVec3f* points)
1319 {
1320 // Draw arc helpers if needed
1321 float range1 = this->param4.getValue();
1322 if (range1 != 0.0) {
1323 float startAngle1 = this->param3.getValue();
1324 float radius1 = this->param5.getValue();
1325 SbVec3f center1 = points[2];
1326 glDrawArc(center1, radius1, startAngle1, startAngle1 + range1);
1327 }
1328
1329 float range2 = this->param7.getValue();
1330 if (range2 != 0.0) {
1331 float startAngle2 = this->param6.getValue();
1332 float radius2 = this->param8.getValue();
1333 SbVec3f center2 = points[3];
1334 glDrawArc(center2, radius2, startAngle2, startAngle2 + range2);
1335 }
1336 }
1337
1338 void SoDatumLabel::drawRadiusOrDiameter(const SbVec3f* points, float& angle, SbVec3f& textOffset)
1339 {
1340 // Use shared geometry calculation
1341 DiameterGeometry geom = calculateDiameterGeometry(points);
1342
1343 angle = geom.angle;
1344 textOffset = geom.textOffset;
1345
1346 // Draw the Lines
1347 glBegin(GL_LINES);
1348 glVertex(geom.p1);
1349 glVertex(geom.pnt1);
1350
1351 glVertex(geom.pnt2);
1352 glVertex(geom.p2);
1353 glEnd();
1354
1355 // Draw arrowhead at elevated Z to render ON TOP of geometry lines
1356 glBegin(GL_TRIANGLES);
1357 glVertex3f(geom.ar0[0], geom.ar0[1], ZARROW_TEXT_OFFSET);
1358 glVertex3f(geom.ar1[0], geom.ar1[1], ZARROW_TEXT_OFFSET);
1359 glVertex3f(geom.ar2[0], geom.ar2[1], ZARROW_TEXT_OFFSET);
1360 glEnd();
1361
1362 if (geom.isDiameter) {
1363 // Draw second arrowhead at elevated Z
1364 glBegin(GL_TRIANGLES);
1365 glVertex3f(geom.ar0_1[0], geom.ar0_1[1], ZARROW_TEXT_OFFSET);
1366 glVertex3f(geom.ar1_1[0], geom.ar1_1[1], ZARROW_TEXT_OFFSET);
1367 glVertex3f(geom.ar2_1[0], geom.ar2_1[1], ZARROW_TEXT_OFFSET);
1368 glEnd();
1369 }
1370
1371 // Draw arc helpers if needed
1372 if (geom.startRange != 0.0) {
1373 glDrawArc(geom.center, geom.radius, geom.startAngle, geom.startAngle + geom.startRange);
1374 }
1375
1376 if (geom.endRange != 0.0) {
1377 glDrawArc(geom.center, geom.radius, geom.endAngle, geom.endAngle + geom.endRange);
1378 }
1379 }
1380
1381 void SoDatumLabel::drawAngle(const SbVec3f* points, float& angle, SbVec3f& textOffset)
1382 {
1383 // use shared geometry calculation
1384 AngleGeometry geom = calculateAngleGeometry(points);
1385
1386 angle = geom.angle;
1387 textOffset = geom.textOffset;
1388
1389 // draw arc segments
1390 glDrawArc(geom.p0, geom.r, geom.startangle, geom.startangle + geom.range / 2.0 - geom.textMargin);
1391 glDrawArc(geom.p0, geom.r, geom.startangle + geom.range / 2.0 + geom.textMargin, geom.endangle);
1392
1393 // draw extension lines
1394 glDrawLine(geom.pnt1, geom.pnt2);
1395 glDrawLine(geom.pnt3, geom.pnt4);
1396
1397 // draw arrowheads
1398 glDrawArrow(geom.startArrowBase, geom.dirStart, geom.arrowWidth, geom.arrowLength);
1399 glDrawArrow(geom.endArrowBase, geom.dirEnd, geom.arrowWidth, geom.arrowLength);
1400 }
1401
1402 void SoDatumLabel::drawSymmetric(const SbVec3f* points)
1403 {
1404 // use shared geometry calculation
1405 SymmetricGeometry geom = calculateSymmetricGeometry(points);
1406
1407 // draw first constraint line (at constraint Z)
1408 glBegin(GL_LINES);
1409 glVertex3f(geom.p1[0], geom.p1[1], ZCONSTR);
1410 glVertex3f(geom.ar0[0], geom.ar0[1], ZCONSTR);
1411 glEnd();
1412
1413 // draw first arrowhead at elevated Z to render ON TOP of geometry lines
1414 glBegin(GL_LINES);
1415 glVertex3f(geom.ar0[0], geom.ar0[1], ZARROW_TEXT_OFFSET);
1416 glVertex3f(geom.ar1[0], geom.ar1[1], ZARROW_TEXT_OFFSET);
1417 glVertex3f(geom.ar0[0], geom.ar0[1], ZARROW_TEXT_OFFSET);
1418 glVertex3f(geom.ar2[0], geom.ar2[1], ZARROW_TEXT_OFFSET);
1419 glEnd();
1420
1421 // draw second constraint line (at constraint Z)
1422 glBegin(GL_LINES);
1423 glVertex3f(geom.p2[0], geom.p2[1], ZCONSTR);
1424 glVertex3f(geom.ar3[0], geom.ar3[1], ZCONSTR);
1425 glEnd();
1426
1427 // draw second arrowhead at elevated Z to render ON TOP of geometry lines
1428 glBegin(GL_LINES);
1429 glVertex3f(geom.ar3[0], geom.ar3[1], ZARROW_TEXT_OFFSET);
1430 glVertex3f(geom.ar4[0], geom.ar4[1], ZARROW_TEXT_OFFSET);
1431 glVertex3f(geom.ar3[0], geom.ar3[1], ZARROW_TEXT_OFFSET);
1432 glVertex3f(geom.ar5[0], geom.ar5[1], ZARROW_TEXT_OFFSET);
1433 glEnd();
1434 }
1435
1436 void SoDatumLabel::drawArcLength(const SbVec3f* points, float& angle, SbVec3f& textOffset)
1437 {
1438 // use shared geometry calculation
1439 ArcLengthGeometry geom = calculateArcLengthGeometry(points);
1440
1441 // set output parameters
1442 angle = geom.angle;
1443 textOffset = geom.textOffset;
1444
1445 // draw arc
1446 glDrawArc(geom.arcCenter, geom.arcRadius, geom.startangle, geom.endangle);
1447
1448 // draw lines
1449 glDrawLine(geom.pnt1, geom.pnt2);
1450 glDrawLine(geom.pnt3, geom.pnt4);
1451
1452 // create the arrowheads
1453 float arrowLength = geom.margin * 2;
1454 float arrowWidth = geom.margin * 0.5F;
1455
1456 glDrawArrow(geom.pnt2, geom.dirStart, arrowWidth, arrowLength);
1457 glDrawArrow(geom.pnt4, geom.dirEnd, arrowWidth, arrowLength);
1458 }
1459
1460 // NOLINTNEXTLINE
1461 void SoDatumLabel::drawText(SoState* state, int srcw, int srch, float angle, const SbVec3f& textOffset)
1462 {
1463 SbVec2s imgsize;
1464 int nc {};
1465 const unsigned char* dataptr = this->image.getValue(imgsize, nc);
1466
1467 // Get the camera z-direction
1468 const SbViewVolume& vv = SoViewVolumeElement::get(state);
1469 SbVec3f z = vv.zVector();
1470
1471 bool flip = norm.getValue().dot(z) > std::numeric_limits<float>::epsilon();
1472
1473 static bool init = false;
1474 static bool npot = false;
1475 if (!init) {
1476 init = true;
1477 std::string ext = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); // NOLINT
1478 npot = (ext.find("GL_ARB_texture_non_power_of_two") != std::string::npos);
1479 }
1480
1481 int w = srcw;
1482 int h = srch;
1483 if (!npot) {
1484 // make power of two
1485 if ((w & (w - 1)) != 0) {
1486 int i = 1;
1487 while (i < 8) {
1488 if ((w >> i) == 0) {
1489 break;
1490 }
1491 i++;
1492 }
1493 w = (1 << i);
1494 }
1495 // make power of two
1496 if ((h & (h - 1)) != 0) {
1497 int i = 1;
1498 while (i < 8) {
1499 if ((h >> i) == 0) {
1500 break;
1501 }
1502 i++;
1503 }
1504 h = (1 << i);
1505 }
1506 }
1507
1508 glDisable(GL_DEPTH_TEST);
1509 glEnable(GL_TEXTURE_2D); // Enable Textures
1510 glEnable(GL_BLEND);
1511
1512 // glGenTextures/glBindTexture was commented out but it must be active, see:
1513 // #0000971: Tracing over a background image in Sketcher: image is overwritten by first
1514 // dimensional constraint text #0001185: Planer image changes to number graphic when a part
1515 // design constraint is made after the planar image
1516 //
1517 // Copy the text bitmap into memory and bind
1518 GLuint myTexture {};
1519 // generate a texture
1520 glGenTextures(1, &myTexture);
1521 glBindTexture(GL_TEXTURE_2D, myTexture);
1522
1523 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1524 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1525
1526 if (!npot) {
1527 QImage imagedata(w, h, QImage::Format_ARGB32_Premultiplied);
1528 imagedata.fill(0x00000000);
1529 int sx = (w - srcw) / 2;
1530 int sy = (h - srch) / 2;
1531 glTexImage2D(
1532 GL_TEXTURE_2D,
1533 0,
1534 nc,
1535 w,
1536 h,
1537 0,
1538 GL_RGBA,
1539 GL_UNSIGNED_BYTE,
1540 (const GLvoid*)imagedata.bits()
1541 );
1542 glTexSubImage2D(
1543 GL_TEXTURE_2D,
1544 0,
1545 sx,
1546 sy,
1547 srcw,
1548 srch,
1549 GL_RGBA,
1550 GL_UNSIGNED_BYTE,
1551 (const GLvoid*)dataptr
1552 );
1553 }
1554 else {
1555 glTexImage2D(GL_TEXTURE_2D, 0, nc, srcw, srch, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)dataptr);
1556 }
1557 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1558
1559 glMatrixMode(GL_MODELVIEW);
1560 glPushMatrix();
1561
1562 // Apply a rotation and translation matrix
1563 glTranslatef(textOffset[0], textOffset[1], textOffset[2]);
1564 glRotatef(Base::toDegrees<GLfloat>(angle), 0, 0, 1);
1565 glBegin(GL_QUADS);
1566
1567 glColor3f(1.F, 1.F, 1.F);
1568
1569 glTexCoord2f(flip ? 0.F : 1.F, 1.F);
1570 glVertex2f(-this->imgWidth / 2, this->imgHeight / 2);
1571 glTexCoord2f(flip ? 0.F : 1.F, 0.F);
1572 glVertex2f(-this->imgWidth / 2, -this->imgHeight / 2);
1573 glTexCoord2f(flip ? 1.F : 0.F, 0.F);
1574 glVertex2f(this->imgWidth / 2, -this->imgHeight / 2);
1575 glTexCoord2f(flip ? 1.F : 0.F, 1.F);
1576 glVertex2f(this->imgWidth / 2, this->imgHeight / 2);
1577
1578 glEnd();
1579
1580 // Reset the Mode
1581 glPopMatrix();
1582
1583 // wmayer: see bug report below which is caused by generating but not
1584 // deleting the texture.
1585 // #0000721: massive memory leak when dragging an unconstrained model
1586 glDeleteTextures(1, &myTexture);
1587 }
1588
1589 void SoDatumLabel::setPoints(SbVec3f p1, SbVec3f p2)
1590 {
1591 pnts.setNum(2);
1592 SbVec3f* verts = pnts.startEditing();
1593 verts[0] = p1;
1594 verts[1] = p2;
1595 pnts.finishEditing();
1596 }
1597 // NOLINTEND(readability-magic-numbers,cppcoreguidelines-pro-bounds-pointer-arithmetic)
1598
1599 SoDatumLabel::DistanceGeometry SoDatumLabel::calculateDistanceGeometry(const SbVec3f* points) const
1600 {
1601 using std::numbers::pi;
1602
1603 SoDatumLabel::DistanceGeometry geom;
1604
1605 float length = this->param1.getValue();
1606 float length2 = this->param2.getValue();
1607
1608 geom.p1 = points[0];
1609 geom.p2 = points[1];
1610
1611 constexpr float floatEpsilon = std::numeric_limits<float>::epsilon();
1612 if (this->datumtype.getValue() == DISTANCE) {
1613 geom.dir = (geom.p2 - geom.p1);
1614 }
1615 else if (this->datumtype.getValue() == DISTANCEX) {
1616 geom.dir = SbVec3f((geom.p2[0] - geom.p1[0] >= floatEpsilon) ? 1 : -1, 0, 0);
1617 }
1618 else if (this->datumtype.getValue() == DISTANCEY) {
1619 geom.dir = SbVec3f(0, (geom.p2[1] - geom.p1[1] >= floatEpsilon) ? 1 : -1, 0);
1620 }
1621
1622 geom.dir.normalize();
1623 geom.normal = SbVec3f(-geom.dir[1], geom.dir[0], 0);
1624
1625 // when the datum line is not parallel to p1-p2 the projection of
1626 // p1-p2 on normal is not zero, p2 is considered as reference and p1
1627 // is replaced by its projection p1_
1628 float normproj12 = (geom.p2 - geom.p1).dot(geom.normal);
1629 SbVec3f p1_ = geom.p1 + normproj12 * geom.normal;
1630
1631 geom.midpos = (p1_ + geom.p2) / 2;
1632
1633 // Get magnitude of angle between horizontal
1634 geom.angle = atan2f(geom.dir[1], geom.dir[0]);
1635 if (geom.angle > pi / 2 + pi / 12) {
1636 geom.angle -= (float)pi;
1637 }
1638 else if (geom.angle <= -pi / 2 + pi / 12) {
1639 geom.angle += (float)pi;
1640 }
1641
1642 geom.textOffset = geom.midpos + geom.normal * length + geom.dir * length2;
1643
1644 geom.margin = this->imgHeight / 3.0F;
1645
1646 float offset1 = ((length + normproj12 < 0) ? -1.F : 1.F) * geom.margin;
1647 float offset2 = ((length < 0) ? -1 : 1) * geom.margin;
1648
1649 geom.perp1 = p1_ + geom.normal * (length + offset1);
1650 geom.perp2 = geom.p2 + geom.normal * (length + offset2);
1651
1652 // Calculate the coordinates for the parallel datum lines
1653 geom.par1 = p1_ + geom.normal * length;
1654 geom.par2 = geom.midpos + geom.normal * length
1655 + geom.dir * (length2 - this->imgWidth / 2 - geom.margin);
1656 geom.par3 = geom.midpos + geom.normal * length
1657 + geom.dir * (length2 + this->imgWidth / 2 + geom.margin);
1658 geom.par4 = geom.p2 + geom.normal * length;
1659
1660 geom.flipTriang = false;
1661
1662 if ((geom.par3 - geom.par1).dot(geom.dir) > (geom.par4 - geom.par1).length()) {
1663 // Increase Margin to improve visibility
1664 float tmpMargin = this->imgHeight / 0.75F;
1665 geom.par3 = geom.par4;
1666 if ((geom.par2 - geom.par1).dot(geom.dir) > (geom.par4 - geom.par1).length()) {
1667 geom.par3 = geom.par2;
1668 geom.par2 = geom.par1 - geom.dir * tmpMargin;
1669 geom.flipTriang = true;
1670 }
1671 }
1672 else if ((geom.par2 - geom.par1).dot(geom.dir) < 0.F) {
1673 float tmpMargin = this->imgHeight / 0.75F;
1674 geom.par2 = geom.par1;
1675 if ((geom.par3 - geom.par1).dot(geom.dir) < 0.F) {
1676 geom.par2 = geom.par3;
1677 geom.par3 = geom.par4 + geom.dir * tmpMargin;
1678 geom.flipTriang = true;
1679 }
1680 }
1681
1682 geom.arrowWidth = geom.margin * 0.5F;
1683
1684 geom.ar1 = geom.par1 + ((geom.flipTriang) ? -1 : 1) * geom.dir * 0.866F * 2 * geom.margin;
1685 geom.ar2 = geom.ar1 + geom.normal * geom.arrowWidth;
1686 geom.ar1 -= geom.normal * geom.arrowWidth;
1687
1688 geom.ar3 = geom.par4 - ((geom.flipTriang) ? -1 : 1) * geom.dir * 0.866F * 2 * geom.margin;
1689 geom.ar4 = geom.ar3 + geom.normal * geom.arrowWidth;
1690 geom.ar3 -= geom.normal * geom.arrowWidth;
1691
1692 return geom;
1693 }
1694
1695 SoDatumLabel::DiameterGeometry SoDatumLabel::calculateDiameterGeometry(const SbVec3f* points) const
1696 {
1697 DiameterGeometry geom;
1698
1699 // Get the Points
1700 geom.p1 = points[0];
1701 geom.p2 = points[1];
1702
1703 geom.dir = (geom.p2 - geom.p1);
1704 geom.center = geom.p1;
1705 geom.radius = (geom.p2 - geom.p1).length();
1706 geom.isDiameter = (this->datumtype.getValue() == DIAMETER);
1707
1708 if (geom.isDiameter) {
1709 geom.center = (geom.p1 + geom.p2) / 2;
1710 geom.radius = geom.radius / 2;
1711 }
1712
1713 geom.dir.normalize();
1714 geom.normal = SbVec3f(-geom.dir[1], geom.dir[0], 0);
1715
1716 float length = this->param1.getValue();
1717 geom.pos = geom.p2 + length * geom.dir;
1718
1719 // Get magnitude of angle between horizontal
1720 geom.angle = atan2f(geom.dir[1], geom.dir[0]);
1721 if (geom.angle > std::numbers::pi / 2 + std::numbers::pi / 12) {
1722 geom.angle -= (float)std::numbers::pi;
1723 }
1724 else if (geom.angle <= -std::numbers::pi / 2 + std::numbers::pi / 12) {
1725 geom.angle += (float)std::numbers::pi;
1726 }
1727
1728 geom.textOffset = geom.pos;
1729
1730 geom.margin = this->imgHeight / 3.0F;
1731
1732 // Create the first arrowhead
1733 geom.arrowWidth = geom.margin * 0.5F;
1734 geom.ar0 = geom.p2;
1735 geom.ar1 = geom.p2 - geom.dir * 0.866F * 2 * geom.margin;
1736 geom.ar2 = geom.ar1 + geom.normal * geom.arrowWidth;
1737 geom.ar1 -= geom.normal * geom.arrowWidth;
1738
1739 SbVec3f p3 = geom.pos + geom.dir * (this->imgWidth / 2 + geom.margin);
1740 if ((p3 - geom.p1).length() > (geom.p2 - geom.p1).length()) {
1741 geom.p2 = p3;
1742 }
1743
1744 // Calculate the line segment points around text
1745 geom.pnt1 = geom.pos - geom.dir * (geom.margin + this->imgWidth / 2);
1746 geom.pnt2 = geom.pos + geom.dir * (geom.margin + this->imgWidth / 2);
1747
1748 if (geom.isDiameter) {
1749 // Create second arrowhead for diameter
1750 geom.ar0_1 = geom.p1;
1751 geom.ar1_1 = geom.p1 + geom.dir * 0.866F * 2 * geom.margin;
1752 geom.ar2_1 = geom.ar1_1 + geom.normal * geom.arrowWidth;
1753 geom.ar1_1 -= geom.normal * geom.arrowWidth;
1754 }
1755
1756 // Arc helper parameters
1757 geom.startAngle = this->param3.getValue();
1758 geom.startRange = this->param4.getValue();
1759
1760 geom.endAngle = this->param5.getValue();
1761 geom.endRange = this->param6.getValue();
1762
1763 return geom;
1764 }
1765
1766 SoDatumLabel::AngleGeometry SoDatumLabel::calculateAngleGeometry(const SbVec3f* points) const
1767 {
1768 AngleGeometry geom;
1769
1770 // only the angle intersection point is needed
1771 geom.p0 = points[0];
1772
1773 geom.margin = this->imgHeight / 3.0F;
1774
1775 // load the parameters
1776 geom.length = this->param1.getValue();
1777 geom.startangle = this->param2.getValue();
1778 geom.range = this->param3.getValue();
1779 geom.endangle = geom.startangle + geom.range;
1780 geom.endLineLength1 = std::max(this->param4.getValue(), geom.margin);
1781 geom.endLineLength2 = std::max(this->param5.getValue(), geom.margin);
1782 geom.endLineLength12 = std::max(-this->param4.getValue(), geom.margin);
1783 geom.endLineLength22 = std::max(-this->param5.getValue(), geom.margin);
1784
1785 geom.r = 2 * geom.length;
1786
1787 // set the text label angle to zero
1788 geom.angle = 0.F;
1789
1790 // useful information
1791 // v0 - vector for text position
1792 // p0 - vector for angle intersect
1793 geom.v0 = SbVec3f(cos(geom.startangle + geom.range / 2), sin(geom.startangle + geom.range / 2), 0);
1794
1795 // leave some space for the text
1796 geom.textMargin = std::min(0.2F * abs(geom.range), this->imgWidth / (2 * geom.r));
1797
1798 geom.textOffset = geom.p0 + geom.v0 * geom.r;
1799
1800 // direction vectors for start and end lines
1801 geom.v1 = SbVec3f(cos(geom.startangle), sin(geom.startangle), 0);
1802 geom.v2 = SbVec3f(cos(geom.endangle), sin(geom.endangle), 0);
1803
1804 if (geom.range < 0 || geom.length < 0) {
1805 std::swap(geom.v1, geom.v2);
1806 geom.textMargin = -geom.textMargin;
1807 }
1808
1809 geom.pnt1 = geom.p0 + (geom.r - geom.endLineLength1) * geom.v1;
1810 geom.pnt2 = geom.p0 + (geom.r + geom.endLineLength12) * geom.v1;
1811 geom.pnt3 = geom.p0 + (geom.r - geom.endLineLength2) * geom.v2;
1812 geom.pnt4 = geom.p0 + (geom.r + geom.endLineLength22) * geom.v2;
1813
1814 // create the arrowheads
1815 geom.arrowLength = geom.margin * 2;
1816 geom.arrowWidth = geom.margin * 0.5F;
1817
1818 geom.dirStart = SbVec3f(geom.v1[1], -geom.v1[0], 0);
1819 geom.startArrowBase = geom.p0 + geom.r * geom.v1;
1820
1821 geom.dirEnd = SbVec3f(-geom.v2[1], geom.v2[0], 0);
1822 geom.endArrowBase = geom.p0 + geom.r * geom.v2;
1823
1824 return geom;
1825 }
1826
1827 SoDatumLabel::SymmetricGeometry SoDatumLabel::calculateSymmetricGeometry(const SbVec3f* points) const
1828 {
1829 SymmetricGeometry geom;
1830
1831 geom.p1 = points[0];
1832 geom.p2 = points[1];
1833
1834 geom.dir = (geom.p2 - geom.p1);
1835 geom.dir.normalize();
1836 geom.normal = SbVec3f(-geom.dir[1], geom.dir[0], 0);
1837
1838 geom.margin = this->imgHeight / 4.0F;
1839
1840 // calculate coordinates for the first arrow
1841 geom.ar0 = geom.p1 + geom.dir * 4 * geom.margin; // tip of arrow
1842 geom.ar1 = geom.ar0 - geom.dir * 0.866F * 2 * geom.margin;
1843 geom.ar2 = geom.ar1 + geom.normal * geom.margin;
1844 geom.ar1 -= geom.normal * geom.margin;
1845
1846 // calculate coordinates for the second arrow
1847 geom.ar3 = geom.p2 - geom.dir * 4 * geom.margin; // tip of 2nd arrow
1848 geom.ar4 = geom.ar3 + geom.dir * 0.866F * 2 * geom.margin;
1849 geom.ar5 = geom.ar4 + geom.normal * geom.margin;
1850 geom.ar4 -= geom.normal * geom.margin;
1851
1852 return geom;
1853 }
1854
1855 void SoDatumLabel::generateLineSelectionPrimitive(
1856 SoAction* action,
1857 const SbVec3f& start,
1858 const SbVec3f& end,
1859 float width
1860 )
1861 {
1862 // create a thicker line used for selection
1863 SbVec3f dir = end - start;
1864 dir.normalize();
1865 SbVec3f perp = SbVec3f(-dir[1], dir[0], 0) * (width / 2.0f);
1866
1867 SbVec3f p1 = start + perp;
1868 SbVec3f p2 = start - perp;
1869 SbVec3f p3 = end + perp;
1870 SbVec3f p4 = end - perp;
1871
1872 SoPrimitiveVertex pv;
1873 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
1874
1875 this->beginShape(action, TRIANGLE_STRIP);
1876 pv.setPoint(p1);
1877 shapeVertex(&pv);
1878 pv.setPoint(p2);
1879 shapeVertex(&pv);
1880 pv.setPoint(p3);
1881 shapeVertex(&pv);
1882 pv.setPoint(p4);
1883 shapeVertex(&pv);
1884 this->endShape();
1885 }
1886
1887 void SoDatumLabel::generateArcSelectionPrimitive(
1888 SoAction* action,
1889 const SbVec3f& center,
1890 float radius,
1891 float startAngle,
1892 float endAngle,
1893 float width
1894 )
1895 {
1896 // create selectable arc by generating line segments
1897 int countSegments = std::max(6, abs(int(50.0 * (endAngle - startAngle) / (2 * std::numbers::pi))));
1898 double segment = (endAngle - startAngle) / (countSegments - 1);
1899
1900 for (int i = 0; i < countSegments - 1; i++) {
1901 double theta1 = startAngle + segment * i;
1902 double theta2 = startAngle + segment * (i + 1);
1903 SbVec3f v1 = center + SbVec3f(radius * cos(theta1), radius * sin(theta1), 0);
1904 SbVec3f v2 = center + SbVec3f(radius * cos(theta2), radius * sin(theta2), 0);
1905 generateLineSelectionPrimitive(action, v1, v2, width);
1906 }
1907 }
1908
1909 void SoDatumLabel::generateArrowSelectionPrimitive(
1910 SoAction* action,
1911 const SbVec3f& base,
1912 const SbVec3f& dir,
1913 float width,
1914 float length
1915 )
1916 {
1917 // create selectable arrow as a triangle at elevated Z for selection above geometry
1918 SbVec3f tip = base + dir * length;
1919 SbVec3f perp = SbVec3f(-dir[1], dir[0], 0) * (width / 2.0f);
1920
1921 SbVec3f p1 = base + perp;
1922 SbVec3f p2 = base - perp;
1923
1924 SoPrimitiveVertex pv;
1925 pv.setNormal(SbVec3f(0.F, 0.F, 1.F));
1926
1927 this->beginShape(action, TRIANGLES);
1928 pv.setPoint(SbVec3f(tip[0], tip[1], ZARROW_TEXT_OFFSET));
1929 shapeVertex(&pv);
1930 pv.setPoint(SbVec3f(p1[0], p1[1], ZARROW_TEXT_OFFSET));
1931 shapeVertex(&pv);
1932 pv.setPoint(SbVec3f(p2[0], p2[1], ZARROW_TEXT_OFFSET));
1933 shapeVertex(&pv);
1934 this->endShape();
1935 }
1936
1937 SoDatumLabel::ArcLengthGeometry SoDatumLabel::calculateArcLengthGeometry(const SbVec3f* points) const
1938 {
1939 using std::numbers::pi;
1940
1941 ArcLengthGeometry geom;
1942
1943 geom.ctr = points[0];
1944 geom.p1 = points[1];
1945 geom.p2 = points[2];
1946 geom.length = this->param1.getValue();
1947
1948 geom.margin = this->imgHeight / 3.0F;
1949
1950 // angles calculations
1951 SbVec3f vc1 = (geom.p1 - geom.ctr);
1952 SbVec3f vc2 = (geom.p2 - geom.ctr);
1953
1954 geom.startangle = atan2f(vc1[1], vc1[0]);
1955 geom.endangle = atan2f(vc2[1], vc2[0]);
1956 if (geom.endangle < geom.startangle) {
1957 geom.endangle += 2.0F * (float)pi;
1958 }
1959
1960 geom.range = geom.endangle - geom.startangle;
1961 geom.radius = vc1.length();
1962
1963 // text orientation
1964 SbVec3f dir = (geom.p2 - geom.p1);
1965 dir.normalize();
1966 // get magnitude of angle between horizontal
1967 geom.angle = atan2f(dir[1], dir[0]);
1968 if (geom.angle > pi / 2 + pi / 12) {
1969 geom.angle -= (float)pi;
1970 }
1971 else if (geom.angle <= -pi / 2 + pi / 12) {
1972 geom.angle += (float)pi;
1973 }
1974
1975 // text location
1976 geom.textOffset = getLabelTextCenterArcLength(geom.ctr, geom.p1, geom.p2);
1977
1978 // lines direction
1979 geom.vm = (geom.p1 + geom.p2) / 2 - geom.ctr;
1980 geom.vm.normalize();
1981
1982 // determine if this is a large arc (> pi)
1983 geom.isLargeArc = (geom.range > pi);
1984
1985 // lines points
1986 geom.pnt1 = geom.p1;
1987 geom.pnt3 = geom.p2;
1988
1989 if (geom.isLargeArc) {
1990 geom.pnt2 = geom.p1 + geom.length * geom.vm;
1991 geom.pnt4 = geom.p2 + geom.length * geom.vm;
1992
1993 // recalculate angles for the outer arc
1994 SbVec3f vc1_outer = (geom.pnt2 - geom.ctr);
1995 SbVec3f vc2_outer = (geom.pnt4 - geom.ctr);
1996 geom.arcCenter = geom.ctr;
1997 geom.arcRadius = vc1_outer.length();
1998 // update angles for outer arc
1999 geom.startangle = atan2f(vc1_outer[1], vc1_outer[0]);
2000 geom.endangle = atan2f(vc2_outer[1], vc2_outer[0]);
2001 }
2002 else {
2003 geom.pnt2 = geom.p1 + (geom.length - geom.radius) * geom.vm;
2004 geom.pnt4 = geom.p2 + (geom.length - geom.radius) * geom.vm;
2005
2006 // arc center and radius for inner arc
2007 geom.arcCenter = geom.ctr + (geom.length - geom.radius) * geom.vm;
2008 geom.arcRadius = geom.radius;
2009 }
2010
2011 // normals for the arrowheads at arc start and end
2012 geom.dirStart = SbVec3f(sin(geom.startangle), -cos(geom.startangle), 0);
2013 geom.dirEnd = SbVec3f(-sin(geom.endangle), cos(geom.endangle), 0);
2014
2015 return geom;
2016 }