ug4
sphere_projector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG_sphere_projector_new
34 #define __H__UG_sphere_projector_new
35 
36 #include "refinement_projector.h"
37 
38 namespace ug{
39 
41 
50 public:
52  m_center (0, 0, 0),
53  m_radius (-1),
55  {}
56 
58  m_center (center),
59  m_radius (-1),
61  {}
62 
64  number radius) :
65  m_center (center),
66  m_radius (radius),
68  {}
69 
71  number radius,
72  number influenceRadius) :
73  m_center (center),
74  m_radius (radius),
75  m_influenceRadius (influenceRadius)
76  {}
77 
80  const vector3& center) :
82  m_center (center),
83  m_radius (-1),
85  {}
86 
89  const vector3& center,
90  number radius) :
92  m_center (center),
93  m_radius (radius),
95  {}
96 
98  template <class TGeomProvider>
99  SphereProjector (const TGeomProvider& geometry,
100  const vector3& center,
101  number radius,
102  number influenceRadius) :
104  m_center (center),
105  m_radius (radius),
106  m_influenceRadius (influenceRadius)
107  {}
108 
109  virtual ~SphereProjector () {}
110 
112  const vector3& center () const {return m_center;}
113 
115  number radius () const {return m_radius;}
116 
117  void set_influence_radius (number influenceRadius) {m_influenceRadius = influenceRadius;}
119 
121  virtual number new_vertex(Vertex* vrt, Edge* parent)
122  {
123  return perform_projection(vrt, parent);
124  }
125 
127  virtual number new_vertex(Vertex* vrt, Face* parent)
128  {
129  return perform_projection(vrt, parent);
130  }
131 
133  virtual number new_vertex(Vertex* vrt, Volume* parent)
134  {
135  return perform_projection(vrt, parent);
136  }
137 
138 private:
139 
140  template <class TElem>
141  number perform_projection(Vertex* vrt, TElem* parent)
142  {
143  // first calculate the average distance of corners of the parent to the
144  // parents center
145  typename TElem::ConstVertexArray vrts = parent->vertices();
146  size_t numVrts = parent->num_vertices();
147 
148  if(numVrts == 0){
149  set_pos(vrt, vector3(0, 0, 0));
150  return 1;
151  }
152 
153  number avDist = 0;
154  vector3 parentCenter(0, 0, 0);
155 
156  for(size_t i = 0; i < numVrts; ++i){
157  vector3 p = pos(vrts[i]);
158  avDist += VecDistance(p, m_center);
159  parentCenter += p;
160  }
161 
162  avDist /= (number)numVrts;
163  VecScale(parentCenter, parentCenter, 1. / (number)numVrts);
164 
165  // calculate projection
166  vector3 proj;
167  VecSubtract(proj, parentCenter, m_center);
168  number len = VecLength(proj);
169  if(len > SMALL * avDist){ // if avDist is very small, len may be small, too
170  VecScale(proj, proj, avDist / len);
171  proj += m_center;
172  set_pos(vrt, proj);
173  }
174  else
175  set_pos(vrt, parentCenter);
176 
177  if(m_influenceRadius > 0) {
179  const number dist = m_radius - m_influenceRadius;
180  if(dist > 0)
181  return clip<number>((len - m_influenceRadius) / dist, 0, 1);
182  return len > m_radius ? 1 : 0;
183  }
184  else if(m_radius >= 0){
185  const number dist = m_influenceRadius - m_radius;
186  if(dist > 0)
187  return clip<number>(1 - (len - m_radius) / dist, 0, 1);
188  return len < m_radius ? 1 : 0;
189  }
190  else
191  return clip<number>(1 - len / m_influenceRadius, 0, 1);
192  }
193 
194  return 1;
195  }
196 
197 
199 
200  template <class Archive>
201  void serialize( Archive& ar, const unsigned int version)
202  {
203  ar & make_nvp("center", m_center);
204  ar & make_nvp("radius", m_radius);
205  ar & make_nvp("influence radius", m_influenceRadius);
207  }
208 
212 };
213 
214 
215 }// end of namespace
216 
217 #endif //__H__UG_sphere_projector_new
parameterString p
#define UG_EMPTY_BASE_CLASS_SERIALIZATION(clsDerived, clsBase)
Definition: boost_serialization.h:51
Base-class for edges.
Definition: grid_base_objects.h:397
Faces are 2-dimensional objects.
Definition: grid_base_objects.h:510
Adjusts vertex coordinates during refinement.
Definition: refinement_projector.h:55
void set_pos(Vertex *v, const vector3 &p)
Definition: refinement_projector.h:157
vector3 pos(Vertex *v) const
Definition: refinement_projector.h:149
virtual SPIGeometry3d geometry() const
Definition: refinement_projector.h:81
Projects new vertices onto a sphere during refinement.
Definition: sphere_projector.h:49
SphereProjector(const vector3 &center)
Definition: sphere_projector.h:57
number perform_projection(Vertex *vrt, TElem *parent)
Definition: sphere_projector.h:141
SphereProjector(SPIGeometry3d geometry, const vector3 &center)
Definition: sphere_projector.h:79
virtual number new_vertex(Vertex *vrt, Face *parent)
called when a new vertex was created from an old face.
Definition: sphere_projector.h:127
number m_influenceRadius
Definition: sphere_projector.h:211
virtual number new_vertex(Vertex *vrt, Edge *parent)
called when a new vertex was created from an old edge.
Definition: sphere_projector.h:121
vector3 m_center
Definition: sphere_projector.h:209
void set_radius(number radius)
Definition: sphere_projector.h:114
SphereProjector(const TGeomProvider &geometry, const vector3 &center, number radius, number influenceRadius)
Definition: sphere_projector.h:99
const vector3 & center() const
Definition: sphere_projector.h:112
SphereProjector()
Definition: sphere_projector.h:51
SphereProjector(SPIGeometry3d geometry, const vector3 &center, number radius)
Definition: sphere_projector.h:88
number radius() const
Definition: sphere_projector.h:115
virtual number new_vertex(Vertex *vrt, Volume *parent)
called when a new vertex was created from an old volume.
Definition: sphere_projector.h:133
void set_center(const vector3 &center)
Definition: sphere_projector.h:111
number m_radius
Definition: sphere_projector.h:210
SphereProjector(const vector3 &center, number radius, number influenceRadius)
Definition: sphere_projector.h:70
void set_influence_radius(number influenceRadius)
Definition: sphere_projector.h:117
virtual ~SphereProjector()
Definition: sphere_projector.h:109
friend class boost::serialization::access
Definition: sphere_projector.h:198
number influence_radius() const
Definition: sphere_projector.h:118
void serialize(Archive &ar, const unsigned int version)
Definition: sphere_projector.h:201
SphereProjector(const vector3 &center, number radius)
Definition: sphere_projector.h:63
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
Volumes are 3-dimensional objects.
Definition: grid_base_objects.h:754
double number
Definition: types.h:124
vector_t::value_type VecLength(const vector_t &v)
returns the length of v. Slower than VecLengthSq.
Definition: math_vector_functions_common_impl.hpp:341
void VecSubtract(vector_t &vOut, const vector_t &v1, const vector_t &v2)
subtracts v2 from v1 and stores the result in a vOut
Definition: math_vector_functions_common_impl.hpp:226
MathVector< 3, number > vector3
a 3d vector
Definition: ugmath_types.h:72
vector_t::value_type VecDistance(const vector_t &v1, const vector_t &v2)
returns the distance of two vector_ts.
Definition: math_vector_functions_common_impl.hpp:375
void VecScale(vector_t &vOut, const vector_t &v, typename vector_t::value_type s)
scales a MathVector<N>
Definition: math_vector_functions_common_impl.hpp:252
the ug namespace
const number SMALL
Definition: math_constants.h:41