@@ -52,23 +52,24 @@ class Parameters < FFI::Struct
52
52
class << self
53
53
54
54
55
- def dtype_to_c d
56
- return :float if d == :float32
57
- return :double if d == :float64
58
- return d
55
+ DTYPE_TO_C = { :float32 => :float , :float64 => :double , :int32 => :int , :byte => :byte , :int8 => :byte }
56
+
57
+ def dtype_to_c d #:nodoc:
58
+ return DTYPE_TO_C [ d ] if DTYPE_TO_C . has_key? ( d )
59
+ raise ( NMatrix ::DataTypeError , "FLANN does not support this dtype" )
59
60
end
60
61
61
62
62
63
# Allocates index space and distance space for storing results from various searches. For a k-nearest neighbors
63
64
# search, for example, you want trows (the number of rows in the testset) times k (the number of nearest neighbors
64
65
# being searched for).
65
- def allocate_results_space result_size
66
+ def allocate_results_space result_size #:nodoc:
66
67
[ FFI ::MemoryPointer . new ( :int , result_size ) , FFI ::MemoryPointer . new ( :float , result_size ) ]
67
68
end
68
69
69
70
70
71
# Don't know if these will be a hash, a static struct, or a pointer to a struct. Return the pointer and the struct.
71
- def handle_parameters parameters
72
+ def handle_parameters parameters #:nodoc:
72
73
parameters ||= DEFAULT_PARAMETERS unless block_given?
73
74
74
75
if parameters . is_a? ( FFI ::MemoryPointer ) # User supplies us with the necessary parameters already in the correct form.
@@ -94,38 +95,11 @@ def handle_parameters parameters
94
95
[ c_parameters_ptr , c_parameters ]
95
96
end
96
97
97
- def handle_index index
98
- return index . index_ptr if index . is_a? ( Flann ::Index )
99
- index
100
- end
101
-
102
-
103
- # Find the k nearest neighbors with an index already built.
104
- #
105
- def nearest_neighbors_by_index index , testset , k , parameters : DEFAULT_PARAMETERS
106
- parameters_ptr , parameters = handle_parameters ( parameters )
107
- result_size = testset . shape [ 0 ] * k
108
- indices_int_ptr , distances_float_ptr = allocate_results_space ( result_size )
109
- index_ptr = handle_index ( index )
110
-
111
- raise ( ArgumentError , "did you forget to call #build! on your index?" ) if index . index_ptr . nil?
112
-
113
- Flann . flann_find_nearest_neighbors_index index_ptr ,
114
- FFI ::Pointer . new_from_nmatrix ( testset ) ,
115
- testset . shape [ 0 ] ,
116
- indices_int_ptr , distances_float_ptr ,
117
- k ,
118
- parameters_ptr
119
-
120
- [ indices_int_ptr . read_array_of_int ( result_size ) , distances_float_ptr . read_array_of_float ( result_size ) ]
121
- end
122
-
123
98
124
99
# Find the k nearest neighbors.
125
100
#
126
101
# If no index parameters are given, FLANN_DEFAULT_PARAMETERS are used. A block is accepted as well.
127
102
def nearest_neighbors dataset , testset , k , parameters : DEFAULT_PARAMETERS
128
-
129
103
# Get a pointer and a struct regardless of how the arguments are supplied.
130
104
parameters_ptr , parameters = handle_parameters ( parameters )
131
105
result_size = testset . shape [ 0 ] * k
@@ -147,20 +121,23 @@ def set_distance_type! distance_function, order = 0
147
121
end
148
122
149
123
# Perform hierarchical clustering of a set of points.
150
- def compute_cluster_centers dataset , clusters , parameters : DEFAULT_PARAMETERS
124
+ def cluster dataset , clusters , parameters : DEFAULT_PARAMETERS
151
125
c_method = "flann_compute_cluster_centers_#{ Flann ::dtype_to_c ( dataset . dtype ) } " . to_sym
152
126
153
127
result = dataset . clone_structure
154
128
parameters_ptr , parameters = handle_parameters ( parameters )
155
129
Flann . send ( c_method , FFI ::Pointer . new_from_nmatrix ( dataset ) , dataset . shape [ 0 ] , dataset . shape [ 1 ] , clusters , FFI ::Pointer . new_from_nmatrix ( result ) , parameters_ptr )
130
+
131
+ result
156
132
end
133
+ alias :compute_cluster_centers :cluster
157
134
end
158
135
159
136
160
137
protected
161
138
162
- # byte: unsigned char*dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params
163
- # only thing that changes is the pointer type for the first arg.
139
+ # byte: unsigned char*dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params
140
+ # only thing that changes is the pointer type for the first arg.
164
141
attach_function :flann_build_index_byte , [ :pointer , :int , :int , :pointer , :index_params_ptr ] , :index_ptr
165
142
attach_function :flann_build_index_int , [ :pointer , :int , :int , :pointer , :index_params_ptr ] , :index_ptr
166
143
attach_function :flann_build_index_float , [ :pointer , :int , :int , :pointer , :index_params_ptr ] , :index_ptr
@@ -178,26 +155,26 @@ def compute_cluster_centers dataset, clusters, parameters: DEFAULT_PARAMETERS
178
155
attach_function :flann_radius_search_float , [ :index_ptr , :pointer , :pointer , :pointer , :int , :float , :index_params_ptr ] , :int
179
156
attach_function :flann_radius_search_double , [ :index_ptr , :pointer , :pointer , :pointer , :int , :float , :index_params_ptr ] , :int
180
157
181
- attach_function :flann_save_index_byte , [ :index_ptr , :pointer ] , :int
182
- attach_function :flann_save_index_int , [ :index_ptr , :pointer ] , :int
183
- attach_function :flann_save_index_float , [ :index_ptr , :pointer ] , :int
184
- attach_function :flann_save_index_double , [ :index_ptr , :pointer ] , :int
158
+ attach_function :flann_save_index_byte , [ :index_ptr , :string ] , :int
159
+ attach_function :flann_save_index_int , [ :index_ptr , :string ] , :int
160
+ attach_function :flann_save_index_float , [ :index_ptr , :string ] , :int
161
+ attach_function :flann_save_index_double , [ :index_ptr , :string ] , :int
185
162
186
- attach_function :flann_load_index_byte , [ :pointer , :pointer , :int , :int ] , :index_ptr
187
- attach_function :flann_load_index_int , [ :pointer , :pointer , :int , :int ] , :index_ptr
188
- attach_function :flann_load_index_float , [ :pointer , :pointer , :int , :int ] , :index_ptr
189
- attach_function :flann_load_index_double , [ :pointer , :pointer , :int , :int ] , :index_ptr
163
+ attach_function :flann_load_index_byte , [ :string , :pointer , :int , :int ] , :index_ptr
164
+ attach_function :flann_load_index_int , [ :string , :pointer , :int , :int ] , :index_ptr
165
+ attach_function :flann_load_index_float , [ :string , :pointer , :int , :int ] , :index_ptr
166
+ attach_function :flann_load_index_double , [ :string , :pointer , :int , :int ] , :index_ptr
190
167
191
- attach_function :flann_free_index_byte , [ :index_ptr , :index_params_ptr ] , :int
192
- attach_function :flann_free_index_int , [ :index_ptr , :index_params_ptr ] , :int
193
- attach_function :flann_free_index_float , [ :index_ptr , :index_params_ptr ] , :int
168
+ attach_function :flann_free_index_byte , [ :index_ptr , :index_params_ptr ] , :int
169
+ attach_function :flann_free_index_int , [ :index_ptr , :index_params_ptr ] , :int
170
+ attach_function :flann_free_index_float , [ :index_ptr , :index_params_ptr ] , :int
194
171
attach_function :flann_free_index_double , [ :index_ptr , :index_params_ptr ] , :int
195
172
196
173
attach_function :flann_set_distance_type , [ DistanceType , :int ] , :void
197
174
198
- attach_function :flann_compute_cluster_centers_byte , [ :pointer , :int , :int , :int , :pointer , :index_params_ptr ] , :int
199
- attach_function :flann_compute_cluster_centers_int , [ :pointer , :int , :int , :int , :pointer , :index_params_ptr ] , :int
200
- attach_function :flann_compute_cluster_centers_float , [ :pointer , :int , :int , :int , :pointer , :index_params_ptr ] , :int
201
- attach_function :flann_compute_cluster_centers_double , [ :pointer , :int , :int , :int , :pointer , :index_params_ptr ] , :int
175
+ attach_function :flann_compute_cluster_centers_byte , [ :pointer , :int , :int , :int , :pointer , :index_params_ptr ] , :int
176
+ attach_function :flann_compute_cluster_centers_int , [ :pointer , :int , :int , :int , :pointer , :index_params_ptr ] , :int
177
+ attach_function :flann_compute_cluster_centers_float , [ :pointer , :int , :int , :int , :pointer , :index_params_ptr ] , :int
178
+ attach_function :flann_compute_cluster_centers_double , [ :pointer , :int , :int , :int , :pointer , :index_params_ptr ] , :int
202
179
203
180
end
0 commit comments