FeatureSet.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  FeatureSet.cpp
00037 // Copyright: Keyetech UG (haftungsbeschraenkt)
00038 // Author:    Pedram Azad
00039 // Date:      10.02.2010
00040 // ****************************************************************************
00041 
00042 
00043 // ****************************************************************************
00044 // Includes
00045 // ****************************************************************************
00046 
00047 #include "FeatureSet.h"
00048 #include "FeatureEntry.h"
00049 #include "Helpers/helpers.h"
00050 
00051 #include "Features/SIFTFeatures/SIFTFeatureEntry.h"
00052 
00053 #include <stdio.h>
00054 
00055 
00056 
00057 #define HEADER_FEATURE_SET      "FEATURESET"
00058 
00059 
00060 
00061 // ****************************************************************************
00062 // Constructor / Destructor
00063 // ****************************************************************************
00064 
00065 CFeatureSet::CFeatureSet() : m_featureArray(true, 1000), m_contourPointArray(10)
00066 {
00067 }
00068 
00069 CFeatureSet::~CFeatureSet()
00070 {
00071 }
00072 
00073 
00074 // ****************************************************************************
00075 // Methods
00076 // ****************************************************************************
00077 
00078 void CFeatureSet::SetName(const char *pName)
00079 {
00080         m_sName = "";
00081         m_sName += pName;
00082 }
00083 
00084 void CFeatureSet::AddFeature(const CFeatureEntry *pFeature)
00085 {
00086         m_featureArray.AddElement(pFeature->Clone());
00087 };
00088 
00089 void CFeatureSet::AddFeatureWithoutCloning(CFeatureEntry *pFeature)
00090 {
00091         m_featureArray.AddElement(pFeature);
00092 };
00093 
00094 void CFeatureSet::ClearFeatureList()
00095 {
00096         m_featureArray.Clear();
00097 }
00098 
00099 void CFeatureSet::AddContourPoint(const Vec2d &point)
00100 {
00101         ContourPoint element;
00102         Math2d::SetVec(element.point, point);
00103         Math3d::SetVec(element.point3d, Math3d::zero_vec);
00104         element.bHas3dPoint = false;
00105 
00106         m_contourPointArray.AddElement(element);
00107 }
00108 
00109 void CFeatureSet::AddContourPoint(const Vec2d &point, const Vec3d &point3d)
00110 {
00111         ContourPoint element;
00112         Math2d::SetVec(element.point, point);
00113         Math3d::SetVec(element.point3d, point3d);
00114         element.bHas3dPoint = true;
00115 
00116         m_contourPointArray.AddElement(element);
00117 }
00118 
00119 void CFeatureSet::ClearContourPointList()
00120 {
00121         m_contourPointArray.Clear();
00122 }
00123 
00124 
00125 bool CFeatureSet::SaveToFile(const char *pFileName) const
00126 {
00127         FILE *f = fopen(pFileName, "wb");
00128         if (!f)
00129                 return false;
00130         
00131         if (fwrite(HEADER_FEATURE_SET, sizeof(HEADER_FEATURE_SET) - 1, 1, f) != 1)
00132                 return false;
00133 
00134         int i, temp;
00135 
00136         // name
00137         #ifdef IVT_BIG_ENDIAN
00138         temp = invert_byte_order_int(m_sName.length());
00139         #else
00140         temp = (int) m_sName.length();
00141         #endif
00142 
00143         if (fwrite(&temp, sizeof(int), 1, f) != 1)
00144                 return false;
00145 
00146         if (m_sName.length() > 0)
00147         {
00148                 if (fwrite(m_sName.c_str(), m_sName.length(), 1, f) != 1)
00149                         return false;
00150         }
00151 
00152         // contour points
00153         const int nContourPoints = m_contourPointArray.GetSize();
00154         
00155         #ifdef IVT_BIG_ENDIAN
00156         temp = invert_byte_order_int(nContourPoints);
00157         #else
00158         temp = nContourPoints;
00159         #endif
00160         
00161         if (fwrite(&temp, sizeof(int), 1, f) != 1)
00162                 return false;
00163         
00164         for (i = 0; i < nContourPoints; i++)
00165         {
00166                 const ContourPoint &point = m_contourPointArray[i];
00167                 
00168                 #ifdef IVT_BIG_ENDIAN
00169                 const float u = invert_byte_order_float(point.point.x);
00170                 const float v = invert_byte_order_float(point.point.y);
00171                 const float x = invert_byte_order_float(point.point3d.x);
00172                 const float y = invert_byte_order_float(point.point3d.y);
00173                 const float z = invert_byte_order_float(point.point3d.z);
00174                 const int nHas3dPoint = invert_byte_order_int(int(point.bHas3dPoint));
00175                 #else
00176                 const float u = point.point.x;
00177                 const float v = point.point.y;
00178                 const float x = point.point3d.x;
00179                 const float y = point.point3d.y;
00180                 const float z = point.point3d.z;
00181                 const int nHas3dPoint = int(point.bHas3dPoint);
00182                 #endif
00183                 
00184                 if (fwrite(&u, sizeof(float), 1, f) != 1)
00185                         return false;
00186                 
00187                 if (fwrite(&v, sizeof(float), 1, f) != 1)
00188                         return false;
00189 
00190                 if (fwrite(&x, sizeof(float), 1, f) != 1)
00191                         return false;
00192 
00193                 if (fwrite(&y, sizeof(float), 1, f) != 1)
00194                         return false;
00195 
00196                 if (fwrite(&z, sizeof(float), 1, f) != 1)
00197                         return false;
00198 
00199                 if (fwrite(&nHas3dPoint, sizeof(int), 1, f) != 1)
00200                         return false;
00201         }
00202         
00203         // features
00204         const int nFeatures = m_featureArray.GetSize();
00205         
00206         #ifdef IVT_BIG_ENDIAN
00207         temp = invert_byte_order_int(nFeatures);
00208         #else
00209         temp = nFeatures;
00210         #endif
00211         
00212         if (fwrite(&temp, sizeof(int), 1, f) != 1)
00213                 return false;
00214         
00215         for (i = 0; i < nFeatures; i++)
00216         {
00217                 const CFeatureEntry *pFeatureEntry = m_featureArray[i];
00218 
00219                 #ifdef IVT_BIG_ENDIAN
00220                 int type = invert_byte_order_int(pFeatureEntry->GetType());
00221                 #else
00222                 int type = pFeatureEntry->GetType();
00223                 #endif
00224 
00225                 if (fwrite(&type, sizeof(int), 1, f) != 1) 
00226                         return false;
00227 
00228                 if (!pFeatureEntry->WriteToFile(f))
00229                         return false;
00230         }
00231 
00232         fclose(f);
00233 
00234         return true;
00235 }
00236 
00237 bool CFeatureSet::LoadFromFile(const char *pFileName)
00238 {
00239         m_featureArray.Clear();
00240         m_contourPointArray.Clear();
00241         
00242         FILE *f = fopen(pFileName, "rb");
00243         if (!f)
00244                 return false;
00245         
00246         char buffer[sizeof(HEADER_FEATURE_SET)];
00247         if (fread(buffer, sizeof(HEADER_FEATURE_SET) - 1, 1, f) != 1)
00248                 return false;
00249         buffer[sizeof(HEADER_FEATURE_SET) - 1] = '\0';
00250         if (strcmp(buffer, HEADER_FEATURE_SET) != 0)
00251         {
00252                 fclose(f);
00253                 return false;
00254         }
00255 
00256         // name
00257         int nStringLength;
00258         if (fread(&nStringLength, sizeof(int), 1, f) != 1)
00259                 return false;
00260 
00261         #ifdef IVT_BIG_ENDIAN
00262         nStringLength = invert_byte_order_int(nStringLength);
00263         #endif
00264 
00265         if (nStringLength < 0 || nStringLength > 4096)
00266                 return false;
00267 
00268         m_sName = "";
00269         
00270         if (nStringLength > 0)
00271         {
00272                 // allocate memory
00273                 char *pBuffer = new char[nStringLength + 1];
00274                 
00275                 // read name from file
00276                 if (fread(pBuffer, nStringLength, 1, f) != 1)
00277                         return false;
00278                 
00279                 // set termination
00280                 pBuffer[nStringLength] = '\0';
00281                 
00282                 // update name
00283                 m_sName += pBuffer;
00284                 
00285                 // free memory
00286                 delete [] pBuffer;
00287         }
00288         
00289         int i;
00290         
00291         // contour points
00292         int nContourPoints;
00293         if (fread(&nContourPoints, sizeof(int), 1, f) != 1)
00294         {
00295                 fclose(f);
00296                 return false;
00297         }
00298         
00299         #ifdef IVT_BIG_ENDIAN
00300         nContourPoints = invert_byte_order_int(nContourPoints);
00301         #endif
00302 
00303         if (nContourPoints < 0)
00304                 return false;
00305         
00306         for (i = 0; i < nContourPoints; i++)
00307         {
00308                 float u, v, x, y, z;
00309                 int nHas3dPoint;
00310 
00311                 if (fread(&u, sizeof(float), 1, f) != 1)
00312                 {
00313                         fclose(f);
00314                         return false;
00315                 }
00316                 
00317                 if (fread(&v, sizeof(float), 1, f) != 1)
00318                 {
00319                         fclose(f);
00320                         return false;
00321                 }
00322 
00323                 if (fread(&x, sizeof(float), 1, f) != 1)
00324                 {
00325                         fclose(f);
00326                         return false;
00327                 }
00328 
00329                 if (fread(&y, sizeof(float), 1, f) != 1)
00330                 {
00331                         fclose(f);
00332                         return false;
00333                 }
00334 
00335                 if (fread(&z, sizeof(float), 1, f) != 1)
00336                 {
00337                         fclose(f);
00338                         return false;
00339                 }
00340 
00341                 if (fread(&nHas3dPoint, sizeof(int), 1, f) != 1)
00342                 {
00343                         fclose(f);
00344                         return false;
00345                 }
00346 
00347                 #ifdef IVT_BIG_ENDIAN
00348                 u = invert_byte_order_float(u);
00349                 v = invert_byte_order_float(v);
00350                 x = invert_byte_order_float(x);
00351                 y = invert_byte_order_float(y);
00352                 z = invert_byte_order_float(z);
00353                 nHas3dPoint = invert_byte_order_float(nHas3dPoint);
00354                 #endif
00355 
00356                 ContourPoint point;
00357                 Math2d::SetVec(point.point, u, v);
00358                 Math3d::SetVec(point.point3d, x, y, z);
00359                 point.bHas3dPoint = nHas3dPoint ? true : false;
00360                         
00361                 m_contourPointArray.AddElement(point);
00362         }
00363         
00364         // features
00365         int nFeatures;
00366         if (fread(&nFeatures, sizeof(int), 1, f) != 1)
00367         {
00368                 fclose(f);
00369                 return false;
00370         }
00371 
00372         if (nFeatures < 0)
00373                 return false;
00374         
00375         #ifdef IVT_BIG_ENDIAN
00376         nFeatures = invert_byte_order_int(nFeatures);
00377         #endif
00378         
00379         for (i = 0; i < nFeatures; i++)
00380         {
00381                 int type;
00382 
00383                 if (fread(&type, sizeof(int), 1, f) != 1)
00384                 {
00385                         fclose(f);
00386                         return false;
00387                 }
00388 
00389                 #ifdef IVT_BIG_ENDIAN
00390                 type = invert_byte_order_int(type);
00391                 #endif
00392 
00393                 CFeatureEntry *pFeatureEntry;
00394 
00395                 switch (type) 
00396                 {
00397                         case CFeatureEntry::tSIFT:
00398                                 pFeatureEntry = new CSIFTFeatureEntry();
00399                         break;
00400 
00401                         default:
00402                                 fclose(f);
00403                                 printf("error: type %i in file '%s' is note supported\n", type, pFileName);
00404                                 return false;
00405                 }
00406 
00407                 if (!pFeatureEntry->ReadFromFile(f)) 
00408                 {
00409                         printf("error: ReadFromFile\n");
00410                         fclose(f);
00411                         return false;
00412                 }
00413 
00414                 m_featureArray.AddElement(pFeatureEntry);
00415         }
00416 
00417         fclose(f);
00418 
00419         return true;
00420 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57