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.pipe;
019
020/**
021 * Defines a pipe. A pipe is a single step in a pipeline. This class is to be extended, never used
022 * on its own.
023 *
024 * @param <I> Input type for the pipe
025 * @param <O> Output type for the pipe
026 * @param <P> Parameters type for the pipe
027 */
028public abstract class CVPipe<I, O, P> {
029    protected CVPipeResult<O> result = new CVPipeResult<>();
030    protected P params;
031
032    public void setParams(P params) {
033        this.params = params;
034    }
035
036    public P getParams() {
037        return this.params;
038    }
039
040    /**
041     * Runs the process for the pipe.
042     *
043     * @param in Input for pipe processing.
044     * @return Result of processing.
045     */
046    protected abstract O process(I in);
047
048    /**
049     * @param in Input for pipe processing.
050     * @return Result of processing.
051     */
052    public CVPipeResult<O> run(I in) {
053        long pipeStartNanos = System.nanoTime();
054        result.output = process(in);
055        result.nanosElapsed = System.nanoTime() - pipeStartNanos;
056        return result;
057    }
058
059    public static class CVPipeResult<O> {
060        public O output;
061        public long nanosElapsed;
062    }
063}