Wed, 16 Jul 2014 15:00:41 +0300
- changed copyright lines to use my legal name instead of my nickname
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 |
844
11587d419d2f
- changed copyright lines to use my legal name instead of my nickname
Teemu Piippo <crimsondusk64@gmail.com>
parents:
842
diff
changeset
|
3 | * Copyright (C) 2013, 2014 Teemu Piippo |
655
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 |
842
e1c9010eb9e8
- moved ringFinder into root source directory, clearing the src/misc/ directory
Teemu Piippo <crimsondusk64@gmail.com>
parents:
812
diff
changeset
|
20 | #include "main.h" |
655
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; |
812
b7e0d08becac
- ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents:
811
diff
changeset
|
44 | void scaleComponents (double scale); |
811
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
45 | bool isSuperiorTo (const Solution* other) const; |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | private: |
811
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
48 | QVector<Component> m_components; |
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 | |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
51 | RingFinder(); |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | |
811
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
53 | inline const QVector<Solution>& allSolutions() const; |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
54 | inline const Solution* bestSolution() const; |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
55 | bool findRings (double r0, double r1); |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
56 | |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
57 | private: |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
58 | QVector<Solution> m_solutions; |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
59 | const Solution* m_bestSolution; |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
60 | int m_stack; |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
61 | |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
62 | bool findRingsRecursor (double r0, double r1, Solution& currentSolution); |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | }; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | |
811
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
65 | // |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
66 | // Gets the components of a solution |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
67 | // |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
68 | 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
|
69 | { |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
70 | return m_components; |
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 | // |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
74 | // Adds a component to a solution |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
75 | // |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
76 | 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
|
77 | { |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
78 | m_components.push_back (a); |
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 | // |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
82 | // 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
|
83 | // 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
|
84 | // |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
85 | 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
|
86 | { |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
87 | return m_bestSolution; |
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 | // |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
91 | // Returns all found solutions. |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
92 | // |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
93 | 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
|
94 | { |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
95 | return m_solutions; |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
96 | } |
27ccc8eca322
- refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
97 | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | extern RingFinder g_RingFinder; |