-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSketchSnaps.cpp
More file actions
611 lines (480 loc) · 18.9 KB
/
Copy pathSketchSnaps.cpp
File metadata and controls
611 lines (480 loc) · 18.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
/// Sketchnator
/// Copyright (C) 2021 Luiz Gustavo Pfitscher e Feldmann
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// This program 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 General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "SketchSnaps.h"
#include <glm/gtx/closest_point.hpp>
#include <glm/gtx/norm.hpp>
#include "SketchIntersection.h"
SketchSnaps::Coord SketchSnaps::ProximityThreshold = 10;
// Line snaps that *don't* take a partially defined line as input
// ==================================================================
bool SketchSnaps::ToLineEndpoint(const Sketch& sketch, LineIgnoreTestHandler ignore, const Vector2& mouse, SketchPointList::const_iterator& out_snap)
{
bool bFound = false;
Coord minDist = ProximityThreshold;
for (const SketchLine& l : sketch.lines)
{
if (ignore)
if (ignore(&l)) // make sure currently drawn line does not snap on itself
continue;
const Coord distA = l.first ->distance(mouse);
const Coord distB = l.second->distance(mouse);
if (distA < minDist)
{
out_snap = l.first;
minDist = distA;
bFound = true;
}
if (distB < minDist)
{
out_snap = l.second;
minDist = distB;
bFound = true;
}
}
return bFound;
}
bool SketchSnaps::ToLine(const Sketch& sketch, LineIgnoreTestHandler ignore, const Vector2& mouse, Vector2& out_snap)
{
SketchLineList::const_iterator out_line;
return ToLine(sketch, ignore, mouse, out_line, out_snap);
}
bool SketchSnaps::ToLine(const Sketch& sketch, LineIgnoreTestHandler ignore, const Vector2& mouse, SketchLineList::const_iterator& out_line, Vector2& out_snap)
{
bool bFound = false;
Coord minDist = ProximityThreshold;
for (auto lit = sketch.lines.cbegin(); lit != sketch.lines.cend(); ++lit)
{
const SketchLine& line = *lit;
if (ignore)
if (ignore(&line)) // make sure currently drawn line does not snap on itself
continue;
const Vector2& a = *line.first;
const Vector2& b = *line.second;
const Vector2& p = glm::closestPointOnLine(mouse, a, b);
Coord d;
if ((d = glm::distance(p, mouse)) < minDist)
{
bFound = true;
minDist = d;
out_snap = p;
out_line = lit;
}
}
return bFound;
}
bool SketchSnaps::ToLineMidpoint(const Sketch& sketch, LineIgnoreTestHandler ignore, const Vector2& mouse, Vector2& out_snap)
{
SketchLineList::const_iterator out_line;
return ToLineMidpoint(sketch, ignore, mouse, out_line, out_snap);
}
bool SketchSnaps::ToLineMidpoint(const Sketch& sketch, LineIgnoreTestHandler ignore, const Vector2& mouse, SketchLineList::const_iterator& out_line, Vector2& out_snap)
{
bool bFound = false;
Coord minDist = ProximityThreshold;
for (auto lit = sketch.lines.cbegin(); lit != sketch.lines.cend(); ++lit)
{
const SketchLine& l = *lit;
if (ignore)
if (ignore(&l)) // make sure currently drawn line does not snap on itself
continue;
const Vector2 midpoint((*l.first + *l.second)/Coord(2));
Coord dist;
if ((dist = glm::length( midpoint - mouse )) < minDist)
{
out_snap = midpoint;
out_line = lit;
minDist = dist;
bFound = true;
}
}
return bFound;
}
bool SketchSnaps::LineHorizontal(const SketchLine* line, const Vector2& mouse, Vector2& out_snap)
{
if (!line)
return false;
const Vector2& a = *line->first; // the fixed point of the line
out_snap = Vector2(mouse.x, a.y);
return (glm::distance(out_snap, mouse) < ProximityThreshold);
}
bool SketchSnaps::LineVertical(const SketchLine* line, const Vector2& mouse, Vector2& out_snap)
{
if (!line)
return false;
const Vector2& a = *line->first; // the fixed point of the line
out_snap = Vector2(a.x, mouse.y);
return (glm::distance(out_snap, mouse) < ProximityThreshold);
}
// Line snaps that *do* take a partially defined line as input
// ==================================================================
static bool SnapSketchLineOrthogonal_Internal(const SketchLine& line, const SketchSnaps::Vector2& start_point, const SketchSnaps::Vector2& mouse_point,SketchSnaps:: Coord& out_distance, SketchSnaps::Vector2& out_snap)
{
const SketchSnaps::Vector2& a = *line.first;
const SketchSnaps::Vector2& b = *line.second;
const SketchSnaps::Coord len2ab = glm::distance2(b, a); // avoid dividing by zero below
if (almost_zero(len2ab))
return false;
// the closest point on line is on another line orthogonal to it
out_snap = glm::closestPointOnLine(start_point, a, b);
// test if this point is *inside* the segment
const SketchSnaps::Coord t = glm::dot(out_snap - a, b - a)/len2ab;
if (t < 0 || t > 1)
return false;
// test if the mouse is close to it
if ((out_distance = glm::distance(mouse_point, out_snap)) > SketchSnaps::ProximityThreshold)
return false;
return true;
}
bool SketchSnaps::LineOrthogonal(const Sketch& sketch, const SketchLine* currLine, const Vector2& mouse, Vector2& out_snap)
{
SketchLineList::const_iterator out_line;
return LineOrthogonal(sketch, currLine, mouse, out_line, out_snap);
}
bool SketchSnaps::LineOrthogonal(const Sketch& sketch, const SketchLine* currLine, const Vector2& mouse, SketchLineList::const_iterator& out_line, Vector2& out_snap)
{
if (!currLine)
return false;
bool bFound = false;
Coord minDist = ProximityThreshold;
for (auto lit = sketch.lines.cbegin(); lit != sketch.lines.cend(); ++lit)
{
const SketchLine& l = *lit;
if (&l == currLine) // make sure currently drawn line does not snap on itself
continue;
// test this line snaps
Coord temp_distance;
Vector2 temp_snap;
if (!SnapSketchLineOrthogonal_Internal(l, *currLine->first, mouse, temp_distance, temp_snap))
continue;
// choose the closest snap
if (temp_distance < minDist)
{
minDist = temp_distance;
bFound = true;
out_snap = temp_snap;
out_line = lit;
}
}
return bFound;
}
bool SketchSnaps::LineTangentToCircle(const Sketch& sketch, const SketchLine* currLine, const Vector2& mouse, Vector2& out_snap)
{
SketchCircleList::const_iterator out_circle;
return LineTangentToCircle(sketch, currLine, mouse, out_circle, out_snap);
}
bool SketchSnaps::LineTangentToCircle(const Sketch& sketch, const SketchLine* currLine, const Vector2& mouse, SketchCircleList::const_iterator& out_circle, Vector2& out_snap)
{
if (!currLine)
return false;
const Vector2& a = *currLine->first;
bool bFound = false;
Coord minDist = ProximityThreshold;
for (auto cit = sketch.circles.cbegin(); cit != sketch.circles.cend(); ++cit)
{
const SketchCircle& cir = *cit;
const Vector2& c = *cir.center; // center of circle
const Vector2 ac = a - c; // vector points from center to line original point
const Coord r = cir.GetRadius();
const Coord h = glm::length(ac); // hipothenuse of right triangle
if (almost_zero(h)) // would cause division by zero below
continue;
const Coord cos_alpha = r/h;
const Coord sin_alpha = glm::sqrt( 1 - glm::pow(cos_alpha, 2) );
const Vector2 inter = glm::normalize(ac)*r; // segment AC scaled to radius (inter + center would be the point where AC crosses the perimeter)
const Vector2 p[2] = {
c + Vector2( inter.x*cos_alpha - inter.y*sin_alpha, // rotate
inter.x*sin_alpha + inter.y*cos_alpha),
c + Vector2( inter.x*cos_alpha + inter.y*sin_alpha,
-inter.x*sin_alpha + inter.y*cos_alpha)
};
// test if any of those points is close to mouse
for (unsigned char i = 0; i < 2; i++)
{
Coord d;
if ((d = glm::distance(p[i], mouse)) < minDist)
{
bFound = true;
minDist = d;
out_snap = p[i];
out_circle = cit;
}
}
}
return bFound;
}
// Circle snaps that *don't* take a partially defined circle as input
// ==================================================================
bool SketchSnaps::ToArcEndpoints(const Sketch& sketch, CircleIgnoreTestHandler ignore, const Vector2& mouse, Vector2& out_snap)
{
bool bFound = false;
Coord minDist = ProximityThreshold;
for (const SketchCircle& cir : sketch.circles)
{
if (ignore)
if (ignore(&cir))
continue;
const Vector2 canditates[2] = { *cir.radius, cir.GetArcEndpoint() };
for (const Vector2 p : canditates)
{
Coord d;
if ( (d = glm::distance(p, mouse)) > minDist )
continue;
minDist = d;
out_snap = p;
bFound = true;
}
}
return bFound;
}
bool SketchSnaps::ToArcCenter(const Sketch& sketch, CircleIgnoreTestHandler ignore, const Vector2& mouse, SketchPointList::const_iterator& out_snap)
{
bool bFound = false;
Coord minDist = ProximityThreshold;
for (const SketchCircle& c : sketch.circles)
{
if (ignore)
if (ignore(&c))
continue;
Coord d;
if ( (d = c.center->distance(mouse)) > minDist )
continue;
minDist = d;
out_snap = c.center;
bFound = true;
}
return bFound;
}
bool SketchSnaps::ToAnyCircleQuadrants(const Sketch& sketch, CircleIgnoreTestHandler ignore, const Vector2& mouse, Vector2& out_snap)
{
bool bFound = false;
Coord minDist = ProximityThreshold;
for (const SketchCircle& cir : sketch.circles)
{
if (ignore)
if (ignore(&cir))
continue;
Vector2 quadrants[4];
cir.GetQuadrants(quadrants);
for (const Vector2 p : quadrants)
{
Coord d;
if ( (d = glm::distance(p, mouse)) > minDist )
continue;
minDist = d;
out_snap = p;
bFound = true;
}
}
return bFound;
}
bool SketchSnaps::ToArc(const Sketch& sketch, CircleIgnoreTestHandler ignore, const Vector2& mouse, Vector2& out_snap)
{
SketchCircleList::const_iterator out_circle;
return ToArc(sketch, ignore, mouse, out_circle, out_snap);
}
bool SketchSnaps::ToArc(const Sketch& sketch, CircleIgnoreTestHandler ignore, const Vector2& mouse, SketchCircleList::const_iterator& out_circle, Vector2& out_snap)
{
bool bFound = false;
Coord minDist = ProximityThreshold;
for (auto cit = sketch.circles.cbegin(); cit != sketch.circles.cend(); ++cit)
{
const SketchCircle& cir = *cit;
if (ignore)
if (ignore(&cir))
continue;
const Vector2& c = *cir.center; // center of circle
const Vector2 p = c + cir.GetRadius() * glm::normalize(mouse - c); // point on the circumference whose radius line crosses the mouse
if (cir.GetPointAngle(p) > cir.GetAngleRad())
continue;
Coord d;
if ((d = glm::distance(p, mouse)) < minDist)
{
bFound = true;
minDist = d;
out_snap = p;
out_circle = cit;
}
}
return bFound;
}
// Circle snaps that *do* take a partially defined circle as input
// ==================================================================
bool SketchSnaps::ToCircleOwnQuadrants(const Sketch& sketch, const SketchCircle* cir, const Vector2& mouse, Vector2& out_snap)
{
if (!cir)
return false;
Vector2 quadrants[4];
cir->GetQuadrants(quadrants);
bool bFound = false;
Coord minDist = ProximityThreshold;
for (const Vector2 p : quadrants)
{
Coord d;
if ( (d = glm::distance(p, mouse)) > minDist )
continue;
minDist = d;
out_snap = p;
bFound = true;
}
return bFound;
}
bool SketchSnaps::ToArcOwnAxis(const Sketch& sketch, const SketchCircle* cir, const Vector2& mouse, Vector2& out_snap)
{
if (!cir)
return false;
const Vector2& center = *cir->center;
const Vector2 q[2] = { Vector2( mouse.x, center.y),
Vector2(center.x, mouse.y) };
bool bFound = false;
Coord minDist = ProximityThreshold;
for (const Vector2 p : q)
{
Coord d;
if ( (d = glm::distance(p, mouse)) > minDist )
continue;
minDist = d;
out_snap = p;
bFound = true;
}
return bFound;
}
bool SketchSnaps::ToCirclesIntersection(const Sketch& sketch, const SketchCircle* self, const Vector2& mouse, Vector2& out_snap)
{
if (!self)
return false;
bool bFound = false;
Coord minDist = ProximityThreshold;
const Vector2& c0 = *self->center;
const Coord r0 = self->GetRadius();
for (const SketchCircle& cir : sketch.circles)
{
if (&cir == self)
continue;
const Vector2& c1 = *cir.center;
const Coord r1 = cir.GetRadius();
Vector2 p[2];
if (!SketchIntersection::CircleCircle(c0, r0, c1, r1, p))
continue;
for (unsigned char i = 0; i < 2; i++)
{
if (cir.GetPointAngle(p[i]) > cir.GetAngleRad())
continue;
Coord d;
if ((d = glm::distance(p[i], mouse)) < minDist)
{
bFound = true;
minDist = d;
out_snap = p[i];
}
}
}
return bFound;
}
// BOUNDING BOX TEST
// ==========================================================================================================================================================================
bool SketchFeatureBoundingRegion::Hit(const SketchPoint& sketchpoint, const Vector2& point, Coord& out_distance, Vector2& out_closest)
{
out_closest = point;
return ((out_distance = sketchpoint.distance(point)) < SketchSnaps::ProximityThreshold);
}
bool SketchFeatureBoundingRegion::Hit(const SketchLine& line, const Vector2& point, Coord& out_distance, Vector2& out_closest)
{
const Vector2& a = *line.first;
const Vector2& b = *line.second;
const Coord length2ab = glm::distance2(b, a);
if (almost_zero(length2ab)) // avoid division by zero below
return false;
out_closest = glm::closestPointOnLine(point, a, b);
if ((out_distance = glm::distance(point, out_closest)) > SketchSnaps::ProximityThreshold)
return false;
const Coord t = glm::dot(out_closest - a, b - a)/length2ab;
if (t < 0 || t > 1)
return false;
return true;
}
bool SketchFeatureBoundingRegion::Hit(const SketchCircle& circle, const Vector2& point, Coord& out_distance, Vector2& out_closest)
{
const Vector2& center = *circle.center;
const Vector2 radialDirection = glm::normalize(point - center); // vector pointing from center to P
out_closest = center + circle.GetRadius() * radialDirection; // lengthen or shorten the vector to touch the circumference
// test distance is ok
if ((out_distance = glm::distance(out_closest, point)) > SketchSnaps::ProximityThreshold)
return false;
// test the angle is ok
return (circle.GetPointAngle(out_closest) <= circle.GetAngleRad() );
}
/// RECTANGLE TEST
/// ===========================================================================================================================================================================
RectangleDragTest SketchFeatureBoundingRectangle::Contains(const Vector2& p, const Vector2& r1, const Vector2& r2)
{
const Coord xmax = std::fmax(r1.x, r2.x);
const Coord xmin = std::fmin(r1.x, r2.x);
const Coord ymax = std::fmax(r1.y, r2.y);
const Coord ymin = std::fmin(r1.y, r2.y);
return ( ((p.x >= xmin) && (p.x <= xmax)) && ((p.y >= ymin) && (p.y <= ymax)) ? DRAG_FULLY_IN : DRAG_FULLY_OUT);
}
RectangleDragTest SketchFeatureBoundingRectangle::Contains(const SketchPoint& p, const Vector2& r1, const Vector2& r2)
{
return Contains((const Vector2&)p, r1, r2);
}
RectangleDragTest SketchFeatureBoundingRectangle::Contains(const SketchLine& l, const Vector2& r1, const Vector2& r2)
{
const Vector2& a = *l.first;
const Vector2& b = *l.second;
// fully in = both endpoints are in
if (Contains(a, r1, r2) && Contains(b, r1, r2))
return DRAG_FULLY_IN;
// partially in = rectangle's edge intersects this line
const Vector2 corners[4] = {r1, Vector2(r2.x, r1.y), r2, Vector2(r1.x, r2.y)};
for (unsigned char i = 0; i < 4; i++)
{
Vector2 p;
if ( SketchIntersection::LineLine(a, b, corners[i], corners[(i + 1) % 4 ], p) )
return DRAG_PARTIALLY_IN;
}
// else, fully out
return DRAG_FULLY_OUT;
}
RectangleDragTest SketchFeatureBoundingRectangle::Contains(const SketchCircle& c, const Vector2& r1, const Vector2& r2)
{
// ARC PARTLY IN: rectangle crosses it
const Coord radius = c.GetRadius();
const Vector2& center = *c.center;
const auto theta = c.GetAngleRad();
// test is based on origin centered circle, so we offset the line to make the test work
const Vector2 corners[4] = {r1 - center, Vector2(r2.x, r1.y) - center, r2 - center, Vector2(r1.x, r2.y) - center};
for (unsigned char i = 0; i < 4; i++)
{
unsigned char n;
Vector2 intersections[2]; // coordinates are in origin-centered circle space
if (SketchIntersection::LineCircle(radius, corners[i], corners[(i + 1) % 4 ], n, intersections))
{
// found intersection - now validate it (test is testing for circle, but we have an arc - test if intersection point happens inside the arc's endpoints)
for (unsigned char j = 0; j < n; j++) // test 1 or 2 intersections
{
const Vector2& point = intersections[j] + center;
// it must be less than the theta angle of the arc to be considered inside the arc
if (c.GetPointAngle(point) < theta)
return DRAG_PARTIALLY_IN;
}
}
}
// no intersection found: it's either fully in or fully out
// ARC FULLY IN: one point must be in
if (Contains(*c.radius, r1, r2))
return DRAG_FULLY_IN;
return DRAG_FULLY_OUT;
}