Skip to content

Commit 6953eaf

Browse files
committed
Inline test added to Arrangement function.
1 parent 477ba1a commit 6953eaf

File tree

4 files changed

+527
-27
lines changed

4 files changed

+527
-27
lines changed

gcodetools-dev.inx

+2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ Press Apply to create new tool.
257257
<page name='arrangement' _gui-text='Arrangement'>
258258
<param name='arrangement-material-width' type="float" precision="5" min="0" max="1000000" _gui-text='Material width:'>500</param>
259259
<param name='arrangement-population-count' type="int" min="0" max="1000000" _gui-text='Genetic algorithm populations count:'>100</param>
260+
<param name="arrangement-inline-test" type="boolean" _gui-text="Use C-inline test (some additional
261+
packets will be needed)">false</param>
260262
</page>
261263
<!-- Gcodetools: /arrangement block -->
262264

gcodetools-dev.py

+86-27
Original file line numberDiff line numberDiff line change
@@ -2751,7 +2751,8 @@ def populate_species(self,count, parent_count):
27512751
self.incest_mutation_multiplyer = 2.
27522752
self.incest_mutation_count_multiplyer = 2.
27532753
else :
2754-
if random.random()<.01 : print_(self.species_distance2(parent1, parent2))
2754+
pass
2755+
# if random.random()<.01 : print_(self.species_distance2(parent1, parent2))
27552756
start_gene = random.randint(0,self.genes_count)
27562757
end_gene = (max(1,random.randint(0,self.genes_count),int(self.genes_count/4))+start_gene) % self.genes_count
27572758
if end_gene<start_gene :
@@ -2809,24 +2810,82 @@ def test(self,test_function):
28092810
self.population[i][0] = (b[3]-b[1])*(b[2]-b[0])
28102811
self.population.sort()
28112812

2812-
28132813
def test_spiece_centroid(self,spiece) :
28142814
poly = Polygon( self.polygons[spiece[0][0]].polygon[:])
2815-
poly.rotate(spiece[0][2]*math.pi2)
2815+
poly.rotate(spiece[0][1]*math.pi2)
28162816
surface = Polygon(poly.polygon)
2817-
i = 0
28182817
for p in spiece[1:] :
2819-
i += 1
28202818
poly = Polygon(self.polygons[p[0]].polygon[:])
2821-
poly.rotate(p[2]*math.pi2)
28222819
c = surface.centroid()
2820+
surface.move(-c[0],-c[1])
28232821
c1 = poly.centroid()
2824-
direction = [math.cos(p[1]*math.pi2), -math.sin(p[1]*math.pi2)]
2825-
poly.move(c[0]-c1[0]-direction[0]*100,c[1]-c1[1]-direction[1]*100)
2826-
poly.drop_into_direction(direction,surface)
2822+
poly.move(-c1[0],-c1[1])
2823+
poly.rotate(p[1]*math.pi2+p[2]*math.pi2)
2824+
surface.rotate(p[2]*math.pi2)
2825+
poly.drop_down(surface)
28272826
surface.add(poly)
2827+
surface.rotate(-p[2]*math.pi2)
28282828
return surface
28292829

2830+
2831+
def test_inline(self) :
2832+
###
2833+
### Fast test function using weave's from scipy inline function
2834+
###
2835+
try :
2836+
converters is None
2837+
except :
2838+
try:
2839+
from scipy import weave
2840+
from scipy.weave import converters
2841+
except:
2842+
options.self.error("For this function Scipy is needed. See http://www.cnc-club.ru/gcodetools for details.","error")
2843+
2844+
# Prepare vars
2845+
poly_, subpoly_, points_ = [], [], []
2846+
for poly in self.polygons :
2847+
p = poly.polygon
2848+
poly_ += [len(subpoly_), len(subpoly_)+len(p)*2]
2849+
for subpoly in p :
2850+
subpoly_ += [len(points_), len(points_)+len(subpoly)*2+2]
2851+
for point in subpoly :
2852+
points_ += point
2853+
points_ += subpoly[0] # Close subpolygon
2854+
2855+
test_ = []
2856+
population_ = []
2857+
for spiece in self.population:
2858+
test_.append( spiece[0] if spiece[0] != None else -1)
2859+
for sp in spiece[1]:
2860+
population_ += sp
2861+
2862+
lp_, ls_, l_, lt_ = len(poly_), len(subpoly_), len(points_), len(test_)
2863+
2864+
f = open('inline_test.c', 'r')
2865+
code = f.read()
2866+
f.close()
2867+
2868+
f = open('inline_test_functions.c', 'r')
2869+
functions = f.read()
2870+
f.close()
2871+
2872+
stdout_ = sys.stdout
2873+
s = ''
2874+
sys.stdout = s
2875+
2876+
test = weave.inline(
2877+
code,
2878+
['points_','subpoly_','poly_', 'lp_', 'ls_', 'l_', 'lt_','test_', 'population_'],
2879+
compiler='gcc',
2880+
support_code = functions,
2881+
)
2882+
if s!='' : options.self.error(s,"warning")
2883+
sys.stdout = stdout_
2884+
2885+
for i in range(len(test_)):
2886+
self.population[i][0] = test_[i]
2887+
2888+
28302889

