00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
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
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