.. _program_listing_file__tmp_ws_src_aruco_ros_aruco_include_aruco_picoflann.h: Program Listing for File picoflann.h ==================================== |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/aruco_ros/aruco/include/aruco/picoflann.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #ifndef PicoFlann_H #define PicoFlann_H #include #include #include #include #include #include #include namespace picoflann { struct L2 { template double compute_distance(const ElementType &elema, const ElementType2 &elemb, const Adapter &adapter, int ndims, double worstDist) const { // compute dist double sqd = 0; for (int i = 0; i < ndims; i++) { double d = adapter(elema, i) - adapter(elemb, i); sqd += d * d; if (sqd > worstDist) return sqd; } return sqd; } }; template class KdTreeIndex { public: template inline void build(const Container &container) { _index.clear(); _index.reserve(container.size() * 2); _index.dims = DIMS; _index.nValues = container.size(); // Create root and assign all items all_indices.resize(container.size()); for (size_t i = 0; i < container.size(); i++) all_indices[i] = i; if (container.size() == 0) return; computeBoundingBox(_index.rootBBox, 0, all_indices.size(), container); _index.push_back(Node()); divideTree(_index, 0, 0, all_indices.size(), _index.rootBBox, container); } inline void clear() { _index.clear(); all_indices.clear(); } // saves to a stream. Note that the container is not saved! inline void toStream(std::ostream &str) const; // reads from an stream. Note that the container is not readed! inline void fromStream(std::istream &str); template inline std::vector > searchKnn(const Container &container, const Type &val, int nn, bool sorted = true) { std::vector > res; generalSearch(res, container, val, -1, sorted, nn); return res; } template inline std::vector > radiusSearch(const Container &container, const Type &val, double dist, bool sorted = true, int maxNN = -1) const { std::vector > res; generalSearch(res, container, val, dist, sorted, maxNN); return res; } template inline void radiusSearch(std::vector > &res, const Container &container, const Type &val, double dist, bool sorted = true, int maxNN = -1) { generalSearch(res, container, val, dist, sorted, maxNN); } private: struct Node { inline bool isLeaf() const { return _ileft == -1 && _iright == -1; } inline void setNodesInfo(uint32_t l, uint32_t r) { _ileft = l; _iright = r; } double div_val; uint16_t col_index; // column index of the feature vector std::vector idx; float divhigh, divlow; int64_t _ileft = -1, _iright = -1; // children void toStream(std::ostream &str) const; void fromStream(std::istream &str); }; typedef std::vector > BoundingBox; struct Index : public std::vector { BoundingBox rootBBox; int dims = 0; int nValues = 0; // number of elements of the set when call to build inline void toStream(std::ostream &str) const; inline void fromStream(std::istream &str); }; Index _index; DistanceType _distance; Adapter adapter; // next are only used during build std::vector all_indices; int _maxLeafSize = 10; // temporal used during creation of the tree template void divideTree(Index &index, uint64_t nodeIdx, int startIndex, int endIndex, BoundingBox &bbox, const Container &container) { // std::cout<<"CREATE="<(bbox, startIndex, endIndex, container); // std::cout<(_bbox, startIndex, endIndex, container); // //get the dimension with highest distnaces double max_spread = -1; currNode.col_index = 0; for (int i = 0; i < DIMS; i++) { double spread = _bbox[i].second - _bbox[i].first; // maxV[i]-minV[i]; if (spread > max_spread) { max_spread = spread; currNode.col_index = i; } } // select the split val double split_val = (bbox[currNode.col_index].first + bbox[currNode.col_index].second) / 2; if (split_val < _bbox[currNode.col_index].first) currNode.div_val = _bbox[currNode.col_index].first; else if (split_val > _bbox[currNode.col_index].second) currNode.div_val = _bbox[currNode.col_index].second; else currNode.div_val = split_val; } else { double var[DIMS], mean[DIMS]; // compute the variance of the features to select the highest one mean_var_calculate(startIndex, endIndex, var, mean, container); currNode.col_index = 0; // select element with highest variance for (int i = 1; i < DIMS; i++) if (var[i] > var[currNode.col_index]) currNode.col_index = i; // now sort all indices according to the selected value currNode.div_val = mean[currNode.col_index]; } // compute the variance of the features to select the highest one // now sort all indices according to the selected value // std::cout<<" CUT FEAT="< void KdTreeIndex::toStream(std::ostream &str) const { _index.toStream(str); } template void KdTreeIndex::fromStream(std::istream &str) { _index.fromStream(str); } } // namespace picoflann #endif