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