Moka kernel
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
g-map-basic.icc
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 "dart.hh"
26 #include <cassert>
27 #include <cstdlib>
28 //******************************************************************************
29 namespace GMap3d
30 {
31 //******************************************************************************
32 INLINE
34 {
35  return FFirstDart;
36 }
37 //******************************************************************************
38 INLINE
40 {
41  FFirstDart = ADart;
42 }
43 //******************************************************************************
44 INLINE
46 {
47  return new CDart(FMaskMarks);
48 }
49 //******************************************************************************
50 INLINE
52 {
53  delete ADart;
54 }
55 //******************************************************************************
56 INLINE
58 {
59  // Insertion dans la liste des brins :
60  if ( getFirstDart()!=NULL )
61  {
62  getFirstDart()->setPrev(ADart);
63  ADart->setNext(getFirstDart());
64  }
65 
66  setFirstDart(ADart);
67 }
68 //******************************************************************************
69 INLINE
71 {
72  if ( getFirstDart()==ADart )
73  {
74  assert( ADart->getPrev()==NULL );
75  setFirstDart(ADart->getNext());
76  }
77  else
78  {
79  assert( ADart->getPrev()!=NULL );
80  ADart->getPrev()->setNext(ADart->getNext());
81  }
82 
83  if ( ADart->getNext()!=NULL )
84  ADart->getNext()->setPrev(ADart->getPrev());
85 }
86 //******************************************************************************
87 INLINE
89 {
90  CDart* ADart = newDart();
91  assert(ADart != NULL);
92  addDartInList(ADart);
93  return ADart;
94 }
95 //******************************************************************************
96 INLINE
98 {
99  assert( ADart!=NULL );
100  removeDartInList(ADart);
101  delDart(ADart);
102 }
103 //******************************************************************************
104 INLINE
106 {
107  CDart* first = getFirstDart();
108 
109  if (first==NULL) return;
110 
111  CDart* current = first->getNext();
112  CDart* tmp;
113  CDart* next;
114  int i, dejaTraite = 1;
115 
116 #ifndef _WINDOWS
117  srand(time(NULL));
118 #endif
119 
120  first->setNext(NULL);
121 
122  while ( current!=NULL )
123  {
124  next = current->getNext();
125 
126  i = rand()%dejaTraite;
127  tmp = first;
128  while ( i!=0 ) { tmp=tmp->getNext(); --i; }
129  assert( tmp!=NULL );
130 
131  current->setPrev( tmp->getPrev() );
132  current->setNext( tmp );
133  if ( tmp==first )
134  first=current;
135  else
136  tmp->getPrev()->setNext(current);
137 
138  tmp->setPrev( current );
139 
140  current = next;
141  ++dejaTraite;
142  }
143 
144  setFirstDart(first);
145 }
146 //******************************************************************************
147 INLINE
149 {
150  CDart* d = getFirstDart();
151  CDart* tmp;
152 
153  while ( d!=NULL )
154  {
155  tmp = d;
156  d = d->getNext();
157  delDart(tmp);
158  }
159  setFirstDart(NULL);
160 }
161 //******************************************************************************
162 INLINE
164 {
165  removeAllDarts();
166 
167  // Set all the bits to false
168  FMaskMarks.reset();
169  FUsedMarks.reset();
170  FUsedOrbits.reset();
171  FUsedDirectInfo.reset();
172 
173  FNbUsedMarks = 0;
174  FNbUsedDirectInfos = 0;
175 
176 #ifndef NDEBUG
177  FMaxNbUsedMarks = 0;
178  FMaxNbUsedDirectInfos = 0;
179 #endif // NDEBUG
180 
181  int i;
182 
183  for (i=0; i<NB_MARKS; ++i)
184  FMarks[i] = i;
185 
186  for (i=0; i<NB_DIRECT_INFO; ++i)
187  FDirectInfos[i] = i;
188 }
189 //******************************************************************************
190 INLINE
192  FFirstDart(NULL)
193 {
194  empty();
195 }
196 //******************************************************************************
197 INLINE
198 bool CGMapBasic::isFree(const CDart* ADart, int ADimension) const
199 {
200  return ADart->isFree(ADimension);
201 }
202 //******************************************************************************
203 INLINE
204 bool CGMapBasic::isFree0(const CDart* ADart) const
205 {
206  return ADart->isFree0();
207 }
208 //******************************************************************************
209 INLINE
210 bool CGMapBasic::isFree1(const CDart* ADart) const
211 {
212  return ADart->isFree1();
213 }
214 //******************************************************************************
215 INLINE
216 bool CGMapBasic::isFree2(const CDart* ADart) const
217 {
218  return ADart->isFree2();
219 }
220 //******************************************************************************
221 INLINE
222 bool CGMapBasic::isFree3(const CDart* ADart) const
223 {
224  return ADart->isFree3();
225 }
226 //******************************************************************************
227 INLINE
228 CDart* CGMapBasic::alpha(const CDart* ADart, int ADimension) const
229 {
230  return ADart->getAlpha(ADimension);
231 }
232 //******************************************************************************
233 INLINE
234 CDart* CGMapBasic::alpha0(const CDart* ADart) const
235 {
236  return ADart->getAlpha0();
237 }
238 //******************************************************************************
239 INLINE
240 CDart* CGMapBasic::alpha1(const CDart* ADart) const
241 {
242  return ADart->getAlpha1();
243 }
244 //******************************************************************************
245 INLINE
246 CDart* CGMapBasic::alpha2(const CDart* ADart) const
247 {
248  return ADart->getAlpha2();
249 }
250 //******************************************************************************
251 INLINE
252 CDart* CGMapBasic::alpha3(const CDart* ADart) const
253 {
254  return ADart->getAlpha3();
255 }
256 //******************************************************************************
257 INLINE
259 {
260 #ifndef NDEBUG
261  if (FNbUsedDirectInfos == NB_DIRECT_INFO)
262  // Pas assez de champs directInfo (voir macro NB_DIRECT_INFO).
263  assert(false);
264 
265  if (FNbUsedDirectInfos == FMaxNbUsedDirectInfos)
266  FMaxNbUsedDirectInfos = FNbUsedDirectInfos + 1;
267 #endif // NDEBUG
268 
269  int directInfo = FDirectInfos[FNbUsedDirectInfos ++];
270  FUsedDirectInfo.set(directInfo, true);
271  return directInfo;
272 }
273 //******************************************************************************
274 INLINE
275 void CGMapBasic::freeDirectInfo(int ADirectIndex)
276 {
277  assert(0 <= ADirectIndex && ADirectIndex < NB_DIRECT_INFO);
278  assert(FUsedDirectInfo[ADirectIndex]);
279 
280  FUsedDirectInfo.set(ADirectIndex, false);
281  FDirectInfos[-- FNbUsedDirectInfos] = ADirectIndex;
282 }
283 //******************************************************************************
284 INLINE
285 void* CGMapBasic::getDirectInfo(CDart* ADart, int ADirectIndex) const
286 {
287  assert( FUsedDirectInfo[ADirectIndex] );
288 
289  return ADart->getDirectInfo(ADirectIndex);
290 }
291 //******************************************************************************
292 INLINE
293 void CGMapBasic::setDirectInfo(CDart* ADart, int ADirectIndex, void* APointer)
294 {
295  assert( FUsedDirectInfo[ADirectIndex] );
296 
297  ADart->setDirectInfo(ADirectIndex, APointer);
298 }
299 //******************************************************************************
300 INLINE
301 bool CGMapBasic::getMaskMark(int AMarkNumber) const
302 {
303  return FMaskMarks[AMarkNumber];
304 }
305 //******************************************************************************
306 INLINE
308 {
309  return FNbUsedMarks;
310 }
311 //******************************************************************************
312 INLINE
313 void CGMapBasic::setMarks(CDart* ADart, const std::bitset<NB_MARKS> & AMarks) const
314 {
315  ADart->setMarks(AMarks ^ FMaskMarks);
316 }
317 //******************************************************************************
318 INLINE
319 std::bitset<NB_MARKS> CGMapBasic::getMarks(CDart* ADart) const
320 {
321  return ADart->getMarks() ^ FMaskMarks;
322 }
323 //******************************************************************************
324 INLINE
326 {
327 #ifndef NDEBUG
328  if ( FNbUsedMarks==NB_MARKS )
329  // Pas assez de marques booleennes (voir macro NB_MARKS).
330  assert(false);
331 
332  if (FNbUsedMarks==FMaxNbUsedMarks)
333  FMaxNbUsedMarks = FNbUsedMarks + 1;
334 #endif // NDEBUG
335 
336  int mark = FMarks[FNbUsedMarks++];
337  FUsedMarks.set(mark, true);
338  return mark;
339 }
340 //******************************************************************************
341 INLINE
342 void CGMapBasic::negateMaskMark(int AMarkNumber)
343 {
344  assert(FUsedMarks[AMarkNumber]);
345 
346  FMaskMarks.flip(AMarkNumber);
347 }
348 //******************************************************************************
349 INLINE
350 bool CGMapBasic::isMarked(const CDart* ADart, int AMarkNumber) const
351 {
352  assert(FUsedMarks[AMarkNumber]);
353 
354  return ADart->getMark(AMarkNumber)!=getMaskMark(AMarkNumber);
355 }
356 //******************************************************************************
357 INLINE
358 void CGMapBasic::setMarkTo(CDart* ADart, int AMarkNumber, bool AState)
359 {
360  assert(FUsedMarks[AMarkNumber]);
361 
362  ADart->setMark(AMarkNumber, AState ^ FMaskMarks[AMarkNumber]);
363 }
364 //******************************************************************************
365 INLINE
366 void CGMapBasic::setMark(CDart* ADart, int AMarkNumber)
367 {
368  setMarkTo(ADart, AMarkNumber, true);
369 }
370 //******************************************************************************
371 INLINE
372 void CGMapBasic::unsetMark(CDart* ADart, int AMarkNumber)
373 {
374  setMarkTo(ADart, AMarkNumber, false);
375 }
376 //******************************************************************************
377 INLINE
378 bool CGMapBasic::isWholeMapUnmarked(int AMarkNumber)
379 {
380  for (CDart* current = FFirstDart; current!=NULL; current = current->getNext())
381  if (isMarked(current, AMarkNumber))
382  return false;
383 
384  return true;
385 }
386 //******************************************************************************
387 INLINE
388 void CGMapBasic::freeMark(int AMarkNumber)
389 {
390  // L'assert qui tue:
391  // assert(isWholeMapUnmarked(AMarkNumber));
392 
393  assert( 0<=AMarkNumber && AMarkNumber<NB_MARKS );
394  assert( FUsedMarks[AMarkNumber] );
395 
396  FUsedMarks.set(AMarkNumber, false);
397  FMarks[-- FNbUsedMarks] = AMarkNumber;
398 }
399 //******************************************************************************
400 INLINE
401 bool CGMapBasic::isOrbitUsed(TOrbit AOrbit) const
402 {
403  return FUsedOrbits[static_cast<int>(AOrbit)];
404 }
405 //******************************************************************************
406 INLINE
407 bool CGMapBasic::isOrbitUsed(CDart* ADart, TOrbit AOrbit) const
408 {
409  return ADart->isOrbitUsed(AOrbit);
410 }
411 //******************************************************************************
412 INLINE
414 {
415  FUsedOrbits.set(static_cast<int>(AOrbit), true);
416 }
417 //******************************************************************************
418 INLINE
420 {
421  // assert(isOrbitUsed(AOrbit));
422 
423  // L'assert qui tue, le retour:
424 #ifndef NDEBUG
425  // for (CDart* current = FFirstDart; current != NULL;
426  // current = current->getNext())
427  // assert(current->getEmbedding(AOrbit) == NULL);
428 #endif // NDEBUG
429 
430  FUsedOrbits.set(static_cast<int>(AOrbit), false);
431 }
432 //******************************************************************************
433 INLINE
434 void CGMapBasic::linkAlpha(CDart* ADart1, CDart* ADart2, int ADimension)
435 {
436  assert( ADart1!=NULL && ADart2!=NULL);
437 
438  ADart1->setAlpha(ADart2, ADimension);
439  ADart2->setAlpha(ADart1, ADimension);
440 }
441 //******************************************************************************
442 INLINE
443 void CGMapBasic::linkAlpha0(CDart* ADart1, CDart* ADart2)
444 {
445  linkAlpha(ADart1, ADart2, 0);
446 }
447 //******************************************************************************
448 INLINE
449 void CGMapBasic::linkAlpha1(CDart* ADart1, CDart* ADart2)
450 {
451  linkAlpha(ADart1, ADart2, 1);
452 }
453 //******************************************************************************
454 INLINE
455 void CGMapBasic::linkAlpha2(CDart* ADart1, CDart* ADart2)
456 {
457  linkAlpha(ADart1, ADart2, 2);
458 }
459 //******************************************************************************
460 INLINE
461 void CGMapBasic::linkAlpha3(CDart* ADart1, CDart* ADart2)
462 {
463  linkAlpha(ADart1, ADart2, 3);
464 }
465 //******************************************************************************
466 INLINE
467 void CGMapBasic::unlinkAlpha(CDart* ADart, int ADimension)
468 {
469  assert( ADart!=NULL );
470 
471  ADart->getAlpha(ADimension)->setFree(ADimension);
472  ADart->setFree(ADimension);
473 }
474 //******************************************************************************
475 INLINE
477 {
478  unlinkAlpha(ADart, 0);
479 }
480 //******************************************************************************
481 INLINE
483 {
484  unlinkAlpha(ADart, 1);
485 }
486 //******************************************************************************
487 INLINE
489 {
490  unlinkAlpha(ADart, 2);
491 }
492 //******************************************************************************
493 INLINE
495 {
496  unlinkAlpha(ADart, 3);
497 }
498 //******************************************************************************
499 } // namespace GMap3d
500 //******************************************************************************