From 5a0ae9e5246f48786f58274a10239b32168cc2f9 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sat, 19 Mar 2011 18:56:16 +0900 Subject: [PATCH] implemented CvLine#[] and tested CvLine --- ext/cvline.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ ext/cvline.h | 2 ++ test/test_cvline.rb | 50 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100755 test/test_cvline.rb diff --git a/ext/cvline.cpp b/ext/cvline.cpp index bc592a8..792209d 100644 --- a/ext/cvline.cpp +++ b/ext/cvline.cpp @@ -41,6 +41,8 @@ define_ruby_class() rb_define_method(rb_klass, "rho=", RUBY_METHOD_FUNC(rb_set_rho), 1); rb_define_method(rb_klass, "theta", RUBY_METHOD_FUNC(rb_theta), 0); rb_define_method(rb_klass, "theta=", RUBY_METHOD_FUNC(rb_set_theta), 1); + rb_define_method(rb_klass, "[]", RUBY_METHOD_FUNC(rb_aref), 1); + rb_define_method(rb_klass, "[]=", RUBY_METHOD_FUNC(rb_aset), 2); } VALUE @@ -94,6 +96,52 @@ rb_set_theta(VALUE self, VALUE theta) return self; } +/* + * call-seq: + * [index] + * + * Return value of index dimension. + */ +VALUE +rb_aref(VALUE self, VALUE index) +{ + switch (NUM2INT(index)) { + case 0: + return DBL2NUM(CVLINE(self)->rho); + break; + case 1: + return DBL2NUM(CVLINE(self)->theta); + break; + default: + rb_raise(rb_eIndexError, "index should be 0...2"); + break; + } + return Qnil; +} + +/* + * call-seq: + * [index] = value + * + * Set value of index dimension to value + */ +VALUE +rb_aset(VALUE self, VALUE index, VALUE value) +{ + switch (NUM2INT(index)) { + case 0: + CVLINE(self)->rho = NUM2DBL(value); + break; + case 1: + CVLINE(self)->theta = NUM2DBL(value); + break; + default: + rb_raise(rb_eIndexError, "index should be 0...2"); + break; + } + return value; +} + VALUE new_object(CvLine line) { diff --git a/ext/cvline.h b/ext/cvline.h index 6accb73..8923614 100644 --- a/ext/cvline.h +++ b/ext/cvline.h @@ -34,6 +34,8 @@ VALUE rb_rho(VALUE self); VALUE rb_set_rho(VALUE self, VALUE rho); VALUE rb_theta(VALUE self); VALUE rb_set_theta(VALUE self, VALUE theta); +VALUE rb_aref(VALUE self, VALUE index); +VALUE rb_aset(VALUE self, VALUE index, VALUE value); VALUE new_object(CvLine line); diff --git a/test/test_cvline.rb b/test/test_cvline.rb new file mode 100755 index 0000000..01847ac --- /dev/null +++ b/test/test_cvline.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby +# -*- mode: ruby; coding: utf-8-unix -*- +require 'test/unit' +require 'opencv' +require File.expand_path(File.dirname(__FILE__)) + '/helper' + +include OpenCV + +# Tests for OpenCV::CvLine +class TestCvLine < OpenCVTestCase + def setup + @line = CvLine.new + end + + def test_initialize + assert_not_nil(@line) + assert_equal(CvLine, @line.class) + end + + def test_rho + @line.rho = 0.0 + assert_in_delta(0.0, @line.rho, 0.001) + @line.rho = 3.14 + assert_in_delta(3.14, @line.rho, 0.001) + end + + def test_theta + @line.theta = 0.0 + assert_in_delta(0.0, @line.theta, 0.001) + @line.theta = 3.14 + assert_in_delta(3.14, @line.theta, 0.001) + end + + def test_aref_aset + @line[0] = 0.0 + @line[1] = 0.0 + assert_in_delta(0.0, @line[0], 0.001) + assert_in_delta(0.0, @line[1], 0.001) + + @line[0] = 3.14 + @line[1] = 2.71 + assert_in_delta(3.14, @line[0], 0.001) + assert_in_delta(2.71, @line[1], 0.001) + + assert_raise(IndexError) { + @line[2] = 1 + } + end +end +