RansacFitterOutputs.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.hipparchus.fitting.ransac;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Class containing the best estimate of the model parameters.
 * @param <M> mathematical model representing the parameters to estimate
 * @since 4.1
 */
public class RansacFitterOutputs<M> {

    /** Mathematical model fitter used by RANSAC algorithm. */
    private final IModelFitter<M> fitter;

    /** Best model parameters. */
    private final Optional<M> bestModel;

    /** List of points used to determine the best model parameters. */
    private final List<Fittable> bestInliers;

    /**
     * Constructor.
     * @param bestModel best model parameters
     * @param bestInliers list of points used to determine the best model parameters
     * @param fitter mathematical model fitter used by RANSAC algorithm
     */
    public RansacFitterOutputs(final Optional<M> bestModel, final List<Fittable> bestInliers, final IModelFitter<M> fitter) {
        this.bestModel = bestModel;
        this.bestInliers = new ArrayList<>(bestInliers);
        this.fitter = fitter;
    }

    /**
     * Get the best model parameters.
     * @return the best model parameters
     */
    public Optional<M> getBestModel() {
        return bestModel;
    }

    /**
     * Get the list of points used to determine the best model parameters.
     * @return the list of points used to determine the best model parameters
     */
    public List<Fittable> getBestInliers() {
        return new ArrayList<>(bestInliers);
    }

    /**
     * Finds the points below a given threshold based on the computed best model parameters.
     * @param points input list of points
     * @param threshold threshold to use
     * @return the list of points below the given threshold based on the computed best model parameters
     *         (can be empty if the all points are above the threshold or if no best model has been found)
     */
    public List<Fittable> filterPointsBelowThreshold(final List<Fittable> points, final double threshold) {
        return bestModel.map(model -> points.stream().filter(point -> fitter.computeModelError(model, point) < threshold).collect(Collectors.toList()))
                        .orElse(Collections.emptyList());
    }
}