Moka libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
image-2d-extraction.icc
Go to the documentation of this file.
1 /*
2  * lib-extraction-images : Extraction de cartes à partir d'images 2D et 3D.
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-extraction-images
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 "geometry.hh"
26 #include "pixel-step.hh"
27 
28 namespace GMap3d
29 {
30 //******************************************************************************
31 #ifndef MODULE_EXTRACTION_IMAGE_WITHOUT_MAGICK
32 //------------------------------------------------------------------------------
33 INLINE
34 CDart* CExtractionImage::createBorder2d(unsigned int columns)
35 {
36  CDartVertex* prev;
37  CDartVertex* actu;
38  CDartVertex* first = (CDartVertex *) FMap->addMapDart();
39  FMap->linkAlpha0(first, FMap->addMapDart());
40 
41  FMap->addAttribute(first, ORBIT_VERTEX, new CAttributeVertex(0,STEP2D_Y,0));
42 
43  prev = first;
44 
45  for (unsigned int i=0;i<columns;++i)
46  {
47  actu = (CDartVertex *) FMap->addMapDart();
48  FMap->linkAlpha0(actu, FMap->addMapDart());
49  FMap->linkAlpha1(actu, FMap->alpha0(prev));
50 
51  FMap->addAttribute(actu, ORBIT_VERTEX, new CAttributeVertex(i*STEP2D_X,0,0));
52 
53  prev = actu;
54  }
55 
56  actu = (CDartVertex *) FMap->addMapDart();
57  FMap->linkAlpha0(actu, FMap->addMapDart());
58  FMap->addAttribute(actu, ORBIT_VERTEX, new CAttributeVertex(columns*STEP2D_X,0,0));
59 
60  FMap->linkAlpha1(actu, FMap->alpha0(prev));
61  FMap->linkAlpha1(FMap->alpha0(actu), first);
62 
63  return first;
64 }
65 //******************************************************************************
66 INLINE
67 CDart* CExtractionImage::computeUpFromLast2d(CDart* ADart)
68 {
69  CDart* res = FMap->alpha01(ADart);
70 
71  while(!FMap->isFree2(res))
72  res = FMap->alpha21(res);
73 
74  return res;
75 }
76 //******************************************************************************
77 INLINE
78 CDartVertex* CExtractionImage::createFaceForExtract2d(unsigned int x, unsigned int y)
79 {
80  CDartVertex* first = (CDartVertex *) FMap->createTopoPolygon(4);
81 
82  FMap->addAttribute(first, ORBIT_VERTEX,
83  new CAttributeVertex(x*STEP2D_X,y*STEP2D_Y,0));
84 
85  return first;
86 }
87 
88 //******************************************************************************
89 INLINE
90 bool CExtractionImage::samePixelActuLeft2d(Magick::Image & image,
91  unsigned int x, unsigned int y)
92 {
93  if (y==image.rows()) return true;
94  if (x==0 || x==image.columns()) return false;
95 
96  return image.pixelColor(x,y)==image.pixelColor(x-1,y);
97 }
98 //******************************************************************************
99 INLINE
100 bool CExtractionImage::samePixelActuUp2d(Magick::Image &image,
101  unsigned int x, unsigned int y)
102 {
103  if (x==image.columns()) return true;
104  if (y==0 || y==image.rows()) return false;
105 
106  return image.pixelColor(x,y)==image.pixelColor(x,y-1);
107 }
108 //******************************************************************************
109 INLINE
110 void CExtractionImage::mergeImage2dFaces(CDart* ADart)
111 {
112  CDart *tmp, *tmp2;
113 
114  if (FMap->alpha2(ADart)!=FMap->alpha1(ADart))
115  {
116  tmp=FMap->alpha1(ADart); tmp2=FMap->alpha21(ADart);
117 
118  FMap->unsew1(ADart); FMap->unsew1(FMap->alpha2(ADart));
119 
120  FMap->sew1(tmp,tmp2);
121  }
122 
123  if (FMap->alpha02(ADart)!=FMap->alpha01(ADart))
124  {
125  tmp=FMap->alpha01(ADart); tmp2=FMap->alpha021(ADart);
126 
127  FMap->unsew1(FMap->alpha0(ADart)); FMap->unsew1(FMap->alpha02(ADart));
128 
129  FMap->sew1(tmp,tmp2);
130  }
131 
132  FMap->delMapDart(FMap->alpha02(ADart));
133  FMap->delMapDart(FMap->alpha2(ADart));
134  FMap->delMapDart(FMap->alpha0(ADart));
135  FMap->delMapDart(ADart);
136 }
137 //******************************************************************************
138 INLINE
139 void CExtractionImage::mergeImage2dEdges(CDart* ADart)
140  // Attention : cette méthode plante si le sommet est de degré un.
141 {
142  CDart *tmp, *tmp2;
143 
144  tmp = FMap->alpha0(ADart);
145  tmp2 = FMap->alpha10(ADart);
146 
147  FMap->unlinkAlpha0(FMap->alpha12(ADart)); FMap->unlinkAlpha0(FMap->alpha1(ADart));
148  FMap->unlinkAlpha0(FMap->alpha2(ADart)); FMap->unlinkAlpha0(ADart);
149 
150  FMap->linkAlpha0(FMap->alpha2(tmp),FMap->alpha2(tmp2));
151  FMap->linkAlpha0(tmp,tmp2);
152 
153  FMap->delMapDart(FMap->alpha12(ADart));
154  FMap->delMapDart(FMap->alpha2(ADart));
155  FMap->delMapDart(FMap->alpha1(ADart));
156  FMap->delMapDart(ADart);
157 }
158 //******************************************************************************
159 INLINE
160 bool CExtractionImage::isDegre2Vertex2d(CDart* ADart)
161 {
162  if (FMap->alpha2(ADart)==FMap->alpha1(ADart)) return false;
163 
164  return (FMap->alpha12(ADart)==FMap->alpha21(ADart));
165 }
166 //------------------------------------------------------------------------------
167 #endif // MODULE_EXTRACTION_IMAGE_WITHOUT_MAGICK
168 //******************************************************************************
169 INLINE
170 bool CExtractionImage::areEdgesAlign(CDart* ADart, CDart* ADart2)
171 {
172  CAttributeVertex C = *FMap->findVertex(ADart);
173 
174  return CGeometry::areColinear(*FMap->findVertex(FMap->alpha0(ADart)) - *FMap->findVertex(ADart),
175  *FMap->findVertex(FMap->alpha0(ADart2)) - *FMap->findVertex(ADart2));
176 }
177 //******************************************************************************
178 INLINE
179 bool CExtractionImage::extract2dImage(const std::string & AFilename)
180 {
181 #ifdef MODULE_EXTRACTION_IMAGE_WITHOUT_MAGICK
182  return false;
183 #else
184  try {
185  Magick::Image image(AFilename);
186 
187  std::cout<<"Apres lecture dans CExtractionImage::extract2dImage"<<std::endl;
188  std::cout<<" columns="<<image.columns()<<std::endl
189  <<" rows="<<image.rows()<<std::endl;
190 
191 
192  CDart* last = createBorder2d(image.columns());
193  CDart* tmp;
194 
195  // return true;
196 
197  unsigned int i,j;
198  for(j=0;j<=image.rows();++j)
199  for(i=0;i<=image.columns();++i)
200  {
201  tmp = createFaceForExtract2d((i+1)%(image.columns()+1),
202  j+1+((i+1)/(image.columns()+1)));
203  FMap->topoSew2(last,FMap->alpha101(tmp));
204  FMap->topoSew2(computeUpFromLast2d(last),FMap->alpha010(tmp));
205 
206  last = tmp;
207  tmp = NULL;
208 
209  // Pour le niveau 1
210  if (samePixelActuLeft2d(image,i,j))
211  {
212  mergeImage2dFaces(FMap->alpha101(last));
213  tmp = FMap->alpha010(last);
214  }
215 
216  if (samePixelActuUp2d(image,i,j))
217  {
218  mergeImage2dFaces(FMap->alpha01(last));
219 
220  if (tmp==NULL) tmp = FMap->alpha1010(last);
221  else tmp = NULL;
222  }
223 
224  // Pour le niveau 2
225  if ( tmp!=NULL &&
226  isDegre2Vertex2d(tmp) &&
227  areEdgesAlign(tmp,FMap->alpha1(tmp)) )
228  mergeImage2dEdges(tmp);
229  }
230 
231  CStaticCoverage01 it(FMap,last);
232  while(it.cont())
233  FMap->delMapDart(it++);
234 
235  }
236  catch( Magick::Exception &error_ )
237  {
238  std::cerr << "Exception dans CExtractionImage::extract2dImage: "
239  << error_.what() << std::endl;
240  return false;
241  }
242 
243  return true;
244 #endif // MODULE_EXTRACTION_IMAGE_WITHOUT_MAGICK
245 }
246 //******************************************************************************
247 } // namespace GMap3d