This repository has been archived by the owner on May 21, 2019. It is now read-only.
/
main_sources_ani.py
executable file
·174 lines (116 loc) · 2.77 KB
/
main_sources_ani.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from numpy import pi
from numpy import sqrt
from numpy import zeros
from numpy import cos
from numpy import sin
from numpy.random import random
NMAX = 10e7
SIZE = 512
ONE = 1./SIZE
RAD = 2*ONE
H = sqrt(3.)*RAD
NEARL = 2*RAD
FARL = RAD*10
MID = 0.5
LINEWIDTH = 1*ONE
STP = 1./SIZE
ATTRACT_STP = STP*0.1
REJECT_STP = STP*0.1
TRIANGLE_STP = STP*0.01
ALPHA = 0.05
DIMINISH = 0.98
MINIMUM_LENGTH = H*0.8
MAXIMUM_LENGTH = H*2
SPLIT_LIMIT = H*2
FLIP_LIMIT = NEARL*0.5
BACK = [1,1,1,1]
FRONT = [0,0,0,0.3]
NUM_SOURCES = 50
TWOPI = pi*2.
PROCS = 2
np_coord = zeros((NMAX,6), 'float')
i = 0
def show(render, dm, sources):
global np_coord
global i
render.clear_canvas()
num = dm.np_get_triangles_coordinates(np_coord)
render_triangle = render.triangle
for f,vv in enumerate(np_coord[:num,:]):
intens = dm.get_triangle_intensity(f)
intens = intens**0.5
rgb = [0.0]*4
rgb[0] = intens*0.5
rgb[1] = intens
rgb[2] = intens
rgb[3] = 1
render.set_front(rgb)
render_triangle(*vv,fill=True)
if not i%2:
render.write_to_png('ani_{:05d}.png'.format(i))
i += 1
def steps(dm):
from time import time
from modules.helpers import print_stats
from numpy import array
t1 = time()
dm.find_nearby_sources()
henum = dm.get_henum()
surface_edges = array([
dm.is_surface_edge(i)>0 and r<dm.get_edge_intensity(i)
for i,r in enumerate(random(size=henum))
],
'bool'
).nonzero()[0]
rnd = random(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(2.0*H, NEARL*0.5)
dm.optimize_edges(SPLIT_LIMIT, FLIP_LIMIT)
if dm.safe_vertex_positions(3*H)<0:
print('vertices reached the boundary. stopping.')
return False
t2 = time()
print_stats(0, t2-t1, dm)
return True
def main():
from differentialMesh import DifferentialMesh
from iutils.render import Animate
from modules.helpers import darts
DM = DifferentialMesh(NMAX, 2*FARL, NEARL, FARL, PROCS)
DM.new_faces_in_ngon(MID,MID, H, 3, 0)
DM.set_edge_intensity(1, 1)
sources = [(x,y) for x,y in darts(NUM_SOURCES, MID, MID, 0.40, 3*NEARL)]
DM.initialize_sources(sources, NEARL)
def wrap(render):
res = steps(DM)
show(render, DM, sources)
return res
render = Animate(SIZE, BACK, FRONT, wrap)
render.set_line_width(LINEWIDTH)
render.start()
if __name__ == '__main__' :
main()