-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer_graph.cpp
More file actions
2421 lines (2187 loc) · 86.8 KB
/
Copy pathlayer_graph.cpp
File metadata and controls
2421 lines (2187 loc) · 86.8 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "layer_graph.h"
namespace {
static bool SurfaceMeshContourIsSelfSupport(
const std::vector<int>& face_ids,
const std::vector<Eigen::Vector3d>& face_normals)
{
if (face_ids.empty()) {
return true;
}
const Eigen::Vector3d base_normal(0, 0, 1);
int cont_not_set_support = 0;
for (int face_id : face_ids) {
if (face_id < 0 || face_id >= static_cast<int>(face_normals.size())) {
continue;
}
const Eigen::Vector3d& face_normal = face_normals[face_id];
if (face_normal.dot(base_normal) + sin(PI / 3.6) < 0) {
++cont_not_set_support;
}
}
return double(cont_not_set_support) / double(face_ids.size()) <= 0.2;
}
static std::vector<Eigen::Vector2d> SliceContourToPolygon(const Polyline_type& contour)
{
std::vector<Eigen::Vector2d> polygon;
polygon.reserve(contour.size());
for (const auto& p : contour) {
polygon.emplace_back(p.x(), p.y());
}
return polygon;
}
// Tests whether a point lies in the offset band of any component boundary.
// Both the outer contour and every hole contour contribute supporting walls.
static bool PointInsideComponentBoundaryBand(
Polygon& outer_boundary_band,
std::vector<Polygon>& hole_boundary_bands,
const Eigen::Vector2d& point)
{
if (outer_boundary_band.JudgePointInside(point)) {
return true;
}
for (Polygon& hole_boundary_band : hole_boundary_bands) {
if (hole_boundary_band.JudgePointInside(point)) {
return true;
}
}
return false;
}
// Tests whether any outer or hole boundary pair is closer than the nozzle radius.
// An AABB rejection keeps distant component pairs out of the quadratic point test.
static bool ComponentBoundariesWithinDistance(
const Data& data,
std::size_t first_layer,
std::size_t first_component,
std::size_t second_layer,
std::size_t second_component,
double distance_squared)
{
const auto test_boundary_pair = [distance_squared](
const std::vector<Eigen::Vector2d>& first,
const std::vector<Eigen::Vector2d>& second) {
if (first.empty() || second.empty()) {
return false;
}
double first_min_x = first.front().x();
double first_max_x = first.front().x();
double first_min_y = first.front().y();
double first_max_y = first.front().y();
for (const Eigen::Vector2d& point : first) {
first_min_x = std::min(first_min_x, point.x());
first_max_x = std::max(first_max_x, point.x());
first_min_y = std::min(first_min_y, point.y());
first_max_y = std::max(first_max_y, point.y());
}
double second_min_x = second.front().x();
double second_max_x = second.front().x();
double second_min_y = second.front().y();
double second_max_y = second.front().y();
for (const Eigen::Vector2d& point : second) {
second_min_x = std::min(second_min_x, point.x());
second_max_x = std::max(second_max_x, point.x());
second_min_y = std::min(second_min_y, point.y());
second_max_y = std::max(second_max_y, point.y());
}
const double distance = std::sqrt(std::max(0.0, distance_squared));
if (first_max_x + distance < second_min_x
|| second_max_x + distance < first_min_x
|| first_max_y + distance < second_min_y
|| second_max_y + distance < first_min_y) {
return false;
}
for (const Eigen::Vector2d& a : first) {
for (const Eigen::Vector2d& b : second) {
if ((a - b).squaredNorm() < distance_squared) {
return true;
}
}
}
return false;
};
std::vector<const std::vector<Eigen::Vector2d>*> first_boundaries{
&data.slice_points[first_layer][first_component]
};
std::vector<const std::vector<Eigen::Vector2d>*> second_boundaries{
&data.slice_points[second_layer][second_component]
};
for (const auto& hole : data.slice_points_holes[first_layer][first_component]) {
first_boundaries.push_back(&hole);
}
for (const auto& hole : data.slice_points_holes[second_layer][second_component]) {
second_boundaries.push_back(&hole);
}
for (const auto* first : first_boundaries) {
for (const auto* second : second_boundaries) {
if (test_boundary_pair(*first, *second)) {
return true;
}
}
}
return false;
}
}
Layer_Graph::Layer_Graph(const Data& data)
{
this->total_node_num = data.total_node_num;
this->in_degree.resize(this->total_node_num, 0);
this->out_degree.resize(this->total_node_num, 0);
this->node_visited.resize(this->total_node_num, false);
this->edge_deleted.resize(this->total_node_num * this->total_node_num, false);
this->data = data;
this->G.resize(maxn);
this->G_2.resize(maxn);
this->G_3.resize(maxn);
this->cont_normal_dependency_edges = 0;
}
Layer_Graph::~Layer_Graph()
{
}
void Layer_Graph::creat_ball(string file_name, std::vector<Eigen::MatrixXd> vis_points)
{
Eigen::MatrixXd V_2;
Eigen::MatrixXi F_2;
igl::readOBJ("ball.obj", V_2, F_2);
ofstream all_balls(".\\vis\\" + file_name + "_unaccessivle_points.obj");
for (int i = 0; i < vis_points.size(); i++) {
for (int j = 0; j < V_2.rows(); j++)
all_balls << "v " << V_2(j, 0) + vis_points[i](0,0) << " " << V_2(j, 1) + vis_points[i](1, 0) << " " << V_2(j, 2) + vis_points[i](2, 0)<< " 0.9"<< " 0.05" <<" 0.05" << endl;
for (int j = 0; j < F_2.rows(); j++)
all_balls << "f " << F_2(j, 0) + i * V_2.rows() + 1 << " " << F_2(j, 1) + i * V_2.rows() + 1 << " " << F_2(j, 2) + i * V_2.rows() + 1 << endl;
}
}
void Layer_Graph::GetTrianglesForSurfaceMeshSlices(
const std::vector<SurfaceMeshSliceData>& slices,
const Eigen::Vector3d& vectorAfter,
int height_of_beam_search,
int id_continue)
{
std::vector<std::vector<std::vector<int>>> contour_face_ids;
std::vector<Eigen::Vector3d> face_normals;
for (std::size_t layer_id = 0; layer_id < data.slice_points.size(); ++layer_id) {
contour_face_ids.push_back({});
if (layer_id >= slices.size()) {
continue;
}
const auto& slice = slices[layer_id];
for (std::size_t component_id = 0;
component_id < data.slice_points[layer_id].size();
++component_id) {
contour_face_ids.back().push_back({});
auto& component_face_ids = contour_face_ids.back().back();
if (layer_id >= data.source_contour_ids.size()
|| layer_id >= data.source_hole_contour_ids.size()
|| component_id >= data.source_contour_ids[layer_id].size()
|| component_id >= data.source_hole_contour_ids[layer_id].size()) {
continue;
}
const int source_id = data.source_contour_ids[layer_id][component_id];
if (source_id >= 0
&& static_cast<std::size_t>(source_id) < slice.contour_face_ids.size()) {
for (const auto& face_index : slice.contour_face_ids[static_cast<std::size_t>(source_id)]) {
component_face_ids.push_back(static_cast<int>(face_index));
}
}
for (int hole_source_id : data.source_hole_contour_ids[layer_id][component_id]) {
if (hole_source_id < 0
|| static_cast<std::size_t>(hole_source_id) >= slice.contour_face_ids.size()) {
continue;
}
for (const auto& face_index : slice.contour_face_ids[static_cast<std::size_t>(hole_source_id)]) {
component_face_ids.push_back(static_cast<int>(face_index));
}
}
}
for (const auto& normal : slice.face_normals) {
face_normals.push_back(normal);
}
}
GetTrianglesForLayersFromMesh(contour_face_ids, face_normals, vectorAfter, height_of_beam_search, id_continue);
}
void Layer_Graph::GenerateDependencyEdgesFromSurfaceMeshSlices( //������ֻ���ݵ��Ƿ�����һ����������������������ߣ����Ըijɸ��ݱ��Ƿ���һ���������������������
const std::vector<SurfaceMeshSliceData>& slices)
{
for (std::size_t i = 1; i < data.slice_points.size(); ++i) {
std::vector<Polygon> last_layer_polygons;
std::vector<std::vector<Polygon>> last_layer_hole_polygons;
last_layer_polygons.reserve(data.slice_points[i - 1].size());
last_layer_hole_polygons.resize(data.slice_points[i - 1].size());
for (std::size_t component_id = 0;
component_id < data.slice_points[i - 1].size();
++component_id) {
last_layer_polygons.emplace_back(
ConstructPolygonPoints(data.slice_points[i - 1][component_id], dependence_offset));
for (const auto& hole : data.slice_points_holes[i - 1][component_id]) {
last_layer_hole_polygons[component_id].emplace_back(
ConstructPolygonPoints(hole, dependence_offset));
}
}
for (std::size_t j = 0; j < data.slice_points[i].size(); ++j) {
for (std::size_t m = 0; m < last_layer_polygons.size(); ++m) {
for (const Eigen::Vector2d& test_point : data.slice_points[i][j]) {
if (!PointInsideComponentBoundaryBand(
last_layer_polygons[m],
last_layer_hole_polygons[m],
test_point)) {
continue;
}
const int from_id = data.index_inv[std::make_pair(
static_cast<int>(i - 1),
static_cast<int>(m))];
const int to_id = data.index_inv[std::make_pair(
static_cast<int>(i),
static_cast<int>(j))];
this->AddEdge(from_id, to_id);
temp_edges.push_back(make_pair(from_id, to_id));
break;
}
}
}
}
}
void Layer_Graph::CollisionDetectionForAdditiveManufacturingFromSurfaceMeshSlices(
const std::vector<SurfaceMeshSliceData>& slices,
nozzle the_nozzle)
{
if (slices.empty()) {
std::cout << "[Layer_Graph::CollisionDetectionForAdditiveManufacturingFromSurfaceMeshSlices] Warning: No slices provided. Collision detection skipped." << std::endl;
return;
}
vector<pair<int, int>> temp_collision_edges;
for (std::size_t i = 0; i < data.slice_points.size(); ++i) {
for (std::size_t j = 0; j < data.slice_points[i].size(); ++j) {
for (std::size_t ii = i + 1; ii < data.slice_points.size(); ++ii) {
double circle_r;
if (slices[ii].layer_z - slices[i].layer_z < 0) {
std::cout << "[Layer_Graph::CollisionDetectionForAdditiveManufacturingFromSurfaceMeshSlices] Warning: Layer " << ii << " is below layer " << i << ". Skipping collision detection for this pair of layers." << std::endl;
continue;
}
double layer_distance = slices[ii].layer_z - slices[i].layer_z;
if (layer_distance < the_nozzle.nozzle_H_half) {
circle_r = the_nozzle.lowwer_surface_r + layer_distance * (the_nozzle.upper_surface_r - the_nozzle.lowwer_surface_r) / the_nozzle.nozzle_H_half;
}
//if ((ii - i) * dh < the_nozzle.nozzle_H_half)
// circle_r = the_nozzle.lowwer_surface_r + (ii - i) * dh * (the_nozzle.upper_surface_r - the_nozzle.lowwer_surface_r) / the_nozzle.nozzle_H_half;
else {
circle_r = the_nozzle.upper_surface_r;
}
for (std::size_t jj = 0; jj < data.slice_points[ii].size(); ++jj) {
const int lower_layer_id = static_cast<int>(i);
const int lower_component_id = static_cast<int>(j);
const int upper_layer_id = static_cast<int>(ii);
const int upper_component_id = static_cast<int>(jj);
bool jud_collision = false;
if (layer_distance > the_nozzle.nozzle__H_total) {
jud_collision = true;
temp_collision_edges.push_back(make_pair(
data.index_inv[std::make_pair(lower_layer_id, lower_component_id)],
data.index_inv[std::make_pair(upper_layer_id, upper_component_id)]));
continue;
}
else {
jud_collision = ComponentBoundariesWithinDistance(
data,
i,
j,
ii,
jj,
circle_r * circle_r);
}
if (jud_collision) {
const int from_id =
data.index_inv[std::make_pair(lower_layer_id, lower_component_id)];
const int to_id =
data.index_inv[std::make_pair(upper_layer_id, upper_component_id)];
//this->AddEdge_2(from_id, to_id);
//temp_edges.push_back(make_pair(from_id, to_id));
temp_collision_edges.push_back(make_pair(from_id, to_id));
}
}
}
}
}
// Build the dependency matrix without raw pointers.
std::vector<std::vector<bool>> dependency_relationship(
data.total_node_num,
std::vector<bool>(data.total_node_num, false));
for (int layer_id = 1; layer_id < static_cast<int>(data.slice_points.size()); ++layer_id) {
for (int point_id = 0; point_id < static_cast<int>(data.slice_points[layer_id].size()); ++point_id) {
const int target_node = data.index_inv[std::make_pair(layer_id, point_id)];
std::vector<int> current_frontier{ target_node };
for (int remaining_layers = layer_id; remaining_layers > 0 && !current_frontier.empty(); --remaining_layers) {
std::vector<int> next_frontier;
for (const auto& edge : temp_edges) {
if (std::find(current_frontier.begin(), current_frontier.end(), edge.second) == current_frontier.end()) {
continue;
}
dependency_relationship[edge.first][target_node] = true;
next_frontier.push_back(edge.first);
}
current_frontier = std::move(next_frontier);
}
}
}
cont_normal_dependency_edges = temp_edges.size();
//cout << "the number of dependency edges: " << temp_edges.size() << endl;
for (const auto& collision_edge : temp_collision_edges) {
const int from = collision_edge.first;
const int to = collision_edge.second;
if (dependency_relationship[from][to]) {
continue;
}
dependency_relationship[from][to] = true;
for (int node_id = 0; node_id < data.total_node_num; ++node_id) {
if (dependency_relationship[to][node_id]) {
dependency_relationship[from][node_id] = true;
}
}
this->AddEdge_2(from, to);
temp_edges.push_back(make_pair(from, to));
}
//cout << endl << "time..." << double(end_time_9 - start_time_9) / CLOCKS_PER_SEC << endl;
}
void Layer_Graph::BuildLayerGraphFromSurfaceMeshSlices(
const std::vector<SurfaceMeshSliceData>& slices,
nozzle the_nozzle)
{
this->total_node_num = data.total_node_num;
this->in_degree.assign(this->total_node_num, 0);
this->out_degree.assign(this->total_node_num, 0);
this->node_visited.assign(this->total_node_num, false);
this->edge_deleted.assign(this->total_node_num * this->total_node_num, false);
this->G.resize(maxn);
this->G_2.resize(maxn);
this->G_3.resize(maxn);
this->temp_edges.clear();
this->cont_normal_dependency_edges = 0;
this->all_triangles_of_layers.clear();
this->is_the_layer_self_suppot.clear();
this->GetTrianglesForSurfaceMeshSlices(slices, Eigen::Vector3d(0, 0, 1), 0, 0);
this->GenerateDependencyEdgesFromSurfaceMeshSlices(slices);
this->CollisionDetectionForAdditiveManufacturingFromSurfaceMeshSlices(slices, the_nozzle);
}
void Layer_Graph::GetTrianglesForLayersFromMesh(
const std::vector<std::vector<std::vector<int>>>& contour_face_ids,
const std::vector<Eigen::Vector3d>& face_normals,
const Eigen::Vector3d& vectorAfter,
int height_of_beam_search,
int id_continue)
{
is_the_layer_self_suppot.resize(total_node_num);
int cont_num = 0;
all_triangles_of_layers.resize(total_node_num);
const Eigen::Vector3d base_normal(0, 0, 1);
for (int i = 0; i < contour_face_ids.size(); i++) {
for (int j = 0; j < contour_face_ids[i].size(); j++) {
is_the_layer_self_suppot[cont_num] = true;
int cont_not_set_support = 0;
const auto& face_ids = contour_face_ids[i][j];
for (int k = 0; k < face_ids.size(); k++) {
int face_id = face_ids[k];
if (face_id < 0 || face_id >= face_normals.size()) {
continue;
}
const Eigen::Vector3d& face_normal = face_normals[face_id];
bool jud_self_support = (face_normal.dot(base_normal) + sin(PI / 3.6) >= 0);
//bool jud_self_support = (face_normal.dot(base_normal) + sin(PI / 4) >= 0);
if (!jud_self_support) {
cont_not_set_support++;
}
}
if (!face_ids.empty()) {
if (double(cont_not_set_support) / double(face_ids.size()) > 0.2) {
is_the_layer_self_suppot[cont_num] = false;
}
}
cont_num++;
}
}
}
void Layer_Graph::GetTrianglesForLayers(vector<vector<vector<Vertex>>> all_slice_points, std::vector<map<pair<Vertex, Vertex>, Triangle*>> map_segment_triangles, vector<Vertex> all_vertex, Eigen::Vector3d vectorAfter, int height_of_beam_search, int id_continue)
{ is_the_layer_self_suppot.resize(total_node_num);
int cont_num = 0;
all_triangles_of_layers.resize(total_node_num);
//cout << "%%" << data.slice_points.size() << endl;
for (int i = 0; i < data.slice_points.size(); i++) {
for (int j = 0; j < data.slice_points[i].size(); j++) {
is_the_layer_self_suppot[cont_num] = true;
int cont_not_set_support = 0;
for (int k = 1; k < data.slice_points[i][j].size(); k++) {
Vertex v_left = all_slice_points[i][j][k - 1];
Vertex v_right = all_slice_points[i][j][k];
pair<Vertex, Vertex> temp_pair(v_left, v_right);
if (map_segment_triangles[i].count(temp_pair) <= 0)
continue;
Triangle* tri = map_segment_triangles[i][temp_pair];
if (tri == nullptr) {
std::cout << "[Layer_Graph::GetTrianglesForLayers] Triangle is null" << std::endl;
continue;
}
all_triangles_of_layers[cont_num].push_back(tri);
//cout << "d" << endl;
Vertex* v1 = tri->vertices_2[0];
//cout << "k" << endl;
Vertex* v2 = tri->vertices_2[1];
//cout << "m" << endl;
Vertex* v3 = tri->vertices_2[2];
//cout << "d" << endl;
//double ans = (v2->x - v1->x) * (v2->y - v3->y) - (v2->y - v1->y) * (v2->x - v3->x);
//if (ans > 0) //is clockwise
// swap(v2, v3);
double na = (v2->y - v1->y) * (v3->z - v1->z) - (v2->z - v1->z) * (v3->y - v1->y);
double nb = (v2->z - v1->z) * (v3->x - v1->x) - (v2->x - v1->x) * (v3->z - v1->z);
double nc = (v2->x - v1->x) * (v3->y - v1->y) - (v2->y - v1->y) * (v3->x - v1->x);
Eigen::Vector3d face_normal(na, nb, nc);
face_normal.normalize();
Eigen::Vector3d base_normal (0, 0, 1);
base_normal.normalize();
bool jud_self_support;
//cout << "#####" << height_of_beam_search << " " << id_continue << endl;
//if (height_of_beam_search != 6 || id_continue != 0)
// jud_self_support = (face_normal.dot(base_normal) + sin(PI / 10) >= 0); //PI / 3.6
//else
jud_self_support = (face_normal.dot(base_normal) + sin(PI / 3.6) >= 0); //PI / 3.6
if (jud_self_support == false) {
//cont_not_set_support++; //ע��ʱ�ر�
}
//cout << "%%" << height_of_beam_search << " " << id_continue << endl;
//if (height_of_beam_search <= 2) {
if (double(cont_not_set_support) / double(data.slice_points[i][j].size()) > 0.2) { //data.slice_points[i][j].size()) > 0.2
is_the_layer_self_suppot[cont_num] = false;
break;
}
//}
//else {
// if (double(cont_not_set_support) / double(data.slice_points[i][j].size()) > 1.0) { //data.slice_points[i][j].size()) > 0.2
// is_the_layer_self_suppot[cont_num] = false;
// break;
// }
//}
}
cont_num++;
}
}
}
void Layer_Graph::GenerateDependencyEdges()
{
for (std::size_t layer_id = 1; layer_id < data.slice_points.size(); ++layer_id) {
std::vector<Polygon> lower_outer_polygons;
std::vector<std::vector<Polygon>> lower_hole_polygons;
lower_outer_polygons.reserve(data.slice_points[layer_id - 1].size());
lower_hole_polygons.resize(data.slice_points[layer_id - 1].size());
for (std::size_t component_id = 0;
component_id < data.slice_points[layer_id - 1].size();
++component_id) {
lower_outer_polygons.emplace_back(
ConstructPolygonPoints(
data.slice_points[layer_id - 1][component_id],
dependence_offset));
for (const auto& hole : data.slice_points_holes[layer_id - 1][component_id]) {
lower_hole_polygons[component_id].emplace_back(
ConstructPolygonPoints(hole, dependence_offset));
}
}
for (std::size_t upper_component_id = 0;
upper_component_id < data.slice_points[layer_id].size();
++upper_component_id) {
for (std::size_t lower_component_id = 0;
lower_component_id < lower_outer_polygons.size();
++lower_component_id) {
for (const Eigen::Vector2d& test_point : data.slice_points[layer_id][upper_component_id]) {
if (!PointInsideComponentBoundaryBand(
lower_outer_polygons[lower_component_id],
lower_hole_polygons[lower_component_id],
test_point)) {
continue;
}
const int from_id = data.index_inv[std::make_pair(
static_cast<int>(layer_id - 1),
static_cast<int>(lower_component_id))];
const int to_id = data.index_inv[std::make_pair(
static_cast<int>(layer_id),
static_cast<int>(upper_component_id))];
this->AddEdge(from_id, to_id);
temp_edges.emplace_back(from_id, to_id);
break;
}
}
}
}
}
void Layer_Graph::BuildLayerGraph(nozzle the_nozzle)
{
clock_t start_time, end_time;
start_time = clock();
vector<pair<int, int>> temp_collision_edges;
vector<pair<int, int>> all_id_layers;
Eigen::Vector2d dir;
for (int i = 1; i < data.slice_points.size(); i++) {
//get (i-1) layer segment's contour
std::vector<Polygon> Last_layer_polygons;
for (int j = 0; j < data.slice_points[i - 1].size(); j++) {
Last_layer_polygons.push_back(Polygon(ConstructPolygonPoints(data.slice_points[i - 1][j], dependence_offset)));
}
// build graph
for (int j = 0; j < data.slice_points[i].size(); j++) {
for (int m = 0; m < Last_layer_polygons.size(); m++) {
for (int k = 0; k < data.slice_points[i][j].size(); k++) {
if (Last_layer_polygons[m].JudgePointInside(data.slice_points[i][j][k])) {
this->AddEdge(data.index_inv[std::make_pair(i - 1, m)], data.index_inv[std::make_pair(i, j)]);
temp_edges.push_back(make_pair(data.index_inv[std::make_pair(i - 1, m)], data.index_inv[std::make_pair(i, j)]));
all_id_layers.push_back(make_pair(i - 1 , i));
break;
}
}
}
}
}
cont_normal_dependency_edges = temp_edges.size();
//collision detect, add dependency edge
for (int i = 0; i < data.slice_points.size(); i++) {
for (int j = 0; j < data.slice_points[i].size(); j++) {
for (int ii = i + 1; ii < data.slice_points.size(); ii++) {
double circle_r;
if ((ii - i) * dh < the_nozzle.nozzle_H_half)
circle_r = the_nozzle.lowwer_surface_r + (ii - i) * dh * (the_nozzle.upper_surface_r - the_nozzle.lowwer_surface_r) / the_nozzle.nozzle_H_half;
else
circle_r = the_nozzle.upper_surface_r;
for (int jj = 0; jj < data.slice_points[ii].size(); jj++) {
bool jud_collision = false;
if ((ii - i) * dh > the_nozzle.nozzle__H_total) //exceed nozzle_H
{
jud_collision = true;
temp_collision_edges.push_back(make_pair(data.index_inv[std::make_pair(i, j)], data.index_inv[std::make_pair(ii, jj)]));
continue;
}
for (int k = 0; k < data.slice_points[i][j].size(); k+=20) { //step == 2
for (int kk = 0; kk < data.slice_points[ii][jj].size(); kk+=20) {
if (pow(data.slice_points[ii][jj][kk].x() - data.slice_points[i][j][k].x(),2) + pow(data.slice_points[ii][jj][kk].y() - data.slice_points[i][j][k].y(),2) - pow(circle_r,2) < 0) {
jud_collision = true;
break;
}
}
if (jud_collision == true)
break;
}
if (jud_collision == true) {
temp_collision_edges.push_back(make_pair(data.index_inv[std::make_pair(i, j)], data.index_inv[std::make_pair(ii, jj)]));
}
}
}
}
}
//establish hase graph
bool** dependency_relationship = new bool*[10000];
for (int i = 0;i < 10000;i++)
dependency_relationship[i] = new bool[10000];
for (int i = 0;i < 10000;i++)
for (int j = 0;j < 10000;j++)
dependency_relationship[i][j] = false;
for (int i = 1; i < data.slice_points.size(); i++) {
for (int j = 0; j < data.slice_points[i].size(); j++) {
int id_layer = i;
vector<int> id_current_layer;
vector<int> temp_id_current_layer;
id_current_layer.push_back(data.index_inv[std::make_pair(i, j)]);
while (id_layer > 0) {
for (int k = 0;k < temp_edges.size();k++) {
for (int m = 0;m < id_current_layer.size();m++) {
if (temp_edges[k].second == id_current_layer[m]) {
dependency_relationship[temp_edges[k].first][data.index_inv[std::make_pair(i, j)]] = true;
temp_id_current_layer.push_back(temp_edges[k].first);
break;
}
}
}
id_layer--;
id_current_layer = temp_id_current_layer;
temp_id_current_layer.clear();
}
}
}
cout << "the number of dependency edges: " << temp_edges.size() << endl;
for (int i = 0;i < temp_collision_edges.size();i++) {
if (dependency_relationship[temp_collision_edges[i].first][temp_collision_edges[i].second] == false) {
dependency_relationship[temp_collision_edges[i].first][temp_collision_edges[i].second] = true;
for (int j = 0;j < 10000;j++) { //update
if (dependency_relationship[temp_collision_edges[i].second][j] == true) {
dependency_relationship[temp_collision_edges[i].first][j] = true;
}
}
this->AddEdge_2(temp_collision_edges[i].first, temp_collision_edges[i].second);
temp_edges.push_back(make_pair(temp_collision_edges[i].first, temp_collision_edges[i].second));
}
}
cout << "the number of edges: " << temp_edges.size()<<endl;
end_time = clock();
cout << "&&&&&&& time of establish dependency graph (contain collision detection): " << double(end_time - start_time) / CLOCKS_PER_SEC << "s &&&&&&&" << endl;
}
void Layer_Graph::BuildDependencyGraph(std::vector<Eigen::Vector3d>& all_points)
{
bool** Dependen_edges;
Dependen_edges = new bool*[3000];
for (int i = 0;i < 3000;i++)
Dependen_edges[i] = new bool[3000];
for (int i = 0;i < 3000;i++)
for (int j = 0;j < 3000;j++)
Dependen_edges[i][j] = false;
std::vector<Eigen::Vector2d> all_2d_points;
std::vector<Polygon> All_polygons;
for (int i = 0;i < all_points.size();i++) {
Eigen::Vector2d temp_point(all_points[i].x(), all_points[i].y());
all_2d_points.push_back(temp_point);
std::vector<Eigen::Vector2d> temp_2d_point;
Eigen::Vector2d temp_2d_one_point = all_2d_points[i];
double r = 0.2;
//temp_2d_point.push_back(temp_2d_one_point);
//temp_2d_one_point.x -= r;
temp_2d_one_point.y() -= 2 * r;
temp_2d_point.push_back(temp_2d_one_point);
temp_2d_one_point.y() += 2 * r;
temp_2d_point.push_back(temp_2d_one_point);
temp_2d_one_point.y() += 2 * r;
temp_2d_point.push_back(temp_2d_one_point);
//temp_2d_one_point.x += r;
//temp_2d_one_point.x += r;
//temp_2d_one_point.y += r;
/*for (double sita = 0;sita < 2 * PI;sita += 1) {
temp_2d_one_point.x += r * sin(sita);
temp_2d_one_point.y += r * cos(sita);
temp_2d_point.push_back(temp_2d_one_point);
temp_2d_one_point.x -= r * sin(sita);
temp_2d_one_point.y -= r * cos(sita);
}*/
All_polygons.push_back(Polygon(ConstructPolygonPoints_2(temp_2d_point, dependence_offset/4*0.48)));
}
int cont_edges = 0;
for (int i = 0;i < all_points.size();i++) {
for (int j = 0;j < all_points.size();j++) {
if (i != j && all_points[j].z() < all_points[i].z())
if (All_polygons[j].JudgePointInside(all_2d_points[i])) {
std::pair<int, int> Pair(i, j);
Dependen_edges[i][j] = true;
cont_edges++;
}
}
}
for (int i = 0;i < 3000;i++) {
for (int j = 0;j < 3000;j++) {
if (Dependen_edges[i][j] == true) {
for (int t = 0;t < 3000;t++) {
if (t != j && Dependen_edges[i][t] == true) {
if (Dependen_edges[t][j] == true) {
Dependen_edges[i][j] = false;
break;
}
}
}
}
}
}
ofstream all_edges("D:\\360MoveData\\Users\\zhong\\Desktop\\Others\\Open_Surface_Ceramics_Printing-master\\base_line\\all_edges.txt");
all_edges << cont_edges << endl;;
for (int i = 0;i < 3000;i++)
for (int j = 0;j < 3000;j++)
if (Dependen_edges[i][j] == true)
all_edges << i << " " << j << endl;
//Visual Vis;
//Vis.generateModelForRendering_6(all_points, Dependen_edges);
}
void Layer_Graph::GetInitialOPP()
{
clock_t start_time, end_time;
start_time = clock();
//std::cout << this->total_node_num << std::endl;
int d_num = 0;
for (int u = 0; u < this->total_node_num; u++) {
for (int i = 0; i < G[u].size(); i++) {
int v = this->edges[G[u][i]].GetTo(); d_num++;
if (in_degree[v] >= 2 || out_degree[u] >= 2) {
edge_deleted[G[u][i]] = true;
d_num++;
}
}
}
std::cout << "d_num: " << d_num << std::endl;
for (int i = 0; i < this->edges.size(); i++) {
if (edge_deleted[i]) {
int u = this->edges[i].GetFrom();
int v = this->edges[i].GetTo();
//std::cout << u << " " << v << std::endl;
in_degree[v]--;
out_degree[u]--;
}
}
for (int i = 0; i < this->total_node_num; i++) {
if ((this->node_visited[i] == false) && (this->in_degree[i] == 0)) {
//std::cout << i << std::endl;
this->UpdateDegree(i, -1);
std::vector<int> initial_opp;
initial_opp.push_back(i);
this->node_visited[i] = true;
DFS(i, initial_opp);
this->initial_opp_info.push_back(initial_opp);
}
}
end_time = clock();
std::cout << "***initial opp num***: " << this->initial_opp_info.size() << std::endl << std::endl;
std::cout << "&&&&&&& time of establish initial opp graph: " << double(end_time - start_time) / CLOCKS_PER_SEC << "s &&&&&&&" << std::endl;
//std::cout<<"&&&&&&& time of establish initial opp graph(vertex): " << double(end_time - start_time) / CLOCKS_PER_SEC << "s &&&&&&&" << endl;
#if DEFAULT_PRINTING_DIRECTION
std::cout << "initial opp num: " << this->initial_opp_info.size() << std::endl;
#else
#endif
this->OutputInitialOpp(file_name + "_initial_opp.txt");
}
bool Layer_Graph::IsDepend_collision(int i, int j)
{
/////////*int id_top_layer_from = initial_opp_info[i][initial_opp_info[i].size() - 1];
////////int id_bottom_layer_to = initial_opp_info[j][0];
////////for (int k = cont_normal_dependency_edges;k < temp_edges.size();k++) {
//////// if (temp_edges[k].first == id_top_layer_from && temp_edges[k].second == id_bottom_layer_to) {
//////// return true;
//////// }
////////}
////////return false;*/
/*int layer_1, layer_2;
for (int k = 0; k < data.slice_points[i].size(); k++) {
layer_1 = data.index_inv[std::make_pair(i, k)];
for (int l = 0; l < data.slice_points[j].size(); l++) {
layer_2 = data.index_inv[std::make_pair(j, l)];
for (int m = cont_normal_dependency_edges; m < temp_edges.size(); m++) {
if (temp_edges[m].first == layer_1 && temp_edges[m].second == layer_2) {
return true;
}
}
}
}*/
for (int m = cont_normal_dependency_edges; m < temp_edges.size(); m++) {
if (temp_edges[m].first == i && temp_edges[m].second == j) {
return true;
}
}
return false;
}
void Layer_Graph::DFS(int u, std::vector<int>& initial_opp)
{
for (int i = 0; i < this->G[u].size(); i++) {
int v = this->edges[G[u][i]].GetTo();
if (edge_deleted[G[u][i]]) continue;
if (this->node_visited[v]) continue;
if (this->in_degree[v] != 0) continue;
bool f1 = data.is_contour[data.index[u].first][data.index[u].second];
bool f2 = data.is_contour[data.index[v].first][data.index[v].second];
if (f1 != f2) {
/*std::cout << data.index[u].first << " " << data.index[u].second << std::endl;
std::cout << data.index[v].first << " " << data.index[v].second << std::endl;
std::cout << *(data.slice_points[data.index[u].first][data.index[u].second].begin()) << std::endl;
std::cout << *(data.slice_points[data.index[u].first][data.index[u].second].end() - 1) << std::endl;
std::cout << *(data.slice_points[data.index[v].first][data.index[v].second].begin()) << std::endl;
std::cout << *(data.slice_points[data.index[v].first][data.index[v].second].end() - 1) << std::endl;
std::cout << "contour and segment " << f1 << " " << f2 << std::endl;*/
continue;
}
this->UpdateDegree(v, -1);
this->node_visited[v] = true;
initial_opp.push_back(v);
DFS(v, initial_opp);
break;
}
}
bool Layer_Graph::compare_two_node(std::vector<int>a, std::vector<int>b)
{
bool jud_different = true;
for (int k = 0; k < a.size();) {
bool jud_different_2 = false;
for (int l = 0; l < b.size();) {
if (a[k] == b[l]) {
k++;
jud_different_2 = true;
break;
}
else {
l++;
}
}
if (jud_different_2 == false) {
jud_different = false;
break;
}
}
return jud_different;
}
void Layer_Graph::DFS_One(int u, std::vector<int>& medium_path, int num_blocks)
{
for (int i = 0; i < this->total_node_num; i++) {
int v = i;
if (u == v) continue;
if (this->node_visited[v]) continue;
if (this->in_degree[v] != 0) continue;
//bool jud_continue = false;
//int sum_layers = 0;
//for (int p = num_patches-1; p >= 0; p--) { //�ⲿ����û��Ǹij�ÿ��patches֮�����������Ȼ�����д�
// sum_layers += cont_nodes_of_patches[p];
// if (i == sum_layers && medium_path.size() != 0) {
// jud_continue = true;
// for (int q = 0; q < medium_path.size(); q++) {
// if (medium_path[q] == sum_layers - 1)
// jud_continue = false;
// }
// }
//}
//if (jud_continue == true)
// continue;
//if (i >= 166 && num_blocks <= 6)
//continue;
//////////collision dependency edges///////////////
if (IsDepend_collision(u, v))
continue;
///////////////////////////////////////////////////
bool jud_merge_layer = true;
for (int k = 0; k < current_area_S.size(); k++) {
if (has_subtractive_collision_dependency[current_area_S[k].id_layer][i] == true) {
//update subtractive map
Area_S temp_area_s_2 = all_the_area_S[map_index_layers[current_area_S[k].id_layer]];
for (int l = 0; l < temp_area_s_2.id_dep_layers.size(); l++) {
if (temp_area_s_2.id_dep_layers[l] == i) {
int index_id_dep_layers = l;
for (int m = 0; m < temp_area_s_2.K_and_ori[index_id_dep_layers].size(); m++) {
int current_K = temp_area_s_2.K_and_ori[index_id_dep_layers][m].first;
int current_ori = temp_area_s_2.K_and_ori[index_id_dep_layers][m].second;
for (int n = 0; n < current_point_map[k].size(); n++) {
if (current_point_map[k][n].id_k == current_K) {
current_point_map[k][n].flag_ori_unaccessible[current_ori] = true;
}
}
}
}
}
//judge whether can merge the layer
bool jud_merge_layer_2 = true;
for (int l = 0; l < current_point_map[k].size(); l++) {
bool jud_merge_layer_3 = false;
for (int m = 0; m < num_ori_sample; m++) {
if (current_point_map[k][l].flag_ori_unaccessible[m] == false) {
jud_merge_layer_3 = true;
}
}
if (jud_merge_layer_3 == false)
jud_merge_layer_2 = false;
}
if (jud_merge_layer_2 == false)
jud_merge_layer = false;
}
}
if (jud_merge_layer == true) {
if (flag_layer_is_accessible[i] == false) { //add area S to current block
current_area_S.push_back(all_the_area_S[map_index_layers[i]]);
////initial subtractive map
vector<point_subtractive_map> temp_vec_point_subtractive_map;
current_point_map.push_back(temp_vec_point_subtractive_map);
for (int k = 0; k < all_the_area_S[map_index_layers[i]].all_k.size(); k++) {
current_point_map[current_point_map.size() - 1].push_back(point_subtractive_map(all_the_area_S[map_index_layers[i]].all_k[k]));
}
for (int k = 0; k < all_the_area_S[map_index_layers[i]].id_dep_layers.size(); k++) {
int id_layer_2 = all_the_area_S[map_index_layers[i]].id_dep_layers[k];
has_subtractive_collision_dependency[i][id_layer_2] = true;
}
////add privious layers to current_point_map
for (int k = 0; k < medium_path.size(); k++) {
if (has_subtractive_collision_dependency[current_area_S[current_area_S.size() - 1].id_layer][medium_path[k]] == true) {
Area_S temp_area_s_2 = all_the_area_S[map_index_layers[current_area_S[current_area_S.size() - 1].id_layer]];
for (int l = 0; l < temp_area_s_2.id_dep_layers.size(); l++) {
if (temp_area_s_2.id_dep_layers[l] == medium_path[k]) {
int index_id_dep_layers = l;
for (int m = 0; m < temp_area_s_2.K_and_ori[index_id_dep_layers].size(); m++) {
int current_K = temp_area_s_2.K_and_ori[index_id_dep_layers][m].first;
int current_ori = temp_area_s_2.K_and_ori[index_id_dep_layers][m].second;
for (int n = 0; n < current_point_map[current_area_S.size() - 1].size(); n++) {
if (current_point_map[current_area_S.size() - 1][n].id_k == current_K) {
current_point_map[current_area_S.size() - 1][n].flag_ori_unaccessible[current_ori] = true;
}
}
}
}
}
}
}
}
//all_blocks[num_blocks].push_back(i);
}
else { //restore
for (int k = 0; k < current_area_S.size(); k++) {
if (has_subtractive_collision_dependency[current_area_S[k].id_layer][i] == true) {
Area_S temp_area_s_2 = all_the_area_S[map_index_layers[current_area_S[k].id_layer]];
for (int l = 0; l < temp_area_s_2.id_dep_layers.size(); l++) {
if (temp_area_s_2.id_dep_layers[l] == i) {
int index_id_dep_layers = l;
for (int m = 0; m < temp_area_s_2.K_and_ori[index_id_dep_layers].size(); m++) {
int current_K = temp_area_s_2.K_and_ori[index_id_dep_layers][m].first;
int current_ori = temp_area_s_2.K_and_ori[index_id_dep_layers][m].second;