Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1935 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  Problème code C++ débutant watershed

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Problème code C++ débutant watershed

n°2240916
agondel
Posté le 22-10-2014 à 09:35:23  profilanswer
 

Bonjour,
 
Je travaille dans le cadre d'un stage de recherche sur la localisation des ondes sur une plaque de métal.
J'utilise le logiciel Freefem++ afin de résoudre mes équations aux DPP.
A partir de la fonction trouvée et de sa cartographie (voir la photo que je poste), je dois trouver les maxima (crêtes) de la fonction.
Comme il n'y a pas de méthode "watershed" sur Freefem++, je dois compiler un code C++.
 
Mon tuteur de stage m'a donné le code C++ que je devais utiliser pour réaliser la fonction "watershed".
Néanmoins, ce code comporte des erreurs, et étant débutante en C++, je n'arrive pas à les résoudre.
Pouvez-vous m'aider s'il-vous-plaît ?
 
Voici le code :  
 

Code :
  1. #include "ff++.hpp"
  2. // #ifndef WITH_NO_INIT
  3. // #include "ff++.hpp"
  4. // #include "AFunction_ext.hpp"
  5. // #endif
  6. // using namespace std;
  7. #include <set>
  8. #include <vector>
  9. #include <map>
  10. #include <algorithm>
  11. //#include "msh3.hpp"
  12. // #include <iostream>
  13. using namespace  Fem2D;
  14. // FreeFem glue
  15. class WATERSHED_P1_Op : public E_F0mps
  16. {
  17. public:
  18.     Expression eTh,eff,eret;
  19.    
  20.     static const int n_name_param = 1;
  21.     static basicAC_F0::name_and_type name_param[n_name_param];
  22.     Expression nargs[n_name_param];
  23. public:
  24.     WATERSHED_P1_Op(const basicAC_F0 &  args,Expression tth, Expression fff,Expression rrr)
  25.     : eTh(tth),eff(fff),eret(rrr)
  26.     {
  27.         args.SetNameParam(n_name_param,name_param,nargs);
  28.     }
  29.     AnyType operator()(Stack stack) const;
  30.    
  31. private:
  32.     template<typename T>
  33.     T arg(int i, Stack stack, T a) const {
  34.         return nargs[i]
  35.         ? GetAny< T >( (*nargs[i])(stack) )
  36.         : a;
  37.     }
  38. };
  39. basicAC_F0::name_and_type WATERSHED_P1_Op::name_param[]= {
  40.     {  "eps",  &typeid(double)}
  41. };
  42. // algorithm
  43. typedef int triangle_t;
  44. typedef int vertex_t;
  45. typedef int color_t;
  46. struct fat_vertex_t {
  47.     vertex_t vertex;
  48.     triangle_t triangle;
  49.     int edge;
  50.    
  51.     fat_vertex_t(vertex_t v, triangle_t t, int e)
  52.     : vertex(v), triangle(t), edge(e) {}
  53.     friend bool operator<(fat_vertex_t const& a, fat_vertex_t const& b)
  54.     { return a.vertex < b.vertex; }
  55.    
  56.     friend bool operator==(fat_vertex_t const& a, fat_vertex_t const& b)
  57.     { return a.vertex == b.vertex; }
  58. };
  59. typedef std::vector<fat_vertex_t> vertices_t;
  60. typedef std::pair<fat_vertex_t, double> ver_val_t;
  61. struct cmp_t {
  62.     bool operator()(ver_val_t const& t1, ver_val_t const& t2) const {
  63.         return t1.second < t2.second;
  64.     }
  65. };
  66. typedef std::priority_queue<ver_val_t, std::vector<ver_val_t>, cmp_t> queue_t;
  67. typedef KNM<long> ret_type;
  68. template<typename Func>
  69. void for_each_triangle(Mesh const& Th, triangle_t const triangle0, int const edge0, Func func) {
  70.     int const vertex = Th(triangle0, edge0);
  71.    
  72.     if( !func( triangle0 ) )
  73.       return;
  74.     int edge = edge0;
  75.     int triangle = triangle0;
  76.     for(;;) {
  77.         edge = (edge + 1) % 3;
  78.         if( Th(triangle, edge) == vertex )
  79.             edge = (edge + 1) % 3;
  80.         triangle = Th.ElementAdj( triangle, edge );
  81.         if( triangle == triangle0 )
  82.             return;
  83.        
  84.         if( triangle < 0 )
  85.             break;
  86.        
  87.         if( !func( triangle ) )
  88.             return;
  89.     }
  90.     triangle = triangle0;
  91.     edge = edge0;
  92.     for(;;) {
  93.         edge = (edge - 1) % 3;
  94.         if( Th(triangle, edge) == vertex )
  95.             edge = (edge - 1) % 3;
  96.         triangle = Th.ElementAdj( triangle, edge );
  97.         if( triangle == triangle0 )
  98.             return;
  99.        
  100.         if( triangle < 0 )
  101.             break;
  102.        
  103.         if( !func( triangle ) )
  104.             return;
  105.     }
  106. }
  107. template<typename Func>
  108. struct for_each_neighbor_helper {
  109.     Func func;
  110.     Mesh const& Th;
  111.     bool operator()(triangle_t triangle) {
  112.         for(int e = 0; e < 3; ++e)
  113.             if(! func( Th(triangle, e), triangle, e ) )
  114.                 return false;
  115.         return true;
  116.     }
  117. };
  118. template<typename Func>
  119. void for_each_neighbor(Mesh const& Th, triangle_t const triangle0, int const edge0, Func func) {
  120.     for_each_neighbor_helper<Func> help = { func, Th };
  121.     // check adjacent triangles
  122.     for_each_triangle(Th, triangle0, edge0, help);
  123. }
  124. template<typename Cont>
  125. void erase_unique(Cont& cont) {
  126.     std::sort(cont.begin(), cont.end());
  127.     cont.erase(
  128.         std::unique(cont.begin(), cont.end()),
  129.         cont.end()
  130.     );
  131. }
  132. struct maxima_helper {
  133.     KN<double> const& tff;
  134.     double& maxval;
  135.     bool& is_max;
  136.     bool operator()(vertex_t vertex, triangle_t triangle, int edge) const {
  137.         double val = tff[ vertex ];
  138.         if(val > maxval) {
  139.             is_max = false;
  140.             return false;
  141.         }
  142.         return true;
  143.     }
  144. };
  145. static void maxima(Mesh const& Th, KN<double> const& tff, vertices_t& vertices, double epsr)
  146. {
  147.     const int nbt=Th.nt; // nombre de triangles
  148.     // loop over vertices
  149.     for(int it = 0; it < nbt; ++it) {
  150.         int maxiv = 0;
  151.         double maxval = tff[ Th(it,0) ];
  152.         int iv;
  153.         for(iv=1; iv < 3; ++iv) {
  154.             int i = Th(it,iv);
  155.             double val = tff[i];
  156.             if(val > maxval) {
  157.                 maxiv = iv;
  158.                 maxval = val;
  159.             }
  160.         }
  161.         iv = maxiv;
  162.        
  163.         if(std::abs(maxval) < epsr)
  164.             continue;
  165.         bool is_max = true;
  166.        
  167.         maxima_helper helper = { tff, maxval, is_max };
  168.        
  169.         for_each_neighbor(Th, it, iv, helper);
  170.         if(!is_max)
  171.             continue;
  172. //         std::cout << "FOUND " << it << ' ' << maxiv << ' ' << Th(it, maxiv) << ' ' << maxval << std::endl;
  173.         vertices.push_back(fat_vertex_t( Th(it,maxiv), it, maxiv ));
  174.     }
  175.     erase_unique(vertices);
  176. }
  177. #if 0
  178. static void maxima(Mesh const& Th, KN<double> const& tff, queue_t& roots, std::vector<color_t>& colors, double epsr)
  179. {
  180.     const int nbt=Th.nt; // nombre de triangles
  181.     const int nbv=Th.nv; // nombre de vertices
  182.     enum pixel_type {
  183.         MAXIMUM,
  184.         PLATEAU,
  185.         NON_MAXIMUM
  186.     };
  187.     // the one that increments current_color
  188.     // shall push to roots
  189.     color_t current_color = 1;
  190.     std::vector<bool> visited ( nbv, false );
  191.     auto analyse_neighbors = [&](vertex_t const vertex0, triangle_t const triangle0, int edge0) {
  192.         pixel_type pxl = MAXIMUM;
  193.         for_each_neighbor(Th, triangle0, edge0,
  194.           [&](vertex_t vertex, triangle_t triangle, int edge) {
  195.             if( vertex == vertex0 )
  196.                 return true;
  197.             if( tff[vertex] >  tff[vertex0] ) {
  198.                 pxl = NON_MAXIMUM;
  199.                 return false;
  200.             }
  201.             if( tff[vertex] == tff[vertex0] )
  202.                 pxl = PLATEAU;
  203.             return true;
  204.         });
  205.         return pxl;
  206.     };
  207.     auto analyse_plateau = [&](vertex_t const vertex0, triangle_t const triangle0, int edge0) {
  208.         colors[vertex0] = current_color;
  209.         // early exit
  210.         color_t new_label = current_color;
  211.         // do not forget marked nodes
  212.         std::deque<fat_vertex_t> queue;
  213.         queue.push_back({ vertex0, triangle0, edge0 });
  214.         auto it = queue.begin();
  215.         auto const end = queue.end();
  216.         for(; it != end; ++it ) {
  217.             fat_vertex_t const& vv = *it;
  218.             for_each_neighbor(Th, vv.triangle, vv.edge,
  219.               [&](vertex_t vertex, triangle_t triangle, int edge) {
  220.                 if( colors[vertex] == -1 && tff[vertex] == tff[vertex0] ) {
  221.                     colors[vertex] = current_color;
  222.                     queue.push_back({ vertex, triangle, edge });
  223.                     visited[vertex] = true;
  224.                 }
  225.                 else if( tff[vertex] > tff[vertex0] )
  226.                     new_label = -1;
  227.                 return true;
  228.             });
  229.         }
  230.         if( new_label == -1 )
  231.             for(fat_vertex_t const& vv : queue)
  232.                 colors[vv.vertex] = -1;
  233.         else {
  234.             ++current_color;
  235.             roots.push({ { vertex0, triangle0, edge0 }, tff[vertex0] });
  236.         }
  237.     };
  238.     // loop over vertices
  239.     for(triangle_t triangle = 0; triangle < nbt; ++triangle)
  240.     for(int edge = 0; edge < 3; ++edge) {
  241.         vertex_t vertex = Th( triangle, edge );
  242.         if( visited[vertex] )
  243.             continue;
  244.         pixel_type pxl = analyse_neighbors(vertex, triangle, edge);
  245.         if( pxl == MAXIMUM ) {
  246.             for_each_neighbor(Th, triangle, edge,
  247.               [&](vertex_t vertex2, int,int) {
  248.                 ffassert( tff[vertex2] <= tff[vertex] );
  249.                 return true;
  250.             });
  251.             colors[vertex] = current_color++;
  252.             roots.push({{ vertex, triangle, edge }, tff[vertex] });
  253.         }
  254. //         else if( pxl == PLATEAU )
  255. //             analyse_plateau(vertex, triangle, edge);
  256.         visited[vertex] = true;
  257.     }
  258.     ffassert( roots.size() == current_color-1 );
  259. }
  260. #endif
  261. struct color_one_neighbor {
  262.     KN<double> const& tff;
  263.     fat_vertex_t const& current;
  264.     std::vector<color_t>& colors;
  265.     color_t const current_color;
  266.     queue_t& queue;
  267.     bool operator()(vertex_t vertex, triangle_t triangle, int edge) {
  268.        
  269.         fat_vertex_t vv ( vertex, triangle, edge );
  270.         if(vertex == current.vertex)
  271.             return true;
  272.         color_t& color = colors[vertex];
  273.         if( color == -1 ) {
  274.             color = current_color;
  275.             queue.push(ver_val_t( vv, tff[vertex] ));
  276.         }
  277.         else if( color != current_color ) {
  278.             color = 0;
  279.             frontier.push_back( vv );
  280. //                 ffassert( tff[vertex] <= tff[current.vertex] ); // TODO ça explose ici
  281. //                 std::cout << "FOUND " << vertex << " -> " << color << std::endl;
  282.         }
  283.         return true;
  284.     }
  285. };
  286. AnyType WATERSHED_P1_Op::operator()(Stack stack) const
  287. {
  288.     MeshPoint *mp(MeshPointStack(stack));
  289.     ret_type& ret = *GetAny<ret_type* >( (*eret)(stack) );
  290.     Mesh* pTh = GetAny<Mesh *>( (*eTh)(stack) );
  291.    
  292.     ffassert(pTh);
  293.     double  epsr = arg(0,stack,1e-5);
  294.     Mesh const& Th = *pTh;
  295.     const int nbv=Th.nv; // nombre de sommet
  296.     const int nbt=Th.nt; // nombre de triangles
  297.     const int nbe=Th.neb; // nombre d'aretes fontiere
  298.     const double unset = -1e-100;
  299.     KN<double> tff(nbv, unset);
  300.     // loop over triangle
  301.     for(int it=0; it < nbt; ++it) {
  302.         for(int iv=0; iv<3; ++iv) {
  303.             int i = Th(it,iv);
  304.             if(tff[i]==unset) {
  305.                 mp->setP(pTh,it,iv);
  306.                 tff[i]=GetAny<double>((*eff)(stack));
  307.             }
  308.         }
  309.     }
  310.     queue_t queue;
  311.     std::vector<color_t> colors ( nbv, -1 );
  312.     vertices_t frontier;
  313.    
  314.     // prefill
  315.     {
  316.         vertices_t roots;
  317.         maxima(Th, tff, roots, epsr);
  318.         color_t color = 1;
  319.        
  320.         vertices_t::iterator it = roots.begin(), en = roots.end();
  321.         for(; it != en; ++it) {
  322.             fat_vertex_t const& current = *it;
  323.             colors[current.vertex] = color++;
  324.             queue.push(ver_val_t( current, tff[current.vertex] ));
  325.         }
  326.     }
  327.     // loop
  328.     while( !queue.empty() ) {
  329.         fat_vertex_t const current = queue.top().first; queue.pop();
  330.         color_t const current_color = colors[current.vertex];
  331.         ffassert( current_color != -1 );
  332.         if( current_color == 0 )
  333.             continue;
  334.         // check adjacent triangles
  335.         for_each_neighbor(
  336.             Th, current.triangle, current.edge,
  337.             color_one_neighbor(tff, current, colors, current_color, queue)
  338.         );
  339.     }
  340.     erase_unique(frontier);
  341.     std::cout << "OUT " << frontier.size() << std::endl;
  342.     ret.resize(2, frontier.size());
  343.     for(int k = 0; k < frontier.size(); ++k) {
  344.         fat_vertex_t const& vv = frontier[k];
  345.         ret(0, k) = vv.triangle;
  346.         ret(1, k) = vv.edge;
  347.     }
  348.     return 0l;
  349. }
  350. class  WATERSHED_P1: public OneOperator { public
  351.     typedef Mesh *pmesh;
  352.     typedef std::pair<FEbase<double, v_fes>*, int> fem_t;
  353.     WATERSHED_P1() : OneOperator(atype<long>(),atype<pmesh>(),atype<double>(), atype<ret_type*>() ) {}
  354.    
  355.     E_F0 * code(const basicAC_F0 & args) const
  356.     {
  357.         return  new WATERSHED_P1_Op( args,
  358.                                   t[0]->CastTo(args[0]),
  359.                                   t[1]->CastTo(args[1]),
  360.                                   t[2]->CastTo(args[2]) );
  361.     }
  362. };
  363. void finit()
  364. {
  365.     Global.Add("watershed","(",new WATERSHED_P1);
  366. }
  367. LOADFUNC(finit);


 
