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

- old
+ new

@@ -1,19 +1,13 @@ #include "rays/ruby/point.h" -#include <rucy.h> #include "defs.h" -using namespace Rucy; +RUCY_DEFINE_VALUE_OR_ARRAY_FROM_TO(Rays::Point) -using Rays::coord; - - -RUCY_DEFINE_VALUE_FROM_TO(Rays::Point) - #define THIS to<Rays::Point*>(self) #define CHECK RUCY_CHECK_OBJ(Rays::Point, self) @@ -27,26 +21,13 @@ 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<coord>(argv[0])); - break; + if (argc >= 1) + *THIS = to<Rays::Point>(argc, argv); - case 2: - *THIS = Rays::Point(to<coord>(argv[0]), to<coord>(argv[1])); - break; - - case 3: - *THIS = Rays::Point( - to<coord>(argv[0]), to<coord>(argv[1]), to<coord>(argv[2])); - break; - } - return self; } static VALUE initialize_copy(VALUE self, VALUE obj) @@ -94,10 +75,26 @@ return self; } static +VALUE rotate(VALUE self, VALUE degree) +{ + CHECK; + Rays::Point p = *THIS; + p.rotate(to<float>(degree)); + return value(p); +} + +static +VALUE rotate_self(VALUE self, VALUE degree) +{ + CHECK; + THIS->rotate(to<float>(degree)); +} + +static VALUE length(VALUE self) { CHECK; return value(THIS->length()); } @@ -119,11 +116,12 @@ 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) { @@ -133,11 +131,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) { @@ -147,83 +146,91 @@ 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) { CHECK; return value(THIS->z); } static -VALUE add(VALUE self, VALUE point) +VALUE negate(VALUE self) { CHECK; - - Rays::Point p = *THIS; - p += to<Rays::Point&>(point); - return value(p); + return value(-*THIS); } static -VALUE sub(VALUE self, VALUE point) +VALUE add(VALUE self) { CHECK; - - Rays::Point p = *THIS; - p -= to<Rays::Point&>(point); - return value(p); + if (argc == 1 && argv->is_num()) + return value(*THIS + to<coord>(*argv)); + else + return value(*THIS + to<Rays::Point>(argc, argv)); } static -VALUE mult(VALUE self, VALUE point) +VALUE sub(VALUE self) { CHECK; - - Rays::Point p = *THIS; - p *= to<Rays::Point&>(point); - return value(p); + if (argc == 1 && argv->is_num()) + return value(*THIS - to<coord>(*argv)); + else + return value(*THIS - to<Rays::Point>(argc, argv)); } static -VALUE div(VALUE self, VALUE point) +VALUE mult(VALUE self) { CHECK; + if (argc == 1 && argv->is_num()) + return value(*THIS * to<coord>(*argv)); + else + return value(*THIS * to<Rays::Point>(argc, argv)); +} - Rays::Point p = *THIS; - p /= to<Rays::Point&>(point); - return value(p); +static +VALUE div(VALUE self) +{ + CHECK; + if (argc == 1 && argv->is_num()) + return value(*THIS / to<coord>(*argv)); + else + return value(*THIS / to<Rays::Point>(argc, argv)); } 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 || 2 < i) index_error(__FILE__, __LINE__); - return value((*THIS)[i]); + (*THIS)[i] = to<coord>(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 || 2 < i) index_error(__FILE__, __LINE__); - (*THIS)[i] = to<coord>(value); - return value; + return value((*THIS)[i]); } static VALUE inspect(VALUE self) { @@ -243,70 +250,68 @@ 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, "rotate", RUBY_METHOD_FUNC(rotate), 1); + cPoint.define_method("rotate!", rotate_self); 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); + cPoint.define_method("-@", negate); + cPoint.define_method("+", add); + cPoint.define_method("-", sub); + cPoint.define_method("*", mult); + cPoint.define_method("/", div); + cPoint.define_method("[]=", set_at); + cPoint.define_method("[]", get_at); rb_define_method(cPoint, "inspect", RUBY_METHOD_FUNC(inspect), 0); } namespace Rucy { template <> Rays::Point - value_to<Rays::Point> (Value value, bool convert) + value_to<Rays::Point> (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::point_class())) - value = argv[0]; - else if (argv[0].is_i() || argv[0].is_f()) + if (convert) + { + if (argc == 0) + return Rays::Point(); + else if (argv->is_num()) { switch (argc) { - #define V(i) argv[i].as_f(true) + #define V(i) to<coord>(argv[i]) 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__); + default: argument_error(__FILE__, __LINE__); } } } - return value_to<Rays::Point&>(value, convert); + if (argc != 1) + argument_error(__FILE__, __LINE__); + + return value_to<Rays::Point&>(*argv, convert); } }// Rucy