| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include "nn_search_ANN.h" | ||
| 41 | |||
| 42 | namespace GEO { | ||
| 43 | |||
| 44 | 32 | NearestNeighborSearch_ANN::NearestNeighborSearch_ANN( | |
| 45 | coord_index_t dim | ||
| 46 | 32 | ) : | |
| 47 | NearestNeighborSearch(dim), | ||
| 48 | 32 | ann_tree_(nullptr) { | |
| 49 | 32 | } | |
| 50 | |||
| 51 | 32 | void NearestNeighborSearch_ANN::set_points( | |
| 52 | index_t nb_points, const double* points | ||
| 53 | ) { | ||
| 54 | 32 | set_points(nb_points, points, dimension()); | |
| 55 | 32 | } | |
| 56 | |||
| 57 | ✗ | bool NearestNeighborSearch_ANN::stride_supported() const { | |
| 58 | ✗ | return true; | |
| 59 | } | ||
| 60 | |||
| 61 | 32 | void NearestNeighborSearch_ANN::set_points( | |
| 62 | index_t nb_points, const double* points, index_t stride | ||
| 63 | ) { | ||
| 64 | 32 | nb_points_ = nb_points; | |
| 65 | 32 | points_ = points; | |
| 66 | 32 | stride_ = stride; | |
| 67 | |||
| 68 | // Patched ANN so that we no longer need | ||
| 69 | // to generate an array of pointers to | ||
| 70 | // the points, See ANN.h | ||
| 71 | #ifdef ANN_CONTIGUOUS_POINT_ARRAY | ||
| 72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | delete ann_tree_; |
| 73 | ✗ | ann_tree_ = new ANNkd_tree( | |
| 74 | 32 | ANNpointArray(points_, stride_), | |
| 75 | int(nb_points), | ||
| 76 | 32 | int(dimension()) | |
| 77 |
2/4✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 32 times.
|
32 | ); |
| 78 | #else | ||
| 79 | delete ann_tree_; | ||
| 80 | ann_tree_ = nullptr; | ||
| 81 | ann_points_.resize(nb_points); | ||
| 82 | for(index_t i = 0; i < nb_points; i++) { | ||
| 83 | ann_points_[i] = const_cast<double*>(points) + stride_ * i; | ||
| 84 | } | ||
| 85 | ann_tree_ = new ANNkd_tree( | ||
| 86 | &ann_points_[0], int(nb_points), int(dimension()) | ||
| 87 | ); | ||
| 88 | #endif | ||
| 89 | 32 | } | |
| 90 | |||
| 91 | 35242 | void NearestNeighborSearch_ANN::get_nearest_neighbors( | |
| 92 | index_t nb_neighbors, | ||
| 93 | const double* query_point, | ||
| 94 | index_t* neighbors, | ||
| 95 | double* neighbors_sq_dist | ||
| 96 | ) const { | ||
| 97 | // In Gargantua mode, index_t is 64 bits, and ANNidx is always 32 bits, so | ||
| 98 | // we need to allocate space for ANN indices, then convert and copy them | ||
| 99 | // to client's neighbors array. | ||
| 100 | 35242 | ANNidxArray ann_neighbors = ANNidxArray(alloca(sizeof(ANNidx)*nb_neighbors)); | |
| 101 | 35242 | ann_tree_->annkSearch( | |
| 102 | const_cast<double*>(query_point), | ||
| 103 | int(nb_neighbors), ann_neighbors, neighbors_sq_dist, | ||
| 104 |
1/2✓ Branch 0 taken 35242 times.
✗ Branch 1 not taken.
|
35242 | (exact_ ? 0.0 : 0.1) |
| 105 | ); | ||
| 106 |
2/2✓ Branch 0 taken 352354 times.
✓ Branch 1 taken 35242 times.
|
387596 | for(index_t i=0; i<nb_neighbors; ++i) { |
| 107 | 352354 | neighbors[i] = index_t(ann_neighbors[i]); | |
| 108 | } | ||
| 109 | 35242 | } | |
| 110 | |||
| 111 | 128 | NearestNeighborSearch_ANN::~NearestNeighborSearch_ANN() { | |
| 112 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
64 | delete ann_tree_; |
| 113 | 64 | ann_tree_ = nullptr; | |
| 114 | 128 | } | |
| 115 | |||
| 116 | /***********************************************************/ | ||
| 117 | |||
| 118 | ✗ | void NearestNeighborSearch_ANN_BruteForce::set_points( | |
| 119 | index_t nb_points, const double* points, index_t stride | ||
| 120 | ) { | ||
| 121 | ✗ | nb_points_ = nb_points; | |
| 122 | ✗ | points_ = points; | |
| 123 | ✗ | stride_ = stride; | |
| 124 | |||
| 125 | // Patched ANN so that we no longer need | ||
| 126 | // to generate an array of pointers to | ||
| 127 | // the points, See ANN.h | ||
| 128 | #ifdef ANN_CONTIGUOUS_POINT_ARRAY | ||
| 129 | ✗ | delete ann_tree_; | |
| 130 | ✗ | ann_tree_ = new ANNbruteForce( | |
| 131 | ✗ | ANNpointArray(points_, stride_), | |
| 132 | int(nb_points), | ||
| 133 | ✗ | int(dimension()) | |
| 134 | ✗ | ); | |
| 135 | #else | ||
| 136 | delete ann_tree_; | ||
| 137 | ann_tree_ = nullptr; | ||
| 138 | ann_points_.resize(nb_points); | ||
| 139 | for(index_t i = 0; i < nb_points; i++) { | ||
| 140 | ann_points_[i] = const_cast<double*>(points) + stride_ * i; | ||
| 141 | } | ||
| 142 | ann_tree_ = new ANNbruteForce( | ||
| 143 | &ann_points_[0], int(nb_points), int(dimension()) | ||
| 144 | ); | ||
| 145 | #endif | ||
| 146 | ✗ | } | |
| 147 | } | ||
| 148 |