IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
vector_search.h
Go to the documentation of this file.
1/**
2 * \file IMP/algebra/vector_search.h \brief Functions to search over vectors.
3 *
4 * Copyright 2007-2026 IMP Inventors. All rights reserved.
5 *
6 */
7
8#ifndef IMPALGEBRA_VECTOR_SEARCH_H
9#define IMPALGEBRA_VECTOR_SEARCH_H
10
11#include "VectorD.h"
12#include <IMP/types.h>
13#include <IMP/Object.h>
14#include <IMP/log.h>
15#include <IMP/log_macros.h>
16#include <IMP/SetCheckState.h>
17
18#include "grid_storages.h"
19#include "grid_ranges.h"
20#include "GridD.h"
21#ifdef IMP_ALGEBRA_USE_IMP_CGAL
22#include <IMP/cgal/internal/knn.h>
23#endif
24#ifdef IMP_ALGEBRA_USE_ANN
25#include "internal/ann.h"
26#endif
27#include "internal/linear_knn.h"
28#include <fstream>
29#include <algorithm>
30
31#ifdef IMP_ALGEBRA_USE_ANN
32#define IMP_KNN_DATA internal::ANNData
33#elif defined(IMP_ALGEBRA_USE_IMP_CGAL)
34#define IMP_KNN_DATA IMP::cgal::internal::KNNData
35#else
36#define IMP_KNN_DATA internal::LinearKNNData<D>
37#endif
38
39IMPALGEBRA_BEGIN_NAMESPACE
40
41/** @name Vector Search
42
43 These functions and classes create various search structures
44 over sets of vectors.
45 @{
46*/
47
48//! Build a structure for finding nearest neighbors.
49/** Different implementations are used depending on what optional dependencies
50 are available.
51 \uses{class NearestNeighborD, CGAL}
52 \uses{class NearestNeighborD, ANN}
53 */
54template <int D>
55class NearestNeighborD : public IMP::Object {
56 IMP_KNN_DATA data_;
57 double eps_;
58#if IMP_HAS_CHECKS >= IMP_INTERNAL
59 mutable std::ofstream query_log_;
60#endif
61 template <class It>
62 void instantiate(It b, It e) {
63 if (0) {
64 // compile all of them
65 Ints ret;
66 internal::LinearKNNData<D> linear(b, e);
67 linear.fill_nearest_neighbors(*b, 3U, eps_, ret);
68 }
69 }
70
71 public:
72 template <class It>
73 NearestNeighborD(It b, It e, double epsilon = 0)
74 : Object("NearestNeighbor%1%"), data_(b, e), eps_(epsilon) {
75 instantiate(b, e);
76 }
77 NearestNeighborD(const Vector<VectorD<D>> &vs, double epsilon = 0)
78 : Object("NearestNeighbor%1%"),
79 data_(vs.begin(), vs.end()),
80 eps_(epsilon) {
81 instantiate(vs.begin(), vs.end());
82 }
83 //! Log the points and queries to a file for performance studies
84 void set_query_log(std::string fname) {
87 set_was_used(true);
88#if IMP_HAS_CHECKS >= IMP_INTERNAL
89 query_log_.open(fname.c_str());
90 for (unsigned int i = 0; i < data_.get_number_of_points(); ++i) {
91 query_log_ << spaces_io(data_.get_point(i)) << std::endl;
92 }
93 query_log_ << std::endl;
94#endif
95 }
96
97 unsigned int get_nearest_neighbor(const VectorD<D> &q) const {
99 set_was_used(true);
100#if IMP_HAS_CHECKS >= IMP_INTERNAL
101 if (query_log_) {
102 query_log_ << spaces_io(q) << " " << 1 << std::endl;
103 }
104#endif
105 Ints ret(1);
106 data_.fill_nearest_neighbors(q, 1U, eps_, ret);
107 return ret[0];
108 }
109 //! Search using the ith point in the input set.
110 unsigned int get_nearest_neighbor(unsigned int i) const {
112 set_was_used(true);
113#if IMP_HAS_CHECKS >= IMP_INTERNAL
114 if (query_log_) {
115 query_log_ << i << " " << 1 << std::endl;
116 }
117#endif
118 Ints ret(2);
119 data_.fill_nearest_neighbors(data_.get_point(i), 2U, eps_, ret);
120 return ret[1];
121 }
122 //! Search using the ith point in the input set.
123 /** \return the k nearest neighbors. */
124 Ints get_nearest_neighbors(unsigned int i, unsigned int k) const {
126 set_was_used(true);
127#if IMP_HAS_CHECKS >= IMP_INTERNAL
128 if (query_log_) {
129 query_log_ << i << " " << k << std::endl;
130 }
131#endif
132 Ints ret(k + 1);
133 data_.fill_nearest_neighbors(data_.get_point(i), k + 1, eps_, ret);
134 // Fewer than k+1 neighbors are found if there are not enough points
135 ret.resize(std::min(k + 1, data_.get_number_of_points()));
136 // Point i is usually first, but need not be if other points coincide
137 // with it; if it was not found at all, drop the farthest neighbor
138 Ints::iterator it = std::find(ret.begin(), ret.end(), static_cast<int>(i));
139 if (it != ret.end()) {
140 ret.erase(it);
141 } else if (ret.size() > k) {
142 ret.pop_back();
143 }
144 return ret;
145 }
146 Ints get_nearest_neighbors(const algebra::VectorD<D> &v,
147 unsigned int k) const {
149 set_was_used(true);
150#if IMP_HAS_CHECKS >= IMP_INTERNAL
151 if (query_log_) {
152 query_log_ << v << " " << k << std::endl;
153 }
154#endif
155 Ints ret(k);
156 data_.fill_nearest_neighbors(v, k, eps_, ret);
157 // v need not be one of the points, so keep all neighbors found
158 ret.resize(std::min(k, data_.get_number_of_points()));
159 return ret;
160 }
161 //! Find all points within the provided distance.
162 Ints get_in_ball(unsigned int i, double distance) const {
164 set_was_used(true);
165 Ints ret;
166 data_.fill_nearest_neighbors(data_.get_point(i), distance, eps_, ret);
167 // Results are not sorted by distance, so remove point i wherever it is
168 ret.erase(std::remove(ret.begin(), ret.end(), static_cast<int>(i)),
169 ret.end());
170 return ret;
171 }
172 //! Find all points within the provided distance.
173 Ints get_in_ball(const VectorD<D> &pt, double distance) const {
175 set_was_used(true);
176 Ints ret;
177 data_.fill_nearest_neighbors(pt, distance, eps_, ret);
178 return ret;
179 }
180 IMP_OBJECT_METHODS(NearestNeighborD);
181};
182
183/** @} */
184
185#ifndef IMP_DOXYGEN
186typedef NearestNeighborD<1> NearestNeighbor1D;
187typedef NearestNeighborD<2> NearestNeighbor2D;
188typedef NearestNeighborD<3> NearestNeighbor3D;
189typedef NearestNeighborD<4> NearestNeighbor4D;
190typedef NearestNeighborD<5> NearestNeighbor5D;
191typedef NearestNeighborD<6> NearestNeighbor6D;
192typedef NearestNeighborD<-1> NearestNeighborKD;
193#endif
194
195//! Provide an incremental nearest neighbor search function.
196/** The interface and behavior is somewhat different than that of
197 NearestNeighborD, so be aware.
198
199 Later this can support balls by copying points multiple times.
200 */
201class IMPALGEBRAEXPORT DynamicNearestNeighbor3D : public Object {
204 Grid grid_;
205 typedef Grid::Index Index;
206 typedef Grid::ExtendedIndex EIndex;
207 Vector3Ds coords_;
208 Vector<Index> indexes_;
209 void audit() const;
210 void set_coordinates_internal(int id, Vector3D nc);
211
212 public:
213 DynamicNearestNeighbor3D(const Vector3Ds &vs, double query_estimate = 1);
214 Ints get_in_ball(int id, double distance) const;
215 void set_coordinates(int id, Vector3D nc);
216 IMP_OBJECT_METHODS(DynamicNearestNeighbor3D);
217};
218IMPALGEBRA_END_NAMESPACE
219
220#endif /* IMPALGEBRA_VECTOR_SEARCH_H */
A class to represent a voxel grid.
A shared base class to help in debugging and things.
Checking and error reporting support.
Simple D vector class.
#define IMP_INTERNAL_CHECK_VARIABLE(variable)
Common base class for heavy weight IMP objects.
Definition Object.h:111
Object(std::string name)
Construct an object with the given name.
void set_was_used(bool tf) const
A Cartesian vector in D-dimensions.
Definition VectorD.h:38
A more IMP-like version of the std::vector.
Definition Vector.h:60
Embed a grid as an evenly spaced axis aligned grid.
A voxel grid in d-dimensional space.
Definition GridD.h:81
Build a structure for finding nearest neighbors.
Ints get_in_ball(const VectorD< D > &pt, double distance) const
Find all points within the provided distance.
unsigned int get_nearest_neighbor(unsigned int i) const
Search using the ith point in the input set.
Ints get_nearest_neighbors(unsigned int i, unsigned int k) const
Search using the ith point in the input set.
Ints get_in_ball(unsigned int i, double distance) const
Find all points within the provided distance.
void set_query_log(std::string fname)
Log the points and queries to a file for performance studies.
A class to represent a voxel grid.
Dense and sparse storage of the voxel data in a grid.
Logging and error reporting support.
Logging and error reporting support.
#define IMP_OBJECT_LOG
Set the log level to the object's log level.
Definition log_macros.h:284
Vector< VectorD< 3 > > Vector3Ds
Definition VectorD.h:409
IMP::Vector< Int > Ints
Standard way to pass a bunch of Int values.
Definition types.h:48
VectorD< 3 > Vector3D
Definition VectorD.h:407
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Basic types used by IMP.