src/misc/ringFinder.h

changeset 842
e1c9010eb9e8
parent 841
1243abd47381
child 843
9ac9060877cc
equal deleted inserted replaced
841:1243abd47381 842:e1c9010eb9e8
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #pragma once
20 #include "../main.h"
21
22 //
23 // Implements a ring finding algorithm. It is passed two radii and it tries to
24 // find solutions of rings that would fill the given space.
25 //
26 // Note: it is not fool-proof and does not always yield a solution.
27 //
28 class RingFinder
29 {
30 public:
31 // A single component in a solution
32 struct Component
33 {
34 int num;
35 double scale;
36 };
37
38 // A solution whose components fill the desired ring space.
39 class Solution
40 {
41 public:
42 inline void addComponent (const Component& a);
43 inline const QVector<Component>& getComponents() const;
44 void scaleComponents (double scale);
45 bool isSuperiorTo (const Solution* other) const;
46
47 private:
48 QVector<Component> m_components;
49 };
50
51 RingFinder();
52
53 inline const QVector<Solution>& allSolutions() const;
54 inline const Solution* bestSolution() const;
55 bool findRings (double r0, double r1);
56
57 private:
58 QVector<Solution> m_solutions;
59 const Solution* m_bestSolution;
60 int m_stack;
61
62 bool findRingsRecursor (double r0, double r1, Solution& currentSolution);
63 };
64
65 //
66 // Gets the components of a solution
67 //
68 inline const QVector<RingFinder::Component>& RingFinder::Solution::getComponents() const
69 {
70 return m_components;
71 }
72
73 //
74 // Adds a component to a solution
75 //
76 inline void RingFinder::Solution::addComponent (const Component& a)
77 {
78 m_components.push_back (a);
79 }
80
81 //
82 // Returns the solution that was considered best. Returns null
83 // if there isn't any suitable solution.
84 //
85 inline const RingFinder::Solution* RingFinder::bestSolution() const
86 {
87 return m_bestSolution;
88 }
89
90 //
91 // Returns all found solutions.
92 //
93 inline const QVector<RingFinder::Solution>& RingFinder::allSolutions() const
94 {
95 return m_solutions;
96 }
97
98 extern RingFinder g_RingFinder;

mercurial