deal.II version 9.7.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
polygon.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#include <deal.II/base/config.h>
16
18
19#include <deal.II/fe/mapping.h>
20
22#include <deal.II/grid/tria.h> //should be ok to delete is in grid tools
23
24#ifdef DEAL_II_WITH_CGAL
25
26# include <CGAL/Boolean_set_operations_2.h>
27
29
30# ifndef DOXYGEN
31// Template implementations
32namespace CGALWrappers
33{
34 template <typename KernelType>
35 CGAL::Polygon_2<KernelType>
37 const typename Triangulation<2, 2>::cell_iterator &cell,
38 const Mapping<2, 2> &mapping)
39 {
40 CGAL::Polygon_2<KernelType> polygon;
41 const auto &vertices = mapping.get_vertices(cell);
42
44 {
45 polygon.push_back(
46 CGALWrappers::dealii_point_to_cgal_point<CGAL::Point_2<KernelType>,
47 2>(vertices[0]));
48 polygon.push_back(
49 CGALWrappers::dealii_point_to_cgal_point<CGAL::Point_2<KernelType>,
50 2>(vertices[1]));
51 polygon.push_back(
52 CGALWrappers::dealii_point_to_cgal_point<CGAL::Point_2<KernelType>,
53 2>(vertices[2]));
54 }
56 {
57 polygon.push_back(
58 CGALWrappers::dealii_point_to_cgal_point<CGAL::Point_2<KernelType>,
59 2>(vertices[0]));
60 polygon.push_back(
61 CGALWrappers::dealii_point_to_cgal_point<CGAL::Point_2<KernelType>,
62 2>(vertices[1]));
63 polygon.push_back(
64 CGALWrappers::dealii_point_to_cgal_point<CGAL::Point_2<KernelType>,
65 2>(vertices[3]));
66 polygon.push_back(
67 CGALWrappers::dealii_point_to_cgal_point<CGAL::Point_2<KernelType>,
68 2>(vertices[2]));
69 }
70 else
71 {
73 }
74 return polygon;
75 }
76
77
78
79 template <typename KernelType>
80 CGAL::Polygon_with_holes_2<KernelType>
81 dealii_tria_to_cgal_polygon(const Triangulation<2, 2> &tria,
82 const Mapping<2, 2> &mapping)
83 {
84 // Outer boundary vertices counter clockwise
85 // and hole vertices clockwise ordered
86 auto boundaries =
88
89 CGAL::Polygon_2<KernelType> outer_boundary;
90 std::vector<CGAL::Polygon_2<KernelType>> holes;
91 holes.reserve(boundaries.size() - 1);
92
93 for (const auto &boundary : boundaries)
94 {
95 // Add vertices of current boundary to polygon
96 CGAL::Polygon_2<KernelType> current_polygon;
97 for (const auto &vertices : boundary)
98 {
99 current_polygon.push_back(
100 dealii_point_to_cgal_point<CGAL::Point_2<KernelType>, 2>(
101 vertices.second));
102 }
103 // Decide if outer boundary or hole
104 if (current_polygon.is_counterclockwise_oriented())
105 {
106 outer_boundary = current_polygon;
107 }
108 else
109 {
110 holes.push_back(current_polygon);
111 }
112 }
113 return CGAL::Polygon_with_holes_2<KernelType>(outer_boundary,
114 holes.begin(),
115 holes.end());
116 }
117
118
119 template <typename KernelType>
120 CGAL::Polygon_with_holes_2<KernelType>
122 const CGAL::Polygon_2<KernelType> &boundary_outside,
123 const std::vector<CGAL::Polygon_2<KernelType>> &boundary_holes)
124 {
125 return CGAL::Polygon_with_holes_2<KernelType>(boundary_outside,
126 boundary_holes.begin(),
127 boundary_holes.end());
128 }
129
130
131
132 template <typename KernelType>
133 std::vector<CGAL::Polygon_with_holes_2<KernelType>>
135 const CGAL::Polygon_with_holes_2<KernelType> &polygon_1,
136 const CGAL::Polygon_with_holes_2<KernelType> &polygon_2,
137 const BooleanOperation &boolean_operation)
138 {
139 Assert(!(boolean_operation == BooleanOperation::compute_corefinement),
140 ExcMessage("Corefinement has no usecase for 2D polygons"));
141
142 std::vector<CGAL::Polygon_with_holes_2<KernelType>> polygon_out;
143
144 if (boolean_operation == BooleanOperation::compute_intersection)
145 {
146 CGAL::intersection(polygon_1,
147 polygon_2,
148 std::back_inserter(polygon_out));
149 }
150 else if (boolean_operation == BooleanOperation::compute_difference)
151 {
152 CGAL::difference(polygon_1, polygon_2, std::back_inserter(polygon_out));
153 }
154 else if (boolean_operation == BooleanOperation::compute_union)
155 {
156 polygon_out.resize(1);
157 CGAL::join(polygon_1, polygon_2, polygon_out[0]);
158 }
159
160 return polygon_out;
161 }
162
163// Explicit instantiations.
164//
165// We don't build the instantiations.inst file if deal.II isn't
166// configured with CGAL, but doxygen doesn't know that and tries to
167// find that file anyway for parsing -- which then of course it fails
168// on. So exclude the following from doxygen consideration.
169# ifndef DOXYGEN
170# include "cgal/polygon.inst"
171# endif
172
173} // namespace CGALWrappers
174# endif
175
176
178
179#endif
virtual boost::container::small_vector< Point< spacedim >, ReferenceCells::max_n_vertices< dim >() > get_vertices(const typename Triangulation< dim, spacedim >::cell_iterator &cell) const
ReferenceCell reference_cell() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:40
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:41
#define DEAL_II_ASSERT_UNREACHABLE()
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
TriaIterator< CellAccessor< dim, spacedim > > cell_iterator
Definition tria.h:1557
MappingQ< dim, spacedim > StaticMappingQ1< dim, spacedim >::mapping
Definition mapping_q1.h:104
std::vector< CGAL::Polygon_with_holes_2< KernelType > > compute_boolean_operation(const CGAL::Polygon_with_holes_2< KernelType > &polygon_1, const CGAL::Polygon_with_holes_2< KernelType > &polygon_2, const BooleanOperation &boolean_operation)
CGAL::Polygon_2< KernelType > dealii_cell_to_cgal_polygon(const typename Triangulation< 2, 2 >::cell_iterator &cell, const Mapping< 2, 2 > &mapping)
CGAL::Polygon_with_holes_2< KernelType > dealii_tria_to_cgal_polygon(const Triangulation< 2, 2 > &tria, const Mapping< 2, 2 > &mapping)
CGAL::Polygon_with_holes_2< KernelType > polygon_to_polygon_with_holes(const CGAL::Polygon_2< KernelType > &boundary_outside, const std::vector< CGAL::Polygon_2< KernelType > > &boundary_holes={})
std::vector< std::vector< std::pair< unsigned int, Point< spacedim > > > > extract_ordered_boundary_vertices(const Triangulation< dim, spacedim > &tria, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< dim, spacedim >()))
constexpr ReferenceCell Triangle
constexpr ReferenceCell Quadrilateral