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