28312890

28322891
#surface.draw()
@@ -2906,7 +2965,7 @@ def arrangement(self) :
29062965
time_ = time.time()
29072966
pop = copy.deepcopy(population)
29082967
population_count = self.options.arrangement_population_count
2909-
last_champ = []
2968+
last_champ = -1
29102969
champions_count = 0
29112970

29122971

@@ -2932,29 +2991,27 @@ def arrangement(self) :
29322991
population.order_mutation_factor = 1./(i%100-79) if 80<=i%100<100 else 1.
29332992
population.populate_species(250, 10)
29342993
"""
2935-
population.test(population.test_spiece_centroid)
2994+
if self.options.arrangement_inline_test :
2995+
population.test_inline()
2996+
else:
2997+
population.test(population.test_spiece_centroid)
2998+
29362999
print_("Test done at %s"%(time.time()-time_))
29373000
draw_new_champ = False
29383001
print_()
2939-
for x in population.population[:10]:
2940-
print_(x[0])
3002+
29413003

29423004
if population.population[0][0]!= last_champ :
29433005
draw_new_champ = True
3006+
improve = last_champ-population.population[0][0]
29443007
last_champ = population.population[0][0]*1
2945-
29463008

2947-
k = ""
2948-
#for j in range(10) :
2949-
# k += "%s " % population.population[j][0]
3009+
29503010
print_("Cicle %s done in %s"%(i,time.time()-time_))
29513011
time_ = time.time()
2952-
2953-
29543012
print_("%s incests been found"%population.inc)
29553013
print_()
2956-
#print_(k)
2957-
#print_()
3014+
29583015
if i == 0 or i == population_count-1 or draw_new_champ :
29593016
colors = ["blue"]
29603017

@@ -2963,7 +3020,7 @@ def arrangement(self) :
29633020
x,y = 400* (champions_count%10), 700*int(champions_count/10)
29643021
surface.move(x-b[0],y-b[1])
29653022
surface.draw(width=2, color=colors[0])
2966-
draw_text("Step = %s\nSquare = %f\nTime from start = %f"%(i,(b[2]-b[0])*(b[3]-b[1]),time.time()-start_time),x,y-40)
3023+
draw_text("Step = %s\nSquare = %f\nSquare improvement = %f\nTime from start = %f"%(i,(b[2]-b[0])*(b[3]-b[1]),improve,time.time()-start_time),x,y-50)
29673024
champions_count += 1
29683025
"""
29693026
spiece = population.population[0][1]
@@ -3074,16 +3131,18 @@ def __init__(self):
30743131

30753132
self.OptionParser.add_option("", "--arrangement-material-width", action="store", type="float", dest="arrangement_material_width", default=500, help="Materials width for arrangement")
30763133
self.OptionParser.add_option("", "--arrangement-population-count",action="store", type="int", dest="arrangement_population_count", default=100, help="Genetic algorithm populations count")
3134+
self.OptionParser.add_option("", "--arrangement-inline-test", action="store", type="inkbool", dest="arrangement_inline_test", default=False, help="Use C-inline test (some additional packets will be needed)")
3135+
30773136

