MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ compute_convex_hull_2d()

std::vector< glm::vec3 > MayaFlux::Kinesis::compute_convex_hull_2d ( const std::vector< glm::vec3 > &  vertices,
const glm::vec3 &  projection_normal = glm::vec3(0, 0, 1) 
)

Compute convex hull of vertex set (2D projection)

Parameters
verticesInput vertex set
projection_normalNormal of projection plane (default: Z-axis)
Returns
Vertices forming convex hull boundary (closed path)

Uses Graham scan on projected coordinates.

Definition at line 469 of file GeometryPrimitives.cpp.

472{
473 if (vertices.size() < 3) {
474 return vertices;
475 }
476
477 glm::vec3 n = glm::normalize(projection_normal);
478 glm::vec3 u;
479
480 if (std::abs(n.z) < 0.9F) {
481 u = glm::normalize(glm::cross(n, glm::vec3(0, 0, 1)));
482 } else {
483 u = glm::normalize(glm::cross(n, glm::vec3(1, 0, 0)));
484 }
485
486 glm::vec3 v = glm::cross(n, u);
487
488 struct Point2D {
489 glm::vec2 pos;
490 size_t index;
491 };
492
493 std::vector<Point2D> points;
494 points.reserve(vertices.size());
495
496 for (size_t i = 0; i < vertices.size(); ++i) {
497 glm::vec3 offset = vertices[i];
498 float x = glm::dot(offset, u);
499 float y = glm::dot(offset, v);
500 points.push_back({ glm::vec2(x, y), i });
501 }
502
503 auto pivot_it = std::ranges::min_element(points,
504 [](const Point2D& a, const Point2D& b) {
505 if (std::abs(a.pos.y - b.pos.y) < 1e-6F) {
506 return a.pos.x < b.pos.x;
507 }
508 return a.pos.y < b.pos.y;
509 });
510
511 std::swap(points[0], *pivot_it);
512 Point2D pivot = points[0];
513
514 std::sort(points.begin() + 1, points.end(),
515 [&pivot](const Point2D& a, const Point2D& b) {
516 glm::vec2 va = a.pos - pivot.pos;
517 glm::vec2 vb = b.pos - pivot.pos;
518
519 float cross = va.x * vb.y - va.y * vb.x;
520 if (std::abs(cross) < 1e-6F) {
521 return glm::length(va) < glm::length(vb);
522 }
523 return cross > 0.0F;
524 });
525
526 // Graham scan
527 std::vector<size_t> hull;
528 hull.push_back(points[0].index);
529 hull.push_back(points[1].index);
530
531 auto ccw = [](const glm::vec2& a, const glm::vec2& b, const glm::vec2& c) {
532 return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) > 0.0F;
533 };
534
535 for (size_t i = 2; i < points.size(); ++i) {
536 while (hull.size() >= 2) {
537 size_t top = hull.back();
538 size_t second = hull[hull.size() - 2];
539
540 glm::vec2 p1 = points[second].pos;
541 glm::vec2 p2 = points[top].pos;
542 glm::vec2 p3 = points[i].pos;
543
544 if (ccw(p1, p2, p3)) {
545 break;
546 }
547 hull.pop_back();
548 }
549 hull.push_back(points[i].index);
550 }
551
552 std::vector<glm::vec3> result;
553 result.reserve(hull.size() + 1);
554
555 for (size_t idx : hull) {
556 result.push_back(vertices[idx]);
557 }
558
559 result.push_back(vertices[hull[0]]);
560
561 return result;
562}
size_t a
size_t b

References a, and b.