Et voici le genre de fonction dont je veux trouver les crêtes :  
 
http://nsa33.casimages.com/img/2014/10/22/mini_141022093627498097.png

mood
Publicité
Posté le 22-10-2014 à 09:35:23  profilanswer
 

n°2240944
gilou
Modérateur
Modzilla
Posté le 22-10-2014 à 14:02:54  profilanswer
 

Bonjour,

Citation :

Néanmoins, ce code comporte des erreurs, et étant débutante en C++, je n'arrive pas à les résoudre.

Et il ne vous viendrait pas à l'esprit de poster lesdites erreurs afin qu'on puisse vous donner des indications sur ce qui les provoque. Il faut qu'on les devine en lisant 450 lignes de code?  [:nazztazz:2]  
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2241040
agondel
Posté le 23-10-2014 à 17:14:54  profilanswer
 

gilou a écrit :

Bonjour,

Citation :

Néanmoins, ce code comporte des erreurs, et étant débutante en C++, je n'arrive pas à les résoudre.

Et il ne vous viendrait pas à l'esprit de poster lesdites erreurs afin qu'on puisse vous donner des indications sur ce qui les provoque. Il faut qu'on les devine en lisant 450 lignes de code?  [:nazztazz:2]  
A+,


 
En effet, je suis désolée j'étais fatiguée en postant.
 