30783137
self.OptionParser.add_option("", "--postprocessor", action="store", type="string", dest="postprocessor", default='', help="Postprocessor command.")
30793138
self.OptionParser.add_option("", "--postprocessor-custom", action="store", type="string", dest="postprocessor_custom", default='', help="Postprocessor custom command.")
30803139

3081-
self.OptionParser.add_option("", "--graffiti-max-seg-length", action="store", type="float", dest="graffiti_max_seg_length", default=1., help="Ggraffiti maximum segment length.")
3082-
self.OptionParser.add_option("", "--graffiti-min-radius", action="store", type="float", dest="graffiti_min_radius", default=10., help="Ggraffiti minimal connector's radius.")
3083-
self.OptionParser.add_option("", "--graffiti-start-pos", action="store", type="string", dest="graffiti_start_pos", default="(0;0)", help="Ggraffiti Start position (x;y).")
3140+
self.OptionParser.add_option("", "--graffiti-max-seg-length", action="store", type="float", dest="graffiti_max_seg_length", default=1., help="Graffiti maximum segment length.")
3141+
self.OptionParser.add_option("", "--graffiti-min-radius", action="store", type="float", dest="graffiti_min_radius", default=10., help="Graffiti minimal connector's radius.")
3142+
self.OptionParser.add_option("", "--graffiti-start-pos", action="store", type="string", dest="graffiti_start_pos", default="(0;0)", help="Graffiti Start position (x;y).")
30843143
self.OptionParser.add_option("", "--graffiti-create-linearization-preview", action="store", type="inkbool", dest="graffiti_create_linearization_preview", default=True, help="Ggraffiti create linearization preview.")
3085-
self.OptionParser.add_option("", "--graffiti-create-preview", action="store", type="inkbool", dest="graffiti_create_preview", default=True, help="Ggraffiti create preview.")
3086-
self.OptionParser.add_option("", "--graffiti-preview-size", action="store", type="int", dest="graffiti_preview_size", default=800, help="Ggraffiti preview's size.")
3144+
self.OptionParser.add_option("", "--graffiti-create-preview", action="store", type="inkbool", dest="graffiti_create_preview", default=True, help="Graffiti create preview.")
3145+
self.OptionParser.add_option("", "--graffiti-preview-size", action="store", type="int", dest="graffiti_preview_size", default=800, help="Graffiti preview's size.")
30873146
self.OptionParser.add_option("", "--graffiti-preview-emmit", action="store", type="int", dest="graffiti_preview_emmit", default=800, help="Preview's paint emmit (pts/s).")
30883147

30893148

inline_test.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#line 1 "inline.c"
2+
const int len = l_; //points array len
3+
const int lp = lp_; //poly array len
4+
const int ls = ls_; //subpoly array len
5+
const int lpop = lt_*lp_*3/2; //population array len
6+
const int lt = lt_; //test array len
7+
8+
9+
Polygon* polygons = new Polygon;
10+
Polygon* surface = new Polygon;
11+
polygons->points = new double[len];
12+
polygons->poly = new int[lp];
13+
polygons->subpoly = new int[ls];
14+
polygons->len = lp/2;
15+
16+
surface->points = new double[len];
17+
surface->subpoly = new int[ls];
18+
surface->poly = new int[lp];
19+
surface->len = 0;
20+
21+
22+
double *population = new double[lpop];
23+
double *test = new double[lt];
24+
25+
26+
int i,j,k;
27+
28+
// fill the arrays with values;
29+
for (i=0; i<len; i++){polygons->points[i] = points_[i];}
30+
31+
for (i=0; i<lp; i++){polygons->poly[i] = poly_[i];}
32+
for (i=0; i<ls; i++){polygons->subpoly[i] = subpoly_[i];}
33+
for (i=0; i<lpop; i++){population[i] = population_[i];}
34+
35+
for (i=0; i<lt; i++){test[i] = test_[i];}
36+
37+
//rotate_polygon(points, subpoly, poly, 1, 10);
38+
test_centroid(surface, polygons, population, test, lt);
39+
40+
for (i=0; i<lt; i++)
41+
{
42+
test_[i] = test[i];
43+
//printf("%d: %f\n",i,test[i]);
44+
}
45+
46+

0 commit comments

Comments
 (0)