Skip to content

Commit

Permalink
Issue: #23 CGAL adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
attcs committed Mar 7, 2024
1 parent a6d39db commit 6ed4284
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 0 deletions.
210 changes: 210 additions & 0 deletions adaptor.cgal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#pragma once
/*
MIT License
Copyright (c) 2021 Attila Csikós
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "octree.h"
/* Required headers
#include <CGAL/Bbox_2.h>
#include <CGAL/Bbox_3.h>
#include <CGAL/Cartesian.h>
#include <CGAL/Origin.h>
#include <CGAL/Plane_3.h>
#include <CGAL/Point_2.h>
#include <CGAL/Point_3.h>
#include <CGAL/Ray_2.h>
#include <CGAL/Ray_3.h>
#include <CGAL/basic.h>
#include <CGAL/cartesian.h>
*/
namespace CGAL
{
// 2d Hyperplane
struct Plane_2
{
double offset; // n1*x + n2*y + offset = 0
Point_2<Cartesian<double>> normal;
};
} // namespace CGAL


namespace OrthoTree
{
namespace CGALAdaptor
{
struct AdaptorGeneralBasics2D
{
using TGeometry = double;
using TVector = CGAL::Point_2<CGAL::Cartesian<TGeometry>>;
using TBox = CGAL::Bbox_2;
using TRay = CGAL::Ray_2<CGAL::Cartesian<TGeometry>>;
using TPlane = CGAL::Plane_2;

static inline TGeometry GetPointC(TVector const& point, dim_t dimensionID) noexcept { return point[dimensionID]; }
static inline void SetPointC(TVector& point, dim_t dimensionID, TGeometry value) noexcept
{
if (dimensionID == 0)
point = TVector(value, point.y());
else
point = TVector(point.x(), value);
}

static inline TGeometry GetBoxMinC(TBox const& box, dim_t dimensionID) noexcept { return box.min(dimensionID); }
static inline TGeometry GetBoxMaxC(TBox const& box, dim_t dimensionID) noexcept { return box.max(dimensionID); }
static inline void SetBoxMinC(TBox& box, dim_t dimensionID, TGeometry value) noexcept
{
if (dimensionID == 0)
box = TBox(value, box.ymin(), box.xmax(), box.ymax());
else
box = TBox(box.xmin(), value, box.xmax(), box.ymax());
}
static inline void SetBoxMaxC(TBox& box, dim_t dimensionID, TGeometry value) noexcept
{
if (dimensionID == 0)
box = TBox(box.xmin(), box.ymin(), value, box.ymax());
else
box = TBox(box.xmin(), box.ymin(), box.xmax(), value);
}

static inline TVector GetRayDirection(TRay const& ray) noexcept
{
auto const direction = ray.to_vector();
return TVector(direction.x(), direction.y());
}
static inline TVector GetRayOrigin(TRay const& ray) noexcept { return ray.source(); }

static inline TVector const& GetPlaneNormal(TPlane const& plane) noexcept { return plane.normal; }
static inline TGeometry GetPlaneOrigoDistance(TPlane const& plane) noexcept { return -plane.offset; }
};

using CGALAdaptorGeneral2D =
AdaptorGeneralBase<2, CGAL::Point_2<CGAL::Cartesian<double>>, CGAL::Bbox_2, CGAL::Ray_2<CGAL::Cartesian<double>>, CGAL::Plane_2, double, AdaptorGeneralBasics2D>;


struct AdaptorGeneralBasics3D
{
using TGeometry = double;
using TVector = CGAL::Point_3<CGAL::Cartesian<TGeometry>>;
using TBox = CGAL::Bbox_3;
using TRay = CGAL::Ray_3<CGAL::Cartesian<TGeometry>>;
using TPlane = CGAL::Plane_3<CGAL::Cartesian<TGeometry>>;

static inline TGeometry GetPointC(TVector const& point, dim_t dimensionID) noexcept { return point[dimensionID]; }
static inline void SetPointC(TVector& point, dim_t dimensionID, TGeometry value) noexcept
{
switch (dimensionID)
{
case 0: point = TVector(value, point.y(), point.z()); return;
case 1: point = TVector(point.x(), value, point.z()); return;
case 2: point = TVector(point.x(), point.y(), value); return;
}
assert(false);
std::terminate();
}

static inline TGeometry GetBoxMinC(TBox const& box, dim_t dimensionID) noexcept { return box.min(dimensionID); }
static inline TGeometry GetBoxMaxC(TBox const& box, dim_t dimensionID) noexcept { return box.max(dimensionID); }
static inline void SetBoxMinC(TBox& box, dim_t dimensionID, TGeometry value) noexcept
{
switch (dimensionID)
{
case 0: box = TBox(value, box.ymin(), box.zmin(), box.xmax(), box.ymax(), box.zmax()); return;
case 1: box = TBox(box.xmin(), value, box.zmin(), box.xmax(), box.ymax(), box.zmax()); return;
case 2: box = TBox(box.xmin(), box.ymin(), value, box.xmax(), box.ymax(), box.zmax()); return;
}
assert(false);
std::terminate();
}

static inline void SetBoxMaxC(TBox& box, dim_t dimensionID, TGeometry value) noexcept
{
switch (dimensionID)
{
case 0: box = TBox(box.xmin(), box.ymin(), box.zmin(), value, box.ymax(), box.zmax()); return;
case 1: box = TBox(box.xmin(), box.ymin(), box.zmin(), box.xmax(), value, box.zmax()); return;
case 2: box = TBox(box.xmin(), box.ymin(), box.zmin(), box.xmax(), box.ymax(), value); return;
}
assert(false);
std::terminate();
}

static inline TVector GetRayDirection(TRay const& ray) noexcept
{
auto const direction = ray.to_vector();
return TVector(direction.x(), direction.y(), direction.z());
}
static inline TVector GetRayOrigin(TRay const& ray) noexcept { return ray.source(); }

static inline TVector GetPlaneNormal(TPlane const& plane) noexcept { return TVector(plane.a(), plane.b(), plane.c()); }
static inline TGeometry GetPlaneOrigoDistance(TPlane const& plane) noexcept { return -plane.d(); }
};
using CGALAdaptorGeneral3D =
AdaptorGeneralBase<3, CGAL::Point_3<CGAL::Cartesian<double>>, CGAL::Bbox_3, CGAL::Ray_3<CGAL::Cartesian<double>>, CGAL::Plane_3<CGAL::Cartesian<double>>, double, AdaptorGeneralBasics3D>;


} // namespace CGALAdaptor
} // namespace OrthoTree

