API Documentation
Loading...
Searching...
No Matches
Bounds.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: Bounds
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/Vertex.h>
35#include <NDEVR/LineSegment.h>
36#include <NDEVR/Triangle.h>
37#include <NDEVR/RadialObject.h>
38namespace NDEVR
39{
40
41 /**--------------------------------------------------------------------------------------------------
42 Class: Bounds
43
44 \brief A specification of upper and lower bounds in N-dimensions.
45
46 Author: Tyler Parke
47
48 Date: 2017-11-17
49 *-----------------------------------------------------------------------------------------------**/
50 template<uint01 t_dims, class t_type, class t_vertex = Vertex<t_dims, t_type>>
51 class Bounds : public Vector<2, t_vertex>
52 {
53 public:
54 constexpr Bounds()
55 {}
56
57 /**--------------------------------------------------------------------------------------------------
58 \brief Given the vector, creates bounds of size 0 where max and min are both equal to the vertex
59
60 Author: Tyler Parke
61
62 Date: 2017-11-17
63
64 Parameters:
65 vector - The vertex around which a bounds of size 0 will be made.
66 *-----------------------------------------------------------------------------------------------**/
67 constexpr Bounds(const t_vertex& vertex)
68 : Vector<2, t_vertex>(vertex, vertex)
69 {}
70
71 /**--------------------------------------------------------------------------------------------------
72
73 \brief Given the center, creates bounds of size where max and min are both equal to the vertex + size and
74 - size respectively.
75
76 Author: Tyler Parke
77
78 Date: 2017-11-17
79
80 Parameters:
81 center - The center of the bounds.
82 size - The size of the bounds.
83 *-----------------------------------------------------------------------------------------------**/
84 constexpr Bounds(const t_vertex& center, t_type size)
85 : Vector<2, t_vertex>(center - size, center + size)
86 {}
87 constexpr Bounds(const t_vertex& min, const t_vertex& max)
88 : Vector<2, t_vertex>(min, max)
89 {}
90 constexpr Bounds(const t_type& min_scaler, const t_type& max_scaler)
91 : Vector<2, t_vertex>(t_vertex(min_scaler), t_vertex(max_scaler))
92 {}
93
94 constexpr Bounds(const Vector<2, t_vertex>& bounds)
95 : Vector<2, t_vertex>(bounds)
96 {}
97
98 constexpr Bounds(const Bounds& bounds_a, const Bounds& bounds_b)
99 : Vector<2, t_vertex>(getMin(bounds_a[MIN], bounds_b[MIN]), getMax(bounds_a[MAX], bounds_b[MAX]))
100 {}
101
102 constexpr Bounds(const Bounds& bounds, const t_vertex& vector)
103 : Vector<2, t_vertex>(getMin(bounds[MIN], vector), getMax(bounds[MAX], vector))
104 {}
106 : Vector<2, t_vertex>(getMin(line.vertex(A), line.vertex(B)), getMax(line.vertex(A), line.vertex(B)))
107 {}
108
110 : Vector<2, t_vertex>(
111 getMin(tri.vertex(A), tri.vertex(B), tri.vertex(C))
112 , getMax(tri.vertex(A), tri.vertex(B), tri.vertex(C)))
113 {}
114
116 : Vector<2, t_vertex>(radial.center() - radial.radius(), radial.center() + radial.radius())
117 {}
118
119 /**--------------------------------------------------------------------------------------------------
120 \brief The side lengths of these bounds. For each dimension, the span is max - min.
121
122 Author: Tyler Parke
123
124 Date: 2017-11-17
125
126 Returns: A t_vertex defining the span of the range in each dimension.
127 *-----------------------------------------------------------------------------------------------**/
132
133 /**--------------------------------------------------------------------------------------------------
134 \brief Returns the center of the bounds.
135
136 Author: Tyler Parke
137
138 Date: 2017-11-17
139
140 Returns: A t_vertex representing the center.
141 *-----------------------------------------------------------------------------------------------**/
142
143 constexpr t_vertex center() const
144 {
146 }
147
148 /**--------------------------------------------------------------------------------------------------
149 \brief Returns the volume of the bounds. This is defined as length in 1 dimension, area in 2 dimensions.
150
151 Author: Tyler Parke
152
153 Date: 2017-11-17
154
155 Returns: A t_type giving the volume of the bounds.
156 *-----------------------------------------------------------------------------------------------**/
157
158 constexpr t_type volume() const
159 {
160 return span().product();
161 }
162 /**--------------------------------------------------------------------------------------------------
163 \brief The surface area of the shape. This is defined as the area between internal space and non-internal space.
164
165 Author: Tyler Parke
166
167 Date: 2017-11-17
168
169 Returns: A t_type representing the area of the surface created by the bounds.
170 *-----------------------------------------------------------------------------------------------**/
171 constexpr t_type surfaceArea() const
172 {
173 t_type area = 0;
174 t_vertex side_lengths = span();
175 for (uint01 dim_a = 0; dim_a < t_dims; dim_a++)
176 {
177 for (uint01 dim_b = 0; dim_b < t_dims; dim_b++)
178 {
179 if (dim_b != dim_a)
180 {
181 area += side_lengths[dim_a] * side_lengths[dim_b];
182 }
183 }
184 }
185 return area;
186 }
187
188 /**--------------------------------------------------------------------------------------------------
189 \brief Expands the given expansion scaler. such that max and min are both expanded outward from the center by
190 the given expansion scaler
191
192 Author: Tyler Parke
193
194 Date: 2017-11-17
195
196 Parameters:
197 expansion_scaler - The expansion scaler.
198 *-----------------------------------------------------------------------------------------------**/
199
200 constexpr void expand(const t_type& expansion_scaler)
201 {
202 (*this)[MIN] -= expansion_scaler;
203 (*this)[MAX] += expansion_scaler;
204 }
205
206 /**--------------------------------------------------------------------------------------------------
207 \brief Expands the given expansion scaler. such that max and min are both expanded outward from the center by
208 the given expansion vector. That is that each dimension may be scaled out differently.
209
210 Author: Tyler Parke
211
212 Date: 2017-11-17
213
214 Parameters:
215 expansion_vector - The expansion vector.
216 *-----------------------------------------------------------------------------------------------**/
217
218 constexpr void expand(const t_vertex& expansion_vector)
219 {
220 Vector<2, t_vertex>::m_values[MIN] -= expansion_vector;
221 Vector<2, t_vertex>::m_values[MAX] += expansion_vector;
222 }
223
224 /**--------------------------------------------------------------------------------------------------
225
226 \brief Scales this geometry about a center point
227
228 Author: Tyler Parke
229
230 Date: 2017-11-17
231
232 Parameters:
233 scale - The scale.
234 center - The center.
235
236 Returns: A Bounds&lt;t_dims,t_type&gt;
237 *-----------------------------------------------------------------------------------------------**/
239 {
240 t_vertex scale_a = Vector<2, t_vertex>::m_values[MIN].scale(scale, center);
241 t_vertex scale_b = Vector<2, t_vertex>::m_values[MAX].scale(scale, center);
242 return Bounds<t_dims, t_type>(getMin(scale_a, scale_b), getMax(scale_a, scale_b));
243 }
244 constexpr Bounds<t_dims, t_type> scale(const Vector<t_dims, t_type>& center_scale) const
245 {
246 return scale(center_scale, center());
247 }
248
249 constexpr Bounds<t_dims, t_type> scale(const t_type& scale, const Vector<t_dims, t_type>& center) const
250 {
251 t_vertex scale_a = Vector<2, t_vertex>::m_values[MIN].scale(scale, center);
252 t_vertex scale_b = Vector<2, t_vertex>::m_values[MAX].scale(scale, center);
253 return Bounds<t_dims, t_type>(getMin(scale_a, scale_b), getMax(scale_a, scale_b));
254 }
255
256 constexpr Bounds<t_dims, t_type> scale(const t_type& center_scale) const
257 {
258 auto point = center();
259 t_vertex scale_a = Vector<2, t_vertex>::m_values[MIN].scale(center_scale, point);
260 t_vertex scale_b = Vector<2, t_vertex>::m_values[MAX].scale(center_scale, point);
261 if(center_scale > 0)
262 return Bounds<t_dims, t_type>(scale_a, scale_b);
263 else
264 return Bounds<t_dims, t_type>(scale_b, scale_a);
265 }
266
267 /**--------------------------------------------------------------------------------------------------
268 \brief Casts this object into an object of different dimension or precision.
269
270 Author: Tyler Parke
271
272 Date: 2017-11-13
273
274 Parameters:
275 extra_fill_value - (Optional) The extra fill value, or value to set dimensions extra that may be
276 created when t_new_dim > t_dim.
277 *-----------------------------------------------------------------------------------------------**/
278 template<uint01 t_new_dims, class t_new_type, class t_new_vertex = Vertex<t_new_dims, t_new_type>>
279 constexpr Bounds<t_new_dims, t_new_type, t_new_vertex> as(t_new_type extra_fill_value = t_new_type(0)) const
280 {
282 t_new_vertex((*this)[MIN].template as<t_new_dims, t_new_type>(extra_fill_value))
283 , t_new_vertex((*this)[MAX].template as<t_new_dims, t_new_type>(extra_fill_value)));
284 }
285
286 /**--------------------------------------------------------------------------------------------------
287 \brief Query if this object contains the given value.
288
289 Author: Tyler Parke
290
291 Date: 2017-11-17
292
293 Parameters:
294 t_allow_bounds - whether or not to allow boundary cases to be considered valid.
295
296 vector - The const t_vertex&amp; to test for containment.
297
298 Returns: True if the object is in the bounds, false if not.
299 *-----------------------------------------------------------------------------------------------**/
300 template<bool t_allow_bounds = true, uint01 tdims = t_dims
301 , typename = typename std::enable_if<tdims == 1>::type>
302 constexpr bool contains(const t_type & value) const
303 {
304 if (t_allow_bounds)
305 {
306 return !(!(value >= (*this)[MIN][0]) || !(value <= (*this)[MAX][0]));
307 }
308 else
309 {
310 return !(!(value > (*this)[MIN][0]) || !(value < (*this)[MAX][0]));
311 }
312 }
313
314 /**--------------------------------------------------------------------------------------------------
315 \brief Query if this object contains the given vector.
316
317 Author: Tyler Parke
318
319 Date: 2017-11-17
320
321 Parameters:
322 t_allow_bounds - whether or not to allow boundary cases to be considered valid.
323
324 vector - The const t_vertex&amp; to test for containment.
325
326 Returns: True if the object is in the bounds, false if not.
327 *-----------------------------------------------------------------------------------------------**/
328 template<bool t_allow_bounds = true>
329 constexpr bool contains(const t_vertex& vector) const
330 {
331 if (t_allow_bounds)
332 {
333 for (uint01 dim = 0; dim < t_dims; ++dim)
334 {
335 if (!(vector[dim] >= (*this)[MIN][dim]) || !(vector[dim] <= (*this)[MAX][dim]))
336 return false;
337 }
338 }
339 else
340 {
341 for (uint01 dim = 0; dim < t_dims; ++dim)
342 {
343 if (!(vector[dim] > (*this)[MIN][dim]) || !(vector[dim] < (*this)[MAX][dim]))
344 return false;
345 }
346 }
347 return true;
348 }
349
350 /**--------------------------------------------------------------------------------------------------
351 \brief Query if this object contains the given bounds.
352
353 Author: Tyler Parke
354
355 Date: 2017-11-17
356
357 Parameters:
358 t_allow_bounds - whether or not to allow boundary cases to be considered valid.
359
360 bounds - The bounds to check if it is inside object.
361
362 Returns: True if it succeeds, false if it fails.
363 *-----------------------------------------------------------------------------------------------**/
364 template<bool t_allow_bounds = true>
365 constexpr bool contains(const Bounds& bounds) const
366 {
367 if (t_allow_bounds)
368 {
369 for (uint01 dim = 0; dim < t_dims; ++dim)
370 {
371 if (!(bounds[MIN][dim] >= (*this)[MIN][dim]))
372 return false;
373 if (!(bounds[MAX][dim] <= (*this)[MAX][dim]))
374 return false;
375 }
376 }
377 else
378 {
379 for (uint01 dim = 0; dim < t_dims; ++dim)
380 {
381 if (!(bounds[MIN][dim] >(*this)[MIN][dim]))
382 return false;
383 if (!(bounds[MAX][dim] < (*this)[MAX][dim]))
384 return false;
385 }
386 }
387 return true;
388 }
389
390 /**--------------------------------------------------------------------------------------------------
391 Fully contains.
392
393 \brief Author: Tyler Parke
394
395 Date: 2017-11-17
396
397 Parameters:
398 vector - The vector.
399
400 Returns: True if it succeeds, false if it fails.
401 *-----------------------------------------------------------------------------------------------**/
402 template<bool t_allow_bounds = true>
403 constexpr bool contains(const LineSegment<t_dims, t_type, t_vertex>& line) const
404 {
405 if (t_allow_bounds)
406 {
407 for (uint01 dim = 0; dim < t_dims; ++dim)
408 {
409 if (!(line.vertex(A)[dim] >= (*this)[MIN][dim] && line.vertex(B)[dim] >= (*this)[MIN][dim]))
410 return false;
411 if (!(line.vertex(A)[dim] <= (*this)[MAX][dim] && line.vertex(B)[dim] <= (*this)[MAX][dim]))
412 return false;
413 }
414 }
415 else
416 {
417 for (uint01 dim = 0; dim < t_dims; ++dim)
418 {
419 if (!(line.vertex(A)[dim] > (*this)[MIN][dim] && line.vertex(B)[dim] > (*this)[MIN][dim]))
420 return false;
421 if (!(line.vertex(A)[dim] < (*this)[MAX][dim] && line.vertex(B)[dim] < (*this)[MAX][dim]))
422 return false;
423 }
424 }
425 return true;
426 }
427
428
429 /**--------------------------------------------------------------------------------------------------
430 \brief Fully contains.
431
432 Author: Tyler Parke
433
434 Date: 2017-11-17
435
436 Parameters:
437 tri - The triangle.
438
439 Returns: True if it succeeds, false if it fails.
440 *-----------------------------------------------------------------------------------------------**/
441 template<bool t_allow_bounds = true>
442 constexpr bool contains(const Triangle<t_dims, t_type, t_vertex>& tri) const
443 {
444 for (uint01 tri_index = A; tri_index <= C; tri_index++)
445 if (!contains<t_allow_bounds>(tri.vertex(tri_index)))
446 return false;
447 return true;
448 }
449 /**--------------------------------------------------------------------------------------------------
450 \brief Adds to the bounds such that the new bounds fully encompasses the argument.
451
452 Author: Tyler Parke.
453
454 Date:
455 2017 - 11-17.
456
457 Parameters:
458 vector - The vector.
459 *-----------------------------------------------------------------------------------------------**/
460
461 constexpr void addToBounds(const t_vertex& vector)
462 {
463 for (uint01 dim = 0; dim < t_dims; ++dim)
464 {
465 if ((*this)[MAX][dim] < vector[dim])
466 (*this)[MAX][dim] = vector[dim];
467 if ((*this)[MIN][dim] > vector[dim])
468 (*this)[MIN][dim] = vector[dim];
469 }
470 }
471 template<uint01 tdims = t_dims>
472 constexpr void addToBounds(typename std::enable_if<tdims == 1, const t_type&>::type scaler)
473 {
474 for (uint01 dim = 0; dim < tdims; ++dim)
475 {
476 if ((*this)[MAX][dim] < scaler)
477 (*this)[MAX][dim] = scaler;
478 if ((*this)[MIN][dim] > scaler)
479 (*this)[MIN][dim] = scaler;
480 }
481 }
482
483 /**--------------------------------------------------------------------------------------------------
484 \brief Adds to the bounds such that the new bounds fully encompasses the argument.
485
486 Author: Tyler Parke.
487
488 Date:
489 2017-11-17.
490
491 Parameters:
492 bounds - The bounds to add to this bounds.
493 *-----------------------------------------------------------------------------------------------**/
494
495 constexpr void addToBounds(const Bounds& bounds)
496 {
497 for (uint01 dim = 0; dim < t_dims; ++dim)
498 {
499 if ((*this)[MAX][dim] < bounds[MAX][dim])
500 (*this)[MAX][dim] = bounds[MAX][dim];
501 if ((*this)[MIN][dim] > bounds[MIN][dim])
502 (*this)[MIN][dim] = bounds[MIN][dim];
503 }
504 }
505
506 /**--------------------------------------------------------------------------------------------------
507 \brief Adds to the bounds such that the new bounds fully encompasses the argument.
508
509 Author: Tyler Parke.
510
511 Date:
512 2017-11-17.
513
514 Parameters:
515 line_segment - The line segment.
516 *-----------------------------------------------------------------------------------------------**/
517
518 constexpr void addToBounds(const LineSegment<t_dims, t_type, t_vertex>& line_segment)
519 {
520 addToBounds(line_segment.vertex(A));
521 addToBounds(line_segment.vertex(B));
522 }
523
524 /**--------------------------------------------------------------------------------------------------
525 \brief Adds to the bounds such that the new bounds fully encompesses the argument.
526
527 Author: Tyler Parke.
528
529 Date:
530 2017-11-17.
531
532 Parameters:
533 triangle - The triangle.
534 *-----------------------------------------------------------------------------------------------**/
535
536 constexpr void addToBounds(const Triangle<t_dims, t_type, t_vertex>& triangle)
537 {
538 addToBounds(triangle.vertex(A));
539 addToBounds(triangle.vertex(B));
540 addToBounds(triangle.vertex(C));
541 }
542
543 /**--------------------------------------------------------------------------------------------------
544 \brief Query if this object contains the given radial_object.
545
546 Author: Tyler Parke
547
548 Date: 2017-11-18
549
550 Parameters:
551 radial_object - The const RadialObject&lt;t_dims,t_type&gt;&amp; to test for containment.
552
553 Returns: True if the object is in this collection, false if not.
554 *-----------------------------------------------------------------------------------------------**/
555 template<bool t_allow_bounds = true>
556 constexpr bool contains(const RadialObject<t_dims, t_type>& radial_object) const
557 {
558 if (t_allow_bounds)
559 return distanceSquared(closestEdge(radial_object.center()), radial_object.center()) <= radial_object.radius() * radial_object.radius();
560 else
561 return distanceSquared(closestEdge(radial_object.center()), radial_object.center()) < radial_object.radius() * radial_object.radius();
562 }
563
564 /**--------------------------------------------------------------------------------------------------
565 \brief Closest edge.
566
567 Author: Tyler Parke.
568
569 Date:
570 2017 - 11-17.
571
572 Parameters:
573 vertex - The first t_vertex.
574
575 Returns: The closest edge.
576 *-----------------------------------------------------------------------------------------------**/
577
578 constexpr t_vertex closestEdge(const t_vertex& vertex) const
579 {
580 t_vertex closest_edge;
581 for(uint01 dim = 0; dim < t_dims; ++dim)
582 closest_edge[dim] = abs(vertex[dim] - (*this)[MIN][dim]) > abs(vertex[dim] - (*this)[MAX][dim]) ? (*this)[MAX][dim] : (*this)[MIN][dim];
583 return closest_edge;
584 }
585
586 /**--------------------------------------------------------------------------------------------------
587 \brief Closest value.
588
589 Author: Tyler Parke.
590
591 Date:
592 2017 - 11-17.
593
594 Parameters:
595 vertex - The first t_vertex.
596
597 Returns: The closest element.
598 *-----------------------------------------------------------------------------------------------**/
599
600 constexpr t_vertex closestValue(const t_vertex& vertex) const
601 {
602 t_vertex closest_vertex;
603 for(uint01 dim = 0; dim < t_dims; ++dim)
604 closest_vertex[dim] = (vertex[dim] < (*this)[MIN][dim]) ? (*this)[MIN][dim] : (vertex[dim] > (*this)[MAX][dim]) ? (*this)[MAX][dim] : vertex[dim];
605 return closest_vertex;
606 }
607
608 /**--------------------------------------------------------------------------------------------------
609 \brief Furthest value.
610
611 Author: Tyler Parke.
612
613 Date:
614 2017 - 11-17.
615
616 Parameters:
617 vertex - The first t_vertex.
618
619 Returns: The furthest point.
620 *-----------------------------------------------------------------------------------------------**/
621
622 constexpr t_vertex furthestValue(const t_vertex& vertex) const
623 {
624 t_vertex furthest_vertex;
625 for(uint01 dim = 0; dim < t_dims; ++dim)
626 furthest_vertex[dim] = abs(vertex[dim] - (*this)[MIN][dim]) > abs(vertex[dim] - (*this)[MAX][dim]) ? (*this)[MIN][dim] : (*this)[MAX][dim];
627 return furthest_vertex;
628 }
629
630 /**--------------------------------------------------------------------------------------------------
631
632 \brief Checks for intersection of the ray from a given distance, excluding one axis.
633
634 Author: Tyler Parke
635
636 Date: 2017-11-18
637
638 Parameters:
639 distance_a - The distance a.
640 distance_b - The distance b.
641 origin - The origin.
642 ray - The ray.
643 exclusion_axis - The exclusion axis.
644
645 Returns: True if it succeeds, false if it fails.
646 *-----------------------------------------------------------------------------------------------**/
647
648 constexpr bool doesIntersect(t_type distance_a, t_type distance_b, const t_vertex& origin, const Vector<t_dims, t_type>& ray, uint01 exclusion_axis) const
649 {
650 if(!((distance_a * distance_b) < cast<t_type>(0))) //!< for Invalid
651 return false;
652
653 t_type unit_value = (-distance_a / (distance_b - distance_a));
654 for (uint01 dim = 0; dim < exclusion_axis; ++dim)
655 {
656 t_type hit_location = origin[dim] + ray[dim] * unit_value;
657 if (hit_location < (*this)[MIN][dim] || hit_location > (*this)[MAX][dim])
658 return false;
659 }
660 for (uint01 dim = exclusion_axis + 1; dim < t_dims; ++dim)
661 {
662 t_type hit_location = origin[dim] + ray[dim] * unit_value;
663 if (hit_location < (*this)[MIN][dim] || hit_location > (*this)[MAX][dim])
664 return false;
665 }
666 return true;
667 }
668
669 /**--------------------------------------------------------------------------------------------------
670 \brief Query if this object intersects the given pair.
671
672 Author: Tyler Parke
673
674 Date: 2017-11-17
675
676 Parameters:
677 pair - The pair.
678
679 Returns: True if it succeeds, false if it fails.
680 *-----------------------------------------------------------------------------------------------**/
681 template<class t_other_vertex_type>
682 constexpr bool intersects(const Bounds<t_dims, t_type, t_other_vertex_type>& bounds) const
683 {
684 for (uint01 dim = 0; dim < t_dims; ++dim)
685 {
686 if (!((*this)[MAX][dim] > bounds[MIN][dim] && (*this)[MIN][dim] < bounds[MAX][dim]))
687 return false;
688 }
689 return true;
690 }
691
692
693 template<class t_other_vertex_type>
695 {
696 if (contains(seg))
697 {
698 return true;
699 }
700 const Vector<t_dims, t_type> ray = seg.ray();
701 for (uint01 i = 0; i < t_dims; ++i)
702 {
703 if ( doesIntersect(seg.vertex(A)[i] - (*this)[MIN][i], seg.vertex(B)[i] - (*this)[MIN][i], seg.vertex(A), ray, i)
704 || doesIntersect(seg.vertex(A)[i] - (*this)[MAX][i], seg.vertex(B)[i] - (*this)[MAX][i], seg.vertex(A), ray, i))
705 return true;
706 }
707 return false;
708 }
709
710 /**--------------------------------------------------------------------------------------------------
711 \brief Query if this object contains the given circle.
712
713 Author: Tyler Parke
714
715 Date: 2017-11-17
716
717 Parameters:
718 circle - The const RadialObject&lt;t_rad_dims,t_rad_type&gt;&amp; to test for containment.
719
720 Returns: True if the object is in this collection, false if not.
721 *-----------------------------------------------------------------------------------------------**/
722 template<bool t_allow_bounds = true>
723 constexpr bool intersects(const RadialObject<t_dims, t_type>& circle) const
724 {
725 t_type dist_squared = circle.radius() * circle.radius();
726 for (uint01 dim = 0; dim < t_dims; ++dim)
727 {
728 if (circle.getCenter()[dim] < (*this)[MIN][dim])
729 {
730 t_type value = circle.center()[dim] - (*this)[MIN][dim];
731 dist_squared -= (value * value);
732 }
733 else if (circle.getCenter()[dim] > (*this)[MAX][dim])
734 {
735 t_type value = circle.center()[dim] - (*this)[MAX][dim];
736 dist_squared -= (value * value);
737 }
738 if (t_allow_bounds)
739 {
740 if (dist_squared < 0)
741 return false;
742 }
743 else
744 {
745 if (dist_squared <= 0)
746 return false;
747 }
748 }
749 return true;
750 }
751
752 /**--------------------------------------------------------------------------------------------------
753 Validates this object. Returns true if the value at MIN is less than or equal the value at MAX in all
754 dimensions.
755
756 Author: Tyler Parke
757
758 Date: 2017-11-17
759
760 Returns: True if it succeeds, false if it fails.
761 *-----------------------------------------------------------------------------------------------**/
762
763 [[nodiscard]] constexpr bool validate() const
764 {
765 if (getMin((*this)[MIN], (*this)[MAX]) != (*this)[MIN])
766 return false;
767 if (getMax((*this)[MIN], (*this)[MAX]) != (*this)[MAX])
768 return false;
769 return true;
770 }
771
772 /**--------------------------------------------------------------------------------------------------
773 \brief Ensures that this is a valid bounds object. Values are swapped within the structure to ensure that
774 MAX >= MIN
775
776 Author: Tyler Parke
777
778 Date: 2022-01-20
779 *-----------------------------------------------------------------------------------------------**/
781 {
782 t_vertex min = (*this)[MIN];
783 t_vertex max = (*this)[MAX];
784 (*this)[MIN] = getMin(min, max);
785 (*this)[MAX] = getMax(min, max);
786 }
787 Bounds<t_dims, t_type>& operator-=(const t_type& center_scale)
788 {
789 (*this)[MAX] -= center_scale;
790 (*this)[MIN] -= center_scale;
791 return *this;
792 }
793 };
794
795
796
797 template<uint01 t_dims, class t_type, class t_vertex>
798 struct Constant<Bounds<t_dims, t_type, t_vertex>>
799 {
800 constexpr static Bounds<t_dims, t_type, t_vertex> Invalid{ Constant<t_type>::Invalid, Constant<t_type>::Invalid };
801 constexpr static Bounds<t_dims, t_type, t_vertex> Min{ Constant<t_type>::Max, Constant<t_type>::Min };
802 constexpr static Bounds<t_dims, t_type, t_vertex> Max{ Constant<t_type>::Min, Constant<t_type>::Max };
803 };
804
805 template<uint01 t_dims, class t_type, class t_vertex>
806 static constexpr bool IsInvalid(const Bounds<t_dims, t_type, t_vertex>& value)
807 {
808 for (uint01 dim = 0; dim < 2; ++dim)
809 {
810 if (IsInvalid(value[dim]))
811 return true;
812 }
813 return false;
814 }
815
816};
817
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:52
constexpr bool validate() const
Definition Bounds.hpp:763
constexpr Bounds()
Definition Bounds.hpp:54
constexpr Bounds(const t_vertex &min, const t_vertex &max)
Definition Bounds.hpp:87
constexpr t_type surfaceArea() const
The surface area of the shape. This is defined as the area between internal space and non-internal sp...
Definition Bounds.hpp:171
constexpr t_type volume() const
Returns the volume of the bounds. This is defined as length in 1 dimension, area in 2 dimensions.
Definition Bounds.hpp:158
constexpr Bounds< t_new_dims, t_new_type, t_new_vertex > as(t_new_type extra_fill_value=t_new_type(0)) const
Casts this object into an object of different dimension or precision.
Definition Bounds.hpp:279
constexpr void addToBounds(const LineSegment< t_dims, t_type, t_vertex > &line_segment)
Adds to the bounds such that the new bounds fully encompasses the argument.
Definition Bounds.hpp:518
constexpr void addToBounds(const t_vertex &vector)
Adds to the bounds such that the new bounds fully encompasses the argument.
Definition Bounds.hpp:461
constexpr Ray< t_dims, t_type > span() const
The side lengths of these bounds. For each dimension, the span is max - min.
Definition Bounds.hpp:128
constexpr void expand(const t_vertex &expansion_vector)
Expands the given expansion scaler. such that max and min are both expanded outward from the center b...
Definition Bounds.hpp:218
constexpr t_vertex closestValue(const t_vertex &vertex) const
Closest value.
Definition Bounds.hpp:600
constexpr bool contains(const Bounds &bounds) const
Query if this object contains the given bounds.
Definition Bounds.hpp:365
constexpr Bounds(const RadialObject< t_dims, t_type, t_vertex > &radial)
Definition Bounds.hpp:115
constexpr void addToBounds(const Bounds &bounds)
Adds to the bounds such that the new bounds fully encompasses the argument.
Definition Bounds.hpp:495
constexpr bool contains(const Triangle< t_dims, t_type, t_vertex > &tri) const
Fully contains.
Definition Bounds.hpp:442
constexpr Bounds(const Vector< 2, t_vertex > &bounds)
Definition Bounds.hpp:94
constexpr Bounds(const LineSegment< t_dims, t_type, t_vertex > &line)
Definition Bounds.hpp:105
constexpr bool contains(const RadialObject< t_dims, t_type > &radial_object) const
Query if this object contains the given radial_object.
Definition Bounds.hpp:556
constexpr bool intersects(const RadialObject< t_dims, t_type > &circle) const
Query if this object contains the given circle.
Definition Bounds.hpp:723
constexpr Bounds(const t_vertex &center, t_type size)
Given the center, creates bounds of size where max and min are both equal to the vertex + size and.
Definition Bounds.hpp:84
constexpr Bounds(const Bounds &bounds_a, const Bounds &bounds_b)
Definition Bounds.hpp:98
constexpr void addToBounds(const Triangle< t_dims, t_type, t_vertex > &triangle)
Adds to the bounds such that the new bounds fully encompesses the argument.
Definition Bounds.hpp:536
constexpr Bounds< t_dims, t_type > scale(const t_type &scale, const Vector< t_dims, t_type > &center) const
Definition Bounds.hpp:249
constexpr void addToBounds(typename std::enable_if< tdims==1, const t_type & >::type scaler)
Definition Bounds.hpp:472
constexpr Bounds< t_dims, t_type > scale(const t_type &center_scale) const
Definition Bounds.hpp:256
constexpr Bounds< t_dims, t_type > scale(const Vector< t_dims, t_type > &scale, const Vector< t_dims, t_type > &center) const
Scales this geometry about a center point.
Definition Bounds.hpp:238
Bounds< t_dims, t_type > & operator-=(const t_type &center_scale)
Definition Bounds.hpp:787
constexpr bool contains(const t_type &value) const
Query if this object contains the given value.
Definition Bounds.hpp:302
constexpr Bounds< t_dims, t_type > scale(const Vector< t_dims, t_type > &center_scale) const
Definition Bounds.hpp:244
constexpr t_vertex center() const
Returns the center of the bounds.
Definition Bounds.hpp:143
constexpr bool contains(const LineSegment< t_dims, t_type, t_vertex > &line) const
Author: Tyler Parke.
Definition Bounds.hpp:403
constexpr Bounds(const t_type &min_scaler, const t_type &max_scaler)
Definition Bounds.hpp:90
constexpr bool doesIntersect(t_type distance_a, t_type distance_b, const t_vertex &origin, const Vector< t_dims, t_type > &ray, uint01 exclusion_axis) const
Checks for intersection of the ray from a given distance, excluding one axis.
Definition Bounds.hpp:648
constexpr Bounds(const Triangle< t_dims, t_type, t_vertex > &tri)
Definition Bounds.hpp:109
constexpr t_vertex furthestValue(const t_vertex &vertex) const
Furthest value.
Definition Bounds.hpp:622
constexpr void expand(const t_type &expansion_scaler)
Expands the given expansion scaler. such that max and min are both expanded outward from the center b...
Definition Bounds.hpp:200
constexpr bool intersects(const LineSegment< t_dims, t_type, t_other_vertex_type > &seg) const
Definition Bounds.hpp:694
void ensureValid()
Ensures that this is a valid bounds object. Values are swapped within the structure to ensure that MA...
Definition Bounds.hpp:780
constexpr Bounds(const t_vertex &vertex)
Given the vector, creates bounds of size 0 where max and min are both equal to the vertex.
Definition Bounds.hpp:67
constexpr Bounds(const Bounds &bounds, const t_vertex &vector)
Definition Bounds.hpp:102
constexpr bool contains(const t_vertex &vector) const
Query if this object contains the given vector.
Definition Bounds.hpp:329
constexpr bool intersects(const Bounds< t_dims, t_type, t_other_vertex_type > &bounds) const
Query if this object intersects the given pair.
Definition Bounds.hpp:682
constexpr t_vertex closestEdge(const t_vertex &vertex) const
Closest edge.
Definition Bounds.hpp:578
A line segment represented by two vertices, a start and end.
Definition Line.hpp:49
constexpr t_vertex ray() const
Definition Line.hpp:120
constexpr const t_vertex & vertex(uint01 index) const
Definition Line.hpp:153
A radial object.
Definition RadialObject.hpp:52
constexpr t_type radius() const
Definition RadialObject.hpp:123
constexpr const t_vertex & center() const
Definition RadialObject.hpp:152
Definition Vertex.hpp:317
A triangle is a polygon with three edges and three vertices. It is one of the basic shapes in geometr...
Definition Triangle.hpp:138
constexpr t_vertex & vertex(TriangleLocation triangle_node)
Vertices the given triangle node.
Definition Triangle.hpp:173
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
constexpr Vector< t_dims, t_new_type > as() const
Definition Vector.hpp:300
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:177
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:101
@ MIN
Definition BaseValues.hpp:191
@ MAX
Definition BaseValues.hpp:192
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:78
t_type distanceSquared(const Bounds< t_dims, t_type, t_vertex > &bounds, const Vector< t_dims, t_type > &vertex)
Definition Distance.hpp:45
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:379
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Changes an input with a negative sign, to a positive sign.
Definition AngleFunctions.h:655
@ B
Definition BaseValues.hpp:166
@ A
Definition BaseValues.hpp:164
@ C
Definition BaseValues.hpp:168
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
Definition BaseFunctions.hpp:56
static const t_type Invalid
Definition BaseValues.hpp:231
static const t_type Min
Definition BaseValues.hpp:232
static const t_type Max
Definition BaseValues.hpp:233