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 04d6f32 commit 4150577
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/capi/sfcgal_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <SFCGAL/algorithm/tesselate.h>
#include <SFCGAL/algorithm/union.h>
#include <SFCGAL/algorithm/volume.h>
#include <SFCGAL/algorithm/visibility.h>
#include <SFCGAL/triangulate/triangulate2DZ.h>

#include <SFCGAL/detail/transform/ForceOrderPoints.h>
Expand Down Expand Up @@ -1334,3 +1335,76 @@ sfcgal_optimal_convex_partition_2(const sfcgal_geometry_t *geom)

return result.release();
}


extern "C" sfcgal_geometry_t *
sfcgal_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_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();
}


25 changes: 25 additions & 0 deletions src/capi/sfcgal_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,31 @@ 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_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_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 4150577

Please sign in to comment.