-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
algorithm: Add alpha wrapping 3d algorithm
- Loading branch information
Showing
10 changed files
with
283 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2024-2024, SFCGAL Contributors and Oslandia | ||
// SPDX-License-Identifier: LGPL-2.0-or-later | ||
|
||
#include "SFCGAL/algorithm/alphaWrapping3D.h" | ||
#include "SFCGAL/detail/GetPointsVisitor.h" | ||
|
||
#include <CGAL/Cartesian_converter.h> | ||
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> | ||
#include <CGAL/alpha_wrap_3.h> | ||
|
||
namespace SFCGAL::algorithm { | ||
|
||
using InexactKernel = CGAL::Exact_predicates_inexact_constructions_kernel; | ||
using Inexact_Point_3 = InexactKernel::Point_3; | ||
using Mesh = CGAL::Surface_mesh<Inexact_Point_3>; | ||
using ExactMesh = CGAL::Surface_mesh<Kernel::Point_3>; | ||
using EK_to_IK = CGAL::Cartesian_converter<Kernel, InexactKernel>; | ||
using IK_to_EK = CGAL::Cartesian_converter<InexactKernel, Kernel>; | ||
|
||
auto | ||
alphaWrapping3D(const Geometry &geom, size_t relativeAlpha, | ||
size_t relativeOffset) -> std::unique_ptr<PolyhedralSurface> | ||
{ | ||
if (geom.isEmpty()) { | ||
return std::make_unique<PolyhedralSurface>(); | ||
} | ||
|
||
// Collect points from geometry | ||
SFCGAL::detail::GetPointsVisitor getPointVisitor; | ||
const_cast<Geometry &>(geom).accept(getPointVisitor); | ||
|
||
// Need at least 4 points for 3D alpha wrapping | ||
if (getPointVisitor.points.size() < 4) { | ||
return std::make_unique<PolyhedralSurface>(); | ||
} | ||
|
||
// Create points vector | ||
EK_to_IK toInexact; | ||
std::vector<Inexact_Point_3> points; | ||
points.reserve(getPointVisitor.points.size()); | ||
for (const auto &point : getPointVisitor.points) { | ||
points.push_back(toInexact(point->toPoint_3())); | ||
} | ||
|
||
// compute alpha and offset | ||
CGAL::Bbox_3 bbox = CGAL::bbox_3(points.begin(), points.end()); | ||
const double diag_length = std::sqrt(CGAL::square(bbox.xmax() - bbox.xmin()) + | ||
CGAL::square(bbox.ymax() - bbox.ymin()) + | ||
CGAL::square(bbox.zmax() - bbox.zmin())); | ||
const double alpha = diag_length / static_cast<double>(relativeAlpha); | ||
|
||
Mesh wrapMesh; | ||
if (relativeOffset == 0) { | ||
CGAL::alpha_wrap_3(points, alpha, wrapMesh); | ||
} else { | ||
const double offset = diag_length / static_cast<double>(relativeOffset); | ||
CGAL::alpha_wrap_3(points, alpha, offset, wrapMesh); | ||
} | ||
|
||
return std::make_unique<PolyhedralSurface>(PolyhedralSurface(wrapMesh)); | ||
} | ||
} // namespace SFCGAL::algorithm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* SFCGAL | ||
* | ||
* Copyright (C) 2024 SFCGAL Contributors and Oslandia <[email protected]> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Library General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Library General Public License for more details. | ||
* You should have received a copy of the GNU Library General Public | ||
* License along with this library; if not, see | ||
<http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef _SFCGAL_ALGORITHM_ALPHASHAPES3D_H_ | ||
#define _SFCGAL_ALGORITHM_ALPHASHAPES3D_H_ | ||
|
||
#include "SFCGAL/Geometry.h" | ||
#include "SFCGAL/PolyhedralSurface.h" | ||
|
||
namespace SFCGAL::algorithm { | ||
|
||
/** | ||
* Computes the 3D alpha wrapping of a geometry | ||
* https://doc.cgal.org/latest/Alpha_wrap_3/index.html | ||
* @ingroup public_api | ||
* @since 2.1 | ||
* @param geom input geometry | ||
* @param relativeAlpha This parameter is used to determine which features will | ||
* appear in the output A small relativeAlpha will produce an output less | ||
* complex but less faithful to the input | ||
* @param relativeOffset This parameter controls the tightness of the result | ||
* A large relativeOffset parameter will tend to better preserve sharp features | ||
* as projection If this parameter is equal to 0, it is computed from the alpha | ||
* parameter | ||
* @return A PolyhedralSurface representing the 3D alpha wrapping of the | ||
* geometry | ||
*/ | ||
SFCGAL_API std::unique_ptr<PolyhedralSurface> | ||
alphaWrapping3D(const Geometry &geom, size_t relativeAlpha, | ||
size_t relativeOffset = 0); | ||
|
||
} // namespace SFCGAL::algorithm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* SFCGAL | ||
* | ||
* Copyright (C) 2012-2013 Oslandia <[email protected]> | ||
* Copyright (C) 2012-2013 IGN (http://www.ign.fr) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Library General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Library General Public License for more details. | ||
* You should have received a copy of the GNU Library General Public | ||
* License along with this library; if not, see | ||
<http://www.gnu.org/licenses/>. | ||
*/ | ||
#include <boost/format/parsing.hpp> | ||
#include <boost/test/tools/old/interface.hpp> | ||
#include <boost/test/unit_test.hpp> | ||
|
||
#include "SFCGAL/GeometryCollection.h" | ||
#include "SFCGAL/Polygon.h" | ||
#include "SFCGAL/algorithm/alphaWrapping3D.h" | ||
#include "SFCGAL/algorithm/covers.h" | ||
#include "SFCGAL/io/wkt.h" | ||
|
||
using namespace SFCGAL; | ||
|
||
#include "../../../test_config.h" | ||
// always after CGAL | ||
using namespace boost::unit_test; | ||
|
||
BOOST_AUTO_TEST_SUITE(SFCGAL_algorithm_AlphaWrapping3DTest) | ||
|
||
// algorithm::alphaWrapping3D | ||
|
||
|
||
BOOST_AUTO_TEST_CASE(testAlphaWrapping3D_Empty) | ||
{ | ||
GeometryCollection emptyCollection; | ||
emptyCollection.addGeometry(Polygon()); | ||
emptyCollection.addGeometry(Polygon()); | ||
std::unique_ptr<Geometry> emptyAlphaWrapping3D (algorithm::alphaWrapping3D(emptyCollection, 300, 5000)); | ||
BOOST_CHECK(emptyAlphaWrapping3D->isEmpty()); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(testAlphaWrapping3D_MultiPoint) | ||
{ | ||
std::string inputData(SFCGAL_TEST_DIRECTORY); | ||
inputData += "/data/bunny1000Wkt.txt"; | ||
std::ifstream bunnyFSInput(inputData.c_str()); | ||
BOOST_REQUIRE(bunnyFSInput.good()); | ||
std::ostringstream inputWkt; | ||
inputWkt << bunnyFSInput.rdbuf(); | ||
|
||
std::unique_ptr<Geometry> inputGeom(io::readWkt(inputWkt.str())); | ||
BOOST_REQUIRE(inputGeom->is3D()); | ||
|
||
std::unique_ptr<Geometry> alphaWrappingResult(algorithm::alphaWrapping3D(inputGeom->as<const SFCGAL::Geometry>(), 20)); | ||
|
||
std::string resultData(SFCGAL_TEST_DIRECTORY); | ||
resultData += "/data/bunny1000AlphaWrapping20Wkt.txt"; | ||
std::ifstream bunnyFSResult(resultData.c_str()); | ||
BOOST_REQUIRE(bunnyFSResult.good()); | ||
std::ostringstream resultWkt; | ||
resultWkt << bunnyFSResult.rdbuf(); | ||
|
||
std::unique_ptr<Geometry> alphaWrappingExpectedGeom(io::readWkt(resultWkt.str())); | ||
BOOST_REQUIRE(alphaWrappingExpectedGeom->is3D()); | ||
|
||
BOOST_CHECK(algorithm::covers3D(*alphaWrappingResult, *alphaWrappingExpectedGeom)); | ||
} | ||
|
||
|
||
BOOST_AUTO_TEST_SUITE_END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters