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

removed CvRect#or, implemented CvRect.max_rect instead of CvRect#or, and added some tests for CvRect

This commit is contained in:
ser1zw 2011-01-09 18:21:59 +09:00
parent e23e7a838e
commit 7467964ee8
3 changed files with 154 additions and 16 deletions

View file

@ -48,6 +48,7 @@ define_ruby_class()
rb_klass = rb_define_class_under(opencv, "CvRect", rb_cObject);
rb_define_alloc_func(rb_klass, rb_allocate);
rb_define_singleton_method(rb_klass, "compatible?", RUBY_METHOD_FUNC(rb_compatible_q), 1);
rb_define_singleton_method(rb_klass, "max_rect", RUBY_METHOD_FUNC(rb_max_rect), 2);
rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
rb_define_method(rb_klass, "x", RUBY_METHOD_FUNC(rb_x), 0);
@ -64,8 +65,8 @@ define_ruby_class()
rb_define_method(rb_klass, "top_right", RUBY_METHOD_FUNC(rb_top_right), 0);
rb_define_method(rb_klass, "bottom_left", RUBY_METHOD_FUNC(rb_bottom_left), 0);
rb_define_method(rb_klass, "bottom_right", RUBY_METHOD_FUNC(rb_bottom_right), 0);
rb_define_method(rb_klass, "or", RUBY_METHOD_FUNC(rb_or), 1);
rb_define_alias(rb_klass, "|", "or");
// rb_define_method(rb_klass, "or", RUBY_METHOD_FUNC(rb_or), 1);
// rb_define_alias(rb_klass, "|", "or");
}
/*
@ -99,6 +100,18 @@ rb_compatible_q(VALUE klass, VALUE object)
return (rb_respond_to(object, rb_intern("x")) && rb_respond_to(object, rb_intern("y")) && rb_respond_to(object, rb_intern("width")) && rb_respond_to(object, rb_intern("height"))) ? Qtrue : Qfalse;
}
/*
* call-seq:
* max_rect(rect1, rect2) -> cvrect
*
* Finds bounding rectangle for given rectangles.
*/
VALUE
rb_max_rect(VALUE klass, VALUE rect1, VALUE rect2)
{
return cCvRect::new_object(cvMaxRect(CVRECT(rect1), CVRECT(rect2)));
}
/*
* call-seq:
* CvRect.bounding
@ -316,18 +329,6 @@ rb_bottom_right(VALUE self)
return cCvPoint::new_object(cvPoint(CVRECT(self)->x + CVRECT(self)->width, CVRECT(self)->y + CVRECT(self)->height));
}
/*
* call-seq:
* or(rect) -> cvrect
*
* Finds bounding rectangle for self and given rectangles.
*/
VALUE
rb_or(VALUE self, VALUE rect)
{
return cCvRect::new_object(cvMaxRect(CVRECT(self), CVRECT(rect)));
}
VALUE
new_object(CvRect rect)
{

View file

@ -23,6 +23,7 @@ VALUE rb_class();
void define_ruby_class();
VALUE rb_compatible_q(VALUE klass, VALUE object);
VALUE rb_max_rect(VALUE klass, VALUE rect1, VALUE rect2);
VALUE rb_bounding(VALUE klass, VALUE points);
VALUE rb_allocate(VALUE klass);
@ -47,8 +48,6 @@ VALUE rb_set_bottom_left(VALUE self, VALUE point);
VALUE rb_bottom_right(VALUE self);
VALUE rb_set_bottom_right(VALUE self, VALUE point);
VALUE rb_or(VALUE self, VALUE rect);
VALUE new_object(CvRect rect);
__NAMESPACE_END_CVRECT

138
test/test_cvrect.rb Executable file
View file

@ -0,0 +1,138 @@
#!/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::CvRect
class TestCvRect < OpenCVTestCase
class MyRect; end
def test_x
rect = CvRect.new
rect.x = 100
assert_equal(100, rect.x)
rect.x = 200
assert_equal(200, rect.x)
end
def test_y
rect = CvRect.new
rect.y = 100
assert_equal(100, rect.y)
rect.y = 200
assert_equal(200, rect.y)
end
def test_width
rect = CvRect.new
rect.width = 100
assert_equal(100, rect.width)
rect.width = 200
assert_equal(200, rect.width)
end
def test_height
rect = CvRect.new
rect.height = 100
assert_equal(100, rect.height)
rect.height = 200
assert_equal(200, rect.height)
end
def test_compatible
assert(!(CvRect.compatible? MyRect.new))
MyRect.class_eval { def x; end }
assert(!(CvRect.compatible? MyRect.new))
MyRect.class_eval { def y; end }
assert(!(CvRect.compatible? MyRect.new))
MyRect.class_eval { def width; end }
assert(!(CvRect.compatible? MyRect.new))
MyRect.class_eval { def height; end }
assert(CvRect.compatible? MyRect.new)
assert(CvRect.compatible? CvRect.new)
end
def test_initialize
rect = CvRect.new
assert_equal(0, rect.x)
assert_equal(0, rect.y)
assert_equal(0, rect.width)
assert_equal(0, rect.height)
rect = CvRect.new(10, 20, 30, 40)
assert_equal(10, rect.x)
assert_equal(20, rect.y)
assert_equal(30, rect.width)
assert_equal(40, rect.height)
rect = CvRect.new(CvRect.new(10, 20, 30, 40))
assert_equal(10, rect.x)
assert_equal(20, rect.y)
assert_equal(30, rect.width)
assert_equal(40, rect.height)
assert_raise(ArgumentError) {
CvRect.new('string')
}
assert_raise(ArgumentError) {
CvRect.new(1, 2, 3)
}
end
def test_center
center = CvRect.new(10, 20, 35, 45).center
assert_in_delta(27.5, center.x, 0.01)
assert_in_delta(42.5, center.y, 0.01)
end
def test_points
points = CvRect.new(10, 20, 35, 45).points
assert_equal(4, points.size)
assert_in_delta(10, points[0].x, 0.01)
assert_in_delta(20, points[0].y, 0.01)
assert_in_delta(10, points[1].x, 0.01)
assert_in_delta(65, points[1].y, 0.01)
assert_in_delta(45, points[2].x, 0.01)
assert_in_delta(65, points[2].y, 0.01)
assert_in_delta(45, points[3].x, 0.01)
assert_in_delta(20, points[3].y, 0.01)
end
def test_top_left
tl = CvRect.new(10, 20, 35, 45).top_left
assert_equal(10, tl.x)
assert_equal(20, tl.y)
end
def test_top_right
tr = CvRect.new(10, 20, 35, 45).top_right
assert_equal(45, tr.x)
assert_equal(20, tr.y)
end
def test_bottom_left
bl = CvRect.new(10, 20, 35, 45).bottom_left
assert_equal(10, bl.x)
assert_equal(65, bl.y)
end
def test_bottom_right
br = CvRect.new(10, 20, 35, 45).bottom_right
assert_equal(45, br.x)
assert_equal(65, br.y)
end
def test_max_rect
rect1 = CvRect.new(10, 20, 30, 40)
rect2 = CvRect.new(30, 40, 70, 80)
rect3 = CvRect.max_rect(rect1, rect2)
assert_equal(10, rect3.x)
assert_equal(20, rect3.y)
assert_equal(90, rect3.width)
assert_equal(100, rect3.height)
end
end