voici les erreurs :
 
watershed.cxx03.cpp: In member function ‘bool color_one_neighbor::operator()(vertex_t, triangle_t, int)’:
watershed.cxx03.cpp:353:13: error: ‘frontier’ was not declared in this scope
             frontier.push_back( vv );
             ^
watershed.cxx03.cpp: In member function ‘virtual AnyType WATERSHED_P1_Op::operator()(Stack) const’:
watershed.cxx03.cpp:427:74: error: no matching function for call to ‘color_one_neighbor::color_one_neighbor(KN<double>&, const fat_vertex_t&, std::vector<int>&, const color_t&, queue_t& )’
             color_one_neighbor(tff, current, colors, current_color, queue)
                                                                          ^
watershed.cxx03.cpp:427:74: note: candidates are:
watershed.cxx03.cpp:330:8: note: color_one_neighbor::color_one_neighbor()
 struct color_one_neighbor {
        ^
watershed.cxx03.cpp:330:8: note:   candidate expects 0 arguments, 5 provided
watershed.cxx03.cpp:330:8: note: color_one_neighbor::color_one_neighbor(const color_one_neighbor& )
watershed.cxx03.cpp:330:8: note:   candidate expects 1 argument, 5 provided


Message édité par agondel le 23-10-2014 à 17:15:24
n°2241057
gilou
Modérateur
Modzilla
Posté le 24-10-2014 à 00:20:22  profilanswer
 

>> Néanmoins, ce code comporte des erreurs, et étant débutante en C++, je n'arrive pas à les résoudre.  
C'est pas du code pour débutant...
 
Bon, il y a deux erreurs.
La première:
color_one_neighbor(tff, current, colors, current_color, queue)
mais la struct color_one_neighbor (une sorte de classe particulière en C++) n'a pas de constructeur, en particulier pas de constructeur a 5 arguments (le compilo a du en générer un a 0 argument), il va donc falloir rajouter un tel constructeur a color_one_neighbor.
 
La seconde erreur: frontier est utilis dans la struct color_one_neighbor sans être déclaré. il est déclaré dans Watershed_P1_Op:
vertices_t frontier;
 
Il va donc falloir déclarer un membre de plus dans color_one_neighbor, rajouter ce type de membre dans le constructeur (a 6 et non plus 5 paramètres) et ajouter ce parametre supplémentaire a l'appel (actuellement fautif) color_one_neighbor(tff, current, colors, current_color, queue)
 
C'est mon impression au vu du code, sans savoir ce qu'il est censé faire (et quelqu'un qui le comprends saura si c'est bon ou pas, mais la le code est trop opaque pour que je puisse en dire plus).  
 
A+,
 


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  Problème code C++ débutant watershed

 

Sujets relatifs
programme en language CProblème d'envoi mail en PHP avec easyphp 13.1
Probleme encodage JEE/ eclipse / formulaire JSP[RÉSOLU] Problème fenêtre modale et notifications
C++[Excel 2007] Problème pour trouver le nombre de ligne d'une feuille
Problème shell_exec fonctionne pas Graphviz Mediawikiprobleme de soulignement sur code blocks 13.12
dd 
Plus de sujets relatifs à : Problème code C++ débutant watershed


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR