IVT
FeatureSet.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: FeatureSet.cpp
37 // Copyright: Keyetech UG (haftungsbeschraenkt)
38 // Author: Pedram Azad
39 // Date: 10.02.2010
40 // ****************************************************************************
41 
42 
43 // ****************************************************************************
44 // Includes
45 // ****************************************************************************
46 
47 #include "FeatureSet.h"
48 #include "FeatureEntry.h"
49 #include "Helpers/helpers.h"
50 
52 
53 #include <stdio.h>
54 
55 
56 
57 #define HEADER_FEATURE_SET "FEATURESET"
58 
59 
60 
61 // ****************************************************************************
62 // Constructor / Destructor
63 // ****************************************************************************
64 
65 CFeatureSet::CFeatureSet() : m_featureArray(true, 1000), m_contourPointArray(10)
66 {
67 }
68 
70 {
71 }
72 
73 
74 // ****************************************************************************
75 // Methods
76 // ****************************************************************************
77 
78 void CFeatureSet::SetName(const char *pName)
79 {
80  m_sName = "";
81  m_sName += pName;
82 }
83 
85 {
86  m_featureArray.AddElement(pFeature->Clone());
87 };
88 
90 {
91  m_featureArray.AddElement(pFeature);
92 };
93 
95 {
96  m_featureArray.Clear();
97 }
98 
100 {
101  ContourPoint element;
102  Math2d::SetVec(element.point, point);
104  element.bHas3dPoint = false;
105 
106  m_contourPointArray.AddElement(element);
107 }
108 
109 void CFeatureSet::AddContourPoint(const Vec2d &point, const Vec3d &point3d)
110 {
111  ContourPoint element;
112  Math2d::SetVec(element.point, point);
113  Math3d::SetVec(element.point3d, point3d);
114  element.bHas3dPoint = true;
115 
116  m_contourPointArray.AddElement(element);
117 }
118 
120 {
121  m_contourPointArray.Clear();
122 }
123 
124 
125 bool CFeatureSet::SaveToFile(const char *pFileName) const
126 {
127  FILE *f = fopen(pFileName, "wb");
128  if (!f)
129  return false;
130 
131  if (fwrite(HEADER_FEATURE_SET, sizeof(HEADER_FEATURE_SET) - 1, 1, f) != 1)
132  return false;
133 
134  int i, temp;
135 
136  // name
137  #ifdef IVT_BIG_ENDIAN
138  temp = invert_byte_order_int(m_sName.length());
139  #else
140  temp = (int) m_sName.length();
141  #endif
142 
143  if (fwrite(&temp, sizeof(int), 1, f) != 1)
144  return false;
145 
146  if (m_sName.length() > 0)
147  {
148  if (fwrite(m_sName.c_str(), m_sName.length(), 1, f) != 1)
149  return false;
150  }
151 
152  // contour points
153  const int nContourPoints = m_contourPointArray.GetSize();
154 
155  #ifdef IVT_BIG_ENDIAN
156  temp = invert_byte_order_int(nContourPoints);
157  #else
158  temp = nContourPoints;
159  #endif
160 
161  if (fwrite(&temp, sizeof(int), 1, f) != 1)
162  return false;
163 
164  for (i = 0; i < nContourPoints; i++)
165  {
166  const ContourPoint &point = m_contourPointArray[i];
167 
168  #ifdef IVT_BIG_ENDIAN
169  const float u = invert_byte_order_float(point.point.x);
170  const float v = invert_byte_order_float(point.point.y);
171  const float x = invert_byte_order_float(point.point3d.x);
172  const float y = invert_byte_order_float(point.point3d.y);
173  const float z = invert_byte_order_float(point.point3d.z);
174  const int nHas3dPoint = invert_byte_order_int(int(point.bHas3dPoint));
175  #else
176  const float u = point.point.x;
177  const float v = point.point.y;
178  const float x = point.point3d.x;
179  const float y = point.point3d.y;
180  const float z = point.point3d.z;
181  const int nHas3dPoint = int(point.bHas3dPoint);
182  #endif
183 
184  if (fwrite(&u, sizeof(float), 1, f) != 1)
185  return false;
186 
187  if (fwrite(&v, sizeof(float), 1, f) != 1)
188  return false;
189 
190  if (fwrite(&x, sizeof(float), 1, f) != 1)
191  return false;
192 
193  if (fwrite(&y, sizeof(float), 1, f) != 1)
194  return false;
195 
196  if (fwrite(&z, sizeof(float), 1, f) != 1)
197  return false;
198 
199  if (fwrite(&nHas3dPoint, sizeof(int), 1, f) != 1)
200  return false;
201  }
202 
203  // features
204  const int nFeatures = m_featureArray.GetSize();
205 
206  #ifdef IVT_BIG_ENDIAN
207  temp = invert_byte_order_int(nFeatures);
208  #else
209  temp = nFeatures;
210  #endif
211 
212  if (fwrite(&temp, sizeof(int), 1, f) != 1)
213  return false;
214 
215  for (i = 0; i < nFeatures; i++)
216  {
217  const CFeatureEntry *pFeatureEntry = m_featureArray[i];
218 
219  #ifdef IVT_BIG_ENDIAN
220  int type = invert_byte_order_int(pFeatureEntry->GetType());
221  #else
222  int type = pFeatureEntry->GetType();
223  #endif
224 
225  if (fwrite(&type, sizeof(int), 1, f) != 1)
226  return false;
227 
228  if (!pFeatureEntry->WriteToFile(f))
229  return false;
230  }
231 
232  fclose(f);
233 
234  return true;
235 }
236 
237 bool CFeatureSet::LoadFromFile(const char *pFileName)
238 {
239  m_featureArray.Clear();
240  m_contourPointArray.Clear();
241 
242  FILE *f = fopen(pFileName, "rb");
243  if (!f)
244  return false;
245 
246  char buffer[sizeof(HEADER_FEATURE_SET)];
247  if (fread(buffer, sizeof(HEADER_FEATURE_SET) - 1, 1, f) != 1)
248  return false;
249  buffer[sizeof(HEADER_FEATURE_SET) - 1] = '\0';
250  if (strcmp(buffer, HEADER_FEATURE_SET) != 0)
251  {
252  fclose(f);
253  return false;
254  }
255 
256  // name
257  int nStringLength;
258  if (fread(&nStringLength, sizeof(int), 1, f) != 1)
259  return false;
260 
261  #ifdef IVT_BIG_ENDIAN
262  nStringLength = invert_byte_order_int(nStringLength);
263  #endif
264 
265  if (nStringLength < 0 || nStringLength > 4096)
266  return false;
267 
268  m_sName = "";
269 
270  if (nStringLength > 0)
271  {
272  // allocate memory
273  char *pBuffer = new char[nStringLength + 1];
274 
275  // read name from file
276  if (fread(pBuffer, nStringLength, 1, f) != 1)
277  return false;
278 
279  // set termination
280  pBuffer[nStringLength] = '\0';
281 
282  // update name
283  m_sName += pBuffer;
284 
285  // free memory
286  delete [] pBuffer;
287  }
288 
289  int i;
290 
291  // contour points
292  int nContourPoints;
293  if (fread(&nContourPoints, sizeof(int), 1, f) != 1)
294  {
295  fclose(f);
296  return false;
297  }
298 
299  #ifdef IVT_BIG_ENDIAN
300  nContourPoints = invert_byte_order_int(nContourPoints);
301  #endif
302 
303  if (nContourPoints < 0)
304  return false;
305 
306  for (i = 0; i < nContourPoints; i++)
307  {
308  float u, v, x, y, z;
309  int nHas3dPoint;
310 
311  if (fread(&u, sizeof(float), 1, f) != 1)
312  {
313  fclose(f);
314  return false;
315  }
316 
317  if (fread(&v, sizeof(float), 1, f) != 1)
318  {
319  fclose(f);
320  return false;
321  }
322 
323  if (fread(&x, sizeof(float), 1, f) != 1)
324  {
325  fclose(f);
326  return false;
327  }
328 
329  if (fread(&y, sizeof(float), 1, f) != 1)
330  {
331  fclose(f);
332  return false;
333  }
334 
335  if (fread(&z, sizeof(float), 1, f) != 1)
336  {
337  fclose(f);
338  return false;
339  }
340 
341  if (fread(&nHas3dPoint, sizeof(int), 1, f) != 1)
342  {
343  fclose(f);
344  return false;
345  }
346 
347  #ifdef IVT_BIG_ENDIAN
353  nHas3dPoint = invert_byte_order_float(nHas3dPoint);
354  #endif
355 
356  ContourPoint point;
357  Math2d::SetVec(point.point, u, v);
358  Math3d::SetVec(point.point3d, x, y, z);
359  point.bHas3dPoint = nHas3dPoint ? true : false;
360 
361  m_contourPointArray.AddElement(point);
362  }
363 
364  // features
365  int nFeatures;
366  if (fread(&nFeatures, sizeof(int), 1, f) != 1)
367  {
368  fclose(f);
369  return false;
370  }
371 
372  if (nFeatures < 0)
373  return false;
374 
375  #ifdef IVT_BIG_ENDIAN
376  nFeatures = invert_byte_order_int(nFeatures);
377  #endif
378 
379  for (i = 0; i < nFeatures; i++)
380  {
381  int type;
382 
383  if (fread(&type, sizeof(int), 1, f) != 1)
384  {
385  fclose(f);
386  return false;
387  }
388 
389  #ifdef IVT_BIG_ENDIAN
390  type = invert_byte_order_int(type);
391  #endif
392 
393  CFeatureEntry *pFeatureEntry;
394 
395  switch (type)
396  {
398  pFeatureEntry = new CSIFTFeatureEntry();
399  break;
400 
401  default:
402  fclose(f);
403  printf("error: type %i in file '%s' is note supported\n", type, pFileName);
404  return false;
405  }
406 
407  if (!pFeatureEntry->ReadFromFile(f))
408  {
409  printf("error: ReadFromFile\n");
410  fclose(f);
411  return false;
412  }
413 
414  m_featureArray.AddElement(pFeatureEntry);
415  }
416 
417  fclose(f);
418 
419  return true;
420 }
Data structure for the representation of a 2D vector.
Definition: Math2d.h:82
float y
Definition: Math3d.h:75
float invert_byte_order_float(float x)
Definition: helpers.cpp:73
virtual CFeatureEntry * Clone() const =0
float x
Definition: Math2d.h:84
Base class for the representation of local features.
Definition: FeatureEntry.h:72
virtual bool WriteToFile(FILE *pFile) const
Definition: FeatureEntry.h:200
float x
Definition: Math3d.h:75
unsigned int invert_byte_order_int(unsigned int x)
Definition: helpers.cpp:89
Definition: FeatureEntry.h:76
void ClearFeatureList()
Definition: FeatureSet.cpp:94
float y
Definition: Math2d.h:84
#define HEADER_FEATURE_SET
Definition: FeatureSet.cpp:57
void AddFeature(const CFeatureEntry *pFeature)
Definition: FeatureSet.cpp:84
bool LoadFromFile(const char *pFileName)
Definition: FeatureSet.cpp:237
const Vec3d zero_vec
Definition: Math3d.cpp:59
Data structure for the representation of SIFT features.
Data structure for the representation of a 3D vector.
Definition: Math3d.h:73
virtual bool ReadFromFile(FILE *pFile)
Definition: FeatureEntry.h:161
float z
Definition: Math3d.h:75
void AddFeatureWithoutCloning(CFeatureEntry *pFeature)
Definition: FeatureSet.cpp:89
virtual eFeatureType GetType() const =0
bool SaveToFile(const char *pFileName) const
Definition: FeatureSet.cpp:125
void SetName(const char *pName)
Definition: FeatureSet.cpp:78
void AddContourPoint(const Vec2d &point)
Definition: FeatureSet.cpp:99
void ClearContourPointList()
Definition: FeatureSet.cpp:119
void SetVec(Vec2d &vec, float x, float y)
Definition: Math2d.cpp:68
void SetVec(Vec3d &vec, float x, float y, float z)
Definition: Math3d.cpp:243