aimsalgo  5.0.5
Neuroimaging image processing
meshdistance_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 
37 #include <map>
38 #include <set>
39 #include <float.h>
40 
41 
42 template<class T>
44  const Texture<T> & inittex,
45  bool allowUnreached )
46 {
47  Texture<float> tex;
48  const std::vector<Point3df> & vert = mesh.vertex();
49  const std::vector< AimsVector<uint,3> > & poly = mesh.polygon();
50  unsigned i, n = vert.size();
51 
52  ASSERT( inittex.nItem() == n );
53  tex.reserve( n );
54 
55  // neighbours map
56 
57  std::map<unsigned, std::set<unsigned> > neighbours;
58  unsigned v1, v2, v3;
59 
60  for( i=0; i<poly.size(); ++i )
61  {
62  v1 = poly[i][0];
63  v2 = poly[i][1];
64  v3 = poly[i][2];
65  if(inittex.item(v1)!=MESHDISTANCE_FORBIDDEN
66  && inittex.item(v2)!=MESHDISTANCE_FORBIDDEN)
67  {
68  neighbours[v1].insert( v2 );
69  neighbours[v2].insert( v1 );
70  }
71  if(inittex.item(v1)!=MESHDISTANCE_FORBIDDEN
72  && inittex.item(v3)!=MESHDISTANCE_FORBIDDEN)
73  {
74  neighbours[v1].insert( v3 );
75  neighbours[v3].insert( v1 );
76  }
77  if(inittex.item(v2)!=MESHDISTANCE_FORBIDDEN
78  && inittex.item(v3)!=MESHDISTANCE_FORBIDDEN)
79  {
80  neighbours[v2].insert( v3 );
81  neighbours[v3].insert( v2 );
82  }
83  }
84 
85  // init texture
86 
87  for( i=0; i<n; ++i )
88  {
89  if( inittex.item(i) == 0 )
90  tex.push_back( FLT_MAX );
91  else if( inittex.item(i) == MESHDISTANCE_FORBIDDEN )
93  else
94  tex.push_back( 0 );
95  }
96 
97  std::multimap<float,unsigned> front1, front2;
98  std::multimap<float,unsigned> *cfront = &front1, *nfront = &front2, *tmpf;
99  std::multimap<float,unsigned>::iterator iv, fv;
100  std::set<unsigned> neigh;
101  std::set<unsigned>::iterator in, fn;
102  float d, d2, l;
103  Point3df pos;
104 
105  // init first front
106 
107  for( i=0; i<n; ++i )
108  if( tex.item(i) == 0 )
109  front1.insert( std::pair<float,unsigned>( 0, i ) );
110 
111  // loop until current front is empty
112 
113  while( cfront->size() > 0 )
114  {
115  nfront->clear();
116  neigh.clear();
117 
118  for( iv=cfront->begin(), fv=cfront->end(); iv!=fv; ++iv )
119  {
120  i = (*iv).second;
121  d = (*iv).first;
122  for( in=neighbours[i].begin(), fn=neighbours[i].end(); in!=fn; ++in )
123  {
124  d2 = tex.item( *in );
125  pos = vert[i] - vert[*in];
126  l = sqrt( pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2] );
127  if( d2 > d + l )
128  {
129  tex.item( *in ) = d + l;
130  neigh.insert( *in );
131  }
132  }
133  }
134 
135  for( in=neigh.begin(), fn=neigh.end(); in!=fn; ++in )
136  nfront->insert( std::pair<float,unsigned>( tex.item( *in ), *in ) );
137 
138  tmpf = cfront;
139  cfront = nfront;
140  nfront = tmpf;
141  }
142 
143 
144  if( !allowUnreached )
145  for( i=0; i<n; ++i )
146  if( tex.item(i) == FLT_MAX )
147  tex.item(i) = MESHDISTANCE_UNREACHED;
148 
149  return( tex );
150 }
151 
const short MESHDISTANCE_FORBIDDEN
global variable...
Definition: meshvoronoi.h:85
size_t nItem() const
const T & item(int n) const
Texture< float > MeshDistance(const AimsSurface< 3, Void > &mesh, const Texture< T > &inittex, bool allowUnreached)
const std::vector< Point3df > & vertex() const
void reserve(size_t size)
const std::vector< AimsVector< uint, D > > & polygon() const
#define ASSERT(EX)
const short MESHDISTANCE_UNREACHED
Definition: meshvoronoi.h:86
void push_back(const T &item)