namespace CGAL
{
using namespace OrthoTree::CGALAdaptor;

// Core types
using QuadtreePoint =
OrthoTree::OrthoTreePoint<2, CGAL::Point_2<CGAL::Cartesian<double>>, CGAL::Bbox_2, CGAL::Ray_2<CGAL::Cartesian<double>>, CGAL::Plane_2, double, CGALAdaptorGeneral2D>;

using OctreePoint = OrthoTree::
OrthoTreePoint<3, CGAL::Point_3<CGAL::Cartesian<double>>, CGAL::Bbox_3, CGAL::Ray_3<CGAL::Cartesian<double>>, CGAL::Plane_3<CGAL::Cartesian<double>>, double, CGALAdaptorGeneral3D>;

template<int SPLIT_DEPTH_INCREASEMENT>
using QuadtreeBoxs =
OrthoTree::OrthoTreeBoundingBox<2, CGAL::Point_2<CGAL::Cartesian<double>>, CGAL::Bbox_2, CGAL::Ray_2<CGAL::Cartesian<double>>, CGAL::Plane_2, double, SPLIT_DEPTH_INCREASEMENT, CGALAdaptorGeneral2D>;

using QuadtreeBox = QuadtreeBoxs<2>;

template<int SPLIT_DEPTH_INCREASEMENT>
using OctreeBoxs = OrthoTree::OrthoTreeBoundingBox<
3,
CGAL::Point_3<CGAL::Cartesian<double>>,
CGAL::Bbox_3,
CGAL::Ray_3<CGAL::Cartesian<double>>,
CGAL::Plane_3<CGAL::Cartesian<double>>,
double,
SPLIT_DEPTH_INCREASEMENT,
CGALAdaptorGeneral3D>;

using OctreeBox = OctreeBoxs<2>;


// Container types

using QuadtreePointC = OrthoTree::OrthoTreeContainerPoint<QuadtreePoint, Point_2<Cartesian<double>>>;
using OctreePointC = OrthoTree::OrthoTreeContainerPoint<OctreePoint, Point_3<Cartesian<double>>>;

template<int SPLIT_DEPTH_INCREASEMENT>
using QuadtreeBoxCs = OrthoTree::OrthoTreeContainerBox<QuadtreeBoxs<SPLIT_DEPTH_INCREASEMENT>, Bbox_2>;
using QuadtreeBoxC = QuadtreeBoxCs<2>;
template<int SPLIT_DEPTH_INCREASEMENT>
using OctreeBoxCs = OrthoTree::OrthoTreeContainerBox<OctreeBoxs<SPLIT_DEPTH_INCREASEMENT>, Bbox_3>;
using OctreeBoxC = OctreeBoxCs<2>;
} // namespace CGAL
1 change: 1 addition & 0 deletions adaptortests/adaptortests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\adaptor.cgal.h" />
<ClInclude Include="..\adaptor.glm.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions adaptortests/adaptortests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<ClInclude Include="..\adaptor.glm.h">
<Filter>Adapters</Filter>
</ClInclude>
<ClInclude Include="..\adaptor.cgal.h">
<Filter>Adapters</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
63 changes: 63 additions & 0 deletions adaptortests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
#include <boost/geometry.hpp>
#include "../adaptor.boost.h"

