Skip to content

Commit

Permalink
[capi] add visibility to capi
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti committed Sep 29, 2023
1 parent 0b15dd1 commit de07c1c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
85 changes: 80 additions & 5 deletions src/capi/sfcgal_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <SFCGAL/algorithm/straightSkeleton.h>
#include <SFCGAL/algorithm/tesselate.h>
#include <SFCGAL/algorithm/union.h>
#include <SFCGAL/algorithm/visibility.h>
#include <SFCGAL/algorithm/volume.h>
#include <SFCGAL/triangulate/triangulate2DZ.h>

Expand Down Expand Up @@ -1259,15 +1260,15 @@ sfcgal_geometry_optimal_alpha_shapes(const sfcgal_geometry_t *geom,
return result.release();
}


extern "C" sfcgal_geometry_t *
sfcgal_y_monotone_partition_2(const sfcgal_geometry_t *geom)
{
const SFCGAL::Geometry *g1 = reinterpret_cast<const SFCGAL::Geometry *>(geom);
std::unique_ptr<SFCGAL::Geometry> result;

try {
result = SFCGAL::algorithm::partition_2(g1->as<const SFCGAL::Geometry>(), SFCGAL::algorithm::y_monotone);
result = SFCGAL::algorithm::partition_2(g1->as<const SFCGAL::Geometry>(),
SFCGAL::algorithm::y_monotone);
} catch (std::exception &e) {
SFCGAL_WARNING("During y_monotone_partition_2(A):");
SFCGAL_WARNING(" with A: %s",
Expand All @@ -1286,7 +1287,8 @@ sfcgal_approx_convex_partition_2(const sfcgal_geometry_t *geom)
std::unique_ptr<SFCGAL::Geometry> result;

try {
result = SFCGAL::algorithm::partition_2(g1->as<const SFCGAL::Geometry>(), SFCGAL::algorithm::approx_convex);
result = SFCGAL::algorithm::partition_2(g1->as<const SFCGAL::Geometry>(),
SFCGAL::algorithm::approx_convex);
} catch (std::exception &e) {
SFCGAL_WARNING("During approx_convex_partition_2(A):");
SFCGAL_WARNING(" with A: %s",
Expand All @@ -1305,7 +1307,9 @@ sfcgal_greene_approx_convex_partition_2(const sfcgal_geometry_t *geom)
std::unique_ptr<SFCGAL::Geometry> result;

try {
result = SFCGAL::algorithm::partition_2(g1->as<const SFCGAL::Geometry>(), SFCGAL::algorithm::greene_approx_convex);
result =
SFCGAL::algorithm::partition_2(g1->as<const SFCGAL::Geometry>(),
SFCGAL::algorithm::greene_approx_convex);
} catch (std::exception &e) {
SFCGAL_WARNING("During greene_approx_convex_partition_2(A):");
SFCGAL_WARNING(" with A: %s",
Expand All @@ -1323,7 +1327,8 @@ sfcgal_optimal_convex_partition_2(const sfcgal_geometry_t *geom)
std::unique_ptr<SFCGAL::Geometry> result;

try {
result = SFCGAL::algorithm::partition_2(g1->as<const SFCGAL::Geometry>(), SFCGAL::algorithm::optimal_convex);
result = SFCGAL::algorithm::partition_2(g1->as<const SFCGAL::Geometry>(),
SFCGAL::algorithm::optimal_convex);
} catch (std::exception &e) {
SFCGAL_WARNING("During optimal_convex_partition_2(A):");
SFCGAL_WARNING(" with A: %s",
Expand All @@ -1334,3 +1339,73 @@ sfcgal_optimal_convex_partition_2(const sfcgal_geometry_t *geom)

return result.release();
}

extern "C" sfcgal_geometry_t *
sfcgal_geometry_visibility_point(const sfcgal_geometry_t *polygon,
const sfcgal_geometry_t *point)
{

const auto *poly = reinterpret_cast<const SFCGAL::Geometry *>(polygon);
const auto *pt = reinterpret_cast<const SFCGAL::Geometry *>(point);
std::unique_ptr<SFCGAL::Geometry> result;

if (poly->geometryTypeId() != SFCGAL::TYPE_POLYGON) {
SFCGAL_ERROR("visibility() only applies to polygons");
return result.release();
}

if (pt->geometryTypeId() != SFCGAL::TYPE_POINT) {
SFCGAL_ERROR("second argument must be a point");
return result.release();
}

try {
result = SFCGAL::algorithm::visibility(poly->as<const SFCGAL::Polygon>(),
pt->as<const SFCGAL::Point>());
} catch (std::exception &e) {
SFCGAL_WARNING("During visibility(A, B) :");
SFCGAL_WARNING(" with A: %s", poly->asText().c_str());
SFCGAL_WARNING(" and B: %s", pt->asText().c_str());
SFCGAL_ERROR("%s", e.what());
return result.release();
}

return result.release();
}

extern "C" sfcgal_geometry_t *
sfcgal_geometry_visibility_segment(const sfcgal_geometry_t *polygon,
const sfcgal_geometry_t *pointA,
const sfcgal_geometry_t *pointB)
{
const auto *poly = reinterpret_cast<const SFCGAL::Geometry *>(polygon);
const auto *ptA = reinterpret_cast<const SFCGAL::Geometry *>(pointA);
const auto *ptB = reinterpret_cast<const SFCGAL::Geometry *>(pointB);
std::unique_ptr<SFCGAL::Geometry> result;

if (poly->geometryTypeId() != SFCGAL::TYPE_POLYGON) {
SFCGAL_ERROR("visibility() only applies to polygons");
return result.release();
}

if ((ptA->geometryTypeId() != SFCGAL::TYPE_POINT) ||
(ptB->geometryTypeId() != SFCGAL::TYPE_POINT)) {
SFCGAL_ERROR("second and third argument must be a point");
return result.release();
}

try {
result = SFCGAL::algorithm::visibility(poly->as<const SFCGAL::Polygon>(),
ptA->as<const SFCGAL::Point>(),
ptB->as<const SFCGAL::Point>());
} catch (std::exception &e) {
SFCGAL_WARNING("During visibility(A, B, C) :");
SFCGAL_WARNING(" with A: %s", poly->asText().c_str());
SFCGAL_WARNING(" and B: %s", ptA->asText().c_str());
SFCGAL_WARNING(" and C: %s", ptB->asText().c_str());
SFCGAL_ERROR("%s", e.what());
return result.release();
}

return result.release();
}
33 changes: 31 additions & 2 deletions src/capi/sfcgal_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef enum {
// TYPE_SURFACE = 14, //abstract
SFCGAL_TYPE_POLYHEDRALSURFACE = 15,
SFCGAL_TYPE_TRIANGULATEDSURFACE = 16,
SFCGAL_TYPE_TRIANGLE = 17,
SFCGAL_TYPE_TRIANGLE = 17,

//-- not official codes
SFCGAL_TYPE_SOLID = 101,
Expand Down Expand Up @@ -1064,7 +1064,8 @@ SFCGAL_API sfcgal_geometry_t *
sfcgal_approx_convex_partition_2(const sfcgal_geometry_t *geom);

/**
* Returns the greene approximal convex partition of a geometry (polygon without hole)
* Returns the greene approximal convex partition of a geometry (polygon without
* hole)
* @pre isValid(geom) == true
* @post isValid(return) == true
* @ingroup capi
Expand All @@ -1081,6 +1082,34 @@ sfcgal_greene_approx_convex_partition_2(const sfcgal_geometry_t *geom);
SFCGAL_API sfcgal_geometry_t *
sfcgal_optimal_convex_partition_2(const sfcgal_geometry_t *geom);

/**
* Returns the visibility polygon of a Point inside a Polygon
* @param polygon input geometry
* @param point input geometry
* @ingroup capi
* @pre polygon is a valid geometry
* @pre point must be inside polygon or on the boundary
*/
SFCGAL_API sfcgal_geometry_t *
sfcgal_geometry_visibility_point(const sfcgal_geometry_t *polygon,
const sfcgal_geometry_t *point);

/**
* @brief build the visibility polygon of the segment [pointA ; pointB] on a
* Polygon
* @param polygon input geometry
* @param pointA input geometry
* @param pointB input geometry
* @ingroup public_api
* @pre polygon is a valid geometry
* @pre pointA and pointB must be vertices of poly, adjacents and respect the
* direction
*/
SFCGAL_API sfcgal_geometry_t *
sfcgal_geometry_visibility_segment(const sfcgal_geometry_t *polygon,
const sfcgal_geometry_t *pointA,
const sfcgal_geometry_t *pointB);

/*--------------------------------------------------------------------------------------*
*
* Error handling
Expand Down

0 comments on commit de07c1c

Please sign in to comment.