forked from inconvergent/differential-mesh
/
main_sources.py
executable file
·225 lines (152 loc) · 4.27 KB
/
main_sources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
from numpy import pi
from numpy import sqrt
from numpy import zeros
from numpy import cos
from numpy import sin
from numpy import power
from numpy import floor
from numpy.random import random
TWOPI = pi*2.
MID = 0.5
NMAX = 10e7
SIZE = 10000
ONE = 1./SIZE*0.5
RAD = 5*ONE
H = sqrt(3.)*RAD
NEARL = 2*RAD
FARL = RAD*13
STEPS_ITT = 100
STP = 1./SIZE
ATTRACT_STP = STP*0.1
REJECT_STP = STP
TRIANGLE_STP = STP*0.01
ALPHA = 0
DIMINISH = 0.99
MINIMUM_LENGTH = H*0.8
MAXIMUM_LENGTH = H*2
SPLIT_LIMIT = H*2
FLIP_LIMIT = NEARL*0.5
BACK = [1, 1.0, 1.0, 1]
FRONT = [0,0,0,0.3]
RED = [1,0,0,0.3]
BLUE = [0,0,1,0.3]
GREEN = [0,1,0,0.3]
CYAN = [0,0.5,0.5,0.5]
NUM_SOURCES = 2500*4
PROCS = 6
GRAINS = 80
np_coord = zeros((NMAX,6), 'float')
np_edges_coord = zeros((NMAX,4), 'float')
np_gen = zeros(NMAX, 'int')
def show_circles(render, dm, sources):
global np_coord
global np_gen
render.clear_canvas()
#num = dm.np_get_edges_coordinates(np_edges_coord)
#render_random_circle = render.random_circle
render_random_triangle = render.random_triangle
#render_random_uniform_circle = render.random_uniform_circle
#render_line = render.line
render.set_front(FRONT)
render.set_line_width(render.pix*2)
#rotation = 10
num = dm.np_get_triangles_coordinates(np_coord)
render_triangle = render.triangle
for f,vv in enumerate(np_coord[:num,:]):
render.set_front([0.,0.,0.,0.1])
render_random_triangle(*vv,grains=200)
#for e,vv in enumerate(np_edges_coord[:num,:]):
##g = (np_gen[e]%rotation)/float(rotation)
##rgba = [g]*4
##rgba[3] = 0.4
##if g<=0.:
##rgba = RED
##render.set_front(rgba)
##render_line(*vv)
##else:
##rgba = FRONT
##render.set_front(rgba)
#render_random_uniform_circle(vv[0], vv[1], dd[e], grains=30, dst=0)
render.write_to_png('res/ello_circ_i_{:05d}.png'.format(render.num_img))
def show_triangles(render, dm, sources):
global np_coord
render.clear_canvas()
#render_circle = render.circle
#source_rad = ONE*10
#for x,y in sources:
#render.set_front(CYAN)
#render_circle(x, y, source_rad, fill=True)
num = dm.np_get_triangles_coordinates(np_coord)
render_random_triangle = render.random_triangle
#render_triangle = render.triangle
for f,vv in enumerate(np_coord[:num,:]):
intens = dm.get_triangle_intensity(f)
rgb = [0.05]*4
rgb[1] += intens
rgb[2] += intens
rgb[3] = 0.4
render.set_front(rgb)
render_random_triangle(*vv,grains=GRAINS)
render.write_to_png('res/expand_c_{:05d}.png'.format(i))
def main():
from time import time
from render.render import Render
from differentialMesh import DifferentialMesh
from modules.helpers import darts
from modules.helpers import print_stats
from numpy import array
DM = DifferentialMesh(NMAX, 2*FARL, NEARL, FARL, PROCS)
DM.new_faces_in_ngon(MID,MID, H, 7, 0)
DM.set_edge_intensity(1, 1)
sources = [(x,y) for x,y in darts(NUM_SOURCES, MID, MID, 0.43, 0.002)]
DM.initialize_sources(sources, NEARL)
render = Render(SIZE, BACK, FRONT)
for i in xrange(1000000):
t1 = time()
for _ in xrange(STEPS_ITT):
DM.find_nearby_sources()
henum = DM.get_henum()
surface_edges = array(
[DM.is_surface_edge(e)>0 and r<DM.get_edge_intensity(e)
for e,r in enumerate(random(size=henum))],
'bool').nonzero()[0]
rnd = random(size=len(surface_edges)*2)
the = (1.-2*rnd[::2])*pi
rad = rnd[1::2]*0.5*H
dx = cos(the)*rad
dy = sin(the)*rad
DM.new_triangles_from_surface_edges(
surface_edges,
len(surface_edges),
H,
dx,
dy,
MINIMUM_LENGTH,
MAXIMUM_LENGTH,
1
)
DM.optimize_position(
ATTRACT_STP,
REJECT_STP,
TRIANGLE_STP,
ALPHA,
DIMINISH,
-1
)
henum = DM.get_henum()
DM.optimize_edges(
SPLIT_LIMIT,
FLIP_LIMIT
)
if DM.safe_vertex_positions(3*H)<0:
show_circles(render, DM, sources)
print('vertices reached the boundary. stopping.')
return
show_circles(render, DM, sources)
t2 = time()
print_stats(i*STEPS_ITT, t2-t1, DM)
if __name__ == '__main__' :
main()