FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
cgal.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5#pragma once
6
8#include <kernel/geometry/mesh_part.hpp>
9#include <kernel/shape.hpp>
10#include <kernel/util/string.hpp>
11#include <kernel/util/tiny_algebra.hpp>
12
13#if defined(FEAT_HAVE_CGAL) || defined(DOXYGEN)
14
15#include <cstddef>
16
17namespace FEAT
18{
19 namespace Geometry
20 {
21
22 enum class CGALFileMode
23 {
24 fm_off = 0,
25 fm_obj
26 };
27
36 using CGALFeature = std::vector<std::uint32_t>;
37
43 using CGALFeatureNetwork = std::vector<CGALFeature>;
44
55 template<typename DT_>
57 {
58 public:
59 typedef DT_ DataType;
62
63 private:
64 void * _cgal_data;
65
67 void _parse_mesh(std::istream & file, CGALFileMode file_mode);
68
69 public:
71 CGALWrapper(const CGALWrapper&) = delete;
72 CGALWrapper& operator=(const CGALWrapper&) = delete;
73 CGALWrapper(CGALWrapper&&) noexcept;
74 CGALWrapper& operator=(CGALWrapper&& other) noexcept;
75 virtual ~CGALWrapper();
76
78 explicit CGALWrapper(const String & filename, CGALFileMode file_mode);
79
81 explicit CGALWrapper(std::istream & file, CGALFileMode file_mode);
82
94 explicit CGALWrapper(const std::vector<PointType>& vertices, const std::vector<std::array<Index, 3>>& faces);
95
97 bool point_inside(DataType x, DataType y, DataType z) const;
98
100 bool point_not_outside(DataType x, DataType y, DataType z) const;
101
103 DataType squared_distance(DataType x, DataType y, DataType z) const;
104
106 DataType squared_distance_to_feature(const CGALFeature& f, const PointType& p) const;
107
109 PointType point(std::uint32_t idx) const;
110
113
115 PointType closest_point(DataType x, DataType y, DataType z) const;
116
117 PointType closest_point(const PointType& point, PointType& primitive_grad) const;
118
121
123 CGALFeatureNetwork detect_features(DataType critical_angle);
124
126 bool intersects_line(const PointType& a, const PointType& b) const;
127
131 void transform(const TransformMatrix& trafo_mat, const PointType& translation, DataType scale = DataType(1));
132
134 std::size_t bytes() const;
135
136
137 private:
142
143 }; // class CGALWrapper<typename DT_>
144
145 template<typename MeshType_>
146 static CGALWrapper<typename MeshType_::CoordType> cgal_wrapper_from_mesh(const MeshType_& mesh, const MeshPart<MeshType_>& part)
147 {
148 using MeshType = MeshType_;
149 using DT = typename MeshType::CoordType;
150 using ShapeType = typename MeshType::ShapeType;
151 using FaceType = typename Shape::FaceTraits<ShapeType, 2>::ShapeType;
152 using PointType = typename CGALWrapper<DT>::PointType;
153
154 constexpr int vertices_per_face = Shape::FaceTraits<FaceType, 0>::count;
155
156 static_assert(ShapeType::dimension == 3);
157
158 std::vector<PointType> vertices;
159 vertices.reserve(part.get_num_entities(0));
160
161 // Prepare vertices
162 const auto& vertex_set = mesh.get_vertex_set();
163 const TargetSet& vts = part.template get_target_set<0>();
164 for(Index i(0); i < part.get_num_entities(0); i++)
165 {
166 vertices.push_back(vertex_set[vts[i]]);
167 }
168
169 // Prepare faces
170 std::vector<std::array<Index, 3>> faces;
171
172 const auto& v_at_f = mesh.template get_index_set<2, 0>();
173 const TargetSet& fts = part.template get_target_set<2>();
174 const Index num_mesh_faces = part.get_num_entities(2);
175
176 if constexpr(std::is_same_v<ShapeType, Shape::Hexahedron>)
177 {
178 // Quad faces get split into triangles
179 faces.reserve(2 * num_mesh_faces);
180 }
181 if constexpr(std::is_same_v<ShapeType, Shape::Tetrahedron>)
182 {
183 // Triangle faces do not need to be split
184 faces.reserve(num_mesh_faces);
185 }
186
187 std::array<Index, vertices_per_face> vs;
188 for(Index i(0); i < num_mesh_faces; i++)
189 {
190 for(int j(0); j < vertices_per_face; j++)
191 {
192 // Get mesh index
193 Index v = v_at_f(fts[i], j);
194
195 // Find corresponding index in vertex target set
196 for(Index k(0); k < vts.get_num_entities(); k++)
197 {
198 if(vts[k] == v)
199 {
200 vs[j] = k;
201 break;
202 }
203 }
204 }
205
206 if constexpr(std::is_same_v<ShapeType, Shape::Hexahedron>)
207 {
208 faces.push_back({vs[0], vs[1], vs[2]});
209 faces.push_back({vs[3], vs[2], vs[1]});
210 }
211 if constexpr(std::is_same_v<ShapeType, Shape::Tetrahedron>)
212 {
213 faces.push_back({vs[0], vs[1], vs[2]});
214 }
215 }
216
217 return CGALWrapper<DT>(vertices, faces);
218 }
219 } // namespace Geometry
220} // namespace FEAT
221#endif //defined(FEAT_HAVE_CGAL) || defined(DOXYGEN)
FEAT Kernel base header.
Wrapper for the CGAL Library.
Definition: cgal.hpp:57
void _init_wrapper()
initializes tree and inside tester with already initialized polyhedron
void transform(const TransformMatrix &trafo_mat, const PointType &translation, DataType scale=DataType(1))
bool point_inside(DataType x, DataType y, DataType z) const
Check whether a point is inside the Polyhedron defined at objects' construction.
bool intersects_line(const PointType &a, const PointType &b) const
tests whether the cgal mesh intersects with the line segment a->b
void _delete_tree()
Delete tree, which also requires to delete the inside tester.
DataType squared_distance(DataType x, DataType y, DataType z) const
Returns the minimun squared distance between the query point and all input primitives defined at obje...
std::size_t bytes() const
Returns the size in bytes.
CGALFeatureNetwork detect_features(DataType critical_angle)
Returns a vector of all feature vertices defined at objects' construction.
CGALWrapper(const CGALWrapper &)=delete
rule of five
PointType closest_point(const PointType &point) const
Returns the nearest point regarding on all input primitives defined at objects' construction.
DataType squared_distance_to_feature(const CGALFeature &f, const PointType &p) const
Returns the minimum squared distance between the query point and the given feature.
bool point_not_outside(DataType x, DataType y, DataType z) const
Check whether a point is inside or on the boundary of the Polyhedron defined at objects' construction...
PointType point(std::uint32_t idx) const
Returns the point of the surface mesh with the given index.
void _parse_mesh(std::istream &file, CGALFileMode file_mode)
read in stream in prescibed file format and preprocess search tree for in/out test
PointType closest_point_on_feature(const CGALFeature &f, const PointType &p) const
Returns the closest point closest to p on the given feature.
Class template for partial meshes.
Definition: mesh_part.hpp:90
Target set class.
Definition: target_set.hpp:27
Index get_num_entities() const
Returns the number of entities.
Definition: target_set.hpp:92
String class implementation.
Definition: string.hpp:46
Tiny Matrix class template.
Tiny Vector class template.
std::vector< CGALFeature > CGALFeatureNetwork
A FeatureNetwork is a list of features.
Definition: cgal.hpp:43
std::vector< std::uint32_t > CGALFeature
A feature is an edge-path on a surface mesh, stored as a list of vertex indices.
Definition: cgal.hpp:36
@ other
generic/other permutation strategy
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.
Face traits tag struct template.
Definition: shape.hpp:106