Moka kernel
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gmg-selection.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-generic.hh"
26 using namespace GMap3d;
27 //******************************************************************************
28 unsigned int CGMapGeneric::markOrbit(CDart * ADart, TOrbit AOrbit,
29  int AMarkNumber)
30 {
31  assert(ADart!=NULL);
32 
33  CCoverage * cov= getDynamicCoverage(ADart, AOrbit);
34  unsigned int res = 0;
35  while (cov->cont())
36  {
37  setMark((*cov)++, AMarkNumber);
38  ++res;
39  }
40 
41  delete cov;
42  return res;
43 }
44 //******************************************************************************
46  int AMarkNumber)
47 {
48  assert(ADart!=NULL);
49  assert(AOrbit>=ORBIT_SELF && AOrbit<=ORBIT_CC);
50  assert(isOrientable(ADart, AOrbit));
51 
52  bool usedDim[4];
53  int selected = getNewMark();
54 
55  for (int dim=0; dim<=3; ++dim)
56  usedDim[dim] = AND_ORBIT(AOrbit, ORBIT_DIM[dim]) != ORBIT_SELF;
57 
58  CCoverage * cov= getDynamicCoverage(ADart, AOrbit);
59 
60  setMark(**cov, selected);
61  setMark(**cov, AMarkNumber);
62 
63  for (; cov->cont(); ++(*cov))
64  if (!isMarked(**cov, selected))
65  for (int dim=0; dim<=3; ++dim)
66  if (usedDim[dim] && !isFree(**cov, dim))
67  {
68  setMark(alpha(**cov, dim), selected);
69  setMark(alpha(**cov, dim), AMarkNumber);
70  }
71 
72  delete cov;
73 
74  unmarkOrbit(ADart, AOrbit, selected);
75  freeMark(selected);
76 }
77 //******************************************************************************
79  int AMarkNumber)
80 {
81  negateMaskMark(AMarkNumber);
82  halfMarkOrbit(ADart, AOrbit, AMarkNumber);
83  negateMaskMark(AMarkNumber);
84 }
85 //******************************************************************************
86 void CGMapGeneric::unmarkOrbit(CDart * ADart, TOrbit AOrbit,
87  int AMarkNumber)
88 {
89  assert(ADart!=NULL);
90 
91  CCoverage * cov= getDynamicCoverage(ADart, AOrbit);
92 
93  while (cov->cont())
94  unsetMark((*cov)++, AMarkNumber);
95 
96  delete cov;
97 }
98 //******************************************************************************
99 void CGMapGeneric::unmarkAll(int AMarkNumber)
100 {
101  CDynamicCoverageAll it(this);
102 
103  while (it.cont())
104  unsetMark(it++, AMarkNumber);
105 }
106 //******************************************************************************
108  int AMarkNumberSrce, int AMarkNumberDest)
109 {
110  assert(AMarkNumberSrce!=AMarkNumberDest);
111 
112  int treated = getNewMark();
113  int nb_cells = 0;
114 
115  CDynamicCoverageAll it(this);
116 
117  for (; it.cont(); ++it)
118  if (!isMarked(*it, treated))
119  {
120  if (isMarked(*it, AMarkNumberSrce))
121  {
122  markOrbit(*it, AOrbit, AMarkNumberDest);
123  markOrbit(*it, AOrbit, treated );
124  ++nb_cells;
125  }
126  else
127  setMark(*it, treated);
128  }
129 
130  negateMaskMark(treated);
131  freeMark(treated);
132 
133  return nb_cells;
134 }
135 //******************************************************************************
136 int CGMapGeneric::markIncidentCells(TOrbit AOrbit, int AMarkNumber)
137 {
138  int mark = getNewMark();
139 
140  markCopy(AMarkNumber, mark);
141  int nb_cells = markIncidentCells(AOrbit, mark, AMarkNumber);
142  unmarkAll(mark);
143 
144  freeMark(mark);
145 
146  return nb_cells;
147 }
148 //******************************************************************************
150  int AMarkNumberSrce,
151  int AMarkNumberDest)
152 {
153  assert(AMarkNumberSrce!=AMarkNumberDest);
154 
155  int treated = getNewMark();
156 
157  CDynamicCoverageAll it(this);
158 
159  for (; it.cont(); ++it)
160  if (!isMarked(*it, treated))
161  {
162  if (isMarked(*it, AMarkNumberSrce))
163  {
164  if (isOrientable(*it, AOrbit))
165  {
166  halfMarkOrbit(*it, AOrbit, AMarkNumberDest);
167  halfMarkOrbit(*it, AOrbit, treated );
168  }
169  else
170  {
171  markOrbit(*it, AOrbit, AMarkNumberDest);
172  markOrbit(*it, AOrbit, treated );
173  }
174  }
175  else
176  setMark(*it, treated);
177  }
178 
179  negateMaskMark(treated);
180  freeMark(treated);
181 }
182 //******************************************************************************
183 void CGMapGeneric::markInterior(CDart * ADart, int ADimension,
184  int AMarkBorder, int AMarkInterior)
185 {
186  assert(ADart != NULL);
187  assert(2 <= ADimension && ADimension <= 3);
188 
189  if (AMarkInterior < 0)
190  AMarkInterior = AMarkBorder;
191 
192  bool initialMarked = isMarked(ADart, AMarkBorder);
193  unsetMark(ADart, AMarkBorder);
194 
195  TOrbit orbit = ORBIT_CELL[1+ADimension];
196 
197  CCoverage * cov = getBasicDynamicCoverage(ADart, orbit, AMarkInterior);
198 
199  for (; cov->cont(); ++(*cov))
200  setMark(**cov, AMarkInterior);
201 
202  delete cov;
203 
204  if (initialMarked)
205  setMark(ADart, AMarkBorder);
206 }
207 //******************************************************************************
208 void CGMapGeneric::halfMarkIncidentCells(TOrbit AOrbit, int AMarkNumber)
209 {
210  int mark = getNewMark();
211 
212  markCopy(AMarkNumber, mark);
213  halfMarkIncidentCells(AOrbit, mark, AMarkNumber);
214  unmarkAll(mark);
215 
216  freeMark(mark);
217 }
218 //******************************************************************************
219 void CGMapGeneric::markIntersection(int AMarkNumber1, int AMarkNumber2,
220  TOrbit AOrbit1,TOrbit AOrbit2,
221  int AMarkResult,
222  bool AInitialMarkIncidentCells)
223 {
224  assert(AMarkNumber1 != AMarkNumber2);
225 
226  int m1 = getNewMark();
227  int m2 = getNewMark();
228 
229  if (AInitialMarkIncidentCells)
230  {
231  markIncidentCells(AOrbit1, AMarkNumber1, m1);
232  markIncidentCells(AOrbit1, AMarkNumber2, m2);
233  }
234  else
235  {
236  markCopy(AMarkNumber1, m1);
237  markCopy(AMarkNumber2, m2);
238  }
239 
240  markIncidentCells(AOrbit2, m1);
241  markIncidentCells(AOrbit2, m2);
242 
243  markCopy(m2, AMarkResult);
244  markAnd(m1, AMarkResult);
245 
246  unmarkAll(m1); freeMark(m1);
247  unmarkAll(m2); freeMark(m2);
248 }
249 //******************************************************************************
250 void CGMapGeneric::markCopy(int AMarkNumberFrom, int AMarkNumberTo)
251 {
252  assert(AMarkNumberFrom!=AMarkNumberTo);
253 
254  for (CDynamicCoverageAll it(this); it.cont(); ++it)
255  setMarkTo(*it, AMarkNumberTo, isMarked(*it, AMarkNumberFrom));
256 }
257 //******************************************************************************
258 void CGMapGeneric::markCopy(int AMarkNumberFrom, int AMarkNumberTo,
259  CDart * ADart, TOrbit AOrbit)
260 {
261  assert(AMarkNumberFrom!=AMarkNumberTo);
262  assert(ADart!=NULL);
263 
264  CCoverage * cov = getDynamicCoverage(ADart, AOrbit);
265 
266  for (; cov->cont(); ++(*cov))
267  setMarkTo(**cov, AMarkNumberTo, isMarked(**cov, AMarkNumberFrom));
268 
269  delete cov;
270 }
271 //******************************************************************************
272 void CGMapGeneric::markAdd(int AMarkNumberFrom, int AMarkNumberTo)
273 {
274  assert(AMarkNumberFrom!=AMarkNumberTo);
275 
276  CDynamicCoverageAll it(this);
277 
278  for (; it.cont(); ++it)
279  if (isMarked(*it, AMarkNumberFrom))
280  setMark(*it, AMarkNumberTo);
281 }
282 //******************************************************************************
283 void CGMapGeneric::markAdd(int AMarkNumberFrom, int AMarkNumberTo,
284  CDart * ADart, TOrbit AOrbit)
285 {
286  assert(AMarkNumberFrom!=AMarkNumberTo);
287  assert(ADart!=NULL);
288 
289  CCoverage * cov = getDynamicCoverage(ADart, AOrbit);
290 
291  for (; cov->cont(); ++(*cov))
292  if (isMarked(**cov, AMarkNumberFrom))
293  setMark(**cov, AMarkNumberTo);
294 
295  delete cov;
296 }
297 //******************************************************************************
298 void CGMapGeneric::markAnd(int AMarkNumberFrom, int AMarkNumberTo)
299 {
300  assert(AMarkNumberFrom!=AMarkNumberTo);
301 
302  CDynamicCoverageAll it(this);
303 
304  for (; it.cont(); ++it)
305  if (!isMarked(*it, AMarkNumberFrom))
306  unsetMark(*it, AMarkNumberTo);
307 }
308 //******************************************************************************
309 void CGMapGeneric::markAnd(int AMarkNumberFrom, int AMarkNumberTo,
310  CDart * ADart, TOrbit AOrbit)
311 {
312  assert(AMarkNumberFrom!=AMarkNumberTo);
313  assert(ADart!=NULL);
314 
315  CCoverage * cov = getDynamicCoverage(ADart, AOrbit);
316 
317  for (; cov->cont(); ++(*cov))
318  if (!isMarked(**cov, AMarkNumberFrom))
319  unsetMark(**cov, AMarkNumberTo);
320 
321  delete cov;
322 }
323 //******************************************************************************
324 void CGMapGeneric::markSub(int AMarkNumberFrom, int AMarkNumberTo)
325 {
326  assert(AMarkNumberFrom!=AMarkNumberTo);
327 
328  CDynamicCoverageAll it(this);
329 
330  for (; it.cont(); ++it)
331  if (isMarked(*it, AMarkNumberFrom))
332  unsetMark(*it, AMarkNumberTo);
333 }
334 //******************************************************************************
335 void CGMapGeneric::markSub(int AMarkNumberFrom, int AMarkNumberTo,
336  CDart * ADart, TOrbit AOrbit)
337 {
338  assert(AMarkNumberFrom!=AMarkNumberTo);
339  assert(ADart!=NULL);
340 
341  CCoverage * cov = getDynamicCoverage(ADart, AOrbit);
342 
343  for (; cov->cont(); ++(*cov))
344  if (isMarked(**cov, AMarkNumberFrom))
345  unsetMark(**cov, AMarkNumberTo);
346 
347  delete cov;
348 }
349 //******************************************************************************
350 void CGMapGeneric::markMove(int AMarkNumberFrom, int AMarkNumberTo)
351 {
352  assert(AMarkNumberFrom!=AMarkNumberTo);
353 
354  CDynamicCoverageAll it(this);
355 
356  for (; it.cont(); ++it)
357  if (isMarked(*it, AMarkNumberFrom))
358  {
359  setMark (*it, AMarkNumberTo );
360  unsetMark(*it, AMarkNumberFrom);
361  }
362 }
363 //******************************************************************************
364 void CGMapGeneric::markMove(int AMarkNumberFrom, int AMarkNumberTo,
365  CDart * ADart, TOrbit AOrbit)
366 {
367  assert(AMarkNumberFrom!=AMarkNumberTo);
368  assert(ADart!=NULL);
369 
370  CCoverage * cov = getDynamicCoverage(ADart, AOrbit);
371 
372  for (; cov->cont(); ++(*cov))
373  if (isMarked(**cov, AMarkNumberFrom))
374  {
375  setMark (**cov, AMarkNumberTo );
376  unsetMark(**cov, AMarkNumberFrom);
377  }
378 
379  delete cov;
380 }
381 //******************************************************************************
383  int AMarkNumber)
384 {
385  assert(ADart!=NULL);
386 
387  bool wholeCellMarked = true;
388  CCoverage * it = getDynamicCoverage(ADart, AOrbit);
389 
390  for (; it->cont() && wholeCellMarked; ++(*it))
391  if (!isMarked(**it, AMarkNumber))
392  wholeCellMarked = false;
393 
394  delete it;
395 
396  return wholeCellMarked;
397 }
398 //******************************************************************************
399 int CGMapGeneric::getMarkedCells(TOrbit AOrbit, int AMarkNumber,
400  CDart * ALastMarked,
401  CDart * * ADart1,
402  CDart * * ADart2,
403  CDart * * ADart3)
404 {
405  int maxToFind = ADart1==NULL ? 0 : ADart2==NULL ? 1 : ADart3==NULL ? 2 : 3;
406  int nbFound = 0;
407 
408  CDart * * dart[3] = {ADart1, ADart2, ADart3};
409 
410  int treated = getNewMark();
411 
412  for (int i=0; i<maxToFind; ++i)
413  *dart[i] = NULL;
414 
415  if (ALastMarked!=NULL && isMarked(ALastMarked, AMarkNumber))
416  {
417  markOrbit(ALastMarked, AOrbit, treated);
418  *dart[nbFound++]= ALastMarked;
419  }
420 
421  for (CDynamicCoverageAll it(this); it.cont(); ++it)
422  if (!isMarked(*it, treated))
423  {
424  if (isMarked(*it, AMarkNumber))
425  {
426  markOrbit(*it, AOrbit, treated);
427 
428  if (nbFound<maxToFind)
429  *dart[nbFound] = *it;
430 
431  ++nbFound;
432  }
433  else
434  setMark(*it, treated);
435  }
436 
437  negateMaskMark(treated);
438  freeMark(treated);
439 
440  return nbFound;
441 }
442 //******************************************************************************