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