ug4
neurite_projector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016: G-CSC, Goethe University Frankfurt
3  * Author: Markus Breit
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  * Created on: 2016-12-19
33  */
34 
35 #ifndef UG__LIB_GRID__REFINEMENT__PROJECTORS__NEURITE_PROJECTOR_H
36 #define UG__LIB_GRID__REFINEMENT__PROJECTORS__NEURITE_PROJECTOR_H
37 
38 #include "common/types.h"
40 
41 #include <boost/serialization/split_member.hpp> // for separate load/save methods
42 
43 namespace ug {
44 
46 : public RefinementProjector
47 {
48  public:
51 
52  virtual ~NeuriteProjector();
53 
54  virtual void set_geometry(SPIGeometry3d geometry);
55 
57  virtual number new_vertex(Vertex* vrt, Vertex* parent);
58 
60  virtual number new_vertex(Vertex* vrt, Edge* parent);
61 
63  virtual number new_vertex(Vertex* vrt, Face* parent);
64 
66  virtual number new_vertex(Vertex* vrt, Volume* parent);
67 
69  void project(Vertex* vrt);
70 
72  void direction_at_grid_object(vector3& dirOut, GridObject* o) const;
73 
74 
75  protected:
76  void attach_surf_params();
77 
78  public:
79  struct Section
80  {
81  Section() : endParam(0) {} // constructor for serialization
82  Section(number _endParam) // constructor for search with CompareSections
83  : endParam(_endParam) {}
84 
86 
91 
92  template <class Archive>
93  void serialize(Archive& ar, const unsigned int version)
94  {
95  ar & endParam;
96  for (size_t i = 0; i < 4; ++i)
97  {
98  ar & splineParamsX[i];
99  ar & splineParamsY[i];
100  ar & splineParamsZ[i];
101  ar & splineParamsR[i];
102  }
103  }
104  };
105 
106  struct BranchingPoint;
108  {
109  BranchingRegion() : t(0.0), bp(SPNULL) {} // constructor for serialization
110  BranchingRegion(number _t) // constructor for search with CompareBranchingPointEnds
111  : t(_t) {}
112 
115 
118 
119  template<class Archive>
120  void save(Archive & ar, const unsigned int version) const
121  {
122  ar << t;
123 
124  bool owns_bp = bp->vRegions[0] == this;
125  ar << owns_bp;
126  if (owns_bp)
127  ar << *bp;
128  }
129 
130  template<class Archive>
131  void load(Archive & ar, const unsigned int version)
132  {
133  // invoke serialization of the base class
134  ar >> t;
135 
136  bool owns_bp;
137  ar >> owns_bp;
138  if (owns_bp)
139  {
140  bp = make_sp(new BranchingPoint());
141  ar >> *bp;
142  }
143  }
144 
145  BOOST_SERIALIZATION_SPLIT_MEMBER()
146  };
147 
148 
150  {
151  std::vector<uint32_t> vNid;
152  std::vector<BranchingRegion*> vRegions;
153 
154  template <class Archive>
155  void serialize(Archive& ar, const unsigned int version)
156  {
157  // please note: We could simply use
158  // ar & vNid
159  // but this somehow takes up a huge number of template recursions
160  size_t sz = vNid.size(); // this does not hurt in the load-case
161  ar & sz;
162  vNid.resize(sz); // this does not hurt in the save-case
163  for (size_t i = 0; i < sz; ++i)
164  ar & vNid[i];
165  }
166  };
167 
168  struct SomaPoint {
172  };
173 
180 
181  template <class Archive>
182  void serialize(Archive& ar, const unsigned int version) {
183  ar & radius;
184  ar & t;
185  }
186 
187  template<class Archive>
188  void load(Archive & ar, const unsigned int version)
189  {
190  ar >> radius;
191  ar >> t;
192  }
193 
195  : radius(radius),
196  t(t),
197  bp(center)
198  {}
199 
201  : radius(-1),
202  t(t)
203  {}
204 
206  : radius(-1),
207  t(-1)
208  {}
209  };
210 
214  struct Mapping {
219  };
220 
221  struct Neurite
222  {
224  std::vector<Section> vSec;
225  std::vector<BranchingRegion> vBR;
226  std::vector<Section> vSomaSec;
227  std::vector<SomaBranchingRegion> vSBR;
228 
229  template <class Archive>
230  void serialize(Archive& ar, const unsigned int version)
231  {
233  ar & refDir;
234 
235  size_t sz = vSec.size();
236  ar & sz;
237  vSec.resize(sz);
238  for (size_t i = 0; i < sz; ++i)
239  ar & vSec[i];
240 
241  sz = vBR.size();
242  ar & sz;
243  vBR.resize(sz);
244  for (size_t i = 0; i < sz; ++i)
245  ar & vBR[i];
246 
248  sz = vSomaSec.size();
249  ar & sz;
250  vSomaSec.resize(sz);
251  for (size_t i = 0; i < sz; ++i)
252  ar & vSomaSec[i];
253 
254  sz = vSBR.size();
255  ar & sz;
256  vSBR.resize(sz);
257  for (size_t i = 0; i < sz; ++i) {
258  ar & vSBR[i];
259  }
260  }
261  };
262 
264  {
279  float axial;
280  float angular;
281  float radial;
282  };
283 
284 
285  public:
287  {
288  bool operator()(const Section& a, const Section& b)
289  {return a.endParam < b.endParam;}
290  };
292  {
293  bool operator()(const BranchingRegion& a, const BranchingRegion& b)
294  {return a.t < b.t;}
295  };
296 
297  // helper struct for branching point calculations
299  {
302  std::vector<Section>::const_iterator sec_start;
304  };
305 
306  void debug_neurites() const;
307 
310  return a.t < b.t;
311  }
312  };
313 
314  public:
315  std::vector<Neurite>& neurites();
316  std::vector<SomaBranchingRegion>& somata();
317  const Neurite& neurite(uint32_t nid) const;
320  const std::vector<std::pair<number, number> >& quadrature_points() const;
321  void average_pos_from_parent(vector3& posOut, const IVertexGroup* const parent) const;
322  vector3 position(Vertex* vrt) const;
323 
325  (
326  uint32_t nid,
327  size_t brInd,
328  number numberOfRadii = 5.0
329  ) const;
330 
332  (
333  const SomaBranchingRegion& sr,
334  size_t numRadii,
335  size_t nid,
336  Vertex* vrt
337  ) const;
338 
340  (
341  const SomaBranchingRegion& sr,
342  size_t nid,
343  Vertex* vrt
344  ) const;
345 
346  void print_surface_params(const Vertex* const v) const;
347 
348  protected:
349  std::vector<Section>::const_iterator get_section_iterator(uint32_t nid, float t) const;
350  std::vector<NeuriteProjector::Section>::const_iterator get_soma_section_iterator(uint32_t nid, float t) const;
351 
352  void prepare_quadrature();
353 
354  void average_params
355  (
356  uint32_t& neuriteID,
357  float& t,
358  float& angle,
359  float& radius,
360  const IVertexGroup* parent
361  ) const;
362 
363  number push_into_place(Vertex* vrt, const IVertexGroup* parent);
364 
365  private:
368 
376  std::vector<Neurite> m_vNeurites;
377 
378  std::vector<std::pair<number, number> > m_qPoints;
379 
387  template <class TElem>
389  (
390  const TElem* elem,
391  const ug::vector3& center,
392  const number radius
393  ) const
394  {
396  vector3 c = CalculateCenter(elem, aaPos);
397  vector3 diff;
398  VecSubtract(diff, c, center);
399  VecPow(diff, diff, 2.0);
400  number s = diff.x() + diff.y() + diff.z();
401  return (s <= (sq(radius) + SMALL));
402  }
403 
405  template<class Archive>
406  void save(Archive & ar, const unsigned int version) const
407  {
409 
410  // only write if data is to be written
412  {
413  // neurites
414  size_t sz = m_vNeurites.size();
415  ar << sz;
416  for (size_t i = 0; i < sz; ++i) {
417  ar << m_vNeurites[i];
418  }
419  }
420 
421  // do not do anything otherwise
423  {}
424  }
425 
426  template<class Archive>
427  void load(Archive & ar, const unsigned int version)
428  {
430 
431  // neurites
432  size_t sz;
433  ar >> sz;
434  m_vNeurites.resize(sz);
435  for (size_t i = 0; i < sz; ++i) {
436  ar >> m_vNeurites[i];
437  }
438 
439  // reconstruct uninitialized pointers in branching points/ranges
440  size_t nNeurites = m_vNeurites.size();
441  for (size_t n = 0; n < nNeurites; ++n)
442  {
444  size_t nBR = neurite.vBR.size();
445  for (size_t b = 0; b < nBR; ++b)
446  {
447  BranchingRegion& br = neurite.vBR[b];
449  if (bp.valid() && !bp->vRegions.size())
450  {
451  size_t nReg = bp->vNid.size();
452  bp->vRegions.resize(nReg);
453  bp->vRegions[0] = &br;
454  for (size_t r = 1; r < nReg; ++r)
455  {
456  UG_ASSERT(m_vNeurites[bp->vNid[r]].vBR.size(),
457  "No branching region in neurite where there should be one.")
458  bp->vRegions[r] = &m_vNeurites[bp->vNid[r]].vBR[0];
459  bp->vRegions[r]->bp = bp;
460  }
461  }
462  }
463  }
464  // debug_neurites();
465  }
466  BOOST_SERIALIZATION_SPLIT_MEMBER()
467 };
468 
469 // DO NOT CHANGE LINES BELOW! Needed for serialization! //
470 std::ostream& operator<<(std::ostream& os, const NeuriteProjector::SurfaceParams& surfParams);
471 std::istream& operator>>(std::istream& in, NeuriteProjector::SurfaceParams& surfParams);
473 std::ostream& operator<<(std::ostream& os, const NeuriteProjector::Mapping& mapping);
474 std::istream& operator>>(std::istream& in, NeuriteProjector::Mapping& mapping);
476 } // namespace ug
477 
478 #endif // UG__LIB_GRID__REFINEMENT__PROJECTORS__NEURITE_PROJECTOR_H
parameterString s
#define UG_EMPTY_BASE_CLASS_SERIALIZATION(clsDerived, clsBase)
Definition: boost_serialization.h:51
bool valid() const
returns true if the pointer is valid, false if not.
Definition: smart_pointer.h:206
A generic specialization of IAttachment.
Definition: attachment_pipe.h:263
Base-class for edges.
Definition: grid_base_objects.h:397
Faces are 2-dimensional objects.
Definition: grid_base_objects.h:510
The base class for all geometric objects, such as vertices, edges, faces, volumes,...
Definition: grid_base_objects.h:157
Base class for all classes which consist of a group of vertices.
Definition: grid_base_objects.h:317
Definition: neurite_projector.h:47
bool IsElementInsideSphere(const TElem *elem, const ug::vector3 &center, const number radius) const
check if an element is inside a sphere
Definition: neurite_projector.h:389
void load(Archive &ar, const unsigned int version)
Definition: neurite_projector.h:427
Grid::VertexAttachmentAccessor< Attachment< SurfaceParams > > & surface_params_accessor()
Definition: neurite_projector.cpp:257
void average_params(uint32_t &neuriteID, float &t, float &angle, float &radius, const IVertexGroup *parent) const
Definition: neurite_projector.cpp:547
number axial_range_around_soma_region(const SomaBranchingRegion &sr, size_t numRadii, size_t nid, Vertex *vrt) const
Definition: neurite_projector.cpp:1622
void debug_neurites() const
Definition: neurite_projector.cpp:209
void print_surface_params(const Vertex *const v) const
Definition: neurite_projector.cpp:897
void save(Archive &ar, const unsigned int version) const
Definition: neurite_projector.h:406
std::vector< std::pair< number, number > > m_qPoints
for quadrature when projecting within branching points
Definition: neurite_projector.h:378
const Neurite & neurite(uint32_t nid) const
Definition: neurite_projector.cpp:248
Attachment< SurfaceParams > m_aSurfParams
Definition: neurite_projector.h:366
std::vector< Neurite > & neurites()
Definition: neurite_projector.cpp:242
number push_into_place(Vertex *vrt, const IVertexGroup *parent)
Definition: neurite_projector.cpp:1664
Grid::VertexAttachmentAccessor< Attachment< SurfaceParams > > m_aaSurfParams
Definition: neurite_projector.h:367
virtual number new_vertex(Vertex *vrt, Vertex *parent)
called when a new vertex was created from an old vertex
Definition: neurite_projector.cpp:98
std::vector< Section >::const_iterator get_section_iterator(uint32_t nid, float t) const
Definition: neurite_projector.cpp:277
virtual ~NeuriteProjector()
Definition: neurite_projector.cpp:87
std::vector< NeuriteProjector::Section >::const_iterator get_soma_section_iterator(uint32_t nid, float t) const
Definition: neurite_projector.cpp:291
vector3 position(Vertex *vrt) const
Definition: neurite_projector.cpp:848
number axial_range_around_branching_region(uint32_t nid, size_t brInd, number numberOfRadii=5.0) const
Definition: neurite_projector.cpp:854
void average_pos_from_parent(vector3 &posOut, const IVertexGroup *const parent) const
Definition: neurite_projector.cpp:793
void direction_at_grid_object(vector3 &dirOut, GridObject *o) const
spline direction at some grid object
Definition: neurite_projector.cpp:134
void prepare_quadrature()
Definition: neurite_projector.cpp:308
void attach_surf_params()
Definition: neurite_projector.cpp:195
std::vector< Neurite > m_vNeurites
storage for cubic splines: Vector of sorted vectors of sections; each inner vector represents a singl...
Definition: neurite_projector.h:376
virtual void set_geometry(SPIGeometry3d geometry)
Definition: neurite_projector.cpp:90
friend class boost::serialization::access
Definition: neurite_projector.h:404
NeuriteProjector()
Definition: neurite_projector.cpp:61
const std::vector< std::pair< number, number > > & quadrature_points() const
Definition: neurite_projector.cpp:270
std::vector< SomaBranchingRegion > & somata()
void project(Vertex *vrt)
project a vertex to its model position
Definition: neurite_projector.cpp:128
bool is_in_axial_range_around_soma_region(const SomaBranchingRegion &sr, size_t nid, Vertex *vrt) const
Definition: neurite_projector.cpp:1652
Adjusts vertex coordinates during refinement.
Definition: refinement_projector.h:55
virtual SPIGeometry3d geometry() const
Definition: refinement_projector.h:81
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
Volumes are 3-dimensional objects.
Definition: grid_base_objects.h:754
std::ostream & operator<<(std::ostream &outStream, const ug::MathMatrix< 2, 2 > &m)
Definition: math_matrix.cpp:38
const NullSmartPtr SPNULL
The equivalent to NULL for smart pointers.
Definition: smart_pointer.h:90
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
ugtypes::uint32_t uint32
Definition: types.h:116
double number
Definition: types.h:124
TNumber sq(TNumber val)
returns the square of a value (val*val)
Definition: math_util_impl.hpp:91
void CalculateCenter(vector_t &centerOut, const vector_t *pointSet, size_t numPoints)
calculates the center of a point-set
Definition: math_util_impl.hpp:98
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
void VecPow(vector_t &vOut, const vector_t &v1, typename vector_t::value_type s)
component-wise exponentiation of a vector
Definition: math_vector_functions_common_impl.hpp:239
the ug namespace
DECLARE_ATTACHMENT_INFO_TRAITS(Attachment< bool >, "bool")
const number SMALL
Definition: math_constants.h:41
std::istream & operator>>(std::istream &in, NeuriteProjector::SurfaceParams &surfParams)
Definition: neurite_projector.cpp:1814
@ AT_GUI
Definition: boost_serialization.h:66
@ AT_DATA
Definition: boost_serialization.h:65
APosition aPosition("position", true)
The standard 3d position type.
Definition: common_attachments.h:84
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition: smart_pointer.h:836
Provides custom information for different archives.
Definition: boost_serialization.h:71
Definition: neurite_projector.h:299
number end
Definition: neurite_projector.h:301
std::vector< Section >::const_iterator sec_start
Definition: neurite_projector.h:302
const SomaBranchingRegion * sr
Definition: neurite_projector.h:303
number start
Definition: neurite_projector.h:300
Definition: neurite_projector.h:150
void serialize(Archive &ar, const unsigned int version)
Definition: neurite_projector.h:155
std::vector< BranchingRegion * > vRegions
Definition: neurite_projector.h:152
std::vector< uint32_t > vNid
Definition: neurite_projector.h:151
Definition: neurite_projector.h:108
void load(Archive &ar, const unsigned int version)
Definition: neurite_projector.h:131
void save(Archive &ar, const unsigned int version) const
Definition: neurite_projector.h:120
number t
the axial parameter where other neurite(s) branch off
Definition: neurite_projector.h:114
BranchingRegion(number _t)
Definition: neurite_projector.h:110
BranchingRegion()
Definition: neurite_projector.h:109
SmartPtr< BranchingPoint > bp
pointer to whole branching point
Definition: neurite_projector.h:117
Definition: neurite_projector.h:292
bool operator()(const BranchingRegion &a, const BranchingRegion &b)
Definition: neurite_projector.h:293
Definition: neurite_projector.h:287
bool operator()(const Section &a, const Section &b)
Definition: neurite_projector.h:288
Definition: neurite_projector.h:308
bool operator()(const SomaBranchingRegion &a, const SomaBranchingRegion &b)
Definition: neurite_projector.h:309
Mapping of model to surface vertices.
Definition: neurite_projector.h:214
vector3 v2
start vertex of edge
Definition: neurite_projector.h:217
vector3 v1
TODO: Mapping should store unique indices of 1d vertices from SWC file.
Definition: neurite_projector.h:216
number lambda
end vertex of edge
Definition: neurite_projector.h:218
Definition: neurite_projector.h:222
vector3 refDir
Definition: neurite_projector.h:223
std::vector< BranchingRegion > vBR
Definition: neurite_projector.h:225
std::vector< Section > vSomaSec
Definition: neurite_projector.h:226
std::vector< Section > vSec
Definition: neurite_projector.h:224
void serialize(Archive &ar, const unsigned int version)
Definition: neurite_projector.h:230
std::vector< SomaBranchingRegion > vSBR
Definition: neurite_projector.h:227
Definition: neurite_projector.h:80
number splineParamsZ[4]
Definition: neurite_projector.h:89
Section()
Definition: neurite_projector.h:81
number endParam
Definition: neurite_projector.h:85
number splineParamsX[4]
Definition: neurite_projector.h:87
void serialize(Archive &ar, const unsigned int version)
Definition: neurite_projector.h:93
Section(number _endParam)
Definition: neurite_projector.h:82
number splineParamsY[4]
Definition: neurite_projector.h:88
number splineParamsR[4]
Definition: neurite_projector.h:90
Definition: neurite_projector.h:174
SomaBranchingRegion(const vector3 &center, number radius, number t)
Definition: neurite_projector.h:194
void serialize(Archive &ar, const unsigned int version)
Definition: neurite_projector.h:182
void load(Archive &ar, const unsigned int version)
Definition: neurite_projector.h:188
vector3 center
Definition: neurite_projector.h:177
vector3 bp
Definition: neurite_projector.h:179
number radius
Definition: neurite_projector.h:176
SomaBranchingRegion()
Definition: neurite_projector.h:205
SmartPtr< SomaPoint > somaPt
Definition: neurite_projector.h:175
number t
Definition: neurite_projector.h:178
SomaBranchingRegion(number t)
Definition: neurite_projector.h:200
Definition: neurite_projector.h:168
SomaPoint(const vector3 &soma, number radius)
Definition: neurite_projector.h:171
number radius
Definition: neurite_projector.h:170
vector3 soma
Definition: neurite_projector.h:169
Definition: neurite_projector.h:264
float angular
Definition: neurite_projector.h:280
float axial
Definition: neurite_projector.h:279
float radial
Definition: neurite_projector.h:281
uint32 neuriteID
Neurite ID a vertex belongs to.
Definition: neurite_projector.h:278