Skip to content

Commit

Permalink
Merge pull request #1053 from Geode-solutions/feat/polygon
Browse files Browse the repository at this point in the history
feat(BasicObject): add new Polygon class
  • Loading branch information
MelchiorSchuh authored Nov 29, 2024
2 parents 74ba972 + 2303bb6 commit dee4fd3
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 129 deletions.
121 changes: 121 additions & 0 deletions include/geode/geometry/basic_objects/polygon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2019 - 2024 Geode-solutions
*
* 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.
*
*/

#pragma once

#include <optional>

#include <geode/geometry/common.hpp>
#include <geode/geometry/vector.hpp>

namespace geode
{
FORWARD_DECLARATION_DIMENSION_CLASS( BoundingBox );
FORWARD_DECLARATION_DIMENSION_CLASS( OwnerPolygon );
FORWARD_DECLARATION_DIMENSION_CLASS( Point );
class OwnerPlane;
class Plane;

template < index_t dimension >
using RefPoint = std::reference_wrapper< const Point< dimension > >;
ALIAS_2D_AND_3D( RefPoint );
} // namespace geode

namespace geode
{
template < typename PointType, index_t dimension >
class GenericPolygon
{
public:
GenericPolygon( std::vector< PointType > vertices ) noexcept;

GenericPolygon(
const GenericPolygon< PointType, dimension >& other ) noexcept;
GenericPolygon< PointType, dimension >& operator=(
const GenericPolygon< PointType, dimension >& other ) noexcept;
GenericPolygon(
GenericPolygon< PointType, dimension >&& other ) noexcept;
GenericPolygon< PointType, dimension >& operator=(
GenericPolygon< PointType, dimension >&& other ) noexcept;

[[nodiscard]] Point< dimension > barycenter() const;
template < index_t T = dimension >
[[nodiscard]]
typename std::enable_if< T == 3, std::optional< Vector3D > >::type
normal() const;
template < index_t T = dimension >
[[nodiscard]]
typename std::enable_if< T == 3, std::optional< Plane > >::type
plane() const;
template < index_t T = dimension >
[[nodiscard]]
typename std::enable_if< T == 3, std::optional< OwnerPlane > >::type
owner_plane() const;
[[nodiscard]] index_t nb_vertices() const;
void set_point( index_t vertex, PointType point );
[[nodiscard]] const std::vector< PointType >& vertices() const;
[[nodiscard]] BoundingBox< dimension > bounding_box() const;
[[nodiscard]] std::vector< std::array< index_t, 3 > >
triangulate() const;
[[nodiscard]] bool is_degenerated() const;
[[nodiscard]] std::string string() const;

private:
std::vector< PointType > vertices_;
};

template < index_t dimension >
class OwnerPolygon : public GenericPolygon< Point< dimension >, dimension >
{
using Base = GenericPolygon< Point< dimension >, dimension >;

public:
explicit OwnerPolygon(
std::vector< Point< dimension > > vertices ) noexcept;

OwnerPolygon( const OwnerPolygon< dimension >& other ) noexcept;
OwnerPolygon< dimension >& operator=(
const OwnerPolygon< dimension >& other ) noexcept;
OwnerPolygon( OwnerPolygon< dimension >&& other ) noexcept;
OwnerPolygon< dimension >& operator=(
OwnerPolygon< dimension >&& other ) noexcept;
};
ALIAS_2D_AND_3D( OwnerPolygon );

template < index_t dimension >
class Polygon : public GenericPolygon< RefPoint< dimension >, dimension >
{
using Base = GenericPolygon< RefPoint< dimension >, dimension >;

public:
Polygon( std::vector< RefPoint< dimension > > vertices ) noexcept;

Polygon( const Polygon< dimension >& other ) noexcept;
Polygon< dimension >& operator=(
const Polygon< dimension >& other ) noexcept;
Polygon( Polygon< dimension >&& other ) noexcept;
Polygon< dimension >& operator=(
Polygon< dimension >&& other ) noexcept;
};
ALIAS_2D_AND_3D( Polygon );
} // namespace geode
3 changes: 3 additions & 0 deletions include/geode/mesh/core/surface_mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
namespace geode
{
FORWARD_DECLARATION_DIMENSION_CLASS( Point );
FORWARD_DECLARATION_DIMENSION_CLASS( Polygon );
FORWARD_DECLARATION_DIMENSION_CLASS( Vector );
FORWARD_DECLARATION_DIMENSION_CLASS( BoundingBox );
FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceEdges );
Expand Down Expand Up @@ -210,6 +211,8 @@ namespace geode
[[nodiscard]] std::optional< local_index_t > vertex_in_polygon(
index_t polygon_id, index_t vertex_id ) const;

Polygon< dimension > polygon( index_t polygon_id ) const;

/*!
* Return the index in the mesh of a given polygon edge vertex.
* @param[in] polygon_edge Local index of edge in a polygon.
Expand Down
3 changes: 3 additions & 0 deletions src/geode/geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_geode_library(
"basic_objects/cylinder.cpp"
"basic_objects/infinite_line.cpp"
"basic_objects/plane.cpp"
"basic_objects/polygon.cpp"
"basic_objects/segment.cpp"
"basic_objects/sphere.cpp"
"basic_objects/tetrahedron.cpp"
Expand Down Expand Up @@ -62,6 +63,7 @@ add_geode_library(
"basic_objects/cylinder.hpp"
"basic_objects/infinite_line.hpp"
"basic_objects/plane.hpp"
"basic_objects/polygon.hpp"
"basic_objects/segment.hpp"
"basic_objects/sphere.hpp"
"basic_objects/tetrahedron.hpp"
Expand Down Expand Up @@ -102,5 +104,6 @@ add_geode_library(
${PROJECT_NAME}::basic
PRIVATE_DEPENDENCIES
Async++
earcut_hpp::earcut_hpp
nanoflann::nanoflann
)
Loading

0 comments on commit dee4fd3

Please sign in to comment.