33#include <NDEVR/BaseValues.h>
34#include <NDEVR/MatrixFunctions.h>
35#include <NDEVR/Buffer.h>
36#include <NDEVR/Vertex.h>
37#include <NDEVR/Bounds.h>
38#include <NDEVR/LineSegment.h>
39#include <NDEVR/Plane.h>
51 template<
class t_type,
class t_vertex = Vertex<2, t_type>>
59 , m_is_inside_cached(false)
60 , m_is_convex_cached(false)
62 , m_polygon_plane(
Vector<3, t_type>(0, 0, 1), 0)
65 : m_vertices(polygon.m_vertices)
66 , m_cache_bounds(polygon.m_cache_bounds)
67 , m_cache_length(polygon.m_cache_length)
68 , m_cache_constant(polygon.m_cache_constant)
69 , m_cache_multiple(polygon.m_cache_multiple)
70 , m_is_inside_cached(polygon.m_is_inside_cached)
71 , m_is_convex_cached(polygon.m_is_convex_cached)
72 , m_is_convex(polygon.m_is_convex)
73 , m_polygon_plane(polygon.m_polygon_plane)
76 : m_cache_bounds(polygon.m_cache_bounds)
77 , m_cache_length(polygon.m_cache_length)
78 , m_is_inside_cached(polygon.m_is_inside_cached)
79 , m_is_convex_cached(polygon.m_is_convex_cached)
80 , m_is_convex(polygon.m_is_convex)
81 , m_polygon_plane(polygon.m_polygon_plane)
83 std::swap(m_vertices, polygon.m_vertices);
84 std::swap(m_cache_multiple, polygon.m_cache_multiple);
85 std::swap(m_cache_constant, polygon.m_cache_constant);
91 , m_is_inside_cached(false)
92 , m_is_convex_cached(false)
94 , m_polygon_plane(
Vector<3, t_type>(0, 0, 1), 0)
99 , m_is_inside_cached(false)
100 , m_is_convex_cached(false)
102 , m_polygon_plane(
Plane<3,
fltp08>::CreateBestFitPlane(points))
105 for (
uint04 n = 0; n < points.size(); n++)
111 : m_vertices(allocated_size)
114 , m_is_inside_cached(false)
115 , m_is_convex_cached(false)
117 , m_polygon_plane(
Vector<3, t_type>(0, 0, 1), 0)
134 for (
uint04 i = 0; i < m_vertices.
size(); i++)
136 m_cache_bounds.addToBounds(m_vertices[i]);
139 return m_cache_bounds;
158 return m_vertices[index];
206 return m_vertices.
size();
221 return m_vertices.
size();
225 if (!m_is_convex_cached)
238 t_type last_cross_prod = 0;
240 for (
uint04 i = 0; i < vert_count; i++)
244 const t_type cross_prod = d1[
X] * d2[
Y] - d1[
Y] * d2[
X];
253 last_cross_prod = cross_prod;
257 m_is_convex_cached =
true;
331 if (m_vertices[index] != vector)
333 m_vertices[index] = vector;
381 for (
uint04 i = 1; i < iMax; ++i)
382 std::swap(m_vertices[i], m_vertices[
vertexCount() - i]);
425 template<
class t_new_type,
class t_new_vertex_type = Vertex<2, t_new_type>>
458 m_cache_length +=
edge(i).template length<t_type>();
462 return m_cache_length;
482 cacheBoundaryCheck();
484 bool odd_vertices =
false;
487 if ((m_vertices[current][
Y] < vector[
Y] && m_vertices[last][
Y] >= vector[
Y])
488 || (m_vertices[last][
Y] < vector[
Y] && m_vertices[current][
Y] >= vector[
Y]))
490 odd_vertices ^= ((vector[
Y] * m_cache_multiple[current] + m_cache_constant[current]) < vector[
X]);
497 template<
class t_precision>
500 if (m_vertices.
size() <= 2)
502 if (!
bounds().intersects(line))
511 if (current_edge.template intersects<t_precision>(line))
529 template<
class t_precision>
534 if (!
bounds().intersects(o_bounds))
536 const t_vertex v1(o_bounds[
MIN][
X], o_bounds[
MIN][
Y]);
537 const t_vertex v2(o_bounds[
MAX][
X], o_bounds[
MIN][
Y]);
538 const t_vertex v3(o_bounds[
MAX][
X], o_bounds[
MAX][
Y]);
539 const t_vertex v4(o_bounds[
MIN][
X], o_bounds[
MAX][
Y]);
541 const bool is_inside =
contains(v1);
565 return m_vertices.
begin();
569 return m_vertices.
begin();
574 return m_vertices.
begin(index);
578 return m_vertices.
begin(index);
583 return m_vertices.
end();
585 decltype(
auto)
end()
const
587 return m_vertices.
end();
603 template<
class t_precision>
607 if(m_vertices.
size() <= 2)
625 if(current_edge.template intersects<t_precision>(tri.
edge(
A,
B)))
return mixed;
626 if(current_edge.template intersects<t_precision>(tri.
edge(
B,
C)))
return mixed;
627 if(current_edge.template intersects<t_precision>(tri.
edge(
C,
A)))
return mixed;
632 template<
class t_precision,
class t_other_vertex>
662 line = intersection(
bounds, line);
666 polygon.
add(line.vertex(
A));
667 polygon.
add(line.vertex(
B));
673 line = intersection(
bounds, line);
677 polygon.
add(line.vertex(
A));
678 polygon.
add(line.vertex(
B));
688 return m_polygon_plane;
692 m_polygon_plane =
plane;
700 sum += (side[
B][
X] - side[
A][
X]) * (side[
B][
Y] + side[
A][
Y]);
719 return m_vertices == polygon.m_vertices && m_polygon_plane == polygon.m_polygon_plane;
723 return m_vertices != polygon.m_vertices || m_polygon_plane != polygon.m_polygon_plane;
751 t_type min_tol2 = min_tol * min_tol;
752 t_type max_tol2 = max_tol * max_tol;
755 if (count < 3 || min_tol2 == 0 || max_tol2 == 0)
760 bool rayDefined =
false;
768 for (
uint04 j = 1; j < count; ++j)
775 if (dist_squared < min_tol2)
783 if (dist_squared < max_tol2)
788 t_type cv =
dot(v, v);
789 t_type cw =
dot(w, v);
800 t_type fraction = cv == 0 ? 0 : cw /cv;
817 m_vertices = poly.m_vertices;
818 m_cache_bounds = poly.m_cache_bounds;
819 m_cache_length = poly.m_cache_length;
820 m_cache_constant = poly.m_cache_constant;
821 m_cache_multiple = poly.m_cache_multiple;
822 m_is_inside_cached = poly.m_is_inside_cached;
827 std::swap(m_vertices, poly.m_vertices);
828 m_cache_bounds = poly.m_cache_bounds;
829 m_cache_length = poly.m_cache_length;
830 m_cache_constant = poly.m_cache_constant;
831 m_cache_multiple = poly.m_cache_multiple;
832 m_is_inside_cached = poly.m_is_inside_cached;
839 auto seg_to_check =
edge(i);
842 if (n == i || (n ==
edgeCount() - 1 && i == 0))
845 if (!
IsInvalid(seg_to_check.template intersection<fltp08>(seg, 0.000000001)))
864 inline void invalidateCache()
const
867 m_is_inside_cached =
false;
868 m_is_convex_cached =
false;
869 m_cache_constant.
clear();
870 m_cache_multiple.
clear();
885 void updateVertices(
const Buffer<t_vertex>&
vertices)
899 void cacheBoundaryCheck()
const
901 if(m_is_inside_cached)
906 for(
uint04 ii = 0; ii < m_vertices.
size(); ++ii)
908 if(m_vertices[jj][
Y] == m_vertices[ii][
Y])
911 m_cache_multiple[ii] = 0;
915 m_cache_constant[ii] = m_vertices[ii][
X]
920 m_cache_multiple[ii] =
cast<fltp08>(m_vertices[jj][
X] - m_vertices[ii][
X])
925 m_is_inside_cached =
true;
930 Buffer<t_vertex> m_vertices;
940 mutable Bounds<2, t_type, t_vertex> m_cache_bounds;
950 mutable t_type m_cache_length;
960 mutable Buffer<fltp08> m_cache_constant;
970 mutable Buffer<fltp08> m_cache_multiple;
980 mutable bool m_is_inside_cached;
981 mutable bool m_is_convex_cached;
982 mutable bool m_is_convex;
983 Plane<3, t_type> m_polygon_plane;
986 template<
class t_type,
class t_vertex, u
int01 t_row_dims, u
int01 t_col_dims>
997 template<
class t_type,
class t_vertex>
998 static bool IsInvalid(
const Polygon<t_type, t_vertex>& poly)
1000 Polygon<t_type, t_vertex> polygon(poly.vertexCount());
1001 for (
uint04 i = 0; i < poly.vertexCount(); i++)
1003 if(IsInvalid(poly.vertex(i)))
#define lib_assert(expression, message)
Definition LibAssert.h:61
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:52
constexpr bool contains(const t_type &value) const
Query if this object contains the given value. t_allow_bounds - whether or not to allow boundary case...
Definition Bounds.hpp:237
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
void add(t_type &&object)
Adds object to the end of the buffer.
Definition Buffer.hpp:186
constexpr t_index_type size() const
Definition Buffer.hpp:823
void addAll(const Buffer< t_type, t_other_index_type, t_other_memory_allocator, t_other_memory_manager > &buffer)
Definition Buffer.hpp:243
decltype(auto) end()
Definition Buffer.hpp:507
void setSize(t_index_type new_size)
Definition Buffer.hpp:803
void insert(t_index_type location, const t_type &object)
Adds an object to a specific location.
Definition Buffer.hpp:227
void removeIndex(t_index_type location)
Definition Buffer.hpp:606
decltype(auto) begin()
Definition Buffer.hpp:402
void clear()
Definition Buffer.hpp:422
A line segment represented by two vertices, a start and end.
Definition Line.hpp:49
constexpr const t_vertex & vertex(uint01 index) const
Definition Line.hpp:152
Definition Matrix.hpp:176
Logic for a given plane or N-dimensions. Planes are coordinate systems of one less dimension than the...
Definition Geometry.h:41
Matrix< t_type > projectionMatrix(const Vector< 3, t_type > &up) const
Definition Plane.hpp:141
An N-sided polygon.
Definition Polygon.hpp:53
Polygon & operator=(Polygon &&poly)
Definition Polygon.hpp:825
LineSegment< 2, t_type, t_vertex > edge(uint04 index) const
Definition Polygon.hpp:174
IntersectionTypes contains(const Triangle< 2, t_type, t_vertex > &tri) const
Definition Polygon.hpp:604
void simplify()
Definition Polygon.hpp:348
Polygon< t_new_type, t_new_vertex_type > as() const
Definition Polygon.hpp:426
void flip()
Definition Polygon.hpp:374
Polygon opheimSimplification(t_type min_tol, t_type max_tol) const
Definition Polygon.hpp:747
Plane< 3, t_type > plane() const
Definition Polygon.hpp:686
Polygon clip(const Bounds< t_vertex::NumberOfDimensions(), t_type > &bounds) const
Definition Polygon.hpp:652
void replace(uint04 index, const t_vertex &vector)
Definition Polygon.hpp:329
bool validate() const
Definition Polygon.hpp:835
void addVertices(const t_vertex *vertices, uint04 size)
Definition Polygon.hpp:276
bool contains(const Vertex< 2, t_type > &vector) const
Definition Polygon.hpp:478
decltype(auto) begin(uint04 index)
Definition Polygon.hpp:576
uint04 vertexCount() const
Definition Polygon.hpp:204
Polygon(const Buffer< Vertex< 3, fltp08 > > &points)
Definition Polygon.hpp:96
void insert(uint04 index, const t_vertex &vertex)
Definition Polygon.hpp:311
t_type perimeter() const
Definition Polygon.hpp:446
Polygon(const Polygon &polygon)
Definition Polygon.hpp:64
IntersectionTypes contains(const Bounds< 2, t_type, t_vertex > &o_bounds) const
Definition Polygon.hpp:530
bool hasClockwiseWinding() const
Definition Polygon.hpp:694
bool isEquivalent(const Polygon &polygon, fltp08 epsilon)
Definition Polygon.hpp:725
void setVertices(const Buffer< t_vertex > &vertices)
Definition Polygon.hpp:272
void remove(uint04 index)
Definition Polygon.hpp:396
decltype(auto) end()
Definition Polygon.hpp:581
Polygon & operator=(const Polygon &poly)
Definition Polygon.hpp:815
bool operator==(const Polygon &polygon) const
Definition Polygon.hpp:717
IntersectionTypes contains(const LineSegment< 2, t_type, t_vertex > &line) const
Definition Polygon.hpp:498
Bounds< 2, t_type, t_vertex > bounds() const
Definition Polygon.hpp:129
decltype(auto) begin()
Definition Polygon.hpp:563
void setPlane(const Plane< 3, t_type > &plane)
Definition Polygon.hpp:690
decltype(auto) begin(uint04 index) const
Definition Polygon.hpp:572
void add(const t_vertex &vertex)
Definition Polygon.hpp:293
bool isConvex() const
Definition Polygon.hpp:223
Polygon(uint04 allocated_size)
Definition Polygon.hpp:110
void clear()
Definition Polygon.hpp:410
decltype(auto) begin() const
Definition Polygon.hpp:567
const t_vertex & vertex(uint04 index) const
Definition Polygon.hpp:156
Polygon(Polygon &&polygon) noexcept
Definition Polygon.hpp:75
Polygon()
Definition Polygon.hpp:55
const Buffer< t_vertex > & vertices() const
Definition Polygon.hpp:189
Polygon(Buffer< t_vertex > &vertices)
Definition Polygon.hpp:87
uint04 edgeCount() const
Definition Polygon.hpp:219
decltype(auto) end() const
Definition Polygon.hpp:585
IntersectionTypes contains(const Polygon< t_type, t_other_vertex > &poly) const
Definition Polygon.hpp:633
bool operator!=(const Polygon &polygon) const
Definition Polygon.hpp:721
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 bool contains(const Vector< t_dims - 1, t_type > &p) const
Definition Triangle.hpp:443
constexpr t_vertex & vertex(TriangleLocation triangle_node)
Vertices the given triangle node.
Definition Triangle.hpp:168
constexpr LineSegment< t_dims, t_type, t_vertex > edge(uint01 triangle_node_a, uint01 triangle_node_b) const
Definition Triangle.hpp:256
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
A vertex or point. A specific type of Vector used primarily for spacial location information.
Definition Vertex.hpp:48
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:170
constexpr std::enable_if< IsVecType< t_vector_type, Angle< fltp08 > >::value, t_vector_type >::type operator*(const t_vector_type &angle, const t_type &mult)
Multiplication operator for a Vector of Angles.
Definition AngleFunctions.h:326
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1030
@ MIN
Definition BaseValues.hpp:196
@ MAX
Definition BaseValues.hpp:197
t_type distanceSquared(const Bounds< t_dims, t_type, t_vertex > &bounds, const Vector< t_dims, t_type > &vertex)
Definition Distance.hpp:46
IntersectionTypes
Used for classifying shape intersections.
Definition BaseValues.hpp:210
@ inside
Definition BaseValues.hpp:212
@ mixed
Definition BaseValues.hpp:213
@ outside
Definition BaseValues.hpp:211
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
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:645
@ B
Definition BaseValues.hpp:170
@ A
Definition BaseValues.hpp:168
@ Y
Definition BaseValues.hpp:169
@ X
Definition BaseValues.hpp:167
@ C
Definition BaseValues.hpp:172
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233