@@ -15,30 +15,52 @@ module Flann
15
15
LogLevel = enum ( :none , :fatal , :error , :warn , :info )
16
16
DistanceType = enum ( :euclidean , :manhattan , :minkowski , :hist_intersect , :hellinger , :chi_square , :kullback_leibler )
17
17
18
- DEFAULT_PARAMETERS = [ :kdtree ,
19
- 32 , 0.0 ,
20
- 0 , -1 , 0 ,
21
- 4 , 4 ,
22
- 32 , 11 , :random , 0.2 ,
23
- 0.9 , 0.01 , 0 , 0.1 ,
24
- :none , 0
25
- ]
26
-
27
18
# For NMatrix compatibility
28
19
typedef :float , :float32
29
20
typedef :double , :float64
30
21
typedef :pointer , :index_params_ptr
31
22
typedef :pointer , :index_ptr
32
23
24
+
25
+ class InitializableStruct < FFI ::Struct
26
+ def initialize pointer = nil , *layout , &block
27
+ if pointer . respond_to? ( :each_pair )
28
+ options = pointer
29
+ pointer = nil
30
+ else
31
+ options
32
+ end
33
+
34
+ super ( pointer , *layout , &block )
35
+
36
+ if defined? ( self . class ::DEFAULTS )
37
+ options = self . class ::DEFAULTS . merge ( options )
38
+ end
39
+
40
+ options . each_pair do |key , value |
41
+ self [ key ] = value
42
+ end unless options . nil?
43
+ end
44
+ end
45
+
46
+
33
47
# A nearest neighbor search index for a given dataset.
34
- class Parameters < FFI :: Struct
48
+ class Parameters < InitializableStruct
35
49
layout :algorithm , Flann ::Algorithm , # The algorithm to use (linear, kdtree, kmeans, composite, kdtree_single, saved, autotuned)
36
50
:checks , :int , # How many leaves (features) to use (for kdtree)
37
- :cluster_boundary_index , :float , # aka cb_tree, used when searching the kmeans tree
51
+ :eps , :float , # eps parameter for eps-knn search
52
+ :sorted , :int , # indicates if results returned by radius search should be sorted or not
53
+ :max_neighbors , :int , # limits the maximum number of neighbors returned
54
+ :cores , :int , # number of parallel cores to use for searching
55
+
38
56
:trees , :int , # Number of randomized trees to use (for kdtree)
57
+ :leaf_max_size , :int , # ?
58
+
39
59
:branching , :int , # Branching factor (for kmeans tree)
40
60
:iterations , :int , # Max iterations to perform in one kmeans clustering (kmeans tree)
41
61
:centers_init , Flann ::CentersInit , # Algorithm used (random, gonzales, kmeanspp)
62
+ :cluster_boundary_index , :float , # Cluster boundary index. Used when searching the kmeans tree
63
+
42
64
:target_precision , :float , # Precision desired (used for auto-tuning, -1 otherwise)
43
65
:build_weight , :float , # Build tree time weighting factor
44
66
:memory_weight , :float , # Index memory weighting factor
@@ -47,8 +69,17 @@ class Parameters < FFI::Struct
47
69
:table_number , :uint , # The number of hash tables to use
48
70
:key_size , :uint , # The length of the key to use in the hash tables
49
71
:multi_probe_level , :uint , # Number of levels to use in multi-probe LSH, 0 for standard LSH
72
+
50
73
:log_level , Flann ::LogLevel , # Determines the verbosity of each flann function
51
74
:random_seed , :long # Random seed to use
75
+
76
+ DEFAULT = { algorithm : :kdtree ,
77
+ checks : 32 , eps : 0.0 ,
78
+ sorted : 0 , max_neighbors : -1 , cores : 0 ,
79
+ trees : 4 , leaf_max_size : 4 ,
80
+ log_level : :none , random_seed : 0 }
81
+
82
+
52
83
end
53
84
54
85
class << self
@@ -72,7 +103,7 @@ def allocate_results_space result_size #:nodoc:
72
103
73
104
# Don't know if these will be a hash, a static struct, or a pointer to a struct. Return the pointer and the struct.
74
105
def handle_parameters parameters #:nodoc:
75
- parameters ||= DEFAULT_PARAMETERS unless block_given?
106
+ parameters ||= Parameters :: DEFAULT unless block_given?
76
107
77
108
if parameters . is_a? ( FFI ::MemoryPointer ) # User supplies us with the necessary parameters already in the correct form.
78
109
c_parameters_ptr = parameters
@@ -100,8 +131,8 @@ def handle_parameters parameters #:nodoc:
100
131
101
132
# Find the k nearest neighbors.
102
133
#
103
- # If no index parameters are given, FLANN_DEFAULT_PARAMETERS are used. A block is accepted as well.
104
- def nearest_neighbors dataset , testset , k , parameters : DEFAULT_PARAMETERS
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 )
105
136
# Get a pointer and a struct regardless of how the arguments are supplied.
106
137
parameters_ptr , parameters = handle_parameters ( parameters )
107
138
result_size = testset . shape [ 0 ] * k
@@ -123,12 +154,19 @@ def set_distance_type! distance_function, order = 0
123
154
end
124
155
125
156
# Perform hierarchical clustering of a set of points.
126
- def cluster dataset , clusters , parameters : DEFAULT_PARAMETERS
157
+ #
158
+ # Arguments:
159
+ # * dataset: NMatrix of points
160
+ # * parameters:
161
+ def cluster dataset , clusters , parameters : Parameters . new ( Parameters ::DEFAULT )
127
162
c_method = "flann_compute_cluster_centers_#{ Flann ::dtype_to_c ( dataset . dtype ) } " . to_sym
128
163
129
164
result = dataset . clone_structure
130
165
parameters_ptr , parameters = handle_parameters ( parameters )
166
+
167
+ #err_code =
131
168
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 )
169
+ #raise("unknown error in cluster") if err_code < 0
132
170
133
171
result
134
172
end
@@ -138,8 +176,8 @@ def cluster dataset, clusters, parameters: DEFAULT_PARAMETERS
138
176
139
177
protected
140
178
141
- # byte: unsigned char*dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params
142
- # only thing that changes is the pointer type for the first arg.
179
+ # byte: unsigned char*dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params
180
+ # only thing that changes is the pointer type for the first arg.
143
181
attach_function :flann_build_index_byte , [ :pointer , :int , :int , :pointer , :index_params_ptr ] , :index_ptr
144
182
attach_function :flann_build_index_int , [ :pointer , :int , :int , :pointer , :index_params_ptr ] , :index_ptr
145
183
attach_function :flann_build_index_float , [ :pointer , :int , :int , :pointer , :index_params_ptr ] , :index_ptr
0 commit comments