// CGAL
#include <CGAL/Bbox_2.h>
#include <CGAL/Bbox_3.h>
#include <CGAL/Cartesian.h>
#include <CGAL/Origin.h>
#include <CGAL/Plane_3.h>
#include <CGAL/Point_2.h>
#include <CGAL/Point_3.h>
#include <CGAL/Ray_2.h>
#include <CGAL/Ray_3.h>
#include <CGAL/basic.h>
#include <CGAL/cartesian.h>
#include "../adaptor.cgal.h"

// Eigen
#include <Eigen/Dense>
#include <Eigen/Geometry>
Expand Down Expand Up @@ -66,6 +80,7 @@ namespace
}
}


// Boost

template<int DIMENSION_NO, typename TRay, typename TVector, typename TOrthoTreeA>
Expand All @@ -82,6 +97,41 @@ namespace
planeA.origo_distance = typename TOrthoTreeA::TGeometry(planeO.OrigoDistance);
}


// CGAL

template<int DIMENSION_NO, typename TRay, typename TVector, typename TOrthoTreeA>
void rayConv(TRay const& rayO, CGAL::Ray_2<CGAL::Cartesian<double>>& rayA)
{
using namespace CGAL;

auto const s = Point_2<Cartesian<double>>(rayO.Origin[0], rayO.Origin[1]);
auto const v = Vector_2<Cartesian<double>>(rayO.Direction[0], rayO.Direction[1]);
rayA = Ray_2<Cartesian<double>>(s, v);
}

template<int DIMENSION_NO, typename TPlane, typename TVector, typename TOrthoTreeA>
void planeConv(TPlane const& planeO, CGAL::Plane_2& planeA)
{
vectorConv<2, TVector, TOrthoTreeA>(planeO.Normal, planeA.normal);
planeA.offset = -typename TOrthoTreeA::TGeometry(planeO.OrigoDistance);
}

template<int DIMENSION_NO, typename TRay, typename TVector, typename TOrthoTreeA>
void rayConv(TRay const& rayO, CGAL::Ray_3<CGAL::Cartesian<double>>& rayA)
{
using namespace CGAL;

auto const s = Point_3<Cartesian<double>>(rayO.origin[0], rayO.origin[1], rayO.origin[2]);
auto const v = Vector_3<Cartesian<double>>(rayO.direction[0], rayO.direction[1], rayO.direction[2]);
rayA = Ray_2<Cartesian<double>>(s, v);
}

template<int DIMENSION_NO, typename TPlane, typename TVector, typename TOrthoTreeA>
void planeConv(TPlane const& planeO, CGAL::Plane_3<CGAL::Cartesian<double>>& planeA)
{
planeA = CGAL::Plane_3<CGAL::Cartesian<double>>(planeO.Normal[0], planeO.Normal[1], planeO.Normal[2], -planeO.OrigoDistance);
}

// Eigen

Expand Down Expand Up @@ -502,6 +552,19 @@ namespace BoostAdapter
}
} // namespace BoostAdapter

namespace CGALAdapter
{
TEST(CGAL_CoreTest, Point3D)
{
corePointTest3D<CGAL::OctreePoint>();
}

TEST(CGAL_ContainerTest, Box2D)
{
containerBoxTest2D<CGAL::QuadtreeBoxC>();
}
} // namespace CGALAdapter

namespace EigenAdapter
{
TEST(Eigen_CoreTest, Point3D)
Expand Down
1 change: 1 addition & 0 deletions adaptortests/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": [
"boost",
"cgal",
"eigen3",
"gtest",
"glm"
Expand Down

0 comments on commit 6ed4284

Please sign in to comment.