Class: OpenCV::CvSeq

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/opencv/cvseq.cpp,
ext/opencv/cvseq.cpp

Overview

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.

Instance Method Summary collapse

Constructor Details

#new(type[,storage]) ⇒ Object

Return a new CvSeq. type should be following classes.

  • CvIndex

  • CvPoint



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/opencv/cvseq.cpp', line 92

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE klass, storage_value;

  rb_scan_args(argc, argv, "11", &klass, &storage_value);
  if (!rb_obj_is_kind_of(klass, rb_cClass))
    raise_typeerror(klass, rb_cClass);

  int type = 0, size = 0;
  if (klass == rb_cFixnum) {
    type = CV_SEQ_ELTYPE_INDEX;
    size = sizeof(int);
  }
  else if (klass == cCvPoint::rb_class()) {
    type = CV_SEQ_ELTYPE_POINT;
    size = sizeof(CvPoint);
  }
  else if (klass == cCvPoint2D32f::rb_class()) {
    type = CV_SEQ_ELTYPE_POINT;
    size = sizeof(CvPoint2D32f);
  }
  else if (klass == cCvPoint3D32f::rb_class()) {
    type = CV_SEQ_ELTYPE_POINT3D;
    size = sizeof(CvPoint3D32f);
  }
  else
    rb_raise(rb_eArgError, "unsupport %s class for sequence-block.", rb_class2name(klass));
  
  CvSeq* seq = NULL;
  if (NIL_P(storage_value)) {
    storage_value = cCvMemStorage::new_object(0);
  }
  else {
    storage_value = CHECK_CVMEMSTORAGE(storage_value);
  }
  
  try {
    seq = cvCreateSeq(type, sizeof(CvSeq), size, CVMEMSTORAGE(storage_value));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  DATA_PTR(self) = seq;
  register_elem_class(seq, klass);
  register_root_object(seq, storage_value);
  
  return self;
}

Instance Method Details

#[](index) ⇒ Object

Return sequence-block at index.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/opencv/cvseq.cpp', line 172

VALUE
rb_aref(VALUE self, VALUE index)
{
  CvSeq *seq = CVSEQ(self);
  int idx = NUM2INT(index);
  if (seq->total == 0)
    return Qnil;
  if (idx >= seq->total)
    rb_raise(rb_eIndexError, "index %d out of sequence", idx);

  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;
}

#clearself

Clears sequence. Removes all elements from the sequence.

Returns:

  • (self)


388
389
390
391
392
393
394
395
396
397
398
# File 'ext/opencv/cvseq.cpp', line 388

VALUE
rb_clear(VALUE self)
{
  try {
    cvClearSeq(CVSEQ(self));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#each {|obj| ... } ⇒ self

Calls block once for each sequence-block in self, passing that sequence-block as a parameter.

seq = CvSeq.new(CvIndex)
seq.push(5, 6, 7)
seq.each {|x| print x, " -- " }

produces:

5 -- 6 -- 7 --

Yields:

  • (obj)

Returns:

  • (self)


463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'ext/opencv/cvseq.cpp', line 463

VALUE
rb_each(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if (seq->total > 0) {
    VALUE klass = seqblock_class(seq);
    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);
    }
  }
  return self;
}

#each_index {|index| ... } ⇒ self

Same as CvSeq#each, but passes the index of the element instead of the element itself.

Yields:

  • (index)

Returns:

  • (self)


490
491
492
493
494
495
496
497
# File 'ext/opencv/cvseq.cpp', line 490

VALUE
rb_each_index(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  for(int i = 0; i < seq->total; ++i)
    rb_yield(INT2NUM(i));
  return self;
}

#empty?Boolean

Return true if contain no object, otherwize return false.

Returns:

  • (Boolean)


160
161
162
163
164
# File 'ext/opencv/cvseq.cpp', line 160

VALUE
rb_empty_q(VALUE self)
{
  return CVSEQ(self)->total == 0 ? Qtrue : Qfalse;
}

#firstObject?

Return first sequence-block.

Returns:

  • (Object, nil)


201
202
203
204
205
# File 'ext/opencv/cvseq.cpp', line 201

VALUE
rb_first(VALUE self)
{
  return rb_aref(self, INT2FIX(0));
}

#h_nextnil

Return the sequence horizontally located in next. Return nil if not existing.

Returns:

  • (nil)


