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

modified CvSeq to also use Fixnum as elements, and removed CvIndex

This commit is contained in:
ser1zw 2011-05-03 01:47:05 +09:00
parent e0881b208c
commit f5a11ea191
6 changed files with 118 additions and 147 deletions

View file

@ -1,73 +0,0 @@
/************************************************************
cvindex.cpp -
$Author: lsxi $
Copyright (C) 2005 Masakazu Yonekura
************************************************************/
#include"cvindex.h"
/*
* Document-class: OpenCV::CvIndex
*
*
*/
namespace mOpenCV{
namespace cCvIndex{
VALUE rb_klass;
VALUE rb_class(){
return rb_klass;
}
void define_ruby_class(){
if(rb_klass)
return;
/*
* opencv = rb_define_module("OpenCV");
*
* note: this comment is used by rdoc.
*/
VALUE opencv = rb_module_opencv();
rb_klass = rb_define_class_under(opencv, "CvIndex", rb_cObject);
rb_define_alloc_func(rb_klass, rb_allocate);
rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
rb_define_singleton_method(rb_klass, "cv_new", RUBY_METHOD_FUNC(rb_cv_new), -1);
rb_define_method(rb_klass, "value", RUBY_METHOD_FUNC(rb_value), 0);
rb_define_method(rb_klass, "to_s", RUBY_METHOD_FUNC(rb_to_s), 0);
}
VALUE rb_allocate(VALUE klass){
CvIndex *ptr;
return Data_Make_Struct(rb_klass, CvIndex, 0, free, ptr);
}
VALUE rb_initialize(int argc, VALUE *argv, VALUE self){
VALUE value;
if(rb_scan_args(argc, argv, "01", &value) > 0){
Check_Type(value, T_FIXNUM);
CVINDEX(self)->value = FIX2INT(value);
}
return self;
}
VALUE rb_cv_new(int argc, VALUE *argv, VALUE klass){
VALUE object = rb_allocate(klass);
return rb_initialize(argc, argv, object);
}
VALUE rb_value(VALUE self){
return INT2FIX(CVINDEX(self)->value);
}
VALUE rb_to_s(VALUE self){
return rb_funcall(INT2FIX(CVINDEX(self)->value), rb_intern("to_s"), 0);
}
}
}

View file

@ -1,40 +0,0 @@
/************************************************************
cvindex.h -
$Author: lsxi $
Copyright (C) 2005 Masakazu Yonekura
************************************************************/
#ifndef RUBY_OPENCV_CVINDEX_H
#define RUBY_OPENCV_CVINDEX_H
#include"opencv.h"
namespace mOpenCV{
typedef struct CvIndex{
int value;
}CvIndex;
namespace cCvIndex{
VALUE rb_class();
void define_ruby_class();
VALUE rb_allocate(VALUE klass);
VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
VALUE rb_cv_new(int argc, VALUE *argv, VALUE klass);
VALUE rb_value(VALUE self);
VALUE rb_to_s(VALUE self);
}
inline CvIndex *CVINDEX(VALUE object){
CvIndex *ptr;
Data_Get_Struct(object, CvIndex, ptr);
return ptr;
}
}
#endif // RUBY_OPENCV_CVINDEX_H

View file

