1 package org.opencv.core;
2 
3 import java.util.Arrays;
4 import java.util.List;
5 
6 public class MatOfPoint extends Mat {
7     // 32SC2
8     private static final int _depth = CvType.CV_32S;
9     private static final int _channels = 2;
10 
MatOfPoint()11     public MatOfPoint() {
12         super();
13     }
14 
MatOfPoint(long addr)15     protected MatOfPoint(long addr) {
16         super(addr);
17         if( !empty() && checkVector(_channels, _depth) < 0 )
18             throw new IllegalArgumentException("Incompatible Mat");
19         //FIXME: do we need release() here?
20     }
21 
fromNativeAddr(long addr)22     public static MatOfPoint fromNativeAddr(long addr) {
23         return new MatOfPoint(addr);
24     }
25 
MatOfPoint(Mat m)26     public MatOfPoint(Mat m) {
27         super(m, Range.all());
28         if( !empty() && checkVector(_channels, _depth) < 0 )
29             throw new IllegalArgumentException("Incompatible Mat");
30         //FIXME: do we need release() here?
31     }
32 
MatOfPoint(Point...a)33     public MatOfPoint(Point...a) {
34         super();
35         fromArray(a);
36     }
37 
alloc(int elemNumber)38     public void alloc(int elemNumber) {
39         if(elemNumber>0)
40             super.create(elemNumber, 1, CvType.makeType(_depth, _channels));
41     }
42 
fromArray(Point...a)43     public void fromArray(Point...a) {
44         if(a==null || a.length==0)
45             return;
46         int num = a.length;
47         alloc(num);
48         int buff[] = new int[num * _channels];
49         for(int i=0; i<num; i++) {
50             Point p = a[i];
51             buff[_channels*i+0] = (int) p.x;
52             buff[_channels*i+1] = (int) p.y;
53         }
54         put(0, 0, buff); //TODO: check ret val!
55     }
56 
toArray()57     public Point[] toArray() {
58         int num = (int) total();
59         Point[] ap = new Point[num];
60         if(num == 0)
61             return ap;
62         int buff[] = new int[num * _channels];
63         get(0, 0, buff); //TODO: check ret val!
64         for(int i=0; i<num; i++)
65             ap[i] = new Point(buff[i*_channels], buff[i*_channels+1]);
66         return ap;
67     }
68 
fromList(List<Point> lp)69     public void fromList(List<Point> lp) {
70         Point ap[] = lp.toArray(new Point[0]);
71         fromArray(ap);
72     }
73 
toList()74     public List<Point> toList() {
75         Point[] ap = toArray();
76         return Arrays.asList(ap);
77     }
78 }
79