243
244
245
246
247
248
249
250
251
# File 'ext/opencv/cvseq.cpp', line 243

VALUE
rb_h_next(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if (seq->h_next)
    return new_sequence(CLASS_OF(self), seq->h_next, seqblock_class(seq), lookup_root_object(seq));
  else
    return Qnil;
}

#h_prevnil

Return the sequence horizontally located in previous. Return nil if not existing.

Returns:

  • (nil)


226
227
228
229
230
231
232
233
234
# File 'ext/opencv/cvseq.cpp', line 226

VALUE
rb_h_prev(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if (seq->h_prev)
    return new_sequence(CLASS_OF(self), seq->h_prev, seqblock_class(seq), lookup_root_object(seq));
  else
    return Qnil;
}

#insert(index, obj) ⇒ self

Inserts the given values before element with the given index (which may be negative).

Returns:

  • (self)


506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'ext/opencv/cvseq.cpp', line 506

VALUE
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)
    rb_raise(rb_eTypeError, "arguments should be %s.", rb_class2name(klass));
  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);
  }
  return self;
}

#lastObject?

Return last sequence-block.

Returns:

  • (Object, nil)


213
214
215
216
217
# File 'ext/opencv/cvseq.cpp', line 213

VALUE
rb_last(VALUE self)
{
  return rb_aref(self, INT2FIX(-1));
}

#popObject?

Remove the last sequence-block from self and return it, or nil if the sequence is empty.

Returns:

  • (Object, nil)


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'ext/opencv/cvseq.cpp', line 356

VALUE
rb_pop(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if (seq->total == 0)
    return Qnil;
  
  VALUE object = Qnil;
  VALUE klass = seqblock_class(seq);
  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));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return object;
}

#push(obj, ...) ⇒ self Also known as: <<

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.

Returns:

  • (self)


343
344
345
346
347
# File 'ext/opencv/cvseq.cpp', line 343

VALUE
rb_push(VALUE self, VALUE args)
{
  return rb_seq_push(self, args, CV_BACK);
}

#remove(index) ⇒ Object? Also known as: delete_at

Deletes the elements at the specified index.

Returns:

  • (Object, nil)


534
535
536
537
538
539
540
541
542
543
544
# File 'ext/opencv/cvseq.cpp', line 534

VALUE
rb_remove(VALUE self, VALUE index)
{
  try {
    cvSeqRemove(CVSEQ(self), NUM2INT(index));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#shiftObject? Also known as: pop_front

Returns the first element of self and removes it (shifting all other elements down by one). Returns nil if the array is empty.

Returns:

  • (Object, nil)


425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'ext/opencv/cvseq.cpp', line 425

VALUE
rb_shift(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if (seq->total == 0)
    return Qnil;

  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));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return object;
}

#totalInteger Also known as: length, size

Return total number of sequence-block.

Returns:

  • (Integer)


148
149
150
151
152
# File 'ext/opencv/cvseq.cpp', line 148

VALUE
rb_total(VALUE self)
{
  return INT2NUM(CVSEQ(self)->total);
}

#unshiftself Also known as: push_front

Prepends objects to the front of sequence. other elements up one.

Returns:

  • (self)


406
407
408
409
410
411
412
413
414
415
416
417
# File 'ext/opencv/cvseq.cpp', line 406

VALUE
rb_unshift(VALUE self, VALUE args)
{
  VALUE result = Qnil;
  try {
    result = rb_seq_push(self, args, CV_FRONT);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return result;
}

#v_nextnil

Return the sequence vertically located in next. Return nil if not existing.

Returns:

  • (nil)


277
278
279
280
281
282
283
284
285
# File 'ext/opencv/cvseq.cpp', line 277

VALUE
rb_v_next(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if (seq->v_next)
    return new_sequence(CLASS_OF(self), seq->v_next, seqblock_class(seq), lookup_root_object(seq));
  else
    return Qnil;
}

#v_prevnil

Return the sequence vertically located in previous. Return nil if not existing.

Returns:

  • (nil)


260
261
262
263
264
265
266
267
268
# File 'ext/opencv/cvseq.cpp', line 260

VALUE
rb_v_prev(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if (seq->v_prev)
    return new_sequence(CLASS_OF(self), seq->v_prev, seqblock_class(seq), lookup_root_object(seq));
  else
    return Qnil;
}