Class: OpenCV::CvSeq

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

Instance Method Summary collapse

Constructor Details

#new(seq_flags, storage = nil) ⇒ CvSeq

Constructor

Examples:

seq1 = CvSeq.new(CV_SEQ_ELTYPE_INDEX)
seq1 << 1
seq1 << CvPoint.new(1, 2) #=> TypeError

seq2 = CvSeq.new(CV_SEQ_ELTYPE_POINT | CV_SEQ_KIND_CURVE)
seq2 << CvPoint.new(1, 2)
seq2 << 3 #=> TypeError

Parameters:

  • seq_flags (Fixnum)

    Flags of the created sequence, which are combinations of the element types and sequence types.

    • Element type:

    • Sequence type:

      • CV_SEQ_KIND_GENERIC: Generic sequence

      • CV_SEQ_KIND_CURVE: Curve

  • storage (CvMemStorage) (defaults to: nil)

    Sequence location



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/opencv/cvseq.cpp', line 157

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE seq_flags_value, storage_value;
  rb_scan_args(argc, argv, "11", &seq_flags_value, &storage_value);
  int seq_flags = 0;

  if (TYPE(seq_flags_value) == T_CLASS) { // To maintain backward compatibility
    seq_flags_value = class2seq_flags_value(seq_flags_value);
  }
  Check_Type(seq_flags_value, T_FIXNUM);
  seq_flags = FIX2INT(seq_flags_value);

  DATA_PTR(self) = create_seq(seq_flags, sizeof(CvSeq), storage_value);

  return self;
}

Instance Method Details

#[](index) ⇒ Object

Return sequence-block at index.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/opencv/cvseq.cpp', line 205

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 {
    VALUE klass = seqblock_class(seq);
    if (RTEST(rb_class_inherited_p(klass, rb_cInteger))) {
      result = INT2NUM(*CV_GET_SEQ_ELEM(int, seq, idx));
    }
    else {
      result = REFER_OBJECT(klass, cvGetSeqElem(seq, idx), self);
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return result;
}

#clearself

Clears sequence. Removes all elements from the sequence.

Returns:

  • (self)


431
432
433
434
435
436
437
438
439
440
441
# File 'ext/opencv/cvseq.cpp', line 431

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)


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

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)


533
534
535
536
537
538
539
540
# File 'ext/opencv/cvseq.cpp', line 533

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)


193
194
195
196
197
# File 'ext/opencv/cvseq.cpp', line 193

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

#firstObject?

Return first sequence-block.

Returns:

  • (Object, nil)


239
240
241
242
243
# File 'ext/opencv/cvseq.cpp', line 239

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)


281
282
283
284
285
286
287
288
289
# File 'ext/opencv/cvseq.cpp', line 281

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)


264
265
266
267
268
269
270
271
272
# File 'ext/opencv/cvseq.cpp', line 264

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)


549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'ext/opencv/cvseq.cpp', line 549

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)


251
252
253
254
255
# File 'ext/opencv/cvseq.cpp', line 251

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)


399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'ext/opencv/cvseq.cpp', line 399

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)


386
387
388
389
390
# File 'ext/opencv/cvseq.cpp', line 386

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)


577
578
579
580
581
582
583
584
585
586
587
# File 'ext/opencv/cvseq.cpp', line 577

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)


468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'ext/opencv/cvseq.cpp', line 468

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)


181
182
183
184
185
# File 'ext/opencv/cvseq.cpp', line 181

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)


449
450
451
452
453
454
455
456
457
458
459
460
# File 'ext/opencv/cvseq.cpp', line 449

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)


315
316
317
318
319
320
321
322
323
# File 'ext/opencv/cvseq.cpp', line 315

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)


298
299
300
301
302
303
304
305
306
# File 'ext/opencv/cvseq.cpp', line 298

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