001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.vision.target;
019
020import java.util.ArrayList;
021import java.util.List;
022import org.opencv.core.RotatedRect;
023import org.photonvision.vision.opencv.CVShape;
024import org.photonvision.vision.opencv.Contour;
025import org.photonvision.vision.opencv.ContourShape;
026import org.photonvision.vision.opencv.Releasable;
027import org.photonvision.vision.pipe.impl.NeuralNetworkPipeResult;
028
029public class PotentialTarget implements Releasable {
030    public final Contour m_mainContour;
031    public final List<Contour> m_subContours;
032    public final CVShape shape;
033
034    // additional metadata about object detections we need to keep around
035    public final double confidence;
036    public final int clsId;
037
038    public PotentialTarget(Contour inputContour) {
039        this(inputContour, List.of());
040    }
041
042    public PotentialTarget(Contour inputContour, List<Contour> subContours) {
043        this(inputContour, subContours, null);
044    }
045
046    public PotentialTarget(Contour inputContour, List<Contour> subContours, CVShape shape) {
047        m_mainContour = inputContour;
048        m_subContours = new ArrayList<>(subContours);
049        this.shape = shape;
050        this.clsId = -1;
051        this.confidence = -1;
052    }
053
054    public PotentialTarget(Contour inputContour, CVShape shape) {
055        this(inputContour, List.of(), shape);
056    }
057
058    public PotentialTarget(NeuralNetworkPipeResult det) {
059        this.shape = new CVShape(new Contour(det.bbox), ContourShape.Quadrilateral);
060        this.m_mainContour = this.shape.getContour();
061        m_subContours = List.of();
062        this.clsId = det.classIdx;
063        this.confidence = det.confidence;
064    }
065
066    public PotentialTarget(CVShape cvShape) {
067        this(cvShape.getContour(), cvShape);
068    }
069
070    public RotatedRect getMinAreaRect() {
071        return m_mainContour.getMinAreaRect();
072    }
073
074    public double getArea() {
075        return m_mainContour.getArea();
076    }
077
078    @Override
079    public void release() {
080        m_mainContour.release();
081        for (var sc : m_subContours) {
082            sc.release();
083        }
084        if (!m_subContours.isEmpty()) m_subContours.clear();
085        if (shape != null) shape.release();
086    }
087}