2008-08-19 11:01:28 -04:00
|
|
|
/************************************************************
|
|
|
|
|
|
|
|
cvseq.cpp -
|
|
|
|
|
|
|
|
$Author: lsxi $
|
|
|
|
|
|
|
|
Copyright (C) 2005-2006 Masakazu Yonekura
|
|
|
|
|
|
|
|
************************************************************/
|
|
|
|
#include "cvseq.h"
|
|
|
|
/*
|
|
|
|
* Document-class: OpenCV::CvSeq
|
|
|
|
*
|
|
|
|
* Generic Sequence class. CvSeq has the method like Array (push, pop, select, etc...).
|
|
|
|
* But, CvSeq cannot store the object of a different class.
|
|
|
|
* When storing object in CvSeq, conversion is automatically tried,
|
|
|
|
* and the object occurs the error if it cannot be done.
|
|
|
|
*
|
|
|
|
* e.g.
|
|
|
|
* seq = CvSeq.new(CvPoint) # Argument mean "this sequence contain only this class's object"
|
|
|
|
* seq.push(CvPoint.new(0, 0)) # => it's ok
|
|
|
|
* seq.push("hello") # => try convert "hello" to CvPoint. but can't do it. raise error.
|
|
|
|
*
|
|
|
|
* If the sequecne contain object of class A.
|
|
|
|
* When storing object(named "obj") of class B to the sequence.
|
|
|
|
* Try automatically : A.from_B(obj) => object of class A.
|
|
|
|
*
|
|
|
|
* The sequence might have another sequence outside. see below.
|
|
|
|
* Each sequece has h_prev, h_next, v_prev, v_next method.
|
|
|
|
* If the adjoining sequence exists, each method return the adjoining sequence.
|
|
|
|
* Otherwise return nil.
|
|
|
|
*
|
|
|
|
* link:../images/CvSeq_relationmap.png
|
|
|
|
*/
|
|
|
|
__NAMESPACE_BEGIN_OPENCV
|
|
|
|
__NAMESPACE_BEGIN_CVSEQ
|
2011-02-14 02:30:42 -05:00
|
|
|
|
|
|
|
VALUE rb_allocate(VALUE klass);
|
|
|
|
void cvseq_free(void *ptr);
|
|
|
|
|
|
|
|
VALUE rb_klass;
|
2008-08-19 11:01:28 -04:00
|
|
|
// contain sequence-block class
|
2011-02-14 02:30:42 -05:00
|
|
|
st_table *seqblock_klass_table = st_init_numtable();
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_class()
|
|
|
|
{
|
|
|
|
return rb_klass;
|
|
|
|
}
|
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
VALUE
|
|
|
|
seqblock_class(void *ptr)
|
|
|
|
{
|
|
|
|
VALUE klass;
|
2011-02-14 02:30:42 -05:00
|
|
|
if (!st_lookup(seqblock_klass_table, (st_data_t)ptr, (st_data_t*)&klass)) {
|
2008-08-19 11:01:28 -04:00
|
|
|
rb_raise(rb_eTypeError, "Invalid sequence error.");
|
|
|
|
}
|
|
|
|
return klass;
|
|
|
|
}
|
|
|
|
|
2011-02-14 02:30:42 -05:00
|
|
|
void
|
2011-08-03 14:09:30 -04:00
|
|
|
register_elem_class(CvSeq *seq, VALUE klass)
|
2008-08-19 11:01:28 -04:00
|
|
|
{
|
2011-02-14 02:30:42 -05:00
|
|
|
st_insert(seqblock_klass_table, (st_data_t)seq, (st_data_t)klass);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
2011-08-03 14:09:30 -04:00
|
|
|
void
|
|
|
|
unregister_elem_class(void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr) {
|
|
|
|
st_delete(seqblock_klass_table, (st_data_t*)&ptr, NULL);
|
2011-08-11 11:53:48 -04:00
|
|
|
unregister_object(ptr);
|
2011-08-03 14:09:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
void
|
|
|
|
define_ruby_class()
|
|
|
|
{
|
2011-08-03 14:09:30 -04:00
|
|
|
if (rb_klass)
|
2008-08-19 11:01:28 -04:00
|
|
|
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, "CvSeq", rb_cObject);
|
2011-02-06 10:24:59 -05:00
|
|
|
rb_include_module(rb_klass, rb_mEnumerable);
|
2008-08-19 11:01:28 -04:00
|
|
|
rb_define_alloc_func(rb_klass, rb_allocate);
|
|
|
|
rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
|
|
|
|
rb_define_method(rb_klass, "total", RUBY_METHOD_FUNC(rb_total), 0);
|
|
|
|
rb_define_alias(rb_klass, "length", "total");
|
|
|
|
rb_define_alias(rb_klass, "size", "total");
|
|
|
|
rb_define_method(rb_klass, "empty?", RUBY_METHOD_FUNC(rb_empty_q), 0);
|
|
|
|
rb_define_method(rb_klass, "[]", RUBY_METHOD_FUNC(rb_aref), 1);
|
|
|
|
rb_define_method(rb_klass, "first", RUBY_METHOD_FUNC(rb_first), 0);
|
|
|
|
rb_define_method(rb_klass, "last", RUBY_METHOD_FUNC(rb_last), 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_klass, "h_prev", RUBY_METHOD_FUNC(rb_h_prev), 0);
|
|
|
|
rb_define_method(rb_klass, "h_next", RUBY_METHOD_FUNC(rb_h_next), 0);
|
|
|
|
rb_define_method(rb_klass, "v_prev", RUBY_METHOD_FUNC(rb_v_prev), 0);
|
|
|
|
rb_define_method(rb_klass, "v_next", RUBY_METHOD_FUNC(rb_v_next), 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_klass, "push", RUBY_METHOD_FUNC(rb_push), -2);
|
|
|
|
rb_define_alias(rb_klass, "<<", "push");
|
|
|
|
rb_define_method(rb_klass, "pop", RUBY_METHOD_FUNC(rb_pop), 0);
|
|
|
|
rb_define_method(rb_klass, "unshift", RUBY_METHOD_FUNC(rb_unshift), -2);
|
|
|
|
rb_define_alias(rb_klass, "push_front", "unshift");
|
|
|
|
rb_define_method(rb_klass, "shift", RUBY_METHOD_FUNC(rb_shift), 0);
|
|
|
|
rb_define_alias(rb_klass, "pop_front", "shift");
|
|
|
|
rb_define_method(rb_klass, "each", RUBY_METHOD_FUNC(rb_each), 0);
|
|
|
|
rb_define_method(rb_klass, "each_index", RUBY_METHOD_FUNC(rb_each_index), 0);
|
|
|
|
rb_define_method(rb_klass, "insert", RUBY_METHOD_FUNC(rb_insert), 2);
|
|
|
|
rb_define_method(rb_klass, "remove", RUBY_METHOD_FUNC(rb_remove), 1);
|
|
|
|
rb_define_alias(rb_klass, "delete_at", "remove");
|
2011-02-06 10:24:59 -05:00
|
|
|
rb_define_method(rb_klass, "clear", RUBY_METHOD_FUNC(rb_clear), 0);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_allocate(VALUE klass)
|
|
|
|
{
|
2011-02-14 02:30:42 -05:00
|
|
|
CvSeq *ptr = ALLOC(CvSeq);
|
2011-08-03 14:09:30 -04:00
|
|
|
return Data_Wrap_Struct(klass, 0, unregister_elem_class, ptr);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* CvSeq.new(<i>type[,storage]</i>)
|
|
|
|
*
|
|
|
|
* Return a new CvSeq. <i>type</i> should be following classes.
|
|
|
|
*
|
|
|
|
* * CvIndex
|
|
|
|
* * CvPoint
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2011-02-14 02:30:42 -05:00
|
|
|
VALUE klass, storage_value;
|
|
|
|
|
2011-08-03 14:09:30 -04:00
|
|
|
rb_scan_args(argc, argv, "11", &klass, &storage_value);
|
2011-07-31 14:33:09 -04:00
|
|
|
if (!rb_obj_is_kind_of(klass, rb_cClass))
|
2011-07-23 06:51:58 -04:00
|
|
|
raise_typeerror(klass, rb_cClass);
|
2011-02-14 02:30:42 -05:00
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
int type = 0, size = 0;
|
2011-05-02 12:47:05 -04:00
|
|
|
if (klass == rb_cFixnum) {
|
2008-08-19 11:01:28 -04:00
|
|
|
type = CV_SEQ_ELTYPE_INDEX;
|
2011-05-02 12:47:05 -04:00
|
|
|
size = sizeof(int);
|
2011-02-13 14:40:25 -05:00
|
|
|
}
|
|
|
|
else if (klass == cCvPoint::rb_class()) {
|
2008-08-19 11:01:28 -04:00
|
|
|
type = CV_SEQ_ELTYPE_POINT;
|
|
|
|
size = sizeof(CvPoint);
|
2011-02-13 14:40:25 -05:00
|
|
|
}
|
|
|
|
else if (klass == cCvPoint2D32f::rb_class()) {
|
2008-08-19 11:01:28 -04:00
|
|
|
type = CV_SEQ_ELTYPE_POINT;
|
|
|
|
size = sizeof(CvPoint2D32f);
|
2011-02-13 14:40:25 -05:00
|
|
|
}
|
|
|
|
else if (klass == cCvPoint3D32f::rb_class()) {
|
2008-08-19 11:01:28 -04:00
|
|
|
type = CV_SEQ_ELTYPE_POINT3D;
|
|
|
|
size = sizeof(CvPoint3D32f);
|
|
|
|
}
|
2011-05-02 12:47:05 -04:00
|
|
|
else
|
2011-07-23 06:51:58 -04:00
|
|
|
rb_raise(rb_eArgError, "unsupport %s class for sequence-block.", rb_class2name(klass));
|
2011-02-14 02:30:42 -05:00
|
|
|
|
2011-07-23 06:51:58 -04:00
|
|
|
CvSeq* seq = NULL;
|
2011-08-03 14:09:30 -04:00
|
|
|
if (NIL_P(storage_value)) {
|
|
|
|
storage_value = cCvMemStorage::new_object(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
storage_value = CHECK_CVMEMSTORAGE(storage_value);
|
|
|
|
}
|
|
|
|
|
2011-07-23 06:51:58 -04:00
|
|
|
try {
|
2011-08-03 14:09:30 -04:00
|
|
|
seq = cvCreateSeq(type, sizeof(CvSeq), size, CVMEMSTORAGE(storage_value));
|
2011-07-23 06:51:58 -04:00
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
DATA_PTR(self) = seq;
|
2011-08-03 14:09:30 -04:00
|
|
|
register_elem_class(seq, klass);
|
2011-08-11 11:53:48 -04:00
|
|
|
register_root_object(seq, storage_value);
|
2011-02-14 02:30:42 -05:00
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* total -> int
|
|
|
|
*
|
|
|
|
* Return total number of sequence-block.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_total(VALUE self)
|
|
|
|
{
|
2011-07-23 06:51:58 -04:00
|
|
|
return INT2NUM(CVSEQ(self)->total);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* empty? -> true or false.
|
|
|
|
*
|
|
|
|
* Return <tt>true</tt> if contain no object, otherwize return <tt>false</tt>.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_empty_q(VALUE self)
|
|
|
|
{
|
|
|
|
return CVSEQ(self)->total == 0 ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* [<i>index</i>] -> obj or nil
|
|
|
|
*
|
|
|
|
* Return sequence-block at <i>index</i>.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_aref(VALUE self, VALUE index)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-02-06 06:12:58 -05:00
|
|
|
int idx = NUM2INT(index);
|
2011-05-02 12:47:05 -04:00
|
|
|
if (seq->total == 0)
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
2011-02-06 06:12:58 -05:00
|
|
|
if (idx >= seq->total)
|
|
|
|
rb_raise(rb_eIndexError, "index %d out of sequence", idx);
|
2011-05-02 12:47:05 -04:00
|
|
|
|
2011-07-23 06:51:58 -04:00
|
|
|
VALUE result = Qnil;
|
|
|
|
try {
|
|
|
|
if (seqblock_class(seq) == rb_cFixnum)
|
|
|
|
result = INT2NUM(*CV_GET_SEQ_ELEM(int, seq, idx));
|
|
|
|
else
|
|
|
|
result = REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, idx), self);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
|
|
|
return result;
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* first -> obj or nil
|
|
|
|
*
|
|
|
|
* Return first sequence-block.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_first(VALUE self)
|
|
|
|
{
|
2011-05-02 12:47:05 -04:00
|
|
|
return rb_aref(self, INT2FIX(0));
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* last -> obj or nil
|
|
|
|
*
|
|
|
|
* Return last sequence-block.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_last(VALUE self)
|
|
|
|
{
|
2011-05-02 12:47:05 -04:00
|
|
|
return rb_aref(self, INT2FIX(-1));
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* h_prev -> seq or nil
|
|
|
|
*
|
|
|
|
* Return the sequence horizontally located in previous.
|
|
|
|
* Return <tt>nil</tt> if not existing.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_h_prev(VALUE self)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-02-06 06:12:58 -05:00
|
|
|
if (seq->h_prev)
|
2008-08-19 11:01:28 -04:00
|
|
|
return new_sequence(CLASS_OF(self), seq->h_prev, seqblock_class(seq), lookup_root_object(seq));
|
2011-02-06 06:12:58 -05:00
|
|
|
else
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* h_next -> seq or nil
|
|
|
|
*
|
|
|
|
* Return the sequence horizontally located in next.
|
|
|
|
* Return <tt>nil</tt> if not existing.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_h_next(VALUE self)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-02-06 06:12:58 -05:00
|
|
|
if (seq->h_next)
|
2008-08-19 11:01:28 -04:00
|
|
|
return new_sequence(CLASS_OF(self), seq->h_next, seqblock_class(seq), lookup_root_object(seq));
|
2011-02-06 06:12:58 -05:00
|
|
|
else
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* v_prev -> seq or nil
|
|
|
|
*
|
|
|
|
* Return the sequence vertically located in previous.
|
|
|
|
* Return <tt>nil</tt> if not existing.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_v_prev(VALUE self)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-02-06 06:12:58 -05:00
|
|
|
if (seq->v_prev)
|
2008-08-19 11:01:28 -04:00
|
|
|
return new_sequence(CLASS_OF(self), seq->v_prev, seqblock_class(seq), lookup_root_object(seq));
|
2011-02-06 06:12:58 -05:00
|
|
|
else
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-02-06 06:12:58 -05:00
|
|
|
* v_next -> seq or nil
|
2008-08-19 11:01:28 -04:00
|
|
|
*
|
|
|
|
* Return the sequence vertically located in next.
|
|
|
|
* Return <tt>nil</tt> if not existing.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_v_next(VALUE self)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-02-06 06:12:58 -05:00
|
|
|
if (seq->v_next)
|
2008-08-19 11:01:28 -04:00
|
|
|
return new_sequence(CLASS_OF(self), seq->v_next, seqblock_class(seq), lookup_root_object(seq));
|
2011-02-06 06:12:58 -05:00
|
|
|
else
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2011-02-06 06:12:58 -05:00
|
|
|
rb_seq_push(VALUE self, VALUE args, int flag)
|
2008-08-19 11:01:28 -04:00
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
|
|
|
VALUE klass = seqblock_class(seq), object;
|
2011-05-02 12:47:05 -04:00
|
|
|
void *elem = NULL;
|
|
|
|
int len = RARRAY_LEN(args);
|
|
|
|
for (int i = 0; i < len; ++i) {
|
2010-12-29 13:39:15 -05:00
|
|
|
object = RARRAY_PTR(args)[i];
|
2011-02-06 06:12:58 -05:00
|
|
|
if (CLASS_OF(object) == klass) {
|
2011-05-02 12:47:05 -04:00
|
|
|
if (TYPE(object) == T_FIXNUM) {
|
|
|
|
int int_elem = FIX2INT(object);
|
|
|
|
elem = &int_elem;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
elem = (void*)DATA_PTR(object);
|
|
|
|
}
|
2011-07-23 06:51:58 -04:00
|
|
|
try {
|
|
|
|
if (flag == CV_FRONT)
|
|
|
|
cvSeqPushFront(seq, elem);
|
|
|
|
else
|
|
|
|
cvSeqPush(seq, elem);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2011-02-06 06:12:58 -05:00
|
|
|
}
|
|
|
|
else if (rb_obj_is_kind_of(object, rb_klass) && CLASS_OF(rb_first(object)) == klass) { // object is CvSeq
|
2011-07-23 06:51:58 -04:00
|
|
|
void *buffer = NULL;
|
|
|
|
try {
|
|
|
|
buffer = cvCvtSeqToArray(CVSEQ(object), rb_cvAlloc(CVSEQ(object)->total * CVSEQ(object)->elem_size));
|
|
|
|
cvSeqPushMulti(seq, buffer, CVSEQ(object)->total, flag);
|
|
|
|
cvFree(&buffer);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
if (buffer != NULL)
|
|
|
|
cvFree(&buffer);
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2011-02-06 06:12:58 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eTypeError, "arguments should be %s or %s which includes %s.",
|
|
|
|
rb_class2name(klass), rb_class2name(rb_klass), rb_class2name(klass));
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
}
|
2011-05-02 12:47:05 -04:00
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
return self;
|
|
|
|
}
|
2011-02-06 06:12:58 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* push(<i>obj, ...</i>) -> self
|
|
|
|
*
|
|
|
|
* Append - Pushes the given object(s) on the end of this sequence. This expression return the sequence itself,
|
|
|
|
* so several append may be chained together.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_push(VALUE self, VALUE args)
|
|
|
|
{
|
|
|
|
return rb_seq_push(self, args, CV_BACK);
|
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* pop -> obj or nil
|
|
|
|
*
|
|
|
|
* Remove the last sequence-block from <i>self</i> and return it,
|
|
|
|
* or <tt>nil</tt> if the sequence is empty.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_pop(VALUE self)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-05-02 12:47:05 -04:00
|
|
|
if (seq->total == 0)
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
2011-05-02 12:47:05 -04:00
|
|
|
|
2011-07-31 14:33:09 -04:00
|
|
|
VALUE object = Qnil;
|
2011-05-02 12:47:05 -04:00
|
|
|
VALUE klass = seqblock_class(seq);
|
2011-07-23 06:51:58 -04:00
|
|
|
try {
|
|
|
|
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));
|
|
|
|
}
|
2011-05-02 12:47:05 -04:00
|
|
|
}
|
2011-07-23 06:51:58 -04:00
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* clear -> self
|
|
|
|
*
|
|
|
|
* Clears sequence. Removes all elements from the sequence.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_clear(VALUE self)
|
|
|
|
{
|
2011-07-23 06:51:58 -04:00
|
|
|
try {
|
|
|
|
cvClearSeq(CVSEQ(self));
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* unshift -> self
|
|
|
|
*
|
|
|
|
* Prepends objects to the front of sequence. other elements up one.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_unshift(VALUE self, VALUE args)
|
|
|
|
{
|
2011-07-23 06:51:58 -04:00
|
|
|
VALUE result = Qnil;
|
|
|
|
try {
|
|
|
|
result = rb_seq_push(self, args, CV_FRONT);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
|
|
|
return result;
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* shift -> obj or nil
|
|
|
|
*
|
|
|
|
* Returns the first element of <i>self</i> and removes it (shifting all other elements down by one). Returns <tt>nil</tt> if the array is empty.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_shift(VALUE self)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-05-02 12:47:05 -04:00
|
|
|
if(seq->total == 0)
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
2011-05-02 12:47:05 -04:00
|
|
|
|
2011-07-23 06:51:58 -04:00
|
|
|
VALUE object = Qnil;
|
|
|
|
try {
|
|
|
|
if (seqblock_class(seq) == rb_cFixnum) {
|
|
|
|
int n = 0;
|
|
|
|
cvSeqPopFront(seq, &n);
|
|
|
|
object = INT2NUM(n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
object = GENERIC_OBJECT(seqblock_class(seq), malloc(seq->elem_size));
|
|
|
|
cvSeqPopFront(seq, DATA_PTR(object));
|
|
|
|
}
|
2011-05-02 12:47:05 -04:00
|
|
|
}
|
2011-07-23 06:51:58 -04:00
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
2011-05-02 12:47:05 -04:00
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* each{|obj| ... } -> self
|
|
|
|
*
|
|
|
|
* Calls block once for each sequence-block in <i>self</i>,
|
|
|
|
* passing that sequence-block as a parameter.
|
|
|
|
* seq = CvSeq.new(CvIndex)
|
|
|
|
* seq.push(5, 6, 7)
|
2011-02-06 06:12:58 -05:00
|
|
|
* seq.each{|x| print x, " -- " }
|
2008-08-19 11:01:28 -04:00
|
|
|
* produces:
|
|
|
|
* 5 -- 6 -- 7 --
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_each(VALUE self)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-05-02 12:47:05 -04:00
|
|
|
if (seq->total > 0) {
|
2008-08-19 11:01:28 -04:00
|
|
|
VALUE klass = seqblock_class(seq);
|
2011-07-23 06:51:58 -04:00
|
|
|
try {
|
|
|
|
if (klass == rb_cFixnum)
|
|
|
|
for (int i = 0; i < seq->total; ++i)
|
|
|
|
rb_yield(INT2NUM(*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));
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* each_index{|index| ... } -> self
|
|
|
|
*
|
|
|
|
* Same as CvSeq#each, but passes the index of the element instead of the element itself.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_each_index(VALUE self)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
2011-05-02 12:47:05 -04:00
|
|
|
for(int i = 0; i < seq->total; ++i)
|
2011-07-23 06:51:58 -04:00
|
|
|
rb_yield(INT2NUM(i));
|
2008-08-19 11:01:28 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* insert(<i>index,obj</i>) -> self
|
|
|
|
*
|
|
|
|
* Inserts the given values before element with the given index (which may be negative).
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_insert(VALUE self, VALUE index, VALUE object)
|
|
|
|
{
|
|
|
|
Check_Type(index, T_FIXNUM);
|
|
|
|
CvSeq *seq = CVSEQ(self);
|
|
|
|
VALUE klass = seqblock_class(seq);
|
2011-05-02 12:47:05 -04:00
|
|
|
if (CLASS_OF(object) != klass)
|
2011-02-06 06:12:58 -05:00
|
|
|
rb_raise(rb_eTypeError, "arguments should be %s.", rb_class2name(klass));
|
2011-07-23 06:51:58 -04:00
|
|
|
try {
|
|
|
|
if (klass == rb_cFixnum) {
|
|
|
|
int n = NUM2INT(object);
|
|
|
|
cvSeqInsert(seq, NUM2INT(index), &n);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cvSeqInsert(seq, NUM2INT(index), DATA_PTR(object));
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
2011-05-02 12:47:05 -04:00
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* remove(<i>index</i>) -> obj or nil
|
|
|
|
*
|
|
|
|
* Deletes the elements at the specified index.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_remove(VALUE self, VALUE index)
|
|
|
|
{
|
2011-07-23 06:51:58 -04:00
|
|
|
try {
|
|
|
|
cvSeqRemove(CVSEQ(self), NUM2INT(index));
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
new_sequence(VALUE klass, CvSeq *seq, VALUE element_klass, VALUE storage)
|
|
|
|
{
|
2011-08-11 11:53:48 -04:00
|
|
|
register_root_object(seq, storage);
|
2008-08-19 11:01:28 -04:00
|
|
|
if (!NIL_P(element_klass))
|
2011-08-03 14:09:30 -04:00
|
|
|
register_elem_class(seq, element_klass);
|
|
|
|
return Data_Wrap_Struct(klass, mark_root_object, unregister_elem_class, seq);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
auto_extend(VALUE object)
|
|
|
|
{
|
|
|
|
CvSeq *seq = CVSEQ(object);
|
2011-08-03 14:09:30 -04:00
|
|
|
if (CV_IS_SEQ_POINT_SET(seq)) {
|
2008-08-19 11:01:28 -04:00
|
|
|
rb_extend_object(object, mPointSet::rb_module());
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
__NAMESPACE_END_CVSEQ
|
|
|
|
__NAMESPACE_END_OPENCV
|