Moka libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
image-3d-cea.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 //******************************************************************************
39 //******************************************************************************
40 #include "math-extension.hh" // pour la fonction pgcd()
41 
42 #include <iostream>
43 #include <fstream>
44 #include <cstring>
45 #include <cassert>
46 #include <cstdlib>
47 
48 namespace GMap3d
49 {
50 //******************************************************************************
51 INLINE
52 CImage3dCEA::CImage3dCEA(const std::string & AFilename,
53  int AFirstPlane, int ANbPlaneToRead) :
54  CImage3d(0),
55  FStreamPos(0)
56 {
57  FPrevPlane = NULL;
58  FActuPlane = NULL;
59 
60  FStream.open(AFilename.c_str(), std::ios::in);
61 
62  if (!FStream.good() || !findPlaneSize())
63  {
64  FIsOk = false;
65  return;
66  }
67 
68  FNbPlaneToRead = ANbPlaneToRead > 0 ? ANbPlaneToRead+AFirstPlane+1 : -1;
69 
70  std::cout << " XMin: " << FXMin
71  << " XMax: " << FXMax
72  << " XStep: " << FXStep
73  << " XSize: " << FWidth
74  << std::endl;
75  std::cout << " YMin: " << FYMin
76  << " YMax: " << FYMax
77  << " YStep: " << FYStep
78  << " YSize: " << FHeight
79  << std::endl;
80 
81  FIsOk = true;
82 
83  while (FIsOk && AFirstPlane >= 0)
84  {
85  --AFirstPlane;
86  FIsOk = readNextPlane();
87  }
88 
89  freePlane(false);
90 }
91 //******************************************************************************
92 INLINE
94 {
95  if (FStream.is_open())
96  FStream.close();
97 }
98 //******************************************************************************
99 INLINE
100 bool CImage3dCEA::readVoxel(int* Ax, int* Ay, int* Az, uint8_t* AColor)
101 {
102  FStreamPos = FStream.tellg();
103  FStream >> *Ax >> *Ay >> *Az >> *AColor;
104  return FStream.good();
105 }
106 //******************************************************************************
107 INLINE
108 void CImage3dCEA::unreadLastVoxel()
109 {
110  FStream.seekg(FStreamPos, std::ios::beg);
111 }
112 //******************************************************************************
113 INLINE
114 bool CImage3dCEA::findPlaneSize()
115 {
116  std::streampos memo = FStreamPos;
117  FStream.seekg(0, std::ios::beg);
118 
119  int x,y,z,z0;
120  uint8_t c;
121 
122  FWidth = FHeight = 0;
123 
124  if (! readVoxel(&x,&y,&z0,&c))
125  {
126  FStream.seekg(memo, std::ios::beg);
127  return false;
128  }
129 
130  FXMin = FXMax = x; FXStep = 0;
131  FYMin = FYMax = y; FYStep = 0;
132 
133  bool ok = readVoxel(&x,&y,&z,&c);
134  while (ok)
135  {
136  if (z == z0)
137  {
138  FXStep = pgcd(FXStep, labs(FXMin - x));
139  FYStep = pgcd(FYStep, labs(FYMin - y));
140 
141  if (x < FXMin) FXMin = x;
142  if (x > FXMax) FXMax = x;
143  if (y < FYMin) FYMin = y;
144  if (y > FYMax) FYMax = y;
145 
146  ok = readVoxel(&x,&y,&z,&c);
147  }
148  else
149  ok = false;
150  }
151 
152  if (FXStep == 0) FXStep = 1;
153  if (FYStep == 0) FYStep = 1;
154 
155  FWidth = (FXMax - FXMin) / FXStep + 1;
156  FHeight = (FYMax - FYMin) / FYStep + 1;
157 
158  FStreamPos = memo;
159  FStream.seekg(FStreamPos, std::ios::beg);
160  return true;
161 }
162 //******************************************************************************
163 INLINE
164 uint8_t** CImage3dCEA::allocArray()
165 {
166  uint8_t** result = new uint8_t* [FWidth];
167 
168  for (unsigned int i=0; i<FWidth; ++i)
169  result[i] = new uint8_t [FHeight];
170 
171  return result;
172 }
173 
174 INLINE
175 void CImage3dCEA::freeArray(uint8_t** AArray)
176 {
177  if (AArray == NULL)
178  return;
179 
180  for (unsigned int i=0; i<FWidth; ++i)
181  delete [] AArray[i];
182 
183  delete [] AArray;
184 }
185 //******************************************************************************
186 INLINE
188 {
189  FActuPlane = allocArray();
190 
191  for (unsigned int x=0; x<FWidth; ++x)
192  for (unsigned int y=0; y<FHeight; ++y)
193  FActuPlane[x][y] = 0;
194 
195  if (FStream.eof())
196  return false;
197 
198  int x,y,z,Z;
199  uint8_t c;
200  readVoxel(&x,&y,&Z,&c);
201  unreadLastVoxel();
202 
203  bool ok = true;
204  while (ok)
205  {
206  ok = readVoxel(&x,&y,&z,&c) && z==Z;
207 
208  if (ok && FXMin <= x && x <= FXMax && FYMin <= y && y <= FYMax)
209  FActuPlane[(x-FXMin)/FXStep][(y-FYMin)/FYStep] = c;
210  }
211 
212  if (! FStream.eof())
213  unreadLastVoxel();
214 
215  return true;
216 }
217 //******************************************************************************
218 INLINE
219 bool CImage3dCEA::sameVoxel(unsigned int Ax1, unsigned int Ay1, bool Az1,
220  unsigned int Ax2, unsigned int Ay2, bool Az2) const
221 {
222  return
223  (Az1 ? FActuPlane : FPrevPlane)[Ax1][Ay1] ==
224  (Az2 ? FActuPlane : FPrevPlane)[Ax2][Ay2];
225 }
226 //******************************************************************************
227 INLINE
228 bool CImage3dCEA::sameVoxel(unsigned int Ax, unsigned int Ay, bool Az,
229  int ARed, int, int, int ) const
230 {
231  return (Az ? FActuPlane : FPrevPlane)[Ax][Ay] == ARed;
232 }
233 //******************************************************************************
234 INLINE
235 void CImage3dCEA::freePlane(bool AActuPlane)
236 {
237  CImage3d::freePlane(AActuPlane);
238 
239  uint8_t** & plane = AActuPlane ? FActuPlane : FPrevPlane;
240 
241  freeArray(plane);
242  plane = NULL;
243 }
244 //******************************************************************************
245 INLINE
247 {
249 
250  FPrevPlane = FActuPlane;
251  FActuPlane = NULL;
252 }
253 //******************************************************************************
254 } // namespace GMap3d