Module: MiniballC

Included in:
Miniball
Defined in:
ext/miniball_ruby/miniball_ruby.c

Instance Method Summary collapse

Instance Method Details

#calc(points, with_analytics) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/miniball_ruby/miniball_ruby.c', line 93

static VALUE calc(VALUE self, VALUE points, VALUE with_analytics) {
  Check_Type(points, T_ARRAY);
  if(RARRAY_LEN(points) == 0) {
    rb_raise(rb_eArgError, "Must have at least one point");
  }

  Check_Type(rb_ary_entry(points, 0), T_ARRAY);
  int dim = RARRAY_LEN(rb_ary_entry(points, 0));

  Miniball* m = Miniball_create(dim);

  int i, j;
  int len =  RARRAY_LEN(points);
  for(i = 0; i < len; ++i) {
    VALUE point = rb_ary_entry(points, i);
    Check_Type(point, T_ARRAY);
    if(RARRAY_LEN(point) != dim) {
      rb_raise(rb_eArgError, "Point dimensions must be consistent");
    }

    Point* p = Point_create(dim);
    for(j = 0; j < dim; ++j) {
      p->coord[j] = NUM2DBL(rb_ary_entry(point, j));
    }
    Miniball_check_in(m, p);
  }

  Miniball_build(m);

  Point* center = Miniball_center(m);
  double radius_squared = Miniball_squared_radius(m);
  //TODO: Support points

  Result* r = ConstructResult(dim, center->coord, radius_squared);

  int support_point_count = Miniball_nr_support_points(m);
  r->support_points = rb_ary_new2(support_point_count);

  Point* support_point = Miniball_support_points_begin(m);
  for(i = 0; i < support_point_count; i++, support_point = support_point->next) {
    VALUE point = rb_ary_new2(dim);
    for(j = 0; j < dim; ++j) {
      rb_ary_store(point, j, rb_float_new(support_point->coord[j]));
    }
    rb_ary_store(r->support_points, i, point);
  }

  if(with_analytics) {
    double slack;
    r->accuracy = rb_float_new(Miniball_accuracy(m, &slack));
    r->slack = slack;
  }

  VALUE result = Data_Wrap_Struct(MiniballResult, MarkResult, DestroyResult, r);

  Miniball_destroy(m);
  return result;
}