cortical_surface  5.0.5
shortestPath.h
Go to the documentation of this file.
1 #ifndef AIMS_SHORTESTPATH_H
2 #define AIMS_SHORTESTPATH_H
3 
4 #include <cstdlib>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <aims/mesh/texture.h>
9 #include <aims/mesh/curv.h>
12 #include <aims/io/reader.h>
13 #include <aims/io/writer.h>
14 #include <limits>
15 
16 namespace aims
17 {
18 
19 
20 // cette classe calcule le plus court chemin entre deux points d'un maillage,
21 // contraint à passer dans un ensemble de noeuds défini par une texture et une valeur
22 // L'algo utilise une carte de distance par fast marching sur la surface et un backtracking tout con
23 // a l'intérieur de l'ensemble de noeuds.
24 
25 template<typename Val> class GraphPath
26 {
27 
28  public:
29 
30  GraphPath() {longueur =0;}
31  float getLongueur(TimeTexture<Val> & tex, AimsSurfaceTriangle & initmesh, Val value, int dep, int arr);
32  TimeTexture<Val> process(TimeTexture<Val> & tex, AimsSurfaceTriangle & initmesh, Val value, int dep, int arr);
33 
34  private:
35 
36  float longueur;
37 };
38 
39 //------------------------------------------------------------------------------
40 // calcul du plus court chemin
41 //------------------------------------------------------------------------------
42 
43 template<typename Val>
44 TimeTexture<Val> GraphPath<Val>::process(TimeTexture<Val> & tex, AimsSurfaceTriangle & initmesh, Val value, int dep, int arr)
45 {
46 
47 
48  uint ns=initmesh.vertex().size();
49 
50 
51 // std::cerr << "Shortest Path In" << std::endl;
52 
53  // ici on construit la carte de distance avec le fast marching
54  // [...]
55 
56  // ici on utilise finalement le bon vieil algo de distanceMap d'aimsalgo
57 
58 
59 
60  TimeTexture<float> distanceMap;
61  Texture<short> initMap(ns);
62  for (uint l=0; l<ns; l++)
63  {
64  if (tex[0].item(l)==value)
65  initMap.item(l)=(short) 0; // domaine de calcul de la distance
66  else initMap.item(l)=(short) -1; // domaine interdit
67  }
68  initMap.item(dep)=(short) 100; // point de départ
69 
70  distanceMap[0]=meshdistance::MeshDistance( initmesh[0] , initMap , true);
71  std::map<uint,float> res;
72  uint i=0;
73  for (uint l=0; l<ns; l++)
74  {
75  res[l]=(float) distanceMap[0].item(l);
76  }
77 
78 
79  std::vector<std::set<uint> > neigh=SurfaceManip::surfaceNeighbours(initmesh);
80  TimeTexture<Val> result(1,ns);
81  i=arr;
82  uint j, inext, iprevious=i;
83  double distmin;
84  result[0].item(i)=(Val) value;
85  distmin=10000.0;
86  TimeTexture<Val> debugTex(tex);
87 
88 
89  // ici on fait le backtracking
90 
91  do
92  {
93  std::set<uint> vois=neigh[i];
94  std::set<uint>::iterator itVois=vois.begin();
95  inext=i; // distmin=10000.0;
96  for (; itVois != vois.end(); ++itVois)
97  {
98  j=(*itVois);
99  if ((tex[0].item(j)==value) && (j != iprevious))
100  {
101  if (res[j]<distmin)
102  {
103  inext=j; distmin=res[j];
104  }
105  }
106  }
107  debugTex[0].item(inext)=value*2;
108  if (inext==i)
109  {
110  std::cerr << "ShortestPath->GraphPath<Val>::process : problem. There is no path between start and end included in the provided set" << std::endl;
111  Writer<TimeTexture<Val> > debugW("debugTex.tex");
112  debugTex[0].item(dep)=value*3;
113  debugTex[0].item(arr)=value*3;
114  debugW.write(debugTex);
115  exit(EXIT_FAILURE);
116 
117  }
118  longueur+=(initmesh.vertex()[i] - initmesh.vertex()[inext]).norm();
119  iprevious=i; i=inext;
120  result[0].item(i)=(Val) value;
121  }
122  while (i!=dep);
123 // std::cerr << "Shortest Path Out" << std::endl;
124 
125  return(result);
126 
127 }
128 
129 //------------------------------------------------------------------------------
130 // renvoie seulement la longueur du plus court chemin
131 //------------------------------------------------------------------------------
132 
133 template<typename Val>
134 float GraphPath<Val>::getLongueur(TimeTexture<Val> & tex, AimsSurfaceTriangle & initmesh, Val value, int dep, int arr)
135 {
136  longueur=0;
137  process(tex, initmesh, value, dep, arr);
138  return longueur;
139 }
140 
141 }
142 
143 
144 
145 #endif
TimeTexture< Val > process(TimeTexture< Val > &tex, AimsSurfaceTriangle &initmesh, Val value, int dep, int arr)
Definition: shortestPath.h:44
AIMSDATA_API AimsTimeSurface< 3, Void > AimsSurfaceTriangle
const T & item(int n) const
Texture< float > MeshDistance(const AimsSurface< 3, Void > &mesh, const Texture< T > &inittex, bool allowUnreached)
float getLongueur(TimeTexture< Val > &tex, AimsSurfaceTriangle &initmesh, Val value, int dep, int arr)
Definition: shortestPath.h:134
static std::vector< std::set< uint > > surfaceNeighbours(const AimsSurface< D, T > &surf)
const T & item(int n) const
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
unsigned int uint
AIMSDATA_API float norm(const Tensor &thing)