10 #include <aliceVision/numeric/numeric.hpp>
28 template<
typename T, std::
size_t N>
35 typedef std::size_t size_type;
45 for (size_type i = 0; i < N; ++i)
46 data[i] = defaultValue;
50 inline size_type
size()
const {
return N; }
53 inline bin_type&
operator[](std::size_t i) {
return data[i]; }
54 inline bin_type
operator[](std::size_t i)
const {
return data[i]; }
56 bin_type & at(std::size_t i) {
return data[i]; }
59 inline This& operator+=(
const This other)
61 for (size_type i = 0; i <
size(); ++i)
67 inline This operator/(
const This other)
const
70 for (size_type i = 0; i <
size(); ++i)
71 res[i] = data[i] / other[i];
75 inline This& operator*=(
const value_type scalar)
77 for (size_type i = 0; i <
size(); ++i)
82 bool operator==(
const Descriptor& other)
const {
return std::equal(data, data + N, other.data); }
84 inline bin_type* getData()
const {
return (bin_type*)(&data[0]); }
87 std::ostream&
print(std::ostream& os)
const;
89 std::istream&
read(std::istream& in);
91 template<
class Archive>
92 void save(Archive& archive)
const
94 std::vector<T> array(data, data + N);
98 template<
class Archive>
99 void load(Archive& archive)
101 std::vector<T> array(N);
103 std::memcpy(data, array.data(),
sizeof(T) * N);
111 template<
typename T, std::
size_t N>
112 inline std::ostream& operator<<(std::ostream& out,
const Descriptor<T, N>& obj)
114 return obj.print(out);
118 template<
typename T, std::
size_t N>
119 inline std::istream& operator>>(std::istream& in, Descriptor<T, N>& obj)
128 inline std::ostream& printT(std::ostream& os, T* tab,
size_t N)
130 std::copy(tab, &tab[N], std::ostream_iterator<T>(os,
" "));
135 inline std::ostream& printT<unsigned char>(std::ostream& os,
unsigned char* tab,
size_t N)
137 for (
size_t i = 0; i < N; ++i)
138 os << (
int)tab[i] <<
" ";
143 inline std::istream& readT(std::istream& is, T* tab,
size_t N)
145 for (
size_t i = 0; i < N; ++i)
151 inline std::istream& readT<unsigned char>(std::istream& is,
unsigned char* tab,
size_t N)
154 for (
size_t i = 0; i < N; ++i)
157 tab[i] = (
unsigned char)temp;
162 template<
typename T, std::
size_t N>
165 return printT<T>(os, (T*)&data[0], N);
168 template<
typename T, std::
size_t N>
171 return readT<T>(in, (T*)&data[0], N);
175 template<
typename DescriptorsT>
176 inline void loadDescsFromFile(
const std::string& sfileNameDescs, DescriptorsT& vec_desc)
180 std::ifstream fileIn(sfileNameDescs);
181 if (!fileIn.is_open())
182 throw std::runtime_error(
"Can't load descriptor file, can't open '" + sfileNameDescs +
"' !");
184 std::copy(std::istream_iterator<typename DescriptorsT::value_type>(fileIn),
185 std::istream_iterator<typename DescriptorsT::value_type>(),
186 std::back_inserter(vec_desc));
189 throw std::runtime_error(
"Can't load descriptor file, '" + sfileNameDescs +
"' is incorrect !");
195 template<
typename DescriptorsT>
196 inline void saveDescsToFile(
const std::string& sfileNameDescs, DescriptorsT& vec_desc)
198 std::ofstream file(sfileNameDescs);
200 throw std::runtime_error(
"Can't save descriptor file, can't open '" + sfileNameDescs +
"' !");
202 std::copy(vec_desc.begin(), vec_desc.end(), std::ostream_iterator<typename DescriptorsT::value_type>(file,
"\n"));
205 throw std::runtime_error(
"Can't save descriptor file, '" + sfileNameDescs +
"' is incorrect !");
217 template<
typename DescFrom,
typename DescTo>
218 void convertDesc(
const DescFrom& descFrom, DescTo& descTo)
220 typename DescFrom::bin_type* ptrFrom = descFrom.getData();
221 typename DescTo::bin_type* ptrTo = descTo.getData();
223 for (
size_t i = 0; i < DescFrom::static_size; ++i)
225 ptrTo[i] =
typename DescTo::value_type(ptrFrom[i]);
246 template<
typename DescriptorT,
typename FileDescriptorT = DescriptorT>
247 inline void loadDescsFromBinFile(
const std::string& sfileNameDescs, std::vector<DescriptorT>& vec_desc,
bool append =
false,
const int Nmax = 0)
252 std::ifstream fileIn(sfileNameDescs, std::ios::in | std::ios::binary);
254 if (!fileIn.is_open())
255 throw std::runtime_error(
"Can't load descriptor binary file, can't open '" + sfileNameDescs +
"' !");
258 std::size_t cardDesc = 0;
259 fileIn.read((
char*)&cardDesc,
sizeof(std::size_t));
261 std::size_t previousSize = vec_desc.size();
264 vec_desc.resize(vec_desc.size() + std::min((
int)cardDesc, Nmax));
266 vec_desc.resize(vec_desc.size() + cardDesc);
267 typename std::vector<DescriptorT>::iterator begin = vec_desc.begin();
268 std::advance(begin, previousSize);
271 constexpr std::size_t oneDescSize = FileDescriptorT::static_size *
sizeof(
typename FileDescriptorT::bin_type);
273 FileDescriptorT fileDescriptor;
274 for (
typename std::vector<DescriptorT>::iterator iter = begin; iter != vec_desc.end(); ++iter)
276 fileIn.read((
char*)fileDescriptor.getData(), oneDescSize);
277 convertDesc<FileDescriptorT, DescriptorT>(fileDescriptor, *iter);
281 throw std::runtime_error(
"Can't load descriptor binary file, '" + sfileNameDescs +
"' is incorrect !");
287 template<
typename DescriptorsT>
288 inline void saveDescsToBinFile(
const std::string& sfileNameDescs, DescriptorsT& vec_desc)
290 typedef typename DescriptorsT::value_type VALUE;
292 std::ofstream file(sfileNameDescs, std::ios::out | std::ios::binary);
295 throw std::runtime_error(
"Can't save descriptor binary file, can't open '" + sfileNameDescs +
"' !");
298 const std::size_t cardDesc = vec_desc.size();
299 file.write((
const char*)&cardDesc,
sizeof(std::size_t));
300 for (
typename DescriptorsT::const_iterator iter = vec_desc.begin(); iter != vec_desc.end(); ++iter)
302 file.write((
const char*)(*iter).getData(), VALUE::static_size *
sizeof(
typename VALUE::bin_type));
306 throw std::runtime_error(
"Can't save descriptor binary file, '" + sfileNameDescs +
"' is incorrect !");