Skip to content

Commit

Permalink
[third_party] Add nanoflann openMVG#856
Browse files Browse the repository at this point in the history
- simplify the code in case of usage with array of static size => CArray can use std::array.
  • Loading branch information
pmoulon committed Jul 6, 2017
1 parent 9f13100 commit 2496b69
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 106 deletions.
1 change: 1 addition & 0 deletions src/third_party/nanoflann/README.openMVG
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ URL: https://github.com/jlblancoc/nanoflann
License: BSD, see LICENSE.
Upstream version: nanoflann 1.2.3: Released Dec 20, 2016
Local modifications:
- use std::array in CArray

127 changes: 21 additions & 106 deletions src/third_party/nanoflann/nanoflann.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@
#ifndef NANOFLANN_HPP_
#define NANOFLANN_HPP_

#include <vector>
#include <cassert>
#include <array>
#include <algorithm>
#include <stdexcept>
#include <cstdio> // for fwrite()
#include <cassert>
#include <cmath> // for abs()
#include <cstdio> // for fwrite()
#include <cstdlib> // for abs()
#include <limits>
#include <stdexcept>
#include <vector>

// Avoid conflicting declaration of min/max macros in windows headers
#if !defined(NOMINMAX) && (defined(_WIN32) || defined(_WIN32_) || defined(WIN32) || defined(_WIN64))
Expand Down Expand Up @@ -165,7 +166,7 @@ namespace nanoflann
inline void addPoint(DistanceType dist, IndexType index)
{
if (dist<radius)
m_indices_dists.push_back(std::make_pair(index,dist));
m_indices_dists.emplace_back(index,dist);
}

inline DistanceType worstDist() const { return radius; }
Expand Down Expand Up @@ -578,108 +579,22 @@ namespace nanoflann
* @{ */

// ---------------- CArray -------------------------
/** A STL container (as wrapper) for arrays of constant size defined at compile time (class imported from the MRPT project)
* This code is an adapted version from Boost, modifed for its integration
* within MRPT (JLBC, Dec/2009) (Renamed array -> CArray to avoid possible potential conflicts).
* See
* http://www.josuttis.com/cppcode
* for details and the latest version.
* See
* http://www.boost.org/libs/array for Documentation.
* for documentation.
*
* (C) Copyright Nicolai M. Josuttis 2001.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*
* 29 Jan 2004 - minor fixes (Nico Josuttis)
* 04 Dec 2003 - update to synch with library TR1 (Alisdair Meredith)
* 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
* 05 Aug 2001 - minor update (Nico Josuttis)
* 20 Jan 2001 - STLport fix (Beman Dawes)
* 29 Sep 2000 - Initial Revision (Nico Josuttis)
*
* Jan 30, 2004
/** A wrapper of the array STL container
* - use in order to
* - define a no effect resize class (for matching template usage)
* - define a assign function
*/
template <typename T, std::size_t N>
class CArray {
class CArray : public std::array<T,N>{
public:
T elems[N]; // fixed-size array of elements of type T

public:
// type definitions
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

// iterator support
inline iterator begin() { return elems; }
inline const_iterator begin() const { return elems; }
inline iterator end() { return elems+N; }
inline const_iterator end() const { return elems+N; }

// reverse iterator support
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
#elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
// workaround for broken reverse_iterator in VC7
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
reference, iterator, reference> > reverse_iterator;
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
const_reference, iterator, reference> > const_reverse_iterator;
#else
// workaround for broken reverse_iterator implementations
typedef std::reverse_iterator<iterator,T> reverse_iterator;
typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
#endif
/** This method has no effects in this class, but raises an exception if the expected size does not match */
inline void resize(const size_t nElements) { if (nElements!=N) throw std::logic_error("Try to change the size of a CArray."); }

reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
// operator[]
inline reference operator[](size_type i) { return elems[i]; }
inline const_reference operator[](size_type i) const { return elems[i]; }
// at() with range check
reference at(size_type i) { rangecheck(i); return elems[i]; }
const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
// front() and back()
reference front() { return elems[0]; }
const_reference front() const { return elems[0]; }
reference back() { return elems[N-1]; }
const_reference back() const { return elems[N-1]; }
// size is constant
static inline size_type size() { return N; }
static bool empty() { return false; }
static size_type max_size() { return N; }
enum { static_size = N };
/** This method has no effects in this class, but raises an exception if the expected size does not match */
inline void resize(const size_t nElements) { if (nElements!=N) throw std::logic_error("Try to change the size of a CArray."); }
// swap (note: linear complexity in N, constant for given instantiation)
void swap (CArray<T,N>& y) { std::swap_ranges(begin(),end(),y.begin()); }
// direct access to data (read-only)
const T* data() const { return elems; }
// use array as C array (direct read/write access to data)
T* data() { return elems; }
// assignment with type conversion
template <typename T2> CArray<T,N>& operator= (const CArray<T2,N>& rhs) {
std::copy(rhs.begin(),rhs.end(), begin());
return *this;
}
// assign one value to all elements
inline void assign (const T& value) { for (size_t i=0;i<N;i++) elems[i]=value; }
// assign (compatible with std::vector's one) (by JLBC for MRPT)
void assign (const size_t n, const T& value) { assert(N==n); for (size_t i=0;i<N;i++) elems[i]=value; }
private:
// check range (may be private because it is static)
static void rangecheck (size_type i) { if (i >= size()) { throw std::out_of_range("CArray<>: index out of range"); } }
// assign one value to all elements
inline void assign (const T& value) { this->fill(value); }
// assign (compatible with std::vector's one) (by JLBC for MRPT)
void assign (const size_t n, const T& value) { assert(N==n); this->fill(value); }
}; // end of CArray

/** Used to declare fixed-size arrays when DIM>0, dynamically-allocated vectors when DIM=-1.
Expand Down Expand Up @@ -901,17 +816,17 @@ namespace nanoflann
bool findNeighbors(RESULTSET& result, const ElementType* vec, const SearchParams& searchParams) const
{
assert(vec);
if (size() == 0)
return false;
if (size() == 0)
return false;
if (!root_node)
throw std::runtime_error("[nanoflann] findNeighbors() called before building the index.");
throw std::runtime_error("[nanoflann] findNeighbors() called before building the index.");
float epsError = 1+searchParams.eps;

distance_vector_t dists; // fixed or variable-sized container (depending on DIM)
dists.assign((DIM>0 ? DIM : dim) ,0); // Fill it with zeros.
DistanceType distsq = computeInitialDistances(vec, dists);
searchLevel(result, vec, root_node, distsq, dists, epsError); // "count_leaf" parameter removed since was neither used nor returned to the user.
return result.full();
return result.full();
}

/**
Expand Down

0 comments on commit 2496b69

Please sign in to comment.