Class: NearTree::RTree

Inherits:
Object
  • Object
show all
Defined in:
ext/neartree/NearTree.c

Instance Method Summary collapse

Constructor Details

#initialize(dimension) ⇒ Object

FEATURE add support for NearTree norms



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/neartree/NearTree.c', line 102

VALUE method_initialize(VALUE self, VALUE dimension) {
  NearTreeRTree* handle = get_object_handle(self);            /* handle         */

  /* dimension */
  if (!FIXNUM_P(dimension)) {
    rb_raise(rb_eTypeError, "Invalid dimension");
  }
  int d = NUM2INT(dimension);
  if (d <= 0) { rb_raise(rb_eArgError, "Invalid dimension %i", d); }
  handle->dimension = d;

  /* create CNearTree */
  check_error_code(CNearTreeCreate(handle->tree_handle, d, CNEARTREE_TYPE_DOUBLE));

  return self;
}

Instance Method Details

#dimensionObject

readers: dimension/points/radius/values



252
# File 'ext/neartree/NearTree.c', line 252

VALUE method_dimension(VALUE self) { return INT2NUM(get_object_handle(self)->dimension); }

#empty?Boolean

Returns:

  • (Boolean)


256
257
258
# File 'ext/neartree/NearTree.c', line 256

VALUE method_empty(VALUE self) {
  return RARRAY_LEN(get_object_handle(self)->points) == 0 ? Qtrue : Qfalse;
}

#find_k_nearest(*args) ⇒ Object

NearTree#find_k_nearest(index, k, radius = nil)



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'ext/neartree/NearTree.c', line 176

VALUE method_find_k_nearest(int argc, VALUE* argv, VALUE self) {
  if (!(argc == 2 || argc == 3)) {
    rb_raise(rb_eArgError, "Invalid parameter number: %d", argc);
  }

  NearTreeRTree* handle = get_object_handle(self);            /* handle           */

  /* array index */
  double* array_index = get_array_from_index(handle->dimension, argv[0]);

  /* k */
  if (!FIXNUM_P(argv[1])) {
    rb_raise(rb_eTypeError, "Invalid k");
  }
  long k = FIX2INT(argv[1]);
//  if (INT2FIX((int)k) != argv[1]) {
//  }
  if (k <= 0) {
    rb_raise(rb_eArgError, "k must be superior to 0");
  }

  /* radius      */
  double radius = get_radius_from_value(argc, argv, 2);

  CVectorHandle nearest_points;
  CVectorHandle nearest_values;
  check_cvector_error(CVectorCreate(&nearest_points, sizeof(void*), k));
  check_cvector_error(CVectorCreate(&nearest_values, sizeof(void*), k));

  check_error_code(CNearTreeFindKNearest(*(handle->tree_handle), (size_t)k, radius,
    nearest_points, nearest_values, array_index, 0));

  xfree(array_index);                                         /* free array index */

  /* result array */
  VALUE result_array = rb_ary_new();
  size_t points_size;
  size_t values_size;
  check_cvector_error(CVectorGetSize(nearest_points, &points_size));
  check_cvector_error(CVectorGetSize(nearest_values, &values_size));

  if (points_size != values_size) {
    rb_raise(rb_eRuntimeError, "Vector sizes error");
  }

  for(int i = 0; i != points_size; ++i) {
    VALUE result = rb_ary_new();
    /* coord */
    VALUE result_coord = rb_ary_new();
    for(int j = 0; j != handle->dimension; ++j) {
      rb_ary_push(result_coord,
        DBL2NUM(((double**)(nearest_points->array))[i][j]));
    }
    rb_ary_push(result, result_coord);
    rb_ary_push(result, *(((VALUE**)nearest_values->array)[i]));

    rb_ary_push(result_array, result);
  }

  return result_array;
}

#find_nearest(*args) ⇒ Object

NearTree#find_nearest(index, radius = nil)



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/neartree/NearTree.c', line 140

VALUE method_find_nearest(int argc, VALUE* argv, VALUE self) {
  if (!(argc == 1 || argc == 2)) {
    rb_raise(rb_eArgError, "Invalid parameter number: %d", argc);
  }

  NearTreeRTree* handle = get_object_handle(self);            /* handle           */

  /* array index */
  double* array_index = get_array_from_index(handle->dimension, argv[0]);

  /* radius      */
  double radius = get_radius_from_value(argc, argv, 1);

  VALUE*  value;                                              /* nearest value    */
  double* nearest_point;                                      /* nearest point    */

  check_error_code(CNearTreeNearestNeighbor(*(handle->tree_handle),
    radius, (void*)&nearest_point, (void*)&value, array_index));

  xfree(array_index);                                         /* free array index */

  /* nearest_point to ruby array */
  VALUE point_array = rb_ary_new();
  for(int i = 0; i != handle->dimension; ++i) {
    rb_ary_push(point_array, DBL2NUM(nearest_point[i]));
  }

  /* result array */
  VALUE result_array = rb_ary_new();
  rb_ary_push(result_array, point_array);
  rb_ary_push(result_array, *value);

  return result_array;
}

#insert(index, value) ⇒ Object

NearTree#insert



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'ext/neartree/NearTree.c', line 120

VALUE method_insert(VALUE self, VALUE index, VALUE value) {
  NearTreeRTree* handle = get_object_handle(self);            /* handle          */

  /* array index, check for bad parameter */
  double* array_index = get_array_from_index(handle->dimension, index);

  /* insert point/value */
  rb_ary_push(handle->points, index);
  rb_ary_push(handle->values, value);

  /* insert element */
  check_error_code(
    CNearTreeImmediateInsert(*(handle->tree_handle), (void*)array_index, (void*)value));

  xfree(array_index);                                         /* free array index */

  return Qnil;
}

#pointsObject



253
# File 'ext/neartree/NearTree.c', line 253

VALUE method_points(   VALUE self) { return get_object_handle(self)->points;             }

#valuesObject



254
# File 'ext/neartree/NearTree.c', line 254

VALUE method_values(   VALUE self) { return get_object_handle(self)->values;             }