.doc/ext/rays/bounds.cpp in rays-0.1.12 vs .doc/ext/rays/bounds.cpp in rays-0.1.13

- old
+ new

@@ -1,20 +1,14 @@ #include "rays/ruby/bounds.h" -#include <rucy.h> #include "rays/ruby/point.h" #include "defs.h" -using namespace Rucy; +RUCY_DEFINE_VALUE_OR_ARRAY_FROM_TO(Rays::Bounds) -using Rays::coord; - - -RUCY_DEFINE_VALUE_FROM_TO(Rays::Bounds) - #define THIS to<Rays::Bounds*>(self) #define CHECK RUCY_CHECK_OBJ(Rays::Bounds, self) @@ -28,47 +22,21 @@ VALUE initialize(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Bounds#initialize", argc, 0, 1, 2, 3, 4, 6); - if (argc == 0) return self; + if (argc >= 1) + *THIS = to<Rays::Bounds>(argc, argv); - switch (argc) - { - case 1: - *THIS = Rays::Bounds(to<coord>(argv[0])); - break; - - case 2: - *THIS = Rays::Bounds(to<coord>(argv[0]), to<coord>(argv[1])); - break; - - case 3: - *THIS = Rays::Bounds( - to<coord>(argv[0]), to<coord>(argv[1]), to<coord>(argv[2])); - break; - - case 4: - *THIS = Rays::Bounds( - to<coord>(argv[0]), to<coord>(argv[1]), - to<coord>(argv[2]), to<coord>(argv[3])); - break; - - case 6: - *THIS = Rays::Bounds( - to<coord>(argv[0]), to<coord>(argv[1]), to<coord>(argv[2]), - to<coord>(argv[3]), to<coord>(argv[4]), to<coord>(argv[5])); - break; - } - return self; } static VALUE initialize_copy(VALUE self, VALUE obj) { CHECK; + *THIS = to<Rays::Bounds&>(obj); return self; } static @@ -110,11 +78,11 @@ argc = argv[0].size(); argv = argv[0].as_array(); } const Rays::Point& p = THIS->position(); - coord x = (argc >= 1 && argv[0]) ? to<coord>(argv[0]) : p.x; + coord x = argv[0] ? to<coord>(argv[0]) : p.x; coord y = (argc >= 2 && argv[1]) ? to<coord>(argv[1]) : p.y; coord z = (argc >= 3 && argv[2]) ? to<coord>(argv[2]) : p.z; THIS->move_to(x, y, z); } @@ -135,11 +103,11 @@ { argc = argv[0].size(); argv = argv[0].as_array(); } - coord x = (argc >= 1 && argv[0]) ? to<coord>(argv[0]) : 0; + coord x = argv[0] ? to<coord>(argv[0]) : 0; coord y = (argc >= 2 && argv[1]) ? to<coord>(argv[1]) : 0; coord z = (argc >= 3 && argv[2]) ? to<coord>(argv[2]) : 0; THIS->move_by(x, y, z); } @@ -161,11 +129,11 @@ argc = argv[0].size(); argv = argv[0].as_array(); } const Rays::Point& p = THIS->size(); - coord x = (argc >= 1 && argv[0]) ? to<coord>(argv[0]) : p.x; + coord x = argv[0] ? to<coord>(argv[0]) : p.x; coord y = (argc >= 2 && argv[1]) ? to<coord>(argv[1]) : p.y; coord z = (argc >= 3 && argv[2]) ? to<coord>(argv[2]) : p.z; THIS->resize_to(x, y, z); } @@ -186,11 +154,11 @@ { argc = argv[0].size(); argv = argv[0].as_array(); } - coord x = (argc >= 1 && argv[0]) ? to<coord>(argv[0]) : 0; + coord x = argv[0] ? to<coord>(argv[0]) : 0; coord y = (argc >= 2 && argv[1]) ? to<coord>(argv[1]) : 0; coord z = (argc >= 3 && argv[2]) ? to<coord>(argv[2]) : 0; THIS->resize_by(x, y, z); } @@ -211,25 +179,34 @@ { argc = argv[0].size(); argv = argv[0].as_array(); } - coord x = (argc >= 1 && argv[0]) ? to<coord>(argv[0]) : 0; + coord x = argv[0] ? to<coord>(argv[0]) : 0; coord y = (argc >= 2 && argv[1]) ? to<coord>(argv[1]) : 0; coord z = (argc >= 3 && argv[2]) ? to<coord>(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; - return value(THIS->x = to<coord>(x)); + THIS->x = to<coord>(x); + return x; } static VALUE get_x(VALUE self) { @@ -241,11 +218,12 @@ static VALUE set_y(VALUE self, VALUE y) { CHECK; - return value(THIS->y = to<coord>(y)); + THIS->y = to<coord>(y); + return y; } static VALUE get_y(VALUE self) { @@ -257,11 +235,12 @@ static VALUE set_z(VALUE self, VALUE z) { CHECK; - return value(THIS->z = to<coord>(z)); + THIS->z = to<coord>(z); + return z; } static VALUE get_z(VALUE self) { @@ -273,11 +252,12 @@ static VALUE set_width(VALUE self, VALUE width) { CHECK; - return value(THIS->width = to<coord>(width)); + THIS->width = to<coord>(width); + return width; } static VALUE get_width(VALUE self) { @@ -289,11 +269,12 @@ static VALUE set_height(VALUE self, VALUE height) { CHECK; - return value(THIS->height = to<coord>(height)); + THIS->height = to<coord>(height); + return height; } static VALUE get_height(VALUE self) { @@ -305,11 +286,12 @@ static VALUE set_depth(VALUE self, VALUE depth) { CHECK; - return value(THIS->depth = to<coord>(depth)); + THIS->depth = to<coord>(depth); + return depth; } static VALUE get_depth(VALUE self) { @@ -320,14 +302,13 @@ static VALUE set_left(VALUE self, VALUE left) { CHECK; - Rays::Bounds* this_ = THIS; - this_->set_left(to<coord>(left)); - return value(this_->left()); + THIS->set_left(to<coord>(left)); + return left; } static VALUE get_left(VALUE self) { @@ -338,14 +319,13 @@ static VALUE set_right(VALUE self, VALUE right) { CHECK; - Rays::Bounds* this_ = THIS; - this_->set_right(to<coord>(right)); - return value(this_->right()); + THIS->set_right(to<coord>(right)); + return right; } static VALUE get_right(VALUE self) { @@ -356,14 +336,13 @@ static VALUE set_top(VALUE self, VALUE top) { CHECK; - Rays::Bounds* this_ = THIS; - this_->set_top(to<coord>(top)); - return value(this_->top()); + THIS->set_top(to<coord>(top)); + return top; } static VALUE get_top(VALUE self) { @@ -374,14 +353,13 @@ static VALUE set_bottom(VALUE self, VALUE bottom) { CHECK; - Rays::Bounds* this_ = THIS; - this_->set_bottom(to<coord>(bottom)); - return value(this_->bottom()); + THIS->set_bottom(to<coord>(bottom)); + return bottom; } static VALUE get_bottom(VALUE self) { @@ -392,14 +370,13 @@ static VALUE set_back(VALUE self, VALUE back) { CHECK; - Rays::Bounds* this_ = THIS; - this_->set_back(to<coord>(back)); - return value(this_->back()); + THIS->set_back(to<coord>(back)); + return back; } static VALUE get_back(VALUE self) { @@ -410,14 +387,13 @@ static VALUE set_front(VALUE self, VALUE front) { CHECK; - Rays::Bounds* this_ = THIS; - this_->set_front(to<coord>(front)); - return value(this_->front()); + THIS->set_front(to<coord>(front)); + return front; } static VALUE get_front(VALUE self) { @@ -428,15 +404,13 @@ static VALUE set_position(VALUE self) { CHECK; - check_arg_count(__FILE__, __LINE__, "Bounds#set_position", argc, 1, 2, 3); - coord* pos = THIS->position().array; - for (int i = 0; i < 3; ++i) - if (argc > i && !argv[i].is_nil()) pos[i] = to<coord>(argv[i]); + THIS->set_position(to<Rays::Point>(argc, argv)); + return value(THIS->position()); } static VALUE get_position(VALUE self) { @@ -447,15 +421,13 @@ static VALUE set_size(VALUE self) { CHECK; - check_arg_count(__FILE__, __LINE__, "Bounds#set_size", argc, 1, 2, 3); - coord* size = THIS->size().array; - for (int i = 0; i < 3; ++i) - if (argc > i && !argv[i].is_nil()) size[i] = to<coord>(argv[i]); + THIS->set_size(to<Rays::Point>(argc, argv)); + return value(THIS->size()); } static VALUE get_size(VALUE self) { @@ -466,76 +438,71 @@ static VALUE set_center(VALUE self) { CHECK; - check_arg_count(__FILE__, __LINE__, "Bounds#set_center", argc, 1, 2, 3); - if (argv[0].is_kind_of(Rays::point_class())) - THIS->set_center(to<Rays::Point&>(argv[0])); - else - { - Rays::Point p = THIS->center(); - coord x = (argc >= 1 && argv[0]) ? to<coord>(argv[0]) : p.x; - coord y = (argc >= 2 && argv[1]) ? to<coord>(argv[1]) : p.y; - coord z = (argc >= 3 && argv[2]) ? to<coord>(argv[2]) : p.z; - THIS->set_center(x, y, z); - } - + THIS->set_center(to<Rays::Point>(argc, argv)); return value(THIS->center()); } static -VALUE center(VALUE self) +VALUE get_center(VALUE self) { CHECK; return value(THIS->center()); } static -VALUE array_get(VALUE self, VALUE index) +VALUE set_at(VALUE self, VALUE index, VALUE value) { CHECK; int i = index.as_i(); if (i < 0 || 1 < i) index_error(__FILE__, __LINE__); - return value((*THIS)[i]); + (*THIS)[i] = to<Rays::Point&>(value); + return value; } static -VALUE array_set(VALUE self, VALUE index, VALUE value) +VALUE get_at(VALUE self, VALUE index) { CHECK; int i = index.as_i(); if (i < 0 || 1 < i) index_error(__FILE__, __LINE__); - (*THIS)[i] = to<Rays::Point&>(value); - return value; + return value((*THIS)[i]); } static -VALUE and_(VALUE self, VALUE bounds) +VALUE op_and(VALUE self, VALUE bounds) { CHECK; Rays::Bounds b = *THIS; b &= to<Rays::Bounds&>(bounds); return value(b); } static -VALUE or_(VALUE self, VALUE bounds) +VALUE op_or(VALUE self, VALUE arg) { CHECK; Rays::Bounds b = *THIS; - b |= to<Rays::Bounds&>(bounds); + if (arg.is_kind_of(Rays::bounds_class())) + b |= to<Rays::Bounds&>(arg); + else if (arg.is_kind_of(Rays::point_class())) + b |= to<Rays::Point&>(arg); + else + argument_error(__FILE__, __LINE__); + return value(b); } static VALUE inspect(VALUE self) @@ -543,11 +510,17 @@ CHECK; return value(Xot::stringf("#<Rays::Bounds %s>", THIS->inspect().c_str())); } +static +VALUE invalid(VALUE self) +{ + return value(Rays::invalid_bounds()); +} + static Class cBounds; void Init_bounds () { @@ -562,10 +535,11 @@ 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); @@ -586,78 +560,74 @@ 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, "set_position", RUBY_METHOD_FUNC(set_position), -1); + 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, "set_size", RUBY_METHOD_FUNC(set_size), -1); + 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, "set_center", RUBY_METHOD_FUNC(set_center), -1); - rb_define_method(cBounds, "center", RUBY_METHOD_FUNC(center), 0); - cBounds.define_method("[]", array_get); - cBounds.define_method("[]=", array_set); - cBounds.define_method("&", and_); - cBounds.define_method("|", or_); + 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<Rays::Bounds> (Value value, bool convert) + value_to<Rays::Bounds> (int argc, const Value* argv, bool convert) { - if (convert) + if (argc == 1 && argv->is_array()) { - size_t argc = 0; - Value* argv = NULL; - if (value.is_array()) - { - argc = value.size(); - argv = value.as_array(); - } - else - { - argc = 1; - argv = &value; - } + argc = argv->size(); + argv = argv->as_array(); + } - if (argc < 1) - Rucy::argument_error(__FILE__, __LINE__); + assert(argc == 0 || (argc > 0 && argv)); - if (argv[0].is_kind_of(Rays::bounds_class())) - value = argv[0]; - else if (argv[0].is_kind_of(Rays::point_class())) + if (convert) + { + if (argc == 0) + return Rays::Bounds(); + else if (argv->is_kind_of(Rays::point_class())) { switch (argc) { #define V(i) to<Rays::Point&>(argv[i]) case 1: return Rays::Bounds(V(0)); case 2: return Rays::Bounds(V(0), V(1)); #undef V - default: Rucy::argument_error(__FILE__, __LINE__); + default: argument_error(__FILE__, __LINE__); } } - else if (argv[0].is_i() || argv[0].is_f()) + 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: Rucy::argument_error(__FILE__, __LINE__); + default: argument_error(__FILE__, __LINE__); } } } - return value_to<Rays::Bounds&>(value, convert); + if (argc != 1) + argument_error(__FILE__, __LINE__); + + return value_to<Rays::Bounds&>(*argv, convert); } }// Rucy