API Documentation
Loading...
Searching...
No Matches
Line.hpp
Go to the documentation of this file.
1/**--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Line
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/Vertex.h>
35
36/**--------------------------------------------------------------------------------------------------
37Namespace: Parke
38Namespace that wraps all Logic created by Tyler Parke
39 *-----------------------------------------------------------------------------------------------**/
40
41namespace NDEVR
42{
43
44 /**--------------------------------------------------------------------------------------------------
45 Class: LineSegment
46
47 \brief A line segment represented by two vertices, a start and end
48
49 Author: Tyler Parke
50
51 Date: 2017-11-19
52 *-----------------------------------------------------------------------------------------------**/
53 template<uint01 t_dims, class t_type, class t_vertex = Vertex<t_dims, t_type>>
54 class LineSegment : public Vector<2, t_vertex>
55 {
56 public:
57 constexpr LineSegment()
58 {}
59 constexpr LineSegment(const t_vertex& p1, const t_vertex& p2)
60 : Vector<2, t_vertex>(p1, p2)
61 {}
62 explicit constexpr LineSegment(const Vector<2, t_vertex>& line)
63 : Vector<2, t_vertex>(line)
64 {}
65
66 /**--------------------------------------------------------------------------------------------------
67 Fn: constexpr LineSegment<t_new_dims, t_new_type, t_new_vertex> LineSegment::as() const
68
69 Gets as.
70
71 Author: Tyler Parke
72
73 Date: 2017-11-19
74
75 Returns: A LineSegment&lt;t_new_dims,t_new_type,t_new_vertex&gt;
76 *-----------------------------------------------------------------------------------------------**/
77 template<uint01 t_new_dims, class t_new_type, class t_new_vertex = Vertex<t_new_dims, t_new_type>>
82
83 /**--------------------------------------------------------------------------------------------------
84 Fn: constexpr inline t_vertex LineSegment::pointAt(t_inter_type index) const
85
86 Point at.
87
88 Author: Tyler Parke
89
90 Date: 2017-11-19
91
92 Parameters:
93 index - Zero-based index of the.
94
95 Returns: A t_vertex.
96 *-----------------------------------------------------------------------------------------------**/
97 template<class t_inter_type>
98 constexpr inline t_vertex pointAt(t_inter_type index) const
99 {
100 return vertex(A) + (ray().template as<t_dims, t_inter_type>() * index).template as<t_dims, t_type>();
101 }
102
103 /**--------------------------------------------------------------------------------------------------
104 Fn: constexpr inline t_vertex LineSegment::midpoint() const
105
106 Gets the midpoint.
107
108 Author: Tyler Parke
109
110 Date: 2017-11-19
111
112 Returns: A t_vertex.
113 *-----------------------------------------------------------------------------------------------**/
114
115 constexpr inline t_vertex midpoint() const
116 {
117 t_vertex avg = (vertex(A) + vertex(B)) / cast<t_type>(2);
118 avg = clip(avg, getMin(vertex(A), vertex(B)), getMax(vertex(A), vertex(B)));
119 return avg;
120 }
121
122 /**--------------------------------------------------------------------------------------------------
123 Fn: constexpr inline t_vertex LineSegment::ray() const
124
125 Gets the ray.
126
127 Author: Tyler Parke
128
129 Date: 2017-11-19
130
131 Returns: A t_vertex.
132 *-----------------------------------------------------------------------------------------------**/
133
134 constexpr inline t_vertex ray() const
135 {
136 return vertex(B) - vertex(A);
137 }
138
139 /**--------------------------------------------------------------------------------------------------
140 Fn: constexpr inline t_vertex LineSegment::center() const
141
142 Gets the center.
143
144 Author: Tyler Parke
145
146 Date: 2017-11-19
147
148 Returns: A t_vertex.
149 *-----------------------------------------------------------------------------------------------**/
150
151 constexpr inline t_vertex center() const
152 {
153 return midpoint();
154 }
155
156 /**--------------------------------------------------------------------------------------------------
157 Fn: constexpr inline const t_vertex& LineSegment::vertex(uint01 index) const
158
159 Vertices the given index.
160
161 Author: Tyler Parke
162
163 Date: 2017-11-19
164
165 Parameters:
166 index - Zero-based index of the.
167
168 Returns: A reference to a const t_vertex.
169 *-----------------------------------------------------------------------------------------------**/
170
171 constexpr inline const t_vertex& vertex(uint01 index) const
172 {
174 }
175
176 /**--------------------------------------------------------------------------------------------------
177 Fn: constexpr inline t_vertex& LineSegment::vertex(uint01 index)
178
179 Vertices the given index.
180
181 Author: Tyler Parke
182
183 Date: 2017-11-19
184
185 Parameters:
186 index - Zero-based index of the.
187
188 Returns: A reference to a t_vertex.
189 *-----------------------------------------------------------------------------------------------**/
190
191 constexpr inline t_vertex& vertex(uint01 index)
192 {
194 }
195
196 /**--------------------------------------------------------------------------------------------------
197 Fn: constexpr bool LineSegment::isParallel(const LineSegment<t_dims, t_type, t_vertex>& line,
198 t_precision epsilon = 0) const
199
200 Query if 'line' is parallel.
201
202 Author: Tyler Parke
203
204 Date: 2017-11-19
205
206 Parameters:
207 line - The line.
208 epsilon - (Optional) The epsilon.
209
210 Returns: True if parallel, false if not.
211 *-----------------------------------------------------------------------------------------------**/
212 template<class t_precision = t_type>
213 constexpr bool isParallel(const LineSegment<t_dims, t_type, t_vertex>& line, t_precision epsilon = 0) const
214 {
216 const Vector<t_dims, t_precision> ray_2(line.ray().template as<t_dims, t_type>().template normalized<t_precision>());
217 return (equals(ray_1, ray_2, epsilon) || equals(ray_1, -ray_2, epsilon));
218 }
219
220 /**--------------------------------------------------------------------------------------------------
221 Fn: constexpr bool LineSegment::isCollinear(const LineSegment<t_dims, t_type>& line,
222 t_precision epsilon = 0) const
223
224 Query if 'line' is collinear.
225
226 Author: Tyler Parke
227
228 Date: 2017-11-19
229
230 Parameters:
231 line - The line.
232 epsilon - (Optional) The epsilon.
233
234 Returns: True if collinear, false if not.
235 *-----------------------------------------------------------------------------------------------**/
236 template<class t_precision = t_type>
237 constexpr bool isCollinear(const LineSegment<t_dims, t_type>& line, t_precision epsilon = 0) const
238 {
239 return isParallel<t_precision>(line, epsilon) && distanceSquared<t_precision>(line, epsilon) <= epsilon * epsilon;
240 }
241
242 /**--------------------------------------------------------------------------------------------------
243 Fn: constexpr bool LineSegment::isCollinear(const t_vertex& vert, t_precision epsilon = 0) const
244
245 Query if 'vert' is collinear.
246
247 Author: Tyler Parke
248
249 Date: 2017-11-19
250
251 Parameters:
252 vert - The vertical.
253 epsilon - (Optional) The epsilon.
254
255 Returns: True if collinear, false if not.
256 *-----------------------------------------------------------------------------------------------**/
257 template<class t_precision = t_type>
258 constexpr bool isCollinear(const t_vertex& vert, t_precision epsilon = 0) const
259 {
261 const Vector<t_dims, t_precision> ray_2((vert - vertex(A)).template normalized<t_precision>());
262 return (equals(ray_1, ray_2, epsilon) || equals(ray_1, -ray_2, epsilon));
263 }
264
265
266 /**--------------------------------------------------------------------------------------------------
267 Fn: constexpr LineSegment<t_dims, t_precision> LineSegment::closestPoints(const LineSegment<t_dims, t_type>& l2,
268 t_precision epsilon = 0) const
269
270 Closest vertices.
271
272 Author: Tyler Parke
273
274 Date: 2017-11-19
275
276 Parameters:
277 l2 - The second LineSegment&lt;t_dims,t_type&gt;
278 epsilon - (Optional) The epsilon.
279
280 Returns: A LineSegment&lt;t_dims,t_precision&gt;
281 *-----------------------------------------------------------------------------------------------**/
282 template<class t_precision, class t_other_vertex>
284 {
286 const Vector<t_dims, t_precision> v(l2.ray());
288
289 const t_precision a(dot(u, u));
290 const t_precision b(dot(u, v));
291 const t_precision c(dot(v, v));
292
293 const t_precision d(dot(u, w));
294 const t_precision e(dot(v, w));
295
296 const t_precision D((a * c) - (b * b));
297 t_precision sD = D;
298 t_precision tD = D;
299
300 t_precision sN;
301 t_precision tN;
302 // compute the line parameters of the two closest points
303 if (D > epsilon) // the lines are almost parallel
304 {
305 sN = (b*e - c*d);
306 tN = (a*e - b*d);
307 if (sN < static_cast<t_precision>(0)) // sc < 0 => the s=0 edge is visible
308 {
309 sN = static_cast<t_precision>(0);
310 tN = e;
311 tD = c;
312 }
313 else if (sN > sD) // sc > 1 => the s=1 edge is visible
314 {
315 sN = sD;
316 tN = e + b;
317 tD = c;
318 }
319 }
320 else//Consider the lines parallel
321 {
322 sN = static_cast<t_precision>(0); // force using point P0 on segment S1
323 sD = static_cast<t_precision>(1); // to prevent possible division by 0.0 later
324 tN = e;
325 tD = c;
326 }
327
328 if (tN < static_cast<t_precision>(0)) // tc < 0 => the t=0 edge is visible
329 {
330 tN = static_cast<t_precision>(0);
331 //recompute sc for this edge
332 if (-d < 0)
333 sN = static_cast<t_precision>(0);
334 else if (-d > a)
335 sN = sD;
336 else
337 {
338 sN = -d;
339 sD = a;
340 }
341 }
342 else if (tN > tD) //tc > 1 => the t=1 edge is visible
343 {
344 tN = tD;
345 // recompute sc for this edge
346 if ((-d + b) < static_cast<t_precision>(0))
347 sN = static_cast<t_precision>(0);
348 else if ((-d + b) > a)
349 sN = sD;
350 else
351 {
352 sN = (-d + b);
353 sD = a;
354 }
355 }
356
357 //finally do the division to get sc and tc
358 t_precision sc = abs(sN) <= epsilon ? static_cast<t_precision>(0) : sN / sD;
359 t_precision tc = abs(sN) <= epsilon ? static_cast<t_precision>(0) : tN / tD;
360
361 //get the difference of the two closest points
362 //Vector<t_dims, t_type>dP = w + (u * sc) - (v * tc);
363 //get the difference of the two closest points
365 (u * sc) + vertex(A).template as<t_dims, t_precision>()
366 , (v * tc) + l2.vertex(A).template as<t_dims, t_precision>());
367 }
368
369 /**--------------------------------------------------------------------------------------------------
370 Fn: constexpr t_type LineSegment::closestPos(const t_vertex& p) const
371
372 Closest position.
373
374 Author: Tyler Parke
375
376 Date: 2017-11-19
377
378 Parameters:
379 p - A t_vertex to process.
380
381 Returns: A t_type.
382 *-----------------------------------------------------------------------------------------------**/
383
384 constexpr t_type closestPos(const t_vertex& p) const
385 {
386 const Vector<t_dims, t_type> ray_1 = ray();
387 const Vector<t_dims, t_type> ray_2 = p - vertex(A);
388
389 t_type sum = (ray_1 * ray_2).sum();
390
391 const t_type mag_squared = ray_1.magnitudeSquared();
392 if (mag_squared != 0)
393 sum /= mag_squared;
394 return clip(sum, cast<t_type>(0), cast<t_type>(1));
395 }
396
397 /**--------------------------------------------------------------------------------------------------
398 Fn: constexpr t_vertex LineSegment::closestValue(const t_vertex& p) const
399
400 Closest value.
401
402 Author: Tyler Parke
403
404 Date: 2017-11-19
405
406 Parameters:
407 p - A t_vertex to process.
408
409 Returns: A t_vertex.
410 *-----------------------------------------------------------------------------------------------**/
411
412 constexpr t_vertex closestValue(const t_vertex& p) const
413 {
414 return pointAt(closestPos(p));
415 }
416 template<class t_precision = t_type>
417 t_precision distanceSquared(const LineSegment<t_dims, t_type, t_vertex>& right, const t_precision& epsilon = cast<t_precision>(0)) const
418 {
419 return closestPoints<t_precision>(right, epsilon).lengthSquared();
420 }
421
422 /**--------------------------------------------------------------------------------------------------
423 Fn: constexpr LineSegment<t_dims, t_type> LineSegment::scale(const Vector<t_dims, t_type>& scale,
424 const t_vertex& center) const
425
426 Scales.
427
428 Author: Tyler Parke
429
430 Date: 2017-11-19
431
432 Parameters:
433 scale - The scale.
434 center - The center.
435
436 Returns: A LineSegment&lt;t_dims,t_type&gt;
437 *-----------------------------------------------------------------------------------------------**/
438
439 /**--------------------------------------------------------------------------------------------------
440 Fn: constexpr LineSegment<t_dims, t_type> LineSegment::scale(const t_type& scale,
441 const t_vertex& center) const
442
443 Scales.
444
445 Author: Tyler Parke
446
447 Date: 2017-11-19
448
449 Parameters:
450 scale - The scale.
451 center - The center.
452
453 Returns: A LineSegment&lt;t_dims,t_type&gt;
454 *-----------------------------------------------------------------------------------------------**/
455 template<class t_inter_type>
460 template<class t_inter_type>
461 constexpr LineSegment<t_dims, t_type> scale(const t_inter_type& a_scale, const t_inter_type& b_scale) const
462 {
463 return LineSegment<t_dims, t_type>(pointAt(-a_scale), pointAt(b_scale + cast<t_inter_type>(1)));
464 }
465
466 constexpr LineSegment<t_dims, t_type> extend(const t_type& extension) const
467 {
468 t_vertex dx = (extension / cast<t_type>(2)) * ray().template normalized<t_type>();
469 return LineSegment<t_dims, t_type>(vertex(A) - dx, vertex(B) + dx);
470 }
471 constexpr LineSegment<t_dims, t_type> extend(const t_type& a_extension, const t_type& b_extension) const
472 {
473 t_vertex dx = ray().template normalized<t_type>();
474 return LineSegment<t_dims, t_type>(vertex(A) - (dx * a_extension), vertex(B) + (dx * b_extension));
475 }
476 /**--------------------------------------------------------------------------------------------------
477 Fn: constexpr t_precision LineSegment::length() const
478
479 Gets the length.
480
481 Author: Tyler Parke
482
483 Date: 2017-11-19
484
485 Returns: A t_precision.
486 *-----------------------------------------------------------------------------------------------**/
487 template<class t_precision = t_type>
488 constexpr t_precision length() const
489 {
490 return ray().template magnitude<t_precision>();
491 }
492
493 /**--------------------------------------------------------------------------------------------------
494 Fn: constexpr t_type LineSegment::lengthSquared() const
495
496 Length squared.
497
498 Author: Tyler Parke
499
500 Date: 2017-11-19
501
502 Returns: A t_type.
503 *-----------------------------------------------------------------------------------------------**/
504
505 constexpr t_type lengthSquared() const
506 {
507 return ray().magnitudeSquared();
508 }
509
510
511
512 /**--------------------------------------------------------------------------------------------------
513 Fn: constexpr Vector<t_dims, t_precision> LineSegment::intersection(const LineSegment& r,
514 t_precision epsilon = 0) const
515
516 Intersections.
517
518 Author: Tyler Parke
519
520 Date: 2017-11-19
521
522 Parameters:
523 r - A LineSegment to process.
524 epsilon - (Optional) The epsilon.
525
526 Returns: A Vector&lt;t_dims,t_precision&gt;
527 *-----------------------------------------------------------------------------------------------**/
528 template<class t_precision = t_type>
529 constexpr Vector<t_dims, t_precision> intersection(const LineSegment& r, t_precision epsilon = 0) const
530 {
532 if(abs(line.vertex(B) - line.vertex(A)) <= epsilon)
533 return line.vertex(B);
534 else
536 }
537
538 /**--------------------------------------------------------------------------------------------------
539 Fn: constexpr t_precision LineSegment::intersectionPosition(const LineSegment<t_dims, t_type>& segment) const
540
541 Intersection position.
542
543 Author: Tyler Parke
544
545 Date: 2017-11-19
546
547 Parameters:
548 segment - The segment.
549
550 Returns: A t_precision.
551 *-----------------------------------------------------------------------------------------------**/
552 template<class t_precision = t_type>
553 constexpr t_precision intersectionPosition(const LineSegment<t_dims, t_type>& segment) const
554 {
555 Vector<t_dims, t_precision> intersection_location(intersection<t_precision>(segment));
556 if(!isNaN(intersection_location))
557 return closestPos(intersection_location);
559 }
560
561 /**--------------------------------------------------------------------------------------------------
562 Fn: constexpr bool LineSegment::intersects(const LineSegment& segment,
563 t_precision epsilon = cast<t_precision>(0))
564
565 Query if this object intersects the given segment.
566
567 Author: Tyler Parke
568
569 Date: 2017-11-19
570
571 Parameters:
572 segment - The segment.
573 epsilon - (Optional) The epsilon.
574
575 Returns: True if it succeeds, false if it fails.
576 *-----------------------------------------------------------------------------------------------**/
577 template<class t_precision = t_type>
578 constexpr bool intersects(const LineSegment& segment, t_precision epsilon = cast<t_precision>(0.001)) const
579 {
580 return closestPoints<t_precision>(segment).lengthSquared() <= epsilon;
581 }
582
583 /**--------------------------------------------------------------------------------------------------
584 Fn: constexpr t_precision LineSegment::getLocationAt(t_precision value, uint01 dim) const
585
586 Gets location at.
587
588 Author: Tyler Parke
589
590 Date: 2017-11-19
591
592 Parameters:
593 value - The value.
594 dim - The dim.
595
596 Returns: The location at.
597 *-----------------------------------------------------------------------------------------------**/
598 template<bool t_clip, class t_precision>
599 constexpr t_precision getLocationAt(t_precision value, uint01 dim) const
600 {
601 if (vertex(A)[dim] == vertex(B)[dim])
603 if (t_clip)
604 {
605 if (vertex(A)[dim] > vertex(B)[dim])
606 {
607 if (value >= vertex(A)[dim])
608 return cast<t_precision>(0);
609 else if (value <= vertex(B)[dim])
610 return cast<t_precision>(1);
611 }
612 else
613 {
614 if (value >= vertex(B)[dim])
615 return cast<t_precision>(1);
616 else if (value <= vertex(A)[dim])
617 return cast<t_precision>(0);
618 }
619 }
620 return (vertex(A)[dim] - value) / ((value - vertex(B)[dim]) + (vertex(A)[dim] - value));
621 }
622
623
624 /**--------------------------------------------------------------------------------------------------
625 Fn: constexpr t_vertex LineSegment::pointAt(t_precision value, uint01 dim,
626 const t_vertex& nan_return = Constant<t_vertex>::NaN) const
627
628 Point at.
629
630 Author: Tyler Parke
631
632 Date: 2017-11-19
633
634 Parameters:
635 value - The value.
636 dim - The dim.
637 nan_return - (Optional) The NaN return.
638
639 Returns: A t_vertex.
640 *-----------------------------------------------------------------------------------------------**/
641 template<bool t_clip, class t_precision>
642 constexpr t_vertex pointAt(t_precision value, uint01 dim, const t_vertex& nan_return = Constant<t_vertex>::NaN) const
643 {
644 if (vertex(A)[dim] == vertex(B)[dim])
645 return nan_return;
646 if (t_clip)
647 {
648 if (vertex(A)[dim] > vertex(B)[dim])
649 {
650 if (value >= vertex(A)[dim])
651 return vertex(A);
652 else if (value <= vertex(B)[dim])
653 return vertex(B);
654 }
655 else
656 {
657 if (value >= vertex(B)[dim])
658 return vertex(B);
659 else if (value <= vertex(A)[dim])
660 return vertex(A);
661 }
662 }
663 const t_precision dt = vertex(A)[dim] - value;
664 const t_precision dy = value - vertex(B)[dim];
665 return (vertex(B) * dt + vertex(A) * dy) / (dt + dy);
666 }
667
668
669
670 /**--------------------------------------------------------------------------------------------------
671 Fn: constexpr static LineSegment LineSegment::createBestFitLine(const t_buffer_type& vertices,
672 uint01 dim_0, uint01 dim_1)
673
674 Creates best fit line.
675
676 Author: Tyler Parke
677
678 Date: 2017-11-19
679
680 Parameters:
681 vertices - The vertices.
682 dim_0 - The dim 0.
683 dim_1 - The first dim.
684
685 Returns: The new best fit line.
686 *-----------------------------------------------------------------------------------------------**/
687 template<class t_buffer_type>
688 constexpr static LineSegment createBestFitLine(const t_buffer_type& vertices, uint01 dim_0, uint01 dim_1)
689 {
690 if(vertices.size() < 2)
692 if(vertices.size() == 2)
693 return LineSegment(vertices[0], vertices[1]);
694
695 t_vertex total_vector(0);
696 t_type total_volume = 0;
697 t_type total_x_sqr = 0;
698 for(uint04 i = 0; i < vertices.size(); i++)
699 {
700 total_vector += vertices[i];
701 total_x_sqr += vertices[i][dim_0] * vertices[i][dim_0];
702 total_volume += vertices[i][dim_0] * vertices[i][dim_1];
703 }
704
705 const t_type x_sqr = total_vector[dim_0] * total_vector[dim_0];
706 const t_type a = (total_vector[dim_1] * total_x_sqr - total_vector[dim_0] * total_volume) / (vertices.size() * total_x_sqr - x_sqr);
707
708 const t_type b = (vertices.size() * total_volume - total_vector[dim_0] * total_vector[dim_1]) / (vertices.size() * total_x_sqr - x_sqr);
709
710 const t_vertex p1(vertices[0]);
711 const t_vertex p2 = vertices.getLast();
712 p1[dim_1] = a + b * p1[dim_0];
713 p2[dim_1] = a + b * p2[dim_0];
714
715 return LineSegment(p1, p2);
716 }
717 };
718
719 /**--------------------------------------------------------------------------------------------------
720 Fn: constexpr t_type distanceSquaredOptimized(const LineSegment<t_dims, t_type, t_vertex>& line,
721 const t_vertex& vertex, const t_vertex& ray, const t_type& dot_ray)
722
723 Distance squared optimized.
724
725 Author: Tyler Parke
726
727 Date: 2017-11-19
728
729 Parameters:
730 line - The line.
731 vertex - The vertex.
732 ray - The ray.
733 dot_ray - The dot ray.
734
735 Returns: A t_type.
736 *-----------------------------------------------------------------------------------------------**/
737 template<uint01 t_dims, class t_type, class t_vertex>
738 constexpr t_type distanceSquaredOptimized(const LineSegment<t_dims, t_type, t_vertex>& line, const t_vertex& vertex, const t_vertex& ray, const t_type& dot_ray)
739 {
740 const t_vertex v_t(vertex - line.vertex(A));
741 const t_type t = dot(v_t, ray);
742 if (t <= 0)
743 return v_t.magnitudeSquared();
744 else if (t >= dot_ray)
745 return distanceSquared(vertex, line.vertex(B));
746 else
747 {
748 t_vertex vc = (ray * t) / dot_ray + line.vertex(A);
749 return distanceSquared(vertex, vc);
750 }
751 }
752 /**--------------------------------------------------------------------------------------------------
753 Fn: t_type distanceSquared(const LineSegment<t_dims, t_type, t_vertex>& line,
754 const t_vertex& vertex)
755
756 Distance squared.
757
758 Author: Tyler Parke
759
760 Date: 2017-11-19
761
762 Parameters:
763 line - The line.
764 vertex - The vertex.
765
766 Returns: A t_type.
767 *-----------------------------------------------------------------------------------------------**/
768 template<uint01 t_dims, class t_type, class t_vertex>
769 t_type distanceSquared(const LineSegment<t_dims, t_type, t_vertex>& line, const t_vertex& vertex)
770 {
771 const t_vertex ray_value(line.ray());
772 const t_type dot_ray = dot(ray_value, ray_value);
773 return distanceSquaredOptimized(line, vertex, ray_value, dot_ray);
774 }
775
776
777 /**--------------------------------------------------------------------------------------------------
778 Fn: constexpr t_type distanceSquared(const t_vertex& vertex,
779 const LineSegment<t_dims, t_type, t_vertex>& line)
780
781 Distance squared.
782
783 Author: Tyler Parke
784
785 Date: 2017-11-19
786
787 Parameters:
788 vertex - The vertex.
789 line - The line.
790
791 Returns: A t_type.
792 *-----------------------------------------------------------------------------------------------**/
793 template<uint01 t_dims, class t_type, class t_vertex>
794 constexpr t_type distanceSquared(const t_vertex& vertex, const LineSegment<t_dims, t_type, t_vertex>& line)
795 {
796 return distanceSquared(line, vertex);
797 }
798
799
800
801 /**--------------------------------------------------------------------------------------------------
802 Fn: constexpr bool equals(const LineSegment<t_dims, t_type, t_vertex>& left,
803 const LineSegment<t_dims, t_type, t_vertex>& right, const t_type& epsilon = cast<t_type>(0))
804
805 Tests if objects are considered equal.
806
807 Author: Tyler Parke
808
809 Date: 2017-11-19
810
811 Parameters:
812 left - Constant line segment&lt;t dims,t type,t vertex&gt;&amp; to be compared.
813 right - Constant line segment&lt;t dims,t type,t vertex&gt;&amp; to be compared.
814 epsilon - (Optional) Constant type&amp; to be compared.
815
816 Returns: True if the objects are considered equal, false if they are not.
817 *-----------------------------------------------------------------------------------------------**/
818 template<uint01 t_dims, class t_type, class t_vertex>
819 constexpr bool equals(const LineSegment<t_dims, t_type, t_vertex>& left, const LineSegment<t_dims, t_type, t_vertex>& right, const t_type& epsilon = cast<t_type>(0))
820 {
821 if (equals(left[A], right[A], epsilon))
822 {
823 return (equals(left[B], right[B], epsilon));
824 }
825 if (equals(left[B], right[A], epsilon))
826 {
827 return (equals(left[A], right[B], epsilon));
828 }
829 return false;
830 }
831
832
833
834
835
836 template<uint01 t_dims, class t_type>
837 static constexpr bool isNaN(const LineSegment<t_dims, t_type>& value)
838 {
839 for (uint01 dim = 0; dim < 2; ++dim)
840 {
841 if (isNaN(value[dim]))
842 return true;
843 }
844 return false;
845 }
846
847 template<uint01 t_dims, class t_type, class t_vector>
854
855};
856
A line segment represented by two vertices, a start and end.
Definition Line.hpp:55
constexpr LineSegment< t_dims, t_type > extend(const t_type &a_extension, const t_type &b_extension) const
Definition Line.hpp:471
constexpr Vector< t_dims, t_precision > intersection(const LineSegment &r, t_precision epsilon=0) const
Definition Line.hpp:529
constexpr t_vertex ray() const
Definition Line.hpp:134
constexpr LineSegment()
Definition Line.hpp:57
constexpr t_precision length() const
Definition Line.hpp:488
constexpr t_vertex pointAt(t_precision value, uint01 dim, const t_vertex &nan_return=Constant< t_vertex >::NaN) const
Definition Line.hpp:642
constexpr LineSegment(const t_vertex &p1, const t_vertex &p2)
Definition Line.hpp:59
constexpr LineSegment< t_dims, t_type > scale(const t_inter_type &scale) const
Definition Line.hpp:456
constexpr bool isCollinear(const LineSegment< t_dims, t_type > &line, t_precision epsilon=0) const
Definition Line.hpp:237
constexpr t_vertex midpoint() const
Definition Line.hpp:115
constexpr t_precision intersectionPosition(const LineSegment< t_dims, t_type > &segment) const
Definition Line.hpp:553
constexpr t_precision getLocationAt(t_precision value, uint01 dim) const
Definition Line.hpp:599
static constexpr LineSegment createBestFitLine(const t_buffer_type &vertices, uint01 dim_0, uint01 dim_1)
Definition Line.hpp:688
constexpr bool isCollinear(const t_vertex &vert, t_precision epsilon=0) const
Definition Line.hpp:258
constexpr LineSegment(const Vector< 2, t_vertex > &line)
Definition Line.hpp:62
constexpr const t_vertex & vertex(uint01 index) const
Definition Line.hpp:171
constexpr t_vertex & vertex(uint01 index)
Definition Line.hpp:191
constexpr LineSegment< t_dims, t_type > extend(const t_type &extension) const
Definition Line.hpp:466
t_precision distanceSquared(const LineSegment< t_dims, t_type, t_vertex > &right, const t_precision &epsilon=cast< t_precision >(0)) const
Definition Line.hpp:417
constexpr t_type lengthSquared() const
Definition Line.hpp:505
constexpr t_vertex center() const
Definition Line.hpp:151
constexpr t_vertex closestValue(const t_vertex &p) const
Definition Line.hpp:412
constexpr t_vertex pointAt(t_inter_type index) const
Definition Line.hpp:98
constexpr bool isParallel(const LineSegment< t_dims, t_type, t_vertex > &line, t_precision epsilon=0) const
Definition Line.hpp:213
constexpr LineSegment< t_dims, t_type > scale(const t_inter_type &a_scale, const t_inter_type &b_scale) const
Definition Line.hpp:461
constexpr t_type closestPos(const t_vertex &p) const
Definition Line.hpp:384
constexpr LineSegment< t_new_dims, t_new_type, t_new_vertex > as() const
Definition Line.hpp:78
constexpr bool intersects(const LineSegment &segment, t_precision epsilon=cast< t_precision >(0.001)) const
Definition Line.hpp:578
constexpr LineSegment< t_dims, t_precision > closestPoints(const LineSegment< t_dims, t_type, t_other_vertex > &l2, t_precision epsilon=0) const
Definition Line.hpp:283
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
constexpr t_magnitude_type magnitude() const
Definition Vector.hpp:482
constexpr Vector() noexcept
Definition Vector.hpp:64
constexpr t_type sum() const
Definition Vector.hpp:545
constexpr t_type & operator[](uint01 dimension_index)
Definition Vector.hpp:568
constexpr t_type magnitudeSquared() const
Definition Vector.hpp:458
constexpr Vector< t_dims, t_norm_type > normalized(Vector< t_dims, t_norm_type > value_if_nan=Constant< Vector< t_dims, t_norm_type > >::NaN) const
Definition Vector.hpp:500
Definition ACIColor.h:37
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator.
Definition BaseFunctions.hpp:116
constexpr t_type clip(const t_type &value, const t_type &lower_bound, const t_type &upper_bound)
Clips the value given so that that the returned value falls between upper and lower bound.
Definition BaseFunctions.hpp:240
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1096
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
t_type distanceSquared(const Bounds< t_dims, t_type, t_vertex > &bounds, const Vector< t_dims, t_type > &vertex)
Definition Distance.hpp:42
constexpr t_type distanceSquaredOptimized(const LineSegment< t_dims, t_type, t_vertex > &line, const t_vertex &vertex, const t_vertex &ray, const t_type &dot_ray)
Definition Line.hpp:738
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr bool equals(const LineSegment< t_dims, t_type, t_vertex > &left, const LineSegment< t_dims, t_type, t_vertex > &right, const t_type &epsilon=cast< t_type >(0))
Definition Line.hpp:819
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Definition AngleFunctions.h:750
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
@ B
Definition BaseValues.hpp:203
@ A
Definition BaseValues.hpp:201
@ D
Definition BaseValues.hpp:207
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator.
Definition BaseFunctions.hpp:67
Definition BaseValues.hpp:272
static const t_type Min
Definition BaseValues.hpp:276
static const t_type Max
Definition BaseValues.hpp:278
static const t_type NaN
Definition BaseValues.hpp:274