1
0
Fork 0
mirror of https://github.com/ruby-opencv/ruby-opencv synced 2023-03-27 23:22:12 -04:00

implemented CvLine#[] and tested CvLine

This commit is contained in:
ser1zw 2011-03-19 18:56:16 +09:00
parent 72d9de3a65
commit 5a0ae9e524
3 changed files with 100 additions and 0 deletions

View file

@ -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, "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_theta), 0);
rb_define_method(rb_klass, "theta=", RUBY_METHOD_FUNC(rb_set_theta), 1); 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 VALUE
@ -94,6 +96,52 @@ rb_set_theta(VALUE self, VALUE theta)
return self; return self;
} }
/*
* call-seq:
* [<i>index</i>]
*
* Return value of <i>index</i> 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:
* [<i>index</i>] = <i>value</i>
*
* Set value of <i>index</i> dimension to <i>value</i>
*/
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 VALUE
new_object(CvLine line) new_object(CvLine line)
{ {

View file

@ -34,6 +34,8 @@ VALUE rb_rho(VALUE self);
VALUE rb_set_rho(VALUE self, VALUE rho); VALUE rb_set_rho(VALUE self, VALUE rho);
VALUE rb_theta(VALUE self); VALUE rb_theta(VALUE self);
VALUE rb_set_theta(VALUE self, VALUE theta); 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); VALUE new_object(CvLine line);

50
test/test_cvline.rb Executable file
View file

@ -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