FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
sphere.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
6#pragma once
7
8#include <kernel/geometry/atlas/chart.hpp>
9
10namespace FEAT
11{
12 namespace Geometry
13 {
14 namespace Atlas
15 {
18 {
20 static constexpr bool is_explicit = false;
22 static constexpr bool is_implicit = true;
24 static constexpr int world_dim = 3;
26 static constexpr int param_dim = 1;
27 };
28
41 template<typename Mesh_>
42 class Sphere :
43 public ChartCRTP<Sphere<Mesh_>, Mesh_, SphereTraits>
44 {
45 public:
54
55 protected:
60
61 public:
71 explicit Sphere(CoordType mid_x, CoordType mid_y, CoordType mid_z, CoordType radius) :
72 _radius(radius)
73 {
74 XASSERTM(radius > CoordType(0), "invalid Sphere radius");
75 _midpoint[0] = mid_x;
76 _midpoint[1] = mid_y;
77 _midpoint[2] = mid_z;
78 }
79
81 virtual String get_type() const override
82 {
83 return "sphere";
84 }
85
87 virtual void transform(const WorldPoint& origin, const WorldPoint& angles, const WorldPoint& offset) override
88 {
89 // create rotation matrix
91 rot.set_rotation_3d(angles(0), angles(1), angles(2));
92
93 // transform midpoint
94 WorldPoint tmp;
95 tmp = _midpoint - origin;
96 _midpoint.set_mat_vec_mult(rot, tmp) += offset;
97 }
98
106 void project_point(WorldPoint& point) const
107 {
108 WorldPoint grad_dist(point - _midpoint);
109 CoordType distance(grad_dist.norm_euclid());
110
111 if(distance < Math::eps<CoordType>())
112 {
113 grad_dist(0) = _radius;
114 point += grad_dist;
115 }
116 else
117 {
118 point = _midpoint + (_radius / distance)*grad_dist;
119 }
120 }
121
132 void project_meshpart(Mesh_& mesh, const MeshPart<Mesh_>& meshpart) const
133 {
134 auto& vtx = mesh.get_vertex_set();
135 const auto& target_vtx = meshpart.template get_target_set<0>();
136
137 for(Index i(0); i < meshpart.get_num_entities(0); ++i)
138 {
139 project_point(reinterpret_cast<WorldPoint&>(vtx[target_vtx[i]]));
140 }
141 }
142
145 {
146 return Math::abs(compute_signed_dist(point));
147 }
148
150 CoordType compute_dist(const WorldPoint& point, WorldPoint& grad_dist) const
151 {
152 WorldPoint projected(point);
153 project_point(projected);
154
155 CoordType my_dist(_radius - (point - _midpoint).norm_euclid());
156
157 grad_dist = (point - projected);
158 grad_dist.normalize();
159
160 return Math::abs(my_dist);
161 }
162
165 {
166 return (point - _midpoint).norm_euclid() - _radius;
167 }
168
170 CoordType compute_signed_dist(const WorldPoint& point, WorldPoint& grad_dist) const
171 {
172 WorldPoint projected(point);
173 project_point(projected);
174
175 CoordType my_dist(_radius - (point - _midpoint).norm_euclid());
176
177 grad_dist = (point - projected);
178 grad_dist.normalize();
179 grad_dist *= Math::signum(my_dist);
180
181 return my_dist;
182 }
183
185 virtual void write(std::ostream& os, const String& sindent) const override
186 {
187 os << sindent << "<Sphere";
188 os << " radius=\"" << this->_radius << "\"";
189 os << " midpoint=\"" << this->_midpoint[0] << " " << this->_midpoint[1] << " " << this->_midpoint[2] << "\"";
190 os << " />\n";
191 }
192 };
193
194 template<typename Mesh_, typename ChartReturn_ = ChartBase<Mesh_>>
196 public Xml::MarkupParser
197 {
198 private:
199 typedef Sphere<Mesh_> ChartType;
200 typedef typename ChartType::CoordType CoordType;
201 std::unique_ptr<ChartReturn_>& _chart;
202
203 public:
204 explicit SphereChartParser(std::unique_ptr<ChartReturn_>& chart) :
205 _chart(chart)
206 {
207 }
208
209 virtual bool attribs(std::map<String,bool>& attrs) const override
210 {
211 attrs.emplace("radius", true);
212 attrs.emplace("midpoint", true);
213 return true;
214 }
215
216 virtual void create(
217 int iline,
218 const String& sline,
219 const String&,
220 const std::map<String, String>& attrs,
221 bool) override
222 {
223 CoordType radius = CoordType(0);
224 CoordType mid_x = CoordType(0);
225 CoordType mid_y = CoordType(0);
226 CoordType mid_z = CoordType(0);
227
228 // try to parse the radius
229 if(!attrs.find("radius")->second.parse(radius))
230 throw Xml::GrammarError(iline, sline, "Failed to parse sphere radius");
231 if(radius < CoordType(1E-5))
232 throw Xml::GrammarError(iline, sline, "Invalid sphere radius");
233
234 // try to parse midpoint
235 std::deque<String> mids = attrs.find("midpoint")->second.split_by_whitespaces();
236 if(mids.size() != std::size_t(3))
237 throw Xml::GrammarError(iline, sline, "Invalid sphere midpoint string");
238 if(!mids.front().parse(mid_x) || !mids.at(1).parse(mid_y) || !mids.back().parse(mid_z))
239 throw Xml::GrammarError(iline, sline, "Failed to parse sphere midpoint");
240
241 _chart.reset(new ChartType(mid_x, mid_y, mid_z, radius));
242 }
243
244 virtual void close(int, const String&) override
245 {
246 }
247
248 virtual bool content(int, const String&) override
249 {
250 return false;
251 }
252
253 virtual std::shared_ptr<Xml::MarkupParser> markup(int, const String&, const String&) override
254 {
255 return nullptr;
256 }
257 }; // class SphereChartParser<...>
258 } // namespace Atlas
259 } // namespace Geometry
260} // namespace FEAT
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Chart CRTP base-class template.
Definition: chart.hpp:354
virtual void create(int iline, const String &sline, const String &, const std::map< String, String > &attrs, bool) override
Creates this markup parser node.
Definition: sphere.hpp:216
virtual void close(int, const String &) override
Closes this markup parser node.
Definition: sphere.hpp:244
virtual bool content(int, const String &) override
Called to process a content line.
Definition: sphere.hpp:248
virtual std::shared_ptr< Xml::MarkupParser > markup(int, const String &, const String &) override
Called to process a child markup node.
Definition: sphere.hpp:253
virtual bool attribs(std::map< String, bool > &attrs) const override
Specifies the mandatory and optional attributes.
Definition: sphere.hpp:209
Sphere chart class template.
Definition: sphere.hpp:44
BaseClass::WorldPoint WorldPoint
Vector type for world points, aka image points.
Definition: sphere.hpp:51
CoordType compute_signed_dist(const WorldPoint &point) const
Computes the signed distance of a point to this chart.
Definition: sphere.hpp:164
CoordType _radius
the sphere's radius
Definition: sphere.hpp:59
CoordType compute_dist(const WorldPoint &point) const
Computes the distance of a point to this chart.
Definition: sphere.hpp:144
WorldPoint _midpoint
the sphere's midpoint
Definition: sphere.hpp:57
CoordType compute_signed_dist(const WorldPoint &point, WorldPoint &grad_dist) const
Computes the signed distance of a point to this chart.
Definition: sphere.hpp:170
ChartCRTP< Sphere< Mesh_ >, Mesh_, SphereTraits > BaseClass
CRTP base class.
Definition: sphere.hpp:47
void project_point(WorldPoint &point) const
Projects a single world point.
Definition: sphere.hpp:106
void project_meshpart(Mesh_ &mesh, const MeshPart< Mesh_ > &meshpart) const
Projects all mesh points identified by a meshpart.
Definition: sphere.hpp:132
virtual String get_type() const override
Writes the type as String.
Definition: sphere.hpp:81
Sphere(CoordType mid_x, CoordType mid_y, CoordType mid_z, CoordType radius)
Constructor.
Definition: sphere.hpp:71
virtual void write(std::ostream &os, const String &sindent) const override
Writes the Chart into a stream in XML format.
Definition: sphere.hpp:185
BaseClass::ParamPoint ParamPoint
Vector type for parameter points, aka domain points.
Definition: sphere.hpp:53
CoordType compute_dist(const WorldPoint &point, WorldPoint &grad_dist) const
Computes the distance of a point to this chart.
Definition: sphere.hpp:150
virtual void transform(const WorldPoint &origin, const WorldPoint &angles, const WorldPoint &offset) override
Definition: sphere.hpp:87
BaseClass::CoordType CoordType
Floating point type.
Definition: sphere.hpp:49
Class template for partial meshes.
Definition: mesh_part.hpp:90
Index get_num_entities(int dim) const
Returns the number of entities.
Definition: mesh_part.hpp:311
String class implementation.
Definition: string.hpp:46
Tiny Matrix class template.
CUDA_HOST_DEVICE Matrix & set_rotation_3d(T_ yaw, T_ pitch, T_ roll)
Sets this matrix to a 3D yaw-pitch-roll rotation matrix.
Tiny Vector class template.
Xml grammar error class.
XML Markup Parser interface.
T_ abs(T_ x)
Returns the absolute value.
Definition: math.hpp:275
T_ signum(T_ x)
Returns the sign of a value.
Definition: math.hpp:250
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.
Sphere chart traits.
Definition: sphere.hpp:18
static constexpr bool is_explicit
we support explicit map
Definition: sphere.hpp:20
static constexpr int param_dim
we have 1D parameters
Definition: sphere.hpp:26
static constexpr bool is_implicit
we support implicit projection
Definition: sphere.hpp:22
static constexpr int world_dim
this is a 2D object
Definition: sphere.hpp:24