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>
56 template<
class t_type,
class t_vertex = Vertex<2, t_type>>
63 , m_cache_length(
Constant<t_type>::NaN)
64 , m_is_inside_cached(false)
65 , m_is_convex_cached(false)
67 , m_polygon_plane(
Vector<3, t_type>(0, 0, 1), 0)
70 : m_vertices(polygon.m_vertices)
71 , m_cache_bounds(polygon.m_cache_bounds)
72 , m_cache_length(polygon.m_cache_length)
73 , m_cache_constant(polygon.m_cache_constant)
74 , m_cache_multiple(polygon.m_cache_multiple)
75 , m_is_inside_cached(polygon.m_is_inside_cached)
76 , m_is_convex_cached(polygon.m_is_convex_cached)
77 , m_is_convex(polygon.m_is_convex)
78 , m_polygon_plane(polygon.m_polygon_plane)
81 : m_cache_bounds(polygon.m_cache_bounds)
82 , m_cache_length(polygon.m_cache_length)
83 , m_is_inside_cached(polygon.m_is_inside_cached)
84 , m_is_convex_cached(polygon.m_is_convex_cached)
85 , m_is_convex(polygon.m_is_convex)
86 , m_polygon_plane(polygon.m_polygon_plane)
88 std::swap(m_vertices, polygon.m_vertices);
89 std::swap(m_cache_multiple, polygon.m_cache_multiple);
90 std::swap(m_cache_constant, polygon.m_cache_constant);
95 , m_cache_length(
Constant<t_type>::NaN)
96 , m_is_inside_cached(false)
97 , m_is_convex_cached(false)
99 , m_polygon_plane(
Vector<3, t_type>(0, 0, 1), 0)
103 , m_cache_length(
Constant<t_type>::NaN)
104 , m_is_inside_cached(false)
105 , m_is_convex_cached(false)
107 , m_polygon_plane(
Plane<3,
fltp08>::CreateBestFitPlane(points))
110 for (
uint04 n = 0; n < points.size(); n++)
116 : m_vertices(allocated_size)
118 , m_cache_length(
Constant<t_type>::NaN)
119 , m_is_inside_cached(false)
120 , m_is_convex_cached(false)
122 , m_polygon_plane(
Vector<3, t_type>(0, 0, 1), 0)
138 if (
isNaN(m_cache_bounds))
141 for (
uint04 i = 0; i < m_vertices.
size(); i++)
143 m_cache_bounds.addToBounds(m_vertices[i]);
146 return m_cache_bounds;
167 return m_vertices[index];
221 return m_vertices.
size();
238 return m_vertices.
size();
242 if (!m_is_convex_cached)
255 t_type last_cross_prod = 0;
257 for (
uint04 i = 0; i < vert_count; i++)
261 const t_type cross_prod = d1[
X] * d2[
Y] - d1[
Y] * d2[
X];
270 last_cross_prod = cross_prod;
274 m_is_convex_cached =
true;
356 if (m_vertices[index] != vector)
358 m_vertices[index] = vector;
408 for (
uint04 i = 1; i < iMax; ++i)
409 std::swap(m_vertices[i], m_vertices[
vertexCount() - i]);
458 template<
class t_new_type,
class t_new_vertex_type = Vertex<2, t_new_type>>
483 if(
isNaN(m_cache_length))
493 m_cache_length +=
edge(i).template length<t_type>();
497 return m_cache_length;
519 cacheBoundaryCheck();
521 bool odd_vertices =
false;
524 if ((m_vertices[current][
Y] < vector[
Y] && m_vertices[last][
Y] >= vector[
Y])
525 || (m_vertices[last][
Y] < vector[
Y] && m_vertices[current][
Y] >= vector[
Y]))
527 odd_vertices ^= ((vector[
Y] * m_cache_multiple[current] + m_cache_constant[current]) < vector[
X]);
534 template<
class t_precision>
537 if (m_vertices.
size() <= 2)
539 if (!
bounds().intersects(line))
548 if (current_edge.template intersects<t_precision>(line))
568 template<
class t_precision>
573 if (!
bounds().intersects(o_bounds))
575 const t_vertex v1(o_bounds[
MIN][
X], o_bounds[
MIN][
Y]);
576 const t_vertex v2(o_bounds[
MAX][
X], o_bounds[
MIN][
Y]);
577 const t_vertex v3(o_bounds[
MAX][
X], o_bounds[
MAX][
Y]);
578 const t_vertex v4(o_bounds[
MIN][
X], o_bounds[
MAX][
Y]);
580 const bool is_inside =
contains(v1);
604 return m_vertices.
begin();
608 return m_vertices.
begin();
613 return m_vertices.
begin(index);
617 return m_vertices.
begin(index);
622 return m_vertices.
end();
624 decltype(
auto)
end()
const
626 return m_vertices.
end();
644 template<
class t_precision>
648 if(m_vertices.
size() <= 2)
666 if(current_edge.template intersects<t_precision>(tri.
edge(
A,
B)))
return mixed;
667 if(current_edge.template intersects<t_precision>(tri.
edge(
B,
C)))
return mixed;
668 if(current_edge.template intersects<t_precision>(tri.
edge(
C,
A)))
return mixed;
673 template<
class t_precision,
class t_other_vertex>
703 line = intersection(
bounds, line);
707 polygon.
add(line.vertex(
A));
708 polygon.
add(line.vertex(
B));
714 line = intersection(
bounds, line);
718 polygon.
add(line.vertex(
A));
719 polygon.
add(line.vertex(
B));
729 return m_polygon_plane;
733 m_polygon_plane =
plane;
741 sum += (side[
B][
X] - side[
A][
X]) * (side[
B][
Y] + side[
A][
Y]);
762 return m_vertices == polygon.m_vertices && m_polygon_plane == polygon.m_polygon_plane;
766 return m_vertices != polygon.m_vertices || m_polygon_plane != polygon.m_polygon_plane;
794 t_type min_tol2 = min_tol * min_tol;
795 t_type max_tol2 = max_tol * max_tol;
798 if (count < 3 || min_tol2 == 0 || max_tol2 == 0)
803 bool rayDefined =
false;
811 for (
uint04 j = 1; j < count; ++j)
818 if (dist_squared < min_tol2)
826 if (dist_squared < max_tol2)
831 t_type cv =
dot(v, v);
832 t_type cw =
dot(w, v);
843 t_type fraction = cv == 0 ? 0 : cw /cv;
860 m_vertices = poly.m_vertices;
861 m_cache_bounds = poly.m_cache_bounds;
862 m_cache_length = poly.m_cache_length;
863 m_cache_constant = poly.m_cache_constant;
864 m_cache_multiple = poly.m_cache_multiple;
865 m_is_inside_cached = poly.m_is_inside_cached;
870 std::swap(m_vertices, poly.m_vertices);
871 m_cache_bounds = poly.m_cache_bounds;
872 m_cache_length = poly.m_cache_length;
873 m_cache_constant = poly.m_cache_constant;
874 m_cache_multiple = poly.m_cache_multiple;
875 m_is_inside_cached = poly.m_is_inside_cached;
882 auto seg_to_check =
edge(i);
885 if (n == i || (n ==
edgeCount() - 1 && i == 0))
888 if (!
isNaN(seg_to_check.template intersection<fltp08>(seg, 0.000000001)))
909 inline void invalidateCache()
const
912 m_is_inside_cached =
false;
913 m_is_convex_cached =
false;
914 m_cache_constant.
clear();
915 m_cache_multiple.
clear();
932 void updateVertices(
const Buffer<t_vertex>&
vertices)
948 void cacheBoundaryCheck()
const
950 if(m_is_inside_cached)
955 for(
uint04 ii = 0; ii < m_vertices.
size(); ++ii)
957 if(m_vertices[jj][
Y] == m_vertices[ii][
Y])
960 m_cache_multiple[ii] = 0;
964 m_cache_constant[ii] = m_vertices[ii][
X]
969 m_cache_multiple[ii] =
cast<fltp08>(m_vertices[jj][
X] - m_vertices[ii][
X])
974 m_is_inside_cached =
true;
979 Buffer<t_vertex> m_vertices;
989 mutable Bounds<2, t_type, t_vertex> m_cache_bounds;
999 mutable t_type m_cache_length;
1009 mutable Buffer<fltp08> m_cache_constant;
1019 mutable Buffer<fltp08> m_cache_multiple;
1029 mutable bool m_is_inside_cached;
1030 mutable bool m_is_convex_cached;
1031 mutable bool m_is_convex;
1032 Plane<3, t_type> m_polygon_plane;
1035 template<
class t_type,
class t_vertex, u
int01 t_row_dims, u
int01 t_col_dims>
1046 template<
class t_type,
class t_vertex>
1047 static bool isNaN(
const Polygon<t_type, t_vertex>& poly)
1049 Polygon<t_type, t_vertex> polygon(poly.vertexCount());
1050 for (
uint04 i = 0; i < poly.vertexCount(); i++)
1052 if(isNaN(poly.vertex(i)))
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:57
constexpr bool contains(const t_type &value) const
Query if this object contains the given value.
Definition Bounds.hpp:329
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
void add(t_type &&object)
Definition Buffer.hpp:199
void insert(t_index_type offset, const Buffer &buffer)
Definition Buffer.hpp:332
constexpr t_index_type size() const
Definition Buffer.hpp:1461
void addAll(const Buffer< t_type, t_other_index_type, t_other_memory_allocator, t_other_memory_manager > &buffer)
Definition Buffer.hpp:248
decltype(auto) end()
Definition Buffer.hpp:746
void setSize(t_index_type new_size)
Definition Buffer.hpp:1413
void removeIndex(t_index_type location)
Definition Buffer.hpp:1037
decltype(auto) begin()
Definition Buffer.hpp:504
void clear()
Definition Buffer.hpp:572
A line segment represented by two vertices, a start and end.
Definition Line.hpp:55
constexpr const t_vertex & vertex(uint01 index) const
Definition Line.hpp:171
Definition Matrix.hpp:173
Matrix< t_type > projectionMatrix(const Vector< 3, t_type > &up) const
Definition Plane.hpp:134
An N-sided polygon.
Definition Polygon.hpp:58
Polygon & operator=(Polygon &&poly)
Definition Polygon.hpp:868
LineSegment< 2, t_type, t_vertex > edge(uint04 index) const
Definition Polygon.hpp:185
IntersectionTypes contains(const Triangle< 2, t_type, t_vertex > &tri) const
Definition Polygon.hpp:645
void simplify()
Definition Polygon.hpp:375
Polygon< t_new_type, t_new_vertex_type > as() const
Definition Polygon.hpp:459
void flip()
Definition Polygon.hpp:401
Polygon opheimSimplification(t_type min_tol, t_type max_tol) const
Definition Polygon.hpp:790
Plane< 3, t_type > plane() const
Definition Polygon.hpp:727
Polygon clip(const Bounds< t_vertex::NumberOfDimensions(), t_type > &bounds) const
Definition Polygon.hpp:693
void replace(uint04 index, const t_vertex &vector)
Definition Polygon.hpp:354
bool validate() const
Definition Polygon.hpp:878
void addVertices(const t_vertex *vertices, uint04 size)
Definition Polygon.hpp:295
bool contains(const Vertex< 2, t_type > &vector) const
Definition Polygon.hpp:515
decltype(auto) begin(uint04 index)
Definition Polygon.hpp:615
uint04 vertexCount() const
Definition Polygon.hpp:219
Polygon(const Buffer< Vertex< 3, fltp08 > > &points)
Definition Polygon.hpp:101
void insert(uint04 index, const t_vertex &vertex)
Definition Polygon.hpp:334
t_type perimeter() const
Definition Polygon.hpp:481
Polygon(const Polygon &polygon)
Definition Polygon.hpp:69
IntersectionTypes contains(const Bounds< 2, t_type, t_vertex > &o_bounds) const
Definition Polygon.hpp:569
bool hasClockwiseWinding() const
Definition Polygon.hpp:735
bool isEquivalent(const Polygon &polygon, fltp08 epsilon)
Definition Polygon.hpp:768
void setVertices(const Buffer< t_vertex > &vertices)
Definition Polygon.hpp:291
void remove(uint04 index)
Definition Polygon.hpp:425
decltype(auto) end()
Definition Polygon.hpp:620
Polygon & operator=(const Polygon &poly)
Definition Polygon.hpp:858
bool operator==(const Polygon &polygon) const
Definition Polygon.hpp:760
IntersectionTypes contains(const LineSegment< 2, t_type, t_vertex > &line) const
Definition Polygon.hpp:535
Bounds< 2, t_type, t_vertex > bounds() const
Definition Polygon.hpp:136
decltype(auto) begin()
Definition Polygon.hpp:602
void setPlane(const Plane< 3, t_type > &plane)
Definition Polygon.hpp:731
decltype(auto) begin(uint04 index) const
Definition Polygon.hpp:611
void add(const t_vertex &vertex)
Definition Polygon.hpp:314
bool isConvex() const
Definition Polygon.hpp:240
Polygon(uint04 allocated_size)
Definition Polygon.hpp:115
void clear()
Definition Polygon.hpp:441
decltype(auto) begin() const
Definition Polygon.hpp:606
const t_vertex & vertex(uint04 index) const
Definition Polygon.hpp:165
Polygon(Polygon &&polygon) noexcept
Definition Polygon.hpp:80
Polygon()
Definition Polygon.hpp:60
const Buffer< t_vertex > & vertices() const
Definition Polygon.hpp:202
Polygon(Buffer< t_vertex > &vertices)
Definition Polygon.hpp:92
uint04 edgeCount() const
Definition Polygon.hpp:236
decltype(auto) end() const
Definition Polygon.hpp:624
IntersectionTypes contains(const Polygon< t_type, t_other_vertex > &poly) const
Definition Polygon.hpp:674
bool operator!=(const Polygon &polygon) const
Definition Polygon.hpp:764
Definition Triangle.hpp:143
constexpr bool contains(const Vector< t_dims - 1, t_type > &p) const
Definition Triangle.hpp:481
constexpr t_vertex & vertex(TriangleLocation triangle_node)
Vertices the given triangle node.
Definition Triangle.hpp:180
constexpr LineSegment< t_dims, t_type, t_vertex > edge(uint01 triangle_node_a, uint01 triangle_node_b) const
Definition Triangle.hpp:276
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
A vertex.
Definition Vertex.hpp:54
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)
Definition AngleFunctions.h:403
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1096
@ MIN
Definition BaseValues.hpp:226
@ MAX
Definition BaseValues.hpp:227
t_type distanceSquared(const Bounds< t_dims, t_type, t_vertex > &bounds, const Vector< t_dims, t_type > &vertex)
Definition Distance.hpp:42
IntersectionTypes
Used for classifying shape intersections.
Definition BaseValues.hpp:241
@ inside
Definition BaseValues.hpp:243
@ mixed
Definition BaseValues.hpp:244
@ outside
Definition BaseValues.hpp:242
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 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
@ Y
Definition BaseValues.hpp:202
@ X
Definition BaseValues.hpp:200
@ C
Definition BaseValues.hpp:205
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:181
Definition BaseValues.hpp:272