001/******************************************************************************* 002 * This software is provided as a supplement to the authors' textbooks on digital 003 * image processing published by Springer-Verlag in various languages and editions. 004 * Permission to use and distribute this software is granted under the BSD 2-Clause 005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit https://imagingbook.com for additional details. 008 ******************************************************************************/ 009package imagingbook.common.geometry.fitting.line; 010 011import imagingbook.common.geometry.basic.Pnt2d; 012import imagingbook.common.geometry.line.AlgebraicLine; 013 014/** 015 * Interface to be implemented by all 2D line fits. 016 * 017 * @author WB 018 * @version 2022/09/22 019 */ 020public interface LineFit { 021 022 /** 023 * Returns the size of the point set used for calculating this line fit. 024 * @return the number of sample points used for fitting 025 */ 026 public abstract int getSize(); 027 028 /** 029 * Returns the parameters [A, B, C] for the {@link AlgebraicLine} associated with this line fit. To be implemented 030 * by concrete classes. {@code null} is returned if no fit was found. 031 * 032 * @return algebraic line parameters [A, B, C] 033 * @see AlgebraicLine 034 */ 035 public abstract double[] getLineParameters(); 036 037 /** 038 * Returns the {@link AlgebraicLine} associated with this line fit. {@code null} is returned if no fit was found. 039 * 040 * @return an {@link AlgebraicLine} instance 041 */ 042 public default AlgebraicLine getLine() { 043 double[] p = this.getLineParameters(); 044 if (p == null) { 045 return null; 046 } 047 else { 048 return new AlgebraicLine(p); 049 } 050 } 051 052 /** 053 * Calculates and returns the sum of the squared orthogonal distances of the specified points for this line fit. 054 * 055 * @param points an array of 2D points 056 * @return the squared orthogonal error 057 */ 058 public default double getSquaredOrthogonalError(Pnt2d[] points) { 059 AlgebraicLine line = getLine(); 060 return line.getSquareError(points); 061 } 062}