-
Notifications
You must be signed in to change notification settings - Fork 0
/
crygrow.cpp
239 lines (208 loc) · 7.38 KB
/
crygrow.cpp
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright © 2019-2020 Artyom Tokarev. All rights reserved.
// Licensed under the MIT License.
#include <iostream>
#include <fstream>
#include <random>
#include "sptalgs.h"
#include "automata.h"
#include "geometry.h"
#include "progress-bar.h"
#define DIM3
#ifdef DIM3
constexpr std::size_t dim = 3;
#else
constexpr std::size_t dim = 2;
#endif
std::size_t seed = 0;
using automata_t = cgr::automata<dim>;
using cell_t = cgr::cell<dim>;
using grain_t = cgr::grain<dim>;
using material_t = cgr::material<dim>;
std::vector<cgr::upos_t<dim>> make_central_pos(std::size_t size) {
std::vector<cgr::upos_t<dim>> res;
res.push_back(cgr::upos_t<dim>::filled_with(size / 2));
return res;
}
std::uint64_t min_distance2(const cgr::upos_t<dim>& pos, const std::vector<cgr::upos_t<dim>>& others) {
std::uint64_t res = std::numeric_limits<std::uint64_t>::max();
for (auto& other : others) {
std::uint64_t dist = (other - pos).magnitude2();
if (dist < res)
res = dist;
}
return res;
}
std::vector<cgr::upos_t<dim>> make_random_poses(std::size_t size, std::size_t num, std::uint64_t min_dist2 = 0) {
std::vector<cgr::upos_t<dim>> res;
std::mt19937_64 gen(seed);
std::uniform_int_distribution<std::size_t> dis(0, size - 1);
for (std::size_t i = 0; i < num;) {
cgr::upos_t<dim> curpos;
for (auto& e : curpos.x)
e = dis(gen);
if (min_distance2(curpos, res) >= min_dist2) {
bool flag = false;
for (auto e : curpos.x)
if (!(static_cast<std::size_t>(e * e) >= min_dist2 / 4 &&
static_cast<std::size_t>(e) < size - static_cast<std::size_t>(std::sqrt(min_dist2 / 4)))) {
flag = true;
break;
}
if (flag)
continue;
res.push_back(curpos);
++i;
}
}
return res;
}
#include "range-iter.h"
int main_test() {
for (itr::range_iter<int> it(0, 7); !it.has_ended(); ++it)
std::cout << *it << ' ';
std::cout << std::endl;
for (itr::range_iter<int> it(0, 7, itr::dir::reverse); !it.has_ended(); ++it)
std::cout << *it << ' ';
return 0;
}
void write_image_pixels(std::ostream& os, const automata_t& atmt, bool blackwhite = false) {
for (std::size_t i = 0; i < atmt.dim_lens()[0] * atmt.dim_lens()[1]; ++i) {
auto curpos = atmt.upos(i);
#ifdef DIM3
//curpos[2] = static_cast<std::int64_t>(size) / 2;
//curpos[2] = size - 1;
curpos[2] = 0;
#endif
auto& cell = *atmt.cell(curpos);
std::array<std::uint8_t, 3> color;
if (!blackwhite) {
if (!cell.crysted &&
!cell.grains.empty()) {
color = { 0, 255, 0 };
} else if (!cell.crysted ||
cell.grains.empty()) {
color = { 255, 255, 255 };
} else if (cell.grains.size() == 1) {
color = { 0, 0, 0 };
} else if (cell.grains.size() == 2) {
color = { 0, 0, 255 };
} else {
color = { 255, 0, 0 };
}
} else {
if (cell.grains.size() == 1 &&
cell.crysted) {
color = { 0, 0, 0 };
} else {
color = { 255, 255, 255 };
}
}
os << curpos[0] << ' '
<< curpos[1] << ' '
<< static_cast<int>(color[0]) << ' '
<< static_cast<int>(color[1]) << ' '
<< static_cast<int>(color[2]) << std::endl;
}
}
void show_picture(const automata_t& atmt) {
std::ofstream ofile("automata-image-data.txt");
ofile << "size " << atmt.dim_lens()[0] << std::endl;
write_image_pixels(ofile, atmt, false);
ofile.close();
std::system("python ./visualize.py");
}
int inner_main() {
std::size_t size = 300;
std::size_t range = 5;
automata_t atmt(size);
atmt.set_range(range);
//auto init_poses = make_central_pos(size);
auto init_poses = make_random_poses(size, 30, std::pow(range * 15, 2));
//material_t mater;
material_t mater({
#ifdef DIM3
spt::vecd<dim>({ 1.0, 0.0, 0.0 }).normalize(),
spt::vecd<dim>({ 0.0, 1.0, 0.0 }).normalize(),
spt::vecd<dim>({ 0.0, 0.0, 1.0 }).normalize() });
#else
spt::vecd<dim>({ 4.0, 1.0 }).normalize(),
spt::vecd<dim>({ -1.0, 4.0 }).normalize() });
#endif
//std::vector<grain_t> grains(init_poses.size(), grain_t(&mater));
std::vector<grain_t> grains;
grains.reserve(init_poses.size());
std::mt19937_64 gen(seed);
std::uniform_real_distribution<double> dis(-1.0, std::nextafter(1.0, 2.0));
for (std::size_t i = 0; i < init_poses.size(); ++i) {
#ifdef DIM3
const double pi = 3.14159265359;
auto rot = spt::rotation(spt::vecd<dim>({ dis(gen), dis(gen), dis(gen) }).normalize(), std::abs(dis(gen)) * pi);
grains.emplace_back(&mater, rot);
#else
auto first = spt::vecd<dim>({ dis(gen), dis(gen) }).normalize();
spt::vecd<dim> second({ -first[1], first[0] });
grains.emplace_back(&mater, spt::matd<dim>{ first, second });
#endif
}
for (std::size_t i = 0; i < init_poses.size(); ++i)
atmt.spawn_grain(&grains[i], atmt.offset(init_poses[i]), cgr::nbh::nbhood_kind::crystallographic);
#define SHOWPIC
//progress_bar bar("crystallization", atmt.num_cells(), 70);
//while (!atmt.stop_condition()) {
// //for (std::size_t i = 0; i < 5; ++i) {
// while (!atmt.stop_condition()) {
// atmt.iterate();
// bar.set_count(atmt.num_crysted_cells());
// }
//
// #ifdef SHOWPIC
// show_picture(atmt);
// #endif // SHOWPIC
//}
//atmt.thin_boundary(4, 1);
//#ifdef SHOWPIC
//show_picture(atmt);
//#endif // SHOWPIC
atmt.voronoi<cgr::nbh::nbhood_kind::euclid>();
std::cout << "started smoothing" << std::endl;
atmt.smooth(1);
#ifdef SHOWPIC
show_picture(atmt);
#endif // SHOWPIC
#ifdef DIM3
std::cout << "diams inter4: " << std::endl;
for (auto d : atmt.diams_inter4())
std::cout << d << std::endl;
#endif // DIM3
#ifdef DIM3
cgr::geo_from_automata simplegeo(&atmt);
simplegeo.make();
std::cout << "planarity: " << simplegeo.planarity() << std::endl;
std::cout << "before nonplanarity optimization:" << std::endl;
std::cout << " best nonplanarity/side_len: " << simplegeo.best_nonplanarity() / size << std::endl;
std::cout << " worst nonplanarity/side_len: " << simplegeo.worst_nonplanarity() / size << std::endl;
std::cout << " gmsh nonplanarity/side_len: " << simplegeo.gmsh_nonplanarity() / size << std::endl;
simplegeo.optimize_nonplanarity();
std::cout << "after nonplanarity optimization:" << std::endl;
std::cout << " gmsh nonplanarity/side_len: " << simplegeo.gmsh_nonplanarity() / size << std::endl;
std::ofstream file("polycr.geo");
simplegeo.write_geo(file);
//simplegeo.write_geo(std::cout);
#endif // DIM3
return 0;
}
int main() {
seed = 0;
for (std::size_t i = 0;; ++i) {
std::cout << "iteration: " << i << std::endl;
std::cout << "seed: " << seed << std::endl;
try {
inner_main();
} catch (int _) {
++seed;
continue;
}
break;
}
return 0;
}