src/misc/ringFinder.cc

Mon, 07 Jul 2014 02:13:31 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 07 Jul 2014 02:13:31 +0300
changeset 832
5c751af82f35
parent 813
987f35e96467
child 840
d077dd19bf9a
permissions
-rw-r--r--

- fixed: ring finder's solutions' scale values would drop to 0.0 if it would not choose to upscale radii

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 #include "ringFinder.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include "../miscallenous.h"
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.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 if (m_stack >= 5)
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 assert (r1 >= r0);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 double scale = r1 - r0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 double num = r0 / scale;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 // If the ring number is integral, we have found a fitting ring to r0 -> r1!
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 if (isInteger (num))
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 Component cmp;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 cmp.scale = scale;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 cmp.num = (int) round (num);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 currentSolution.addComponent (cmp);
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 // 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
48 // 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
49 if (m_stack == 0)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 m_solutions.push_back (currentSolution);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 return true;
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 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 // Try find solutions by splitting the ring in various positions.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 if (isZero (r1 - r0))
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 return false;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 double interval;
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 // Determine interval. The smaller delta between radii, the more precise
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 // 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
65 // calculating rings to 10 -> 105... that would take ages to process!
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 if (r1 - r0 < 0.5)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 interval = 0.1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 else if (r1 - r0 < 10)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 interval = 0.5;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 else if (r1 - r0 < 50)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 interval = 1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 interval = 5;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 // 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
76 for (double r = r0 + interval; r < r1; r += interval)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 Solution sol = currentSolution;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 m_stack++;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 bool res = findRingsRecursor (r0, r, sol) && findRingsRecursor (r, r1, sol);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 m_stack--;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 if (res)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 // 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
87 // 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
88 // to process and we can add the solution.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 // 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
91 // 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
92 // return true to continue processing.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 if (m_stack == 0)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 m_solutions.push_back (sol);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 currentSolution = sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 return true;
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
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 return false;
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
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 return true;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
109 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
110 // 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
111 // 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
112 // 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
113 // 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
114 // 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
115 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
116 // 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
117 // 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
118 // this algorithm, as it would fall into an infinite recursion.
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 bool RingFinder::findRings (double r0, double r1)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 m_solutions.clear();
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 Solution sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
125 // 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
126 // 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
127 // 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
128 // 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
129 // 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
130 // components.
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
131 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
132
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
133 if (not isZero (scale = r0 - floor (r0)) || not isZero (scale = r1 - floor (r1)))
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
134 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
135 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
136 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
137
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
138 if (qFuzzyCompare (floor (r0f), r0f) && qFuzzyCompare (floor (r1f), r1f))
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
139 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
140 r0 = r0f;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
141 r1 = r1f;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
142 }
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
143 }
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
144 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
145 {
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
146 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
147 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
148
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 // Recurse in and try find solutions.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 findRingsRecursor (r0, r1, sol);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
152 // 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
153 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
154 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
155 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
156 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
157 }
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
158
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 // 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
160 // overload to compare two solutions.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 m_bestSolution = null;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
163 for (Solution const& sol : m_solutions)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 if (m_bestSolution == null || sol.isSuperiorTo (m_bestSolution))
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 m_bestSolution = &sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 return (m_bestSolution != null);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
172 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
173 // 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
174 // one is superior.
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
175 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
176 // 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
177 // 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
178 // 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
179 // 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
180 // 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
181 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
182 // 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
183 // will be the one returned by RingFinder::bestSolution().
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 bool RingFinder::Solution::isSuperiorTo (const Solution* other) const
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 {
813
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
187 // If one solution has less components than the other one, it is definitely
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
188 // better.
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
189 if (getComponents().size() != other->getComponents().size())
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
190 return getComponents().size() < other->getComponents().size();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 // Calculate the maximum ring number. Since the solutions have equal
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 // ring counts, the solutions with lesser maximum rings should result
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 // in cleaner code and less new primitives, right?
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 int maxA = 0,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 maxB = 0;
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 for (int i = 0; i < getComponents().size(); ++i)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 maxA = max (getComponents()[i].num, maxA);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 maxB = max (other->getComponents()[i].num, maxB);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203
813
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
204 if (maxA != maxB)
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
205 return maxA < maxB;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 // Solutions have equal rings and equal maximum ring numbers. Let's
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 // 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
209 // one is chosen.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 return true;
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
211 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
212
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
213 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
214 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
215 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
216 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
217 }

mercurial