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

support cmake option to be able to build without CGAL and its dependencies. it...

parent 18508153
Loading
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@ build-img:
  tags:
    - docker


###### Test jobs #####

.test:cmake: &cmake_def
@@ -25,23 +24,27 @@ build-img:
  needs: ['build-img']
  script:
    - conan install -if build -g cmake_find_package catch2/2.13.7@
    - conan install -if build -g cmake_find_package cgal/5.2.1@
    - if [ "$ENABLE_CSG" == "ON" ]; then conan install -if build -g cmake_find_package cgal/5.2.1@; fi
    - conan install -if build -g cmake_find_package taocpp-pegtl/3.2.1@

    - cmake -S. -Bbuild -GNinja -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_MODULE_PATH=$PWD/build
    - cmake -S.
            -Bbuild
            -GNinja
            -DCMAKE_MODULE_PATH=$PWD/build
            -DCMAKE_BUILD_TYPE=Debug
            -DCSG_CGAL_ENABLED=${ENABLE_CSG}
    - cmake --build build --target unit_tests_csg
    - cd build
    - ctest
  tags:
    - docker

test:cmake:debug:
test:cgal:
  variables:
    BUILD_TYPE: "Debug"
    ENABLE_CSG: "ON"
  <<: *cmake_def

test:cmake:release:
test:no_cgal:
  variables:
    BUILD_TYPE: "Release"
  allow_failure: true
    ENABLE_CSG: "OFF"
  <<: *cmake_def
+7 −1
Original line number Diff line number Diff line
@@ -12,13 +12,19 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(CSG_CGAL_ENABLED   "Enable CGAL"   ON)

find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
  set( CMAKE_CXX_COMPILER_LAUNCHER ccache )
endif()

find_package(pegtl REQUIRED)

if (CSG_CGAL_ENABLED)
   find_package(CGAL REQUIRED)
   add_definitions(-DUSE_CGAL)
endif()

add_subdirectory(src/csg)

+27 −0
Original line number Diff line number Diff line
#ifndef CSG_TYPES_H_
#define CSG_TYPES_H_

#if USE_CGAL
#include "csg_cgal_helper.hpp"
#endif
#include "csg_matrix_functions.hpp"

#include <array>
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <variant>
#include <vector>
@@ -50,6 +53,7 @@ struct Cone {
  bool center;
};

#if USE_CGAL
struct Polyhedron {
private:
  std::vector<std::tuple<double, double, double>> m_points;
@@ -77,7 +81,9 @@ public:
    return m_cgal_aabb_tree;
  }
};
#endif

#if USE_CGAL
struct Polygon {
private:
  std::vector<std::tuple<double, double>> m_points;
@@ -97,6 +103,7 @@ public:

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

enum Dimension { D2, D3 };

@@ -110,19 +117,37 @@ struct Hull;

template <Dimension dim> struct TypeHelper;

#if USE_CGAL
template <> struct TypeHelper<Dimension::D2> {
  using Type =
      std::variant<Circle, Square, Union<Dimension::D2>,
                   Intersection<Dimension::D2>, Difference<Dimension::D2>,
                   Mulmatrix<Dimension::D2>, Polygon>;
};
#else
template <> struct TypeHelper<Dimension::D2> {
  using Type =
      std::variant<Circle, Square, Union<Dimension::D2>,
                   Intersection<Dimension::D2>, Difference<Dimension::D2>,
                   Mulmatrix<Dimension::D2>>;
};
#endif

#if USE_CGAL
template <> struct TypeHelper<Dimension::D3> {
  using Type = std::variant<Sphere, Cube, Cylinder, Cone, Union<Dimension::D3>,
                            Intersection<Dimension::D3>,
                            Difference<Dimension::D3>, Mulmatrix<Dimension::D3>,
                            LinearExtrude, RotateExtrude, Polyhedron, Hull>;
};
#else
template <> struct TypeHelper<Dimension::D3> {
  using Type =
      std::variant<Sphere, Cube, Cylinder, Cone, Union<Dimension::D3>,
                   Intersection<Dimension::D3>, Difference<Dimension::D3>,
                   Mulmatrix<Dimension::D3>, LinearExtrude, RotateExtrude>;
};
#endif

template <Dimension dim> struct Union {
  std::vector<typename TypeHelper<dim>::Type> objs;
@@ -195,6 +220,7 @@ struct RotateExtrude {
  Union<Dimension::D2> group;
};

#if USE_CGAL
struct Hull {
private:
  std::shared_ptr<cgal_helper::Polyhedron> m_cgal_polyhedron;
@@ -222,6 +248,7 @@ public:
    return m_cgal_aabb_tree;
  }
};
#endif

struct Tree {
  Union<Dimension::D3> top;
+15 −4
Original line number Diff line number Diff line
################################################################################
# CSG Parser
################################################################################
add_library(csg-eb
  cgal_helper_polygon.cpp
  cgal_helper_polyhedron.cpp

set(csg-eb_SOURCES)
list(APPEND csg-eb_SOURCES
  csg.cpp
  levelset_2d.cpp
  levelset_3d.cpp
@@ -12,10 +12,21 @@ add_library(csg-eb
  exception.cpp
  )

if (CSG_CGAL_ENABLED)
   list(APPEND csg-eb_SOURCES
      cgal_helper_polygon.cpp
      cgal_helper_polyhedron.cpp
   )
endif()

add_library(csg-eb ${csg-eb_SOURCES})

target_link_libraries(csg-eb PRIVATE
                      CGAL::CGAL
                      stdc++fs
                      taocpp::pegtl
                      )
if (CSG_CGAL_ENABLED)
   target_link_libraries(csg-eb PRIVATE CGAL::CGAL)
endif()

add_subdirectory(tests)
+6 −0
Original line number Diff line number Diff line
#if USE_CGAL
#include "csg_cgal_helper.hpp"
#endif
#include "csg_types.hpp"

#include <algorithm>
@@ -25,7 +27,9 @@ 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);
#if USE_CGAL
double signed_distance_2d(const Polygon &, double, double);
#endif

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

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

  bool inside = cgal_helper::inside(polygon.cgal_polygon(), xx, yy);
@@ -99,6 +104,7 @@ double signed_distance_2d(const Polygon &polygon, double xx, double yy) {

  return EXTERNAL_FLOW * sign * dist;
}
#endif

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

Loading