Commit 9c3c012a authored by Deepak Rangarajan's avatar Deepak Rangarajan Committed by Mark Meredith
Browse files

Improve polyhedron perf

parent 7ff3235b
Loading
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -54,7 +54,8 @@ struct Polyhedron {
private:
  std::vector<std::tuple<double, double, double>> m_points;
  std::vector<std::vector<unsigned int>> m_faces;
  cgal_helper::Polyhedron m_cgal_polyhedron;
  std::shared_ptr<cgal_helper::Polyhedron> m_cgal_polyhedron;
  std::shared_ptr<cgal_helper::AABBTree> m_cgal_aabb_tree;

public:
  std::optional<std::string> name;
@@ -65,11 +66,16 @@ public:
      m_faces.push_back(std::vector<unsigned int>(f.begin(), f.end()));
    }
    m_cgal_polyhedron = cgal_helper::create_polyhedron(m_points, m_faces);
    m_cgal_aabb_tree = cgal_helper::create_aabb_tree(m_cgal_polyhedron);
  }

  const cgal_helper::Polyhedron &cgal_polyhedron() const {
  const std::shared_ptr<cgal_helper::Polyhedron> &cgal_polyhedron() const {
    return m_cgal_polyhedron;
  }

  const std::shared_ptr<cgal_helper::AABBTree> &cgal_aabb_tree() const {
    return m_cgal_aabb_tree;
  }
};

struct Polygon {
+18 −17
Original line number Diff line number Diff line
@@ -6,7 +6,6 @@
#include <CGAL/AABB_tree.h>
#include <CGAL/Polygon_mesh_processing/triangulate_faces.h>
#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <CGAL/Side_of_triangle_mesh.h>
#include <CGAL/algorithm.h>
#include <CGAL/boost/graph/graph_traits_Polyhedron_3.h>

@@ -15,10 +14,6 @@ namespace {
typedef cgal_helper::Polyhedron::HalfedgeDS HalfedgeDS;
typedef typename HalfedgeDS::Vertex Vertex;
typedef typename Vertex::Point Point;
typedef CGAL::AABB_face_graph_triangle_primitive<cgal_helper::Polyhedron>
    Primitive;
typedef CGAL::AABB_traits<cgal_helper::CK, Primitive> Traits;
typedef CGAL::AABB_tree<Traits> Tree;
typedef CGAL::Side_of_triangle_mesh<cgal_helper::Polyhedron, cgal_helper::CK>
    Point_inside;

@@ -66,30 +61,36 @@ public:

namespace cgal_helper {

Polyhedron
std::shared_ptr<Polyhedron>
create_polyhedron(const std::vector<std::tuple<double, double, double>> &points,
                  const std::vector<std::vector<unsigned int>> &faces) {
  Polyhedron p;
  auto p = std::make_shared<Polyhedron>();

  // Build incrementally
  PolyhedronBuilder<HalfedgeDS> bp(points, faces);
  p.delegate(bp);
  CGAL_assertion(p.is_valid());
  p->delegate(bp);
  CGAL_assertion(p->is_valid());

  // Triangulate faces - needed for levelset
  CGAL::Polygon_mesh_processing::triangulate_faces(p);
  CGAL_assertion(p.is_valid());
  CGAL::Polygon_mesh_processing::triangulate_faces(*p);
  CGAL_assertion(p->is_valid());

  return p;
}

bool inside(const Polyhedron &polyhedron, double xx, double yy, double zz) {
  cgal_helper::CK::Point_3 pt(xx, yy, zz);
std::shared_ptr<AABBTree>
create_aabb_tree(const std::shared_ptr<Polyhedron> &polyhedron) {
  // Construct AABB tree with a KdTree
  Tree tree(faces(polyhedron).first, faces(polyhedron).second, polyhedron);
  tree.accelerate_distance_queries();
  // Initialize the point-in-polyhedron tester
  Point_inside inside_tester(tree);
  auto tree = std::make_shared<AABBTree>(
      faces(*polyhedron).first, faces(*polyhedron).second, *polyhedron);
  tree->accelerate_distance_queries();
  return tree;
}

bool inside(const std::shared_ptr<AABBTree> &tree, double xx, double yy,
            double zz) {
  cgal_helper::CK::Point_3 pt(xx, yy, zz);
  Point_inside inside_tester(*tree);

  // Determine the side and return true if inside!
  return inside_tester(pt) == CGAL::ON_BOUNDED_SIDE;
+3 −3
Original line number Diff line number Diff line
@@ -166,7 +166,7 @@ double signed_distance_3d(const RotateExtrude &rot_ext, double xx, double yy,
double signed_distance_3d(const Polyhedron &polyhedron, double xx, double yy,
                          double zz) {
  // TODO: support signed distance instead of -1.0/1.0
  return cgal_helper::inside(polyhedron.cgal_polyhedron(), xx, yy, zz) ? 1.0
  return cgal_helper::inside(polyhedron.cgal_aabb_tree(), xx, yy, zz) ? 1.0
                                                                      : -1.0;
}

+3 −3
Original line number Diff line number Diff line
@@ -63,8 +63,8 @@ $name="my_polyhedron");
)");
  auto polyh = std::get<csg::Polyhedron>(st.top.objs.at(0));
  CHECK(polyh.name == "my_polyhedron");
  CHECK(polyh.cgal_polyhedron().size_of_vertices() == 5);
  CHECK(polyh.cgal_polyhedron().size_of_facets() == 6);
  CHECK(polyh.cgal_polyhedron()->size_of_vertices() == 5);
  CHECK(polyh.cgal_polyhedron()->size_of_facets() == 6);
}

TEST_CASE("bad vs good polyhedron openscad example", "[csg]") {
@@ -101,5 +101,5 @@ faces = [[0, 3, 2], [0, 2, 1], [4, 0, 5], [5, 0, 1],
convexity = 1);
)");
  auto polyh = std::get<csg::Polyhedron>(st.top.objs.at(0));
  CHECK(polyh.cgal_polyhedron().size_of_vertices() == 12);
  CHECK(polyh.cgal_polyhedron()->size_of_vertices() == 12);
}
+14 −4
Original line number Diff line number Diff line
@@ -7,7 +7,9 @@
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Side_of_triangle_mesh.h>
#include <CGAL/Simple_cartesian.h>
#include <memory>
#pragma GCC diagnostic pop
#pragma warning default

@@ -16,15 +18,23 @@ namespace cgal_helper {
typedef CGAL::Simple_cartesian<double> CK;
typedef CGAL::Polyhedron_3<CK> Polyhedron;
typedef CGAL::Polygon_2<CK> Polygon;
typedef CGAL::AABB_face_graph_triangle_primitive<cgal_helper::Polyhedron>
    Primitive;
typedef CGAL::AABB_traits<cgal_helper::CK, Primitive> Traits;
typedef CGAL::AABB_tree<Traits> AABBTree;

Polyhedron
std::shared_ptr<Polyhedron>
create_polyhedron(const std::vector<std::tuple<double, double, double>> &points,
                  const std::vector<std::vector<unsigned int>> &faces);

std::shared_ptr<AABBTree>
create_aabb_tree(const std::shared_ptr<Polyhedron> &polyhedron);

Polygon create_polygon(const std::vector<std::tuple<double, double>> &points,
                       const std::vector<unsigned int> &path);

bool inside(const Polyhedron &polyhedron, double xx, double yy, double zz);
bool inside(const std::shared_ptr<AABBTree> &tree, double xx, double yy,
            double zz);

bool inside(const Polygon &polygon, double xx, double yy);