aimsalgo  5.1.2
Neuroimaging image processing
meshvoronoi_d.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_DISTANCEMAP_MESHVORONOI_D_H
36 #define AIMS_DISTANCEMAP_MESHVORONOI_D_H
37 
38 
43 #include <aims/mesh/surface.h>
44 #include <float.h>
45 #include <stack>
46 #include <set>
47 
48 namespace aims
49 {
50  namespace meshdistance
51  {
52 
53 // Compute a geodesic voronoi diagram (dist = MAX_FLOAT,object=true ) of objects defined in inittex.
54 // The background has the label 0 and the objects have a positive label
55 // The distance can be euclidean geodesic (connexity=false) or just the connexity of the triangulation(connexity=true)
56 // This function can as well be used for dilation(object=true)/erosion(object=false)
57 // using the dist parameter as the size of the structuring element
58 template <class T>
60  const Texture<T> & inittex,
61  const T & Back, const T & For,
62  float dist, bool connexity, bool object)
63 {
64  Texture<T> vor = inittex;
65  Texture<float> tex;
66  const std::vector<Point3df> & vert = mesh.vertex();
67  const std::vector< AimsVector<uint,3> > & poly = mesh.polygon();
68  unsigned i, n = vert.size();
69 
70  ASSERT( inittex.nItem() == n );
71 
72  tex.reserve(n);
73 
74  // neighbours map
75 
76  std::map<unsigned, std::set<unsigned> > neighbours;
77  unsigned v1, v2, v3;
78 
79  for( i=0; i<poly.size(); ++i )
80  {
81  v1 = poly[i][0];
82  v2 = poly[i][1];
83  v3 = poly[i][2];
84  if(inittex.item(v1)!=For
85  && inittex.item(v2)!=For)
86  {
87  neighbours[v1].insert( v2 );
88  neighbours[v2].insert( v1 );
89  }
90  if(inittex.item(v1)!=For
91  && inittex.item(v3)!=For)
92  {
93  neighbours[v1].insert( v3 );
94  neighbours[v3].insert( v1 );
95  }
96  if(inittex.item(v2)!=For
97  && inittex.item(v3)!=For)
98  {
99  neighbours[v2].insert( v3 );
100  neighbours[v3].insert( v2 );
101  }
102  }
103 
104 
105  // init distance texture
106  if (object)
107  {
108  for( i=0; i<n; ++i )
109  {
110  if( inittex.item(i) == Back )
111  tex.push_back( FLT_MAX );
112  else
113  if( inittex.item(i) == For )
115  else
116  tex.push_back( 0 );
117  }
118  }
119  else
120  {
121  for( i=0; i<n; ++i )
122  {
123  if( inittex.item(i) == Back )
124  tex.push_back( 0 );
125  else
126  if( inittex.item(i) == For )
128  else
129  tex.push_back( FLT_MAX );
130  }
131  }
132 
133  std::multimap<float,unsigned> front1, front2;
134  std::multimap<float,unsigned> *cfront = &front1;
135  std::multimap<float,unsigned>::iterator iv;
136  std::set<unsigned> neigh;
137  std::set<unsigned>::iterator in, fn,iu,eu = neigh.end();
138  float d=0, d2, l;
139  Point3df pos;
140 
141  // init first front
142  for( i=0; i<n; ++i )
143  if( tex.item(i) == 0 )
144  front1.insert( std::pair<float,unsigned>( 0 , i ) );
145 
146 
147  //TimeTexture<T> tvor;
148  // loop until current front is empty
149  while( cfront->size() > 0 && d < dist )
150  {
151  iv=cfront->begin();
152  i = (*iv).second;
153  d = (*iv).first;
154  cfront->erase(iv);
155  neigh.erase(i);
156  for( in=neighbours[i].begin(), fn=neighbours[i].end(); in!=fn; ++in )
157  {
158  d2 = tex.item( *in );
159  pos = vert[i] - vert[*in];
160  if (!connexity)
161  l = sqrt( pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2] );
162  else
163  l = 1;
164 
165  if( d2 > d + l && vor.item( *in ) != For)
166  {
167  iu = neigh.find(*in);
168  if (iu != eu)
169  {
170  std::pair<std::multimap<float,unsigned>::iterator,
171  std::multimap<float,unsigned>::iterator>
172  im = cfront->equal_range(tex.item( *in ) );
173  std::multimap<float,unsigned>::iterator em;
174  ++im.second;
175  for (em = im.first; em != im.second && em->second != *in ;
176  ++em) {}
177  cfront->erase(em);
178  neigh.erase(iu);
179  }
180  tex.item( *in ) = d + l;
181  neigh.insert( *in );
182  cfront->insert(std::pair<float,unsigned>( tex.item( *in ), *in ) );
183  vor.item( *in ) = vor.item( i );
184 
185  }
186  }
187 
188  }
189 
190  return (vor);
191 
192 }
193 
194  }
195 }
196 
197 
198 #endif
#define ASSERT(EX)
const std::vector< Point3df > & vertex() const
const std::vector< AimsVector< uint, D > > & polygon() const
iterator begin()
void reserve(size_t size)
size_t nItem() const
const T & item(int n) const
void push_back(const T &item)
Texture< T > MeshVoronoiT(const AimsSurface< 3, Void > &mesh, const Texture< T > &inittex, const T &Back, const T &For, float dist, bool connexity, bool object)
Compute a geodesic voronoi diagram (dist = MAX_FLOAT,object=true ) of objects defined in inittex.
Definition: meshvoronoi_d.h:59
const short MESHDISTANCE_FORBIDDEN
global variable...
Definition: meshvoronoi.h:84