#include "rays/ruby/bounds.h" #include "rays/ruby/point.h" #include "defs.h" RUCY_DEFINE_VALUE_OR_ARRAY_FROM_TO(Rays::Bounds) #define THIS to(self) #define CHECK RUCY_CHECK_OBJ(Rays::Bounds, self) static VALUE alloc(VALUE klass) { return new_type(klass); } static VALUE initialize(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Bounds#initialize", argc, 0, 1, 2, 3, 4, 6); if (argc >= 1) *THIS = to(argc, argv); return self; } static VALUE initialize_copy(VALUE self, VALUE obj) { CHECK; *THIS = to(obj); return self; } static VALUE intersect(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Bounds#intersect?", argc, 1, 2); const Rays::Bounds& bounds = to(argv[0]); int dimension = argc >= 2 ? to(argv[1]) : 2; return value(THIS->is_intersect(bounds, dimension)); } static VALUE include(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Bounds#include?", argc, 1, 2); const Rays::Point& point = to(argv[0]); int dimension = argc >= 2 ? to(argv[1]) : 2; return value(THIS->is_include(point, dimension)); } static VALUE move_to(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Bounds#move_to", argc, 1, 2, 3); if (argv[0].is_kind_of(Rays::point_class())) THIS->move_to(to(argv[0])); else { if (argv[0].is_array()) { argc = argv[0].size(); argv = argv[0].as_array(); } const Rays::Point& p = THIS->position(); coord x = 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__, "Bounds#move_by", argc, 1, 2, 3); if (argv[0].is_kind_of(Rays::point_class())) THIS->move_by(to(argv[0])); else { if (argv[0].is_array()) { argc = argv[0].size(); argv = argv[0].as_array(); } coord x = 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 resize_to(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Bounds#resize_to", argc, 1, 2, 3); if (argv[0].is_kind_of(Rays::point_class())) THIS->resize_to(to(argv[0])); else { if (argv[0].is_array()) { argc = argv[0].size(); argv = argv[0].as_array(); } const Rays::Point& p = THIS->size(); coord x = 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->resize_to(x, y, z); } return self; } static VALUE resize_by(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Bounds#resize_by", argc, 1, 2, 3); if (argv[0].is_kind_of(Rays::point_class())) THIS->resize_by(to(argv[0])); else { if (argv[0].is_array()) { argc = argv[0].size(); argv = argv[0].as_array(); } coord x = 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->resize_by(x, y, z); } return self; } static VALUE inset_by(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Bounds#inset_by", argc, 1, 2, 3); if (argv[0].is_kind_of(Rays::point_class())) THIS->inset_by(to(argv[0])); else { if (argv[0].is_array()) { argc = argv[0].size(); argv = argv[0].as_array(); } coord x = 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->inset_by(x, y, z); } return self; } static VALUE is_valid(VALUE self) { CHECK; return value(THIS->operator bool()); } static VALUE set_x(VALUE self, VALUE x) { CHECK; THIS->x = to(x); return x; } static VALUE get_x(VALUE self) { CHECK; return value(THIS->x); } static VALUE set_y(VALUE self, VALUE y) { CHECK; THIS->y = to(y); return y; } static VALUE get_y(VALUE self) { CHECK; return value(THIS->y); } static VALUE set_z(VALUE self, VALUE z) { CHECK; THIS->z = to(z); return z; } static VALUE get_z(VALUE self) { CHECK; return value(THIS->z); } static VALUE set_width(VALUE self, VALUE width) { CHECK; THIS->width = to(width); return width; } static VALUE get_width(VALUE self) { CHECK; return value(THIS->width); } static VALUE set_height(VALUE self, VALUE height) { CHECK; THIS->height = to(height); return height; } static VALUE get_height(VALUE self) { CHECK; return value(THIS->height); } static VALUE set_depth(VALUE self, VALUE depth) { CHECK; THIS->depth = to(depth); return depth; } static VALUE get_depth(VALUE self) { CHECK; return value(THIS->depth); } static VALUE set_left(VALUE self, VALUE left) { CHECK; THIS->set_left(to(left)); return left; } static VALUE get_left(VALUE self) { CHECK; return value(THIS->left()); } static VALUE set_right(VALUE self, VALUE right) { CHECK; THIS->set_right(to(right)); return right; } static VALUE get_right(VALUE self) { CHECK; return value(THIS->right()); } static VALUE set_top(VALUE self, VALUE top) { CHECK; THIS->set_top(to(top)); return top; } static VALUE get_top(VALUE self) { CHECK; return value(THIS->top()); } static VALUE set_bottom(VALUE self, VALUE bottom) { CHECK; THIS->set_bottom(to(bottom)); return bottom; } static VALUE get_bottom(VALUE self) { CHECK; return value(THIS->bottom()); } static VALUE set_back(VALUE self, VALUE back) { CHECK; THIS->set_back(to(back)); return back; } static VALUE get_back(VALUE self) { CHECK; return value(THIS->back()); } static VALUE set_front(VALUE self, VALUE front) { CHECK; THIS->set_front(to(front)); return front; } static VALUE get_front(VALUE self) { CHECK; return value(THIS->front()); } static VALUE set_position(VALUE self) { CHECK; THIS->set_position(to(argc, argv)); return value(THIS->position()); } static VALUE get_position(VALUE self) { CHECK; return value(THIS->position()); } static VALUE set_size(VALUE self) { CHECK; THIS->set_size(to(argc, argv)); return value(THIS->size()); } static VALUE get_size(VALUE self) { CHECK; return value(THIS->size()); } static VALUE set_center(VALUE self) { CHECK; THIS->set_center(to(argc, argv)); return value(THIS->center()); } static VALUE get_center(VALUE self) { CHECK; return value(THIS->center()); } static VALUE set_at(VALUE self, VALUE index, VALUE value) { CHECK; int i = index.as_i(); if (i < 0 || 1 < i) index_error(__FILE__, __LINE__); (*THIS)[i] = to(value); return value; } static VALUE get_at(VALUE self, VALUE index) { CHECK; int i = index.as_i(); if (i < 0 || 1 < i) index_error(__FILE__, __LINE__); return value((*THIS)[i]); } static VALUE op_and(VALUE self, VALUE bounds) { CHECK; Rays::Bounds b = *THIS; b &= to(bounds); return value(b); } static VALUE op_or(VALUE self, VALUE arg) { CHECK; Rays::Bounds b = *THIS; if (arg.is_kind_of(Rays::bounds_class())) b |= to(arg); else if (arg.is_kind_of(Rays::point_class())) b |= to(arg); else argument_error(__FILE__, __LINE__); return value(b); } static VALUE inspect(VALUE self) { CHECK; return value(Xot::stringf("#", THIS->inspect().c_str())); } static VALUE invalid(VALUE self) { return value(Rays::invalid_bounds()); } static Class cBounds; void Init_bounds () { Module mRays = rb_define_module("Rays"); cBounds = rb_define_class_under(mRays, "Bounds", rb_cObject); rb_define_alloc_func(cBounds, alloc); rb_define_private_method(cBounds, "initialize", RUBY_METHOD_FUNC(initialize), -1); rb_define_private_method(cBounds, "initialize_copy", RUBY_METHOD_FUNC(initialize_copy), 1); cBounds.define_method("intersect?", intersect); cBounds.define_method("include?", include); cBounds.define_method("move_to!", move_to); cBounds.define_method("move_by!", move_by); cBounds.define_method("resize_to!", resize_to); cBounds.define_method("resize_by!", resize_by); cBounds.define_method("inset_by!", inset_by); cBounds.define_method("valid?", is_valid); rb_define_method(cBounds, "x=", RUBY_METHOD_FUNC(set_x), 1); rb_define_method(cBounds, "x", RUBY_METHOD_FUNC(get_x), 0); rb_define_method(cBounds, "y=", RUBY_METHOD_FUNC(set_y), 1); rb_define_method(cBounds, "y", RUBY_METHOD_FUNC(get_y), 0); rb_define_method(cBounds, "z=", RUBY_METHOD_FUNC(set_z), 1); rb_define_method(cBounds, "z", RUBY_METHOD_FUNC(get_z), 0); rb_define_method(cBounds, "width=", RUBY_METHOD_FUNC(set_width), 1); rb_define_method(cBounds, "width", RUBY_METHOD_FUNC(get_width), 0); rb_define_method(cBounds, "height=", RUBY_METHOD_FUNC(set_height), 1); rb_define_method(cBounds, "height", RUBY_METHOD_FUNC(get_height), 0); rb_define_method(cBounds, "depth=", RUBY_METHOD_FUNC(set_depth), 1); rb_define_method(cBounds, "depth", RUBY_METHOD_FUNC(get_depth), 0); rb_define_method(cBounds, "left=", RUBY_METHOD_FUNC(set_left), 1); rb_define_method(cBounds, "left", RUBY_METHOD_FUNC(get_left), 0); rb_define_method(cBounds, "right=", RUBY_METHOD_FUNC(set_right), 1); rb_define_method(cBounds, "right", RUBY_METHOD_FUNC(get_right), 0); rb_define_method(cBounds, "top=", RUBY_METHOD_FUNC(set_top), 1); rb_define_method(cBounds, "top", RUBY_METHOD_FUNC(get_top), 0); rb_define_method(cBounds, "bottom=", RUBY_METHOD_FUNC(set_bottom), 1); rb_define_method(cBounds, "bottom", RUBY_METHOD_FUNC(get_bottom), 0); rb_define_method(cBounds, "back=", RUBY_METHOD_FUNC(set_back), 1); rb_define_method(cBounds, "back", RUBY_METHOD_FUNC(get_back), 0); rb_define_method(cBounds, "front=", RUBY_METHOD_FUNC(set_front), 1); rb_define_method(cBounds, "front", RUBY_METHOD_FUNC(get_front), 0); rb_define_method(cBounds, "position=", RUBY_METHOD_FUNC(set_position), -1); rb_define_method(cBounds, "position", RUBY_METHOD_FUNC(get_position), 0); rb_define_method(cBounds, "size=", RUBY_METHOD_FUNC(set_size), -1); rb_define_method(cBounds, "size", RUBY_METHOD_FUNC(get_size), 0); rb_define_method(cBounds, "center=", RUBY_METHOD_FUNC(set_center), -1); rb_define_method(cBounds, "center", RUBY_METHOD_FUNC(get_center), 0); cBounds.define_method("[]", get_at); cBounds.define_method("[]=", set_at); cBounds.define_method("&", op_and); cBounds.define_method("|", op_or); rb_define_method(cBounds, "inspect", RUBY_METHOD_FUNC(inspect), 0); rb_define_singleton_method(cBounds, "invalid", RUBY_METHOD_FUNC(invalid), 0); } namespace Rucy { template <> Rays::Bounds value_to (int argc, const Value* argv, bool convert) { if (argc == 1 && argv->is_array()) { argc = argv->size(); argv = argv->as_array(); } assert(argc == 0 || (argc > 0 && argv)); if (convert) { if (argc == 0) return Rays::Bounds(); else if (argv->is_kind_of(Rays::point_class())) { switch (argc) { #define V(i) to(argv[i]) case 1: return Rays::Bounds(V(0)); case 2: return Rays::Bounds(V(0), V(1)); #undef V default: argument_error(__FILE__, __LINE__); } } else if (argv->is_num()) { switch (argc) { #define V(i) argv[i].as_f(true) case 1: return Rays::Bounds(V(0)); case 2: return Rays::Bounds(V(0), V(1)); case 3: return Rays::Bounds(V(0), V(1), V(2)); case 4: return Rays::Bounds(V(0), V(1), V(2), V(3)); case 6: return Rays::Bounds(V(0), V(1), V(2), V(3), V(4), V(5)); #undef V default: argument_error(__FILE__, __LINE__); } } } if (argc != 1) argument_error(__FILE__, __LINE__); return value_to(*argv, convert); } }// Rucy namespace Rays { Class bounds_class () { return cBounds; } }// Rays