ext/rays/point.cpp in rays-0.1.12 vs 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) @@ -28,26 +22,13 @@ RUCY_DEFN(initialize) { 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; } RUCY_END static @@ -99,10 +80,28 @@ return self; } RUCY_END static +RUCY_DEF1(rotate, degree) +{ + CHECK; + Rays::Point p = *THIS; + p.rotate(to<float>(degree)); + return value(p); +} +RUCY_END + +static +RUCY_DEF1(rotate_self, degree) +{ + CHECK; + THIS->rotate(to<float>(degree)); +} +RUCY_END + +static RUCY_DEF0(length) { CHECK; return value(THIS->length()); } @@ -127,11 +126,12 @@ static RUCY_DEF1(set_x, x) { CHECK; - return value(THIS->x = to<coord>(x)); + THIS->x = to<coord>(x); + return x; } RUCY_END static RUCY_DEF0(get_x) @@ -143,11 +143,12 @@ static RUCY_DEF1(set_y, y) { CHECK; - return value(THIS->y = to<coord>(y)); + THIS->y = to<coord>(y); + return y; } RUCY_END static RUCY_DEF0(get_y) @@ -159,11 +160,12 @@ static RUCY_DEF1(set_z, z) { CHECK; - return value(THIS->z = to<coord>(z)); + THIS->z = to<coord>(z); + return z; } RUCY_END static RUCY_DEF0(get_z) @@ -172,77 +174,85 @@ return value(THIS->z); } RUCY_END static -RUCY_DEF1(add, point) +RUCY_DEF0(negate) { CHECK; - - Rays::Point p = *THIS; - p += to<Rays::Point&>(point); - return value(p); + return value(-*THIS); } RUCY_END static -RUCY_DEF1(sub, point) +RUCY_DEFN(add) { 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)); } RUCY_END static -RUCY_DEF1(mult, point) +RUCY_DEFN(sub) { 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)); } RUCY_END static -RUCY_DEF1(div, point) +RUCY_DEFN(mult) { CHECK; + if (argc == 1 && argv->is_num()) + return value(*THIS * to<coord>(*argv)); + else + return value(*THIS * to<Rays::Point>(argc, argv)); +} +RUCY_END - Rays::Point p = *THIS; - p /= to<Rays::Point&>(point); - return value(p); +static +RUCY_DEFN(div) +{ + CHECK; + if (argc == 1 && argv->is_num()) + return value(*THIS / to<coord>(*argv)); + else + return value(*THIS / to<Rays::Point>(argc, argv)); } RUCY_END static -RUCY_DEF1(array_get, index) +RUCY_DEF2(set_at, index, 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; } RUCY_END static -RUCY_DEF2(array_set, index, value) +RUCY_DEF1(get_at, 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]); } RUCY_END static RUCY_DEF0(inspect) @@ -260,74 +270,72 @@ { Module mRays = define_module("Rays"); cPoint = mRays.define_class("Point"); cPoint.define_alloc_func(alloc); - cPoint.define_private_method("initialize", initialize); + cPoint.define_private_method("initialize", initialize); cPoint.define_private_method("initialize_copy", initialize_copy); cPoint.define_method("move_to!", move_to); cPoint.define_method("move_by!", move_by); + cPoint.define_method("rotate", rotate); + cPoint.define_method("rotate!", rotate_self); cPoint.define_method("length", length); cPoint.define_method("normalize", normalize); cPoint.define_method("normal", normal); cPoint.define_method("x=", set_x); - cPoint.define_method("x", get_x); + cPoint.define_method("x", get_x); cPoint.define_method("y=", set_y); - cPoint.define_method("y", get_y); + cPoint.define_method("y", get_y); cPoint.define_method("z=", set_z); - cPoint.define_method("z", get_z); - cPoint.define_method("op_add", add); - cPoint.define_method("op_sub", sub); - cPoint.define_method("op_mult", mult); - cPoint.define_method("op_div", div); - cPoint.define_method("[]", array_get); - cPoint.define_method("[]=", array_set); + cPoint.define_method("z", get_z); + 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); cPoint.define_method("inspect", inspect); } 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