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

implemented setters and constructor of CvBox2D, and added some tests for them

This commit is contained in:
ser1zw 2011-01-10 04:43:04 +09:00
parent 9791dae3bd
commit 874ed9df69
3 changed files with 160 additions and 0 deletions

View file

@ -41,9 +41,14 @@ define_ruby_class()
*/
VALUE opencv = rb_module_opencv();
rb_klass = rb_define_class_under(opencv, "CvBox2D", rb_cObject);
rb_define_alloc_func(rb_klass, rb_allocate);
rb_define_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
rb_define_method(rb_klass, "center", RUBY_METHOD_FUNC(rb_center), 0);
rb_define_method(rb_klass, "center=", RUBY_METHOD_FUNC(rb_set_center), 1);
rb_define_method(rb_klass, "size", RUBY_METHOD_FUNC(rb_size), 0);
rb_define_method(rb_klass, "size=", RUBY_METHOD_FUNC(rb_set_size), 1);
rb_define_method(rb_klass, "angle", RUBY_METHOD_FUNC(rb_angle), 0);
rb_define_method(rb_klass, "angle=", RUBY_METHOD_FUNC(rb_set_angle), 1);
rb_define_method(rb_klass, "points", RUBY_METHOD_FUNC(rb_points), 0);
}
@ -54,7 +59,32 @@ rb_allocate(VALUE klass)
return Data_Make_Struct(klass, CvBox2D, 0, -1, ptr);
}
/*
* call-seq:
* CvBox2D.new(<i>[center][, size][, angle]</i>) -> cvbox2d
*
* Create a box
*/
VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE center, size, angle;
rb_scan_args(argc, argv, "03", &center, &size, &angle);
if (!NIL_P(center))
CVBOX2D(self)->center = VALUE_TO_CVPOINT2D32F(center);
if (!NIL_P(size))
CVBOX2D(self)->size = VALUE_TO_CVSIZE2D32F(size);
if (!NIL_P(size))
CVBOX2D(self)->angle = NUM2DBL(angle);
return self;
}
/*
* call-seq:
* center -> cvpoint2d32f
* Return center point of box as CvPoint2D32f.
*/
VALUE
@ -64,6 +94,23 @@ rb_center(VALUE self)
}
/*
* call-seq:
* center = <i>value</i>
*
* Set center to <i>value</i>
*/
VALUE
rb_set_center(VALUE self, VALUE value)
{
if (!cCvPoint2D32f::rb_compatible_q(rb_klass, value))
rb_raise(rb_eArgError, "object is not compatible %s.", rb_class2name(cCvPoint2D32f::rb_class()));
CVBOX2D(self)->center = VALUE_TO_CVPOINT2D32F(value);
return self;
}
/*
* call-seq:
* size -> cvsize2d32f
* Return size of box as CvSize2D32f.
*/
VALUE
@ -73,6 +120,24 @@ rb_size(VALUE self)
}
/*
* call-seq:
* size = <i>value</i>
*
* Set size to <i>value</i>
*/
VALUE
rb_set_size(VALUE self, VALUE value)
{
if (!cCvSize2D32f::rb_compatible_q(rb_klass, value))
rb_raise(rb_eArgError, "object is not compatible %s.", rb_class2name(cCvSize2D32f::rb_class()));
CVBOX2D(self)->size = VALUE_TO_CVSIZE2D32F(value);
return self;
}
/*
* call-seq:
* angle -> float
*
* Return angle of box as Float.
*/
VALUE
@ -82,6 +147,21 @@ rb_angle(VALUE self)
}
/*
* call-seq:
* angle = <i>value</i>
*
* Set angle to <i>value</i>
*/
VALUE
rb_set_angle(VALUE self, VALUE value)
{
CVBOX2D(self)->angle = NUM2DBL(value);
return self;
}
/*
* call-seq:
* points -> array(include cvpoint2d32f)
* Find box vertices. Return Array contain 4 CvPoint2D32f.
*/
VALUE

View file

@ -24,9 +24,13 @@ void define_ruby_class();
VALUE rb_allocate(VALUE klass);
VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
VALUE rb_center(VALUE self);
VALUE rb_set_center(VALUE self, VALUE value);
VALUE rb_size(VALUE self);
VALUE rb_set_size(VALUE self, VALUE value);
VALUE rb_angle(VALUE self);
VALUE rb_set_angle(VALUE self, VALUE value);
VALUE rb_points(VALUE self);
VALUE new_object();

76
test/test_cvbox2d.rb Executable file
View file

@ -0,0 +1,76 @@
#!/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::CvBox2D
class TestCvBox2D < OpenCVTestCase
class MyPoint; end
def test_initialize
box = CvBox2D.new
assert_in_delta(0, box.center.x, 0.001)
assert_in_delta(0, box.center.y, 0.001)
assert_in_delta(0, box.size.width, 0.001)
assert_in_delta(0, box.size.height, 0.001)
assert_in_delta(0, box.angle, 0.001)
box = CvBox2D.new(CvPoint2D32f.new(1.1, 2.2), CvSize2D32f.new(3.3, 4.4), 5.5)
assert_in_delta(1.1, box.center.x, 0.001)
assert_in_delta(2.2, box.center.y, 0.001)
assert_in_delta(3.3, box.size.width, 0.001)
assert_in_delta(4.4, box.size.height, 0.001)
assert_in_delta(5.5, box.angle, 0.001)
end
def test_center
box = CvBox2D.new
box.center = CvPoint2D32f.new(1.1, 2.2)
assert_in_delta(1.1, box.center.x, 0.001)
assert_in_delta(2.2, box.center.y, 0.001)
box.center.x = 3.3
box.center.y = 4.4
assert_in_delta(3.3, box.center.x, 0.001)
assert_in_delta(4.4, box.center.y, 0.001)
end
def test_size
box = CvBox2D.new
box.size = CvSize2D32f.new(1.1, 2.2)
assert_in_delta(1.1, box.size.width, 0.001)
assert_in_delta(2.2, box.size.height, 0.001)
box.size.width = 3.3
box.size.height = 4.4
assert_in_delta(3.3, box.size.width, 0.001)
assert_in_delta(4.4, box.size.height, 0.001)
end
def test_angle
box = CvBox2D.new
box.angle = 1.1
assert_in_delta(1.1, box.angle, 0.001)
end
def test_points
box = CvBox2D.new
box.center = CvPoint2D32f.new(10, 20)
box.size = CvSize2D32f.new(5, 7)
pt = box.points
assert_equal(4, pt.size)
assert_in_delta(7.5, pt[0].x, 0.001)
assert_in_delta(23.5, pt[0].y, 0.001)
assert_in_delta(7.5, pt[1].x, 0.001)
assert_in_delta(16.5, pt[1].y, 0.001)
assert_in_delta(12.5, pt[2].x, 0.001)
assert_in_delta(16.5, pt[2].y, 0.001)
assert_in_delta(12.5, pt[3].x, 0.001)
assert_in_delta(23.5, pt[3].y, 0.001)
end
end