47 PolygonArray processed_polygons;
72 fltp08 val = (q[Y] - p[Y]) * (r[X] - q[X]) - (q[X] - p[X]) * (r[Y] - q[Y]);
77 return (val > 0) ? 1 : 2;
89 if (q[X] <=
getMax(p[X], r[X]) && q[X] >=
getMin(p[X], r[X]) &&
90 q[Y] <=
getMax(p[Y], r[Y]) && q[Y] >=
getMin(p[Y], r[Y]))
102 static inline bool intersects(
const Line& s1,
const Line& s2)
110 if (o1 != o2 && o3 != o4)
163 static uint04 mod(
int x,
int m)
178 static t_type CalcHandedness(
const t_vertex& v1,
const t_vertex& v2,
const t_vertex& v3)
180 t_vertex edge1 = v2 - v1;
181 t_vertex edge2 = v3 - v2;
183 return cross(edge1, edge2);
190 void flipPolygon(VertexArray& _verts)
204 return !_verts.hasClockwiseWinding();
221 t_type ls1Product =
cross(relativePos, ls1);
222 t_type ls2Product =
cross(relativePos, ls2);
224 if (ls1Product < t_type(0) && ls2Product > t_type(0))
245 if (isVertexInCone(ls1, ls2, origin, inputVerts.
vertex(i).template as<2, t_type>()))
258 static bool checkVisibility(
const VertexArray& vertices,
uint04 origin,
uint04 vertex_to_check)
260 lib_assert(origin != vertex_to_check,
"Bad visibility check");
261 Line ls(vertices.vertex(vertex_to_check).template as<2, t_type>(), vertices.vertex(origin).template as<2, t_type>());
263 const uint04 vertex_count = vertices.vertexCount();
264 for (
uint04 i = 0; i < vertex_count; ++i)
266 if (origin == i || ((i + 1) % vertex_count) == origin)
268 if (vertex_to_check == i || ((i + 1) % vertex_count) == vertex_to_check)
270 Line l = vertices.edge(i).template as<2, t_type>();
271 if (intersects(ls, l))
274 auto center = ls.
center();
275 if (!vertices.contains(center))
287 static uint04 IsReflexPoint(
uint04 index,
const VertexArray& vertices)
289 const t_vertex& prevVert = vertices.vertex(mod(
cast<sint04>(index - 1), vertices.vertexCount()));
290 const t_vertex& currVert = vertices.vertex(index);
291 const t_vertex& nextVert = vertices.vertex(mod(index + 1, vertices.vertexCount()));
292 return CalcHandedness(prevVert, currVert, nextVert) < 0.0f;
307 static uint04 getBestVertexToConnect(
const IntArray& indices,
const VertexArray& polygonVertices,
const Vector<2, t_type>& origin,
uint04 origin_index)
311 t_type min_distance = Constant<t_type>::Max;
312 uint04 min = Constant<uint04>::Invalid;
314 if (indices.size() == 1)
316 if (checkVisibility(polygonVertices, origin_index, indices[0]))
319 else if (indices.size() > 1)
322 for (
uint04 i = 0; i < indices.size(); ++i)
324 if (indices[i] < origin_index)
326 if (!IsReflexPoint(indices[i], polygonVertices))
328 if (checkVisibility(polygonVertices, origin_index, indices[i]))
334 t_type currDistance = distanceSquared(polygonVertices.
vertex(i).template as<2, t_type>(), origin);
335 if (currDistance * 1000 < min_distance)
337 min_distance = currDistance;
342 for (
uint04 i = 0; i < indices.size(); ++i)
344 if (indices[i] > origin_index && IsReflexPoint(indices[i], polygonVertices))
346 if (checkVisibility(polygonVertices, origin_index, indices[i]))
352 t_type currDistance = distanceSquared(polygonVertices.
vertex(i).template as<2, t_type>(), origin);
353 if (currDistance * 1000 < min_distance)
355 min_distance = currDistance;
368 if (indices.contains(i))
370 if (IsReflexPoint(i, polygonVertices) && checkVisibility(polygonVertices, origin_index, i))
376 if (i == origin_index || i == neighbor_a || i == neighbor_b)
378 if (i > origin_index && IsReflexPoint(i, polygonVertices))
380 t_type currDistance = distanceSquared(polygonVertices.
vertex(i).template as<2, t_type>(), origin);
381 if (currDistance < min_distance)
383 if (indices.contains(i))
385 if (checkVisibility(polygonVertices, origin_index, i))
387 min_distance = currDistance;
390 else if(currDistance * 1000 < min_distance)
392 min_distance = currDistance;
409 static uint04 findFirstReflexVertex(
const VertexArray& _vertices)
414 if (IsReflexPoint(i, _vertices))
418 return Constant<uint04>::Invalid;
426 flipPolygon(vertices);
430 typedef std::pair<int, Vertex<2, fltp08>> VertexIntPair;
439 : vertices{ _vertices }
441 if (vertices.vertexCount() >= 3)
470 for (
uint04 i = 0; i < vertices.vertexCount(); i++)
472 bounds.
addToBounds(vertices.vertex(i).template as<2, t_type>());
477 while (next_sub_polygons_to_process.size() > 0)
479 sub_polygons_to_process.clear();
480 for (
uint04 i = 0; i < next_sub_polygons_to_process.size(); i++)
482 if (next_sub_polygons_to_process[i].vertexCount() > 3)
483 sub_polygons_to_process.
add(next_sub_polygons_to_process[i]);
484 else if(next_sub_polygons_to_process[i].vertexCount() == 3)
485 processed_polygons.add(next_sub_polygons_to_process[i]);
487 next_sub_polygons_to_process.clear();
488 next_sub_polygons_to_process.setSize(2 * sub_polygons_to_process.size());
489 int size = sub_polygons_to_process.size();
490 _parallel_for(
int i = 0; i < size; i++)
492 std::unique_lock<std::mutex> lck(mtx, std::defer_lock);
493 const VertexArray& _vertices = sub_polygons_to_process[
cast<uint04>(i)];
495 uint04 reflexIndex = findFirstReflexVertex(_vertices);
498 lib_assert(_vertices.isConvex(),
"Should be convex");
508 IntArray vertsInCone = findVerticesInCone(prev_ray, next_ray, currVertPos, _vertices);
510 uint04 bestVert = getBestVertexToConnect(vertsInCone, _vertices, currVertPos, reflexIndex);
513 lib_assert(
IsValid(bestVert),
"Not sure why this is true");
516 next_sub_polygons_to_process[2 * i + 0].addVertices(_vertices.begin(), start + 1);
517 next_sub_polygons_to_process[2 * i + 1].addVertices(_vertices.begin() + start, (end + 1) - start);
518 next_sub_polygons_to_process[2 * i + 0].addVertices(_vertices.begin() + end, _vertices.
vertexCount() - end);
522 lib_assert(next_sub_polygons_to_process[2 * i + 0].vertexCount() > 2,
"Bad a size");
523 lib_assert(next_sub_polygons_to_process[2 * i + 1].vertexCount() > 2,
"Bad b size");
524 lib_assert(
checkIfRightHanded(next_sub_polygons_to_process[2 * i + 0]),
"Vertex A not right");
525 lib_assert(
checkIfRightHanded(next_sub_polygons_to_process[2 * i + 1]),
"Vertex B not right");
528 for (
uint04 i = 0; i < sub_polygons_to_process.size(); i++)
530 if (sub_polygons_to_process[i].vertexCount() > 3)
532 lib_assert(sub_polygons_to_process[i].isConvex(),
"Should be convex");
533 processed_polygons.add(sub_polygons_to_process[i]);
555 returnArr.addAll(processed_polygons);
565 if (index >= 0 && index < vertices.size())
566 return vertices[index];
567 lib_assert(
false,
"Bad vertex get");
568 return t_vertex(0.0f);
577 return vertices.vertexCount();