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

Resolve "support polygon"

parent 890f96ca
Loading
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -75,6 +75,29 @@ public:
  }
};

struct Polygon {
private:
  std::vector<std::tuple<double, double>> m_points;
  std::vector<unsigned int> m_path;
  cgal_helper::Polygon m_cgal_polygon;

public:
  std::optional<std::string> name;
  Polygon(const std::vector<std::vector<double>> &points,
          const std::vector<double> &path) {
    for (const auto &pt : points) {
      assert(pt.size() == 2);
      m_points.push_back({pt[0], pt[1]});
    }
    for (const auto &p : path) {
      m_path.push_back((unsigned int)(p));
    }
    m_cgal_polygon = cgal_helper::create_polygon(m_points, m_path);
  }

  const cgal_helper::Polygon &cgal_polygon() const { return m_cgal_polygon; }
};

enum Dimension { D2, D3 };

template <Dimension dim> struct Mulmatrix;
@@ -90,7 +113,7 @@ template <> struct TypeHelper<Dimension::D2> {
  using Type =
      std::variant<Circle, Square, Union<Dimension::D2>,
                   Intersection<Dimension::D2>, Difference<Dimension::D2>,
                   Mulmatrix<Dimension::D2>>;
                   Mulmatrix<Dimension::D2>, Polygon>;
};

template <> struct TypeHelper<Dimension::D3> {
+2 −1
Original line number Diff line number Diff line
@@ -7,7 +7,8 @@ add_library(csg
  levelset_3d.cpp
  matrix_functions.cpp
  parser.cpp
  cgal_helper.cpp
  cgal_helper_polyhedron.cpp
  cgal_helper_polygon.cpp
  )

target_link_libraries(csg PRIVATE
+39 −0
Original line number Diff line number Diff line
#include "csg_cgal_helper.hpp"

#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_2_algorithms.h>

namespace {
typedef CGAL::Polygon_2<cgal_helper::IK> Polygon;
typedef CGAL::Point_2<cgal_helper::IK> Point;
} // namespace

namespace cgal_helper {

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

  Polygon polyg;

  if (path.empty()) { // Include all points
    for (const auto &p : points) {
      auto [px, py] = p;
      polyg.push_back(Point(px, py));
    }
  } else {
    for (auto p_index : path) {
      auto [px, py] = points[p_index];
      polyg.push_back(Point(px, py));
    }
  }

  return polyg;
}

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

  return CGAL::bounded_side_2(polygon.begin(), polygon.end(), Point(xx, yy),
                              IK()) == CGAL::ON_BOUNDED_SIDE;
}

} // namespace cgal_helper
+9 −7
Original line number Diff line number Diff line
@@ -9,17 +9,17 @@
#include <CGAL/algorithm.h>
#include <CGAL/boost/graph/graph_traits_Polyhedron_3.h>

namespace cgal_helper {

namespace {

typedef Polyhedron::HalfedgeDS HalfedgeDS;
typedef cgal_helper::Polyhedron::HalfedgeDS HalfedgeDS;
typedef typename HalfedgeDS::Vertex Vertex;
typedef typename Vertex::Point Point;
typedef CGAL::AABB_face_graph_triangle_primitive<Polyhedron> Primitive;
typedef CGAL::AABB_traits<Kernel, Primitive> Traits;
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<Polyhedron, Kernel> Point_inside;
typedef CGAL::Side_of_triangle_mesh<cgal_helper::Polyhedron, cgal_helper::CK>
    Point_inside;

// A modifier creating a polyhedron with the incremental builder.
template <class HDS> class PolyhedronBuilder : public CGAL::Modifier_base<HDS> {
@@ -58,6 +58,8 @@ public:

} // namespace

namespace cgal_helper {

Polyhedron
create_polyhedron(const std::vector<std::tuple<double, double, double>> &points,
                  const std::vector<std::vector<unsigned int>> &faces) {
@@ -76,7 +78,7 @@ create_polyhedron(const std::vector<std::tuple<double, double, double>> &points,
}

bool inside(const Polyhedron &polyhedron, double xx, double yy, double zz) {
  Kernel::Point_3 pt(xx, yy, zz);
  cgal_helper::CK::Point_3 pt(xx, yy, zz);
  // Construct AABB tree with a KdTree
  Tree tree(faces(polyhedron).first, faces(polyhedron).second, polyhedron);
  tree.accelerate_distance_queries();
+8 −0
Original line number Diff line number Diff line
#include "csg_cgal_helper.hpp"
#include "csg_types.hpp"

#include <algorithm>
@@ -24,6 +25,7 @@ double signed_distance_2d(const Difference2D &, double, double);
double signed_distance_2d(const Intersection2D &, double, double);
double signed_distance_2d(const Mulmatrix2D &, double, double);
double signed_distance_2d(const Union2D &, double, double);
double signed_distance_2d(const Polygon &, double, double);

double signed_distance_2d(const Union2D &group, double xx, double yy) {
  auto sdist = -std::numeric_limits<double>::max();
@@ -85,6 +87,12 @@ double signed_distance_2d(const Circle &cir, double xx, double yy) {
  return EXTERNAL_FLOW * sign * dist;
}

double signed_distance_2d(const Polygon &polygon, double xx, double yy) {

  // TODO: support signed distance instead of -1.0/1.0
  return cgal_helper::inside(polygon.cgal_polygon(), xx, yy) ? 1.0 : -1.0;
}

double signed_distance_2d(const Type2D &obj, double xx, double yy) {

  return std::visit(
Loading