#include "rays/ruby/point.h" #include #include "defs.h" using namespace Rucy; using Rays::coord; RUCY_DEFINE_VALUE_FROM_TO(Rays::Point) #define THIS to(self) #define CHECK RUCY_CHECK_OBJ(Rays::Point, self) static VALUE alloc(VALUE klass) { return new_type(klass); } static VALUE initialize(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Point#initialize", argc, 0, 1, 2, 3); switch (argc) { case 1: *THIS = Rays::Point(to(argv[0])); break; case 2: *THIS = Rays::Point(to(argv[0]), to(argv[1])); break; case 3: *THIS = Rays::Point( to(argv[0]), to(argv[1]), to(argv[2])); break; } return self; } static VALUE initialize_copy(VALUE self, VALUE obj) { CHECK; *THIS = to(obj); return self; } static VALUE move_to(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Point#move_to", argc, 1, 2, 3); if (argv[0].is_kind_of(Rays::point_class())) THIS->move_to(to(argv[0])); else { const Rays::Point& p = *THIS; coord x = (argc >= 1 && argv[0]) ? to(argv[0]) : p.x; coord y = (argc >= 2 && argv[1]) ? to(argv[1]) : p.y; coord z = (argc >= 3 && argv[2]) ? to(argv[2]) : p.z; THIS->move_to(x, y, z); } return self; } static VALUE move_by(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Point#move_by", argc, 1, 2, 3); if (argv[0].is_kind_of(Rays::point_class())) THIS->move_by(to(argv[0])); else { coord x = (argc >= 1 && argv[0]) ? to(argv[0]) : 0; coord y = (argc >= 2 && argv[1]) ? to(argv[1]) : 0; coord z = (argc >= 3 && argv[2]) ? to(argv[2]) : 0; THIS->move_by(x, y, z); } return self; } static VALUE length(VALUE self) { CHECK; return value(THIS->length()); } static VALUE normalize(VALUE self) { CHECK; THIS->normalize(); return self; } static VALUE normal(VALUE self) { CHECK; return value(THIS->normal()); } static VALUE set_x(VALUE self, VALUE x) { CHECK; return value(THIS->x = to(x)); } static VALUE get_x(VALUE self) { CHECK; return value(THIS->x); } static VALUE set_y(VALUE self, VALUE y) { CHECK; return value(THIS->y = to(y)); } static VALUE get_y(VALUE self) { CHECK; return value(THIS->y); } static VALUE set_z(VALUE self, VALUE z) { CHECK; return value(THIS->z = to(z)); } static VALUE get_z(VALUE self) { CHECK; return value(THIS->z); } static VALUE add(VALUE self, VALUE point) { CHECK; Rays::Point p = *THIS; p += to(point); return value(p); } static VALUE sub(VALUE self, VALUE point) { CHECK; Rays::Point p = *THIS; p -= to(point); return value(p); } static VALUE mult(VALUE self, VALUE point) { CHECK; Rays::Point p = *THIS; p *= to(point); return value(p); } static VALUE div(VALUE self, VALUE point) { CHECK; Rays::Point p = *THIS; p /= to(point); return value(p); } static VALUE array_get(VALUE self, VALUE index) { CHECK; int i = index.as_i(); if (i < 0 || 2 < i) index_error(__FILE__, __LINE__); return value((*THIS)[i]); } static VALUE array_set(VALUE self, VALUE index, VALUE value) { CHECK; int i = index.as_i(); if (i < 0 || 2 < i) index_error(__FILE__, __LINE__); (*THIS)[i] = to(value); return value; } static VALUE inspect(VALUE self) { CHECK; return value(Xot::stringf("#", THIS->inspect().c_str())); } static Class cPoint; void Init_point () { Module mRays = rb_define_module("Rays"); cPoint = rb_define_class_under(mRays, "Point", rb_cObject); rb_define_alloc_func(cPoint, alloc); rb_define_private_method(cPoint, "initialize", RUBY_METHOD_FUNC(initialize), -1); rb_define_private_method(cPoint, "initialize_copy", RUBY_METHOD_FUNC(initialize_copy), 1); cPoint.define_method("move_to!", move_to); cPoint.define_method("move_by!", move_by); rb_define_method(cPoint, "length", RUBY_METHOD_FUNC(length), 0); rb_define_method(cPoint, "normalize", RUBY_METHOD_FUNC(normalize), 0); rb_define_method(cPoint, "normal", RUBY_METHOD_FUNC(normal), 0); rb_define_method(cPoint, "x=", RUBY_METHOD_FUNC(set_x), 1); rb_define_method(cPoint, "x", RUBY_METHOD_FUNC(get_x), 0); rb_define_method(cPoint, "y=", RUBY_METHOD_FUNC(set_y), 1); rb_define_method(cPoint, "y", RUBY_METHOD_FUNC(get_y), 0); rb_define_method(cPoint, "z=", RUBY_METHOD_FUNC(set_z), 1); rb_define_method(cPoint, "z", RUBY_METHOD_FUNC(get_z), 0); rb_define_method(cPoint, "op_add", RUBY_METHOD_FUNC(add), 1); rb_define_method(cPoint, "op_sub", RUBY_METHOD_FUNC(sub), 1); rb_define_method(cPoint, "op_mult", RUBY_METHOD_FUNC(mult), 1); rb_define_method(cPoint, "op_div", RUBY_METHOD_FUNC(div), 1); cPoint.define_method("[]", array_get); cPoint.define_method("[]=", array_set); rb_define_method(cPoint, "inspect", RUBY_METHOD_FUNC(inspect), 0); } namespace Rucy { template <> Rays::Point value_to (Value value, bool convert) { if (convert) { size_t argc = 0; Value* argv = NULL; if (value.is_array()) { argc = value.size(); argv = value.as_array(); } else { argc = 1; argv = &value; } if (argc < 1) Rucy::argument_error(__FILE__, __LINE__); if (argv[0].is_kind_of(Rays::point_class())) value = argv[0]; else if (argv[0].is_i() || argv[0].is_f()) { switch (argc) { #define V(i) argv[i].as_f(true) case 1: return Rays::Point(V(0)); case 2: return Rays::Point(V(0), V(1)); case 3: return Rays::Point(V(0), V(1), V(2)); #undef V default: Rucy::argument_error(__FILE__, __LINE__); } } } return value_to(value, convert); } }// Rucy namespace Rays { Class point_class () { return cPoint; } }// Rays