-
Notifications
You must be signed in to change notification settings - Fork 553
Expand file tree
/
Copy pathdrawconstraint.cpp
More file actions
1363 lines (1195 loc) · 52.9 KB
/
drawconstraint.cpp
File metadata and controls
1363 lines (1195 loc) · 52.9 KB
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
//-----------------------------------------------------------------------------
// Given a constraint, draw a graphical and user-selectable representation
// of that constraint on-screen. We can either draw with gl, or compute the
// distance from a point (the location of the mouse pointer) to the lines
// that we would have drawn, for selection.
//
// Copyright 2008-2013 Jonathan Westhues.
//-----------------------------------------------------------------------------
#include "solvespace.h"
std::string Constraint::Label() const {
std::string result;
if(type == Type::ANGLE) {
result = SS.DegreeToString(valA) + "°";
} else if(type == Type::LENGTH_RATIO || type == Type::ARC_ARC_LEN_RATIO || type == Type::ARC_LINE_LEN_RATIO) {
result = ssprintf("%.3f:1", valA);
} else if(type == Type::COMMENT) {
result = comment;
} else if(type == Type::DIAMETER) {
if(!other) {
result = "⌀" + SS.MmToStringSI(valA);
} else {
result = "R" + SS.MmToStringSI(valA / 2);
}
} else {
// valA has units of distance
result = SS.MmToStringSI(fabs(valA));
}
if(reference) {
result += " REF";
}
return result;
}
void Constraint::DoLine(Canvas *canvas, Canvas::hStroke hcs, Vector a, Vector b) {
const Camera &camera = canvas->GetCamera();
a = camera.AlignToPixelGrid(a);
b = camera.AlignToPixelGrid(b);
canvas->DrawLine(a, b, hcs);
}
void Constraint::DoStippledLine(Canvas *canvas, Canvas::hStroke hcs, Vector a, Vector b) {
Canvas::Stroke strokeStippled = *canvas->strokes.FindById(hcs);
strokeStippled.stipplePattern = StipplePattern::SHORT_DASH;
strokeStippled.stippleScale = 4.0;
Canvas::hStroke hcsStippled = canvas->GetStroke(strokeStippled);
DoLine(canvas, hcsStippled, a, b);
}
void Constraint::DoLabel(Canvas *canvas, Canvas::hStroke hcs,
Vector ref, Vector *labelPos, Vector gr, Vector gu) {
const Camera &camera = canvas->GetCamera();
std::string s = Label();
double textHeight = Style::TextHeight(GetStyle()) / camera.scale;
double swidth = VectorFont::Builtin()->GetWidth(textHeight, s),
sheight = VectorFont::Builtin()->GetCapHeight(textHeight);
// By default, the reference is from the center; but the style could
// specify otherwise if one is present, and it could also specify a
// rotation.
if(type == Type::COMMENT && disp.style.v) {
Style *st = Style::Get(disp.style);
// rotation first
double rads = st->textAngle*PI/180;
double c = cos(rads), s = sin(rads);
Vector pr = gr, pu = gu;
gr = pr.ScaledBy( c).Plus(pu.ScaledBy(s));
gu = pr.ScaledBy(-s).Plus(pu.ScaledBy(c));
// then origin
uint32_t o = (uint32_t)st->textOrigin;
if(o & (uint32_t)Style::TextOrigin::LEFT) ref = ref.Plus(gr.WithMagnitude(swidth/2));
if(o & (uint32_t)Style::TextOrigin::RIGHT) ref = ref.Minus(gr.WithMagnitude(swidth/2));
if(o & (uint32_t)Style::TextOrigin::BOT) ref = ref.Plus(gu.WithMagnitude(sheight/2));
if(o & (uint32_t)Style::TextOrigin::TOP) ref = ref.Minus(gu.WithMagnitude(sheight/2));
}
Vector o = ref.Minus(gr.WithMagnitude(swidth/2)).Minus(
gu.WithMagnitude(sheight/2));
canvas->DrawVectorText(s, textHeight, o, gr.WithMagnitude(1), gu.WithMagnitude(1), hcs);
if(labelPos) *labelPos = o;
}
void Constraint::DoProjectedPoint(Canvas *canvas, Canvas::hStroke hcs,
Vector *r, Vector n, Vector o) {
double d = r->DistanceToPlane(n, o);
Vector p = r->Minus(n.ScaledBy(d));
DoStippledLine(canvas, hcs, p, *r);
*r = p;
}
void Constraint::DoProjectedPoint(Canvas *canvas, Canvas::hStroke hcs, Vector *r) {
Vector p = r->ProjectInto(workplane);
DoStippledLine(canvas, hcs, p, *r);
*r = p;
}
//-----------------------------------------------------------------------------
// There is a rectangular box, aligned to our display axes (projRight, projUp)
// centered at ref. This is where a dimension label will be drawn. We want to
// draw a line from A to B. If that line would intersect the label box, then
// trim the line to leave a gap for it, and return zero. If not, then extend
// the line to almost meet the box, and return either positive or negative,
// depending whether that extension was from A or from B.
//-----------------------------------------------------------------------------
int Constraint::DoLineTrimmedAgainstBox(Canvas *canvas, Canvas::hStroke hcs,
Vector ref, Vector a, Vector b, bool extend) {
const Camera &camera = canvas->GetCamera();
double th = Style::TextHeight(GetStyle()) / camera.scale;
double pixels = 1.0 / camera.scale;
double swidth = VectorFont::Builtin()->GetWidth(th, Label()) + 8 * pixels,
sheight = VectorFont::Builtin()->GetCapHeight(th) + 8 * pixels;
Vector gu = camera.projUp.WithMagnitude(1),
gr = camera.projRight.WithMagnitude(1);
return DoLineTrimmedAgainstBox(canvas, hcs, ref, a, b, extend, gr, gu, swidth, sheight);
}
int Constraint::DoLineTrimmedAgainstBox(Canvas *canvas, Canvas::hStroke hcs,
Vector ref, Vector a, Vector b, bool extend,
Vector gr, Vector gu, double swidth, double sheight) {
struct {
Vector n;
double d;
} planes[4];
// reference pos is the center of box occupied by text; build a rectangle
// around that, aligned to axes gr and gu, from four planes will all four
// normals pointing inward
planes[0].n = gu.ScaledBy(-1); planes[0].d = -(gu.Dot(ref) + sheight/2);
planes[1].n = gu; planes[1].d = gu.Dot(ref) - sheight/2;
planes[2].n = gr; planes[2].d = gr.Dot(ref) - swidth/2;
planes[3].n = gr.ScaledBy(-1); planes[3].d = -(gr.Dot(ref) + swidth/2);
double tmin = VERY_POSITIVE, tmax = VERY_NEGATIVE;
Vector dl = b.Minus(a);
for(int i = 0; i < 4; i++) {
bool parallel;
Vector p = Vector::AtIntersectionOfPlaneAndLine(
planes[i].n, planes[i].d,
a, b, ¶llel);
if(parallel) continue;
int j;
for(j = 0; j < 4; j++) {
double d = (planes[j].n).Dot(p) - planes[j].d;
if(d < -LENGTH_EPS) break;
}
if(j < 4) continue;
double t = (p.Minus(a)).DivProjected(dl);
tmin = min(t, tmin);
tmax = max(t, tmax);
}
// Both in range; so there's pieces of the line on both sides of the label box.
if(tmin >= 0.0 && tmin <= 1.0 && tmax >= 0.0 && tmax <= 1.0) {
DoLine(canvas, hcs, a, a.Plus(dl.ScaledBy(tmin)));
DoLine(canvas, hcs, a.Plus(dl.ScaledBy(tmax)), b);
return 0;
}
// Only one intersection in range; so the box is right on top of the endpoint
if(tmin >= 0.0 && tmin <= 1.0) {
DoLine(canvas, hcs, a, a.Plus(dl.ScaledBy(tmin)));
return 0;
}
// Likewise.
if(tmax >= 0.0 && tmax <= 1.0) {
DoLine(canvas, hcs, a.Plus(dl.ScaledBy(tmax)), b);
return 0;
}
// The line does not intersect the label; so the line should get
// extended to just barely meet the label.
// 0 means the label lies within the line, negative means it's outside
// and closer to b, positive means outside and closer to a.
if(tmax < 0.0) {
if(extend) a = a.Plus(dl.ScaledBy(tmax));
DoLine(canvas, hcs, a, b);
return 1;
}
if(tmin > 1.0) {
if(extend) b = a.Plus(dl.ScaledBy(tmin));
DoLine(canvas, hcs, a, b);
return -1;
}
// This will happen if the entire line lies within the box.
return 0;
}
void Constraint::DoArrow(Canvas *canvas, Canvas::hStroke hcs,
Vector p, Vector dir, Vector n, double width, double angle, double da) {
dir = dir.WithMagnitude(width / cos(angle));
dir = dir.RotatedAbout(n, da);
DoLine(canvas, hcs, p, p.Plus(dir.RotatedAbout(n, angle)));
DoLine(canvas, hcs, p, p.Plus(dir.RotatedAbout(n, -angle)));
}
//-----------------------------------------------------------------------------
// Draw a line with arrows on both ends, and possibly a gap in the middle for
// the dimension. We will use these for most length dimensions. The length
// being dimensioned is from A to B; but those points get extended perpendicular
// to the line AB, until the line between the extensions crosses ref (the
// center of the label).
//-----------------------------------------------------------------------------
void Constraint::DoLineWithArrows(Canvas *canvas, Canvas::hStroke hcs,
Vector ref, Vector a, Vector b,
bool onlyOneExt)
{
const Camera &camera = canvas->GetCamera();
double pixels = 1.0 / camera.scale;
Vector ab = a.Minus(b);
Vector ar = a.Minus(ref);
// Normal to a plane containing the line and the label origin.
Vector n = ab.Cross(ar);
// Within that plane, and normal to the line AB; so that's our extension
// line.
Vector out = ab.Cross(n).WithMagnitude(1);
out = out.ScaledBy(-out.Dot(ar));
Vector ae = a.Plus(out), be = b.Plus(out);
// Extension lines extend 10 pixels beyond where the arrows get
// drawn (which is at the same offset perpendicular from AB as the
// label).
DoLine(canvas, hcs, a, ae.Plus(out.WithMagnitude(10*pixels)));
if(!onlyOneExt) {
DoLine(canvas, hcs, b, be.Plus(out.WithMagnitude(10*pixels)));
}
int within = DoLineTrimmedAgainstBox(canvas, hcs, ref, ae, be);
// Arrow heads are 13 pixels long, with an 18 degree half-angle.
double theta = 18*PI/180;
Vector arrow = (be.Minus(ae)).WithMagnitude(13*pixels);
if(within != 0) {
arrow = arrow.ScaledBy(-1);
Vector seg = (be.Minus(ae)).WithMagnitude(18*pixels);
if(within < 0) DoLine(canvas, hcs, ae, ae.Minus(seg));
if(within > 0) DoLine(canvas, hcs, be, be.Plus(seg));
}
DoArrow(canvas, hcs, ae, arrow, n, 13.0 * pixels, theta, 0.0);
DoArrow(canvas, hcs, be, arrow.Negated(), n, 13.0 * pixels, theta, 0.0);
}
void Constraint::DoEqualLenTicks(Canvas *canvas, Canvas::hStroke hcs,
Vector a, Vector b, Vector gn, Vector *refp) {
const Camera &camera = canvas->GetCamera();
Vector m = (a.ScaledBy(1.0/3)).Plus(b.ScaledBy(2.0/3));
if(refp) *refp = m;
Vector ab = a.Minus(b);
Vector n = (gn.Cross(ab)).WithMagnitude(10/camera.scale);
DoLine(canvas, hcs, m.Minus(n), m.Plus(n));
}
void Constraint::DoEqualRadiusTicks(Canvas *canvas, Canvas::hStroke hcs,
hEntity he, Vector *refp) {
const Camera &camera = canvas->GetCamera();
Entity *circ = SK.GetEntity(he);
Vector center = SK.GetEntity(circ->point[0])->PointGetDrawNum();
double r = circ->CircleGetRadiusNum();
Quaternion q = circ->Normal()->NormalGetNum();
Vector u = q.RotationU(), v = q.RotationV();
double theta;
if(circ->type == Entity::Type::CIRCLE) {
theta = PI/2;
} else if(circ->type == Entity::Type::ARC_OF_CIRCLE) {
double thetaa, thetab, dtheta;
circ->ArcGetAngles(&thetaa, &thetab, &dtheta);
theta = thetaa + dtheta/2;
} else ssassert(false, "Unexpected entity type");
Vector d = u.ScaledBy(cos(theta)).Plus(v.ScaledBy(sin(theta)));
d = d.ScaledBy(r);
Vector p = center.Plus(d);
if(refp) *refp = p;
Vector tick = d.WithMagnitude(10/camera.scale);
DoLine(canvas, hcs, p.Plus(tick), p.Minus(tick));
}
void Constraint::DoArcForAngle(Canvas *canvas, Canvas::hStroke hcs,
Vector a0, Vector da, Vector b0, Vector db,
Vector offset, Vector *ref, bool trim,
Vector explodeOffset)
{
const Camera &camera = canvas->GetCamera();
double pixels = 1.0 / camera.scale;
Vector gr = camera.projRight.ScaledBy(1.0);
Vector gu = camera.projUp.ScaledBy(1.0);
if(workplane != Entity::FREE_IN_3D) {
a0 = a0.ProjectInto(workplane);
b0 = b0.ProjectInto(workplane);
da = da.ProjectVectorInto(workplane);
db = db.ProjectVectorInto(workplane);
}
a0 = a0.Plus(explodeOffset);
b0 = b0.Plus(explodeOffset);
Vector a1 = a0.Plus(da);
Vector b1 = b0.Plus(db);
bool skew;
Vector pi = Vector::AtIntersectionOfLines(a0, a0.Plus(da),
b0, b0.Plus(db), &skew);
if(!skew) {
*ref = pi.Plus(offset);
// We draw in a coordinate system centered at the intersection point.
// One basis vector is da, and the other is normal to da and in
// the plane that contains our lines (so normal to its normal).
da = da.WithMagnitude(1);
db = db.WithMagnitude(1);
Vector norm = da.Cross(db);
Vector dna = norm.Cross(da).WithMagnitude(1.0);
Vector dnb = norm.Cross(db).WithMagnitude(1.0);
// da and db magnitudes are 1.0
double thetaf = acos(da.Dot(db));
// Calculate median
Vector m = da.ScaledBy(cos(thetaf/2)).Plus(
dna.ScaledBy(sin(thetaf/2)));
Vector rm = (*ref).Minus(pi);
// Test which side we have to place an arc
if(m.Dot(rm) < 0) {
da = da.ScaledBy(-1); dna = dna.ScaledBy(-1);
db = db.ScaledBy(-1); dnb = dnb.ScaledBy(-1);
}
double rda = rm.Dot(da), rdna = rm.Dot(dna);
// Introduce minimal arc radius in pixels
double r = max(sqrt(rda*rda + rdna*rdna), 15.0 * pixels);
double th = Style::TextHeight(GetStyle()) / camera.scale;
double swidth = VectorFont::Builtin()->GetWidth(th, Label()) + 8*pixels,
sheight = VectorFont::Builtin()->GetCapHeight(th) + 6*pixels;
double textR = sqrt(swidth * swidth + sheight * sheight) / 2.0;
*ref = pi.Plus(rm.WithMagnitude(std::max(rm.Magnitude(), 15 * pixels + textR)));
// Arrow points
Vector apa = da. ScaledBy(r).Plus(pi);
Vector apb = da. ScaledBy(r*cos(thetaf)).Plus(
dna.ScaledBy(r*sin(thetaf))).Plus(pi);
double arrowW = 13 * pixels;
double arrowA = 18.0 * PI / 180.0;
bool arrowVisible = apb.Minus(apa).Magnitude() > 2.5 * arrowW;
// Arrow reversing indicator
bool arrowRev = false;
// The minimal extension length in angular representation
double extAngle = 18 * pixels / r;
// Arc additional angle
double addAngle = 0.0;
// Arc start angle
double startAngle = 0.0;
// Arc extension to db.
// We have just enlarge angle value.
if(HasLabel() && rm.Dot(dnb) > 0.0) {
// rm direction projected to plane with u = da, v = dna
Vector rmp = da.ScaledBy(rda).Plus(dna.ScaledBy(rdna)).WithMagnitude(1.0);
// rmp and db magnitudes are 1.0
addAngle = std::max(acos(rmp.Dot(db)), extAngle);
if(arrowVisible) {
startAngle = -extAngle;
addAngle += extAngle;
arrowRev = true;
}
}
// Arc extension to da.
// We are enlarge angle value and rewrite basis to align along rm projection.
if(HasLabel() && rm.Dot(dna) < 0.0) {
// rm direction projected to plane with u = da, v = dna
Vector rmp = da.ScaledBy(rda).Plus(dna.ScaledBy(rdna)).WithMagnitude(1.0);
// rmp and da magnitudes are 1.0
startAngle = -std::max(acos(rmp.Dot(da)), extAngle);
addAngle = -startAngle;
if(arrowVisible) {
addAngle += extAngle;
arrowRev = true;
}
}
Vector prev;
int n = 30;
for(int i = 0; i <= n; i++) {
double theta = startAngle + (i*(thetaf + addAngle))/n;
Vector p = da.ScaledBy(r*cos(theta)).Plus(
dna.ScaledBy(r*sin(theta))).Plus(pi);
if(i > 0) {
if(trim) {
DoLineTrimmedAgainstBox(canvas, hcs, *ref, prev, p,
/*extend=*/false, gr, gu, swidth, sheight + 2*pixels);
} else {
DoLine(canvas, hcs, prev, p);
}
}
prev = p;
}
DoLineExtend(canvas, hcs, a0, a1, apa, 5.0 * pixels);
DoLineExtend(canvas, hcs, b0, b1, apb, 5.0 * pixels);
// Draw arrows only when we have enough space.
if(arrowVisible) {
double angleCorr = arrowW / (2.0 * r);
if(arrowRev) {
dna = dna.ScaledBy(-1.0);
angleCorr = -angleCorr;
}
DoArrow(canvas, hcs, apa, dna, norm, arrowW, arrowA, angleCorr);
DoArrow(canvas, hcs, apb, dna, norm, arrowW, arrowA, thetaf + PI - angleCorr);
}
} else {
// The lines are skew; no wonderful way to illustrate that.
*ref = a0.Plus(b0);
*ref = (*ref).ScaledBy(0.5).Plus(disp.offset);
gu = gu.WithMagnitude(1);
double textHeight = Style::TextHeight(GetStyle()) / camera.scale;
Vector trans =
(*ref).Plus(gu.ScaledBy(-1.5*VectorFont::Builtin()->GetCapHeight(textHeight)));
canvas->DrawVectorText("angle between skew lines", textHeight,
trans, gr.WithMagnitude(1), gu.WithMagnitude(1),
hcs);
}
}
bool Constraint::IsVisible() const {
if(!SS.GW.showConstraints) return false;
Group *g = SK.GetGroup(group);
// If the group is hidden, then the constraints are hidden and not
// able to be selected.
if(!(g->visible)) return false;
// And likewise if the group is not the active group; except for comments
// with an assigned style.
if(g->h != SS.GW.activeGroup && !(type == Type::COMMENT && disp.style.v)) {
return false;
}
if(disp.style.v) {
Style *s = Style::Get(disp.style);
if(!s->visible) return false;
}
return true;
}
bool Constraint::DoLineExtend(Canvas *canvas, Canvas::hStroke hcs,
Vector p0, Vector p1, Vector pt, double salient) {
Vector dir = p1.Minus(p0);
double k = dir.Dot(pt.Minus(p0)) / dir.Dot(dir);
Vector ptOnLine = p0.Plus(dir.ScaledBy(k));
// Draw projection line.
DoLine(canvas, hcs, pt, ptOnLine);
// Calculate salient direction.
Vector sd = dir.WithMagnitude(1.0).ScaledBy(salient);
Vector from;
Vector to;
if(k < 0.0) {
from = p0;
to = ptOnLine.Minus(sd);
} else if(k > 1.0) {
from = p1;
to = ptOnLine.Plus(sd);
} else {
return false;
}
// Draw extension line.
DoLine(canvas, hcs, from, to);
return true;
}
void Constraint::DoLayout(DrawAs how, Canvas *canvas,
Vector *labelPos, std::vector<Vector> *refs) {
if(!(how == DrawAs::HOVERED || how == DrawAs::SELECTED) &&
!IsVisible()) return;
// Unit vectors that describe our current view of the scene. One pixel
// long, not one actual unit.
const Camera &camera = canvas->GetCamera();
Vector gr = camera.projRight.ScaledBy(1/camera.scale);
Vector gu = camera.projUp.ScaledBy(1/camera.scale);
Vector gn = (gr.Cross(gu)).WithMagnitude(1/camera.scale);
double textHeight = Style::TextHeight(GetStyle()) / camera.scale;
RgbaColor color = {};
switch(how) {
case DrawAs::DEFAULT: color = Style::Color(GetStyle()); break;
case DrawAs::HOVERED: color = Style::Color(Style::HOVERED); break;
case DrawAs::SELECTED: color = Style::Color(Style::SELECTED); break;
}
Canvas::Stroke stroke = Style::Stroke(GetStyle());
stroke.layer = Canvas::Layer::FRONT;
stroke.color = color;
stroke.zIndex = 4;
Canvas::hStroke hcs = canvas->GetStroke(stroke);
Canvas::Fill fill = {};
fill.layer = Canvas::Layer::FRONT;
fill.color = color;
fill.zIndex = stroke.zIndex;
Canvas::hFill hcf = canvas->GetFill(fill);
switch(type) {
case Type::PT_PT_DISTANCE: {
Vector ap = SK.GetEntity(ptA)->PointGetNum();
Vector bp = SK.GetEntity(ptB)->PointGetNum();
if(workplane != Entity::FREE_IN_3D) {
DoProjectedPoint(canvas, hcs, &ap);
DoProjectedPoint(canvas, hcs, &bp);
}
if(ShouldDrawExploded()) {
// Offset A and B by the same offset so the constraint is drawn
// in the plane of one of the exploded points (rather than at an
// angle)
Vector offset = SK.GetEntity(ptA)->ExplodeOffset();
ap = ap.Plus(offset);
bp = bp.Plus(offset);
}
Vector ref = ((ap.Plus(bp)).ScaledBy(0.5)).Plus(disp.offset);
if(refs) refs->push_back(ref);
DoLineWithArrows(canvas, hcs, ref, ap, bp, /*onlyOneExt=*/false);
DoLabel(canvas, hcs, ref, labelPos, gr, gu);
return;
}
case Type::PROJ_PT_DISTANCE: {
Vector ap = SK.GetEntity(ptA)->PointGetNum(),
bp = SK.GetEntity(ptB)->PointGetNum(),
dp = (bp.Minus(ap)),
pp = SK.GetEntity(entityA)->VectorGetNum();
if(ShouldDrawExploded()) {
// explode for whichever point is in the workplane (or the first if both are)
Entity *pt = SK.GetEntity(ptA);
if(pt->group != group) {
pt = SK.GetEntity(ptB);
}
if(pt->group == group) {
Vector offset = pt->ExplodeOffset();
ap = ap.Plus(offset);
bp = bp.Plus(offset);
}
}
Vector ref = ((ap.Plus(bp)).ScaledBy(0.5)).Plus(disp.offset);
if(refs) refs->push_back(ref);
pp = pp.WithMagnitude(1);
double d = dp.Dot(pp);
Vector bpp = ap.Plus(pp.ScaledBy(d));
DoStippledLine(canvas, hcs, ap, bpp);
DoStippledLine(canvas, hcs, bp, bpp);
DoLineWithArrows(canvas, hcs, ref, ap, bpp, /*onlyOneExt=*/false);
DoLabel(canvas, hcs, ref, labelPos, gr, gu);
return;
}
case Type::PT_FACE_DISTANCE:
case Type::PT_PLANE_DISTANCE: {
Vector pt = SK.GetEntity(ptA)->PointGetDrawNum();
Entity *enta = SK.GetEntity(entityA);
Vector n, p;
if(type == Type::PT_PLANE_DISTANCE) {
n = enta->Normal()->NormalN();
p = enta->WorkplaneGetOffset();
} else {
n = enta->FaceGetNormalNum();
p = enta->FaceGetPointNum();
}
double d = (p.Minus(pt)).Dot(n);
Vector closest = pt.Plus(n.WithMagnitude(d));
Vector ref = ((closest.Plus(pt)).ScaledBy(0.5)).Plus(disp.offset);
if(refs) refs->push_back(ref);
if(!pt.Equals(closest)) {
DoLineWithArrows(canvas, hcs, ref, pt, closest, /*onlyOneExt=*/true);
}
DoLabel(canvas, hcs, ref, labelPos, gr, gu);
return;
}
case Type::PT_LINE_DISTANCE: {
Entity *ptEntity = SK.GetEntity(ptA);
Vector pt = ptEntity->PointGetNum();
Entity *line = SK.GetEntity(entityA);
Vector lA = SK.GetEntity(line->point[0])->PointGetNum();
Vector lB = SK.GetEntity(line->point[1])->PointGetNum();
Vector dl = lB.Minus(lA);
if(workplane != Entity::FREE_IN_3D) {
lA = lA.ProjectInto(workplane);
lB = lB.ProjectInto(workplane);
DoProjectedPoint(canvas, hcs, &pt);
}
// Only explode if the point and line are in the same group (and that group is a sketch
// with explode enabled) otherwise it's too visually confusing to figure out what the
// correct projections should be.
bool shouldExplode = ShouldDrawExploded()
&& ptEntity->group == group
&& line->group == group;
if(shouldExplode) {
Vector explodeOffset = ptEntity->ExplodeOffset();
pt = pt.Plus(explodeOffset);
lA = lA.Plus(explodeOffset);
lB = lB.Plus(explodeOffset);
}
// Find the closest point on the line
Vector closest = pt.ClosestPointOnLine(lA, dl);
Vector ref = ((closest.Plus(pt)).ScaledBy(0.5)).Plus(disp.offset);
if(refs) refs->push_back(ref);
DoLabel(canvas, hcs, ref, labelPos, gr, gu);
if(!pt.Equals(closest)) {
DoLineWithArrows(canvas, hcs, ref, pt, closest, /*onlyOneExt=*/true);
// Draw projected point
Vector a = pt;
Vector b = closest;
Vector ab = a.Minus(b);
Vector ar = a.Minus(ref);
Vector n = ab.Cross(ar);
Vector out = ab.Cross(n).WithMagnitude(1);
out = out.ScaledBy(-out.Dot(ar));
Vector be = b.Plus(out);
Vector np = lA.Minus(pt).Cross(lB.Minus(pt)).WithMagnitude(1.0);
DoProjectedPoint(canvas, hcs, &be, np, pt);
// Extensions to line
double pixels = 1.0 / camera.scale;
Vector refClosest = ref.ClosestPointOnLine(lA, dl);
double ddl = dl.Dot(dl);
if(fabs(ddl) > LENGTH_EPS * LENGTH_EPS) {
double t = refClosest.Minus(lA).Dot(dl) / ddl;
if(t < 0.0) {
DoLine(canvas, hcs, refClosest.Minus(dl.WithMagnitude(10.0 * pixels)), lA);
} else if(t > 1.0) {
DoLine(canvas, hcs, refClosest.Plus(dl.WithMagnitude(10.0 * pixels)), lB);
}
}
}
if(workplane != Entity::FREE_IN_3D) {
// Draw the projection marker from the closest point on the
// projected line to the projected point on the real line.
Vector lAB = (lA.Minus(lB));
double t = (lA.Minus(closest)).DivProjected(lAB);
Vector lA = SK.GetEntity(line->point[0])->PointGetNum();
Vector lB = SK.GetEntity(line->point[1])->PointGetNum();
Vector c2 = (lA.ScaledBy(1-t)).Plus(lB.ScaledBy(t));
DoProjectedPoint(canvas, hcs, &c2);
}
return;
}
case Type::DIAMETER: {
Entity *circle = SK.GetEntity(entityA);
Vector center = SK.GetEntity(circle->point[0])->PointGetDrawNum();
Quaternion q = SK.GetEntity(circle->normal)->NormalGetNum();
Vector n = q.RotationN().WithMagnitude(1);
double r = circle->CircleGetRadiusNum();
Vector ref = center.Plus(disp.offset);
// Force the label into the same plane as the circle.
ref = ref.Minus(n.ScaledBy(n.Dot(ref) - n.Dot(center)));
if(refs) refs->push_back(ref);
Vector mark = ref.Minus(center);
mark = mark.WithMagnitude(mark.Magnitude()-r);
DoLineTrimmedAgainstBox(canvas, hcs, ref, ref, ref.Minus(mark));
Vector topLeft;
DoLabel(canvas, hcs, ref, &topLeft, gr, gu);
if(labelPos) *labelPos = topLeft;
return;
}
case Type::POINTS_COINCIDENT: {
if(how == DrawAs::DEFAULT) {
// Let's adjust the color of this constraint to have the same
// rough luma as the point color, so that the constraint does not
// stand out in an ugly way.
RgbaColor cd = Style::Color(Style::DATUM),
cc = Style::Color(Style::CONSTRAINT);
// convert from 8-bit color to a vector
Vector vd = Vector::From(cd.redF(), cd.greenF(), cd.blueF()),
vc = Vector::From(cc.redF(), cc.greenF(), cc.blueF());
// and scale the constraint color to have the same magnitude as
// the datum color, maybe a bit dimmer
vc = vc.WithMagnitude(vd.Magnitude()*0.9);
// and set the color to that.
fill.color = RGBf(vc.x, vc.y, vc.z);
hcf = canvas->GetFill(fill);
}
for(int a = 0; a < 2; a++) {
Vector r = camera.projRight.ScaledBy((a+1)/camera.scale);
Vector d = camera.projUp.ScaledBy((2-a)/camera.scale);
for(int i = 0; i < 2; i++) {
Vector p = SK.GetEntity(i == 0 ? ptA : ptB)->PointGetDrawNum();
if(refs) refs->push_back(p);
canvas->DrawQuad(p.Plus (r).Plus (d),
p.Plus (r).Minus(d),
p.Minus(r).Minus(d),
p.Minus(r).Plus (d),
hcf);
}
}
return;
}
case Type::PT_ON_CIRCLE:
case Type::PT_ON_LINE:
case Type::PT_ON_FACE:
case Type::PT_IN_PLANE: {
double s = 8/camera.scale;
Vector p = SK.GetEntity(ptA)->PointGetDrawNum();
if(refs) refs->push_back(p);
Vector r, d;
if(type == Type::PT_ON_FACE) {
Vector n = SK.GetEntity(entityA)->FaceGetNormalNum();
r = n.Normal(0);
d = n.Normal(1);
} else if(type == Type::PT_IN_PLANE) {
EntityBase *n = SK.GetEntity(entityA)->Normal();
r = n->NormalU();
d = n->NormalV();
} else {
r = gr;
d = gu;
s *= (6.0/8); // draw these a little smaller
}
r = r.WithMagnitude(s); d = d.WithMagnitude(s);
DoLine(canvas, hcs, p.Plus (r).Plus (d), p.Plus (r).Minus(d));
DoLine(canvas, hcs, p.Plus (r).Minus(d), p.Minus(r).Minus(d));
DoLine(canvas, hcs, p.Minus(r).Minus(d), p.Minus(r).Plus (d));
DoLine(canvas, hcs, p.Minus(r).Plus (d), p.Plus (r).Plus (d));
return;
}
case Type::WHERE_DRAGGED: {
Vector p = SK.GetEntity(ptA)->PointGetDrawNum();
if(refs) refs->push_back(p);
Vector u = p.Plus(gu.WithMagnitude(8/camera.scale)).Plus(
gr.WithMagnitude(8/camera.scale)),
uu = u.Minus(gu.WithMagnitude(5/camera.scale)),
ur = u.Minus(gr.WithMagnitude(5/camera.scale));
// Draw four little crop marks, uniformly spaced (by ninety
// degree rotations) around the point.
int i;
for(i = 0; i < 4; i++) {
DoLine(canvas, hcs, u, uu);
DoLine(canvas, hcs, u, ur);
u = u.RotatedAbout(p, gn, PI/2);
ur = ur.RotatedAbout(p, gn, PI/2);
uu = uu.RotatedAbout(p, gn, PI/2);
}
return;
}
case Type::SAME_ORIENTATION: {
for(int i = 0; i < 2; i++) {
Entity *e = SK.GetEntity(i == 0 ? entityA : entityB);
Quaternion q = e->NormalGetNum();
Vector n = q.RotationN().WithMagnitude(25/camera.scale);
Vector u = q.RotationU().WithMagnitude(6/camera.scale);
Vector p = SK.GetEntity(e->point[0])->PointGetNum();
p = p.Plus(n.WithMagnitude(10/camera.scale));
if(refs) refs->push_back(p);
DoLine(canvas, hcs, p.Plus(u), p.Minus(u).Plus(n));
DoLine(canvas, hcs, p.Minus(u), p.Plus(u).Plus(n));
}
return;
}
case Type::EQUAL_ANGLE: {
Vector ref;
Entity *a = SK.GetEntity(entityA);
Entity *b = SK.GetEntity(entityB);
Entity *c = SK.GetEntity(entityC);
Entity *d = SK.GetEntity(entityD);
Vector a0 = a->VectorGetStartPoint();
Vector b0 = b->VectorGetStartPoint();
Vector c0 = c->VectorGetStartPoint();
Vector d0 = d->VectorGetStartPoint();
Vector da = a->VectorGetNum();
Vector db = b->VectorGetNum();
Vector dc = c->VectorGetNum();
Vector dd = d->VectorGetNum();
if(other) {
a0 = a0.Plus(da);
da = da.ScaledBy(-1);
}
DoArcForAngle(canvas, hcs, a0, da, b0, db,
da.WithMagnitude(40/camera.scale), &ref, /*trim=*/false, a->ExplodeOffset());
if(refs) refs->push_back(ref);
DoArcForAngle(canvas, hcs, c0, dc, d0, dd,
dc.WithMagnitude(40/camera.scale), &ref, /*trim=*/false, c->ExplodeOffset());
if(refs) refs->push_back(ref);
return;
}
case Type::ANGLE: {
Entity *a = SK.GetEntity(entityA);
Entity *b = SK.GetEntity(entityB);
Vector a0 = a->VectorGetStartPoint();
Vector b0 = b->VectorGetStartPoint();
Vector da = a->VectorGetNum();
Vector db = b->VectorGetNum();
if(other) {
a0 = a0.Plus(da);
da = da.ScaledBy(-1);
}
Vector ref;
DoArcForAngle(canvas, hcs, a0, da, b0, db, disp.offset, &ref, /*trim=*/true, a->ExplodeOffset());
DoLabel(canvas, hcs, ref, labelPos, gr, gu);
if(refs) refs->push_back(ref);
return;
}
case Type::PERPENDICULAR: {
Vector u = Vector::From(0, 0, 0), v = Vector::From(0, 0, 0);
Vector rn, ru;
if(workplane == Entity::FREE_IN_3D) {
rn = gn;
ru = gu;
} else {
EntityBase *normal = SK.GetEntity(workplane)->Normal();
rn = normal->NormalN();
ru = normal->NormalV(); // ru meaning r_up, not u/v
}
for(int i = 0; i < 2; i++) {
Entity *e = SK.GetEntity(i == 0 ? entityA : entityB);
if(i == 0) {
// Calculate orientation of perpendicular sign only
// once, so that it's the same both times it's drawn
u = e->VectorGetNum();
u = u.WithMagnitude(16/camera.scale);
v = (rn.Cross(u)).WithMagnitude(16/camera.scale);
// a bit of bias to stop it from flickering between the
// two possibilities
if(fabs(u.Dot(ru)) < fabs(v.Dot(ru)) + LENGTH_EPS) {
swap(u, v);
}
if(u.Dot(ru) < 0) u = u.ScaledBy(-1);
}
Vector p = e->VectorGetRefPoint().Plus(e->ExplodeOffset());
Vector s = p.Plus(u).Plus(v);
DoLine(canvas, hcs, s, s.Plus(v));
Vector m = s.Plus(v.ScaledBy(0.5));
DoLine(canvas, hcs, m, m.Plus(u));
if(refs) refs->push_back(m);
}
return;
}
case Type::CURVE_CURVE_TANGENT:
case Type::CUBIC_LINE_TANGENT:
case Type::ARC_LINE_TANGENT: {
Vector textAt, u, v;
if(type == Type::ARC_LINE_TANGENT) {
Entity *arc = SK.GetEntity(entityA);
Entity *norm = SK.GetEntity(arc->normal);
Vector c = SK.GetEntity(arc->point[0])->PointGetDrawNum();
Vector p =
SK.GetEntity(arc->point[other ? 2 : 1])->PointGetDrawNum();
Vector r = p.Minus(c);
textAt = p.Plus(r.WithMagnitude(14/camera.scale));
u = norm->NormalU();
v = norm->NormalV();
} else if(type == Type::CUBIC_LINE_TANGENT) {
Vector n;
if(workplane == Entity::FREE_IN_3D) {
u = gr;
v = gu;
n = gn;
} else {
EntityBase *wn = SK.GetEntity(workplane)->Normal();
u = wn->NormalU();
v = wn->NormalV();
n = wn->NormalN();
}
Entity *cubic = SK.GetEntity(entityA);
Vector p = other ? cubic->CubicGetFinishNum() :
cubic->CubicGetStartNum();
p = p.Plus(cubic->ExplodeOffset());
Vector dir = SK.GetEntity(entityB)->VectorGetNum();
Vector out = n.Cross(dir);
textAt = p.Plus(out.WithMagnitude(14/camera.scale));
} else {
Vector n, dir;
EntityBase *wn = SK.GetEntity(workplane)->Normal();
u = wn->NormalU();
v = wn->NormalV();
n = wn->NormalN();
Entity *eA = SK.GetEntity(entityA);
// Big pain; we have to get a vector tangent to the curve
// at the shared point, which could be from either a cubic
// or an arc.
if(other) {
textAt = eA->EndpointFinish().Plus(eA->ExplodeOffset());
if(eA->type == Entity::Type::CUBIC) {
dir = eA->CubicGetFinishTangentNum();
} else {
dir = SK.GetEntity(eA->point[0])->PointGetNum().Minus(
SK.GetEntity(eA->point[2])->PointGetNum());
dir = n.Cross(dir);
}
} else {
textAt = eA->EndpointStart().Plus(eA->ExplodeOffset());
if(eA->type == Entity::Type::CUBIC) {
dir = eA->CubicGetStartTangentNum();
} else {
dir = SK.GetEntity(eA->point[0])->PointGetNum().Minus(
SK.GetEntity(eA->point[1])->PointGetNum());
dir = n.Cross(dir);
}
}
dir = n.Cross(dir);
textAt = textAt.Plus(dir.WithMagnitude(14/camera.scale));
}
Vector ex = VectorFont::Builtin()->GetExtents(textHeight, "T");
canvas->DrawVectorText("T", textHeight, textAt.Minus(ex.ScaledBy(0.5)),
u.WithMagnitude(1), v.WithMagnitude(1), hcs);
if(refs) refs->push_back(textAt);
return;
}
case Type::PARALLEL: {
for(int i = 0; i < 2; i++) {
Entity *e = SK.GetEntity(i == 0 ? entityA : entityB);
Vector n = e->VectorGetNum();
n = n.WithMagnitude(25/camera.scale);
Vector u = (gn.Cross(n)).WithMagnitude(4/camera.scale);
Vector p = e->VectorGetRefPoint();
if(ShouldDrawExploded()) {
p = p.Plus(e->ExplodeOffset());
}
DoLine(canvas, hcs, p.Plus(u), p.Plus(u).Plus(n));
DoLine(canvas, hcs, p.Minus(u), p.Minus(u).Plus(n));
if(refs) refs->push_back(p.Plus(n.ScaledBy(0.5)));
}
return;
}