@@ -96,8 +96,8 @@ def dtype_to_c d #:nodoc:
96
96
# Allocates index space and distance space for storing results from various searches. For a k-nearest neighbors
97
97
# search, for example, you want trows (the number of rows in the testset) times k (the number of nearest neighbors
98
98
# being searched for).
99
- def allocate_results_space result_size #:nodoc:
100
- [ FFI ::MemoryPointer . new ( :int , result_size ) , FFI ::MemoryPointer . new ( :float , result_size ) ]
99
+ def allocate_results_space result_size , c_type #:nodoc:
100
+ [ FFI ::MemoryPointer . new ( :int , result_size ) , FFI ::MemoryPointer . new ( c_type , result_size ) ]
101
101
end
102
102
103
103
@@ -132,18 +132,23 @@ def handle_parameters parameters #:nodoc:
132
132
# Find the k nearest neighbors.
133
133
#
134
134
# If no index parameters are given, FLANN_Parameters::DEFAULT are used. A block is accepted as well.
135
- def nearest_neighbors dataset , testset , k , parameters : Parameters . new ( Parameters ::DEFAULT )
135
+ def nearest_neighbors dataset , testset , k , parameters = { }
136
+ parameters = Parameters . new ( Flann ::Parameters ::DEFAULT . merge ( parameters ) )
136
137
# Get a pointer and a struct regardless of how the arguments are supplied.
137
138
parameters_ptr , parameters = handle_parameters ( parameters )
138
139
result_size = testset . shape [ 0 ] * k
139
- indices_int_ptr , distances_float_ptr = allocate_results_space ( result_size )
140
140
141
- Flann . flann_find_nearest_neighbors FFI ::Pointer . new_from_nmatrix ( dataset ) , dataset . shape [ 0 ] , dataset . shape [ 1 ] ,
142
- FFI ::Pointer . new_from_nmatrix ( testset ) , testset . shape [ 0 ] ,
143
- indices_int_ptr , distances_float_ptr , k , parameters_ptr
141
+ c_type = Flann ::dtype_to_c ( dataset . dtype )
142
+ c_method = "flann_find_nearest_neighbors_#{ c_type } " . to_sym
143
+ indices_int_ptr , distances_t_ptr = allocate_results_space ( result_size , c_type )
144
+
145
+ # dataset, rows, cols, testset, trows, indices, dists, nn, flann_params
146
+ Flann . send c_method , FFI ::Pointer . new_from_nmatrix ( dataset ) , dataset . shape [ 0 ] , dataset . shape [ 1 ] ,
147
+ FFI ::Pointer . new_from_nmatrix ( testset ) , testset . shape [ 0 ] ,
148
+ indices_int_ptr , distances_t_ptr , k , parameters_ptr
144
149
145
150
# Return results: two arrays, one of indices and one of distances.
146
- [ indices_int_ptr . read_array_of_int ( result_size ) , distances_float_ptr . read_array_of_float ( result_size ) ]
151
+ [ indices_int_ptr . read_array_of_int ( result_size ) , distances_t_ptr . read_array_of_float ( result_size ) ]
147
152
end
148
153
alias :nn :nearest_neighbors
149
154
@@ -158,7 +163,8 @@ def set_distance_type! distance_function, order = 0
158
163
# Arguments:
159
164
# * dataset: NMatrix of points
160
165
# * parameters:
161
- def cluster dataset , clusters , parameters : Parameters . new ( Parameters ::DEFAULT )
166
+ def cluster dataset , clusters , parameters = { }
167
+ parameters = Parameters . new ( Flann ::Parameters ::DEFAULT . merge ( parameters ) )
162
168
c_method = "flann_compute_cluster_centers_#{ Flann ::dtype_to_c ( dataset . dtype ) } " . to_sym
163
169
164
170
result = dataset . clone_structure
@@ -184,10 +190,16 @@ def cluster dataset, clusters, parameters: Parameters.new(Parameters::DEFAULT)
184
190
attach_function :flann_build_index_double , [ :pointer , :int , :int , :pointer , :index_params_ptr ] , :index_ptr
185
191
186
192
# index, testset, trows, indices, dists, nn, flann_params
187
- attach_function :flann_find_nearest_neighbors_index , [ :index_ptr , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
193
+ attach_function :flann_find_nearest_neighbors_index_byte , [ :index_ptr , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
194
+ attach_function :flann_find_nearest_neighbors_index_int , [ :index_ptr , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
195
+ attach_function :flann_find_nearest_neighbors_index_float , [ :index_ptr , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
196
+ attach_function :flann_find_nearest_neighbors_index_double , [ :index_ptr , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
188
197
189
198
# dataset, rows, cols, testset, trows, indices, dists, nn, flann_params
190
- attach_function :flann_find_nearest_neighbors , [ :pointer , :int , :int , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
199
+ attach_function :flann_find_nearest_neighbors_byte , [ :pointer , :int , :int , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
200
+ attach_function :flann_find_nearest_neighbors_int , [ :pointer , :int , :int , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
201
+ attach_function :flann_find_nearest_neighbors_float , [ :pointer , :int , :int , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
202
+ attach_function :flann_find_nearest_neighbors_double , [ :pointer , :int , :int , :pointer , :int , :pointer , :pointer , :int , :index_params_ptr ] , :int
191
203
192
204
# index, query point, result indices, result distances, max_nn, radius, flann_params
193
205
attach_function :flann_radius_search_byte , [ :index_ptr , :pointer , :pointer , :pointer , :int , :float , :index_params_ptr ] , :int
0 commit comments