File indexing completed on 2024-12-15 04:01:12

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include "bezier.hpp"
0010 
0011 namespace glaxnimate::math::bezier {
0012 
0013 class LengthData
0014 {
0015 public:
0016     struct SplitInfo
0017     {
0018         int index = 0;
0019         qreal ratio = 0;
0020         qreal length = 0;
0021         const LengthData* child = nullptr;
0022 
0023         SplitInfo descend() const
0024         {
0025             return child->at_ratio(ratio);
0026         }
0027     };
0028 
0029     explicit LengthData(const Solver& segment, int steps);
0030 
0031     explicit LengthData(const Bezier& bez, int steps);
0032 
0033     explicit LengthData(const MultiBezier& mbez, int steps);
0034 
0035 
0036     SplitInfo at_ratio(qreal ratio) const;
0037 
0038     SplitInfo at_length(qreal length) const;
0039 
0040     /**
0041      * \brief Returns the length such that
0042      *        `at_length(length).ratio == ratio`
0043      */
0044     qreal from_ratio(qreal ratio) const;
0045 
0046     qreal length() const noexcept;
0047 
0048     /**
0049      * \returns The length at which the child at \p index starts
0050      */
0051     qreal child_start(int index) const;
0052 
0053     /**
0054      * \returns The length at which the child at \p index ends
0055      */
0056     qreal child_end(int index) const;
0057 
0058 private:
0059     LengthData(qreal t, qreal length, qreal cumulative_length);
0060 
0061     qreal t_ = 0;
0062     qreal length_ = 0;
0063     qreal cumulative_length_ = 0;
0064     std::vector<LengthData> children_;
0065     bool leaf_ = false;
0066 
0067 };
0068 
0069 } // namespace glaxnimate::math::bezier