src/ringFinder.cpp

Wed, 04 Mar 2015 15:37:21 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Wed, 04 Mar 2015 15:37:21 +0200
branch
projects
changeset 936
aee883858c90
parent 931
85080f7a1c20
child 941
f895379d7fab
permissions
-rw-r--r--

- commit work done on projects

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
927
409b82a4765e - bump copyright year
Teemu Piippo <crimsondusk64@gmail.com>
parents: 868
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.
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!
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
40 if (IsIntegral (num))
655
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.
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
58 if (IsZero (r1 - r0))
655
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++;
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 832
diff changeset
81 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
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
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
133 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
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
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
138 if (IsIntegral (r0f) and IsIntegral (r1f))
812
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 }
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 // If the numbers are both at most one-decimal fractions, we can use a scale of 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
144 elif (IsIntegral (r0 * 10) and IsIntegral (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
145 {
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 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
147 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
148 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
149 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
150 }
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
151 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
152 {
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 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
154 }
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
155
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 // Recurse in and try find solutions.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 findRingsRecursor (r0, r1, sol);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
159 // 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
160 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
161 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
162 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
163 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
164 }
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
165
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 // 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
167 // overload to compare two solutions.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 m_bestSolution = null;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169
812
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
170 for (Solution const& sol : m_solutions)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 {
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 832
diff changeset
172 if (m_bestSolution == null or sol.isSuperiorTo (m_bestSolution))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 m_bestSolution = &sol;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 }
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 return (m_bestSolution != null);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
179 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
180 // 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
181 // one is superior.
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
182 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
183 // 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
184 // 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
185 // 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
186 // 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
187 // 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
188 //
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
189 // 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
190 // will be the one returned by RingFinder::bestSolution().
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 bool RingFinder::Solution::isSuperiorTo (const Solution* other) const
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 {
813
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
194 // If one solution has less components than the other one, it is definitely
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
195 // better.
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
196 if (getComponents().size() != other->getComponents().size())
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
197 return getComponents().size() < other->getComponents().size();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 // Calculate the maximum ring number. Since the solutions have equal
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 // ring counts, the solutions with lesser maximum rings should result
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 // in cleaner code and less new primitives, right?
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 int maxA = 0,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 maxB = 0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 for (int i = 0; i < getComponents().size(); ++i)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 {
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
207 maxA = Max (getComponents()[i].num, maxA);
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
208 maxB = Max (other->getComponents()[i].num, maxB);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210
813
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
211 if (maxA != maxB)
987f35e96467 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 812
diff changeset
212 return maxA < maxB;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 // Solutions have equal rings and equal maximum ring numbers. Let's
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 // 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
216 // one is chosen.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 return true;
811
27ccc8eca322 - refactored up the ringfinder, apply -DDEBUG with RelWithDebInfo
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
218 }
812
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 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
221 {
b7e0d08becac - ringfinder: attempt to upscale fractional radii to integral ones. this improves results
Santeri Piippo <crimsondusk64@gmail.com>
parents: 811
diff changeset
222 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
223 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
224 }

mercurial