-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
DrawUtil.cpp
2001 lines (1769 loc) · 66.6 KB
/
DrawUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2015 WandererFan <wandererfan@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <boost_regex.hpp>
#include <QChar>
#include <QPointF>
#include <QString>
#include <BRepAdaptor_Curve.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepBndLib.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepExtrema_DistShapeShape.hxx>
#include <BRepLProp_CLProps.hxx>
#include <BRepLProp_CurveTool.hxx>
#include <BRepLProp_SLProps.hxx>
#include <BRepTools.hxx>
#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <GCPnts_AbscissaPoint.hxx>
#include <GeomAPI_ExtremaCurveCurve.hxx>
#include <Precision.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <gp_Ax3.hxx>
#include <gp_Dir.hxx>
#include <gp_Elips.hxx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#endif
#include <Base/Console.h>
#include <Base/FileInfo.h>
#include <Base/Parameter.h>
#include <Base/Stream.h>
#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <Base/Vector3D.h>
#include "DrawUtil.h"
#include "GeometryObject.h"
#include "LineGroup.h"
#include "Preferences.h"
#include "DrawViewPart.h"
using namespace TechDraw;
/*static*/ int DrawUtil::getIndexFromName(const std::string& geomName)
{
// Base::Console().Message("DU::getIndexFromName(%s)\n", geomName.c_str());
boost::regex re("\\d+$");// one of more digits at end of string
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
// char* endChar;
std::string::const_iterator begin = geomName.begin();
auto pos = geomName.rfind('.');
if (pos != std::string::npos) {
begin += pos + 1;
}
std::string::const_iterator end = geomName.end();
std::stringstream ErrorMsg;
if (geomName.empty()) {
throw Base::ValueError("getIndexFromName - empty geometry name");
}
if (boost::regex_search(begin, end, what, re, flags)) {
return int(std::stoi(what.str()));
} else {
ErrorMsg << "getIndexFromName: malformed geometry name - " << geomName;
throw Base::ValueError(ErrorMsg.str());
}
}
std::string DrawUtil::getGeomTypeFromName(const std::string& geomName)
{
if (geomName.empty()) {
throw Base::ValueError("getGeomTypeFromName - empty geometry name");
}
boost::regex re("^[a-zA-Z]*");//one or more letters at start of string
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
std::string::const_iterator begin = geomName.begin();
auto pos = geomName.rfind('.');
if (pos != std::string::npos) {
begin += pos + 1;
}
std::string::const_iterator end = geomName.end();
std::stringstream ErrorMsg;
if (boost::regex_search(begin, end, what, re, flags)) {
return what.str();
} else {
ErrorMsg << "In getGeomTypeFromName: malformed geometry name - " << geomName;
throw Base::ValueError(ErrorMsg.str());
}
}
std::string DrawUtil::makeGeomName(const std::string& geomType, int index)
{
std::stringstream newName;
newName << geomType << index;
return newName.str();
}
//! true if v1 and v2 are the same geometric point within tolerance
bool DrawUtil::isSamePoint(TopoDS_Vertex v1, TopoDS_Vertex v2, double tolerance)
{
gp_Pnt p1 = BRep_Tool::Pnt(v1);
gp_Pnt p2 = BRep_Tool::Pnt(v2);
return p1.IsEqual(p2, tolerance);
}
bool DrawUtil::isZeroEdge(TopoDS_Edge e, double tolerance)
{
TopoDS_Vertex vStart = TopExp::FirstVertex(e);
TopoDS_Vertex vEnd = TopExp::LastVertex(e);
if (!isSamePoint(vStart, vEnd, tolerance)) {
return false;
}
//closed edge will have same V's but non-zero length
BRepAdaptor_Curve adapt(e);
double len = GCPnts_AbscissaPoint::Length(adapt, Precision::Confusion());
if (len > tolerance) {
return false;
}
return true;
}
double DrawUtil::simpleMinDist(TopoDS_Shape s1, TopoDS_Shape s2)
{
BRepExtrema_DistShapeShape extss(s1, s2);
if (!extss.IsDone()) {
Base::Console().Message("DU::simpleMinDist - BRepExtrema_DistShapeShape failed");
return -1;
}
int count = extss.NbSolution();
if (count != 0) {
return extss.Value();
} else {
return -1;
}
}
//! returns 2d vector's angle with X axis. result is [0, 2pi].
double DrawUtil::angleWithX(Base::Vector3d inVec)
{
double result = atan2(inVec.y, inVec.x);
if (result < 0) {
result += 2.0 * M_PI;
}
return result;
}
//! assumes 2d on XY
//! quick angle for straight edges
double DrawUtil::angleWithX(TopoDS_Edge e, bool reverse)
{
gp_Pnt gstart = BRep_Tool::Pnt(TopExp::FirstVertex(e));
Base::Vector3d start(gstart.X(), gstart.Y(), gstart.Z());
gp_Pnt gend = BRep_Tool::Pnt(TopExp::LastVertex(e));
Base::Vector3d end(gend.X(), gend.Y(), gend.Z());
Base::Vector3d u;
if (reverse) {
u = start - end;
} else {
u = end - start;
}
double result = atan2(u.y, u.x);
if (result < 0) {
result += 2.0 * M_PI;
}
return result;
}
//! find angle of edge with x-Axis at First/LastVertex
double DrawUtil::angleWithX(TopoDS_Edge e, TopoDS_Vertex v, double tolerance)
{
double param = 0;
BRepAdaptor_Curve adapt(e);
if (isFirstVert(e, v, tolerance)) {
param = adapt.FirstParameter();
} else if (isLastVert(e, v, tolerance)) {
param = adapt.LastParameter();
} else {
//TARFU
Base::Console().Message("Error: DU::angleWithX - v is neither first nor last \n");
}
gp_Pnt paramPoint;
gp_Vec derivative;
const Handle(Geom_Curve) c = adapt.Curve().Curve();
c->D1(param, paramPoint, derivative);
double angle = atan2(derivative.Y(), derivative.X());
if (angle < 0) {//map from [-PI:PI] to [0:2PI]
angle += 2.0 * M_PI;
}
return angle;
}
//! find angle of incidence at First/LastVertex
double DrawUtil::incidenceAngleAtVertex(TopoDS_Edge e, TopoDS_Vertex v, double tolerance)
{
double incidenceAngle = 0;
BRepAdaptor_Curve adapt(e);
double paramRange = adapt.LastParameter() - adapt.FirstParameter();
double paramOffset = paramRange / 100.0;
double vertexParam;
Base::Vector3d anglePoint = DrawUtil::vertex2Vector(v);
Base::Vector3d offsetPoint, incidenceVec;
int noTangents = 0;
if (isFirstVert(e, v, tolerance)) {
vertexParam = adapt.FirstParameter();
BRepLProp_CLProps prop(
adapt, vertexParam + paramOffset, noTangents, Precision::Confusion());
const gp_Pnt& gOffsetPoint = prop.Value();
offsetPoint = Base::Vector3d(gOffsetPoint.X(), gOffsetPoint.Y(), gOffsetPoint.Z());
} else if (isLastVert(e, v, tolerance)) {
vertexParam = adapt.LastParameter();
BRepLProp_CLProps prop(
adapt, vertexParam - paramOffset, noTangents, Precision::Confusion());
const gp_Pnt& gOffsetPoint = prop.Value();
offsetPoint = Base::Vector3d(gOffsetPoint.X(), gOffsetPoint.Y(), gOffsetPoint.Z());
} else {
//TARFU
// Base::Console().Message("DU::incidenceAngle - v is neither first nor last \n");
}
incidenceVec = anglePoint - offsetPoint;
incidenceAngle = atan2(incidenceVec.y, incidenceVec.x);
//map to [0:2PI]
if (incidenceAngle < 0.0) {
incidenceAngle = M_2PI + incidenceAngle;
}
return incidenceAngle;
}
bool DrawUtil::isFirstVert(TopoDS_Edge e, TopoDS_Vertex v, double tolerance)
{
TopoDS_Vertex first = TopExp::FirstVertex(e);
return isSamePoint(first, v, tolerance);
}
bool DrawUtil::isLastVert(TopoDS_Edge e, TopoDS_Vertex v, double tolerance)
{
TopoDS_Vertex last = TopExp::LastVertex(e);
return isSamePoint(last, v, tolerance);
}
bool DrawUtil::fpCompare(const double& d1, const double& d2, double tolerance)
{
return std::fabs(d1 - d2) < tolerance;
}
//brute force intersection points of line(point, dir) with box(xRange, yRange)
std::pair<Base::Vector3d, Base::Vector3d>
DrawUtil::boxIntersect2d(Base::Vector3d point, Base::Vector3d dirIn, double xRange, double yRange)
{
std::pair<Base::Vector3d, Base::Vector3d> result;
Base::Vector3d p1, p2;
Base::Vector3d dir = dirIn;
dir.Normalize();
// y = mx + b
// m = (y1 - y0) / (x1 - x0)
if (DrawUtil::fpCompare(dir.x, 0.0)) {//vertical case
p1 = Base::Vector3d(point.x, point.y - (yRange / 2.0), 0.0);
p2 = Base::Vector3d(point.x, point.y + (yRange / 2.0), 0.0);
} else {
double slope = dir.y / dir.x;
double left = -xRange / 2.0;
double right = xRange / 2.0;
if (DrawUtil::fpCompare(slope, 0.0)) {//horizontal case
p1 = Base::Vector3d(point.x - (xRange / 2.0), point.y);
p2 = Base::Vector3d(point.x + (xRange / 2.0), point.y);
} else {//normal case
double top = yRange / 2.0;
double bottom = -yRange / 2.0;
double yLeft = point.y - slope * (point.x - left);
double yRight = point.y - slope * (point.x - right);
double xTop = point.x - ((point.y - top) / slope);
double xBottom = point.x - ((point.y - bottom) / slope);
if ((bottom < yLeft) && (top > yLeft)) {
p1 = Base::Vector3d(left, yLeft);
} else if (yLeft <= bottom) {
p1 = Base::Vector3d(xBottom, bottom);
} else if (yLeft >= top) {
p1 = Base::Vector3d(xTop, top);
}
if ((bottom < yRight) && (top > yRight)) {
p2 = Base::Vector3d(right, yRight);
} else if (yRight <= bottom) {
p2 = Base::Vector3d(xBottom, bottom);
} else if (yRight >= top) {
p2 = Base::Vector3d(xTop, top);
}
}
}
result.first = p1;
result.second = p2;
Base::Vector3d dirCheck = p2 - p1;
dirCheck.Normalize();
if (!dir.IsEqual(dirCheck, 0.00001)) {
result.first = p2;
result.second = p1;
}
return result;
}
//find the apparent intersection of 2 3d curves. We are only interested in curves that are lines, so we will have either 0 or 1
//apparent intersection. The intersection is "apparent" because the curve's progenator is a trimmed curve (line segment)
//NOTE: these curves do not have a location, so the intersection point does not respect the
//placement of the edges which spawned the curves. Use the apparentIntersection(edge, edge) method instead.
bool DrawUtil::apparentIntersection(const Handle(Geom_Curve) curve1,
const Handle(Geom_Curve) curve2, Base::Vector3d& result)
{
GeomAPI_ExtremaCurveCurve intersector(curve1, curve2);
if (intersector.NbExtrema() == 0 || intersector.LowerDistance() > EWTOLERANCE) {
//no intersection
return false;
}
//for our purposes, only one intersection point is required.
gp_Pnt p1, p2;
intersector.Points(1, p1, p2);
result = toVector3d(p1);
return true;
}
bool DrawUtil::apparentIntersection(TopoDS_Edge& edge0, TopoDS_Edge& edge1, gp_Pnt& intersect)
{
gp_Pnt gStart0 = BRep_Tool::Pnt(TopExp::FirstVertex(edge0));
gp_Pnt gEnd0 = BRep_Tool::Pnt(TopExp::LastVertex(edge0));
gp_Pnt gStart1 = BRep_Tool::Pnt(TopExp::FirstVertex(edge1));
gp_Pnt gEnd1 = BRep_Tool::Pnt(TopExp::LastVertex(edge1));
//intersection of 2 3d lines in point&direction form
//https://math.stackexchange.com/questions/270767/find-intersection-of-two-3d-lines
gp_Vec C(gStart0.XYZ());
gp_Vec D(gStart1.XYZ());
gp_Vec e(gEnd0.XYZ() - gStart0.XYZ());//direction of line0
gp_Vec f(gEnd1.XYZ() - gStart1.XYZ());//direction of line1
Base::Console().Message(
"DU::apparentInter - e: %s f: %s\n", formatVector(e).c_str(), formatVector(f).c_str());
//check for cases the algorithm doesn't handle well
gp_Vec C1(gEnd0.XYZ());
gp_Vec D1(gEnd1.XYZ());
if (C.IsEqual(D, EWTOLERANCE, EWTOLERANCE) || C.IsEqual(D1, EWTOLERANCE, EWTOLERANCE)) {
intersect = gp_Pnt(C.XYZ());
return true;
}
if (C1.IsEqual(D, EWTOLERANCE, EWTOLERANCE) || C1.IsEqual(D1, EWTOLERANCE, EWTOLERANCE)) {
intersect = gp_Pnt(C1.XYZ());
return true;
}
gp_Vec g(D - C);//between a point on each line
Base::Console().Message("DU::apparentInter - C: %s D: %s g: %s\n",
formatVector(C).c_str(),
formatVector(D).c_str(),
formatVector(g).c_str());
gp_Vec fxg = f.Crossed(g);
double h = fxg.Magnitude();
gp_Vec fxe = f.Crossed(e);
double k = fxe.Magnitude();
Base::Console().Message("DU::apparentInter - h: %.3f k: %.3f\n", h, k);
if (fpCompare(k, 0.0)) {
//no intersection
return false;
}
gp_Vec el = e * (h / k);
double pm = 1.0;
if (fpCompare(fxg.Dot(fxe), -1.0)) {
//opposite directions
pm = -1.0;
}
intersect = gp_Pnt(C.XYZ() + el.XYZ() * pm);
return true;
}
//find the intersection of 2 lines (point0, dir0) and (point1, dir1)
//existence of an intersection is not checked
bool DrawUtil::intersect2Lines3d(Base::Vector3d point0, Base::Vector3d dir0, Base::Vector3d point1,
Base::Vector3d dir1, Base::Vector3d& intersect)
{
Base::Vector3d g = point1 - point0;
Base::Vector3d fxg = dir1.Cross(g);
Base::Vector3d fxgn = fxg;
fxgn.Normalize();
Base::Vector3d fxe = dir1.Cross(dir0);
Base::Vector3d fxen = fxe;
fxen.Normalize();
Base::Vector3d dir0n = dir0;
dir0n.Normalize();
Base::Vector3d dir1n = dir1;
dir1n.Normalize();
if (fabs(dir0n.Dot(dir1n)) == 1.0) {
//parallel lines, no intersection
Base::Console().Message("DU::intersect2 - parallel lines, no intersection\n");
return false;
}
double scaler = fxg.Length() / fxe.Length();
double direction = -1.0;
if (fxgn == fxen) {
direction = 1.0;
}
intersect = point0 + dir0 * scaler * direction;
return true;
}
Base::Vector3d DrawUtil::vertex2Vector(const TopoDS_Vertex& v)
{
gp_Pnt gp = BRep_Tool::Pnt(v);
return Base::Vector3d(gp.X(), gp.Y(), gp.Z());
}
//TODO: make formatVector using toVector3d
std::string DrawUtil::formatVector(const Base::Vector3d& v)
{
std::stringstream builder;
builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals());
builder << " (" << v.x << ", " << v.y << ", " << v.z << ") ";
return builder.str();
}
std::string DrawUtil::formatVector(const gp_Dir& v)
{
std::stringstream builder;
builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals());
builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") ";
return builder.str();
}
std::string DrawUtil::formatVector(const gp_Dir2d& v)
{
std::stringstream builder;
builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals());
builder << " (" << v.X() << ", " << v.Y() << ") ";
return builder.str();
}
std::string DrawUtil::formatVector(const gp_Vec& v)
{
std::stringstream builder;
builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals());
builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") ";
return builder.str();
}
std::string DrawUtil::formatVector(const gp_Pnt& v)
{
std::stringstream builder;
builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals());
builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") ";
return builder.str();
}
std::string DrawUtil::formatVector(const gp_Pnt2d& v)
{
std::stringstream builder;
builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals());
builder << " (" << v.X() << ", " << v.Y() << ") ";
return builder.str();
}
std::string DrawUtil::formatVector(const QPointF& v)
{
std::stringstream builder;
builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals());
builder << " (" << v.x() << ", " << v.y() << ") ";
return builder.str();
}
//! compare 2 vectors for sorting - true if v1 < v2
//! precision::Confusion() is too strict for vertex - vertex comparisons
bool DrawUtil::vectorLess(const Base::Vector3d& v1, const Base::Vector3d& v2)
{
if ((v1 - v2).Length() > EWTOLERANCE) {//ie v1 != v2
if (!DrawUtil::fpCompare(v1.x, v2.x, 2.0 * EWTOLERANCE)) {
return (v1.x < v2.x);
} else if (!DrawUtil::fpCompare(v1.y, v2.y, 2.0 * EWTOLERANCE)) {
return (v1.y < v2.y);
} else {
return (v1.z < v2.z);
}
}
return false;
}
//! test for equality of two vertexes using the vectorLess comparator as used
//! in sorts and containers
bool DrawUtil::vertexEqual(TopoDS_Vertex& v1, TopoDS_Vertex& v2)
{
gp_Pnt gv1 = BRep_Tool::Pnt(v1);
gp_Pnt gv2 = BRep_Tool::Pnt(v2);
Base::Vector3d vv1(gv1.X(), gv1.Y(), gv1.Z());
Base::Vector3d vv2(gv2.X(), gv2.Y(), gv2.Z());
return vectorEqual(vv1, vv2);
}
//! test for equality of two vectors using the vectorLess comparator as used
//! in sorts and containers
bool DrawUtil::vectorEqual(Base::Vector3d& v1, Base::Vector3d& v2)
{
bool less1 = vectorLess(v1, v2);
bool less2 = vectorLess(v2, v1);
return !less1 && !less2;
}
//TODO: the next 2 could be templated
//construct a compound shape from a list of edges
TopoDS_Shape DrawUtil::vectorToCompound(std::vector<TopoDS_Edge> vecIn, bool invert)
{
BRep_Builder builder;
TopoDS_Compound compOut;
builder.MakeCompound(compOut);
for (auto& v : vecIn) {
builder.Add(compOut, v);
}
if (invert) {
return ShapeUtils::mirrorShape(compOut);
}
return compOut;
}
//construct a compound shape from a list of wires
TopoDS_Shape DrawUtil::vectorToCompound(std::vector<TopoDS_Wire> vecIn, bool invert)
{
BRep_Builder builder;
TopoDS_Compound compOut;
builder.MakeCompound(compOut);
for (auto& v : vecIn) {
builder.Add(compOut, v);
}
if (invert) {
return ShapeUtils::mirrorShape(compOut);
}
return compOut;
}
// construct a compound shape from a list of shapes
// this version needs a different name since edges/wires are shapes
TopoDS_Shape DrawUtil::shapeVectorToCompound(std::vector<TopoDS_Shape> vecIn, bool invert)
{
BRep_Builder builder;
TopoDS_Compound compOut;
builder.MakeCompound(compOut);
for (auto& v : vecIn) {
if (!v.IsNull()) {
builder.Add(compOut, v);
}
}
if (invert) {
return ShapeUtils::mirrorShape(compOut);
}
return compOut;
}
//constructs a list of edges from a shape
std::vector<TopoDS_Edge> DrawUtil::shapeToVector(TopoDS_Shape shapeIn)
{
std::vector<TopoDS_Edge> vectorOut;
TopExp_Explorer expl(shapeIn, TopAbs_EDGE);
for (; expl.More(); expl.Next()) {
vectorOut.push_back(TopoDS::Edge(expl.Current()));
}
return vectorOut;
}
//!convert fromPoint in coordinate system fromSystem to reference coordinate system
Base::Vector3d DrawUtil::toR3(const gp_Ax2& fromSystem, const Base::Vector3d& fromPoint)
{
gp_Pnt gFromPoint(fromPoint.x, fromPoint.y, fromPoint.z);
gp_Trsf T;
gp_Ax3 gRef;
gp_Ax3 gFrom(fromSystem);
T.SetTransformation(gFrom, gRef);
gp_Pnt gToPoint = gFromPoint.Transformed(T);
Base::Vector3d toPoint(gToPoint.X(), gToPoint.Y(), gToPoint.Z());
return toPoint;
}
//! check if two vectors are parallel. Vectors don't have to be unit vectors
bool DrawUtil::checkParallel(const Base::Vector3d v1, Base::Vector3d v2, double tolerance)
{
double dot = fabs(v1.Dot(v2));
double mag = v1.Length() * v2.Length();
return DrawUtil::fpCompare(dot, mag, tolerance);
}
//! rotate vector by angle radians around axis through org
Base::Vector3d DrawUtil::vecRotate(Base::Vector3d vec, double angle, Base::Vector3d axis,
Base::Vector3d org)
{
Base::Matrix4D xForm;
xForm.rotLine(org, axis, angle);
return Base::Vector3d(xForm * (vec));
}
gp_Vec DrawUtil::closestBasis(gp_Vec inVec)
{
return gp_Vec(togp_Dir(closestBasis(toVector3d(inVec))));
}
//! returns stdX, stdY or stdZ.
Base::Vector3d DrawUtil::closestBasis(Base::Vector3d v)
{
Base::Vector3d result(0.0, -1, 0);
Base::Vector3d stdX(1.0, 0.0, 0.0);
Base::Vector3d stdY(0.0, 1.0, 0.0);
Base::Vector3d stdZ(0.0, 0.0, 1.0);
Base::Vector3d stdXr(-1.0, 0.0, 0.0);
Base::Vector3d stdYr(0.0, -1.0, 0.0);
Base::Vector3d stdZr(0.0, 0.0, -1.0);
//first check if already a basis
if (v.Dot(stdX) == 1.0 || v.Dot(stdY) == 1.0 || v.Dot(stdZ) == 1.0) {
return v;
}
if (v.Dot(stdX) == -1.0 || v.Dot(stdY) == -1.0 || v.Dot(stdZ) == -1.0) {
return -v;
}
//not a basis. find smallest angle with a basis.
double angleX, angleY, angleZ, angleXr, angleYr, angleZr, angleMin;
angleX = stdX.GetAngle(v);
angleY = stdY.GetAngle(v);
angleZ = stdZ.GetAngle(v);
angleXr = stdXr.GetAngle(v);
angleYr = stdYr.GetAngle(v);
angleZr = stdZr.GetAngle(v);
angleMin = std::min({angleX, angleY, angleZ, angleXr, angleYr, angleZr});
if (angleX == angleMin) {
return Base::Vector3d(1.0, 0.0, 0.0);
}
if (angleY == angleMin) {
return Base::Vector3d(0.0, 1.0, 0.0);
}
if (angleZ == angleMin) {
return Base::Vector3d(0.0, 0.0, 1.0);
}
if (angleXr == angleMin) {
return Base::Vector3d(1.0, 0.0, 0.0);
}
if (angleYr == angleMin) {
return Base::Vector3d(0.0, 1.0, 0.0);
}
if (angleZr == angleMin) {
return Base::Vector3d(0.0, 0.0, 1.0);
}
//should not get to here
return Base::Vector3d(1.0, 0.0, 0.0);
}
//! returns +/- stdX, stdY or stdZ.
Base::Vector3d DrawUtil::closestBasisOriented(Base::Vector3d v)
{
Base::Vector3d result(0.0, -1, 0);
Base::Vector3d stdX(1.0, 0.0, 0.0);
Base::Vector3d stdY(0.0, 1.0, 0.0);
Base::Vector3d stdZ(0.0, 0.0, 1.0);
Base::Vector3d stdXr(-1.0, 0.0, 0.0);
Base::Vector3d stdYr(0.0, -1.0, 0.0);
Base::Vector3d stdZr(0.0, 0.0, -1.0);
//first check if already a basis
if (v.Dot(stdX) == 1.0 || v.Dot(stdY) == 1.0 || v.Dot(stdZ) == 1.0) {
return v;
}
if (v.Dot(stdX) == -1.0 || v.Dot(stdY) == -1.0 || v.Dot(stdZ) == -1.0) {
return v;
}
//not a basis. find smallest angle with a basis.
double angleX, angleY, angleZ, angleXr, angleYr, angleZr, angleMin;
angleX = stdX.GetAngle(v);
angleY = stdY.GetAngle(v);
angleZ = stdZ.GetAngle(v);
angleXr = stdXr.GetAngle(v);
angleYr = stdYr.GetAngle(v);
angleZr = stdZr.GetAngle(v);
angleMin = std::min({angleX, angleY, angleZ, angleXr, angleYr, angleZr});
if (angleX == angleMin) {
return Base::Vector3d(1.0, 0.0, 0.0);
}
if (angleY == angleMin) {
return Base::Vector3d(0.0, 1.0, 0.0);
}
if (angleZ == angleMin) {
return Base::Vector3d(0.0, 0.0, 1.0);
}
if (angleXr == angleMin) {
return Base::Vector3d(-1.0, 0.0, 0.0);
}
if (angleYr == angleMin) {
return Base::Vector3d(0.0, -1.0, 0.0);
}
if (angleZr == angleMin) {
return Base::Vector3d(0.0, 0.0, -1.0);
}
//should not get to here
return Base::Vector3d(1.0, 0.0, 0.0);
}
Base::Vector3d DrawUtil::closestBasis(Base::Vector3d vDir, gp_Ax2 coordSys)
{
gp_Dir gDir(vDir.x, vDir.y, vDir.z);
return closestBasis(gDir, coordSys);
}
Base::Vector3d DrawUtil::closestBasis(gp_Dir gDir, gp_Ax2 coordSys)
{
gp_Dir xCS = coordSys.XDirection();
gp_Dir yCS = coordSys.YDirection();
gp_Dir zCS = coordSys.Direction();
//first check if already a basis
if (gDir.Dot(xCS) == 1.0 || gDir.Dot(yCS) == 1.0 || gDir.Dot(zCS) == 1.0) {
//gDir is parallel with a basis
return Base::Vector3d(gDir.X(), gDir.Y(), gDir.Z());
}
if (gDir.Dot(xCS.Reversed()) == 1.0 || gDir.Dot(yCS.Reversed()) == 1.0
|| gDir.Dot(zCS.Reversed()) == 1.0) {
//gDir is anti-parallel with a basis
return Base::Vector3d(-gDir.X(), -gDir.Y(), -gDir.Z());
}
//not a basis. find smallest angle with a basis.
double angleX, angleY, angleZ, angleXr, angleYr, angleZr, angleMin;
angleX = gDir.Angle(xCS);
angleY = gDir.Angle(yCS);
angleZ = gDir.Angle(zCS);
angleXr = gDir.Angle(xCS.Reversed());
angleYr = gDir.Angle(yCS.Reversed());
angleZr = gDir.Angle(zCS.Reversed());
angleMin = std::min({angleX, angleY, angleZ, angleXr, angleYr, angleZr});
if (angleX == angleMin) {
return Base::Vector3d(xCS.X(), xCS.Y(), xCS.Z());
}
if (angleY == angleMin) {
return Base::Vector3d(yCS.X(), yCS.Y(), yCS.Z());
}
if (angleZ == angleMin) {
return Base::Vector3d(zCS.X(), zCS.Y(), zCS.Z());
}
if (angleXr == angleMin) {
return Base::Vector3d(-xCS.X(), -xCS.Y(), -xCS.Z());
}
if (angleYr == angleMin) {
return Base::Vector3d(-yCS.X(), -yCS.Y(), -yCS.Z());
}
if (angleZr == angleMin) {
return Base::Vector3d(-zCS.X(), -zCS.Y(), -zCS.Z());
}
//should not get to here
return Base::Vector3d(xCS.X(), xCS.Y(), xCS.Z());
}
//! find the size of a shape measured in a given (cardinal) direction
double DrawUtil::getWidthInDirection(gp_Dir direction, TopoDS_Shape& shape)
{
Base::Vector3d stdX(1.0, 0.0, 0.0);
Base::Vector3d stdY(0.0, 1.0, 0.0);
Base::Vector3d stdZ(0.0, 0.0, 1.0);
Base::Vector3d stdXr(-1.0, 0.0, 0.0);
Base::Vector3d stdYr(0.0, -1.0, 0.0);
Base::Vector3d stdZr(0.0, 0.0, -1.0);
Base::Vector3d vClosestBasis = closestBasis(toVector3d(direction));
Bnd_Box shapeBox;
shapeBox.SetGap(0.0);
BRepBndLib::AddOptimal(shape, shapeBox);
double xMin = 0, xMax = 0, yMin = 0, yMax = 0, zMin = 0, zMax = 0;
if (shapeBox.IsVoid()) {
//this really shouldn't happen here as null shapes should have been caught
//long before this
Base::Console().Error("DU::getWidthInDirection - shapeBox is void\n");
return 0.0;
}
shapeBox.Get(xMin, yMin, zMin, xMax, yMax, zMax);
if (vClosestBasis.IsEqual(stdX, EWTOLERANCE) || vClosestBasis.IsEqual(stdXr, EWTOLERANCE)) {
return xMax - xMin;
}
if (vClosestBasis.IsEqual(stdY, EWTOLERANCE) || vClosestBasis.IsEqual(stdYr, EWTOLERANCE)) {
return yMax - yMin;
}
if (vClosestBasis.IsEqual(stdZ, EWTOLERANCE) || vClosestBasis.IsEqual(stdZr, EWTOLERANCE)) {
return zMax - zMin;
}
return 0.0;
}
//! mask off one component of the input vector. input vector (a, b, c) with
//! direction to mask (0, 1, 0) would return (a, 0.0, c). The mask is a
//! cardinal direction or the reverse of a cardinal direction.
gp_Vec DrawUtil::maskDirection(gp_Vec inVec, gp_Dir directionToMask)
{
if (fpCompare(std::fabs(directionToMask.Dot(gp::OX().Direction().XYZ())), 1.0, EWTOLERANCE)) {
return {0.0, inVec.Y(), inVec.Z()};
}
if (fpCompare(std::fabs(directionToMask.Dot(gp::OY().Direction().XYZ())), 1.0, EWTOLERANCE)) {
return {inVec.X(), 0.0, inVec.Z()};
}
if (fpCompare(std::fabs(directionToMask.Dot(gp::OZ().Direction().XYZ())), 1.0, EWTOLERANCE)) {
return {inVec.X(), inVec.Y(), 0.0};
}
Base::Console().Message("DU:maskDirection - directionToMask is not cardinal\n");
return {};
}
Base::Vector3d DrawUtil::maskDirection(Base::Vector3d inVec, Base::Vector3d directionToMask)
{
return toVector3d(maskDirection(togp_Vec(inVec), togp_Vec(directionToMask)));
}
//! get the coordinate of inPoint for the cardinal unit direction.
double DrawUtil::coordinateForDirection(Base::Vector3d inPoint, Base::Vector3d cardinal)
{
auto masked = maskDirection(inPoint, cardinal);
auto stripped = inPoint - masked;
return stripped.x + stripped.y + stripped.z;
}
//based on Function provided by Joe Dowsett, 2014
double DrawUtil::sensibleScale(double working_scale)
{
if (!(working_scale > 0.0)) {
return 1.0;
}
//which gives the largest scale for which the min_space requirements can be met, but we want a 'sensible' scale, rather than 0.28457239...
//eg if working_scale = 0.115, then we want to use 0.1, similarly 7.65 -> 5, and 76.5 -> 50
float exponent = std::floor(std::log10(working_scale));//if working_scale = a * 10^b, what is b?
working_scale *= std::pow(10, -exponent); //now find what 'a' is.
//int choices = 10;
float valid_scales[2][10] = {{1.0,
1.25,
2.0,
2.5,
3.75,
5.0,
7.5,
10.0,
50.0,
100.0},//equate to 1:10, 1:8, 1:5, 1:4, 3:8, 1:2, 3:4, 1:1
// .1 .125 .375 .75
{1.0,
1.5,
2.0,
3.0,
4.0,
5.0,
8.0,
10.0,
50.0,
100.0}};//equate to 1:1, 3:2, 2:1, 3:1, 4:1, 5:1, 8:1, 10:1
// 1.5:1
//int i = choices - 1;
int i = 9;
while (valid_scales[(exponent >= 0)][i]
> working_scale) {//choose closest value smaller than 'a' from list.
i -= 1; //choosing top list if exponent -ve, bottom list for +ve exponent
}
//now have the appropriate scale, reapply the *10^b
return valid_scales[(exponent >= 0)][i] * pow(10, exponent);
}
double DrawUtil::getDefaultLineWeight(std::string lineType)
{
return TechDraw::LineGroup::getDefaultWidth(lineType);
}
bool DrawUtil::isBetween(const Base::Vector3d pt, const Base::Vector3d end1,
const Base::Vector3d end2)
{
double segLength = (end2 - end1).Length();
double l1 = (pt - end1).Length();
double l2 = (pt - end2).Length();
if (fpCompare(segLength, l1 + l2)) {
return true;
}
return false;
}
Base::Vector3d DrawUtil::Intersect2d(Base::Vector3d p1, Base::Vector3d d1, Base::Vector3d p2,
Base::Vector3d d2)
{
Base::Vector3d p12(p1.x + d1.x, p1.y + d1.y, 0.0);
double A1 = d1.y;
double B1 = -d1.x;
double C1 = A1 * p1.x + B1 * p1.y;
Base::Vector3d p22(p2.x + d2.x, p2.y + d2.y, 0.0);
double A2 = d2.y;
double B2 = -d2.x;
double C2 = A2 * p2.x + B2 * p2.y;
double det = A1 * B2 - A2 * B1;
if (fpCompare(det, 0.0, Precision::Confusion())) {
Base::Console().Message("Lines are parallel\n");
return Base::Vector3d(0.0, 0.0, 0.0);
}
double x = (B2 * C1 - B1 * C2) / det;
double y = (A1 * C2 - A2 * C1) / det;
return Base::Vector3d(x, y, 0.0);
}
Base::Vector2d DrawUtil::Intersect2d(Base::Vector2d p1, Base::Vector2d d1, Base::Vector2d p2,
Base::Vector2d d2)
{
Base::Vector2d p12(p1.x + d1.x, p1.y + d1.y);
double A1 = d1.y;
double B1 = -d1.x;
double C1 = A1 * p1.x + B1 * p1.y;
Base::Vector2d p22(p2.x + d2.x, p2.y + d2.y);
double A2 = d2.y;
double B2 = -d2.x;
double C2 = A2 * p2.x + B2 * p2.y;
double det = A1 * B2 - A2 * B1;
if (fpCompare(det, 0.0, Precision::Confusion())) {
Base::Console().Message("Lines are parallel\n");
return Base::Vector2d(0.0, 0.0);
}
double x = (B2 * C1 - B1 * C2) / det;
double y = (A1 * C2 - A2 * C1) / det;