src/ringFinder.cpp

Tue, 03 Apr 2018 17:37:33 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 03 Apr 2018 17:37:33 +0300
changeset 1375
689121d162b6
parent 1326
69a90bd2dba2
permissions
-rw-r--r--

refactor

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
1326
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
3 * Copyright (C) 2013 - 2018 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"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 RingFinder g_RingFinder;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
23 RingFinder::RingFinder() {}
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
24
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 // =============================================================================
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 bool RingFinder::findRingsRecursor (double r0, double r1, Solution& currentSolution)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 // 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
30 if (m_stack >= 5 or r1 < r0)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 return false;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 // 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
34 double scale = r1 - r0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 double num = r0 / scale;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 // 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
38 if (isInteger (num))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 Component cmp;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 cmp.scale = scale;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 cmp.num = (int) round (num);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 currentSolution.addComponent (cmp);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 // 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
46 // 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
47 if (m_stack == 0)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 m_solutions.push_back (currentSolution);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 return true;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 }
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 else
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 // 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
56 if (isZero (r1 - r0))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 return false;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 double interval;
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 // Determine interval. The smaller delta between radii, the more precise
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 // 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
63 // calculating rings to 10 -> 105... that would take ages to process!
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 if (r1 - r0 < 0.5)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 interval = 0.1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 else if (r1 - r0 < 10)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 interval = 0.5;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 else if (r1 - r0 < 50)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 interval = 1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 interval = 5;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 // 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
74 for (double r = r0 + interval; r < r1; r += interval)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 Solution sol = currentSolution;
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 m_stack++;
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 832
diff changeset
79 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
80 m_stack--;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 if (res)
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 // 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
85 // 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
86 // to process and we can add the solution.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 // 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
89 // 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
90 // return true to continue processing.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 if (m_stack == 0)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 m_solutions.push_back (sol);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 currentSolution = sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 return true;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 }
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 return false;
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
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 return true;
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
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
107 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
108 // 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
109 // 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
110 // 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
111 // 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
112 // 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
113 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
114 // 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
115 // 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
116 // this algorithm, as it would fall into an infinite recursion.
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 bool RingFinder::findRings (double r0, double r1)
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 m_solutions.clear();
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 Solution sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
123 // 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
124 // 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
125 // 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
126 // 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
127 // 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
128 // components.
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
129 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
130
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
131 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
132 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
133 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
134 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
135
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
136 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
137 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
138 r0 = r0f;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
139 r1 = r1f;
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
140 }
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
141 // 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
142 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
143 {
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 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
145 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
146 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
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 }
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
149 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
150 {
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 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
152 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
153
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 // Recurse in and try find solutions.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 findRingsRecursor (r0, r1, sol);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
157 // 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
158 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
159 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
160 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
161 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
162 }
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
163
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 // 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
165 // overload to compare two solutions.
984
a7b6f987d269 null -> nullptr
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
166 m_bestSolution = nullptr;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
168 for (Solution const& sol : m_solutions)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 {
984
a7b6f987d269 null -> nullptr
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
170 if (m_bestSolution == nullptr or sol.isSuperiorTo (m_bestSolution))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 m_bestSolution = &sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173
985
ed7b31b9f904 Remove "!= nullptr" expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 984
diff changeset
174 return (m_bestSolution);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
177 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
178 // 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
179 // one is superior.
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
180 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
181 // 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
182 // 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
183 // 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
184 // 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
185 // 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
186 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
187 // 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
188 // will be the one returned by RingFinder::bestSolution().
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 //
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 bool RingFinder::Solution::isSuperiorTo (const Solution* other) const
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 {
813
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
192 // If one solution has less components than the other one, it is definitely
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
193 // better.
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
194 if (countof(getComponents()) != countof(other->getComponents()))
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
195 return countof(getComponents()) < countof(other->getComponents());
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 // Calculate the maximum ring number. Since the solutions have equal
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 // ring counts, the solutions with lesser maximum rings should result
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 // in cleaner code and less new primitives, right?
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 int maxA = 0,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 maxB = 0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202
1063
1f15c52c11f6 Replaced uses of 'x.size()' with 'length(x)'
Teemu Piippo <teemu@hecknology.net>
parents: 1014
diff changeset
203 for (const Component& component : getComponents())
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 {
1063
1f15c52c11f6 Replaced uses of 'x.size()' with 'length(x)'
Teemu Piippo <teemu@hecknology.net>
parents: 1014
diff changeset
205 maxA = qMax (component.num, maxA);
1f15c52c11f6 Replaced uses of 'x.size()' with 'length(x)'
Teemu Piippo <teemu@hecknology.net>
parents: 1014
diff changeset
206 maxB = qMax (component.num, maxB);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208
813
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
209 if (maxA != maxB)
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
210 return maxA < maxB;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 // Solutions have equal rings and equal maximum ring numbers. Let's
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 // 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
214 // one is chosen.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 return true;
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
216 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
217
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
218 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
219 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
220 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
221 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
222 }

mercurial