001package org.opencv.core;
002
003import java.util.Arrays;
004import java.util.List;
005
006public class MatOfPoint extends Mat {
007    // 32SC2
008    private static final int _depth = CvType.CV_32S;
009    private static final int _channels = 2;
010
011    public MatOfPoint() {
012        super();
013    }
014
015    protected MatOfPoint(long addr) {
016        super(addr);
017        if( !empty() && checkVector(_channels, _depth) < 0 )
018            throw new IllegalArgumentException("Incompatible Mat");
019        //FIXME: do we need release() here?
020    }
021
022    public static MatOfPoint fromNativeAddr(long addr) {
023        return new MatOfPoint(addr);
024    }
025
026    public MatOfPoint(Mat m) {
027        super(m, Range.all());
028        if( !empty() && checkVector(_channels, _depth) < 0 )
029            throw new IllegalArgumentException("Incompatible Mat");
030        //FIXME: do we need release() here?
031    }
032
033    public MatOfPoint(Point...a) {
034        super();
035        fromArray(a);
036    }
037
038    public void alloc(int elemNumber) {
039        if(elemNumber>0)
040            super.create(elemNumber, 1, CvType.makeType(_depth, _channels));
041    }
042
043    public void fromArray(Point...a) {
044        if(a==null || a.length==0)
045            return;
046        int num = a.length;
047        alloc(num);
048        int buff[] = new int[num * _channels];
049        for(int i=0; i<num; i++) {
050            Point p = a[i];
051            buff[_channels*i+0] = (int) p.x;
052            buff[_channels*i+1] = (int) p.y;
053        }
054        put(0, 0, buff); //TODO: check ret val!
055    }
056
057    public Point[] toArray() {
058        int num = (int) total();
059        Point[] ap = new Point[num];
060        if(num == 0)
061            return ap;
062        int buff[] = new int[num * _channels];
063        get(0, 0, buff); //TODO: check ret val!
064        for(int i=0; i<num; i++)
065            ap[i] = new Point(buff[i*_channels], buff[i*_channels+1]);
066        return ap;
067    }
068
069    public void fromList(List<Point> lp) {
070        Point ap[] = lp.toArray(new Point[0]);
071        fromArray(ap);
072    }
073
074    public List<Point> toList() {
075        Point[] ap = toArray();
076        return Arrays.asList(ap);
077    }
078}