src/ringFinder.cpp

Sun, 06 Sep 2015 16:08:22 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 06 Sep 2015 16:08:22 +0300
changeset 992
5a10b4e7bf4a
parent 985
ed7b31b9f904
child 1014
f0a8ecb6a357
child 1217
314e12e23c3a
permissions
-rw-r--r--

Refactor PartDownloadRequest

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
968
4b93b7963456 Happy new year 2015
Teemu Piippo <crimsondusk64@gmail.com>
parents: 967
diff changeset
3 * Copyright (C) 2013 - 2015 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 #include "ringFinder.h"
842
e1c9010eb9e8 - moved ringFinder into root source directory, clearing the src/misc/ directory
Teemu Piippo <crimsondusk64@gmail.com>
parents: 840
diff changeset
20 #include "miscallenous.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 RingFinder g_RingFinder;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
24 RingFinder::RingFinder() {}
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
25
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 // =============================================================================
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 bool RingFinder::findRingsRecursor (double r0, double r1, Solution& currentSolution)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 // Don't recurse too deep.
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
31 if (m_stack >= 5 or r1 < r0)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 return false;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 // Find the scale and number of a ring between r1 and r0.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 double scale = r1 - r0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 double num = r0 / scale;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 // If the ring number is integral, we have found a fitting ring to r0 -> r1!
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
39 if (isInteger (num))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 Component cmp;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 cmp.scale = scale;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 cmp.num = (int) round (num);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 currentSolution.addComponent (cmp);
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 // If we're still at the first recursion, this is the only
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 // ring and there's nothing left to do. Guess we found the winner.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 if (m_stack == 0)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 m_solutions.push_back (currentSolution);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 return true;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 // Try find solutions by splitting the ring in various positions.
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
57 if (isZero (r1 - r0))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 return false;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 double interval;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 // Determine interval. The smaller delta between radii, the more precise
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 // interval should be used. We can't really use a 0.5 increment when
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 // calculating rings to 10 -> 105... that would take ages to process!
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 if (r1 - r0 < 0.5)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 interval = 0.1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 else if (r1 - r0 < 10)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 interval = 0.5;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 else if (r1 - r0 < 50)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 interval = 1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 interval = 5;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 // Now go through possible splits and try find rings for both segments.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 for (double r = r0 + interval; r < r1; r += interval)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 Solution sol = currentSolution;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 m_stack++;
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 832
diff changeset
80 bool res = findRingsRecursor (r0, r, sol) and findRingsRecursor (r, r1, sol);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 m_stack--;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 if (res)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 // We succeeded in finding radii for this segment. If the stack is 0, this
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 // is the first recursion to this function. Thus there are no more ring segments
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 // to process and we can add the solution.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 // If not, when this function ends, it will be called again with more arguments.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 // Accept the solution to this segment by setting currentSolution to sol, and
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 // return true to continue processing.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 if (m_stack == 0)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 m_solutions.push_back (sol);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 currentSolution = sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 return true;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 return false;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 return true;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
108 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
109 // This is the main algorithm of the ring finder. It tries to use math
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
110 // to find the one ring between r0 and r1. If it fails (the ring number
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
111 // is non-integral), it finds an intermediate radius (ceil of the ring
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
112 // number times scale) and splits the radius at this point, calling this
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
113 // function again to try find the rings between r0 - r and r - r1.
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
114 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
115 // This does not always yield into usable results. If at some point r ==
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
116 // r0 or r == r1, there is no hope of finding the rings, at least with
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
117 // this algorithm, as it would fall into an infinite recursion.
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 bool RingFinder::findRings (double r0, double r1)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 m_solutions.clear();
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 Solution sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
124 // If we're dealing with fractional radii, try upscale them into integral
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
125 // ones. This should yield in more reliable and more optimized results.
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
126 // For instance, using r0=1.5, r1=3.5 causes the algorithm to fail but
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
127 // r0=3, r1=7 (scaled up by 2) yields a 2-component solution. We can then
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
128 // downscale the radii back by dividing the scale fields of the solution
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
129 // components.
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
130 double scale = 1.0;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
131
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
132 if (not isZero (scale = r0 - floor (r0)) or not isZero (scale = r1 - floor (r1)))
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
133 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
134 double r0f = r0 / scale;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
135 double r1f = r1 / scale;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
136
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
137 if (isInteger (r0f) and isInteger (r1f))
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
138 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
139 r0 = r0f;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
140 r1 = r1f;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
141 }
868
6e03c68c81ba - ring finder now also tries scaling by 10 (3.4, 6.4 -> 34, 64), fixed floating point math argghhhhhhhh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 861
diff changeset
142 // If the numbers are both at most one-decimal fractions, we can use a scale of 10
967
eb586d3e1a6a elif -> else if
Teemu Piippo <crimsondusk64@gmail.com>
parents: 966
diff changeset
143 else if (isInteger (r0 * 10) and isInteger (r1 * 10))
868
6e03c68c81ba - ring finder now also tries scaling by 10 (3.4, 6.4 -> 34, 64), fixed floating point math argghhhhhhhh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 861
diff changeset
144 {
6e03c68c81ba - ring finder now also tries scaling by 10 (3.4, 6.4 -> 34, 64), fixed floating point math argghhhhhhhh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 861
diff changeset
145 scale = 0.1;
6e03c68c81ba - ring finder now also tries scaling by 10 (3.4, 6.4 -> 34, 64), fixed floating point math argghhhhhhhh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 861
diff changeset
146 r0 *= 10;
6e03c68c81ba - ring finder now also tries scaling by 10 (3.4, 6.4 -> 34, 64), fixed floating point math argghhhhhhhh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 861
diff changeset
147 r1 *= 10;
6e03c68c81ba - ring finder now also tries scaling by 10 (3.4, 6.4 -> 34, 64), fixed floating point math argghhhhhhhh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 861
diff changeset
148 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
149 }
832
5c751af82f35 - fixed: ring finder's solutions' scale values would drop to 0.0 if it would not choose to upscale radii
Santeri Piippo <crimsondusk64@gmail.com>
parents: 813
diff changeset
150 else
5c751af82f35 - fixed: ring finder's solutions' scale values would drop to 0.0 if it would not choose to upscale radii
Santeri Piippo <crimsondusk64@gmail.com>
parents: 813
diff changeset
151 {
5c751af82f35 - fixed: ring finder's solutions' scale values would drop to 0.0 if it would not choose to upscale radii
Santeri Piippo <crimsondusk64@gmail.com>
parents: 813
diff changeset
152 scale = 1.0;
5c751af82f35 - fixed: ring finder's solutions' scale values would drop to 0.0 if it would not choose to upscale radii
Santeri Piippo <crimsondusk64@gmail.com>
parents: 813
diff changeset
153 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
154
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 // Recurse in and try find solutions.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 findRingsRecursor (r0, r1, sol);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
158 // If we had upscaled our radii, downscale back now.
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
159 if (scale != 1.0)
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
160 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
161 for (Solution& sol : m_solutions)
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
162 sol.scaleComponents (scale);
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
163 }
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
164
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 // Compare the solutions and find the best one. The solution class has an operator>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 // overload to compare two solutions.
984
a7b6f987d269 null -> nullptr
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
167 m_bestSolution = nullptr;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
169 for (Solution const& sol : m_solutions)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 {
984
a7b6f987d269 null -> nullptr
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
171 if (m_bestSolution == nullptr or sol.isSuperiorTo (m_bestSolution))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 m_bestSolution = &sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174
985
ed7b31b9f904 Remove "!= nullptr" expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 984
diff changeset
175 return (m_bestSolution);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
178 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
179 // Compares this solution with @other and determines which
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
180 // one is superior.
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
181 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
182 // A solution is considered superior if solution has less
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
183 // components than the other one. If both solution have an
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
184 // equal amount components, the solution with a lesser maximum
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
185 // ring number is found superior, as such solutions should
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
186 // yield less new primitives and cleaner definitions.
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
187 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
188 // The solution which is found superior to every other solution
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
189 // will be the one returned by RingFinder::bestSolution().
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 bool RingFinder::Solution::isSuperiorTo (const Solution* other) const
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 {
813
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
193 // If one solution has less components than the other one, it is definitely
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
194 // better.
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
195 if (getComponents().size() != other->getComponents().size())
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
196 return getComponents().size() < other->getComponents().size();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 // Calculate the maximum ring number. Since the solutions have equal
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 // ring counts, the solutions with lesser maximum rings should result
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 // in cleaner code and less new primitives, right?
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 int maxA = 0,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 maxB = 0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 for (int i = 0; i < getComponents().size(); ++i)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 {
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
206 maxA = qMax (getComponents()[i].num, maxA);
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
207 maxB = qMax (other->getComponents()[i].num, maxB);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209
813
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
210 if (maxA != maxB)
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
211 return maxA < maxB;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 // Solutions have equal rings and equal maximum ring numbers. Let's
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 // just say this one is better, at this point it does not matter which
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 // one is chosen.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 return true;
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
217 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
218
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
219 void RingFinder::Solution::scaleComponents (double scale)
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
220 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
221 for (Component& cmp : m_components)
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
222 cmp.scale *= scale;
832
5c751af82f35 - fixed: ring finder's solutions' scale values would drop to 0.0 if it would not choose to upscale radii
Santeri Piippo <crimsondusk64@gmail.com>
parents: 813
diff changeset
223 }

mercurial