cortical_surface  5.0.5
linkPath.h
Go to the documentation of this file.
1 #ifndef AIMS_CONNECT_PATH_H
2 #define AIMS_CONNECT_PATH_H
3 
4 
5 #include <cstdlib>
6 #include <aims/mesh/texture.h>
8 #include <aims/mesh/surfacegen.h>
10 
11 namespace aims
12 {
13 
14 // When a line on a mesh is disconnected in two connected components, this class
15 // connect it in a single component with value val
16 
17 template<typename T> class ConnectMeshPath
18 {
19 
20 
21 public:
22 
23  ConnectMeshPath(AimsSurfaceTriangle mesh, TimeTexture<T> tex, T label1, T label2) :
24  _mesh(mesh), _tex(tex), _l1(label1), _l2(label2) {}
25  Texture<T> run(T val);
26 
27 private:
28  AimsSurfaceTriangle _mesh;
29  TimeTexture<T> _tex;
30  T _l1;
31  T _l2;
32 };
33 
34 
35 template<typename T> Texture<T> ConnectMeshPath<T>::run(T val)
36 {
37  std::vector<std::set<uint> > neigh = SurfaceManip::surfaceNeighbours(_mesh);
38  std::set<uint>::iterator neighIt;
39 
40  std::set<uint> set1, set2;
41  std::set<uint> end1, end2;
42  std::set<uint>::iterator setIt, setIt2;
43  uint ext1, ext2;
44 
45  MeshPointDistance distance(_mesh);
46 
47  int ns=_tex[0].nItem(), i;
48 
49  Texture<T> texOut(ns);
50  std::vector<Point3df> vert=_mesh.vertex();
51  Point3df pt1, pt2;
52 
53  std::cout << "Connecting Paths" << std::endl;
54  std::cout << "\tBuilding the two sets" << std::endl;
55  // building the two sets to connect
56  for (i=0; i<ns; i++)
57  {
58  if (fabs((double)(_tex[0].item(i) - _l1)) < 0.001)
59  set1.insert(i);
60  else if (fabs((double)(_tex[0].item(i) - _l2)) < 0.001)
61  set2.insert(i);
62  }
63  // finding extremities of both sets
64  std::cout << "\tFinding their extremities" << std::endl;
65 
66  for (setIt=set1.begin(); setIt!=set1.end(); ++setIt)
67  {
68  std::set<uint> voisin=neigh[*setIt];
69  std::set<uint>::iterator vIt;
70  int count=0;
71  for (vIt=voisin.begin(); vIt!=voisin.end(); ++vIt)
72  if (set1.find(*vIt) != set1.end())
73  count++;
74  if (count < 2)
75  end1.insert(*setIt);
76  }
77  for (setIt=set2.begin(); setIt!=set2.end(); ++setIt)
78  {
79  std::set<uint> voisin=neigh[*setIt];
80  std::set<uint>::iterator vIt;
81  int count=0;
82  for (vIt=voisin.begin(); vIt!=voisin.end(); ++vIt)
83  if (set2.find(*vIt) != set2.end())
84  count++;
85  if (count < 2)
86  end2.insert(*setIt);
87  }
88 
89  // the two extremities to join are the closest to eachother
90  // at the moment I use the 3D distance
91 
92  float dist, distMin=10000.0;
93  for (setIt=end1.begin(); setIt!=end1.end(); ++setIt)
94  for (setIt2=end2.begin(); setIt2!=end2.end(); ++setIt2)
95  {
96  pt1=vert[*setIt]; pt2=vert[*setIt2];
97  dist=sqrt( (pt1[0]-pt2[0])*(pt1[0]-pt2[0])
98  + (pt1[1]-pt2[1])*(pt1[1]-pt2[1])
99  + (pt1[2]-pt2[2])*(pt1[2]-pt2[2]) );
100  if (dist<distMin)
101  {
102  distMin=dist;
103  ext1=*setIt;
104  ext2=*setIt2;
105  }
106  }
107 
108  // now let's join ext1 and ext2;
109  // this time we have no other choice than using geodesic distance
110  std::cout << "\tJoining them" << std::endl;
111  std::cout << "\t ns=" << ns << std::endl;
112  for (i=0; i<ns; i++)
113  {
114  if ( (set1.find(i) != set1.end()) || (set2.find(i) != set2.end()) )
115  texOut.item(i)=val;
116  else
117  texOut.item(i)=0;
118  }
119  std::cout << "\tFinding actual shortest path" << std::endl;
120 
121  std::cout << "\t" << ext1 << " -> " << ext2 << std::endl;
122  i=ext1;
123  uint j=0;
124  while (i != ext2)
125  {
126  std::cout << "\t\t" << i << std::endl;
127  std::set<uint> voisin=neigh[i];
128  std::set<uint>::iterator vIt;
129  distMin=10000.0;
130  std::cout << "\t\tj=" << j << std::endl;
131  std::cout << "\t\tChecking out neighbours" << std::endl;
132  for (vIt=voisin.begin(); vIt!=voisin.end(); ++vIt)
133  {
134  std::cout << "\t\tDistance de " << *vIt << " à " << ext2 << std::endl;
135  dist=distance.compute(ext2, *vIt); // APPELER ICI LA DISTANCE GEODESIQUE ENTRE *vIt et ext2
136  std::cout << "\t\t\t" << dist << std::endl;
137  if (dist<distMin)
138  {
139  distMin=dist;
140  j=*vIt;
141  }
142  }
143  std::cout << "\t\tj=" << j << std::endl;
144  texOut.item(j)=val;
145  i=j;
146 
147  }
148  std::cout << "\tReturning texOut" << std::endl;
149  return(texOut);
150 
151 }
152 
153 } //fin du namespace
154 
155 #endif
AIMSDATA_API AimsTimeSurface< 3, Void > AimsSurfaceTriangle
const T & item(int n) const
Texture< T > run(T val)
Definition: linkPath.h:35
static std::vector< std::set< uint > > surfaceNeighbours(const AimsSurface< D, T > &surf)
float compute(uint p1, uint p2)
unsigned int uint
ConnectMeshPath(AimsSurfaceTriangle mesh, TimeTexture< T > tex, T label1, T label2)
Definition: linkPath.h:23