| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #ifndef H_EXPLORAGRAM_OPTIMAL_TRANSPORT_LINEAR_LEAST_SQUARES_H | ||
| 41 | #define H_EXPLORAGRAM_OPTIMAL_TRANSPORT_LINEAR_LEAST_SQUARES_H | ||
| 42 | |||
| 43 | #include <exploragram/basic/common.h> | ||
| 44 | #include <geogram/basic/matrix.h> | ||
| 45 | |||
| 46 | /** | ||
| 47 | * \file exploragram/optimal_transport/linear_least_squares.h | ||
| 48 | * \brief Functions to compute linear regression with low-degree | ||
| 49 | * polynomials, used to upscale functions sampled on pointsets | ||
| 50 | * in Merigot's multilevel algorithm for optimal transport. | ||
| 51 | */ | ||
| 52 | |||
| 53 | namespace GEO { | ||
| 54 | |||
| 55 | /** | ||
| 56 | * \brief Computes the linear least squares | ||
| 57 | * regression of a function evaluated | ||
| 58 | * in 3d. | ||
| 59 | * | ||
| 60 | * \TODO: have a linear solve function that does | ||
| 61 | * not require a template argument... | ||
| 62 | */ | ||
| 63 | class EXPLORAGRAM_API LinearLeastSquares { | ||
| 64 | public: | ||
| 65 | /** | ||
| 66 | * \brief Constructs a new LinearLeastSquares | ||
| 67 | * \param[in] degree one of 1 (linear), 2 (quadratic) | ||
| 68 | */ | ||
| 69 | LinearLeastSquares(index_t degree); | ||
| 70 | |||
| 71 | /** | ||
| 72 | * \brief Starts a new computation. | ||
| 73 | */ | ||
| 74 | void begin(); | ||
| 75 | |||
| 76 | /** | ||
| 77 | * \brief Ends the current computation. | ||
| 78 | * \details Computes the current equation | ||
| 79 | * from the set of samples declared with | ||
| 80 | * add_point(). | ||
| 81 | */ | ||
| 82 | void end(); | ||
| 83 | |||
| 84 | /** | ||
| 85 | * \brief Adds a sample to the current computation. | ||
| 86 | * \details This function needs to be called between | ||
| 87 | * a begin() / end() pair. | ||
| 88 | * \param[in] p 3d coordinates of the point | ||
| 89 | * \param[in] v function value associated with \p p_in | ||
| 90 | */ | ||
| 91 | void add_point(const double* p, double v); | ||
| 92 | |||
| 93 | /** | ||
| 94 | * \brief Evaluates the least-squares linear estimate | ||
| 95 | * at a given point. | ||
| 96 | * \details This function beeds to be called after end(). | ||
| 97 | * \param[in] p 3d coordinates of the point | ||
| 98 | * \return the linear estimate at \p p | ||
| 99 | */ | ||
| 100 | double eval(const double* p) const; | ||
| 101 | |||
| 102 | protected: | ||
| 103 | |||
| 104 | /** | ||
| 105 | * \brief Implementation of add_point() for degree 1. | ||
| 106 | * \param[in] p 3d coordinates of the point | ||
| 107 | * \param[in] v function value associated with \p p_in | ||
| 108 | */ | ||
| 109 | void add_point_degree_1(const double* p, double v); | ||
| 110 | |||
| 111 | /** | ||
| 112 | * \brief Implementation of add_point() for degree 2. | ||
| 113 | * \param[in] p 3d coordinates of the point | ||
| 114 | * \param[in] v function value associated with \p p_in | ||
| 115 | */ | ||
| 116 | void add_point_degree_2(const double* p, double v); | ||
| 117 | |||
| 118 | /** | ||
| 119 | * \brief Gets the dimension of the function basis. | ||
| 120 | */ | ||
| 121 | ✗ | index_t dim() const { | |
| 122 | ✗ | return dim_; | |
| 123 | } | ||
| 124 | |||
| 125 | /** | ||
| 126 | * \brief Evaluates the function basis at a given | ||
| 127 | * point. | ||
| 128 | * \param[in] p 3d coordinates of the point | ||
| 129 | * \param[out] b array of size dim(), value of the | ||
| 130 | * function basis at \p p | ||
| 131 | */ | ||
| 132 | void eval_basis(const double* p, double* b) const; | ||
| 133 | |||
| 134 | /** | ||
| 135 | * \brief Maximum dimension of the function basis | ||
| 136 | */ | ||
| 137 | static const int MAX_DIM = 10; | ||
| 138 | |||
| 139 | private: | ||
| 140 | index_t degree_; | ||
| 141 | index_t dim_; | ||
| 142 | Matrix<4,double> AtA_4_; | ||
| 143 | Matrix<10,double> AtA_10_; | ||
| 144 | double Atb_[MAX_DIM]; | ||
| 145 | double eqn_[MAX_DIM]; | ||
| 146 | }; | ||
| 147 | } | ||
| 148 | |||
| 149 | #endif | ||
| 150 |