Moka kernel
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gmv-isomorphisme.cc
Go to the documentation of this file.
1 /*
2  * lib-gmapkernel : Un noyau de 3-G-cartes et des opérations.
3  * Copyright (C) 2004, Moka Team, Université de Poitiers, Laboratoire SIC
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * Copyright (C) 2009, Guillaume Damiand, CNRS, LIRIS,
6  * guillaume.damiand@liris.cnrs.fr, http://liris.cnrs.fr/
7  *
8  * This file is part of lib-gmapkernel
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 //******************************************************************************
25 #include "g-map-vertex.hh"
26 
27 #ifndef _WINDOWS
28 #include "chrono.hh"
29 #endif
30 
31 #include <iostream>
32 using namespace std;
33 using namespace GMap3d;
34 //******************************************************************************
35 int CGMapVertex::findMotif( CGMapVertex* AMap, unsigned int* ANbMatched )
36 {
37  int index = getNewDirectInfo();
38 
39  int markTreated = getNewMark();
40  int markTreated2 = AMap->getNewMark();
41  bool matchOne = false;
42  bool matchAll = true;
43 
44 #ifndef _WINDOWS
45  CChrono c;
46  c.start();
47 #endif
48 
49  unsigned int oneMatching=0;
50  unsigned int * ptrOneMatching=NULL;
51 
52  if ( ANbMatched!=NULL )
53  {
54  *ANbMatched=0; ptrOneMatching = &oneMatching;
55  }
56 
57  CDynamicCoverageAll it1(this);
58  CDynamicCoverageAll it2(AMap);
59 
60  for ( ; it1.cont(); ++it1 )
61  setDirectInfo(*it1, index, NULL);
62 
63  for ( it1.reinit(); matchAll && it1.cont(); ++it1 )
64  {
65  if ( getDirectInfo(*it1,index)==NULL )
66  {
67  matchOne = false;
68  for ( it2.reinit(); !matchOne && it2.cont(); ++it2 )
69  {
70  matchOne=findMotifFrom(*it1, markTreated, index,
71  AMap, *it2, markTreated2,
72  ptrOneMatching);
73 
74  unmarkMotifMark(*it1,markTreated,(matchOne?-1:index),
75  AMap,*it2,(matchOne?-1:markTreated2));
76 
77  if ( ANbMatched!=NULL && oneMatching>(*ANbMatched) )
78  (*ANbMatched) = oneMatching;
79 
80  // assert( isWholeMapUnmarked(markTreated) );
81  }
82 
83  if ( !matchOne ) matchAll = false;
84  }
85  }
86 
87  freeMark(markTreated);
88 
89  AMap->unmarkAll(markTreated2);
90  AMap->freeMark(markTreated2);
91 
92 #ifndef _WINDOWS
93  c.stop();
94  c.display("Temps de recherche du motif");
95 #endif
96 
97  if ( !matchAll )
98  {
99  freeDirectInfo(index);
100  return -1;
101  }
102 
103  return index;
104 }
105 //******************************************************************************
107  unsigned int* ANbMatched )
108 {
109  unsigned int res = 0;
110  int index = getNewDirectInfo();
111 
112  int markTreated = getNewMark();
113  int markTreated2 = AMap->getNewMark();
114  bool match = false;
115 
116  unsigned int size = 0;
117  unsigned int oneMatching=0;
118  unsigned int * ptrOneMatching=NULL;
119  if ( ANbMatched!=NULL )
120  { *ANbMatched=0; ptrOneMatching = &oneMatching; }
121 
122  CDynamicCoverageAll it1(this);
123  CDynamicCoverageAll it2(AMap);
124 
125  for ( ; it1.cont(); ++it1 )
126  {
127  setDirectInfo(*it1, index, NULL);
128  ++size;
129  }
130  it1.reinit();
131 
132  int mark = AMap->getNewMark();
133  int totest = AMap->getNewMark();
134  int ccsize;
135  for ( ; it2.cont(); ++it2 )
136  {
137  if ( !AMap->isMarked(*it2,mark) )
138  {
139  ccsize=0;
140  for (CBasicDynamicCoverage0123 it3(AMap,*it2,mark); it3.cont(); ++it3)
141  {
142  AMap->setMark(*it3,mark);
143  ++ccsize;
144  }
145  if ( ccsize>=size )
146  {
147  for (CBasicDynamicCoverage0123 it3(AMap,*it2,totest); it3.cont(); ++it3)
148  AMap->setMark(*it3,totest);
149  }
150  }
151  }
152  AMap->negateMaskMark(mark);
153  AMap->freeMark(mark);
154 
155 #ifndef _WINDOWS
156  CChrono c; c.start();
157 #endif
158 
159  for ( it2.reinit(); it2.cont(); ++it2 )
160  {
161  if ( AMap->isMarked(*it2,totest) )
162  {
163  match=findMotifFrom(*it1, markTreated, index,
164  AMap, *it2, markTreated2,
165  ptrOneMatching);
166 
167  unmarkMotifMark(*it1,markTreated,index,
168  AMap,*it2,(match?-1:markTreated2));
169 
170  if ( ANbMatched!=NULL && oneMatching>(*ANbMatched) )
171  (*ANbMatched) = oneMatching;
172 
173  if ( match ) ++res;
174  // assert( isWholeMapUnmarked(markTreated) );
175  }
176  }
177 
178 #ifndef _WINDOWS
179  c.stop();
180  c.display("Temps de recherche de tout les motifs");
181 #endif
182 
183  freeMark(markTreated);
184  AMap->freeMark(markTreated2);
185 
186  freeDirectInfo(index);
187  AMap->unmarkAll(totest);
188  AMap->freeMark(totest);
189 
190  return res;
191 }
192 //******************************************************************************
193 void CGMapVertex::unmarkMotifMark(CDart* ADart, int AMark, int AIndex,
194  CGMapVertex* AMap, CDart* ADart2, int AMark2)
195 {
196  if ( !isMarked(ADart, AMark) ) return;
197 
198  stack<CDart*> toTreat;
199  stack<CDart*> toTreat2;
200 
201  toTreat.push(ADart);
202  toTreat2.push(ADart2);
203 
204  CDart* current;
205  CDart* other;
206  int i;
207 
208  while (!toTreat.empty())
209  {
210  current = toTreat.top(); toTreat.pop();
211  other = toTreat2.top(); toTreat2.pop();
212 
213  if ( isMarked(current, AMark) )
214  {
215  for ( i=0; i<=3; ++i )
216  if ( isMarked(alpha(current,i), AMark) )
217  {
218  toTreat.push(alpha(current,i));
219  toTreat2.push(AMap->alpha(other,i));
220  }
221 
222  unsetMark(current, AMark);
223  if ( AMark2!=-1 ) AMap->unsetMark(other, AMark2);
224  if ( AIndex!=-1 ) setDirectInfo(current, AIndex, NULL);
225  }
226  }
227 }
228 //******************************************************************************
229 bool CGMapVertex::findMotifFrom( CDart* AFromDart, unsigned int AMarkTreated,
230  unsigned int AIndex,
231  CGMapVertex* AMap, CDart* ADestDart,
232  unsigned int AMarkTreated2,
233  unsigned int* ANbMatched )
234 {
235  bool match = true;
236  int i = 0;
237 
238  if ( ANbMatched!=NULL) *ANbMatched = 0;
239 
240  // Les 2 piles pour parcourir les 2 cartes en parallèle.
241  stack<CDart*> toTreat;
242  stack<CDart*> toTreat2;
243 
244  toTreat.push(AFromDart);
245  toTreat2.push(ADestDart);
246 
247  // Le parcours, tant que la pile n'est pas vide.
248  CDart* current;
249  CDart* other;
250 
251  while (match && !toTreat.empty())
252  {
253  // Le prochain brin.
254  current = toTreat.top(); toTreat.pop();
255  other = toTreat2.top(); toTreat2.pop();
256 
257  if ( !isMarked(current, AMarkTreated) )
258  {
259  if ( ANbMatched!=NULL) ++(*ANbMatched);
260 
261  if ( AMap->isMarked(other, AMarkTreated2) )
262  match = false;
263  else
264  {
265  // On fixe l'injection.
266  setDirectInfo(current, AIndex, other);
267 
268  setMark(current, AMarkTreated);
269  AMap->setMark(other, AMarkTreated2);
270 
271  // On teste si l'injection est valide avec les voisins.
272  // On sort dès qu'il y a un problème.
273  for ( i=0; match && i<=3; ++i )
274  {
275  if ( !isFree(current,i) )
276  {
277  if (isMarked(alpha(current,i), AMarkTreated) )
278  {
279  if ( getDirectInfoAsDart(alpha(current,i), AIndex)!=
280  AMap->alpha(other,i) )
281  match = false;
282  }
283  else
284  {
285  if ( AMap->isMarked(AMap->alpha(other,i),AMarkTreated2) )
286  match = false;
287  else
288  {
289  toTreat.push(alpha(current,i));
290  toTreat2.push(AMap->alpha(other,i));
291  }
292  }
293  }
294  else
295  {
296  if (!AMap->isFree(other,i) &&
297  AMap->isMarked(AMap->alpha(other,i),AMarkTreated2) )
298  match = false;
299  }
300  }
301  }
302  }
303  }
304 
305  return match;
306 }
307 //******************************************************************************