ug4
soma_projector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018: G-CSC, Goethe University Frankfurt
3  * Author: Stephan Grein
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):
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_cylinder_soma_projector
34 #define __H__UG_cylinder_soma_projector
35 
37 #include "refinement_projector.h"
40 
41 namespace ug {
43 
55 private:
59 protected:
61  {
62  // call base class method
65  }
66 public:
68  m_center (0, 0, 0),
69  m_axis (0, 0, 1),
70  m_soma (0, 0, 0),
71  m_somaRad(0)
72  {
74  if (!GlobalAttachments::is_declared("npSurfParams"))
75  GlobalAttachments::declare_attachment<NPSurfParam>("npSurfParams", true);
76 
77  }
78 
79  SomaProjector (const vector3& center,
80  const vector3& axis,
81  const vector3& soma,
82  const number& rad) :
83 
84  m_center (center),
85  m_axis (axis),
86  m_soma (soma),
87  m_somaRad(rad)
88  {
90  if (!GlobalAttachments::is_declared("npSurfParams"))
91  GlobalAttachments::declare_attachment<NPSurfParam>("npSurfParams", true);
92  }
93 
94 public:
97  const vector3& center,
98  const vector3& axis,
99  const vector3& soma,
100  const number& rad) :
102  m_center (center),
103  m_axis (axis),
104  m_soma (soma),
105  m_somaRad(rad)
106  {
108  }
109 
110 
111 public:
112  virtual ~SomaProjector () {}
113 
115  virtual number new_vertex(Vertex* vrt, Edge* parent)
116  {
117  return perform_projection(vrt, parent);
118  }
119 
121  virtual number new_vertex(Vertex* vrt, Face* parent)
122  {
123  return perform_projection(vrt, parent);
124  }
125 
127  virtual number new_vertex(Vertex* vrt, Volume* parent)
128  {
129  return perform_projection(vrt, parent);
130  }
131 
132 private:
136  if (!GlobalAttachments::is_declared("npSurfParams"))
137  GlobalAttachments::declare_attachment<NPSurfParam>("npSurfParams", true);
138  }
139 
140 
144 
145  Grid& grid = this->geometry()->grid();
146 
147  // make sure attachment was declared
148  UG_COND_THROW(!GlobalAttachments::is_declared("npSurfParams"), "GlobalAttachment 'npSurfParams' not declared.");
149  m_aSurfParams = GlobalAttachments::attachment<Attachment<NeuriteProjector::SurfaceParams> >("npSurfParams");
150 
151  // make sure surfaceParams attachment is attached
153  "No surface parameter attachment for neurite projector attached to grid.");
154  m_aaSurfParams.access(grid, m_aSurfParams);
155 
156  // handle attachment values also on higher grid levels (if required)
157  MultiGrid* mg = dynamic_cast<MultiGrid*>(&grid);
158  if (mg) {
159  SmartPtr<MultiGrid> spMG(mg);
160 
161  // never destroy the grid from here - we did not create it
162  ++(*spMG.refcount_ptr());
163 
164  // attach copy attachment handler to propagate attachment to higher levels
165  m_cah.set_attachment(m_aSurfParams);
166  m_cah.set_grid(spMG);
167  }
168  }
169 
170 
171  template <class TElem>
172  number perform_projection(Vertex* vrt, TElem* parent)
173  {
174  // calculate the new position by linear interpolation and project that point
175  // onto the cylinder.
176  typename TElem::ConstVertexArray vrts = parent->vertices();
177  size_t numVrts = parent->num_vertices();
178 
179  if(numVrts == 0){
180  set_pos(vrt, vector3(0, 0, 0));
181  return 1;
182  }
183 
184  number avDist = 0;
185  vector3 parentCenter (0, 0, 0);
186 
187  for(size_t i = 0; i < numVrts; ++i){
188  vector3 p = pos(vrts[i]);
189  avDist += DistancePointToRay(p, m_center, m_axis);
190  parentCenter += p;
191  }
192 
193  avDist /= (number)numVrts;
194  VecScale(parentCenter, parentCenter, 1. / (number)numVrts);
195 
196  vector3 proj, v;
197  ProjectPointToRay(proj, parentCenter, m_center, m_axis);
198  VecSubtract(v, parentCenter, proj);
199  number len = VecLength(v);
200  if(len > SMALL * avDist){ // if avDist is very small, len may be small, too
201  VecScale(v, v, avDist/len);
202  proj += v;
203  set_pos(vrt, proj);
204  }
205  else
206  set_pos(vrt, parentCenter);
207 
208  if (vertex_at_soma_surf(vrt, parent))
209  project_to_soma_surface(vrt, parent);
210 
211  return 1;
212  }
213 
214  template <class TElem>
215  bool vertex_at_soma_surf(Vertex* vrt, TElem* parent) {
216  size_t numSomaVerts = 0;
217  size_t nVrt = parent->num_vertices();
218  for (size_t i = 0; i < nVrt; ++i) {
219  if(m_aaSurfParams[parent->vertex(i)].axial == -1.0) {
220  numSomaVerts++;
221  }
222  }
223  return numSomaVerts >= 2;
224  }
225 
226  template <class TElem>
227  void project_to_soma_surface(Vertex* vrt, TElem* parent) {
229  vector3 v0 = this->pos(vrt);
231  vector3 P;
232  VecSubtract(P, v0, m_soma);
234  number Plength = VecLength(P);
236  vector3 Q = P;
237  VecScale(Q, P, m_somaRad / Plength);
239  vector3 R;
240  VecAdd(R, Q, m_soma);
242  set_pos(vrt, R);
244  m_aaSurfParams[vrt].axial = -1;
245  }
246 
247 
249 
250  template <class Archive>
251  void serialize( Archive& ar, const unsigned int version)
252  {
253  ar & make_nvp("center", m_center);
254  ar & make_nvp("axis", m_axis);
255  ar & make_nvp("soma", m_soma);;
256  ar & make_nvp("somaRad", m_somaRad);;
258  }
259 
264 };
265 
266 }// end of namespace
267 
268 #endif //__H__UG_cylinder_soma_projector
parameterString p
#define UG_EMPTY_BASE_CLASS_SERIALIZATION(clsDerived, clsBase)
Definition: boost_serialization.h:51
int * refcount_ptr() const
WARNING: this method is DANGEROUS!
Definition: smart_pointer.h:263
A generic specialization of IAttachment.
Definition: attachment_pipe.h:263
handler for attachments in a multi-grid
Definition: copy_attachment_handler.h:103
Base-class for edges.
Definition: grid_base_objects.h:397
Faces are 2-dimensional objects.
Definition: grid_base_objects.h:510
static bool is_declared(const std::string &name)
Definition: global_attachments.h:111
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
bool has_vertex_attachment(IAttachment &attachment)
Definition: grid.h:798
Definition: multi_grid.h:72
Adjusts vertex coordinates during refinement.
Definition: refinement_projector.h:55
void set_pos(Vertex *v, const vector3 &p)
Definition: refinement_projector.h:157
virtual void set_geometry(SPIGeometry3d geometry)
Definition: refinement_projector.h:76
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: soma_projector.h:54
virtual number new_vertex(Vertex *vrt, Edge *parent)
called when a new vertex was created from an old edge.
Definition: soma_projector.h:115
void project_to_soma_surface(Vertex *vrt, TElem *parent)
Definition: soma_projector.h:227
void attach_surf_params()
helper method to attach surface parameters and do a consistency check
Definition: soma_projector.h:142
SomaProjector(SPIGeometry3d geometry, const vector3 &center, const vector3 &axis, const vector3 &soma, const number &rad)
Definition: soma_projector.h:96
virtual void set_geometry(SPIGeometry3d geometry)
Definition: soma_projector.h:60
virtual ~SomaProjector()
Definition: soma_projector.h:112
CopyAttachmentHandler< Vertex, Attachment< NeuriteProjector::SurfaceParams > > m_cah
Definition: soma_projector.h:58
SomaProjector()
Definition: soma_projector.h:67
vector3 m_center
Definition: soma_projector.h:260
Grid::VertexAttachmentAccessor< Attachment< NeuriteProjector::SurfaceParams > > m_aaSurfParams
Definition: soma_projector.h:57
virtual number new_vertex(Vertex *vrt, Face *parent)
called when a new vertex was created from an old face.
Definition: soma_projector.h:121
void check_attachment()
check if global attachment was declared
Definition: soma_projector.h:134
void serialize(Archive &ar, const unsigned int version)
Definition: soma_projector.h:251
Attachment< NeuriteProjector::SurfaceParams > m_aSurfParams
Definition: soma_projector.h:56
bool vertex_at_soma_surf(Vertex *vrt, TElem *parent)
Definition: soma_projector.h:215
vector3 m_soma
Definition: soma_projector.h:262
SomaProjector(const vector3 &center, const vector3 &axis, const vector3 &soma, const number &rad)
Definition: soma_projector.h:79
vector3 m_axis
Definition: soma_projector.h:261
friend class boost::serialization::access
Definition: soma_projector.h:248
virtual number new_vertex(Vertex *vrt, Volume *parent)
called when a new vertex was created from an old volume.
Definition: soma_projector.h:127
number m_somaRad
Definition: soma_projector.h:263
number perform_projection(Vertex *vrt, TElem *parent)
Definition: soma_projector.h:172
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
Volumes are 3-dimensional objects.
Definition: grid_base_objects.h:754
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
double number
Definition: types.h:124
number DistancePointToRay(const vector_t &v, const vector_t &from, const vector_t &dir)
calculates the distance of a point to a ray
Definition: math_util_impl.hpp:225
number ProjectPointToRay(vector_t &vOut, const vector_t &v, const vector_t &from, const vector_t &dir)
finds the projection of v onto the ray defined by from and dir
Definition: math_util_impl.hpp:160
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
void VecAdd(vector_t &vOut, const vector_t &v1, const vector_t &v2)
adds two MathVector<N>s and stores the result in a third one
Definition: math_vector_functions_common_impl.hpp:185
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