@ -151,9 +151,9 @@ rb_initialize(int argc, VALUE *argv, VALUE self)
rb_raise(rb_eTypeError, "argument 1 (sequence-block class) should be %s.", rb_class2name(rb_cClass));
int type = 0, size = 0;
if (klass == cCvIndex::rb_class()) {
if (klass == rb_cFixnum) {
type = CV_SEQ_ELTYPE_INDEX;
size = sizeof(CvIndex);
size = sizeof(int);
}
else if (klass == cCvPoint::rb_class()) {
type = CV_SEQ_ELTYPE_POINT;
@ -167,8 +167,7 @@ rb_initialize(int argc, VALUE *argv, VALUE self)
type = CV_SEQ_ELTYPE_POINT3D;
size = sizeof(CvPoint3D32f);
}
// todo: more various class will be support.
if (!size)
else
rb_raise(rb_eTypeError, "unsupport %s class for sequence-block.", rb_class2name(klass));
CvSeq* seq = cvCreateSeq(type, sizeof(CvSeq), size, storage);
@ -213,11 +212,15 @@ rb_aref(VALUE self, VALUE index)
{
CvSeq *seq = CVSEQ(self);
int idx = NUM2INT(index);
if(!(seq->total > 0))
if (seq->total == 0)
return Qnil;
if (idx >= seq->total)
rb_raise(rb_eIndexError, "index %d out of sequence", idx);
return REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, idx), self);
if (seqblock_class(seq) == rb_cFixnum)
return INT2FIX(*CV_GET_SEQ_ELEM(int, seq, idx));
else
return REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, idx), self);
}
/*
@ -229,10 +232,7 @@ rb_aref(VALUE self, VALUE index)
VALUE
rb_first(VALUE self)
{
CvSeq *seq = CVSEQ(self);
if(!(seq->total > 0))
return Qnil;
return REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, 0), self);
return rb_aref(self, INT2FIX(0));
}
/*
@ -244,10 +244,7 @@ rb_first(VALUE self)
VALUE
rb_last(VALUE self)
{
CvSeq *seq = CVSEQ(self);
if(!(seq->total > 0))
return Qnil;
return REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, -1), self);
return rb_aref(self, INT2FIX(-1));
}
/*
@ -324,13 +321,22 @@ rb_seq_push(VALUE self, VALUE args, int flag)
CvSeq *seq = CVSEQ(self);
VALUE klass = seqblock_class(seq), object;
void *buffer = NULL;
for (int i = 0; i < RARRAY_LEN(args); i++) {
void *elem = NULL;
int len = RARRAY_LEN(args);
for (int i = 0; i < len; ++i) {
object = RARRAY_PTR(args)[i];
if (CLASS_OF(object) == klass) {
if (TYPE(object) == T_FIXNUM) {
int int_elem = FIX2INT(object);
elem = &int_elem;
}
else {
elem = (void*)DATA_PTR(object);
}
if (flag == CV_FRONT)
cvSeqPushFront(seq, DATA_PTR(object));
cvSeqPushFront(seq, elem);
else
cvSeqPush(seq, DATA_PTR(object));
cvSeqPush(seq, elem);
}
else if (rb_obj_is_kind_of(object, rb_klass) && CLASS_OF(rb_first(object)) == klass) { // object is CvSeq
buffer = cvCvtSeqToArray(CVSEQ(object), rb_cvAlloc(CVSEQ(object)->total * CVSEQ(object)->elem_size));
@ -342,6 +348,7 @@ rb_seq_push(VALUE self, VALUE args, int flag)
rb_class2name(klass), rb_class2name(rb_klass), rb_class2name(klass));
}
}
return self;
}
@ -369,11 +376,20 @@ VALUE
rb_pop(VALUE self)
{
CvSeq *seq = CVSEQ(self);
if(!(seq->total > 0)){
if (seq->total == 0)
return Qnil;
VALUE object;
VALUE klass = seqblock_class(seq);
if (klass == rb_cFixnum) {
int n = 0;
cvSeqPop(seq, &n);
object = INT2FIX(n);
}
else {
object = GENERIC_OBJECT(klass, malloc(seq->elem_size));
cvSeqPop(seq, DATA_PTR(object));
}
VALUE object = GENERIC_OBJECT(seqblock_class(seq), malloc(seq->elem_size));
cvSeqPop(seq, DATA_PTR(object));
return object;
}
@ -412,11 +428,20 @@ VALUE
rb_shift(VALUE self)
{
CvSeq *seq = CVSEQ(self);
if(!(seq->total > 0)){
if(seq->total == 0)
return Qnil;
VALUE object;
if (seqblock_class(seq) == rb_cFixnum) {
int n = 0;
cvSeqPopFront(seq, &n);
object = INT2FIX(n);
}
VALUE object = GENERIC_OBJECT(seqblock_class(seq), malloc(seq->elem_size));
cvSeqPopFront(seq, DATA_PTR(object));
else {
object = GENERIC_OBJECT(seqblock_class(seq), malloc(seq->elem_size));
cvSeqPopFront(seq, DATA_PTR(object));
}
return object;
}
@ -436,11 +461,14 @@ VALUE
rb_each(VALUE self)
{
CvSeq *seq = CVSEQ(self);
if(seq->total > 0){
if (seq->total > 0) {
VALUE klass = seqblock_class(seq);
for(int i = 0; i < seq->total; i++){
rb_yield(REFER_OBJECT(klass, cvGetSeqElem(seq, i), self));
}
if (klass == rb_cFixnum)
for (int i = 0; i < seq->total; ++i)
rb_yield(INT2FIX(*CV_GET_SEQ_ELEM(int, seq, i)));
else
for (int i = 0; i < seq->total; ++i)
rb_yield(REFER_OBJECT(klass, cvGetSeqElem(seq, i), self));
}
return self;
}
@ -455,7 +483,7 @@ VALUE
rb_each_index(VALUE self)
{
CvSeq *seq = CVSEQ(self);
for(int i = 0; i < seq->total; i++)
for(int i = 0; i < seq->total; ++i)
rb_yield(INT2FIX(i));
return self;
}
@ -473,9 +501,14 @@ rb_insert(VALUE self, VALUE index, VALUE object)
Check_Type(index, T_FIXNUM);
CvSeq *seq = CVSEQ(self);
VALUE klass = seqblock_class(seq);
if(CLASS_OF(object) != klass)
if (CLASS_OF(object) != klass)
rb_raise(rb_eTypeError, "arguments should be %s.", rb_class2name(klass));
cvSeqInsert(seq, FIX2INT(index), DATA_PTR(object));
if (klass == rb_cFixnum) {
int n = NUM2INT(object);
cvSeqInsert(seq, FIX2INT(index), &n);
}
else
cvSeqInsert(seq, FIX2INT(index), DATA_PTR(object));
return self;
}

View file

@ -761,7 +761,6 @@ extern "C"{
mOpenCV::cCvHistogram::define_ruby_class();
mOpenCV::cCvCapture::define_ruby_class();
mOpenCV::cCvIndex::define_ruby_class();
mOpenCV::cCvLine::define_ruby_class();
mOpenCV::cCvTwoPoints::define_ruby_class();
mOpenCV::cCvCircle32f::define_ruby_class();

View file

@ -117,7 +117,6 @@ extern "C"{
#include "cvhistogram.h"
#include "cvcapture.h"
#include "cvindex.h"
#include "cvline.h"
#include "cvtwopoints.h"
#include "cvcircle32f.h"

View file

@ -9,7 +9,8 @@ include OpenCV
# Tests for OpenCV::CvSeq
class TestCvSeq < OpenCVTestCase
def test_initialize
assert_not_nil(CvSeq.new(CvIndex))
# assert_not_nil(CvSeq.new(CvIndex))
assert_not_nil(CvSeq.new(Fixnum))
assert_not_nil(CvSeq.new(CvPoint))
assert_not_nil(CvSeq.new(CvPoint2D32f))
assert_not_nil(CvSeq.new(CvPoint3D32f))
@ -47,6 +48,13 @@ class TestCvSeq < OpenCVTestCase
assert_equal(40, seq1[1].y)
assert_equal(50, seq1[2].x)
assert_equal(60, seq1[2].y)
seq2 = CvSeq.new(Fixnum).push(10, 20, 30)
assert_equal(Fixnum, seq2[0].class)
assert_equal(10, seq2[0])
assert_equal(20, seq2[1])
assert_equal(30, seq2[2])
end
def test_push
@ -73,6 +81,15 @@ class TestCvSeq < OpenCVTestCase
assert_equal(30, seq2[2].x)
assert_equal(40, seq2[2].y)
seq3 = CvSeq.new(Fixnum).push(10)
seq4 = CvSeq.new(Fixnum).push(20, 30)
seq3.push(seq4)
assert_equal(3, seq3.total)
assert_equal(Fixnum, seq3[0].class)
assert_equal(10, seq3[0])
assert_equal(20, seq3[1])
assert_equal(30, seq3[2])
assert_raise(TypeError) {
seq1.push(CvPoint2D32f.new(55.5, 66.6))
}
@ -96,6 +113,11 @@ class TestCvSeq < OpenCVTestCase
assert_equal(20, seq1[0].y)
assert_nil(CvSeq.new(CvPoint).pop)
seq2 = CvSeq.new(Fixnum).push(10, 20, 30)
assert_equal(30, seq2.pop)
assert_equal(20, seq2.pop)
assert_equal(10, seq2.pop)
end
def test_clear
@ -129,6 +151,12 @@ class TestCvSeq < OpenCVTestCase
assert_equal(50, seq2[2].x)
assert_equal(60, seq2[2].y)
seq3 = CvSeq.new(Fixnum).unshift(10, 20, 30)
assert_equal(3, seq3.total)
assert_equal(30, seq3[0])
assert_equal(20, seq3[1])
assert_equal(10, seq3[2])
assert_raise(TypeError) {
seq1.unshift(CvPoint2D32f.new(55.5, 66.6))
}
@ -151,6 +179,11 @@ class TestCvSeq < OpenCVTestCase
assert_equal(30, seq1[0].x)
assert_equal(40, seq1[0].y)
seq2 = CvSeq.new(Fixnum).push(10, 20, 30)
assert_equal(10, seq2.shift)
assert_equal(20, seq2.shift)
assert_equal(30, seq2.shift)
assert_nil(CvSeq.new(CvPoint).shift)
end
@ -160,6 +193,9 @@ class TestCvSeq < OpenCVTestCase
assert_equal(CvPoint, point1.class)
assert_equal(10, point1.x)
assert_equal(20, point1.y)
seq2 = CvSeq.new(Fixnum).push(10, 20, 30)
assert_equal(10, seq2.first)
end
def test_last
@ -168,6 +204,9 @@ class TestCvSeq < OpenCVTestCase
assert_equal(CvPoint, point1.class)
assert_equal(50, point1.x)
assert_equal(60, point1.y)
seq2 = CvSeq.new(Fixnum).push(10, 20, 30)
assert_equal(30, seq2.last)
end
def test_each
@ -180,6 +219,14 @@ class TestCvSeq < OpenCVTestCase
i += 1
}
assert_equal(3, i)
seq2 = CvSeq.new(Fixnum).push(10, 20, 30)
i = 0
seq2.each { |s|
assert_equal(seq2[i], s)
i += 1
}
assert_equal(3, i)
end
def test_each_index
@ -194,7 +241,6 @@ class TestCvSeq < OpenCVTestCase
def test_insert
seq1 = CvSeq.new(CvPoint).push(CvPoint.new(10, 20), CvPoint.new(30, 40))
seq1.insert(1, CvPoint.new(50, 60))
assert_equal(3, seq1.total)
assert_equal(CvPoint, seq1[0].class)
@ -206,6 +252,13 @@ class TestCvSeq < OpenCVTestCase
assert_equal(CvPoint, seq1[2].class)
assert_equal(30, seq1[2].x)
assert_equal(40, seq1[2].y)
seq2 = CvSeq.new(Fixnum).push(10, 20)
seq2.insert(1, 15)
assert_equal(3, seq2.total)
assert_equal(10, seq2[0])
assert_equal(15, seq2[1])
assert_equal(20, seq2[2])
end
def test_remove