Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
saving.h
00001 /*********************************************************************** 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. 00005 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * 1. Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE NNIndexGOODS OR SERVICES; LOSS OF USE, 00023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 *************************************************************************/ 00028 00029 #ifndef OPENCV_FLANN_SAVING_H_ 00030 #define OPENCV_FLANN_SAVING_H_ 00031 00032 #include <cstring> 00033 #include <vector> 00034 00035 #include "general.h" 00036 #include "nn_index.h" 00037 00038 #ifdef FLANN_SIGNATURE_ 00039 #undef FLANN_SIGNATURE_ 00040 #endif 00041 #define FLANN_SIGNATURE_ "FLANN_INDEX" 00042 00043 namespace cvflann 00044 { 00045 00046 template <typename T> 00047 struct Datatype {}; 00048 template<> 00049 struct Datatype<char> { static flann_datatype_t type() { return FLANN_INT8; } }; 00050 template<> 00051 struct Datatype<short> { static flann_datatype_t type() { return FLANN_INT16; } }; 00052 template<> 00053 struct Datatype<int> { static flann_datatype_t type() { return FLANN_INT32; } }; 00054 template<> 00055 struct Datatype<unsigned char> { static flann_datatype_t type() { return FLANN_UINT8; } }; 00056 template<> 00057 struct Datatype<unsigned short> { static flann_datatype_t type() { return FLANN_UINT16; } }; 00058 template<> 00059 struct Datatype<unsigned int> { static flann_datatype_t type() { return FLANN_UINT32; } }; 00060 template<> 00061 struct Datatype<float> { static flann_datatype_t type() { return FLANN_FLOAT32; } }; 00062 template<> 00063 struct Datatype<double> { static flann_datatype_t type() { return FLANN_FLOAT64; } }; 00064 00065 00066 /** 00067 * Structure representing the index header. 00068 */ 00069 struct IndexHeader 00070 { 00071 char signature[16]; 00072 char version[16]; 00073 flann_datatype_t data_type; 00074 flann_algorithm_t index_type; 00075 size_t rows; 00076 size_t cols; 00077 }; 00078 00079 /** 00080 * Saves index header to stream 00081 * 00082 * @param stream - Stream to save to 00083 * @param index - The index to save 00084 */ 00085 template<typename Distance> 00086 void save_header(FILE* stream, const NNIndex<Distance>& index) 00087 { 00088 IndexHeader header; 00089 memset(header.signature, 0, sizeof(header.signature)); 00090 strcpy(header.signature, FLANN_SIGNATURE_); 00091 memset(header.version, 0, sizeof(header.version)); 00092 strcpy(header.version, FLANN_VERSION_); 00093 header.data_type = Datatype<typename Distance::ElementType>::type(); 00094 header.index_type = index.getType (); 00095 header.rows = index.size (); 00096 header.cols = index.veclen (); 00097 00098 std::fwrite(&header, sizeof(header),1,stream); 00099 } 00100 00101 00102 /** 00103 * 00104 * @param stream - Stream to load from 00105 * @return Index header 00106 */ 00107 inline IndexHeader load_header(FILE* stream) 00108 { 00109 IndexHeader header; 00110 size_t read_size = fread(&header,sizeof(header),1,stream); 00111 00112 if (read_size!=(size_t)1) { 00113 throw FLANNException("Invalid index file, cannot read"); 00114 } 00115 00116 if (strcmp(header.signature,FLANN_SIGNATURE_)!=0) { 00117 throw FLANNException("Invalid index file, wrong signature"); 00118 } 00119 00120 return header; 00121 00122 } 00123 00124 00125 template<typename T> 00126 void save_value(FILE* stream, const T& value, size_t count = 1) 00127 { 00128 fwrite(&value, sizeof(value),count, stream); 00129 } 00130 00131 template<typename T> 00132 void save_value(FILE* stream, const cvflann::Matrix<T>& value) 00133 { 00134 fwrite(&value, sizeof(value),1, stream); 00135 fwrite(value.data, sizeof(T),value.rows*value.cols, stream); 00136 } 00137 00138 template<typename T> 00139 void save_value(FILE* stream, const std::vector<T>& value) 00140 { 00141 size_t size = value.size(); 00142 fwrite(&size, sizeof(size_t), 1, stream); 00143 fwrite(&value[0], sizeof(T), size, stream); 00144 } 00145 00146 template<typename T> 00147 void load_value(FILE* stream, T& value, size_t count = 1) 00148 { 00149 size_t read_cnt = fread(&value, sizeof(value), count, stream); 00150 if (read_cnt != count) { 00151 throw FLANNException("Cannot read from file"); 00152 } 00153 } 00154 00155 template<typename T> 00156 void load_value(FILE* stream, cvflann::Matrix<T>& value) 00157 { 00158 size_t read_cnt = fread(&value, sizeof(value), 1, stream); 00159 if (read_cnt != 1) { 00160 throw FLANNException("Cannot read from file"); 00161 } 00162 value.data = new T[value.rows*value.cols]; 00163 read_cnt = fread(value.data, sizeof(T), value.rows*value.cols, stream); 00164 if (read_cnt != (size_t)(value.rows*value.cols)) { 00165 throw FLANNException("Cannot read from file"); 00166 } 00167 } 00168 00169 00170 template<typename T> 00171 void load_value(FILE* stream, std::vector<T>& value) 00172 { 00173 size_t size; 00174 size_t read_cnt = fread(&size, sizeof(size_t), 1, stream); 00175 if (read_cnt!=1) { 00176 throw FLANNException("Cannot read from file"); 00177 } 00178 value.resize(size); 00179 read_cnt = fread(&value[0], sizeof(T), size, stream); 00180 if (read_cnt != size) { 00181 throw FLANNException("Cannot read from file"); 00182 } 00183 } 00184 00185 } 00186 00187 #endif /* OPENCV_FLANN_SAVING_H_ */ 00188
Generated on Tue Jul 12 2022 16:42:40 by
1.7.2