-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessibilityVisualization.cpp
More file actions
1653 lines (1524 loc) · 60.4 KB
/
Copy pathAccessibilityVisualization.cpp
File metadata and controls
1653 lines (1524 loc) · 60.4 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 "AccessibilityVisualization.h"
#include "layer_graph.h"
#include "visual.h"
#include "polyscope/polyscope.h"
#include "polyscope/surface_mesh.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <direct.h>
#include <fstream>
#include <iomanip>
#include <limits>
#include <map>
#include <set>
namespace accessibility_visualization
{
namespace
{
// Precomputes the cutter dimensions used by the collision diagnostics.
void PrepareToolForCollision(cutter& tool)
{
tool.cylinder_height_threshold = tool.cylinder_height + tool.ball_r;
tool.carriage_check_radius_sq = (tool.carriage_r + 5.0) * (tool.carriage_r + 5.0);
tool.cylinder_check_radius_sq = (tool.cylinder_r + 5.0) * (tool.cylinder_r + 5.0);
tool.cylinder_r_sq = tool.cylinder_r * tool.cylinder_r;
tool.carriage_r_sq = tool.carriage_r * tool.carriage_r;
tool.total_height = tool.cylinder_height + tool.ball_r + tool.carriage_height;
}
// Computes the tool-axis center used by the production collision test.
Eigen::Vector3d ComputeToolCenter(
const Eigen::Vector3d& point,
const Eigen::Vector3d& normal,
double radius)
{
return point + radius * normal;
}
// Creates the immediate parent directory required by a debug output file.
void EnsureParentDirectory(const std::string& file_path)
{
const std::size_t slash_pos = file_path.find_last_of("/\\");
if (slash_pos != std::string::npos) {
_mkdir(file_path.substr(0, slash_pos).c_str());
}
}
// Joins one directory and file name using the Windows path separator.
std::string JoinPath(const std::string& directory, const std::string& file_name)
{
if (directory.empty()) {
return file_name;
}
const char last = directory.back();
if (last == '\\' || last == '/') {
return directory + file_name;
}
return directory + "\\" + file_name;
}
}
void WriteDebugMarkersObj(
const std::string& output_file,
const std::vector<Eigen::Vector3d>& points,
const std::array<double, 3>& color,
double marker_radius)
{
EnsureParentDirectory(output_file);
std::ofstream ofs(output_file);
if (!ofs.is_open()) {
std::cout << "[AccessibilityDebug] cannot open file for writing: " << output_file << std::endl;
return;
}
ofs << std::setprecision(17);
ofs << "# marker_count " << points.size() << "\n";
const std::array<Eigen::Vector3d, 6> offsets = {
Eigen::Vector3d(marker_radius, 0.0, 0.0),
Eigen::Vector3d(-marker_radius, 0.0, 0.0),
Eigen::Vector3d(0.0, marker_radius, 0.0),
Eigen::Vector3d(0.0, -marker_radius, 0.0),
Eigen::Vector3d(0.0, 0.0, marker_radius),
Eigen::Vector3d(0.0, 0.0, -marker_radius)
};
int vertex_base = 1;
for (const auto& point : points) {
for (const auto& offset : offsets) {
const Eigen::Vector3d v = point + offset;
ofs << "v " << v.x() << " " << v.y() << " " << v.z() << " "
<< color[0] << " " << color[1] << " " << color[2] << "\n";
}
const int xp = vertex_base;
const int xn = vertex_base + 1;
const int yp = vertex_base + 2;
const int yn = vertex_base + 3;
const int zp = vertex_base + 4;
const int zn = vertex_base + 5;
ofs << "f " << zp << " " << xp << " " << yp << "\n";
ofs << "f " << zp << " " << yp << " " << xn << "\n";
ofs << "f " << zp << " " << xn << " " << yn << "\n";
ofs << "f " << zp << " " << yn << " " << xp << "\n";
ofs << "f " << zn << " " << yp << " " << xp << "\n";
ofs << "f " << zn << " " << xn << " " << yp << "\n";
ofs << "f " << zn << " " << yn << " " << xn << "\n";
ofs << "f " << zn << " " << xp << " " << yn << "\n";
vertex_base += static_cast<int>(offsets.size());
}
std::cout << "[AccessibilityDebug] wrote " << points.size()
<< " markers to " << output_file << std::endl;
}
bool GetLayerGraphNodeCentroid(const Layer_Graph& layer_graph, int node_id, Eigen::Vector3d& centroid)
{
const auto index_it = layer_graph.data.index.find(node_id);
if (index_it == layer_graph.data.index.end()) {
return false;
}
const int layer_id = index_it->second.first;
const int contour_id = index_it->second.second;
if (layer_id < 0 || layer_id >= static_cast<int>(layer_graph.data.slice_points.size())
|| contour_id < 0 || contour_id >= static_cast<int>(layer_graph.data.slice_points[layer_id].size())
|| contour_id >= static_cast<int>(layer_graph.data.z_value[layer_id].size())) {
return false;
}
const auto& contour = layer_graph.data.slice_points[layer_id][contour_id];
const auto& z_values = layer_graph.data.z_value[layer_id][contour_id];
if (contour.empty() || z_values.empty()) {
return false;
}
Eigen::Vector2d xy_sum(0.0, 0.0);
for (const auto& point : contour) {
xy_sum += point;
}
xy_sum /= static_cast<double>(contour.size());
centroid = Eigen::Vector3d(xy_sum.x(), xy_sum.y(), z_values.front());
return true;
}
std::vector<Eigen::Vector3d> CollectMappedVertexPoints(
const Eigen::MatrixXd& original_vertices,
const std::unordered_map<int, int>& point_to_vertex)
{
std::vector<Eigen::Vector3d> points;
points.reserve(point_to_vertex.size());
for (const auto& entry : point_to_vertex) {
const int vertex_id = entry.second;
if (vertex_id < 0 || vertex_id >= original_vertices.rows()) {
continue;
}
points.emplace_back(
original_vertices(vertex_id, 0),
original_vertices(vertex_id, 1),
original_vertices(vertex_id, 2));
}
return points;
}
void WriteInitialSubtractiveAllDirectionDebugVisualization(
const std::string& vis_dir,
const std::string& file_token,
double marker_radius,
const Eigen::MatrixXd& original_vertices,
const std::unordered_map<int, int>& map_S_and_vertex)
{
const auto inaccessible_points = CollectMappedVertexPoints(original_vertices, map_S_and_vertex);
WriteDebugMarkersObj(
JoinPath(vis_dir, "access_debug_subtractive_all_direction_inaccessible_" + file_token + ".obj"),
inaccessible_points,
{ 0.95, 0.05, 0.85 },
marker_radius);
WriteDebugMarkersObj(
JoinPath(vis_dir, "access_debug_subtractive_all_direction_inaccessible_centers_" + file_token + ".obj"),
inaccessible_points,
{ 0.95, 0.05, 0.85 },
0.03);
}
struct ToolCollisionDebugInfo {
std::string reason = "none";
int vertex_index = -1;
double height_diff = 0.0;
double dist_xy_sq = 0.0;
double z_threshold = 0.0;
};
bool CheckToolCollisionWithCellForDebug(
const Eigen::Vector3d& center_point,
const std::vector<Eigen::Vector3d>& target_cell_vertices,
double max_z_target,
const cutter& tool,
ToolCollisionDebugInfo& debug_info,
double z_threshold_divisor = 30.0)
{
debug_info = ToolCollisionDebugInfo{};
debug_info.z_threshold = tool.cylinder_r / z_threshold_divisor;
debug_info.height_diff = max_z_target - center_point.z();
if (debug_info.height_diff <= debug_info.z_threshold) {
debug_info.reason = "below_tool_tip_threshold";
return false;
}
if (debug_info.height_diff > tool.total_height) {
debug_info.reason = "beyond_tool_total_height";
return true;
}
if (!target_cell_vertices.empty()) {
const double dx = target_cell_vertices[0](0, 0) - center_point.x();
const double dy = target_cell_vertices[0](1, 0) - center_point.y();
const double dist_xy_sq = dx * dx + dy * dy;
if (debug_info.height_diff > tool.cylinder_height_threshold) {
if (dist_xy_sq > tool.carriage_check_radius_sq) {
debug_info.reason = "coarse_reject_carriage_xy";
debug_info.dist_xy_sq = dist_xy_sq;
debug_info.vertex_index = 0;
return false;
}
}
else {
if (dist_xy_sq > tool.cylinder_check_radius_sq) {
debug_info.reason = "coarse_reject_cylinder_xy";
debug_info.dist_xy_sq = dist_xy_sq;
debug_info.vertex_index = 0;
return false;
}
}
}
for (int vertex_index = 0; vertex_index < static_cast<int>(target_cell_vertices.size()); ++vertex_index) {
const auto& vertex = target_cell_vertices[vertex_index];
const double diff_z = vertex(2, 0) - center_point.z();
if (diff_z <= debug_info.z_threshold) {
continue;
}
const double dx = vertex(0, 0) - center_point.x();
const double dy = vertex(1, 0) - center_point.y();
const double dist_xy_sq = dx * dx + dy * dy;
if (diff_z <= tool.cylinder_height_threshold) {
if (dist_xy_sq < tool.cylinder_r_sq) {
debug_info.reason = "cylinder_radius_collision";
debug_info.height_diff = diff_z;
debug_info.dist_xy_sq = dist_xy_sq;
debug_info.vertex_index = vertex_index;
return true;
}
}
else if (diff_z <= tool.total_height) {
if (dist_xy_sq < tool.carriage_r_sq) {
debug_info.reason = "carriage_radius_collision";
debug_info.height_diff = diff_z;
debug_info.dist_xy_sq = dist_xy_sq;
debug_info.vertex_index = vertex_index;
return true;
}
}
}
debug_info.reason = "no_vertex_collision";
return false;
}
void WriteObjColoredVertex(
std::ofstream& obj,
const Eigen::Vector3d& point,
const std::array<double, 3>& color,
int& next_vertex_index)
{
obj << "v " << point.x() << " " << point.y() << " " << point.z() << " "
<< color[0] << " " << color[1] << " " << color[2] << "\n";
++next_vertex_index;
}
void WriteObjLine(std::ofstream& obj, int a, int b)
{
obj << "l " << a << " " << b << "\n";
}
std::vector<int> WriteObjRing(
std::ofstream& obj,
int& next_vertex_index,
const Eigen::Matrix3d& rotated_to_world,
const Eigen::Vector3d& center_rotated,
double z_offset,
double radius,
const std::array<double, 3>& color,
int segment_count = 32)
{
std::vector<int> ring_indices;
ring_indices.reserve(segment_count);
const double kPi = 3.14159265358979323846;
for (int i = 0; i < segment_count; ++i) {
const double theta = 2.0 * kPi * static_cast<double>(i) / static_cast<double>(segment_count);
const Eigen::Vector3d point_rotated(
center_rotated.x() + radius * std::cos(theta),
center_rotated.y() + radius * std::sin(theta),
center_rotated.z() + z_offset);
ring_indices.push_back(next_vertex_index);
WriteObjColoredVertex(obj, rotated_to_world * point_rotated, color, next_vertex_index);
}
for (int i = 0; i < segment_count; ++i) {
WriteObjLine(obj, ring_indices[i], ring_indices[(i + 1) % segment_count]);
}
return ring_indices;
}
void WriteToolCollisionCaseObj(
std::ofstream& obj,
int& next_vertex_index,
const Eigen::Matrix3d& rotated_to_world,
const Eigen::Vector3d& center_rotated,
const std::vector<Eigen::Vector3d>& blocker_cell_vertices_rotated,
const Eigen::Vector3d& target_point_world,
const cutter& tool,
int case_index,
int s_id,
int cell_id,
int orientation_id,
int blocker_cell_id,
const ToolCollisionDebugInfo& collision_info)
{
if (!obj.is_open()) {
return;
}
obj << "g collision_case_" << case_index
<< "_s_" << s_id
<< "_cell_" << cell_id
<< "_ori_" << orientation_id
<< "_blocker_" << blocker_cell_id
<< "\n";
obj << "# reason " << collision_info.reason
<< " height_diff " << collision_info.height_diff
<< " dist_xy_sq " << collision_info.dist_xy_sq << "\n";
const std::array<double, 3> axis_color = { 0.0, 0.85, 1.0 };
const std::array<double, 3> cylinder_color = { 1.0, 0.55, 0.05 };
const std::array<double, 3> carriage_color = { 1.0, 0.9, 0.05 };
const std::array<double, 3> blocker_color = { 1.0, 0.05, 0.05 };
const std::array<double, 3> target_color = { 1.0, 0.0, 0.9 };
const int axis_start = next_vertex_index;
WriteObjColoredVertex(obj, rotated_to_world * center_rotated, axis_color, next_vertex_index);
const int axis_end = next_vertex_index;
WriteObjColoredVertex(
obj,
rotated_to_world * (center_rotated + Eigen::Vector3d(0.0, 0.0, tool.total_height)),
axis_color,
next_vertex_index);
WriteObjLine(obj, axis_start, axis_end);
const auto cylinder_bottom = WriteObjRing(
obj, next_vertex_index, rotated_to_world, center_rotated, 0.0, tool.cylinder_r, cylinder_color);
const auto cylinder_top = WriteObjRing(
obj, next_vertex_index, rotated_to_world, center_rotated, tool.cylinder_height_threshold, tool.cylinder_r, cylinder_color);
const auto carriage_bottom = WriteObjRing(
obj, next_vertex_index, rotated_to_world, center_rotated, tool.cylinder_height_threshold, tool.carriage_r, carriage_color);
const auto carriage_top = WriteObjRing(
obj, next_vertex_index, rotated_to_world, center_rotated, tool.total_height, tool.carriage_r, carriage_color);
for (int i = 0; i < static_cast<int>(cylinder_bottom.size()); i += 4) {
WriteObjLine(obj, cylinder_bottom[i], cylinder_top[i]);
WriteObjLine(obj, carriage_bottom[i], carriage_top[i]);
}
if (!blocker_cell_vertices_rotated.empty()) {
std::vector<int> blocker_indices;
blocker_indices.reserve(blocker_cell_vertices_rotated.size());
for (const auto& point_rotated : blocker_cell_vertices_rotated) {
blocker_indices.push_back(next_vertex_index);
WriteObjColoredVertex(obj, rotated_to_world * point_rotated, blocker_color, next_vertex_index);
}
for (int i = 0; i < static_cast<int>(blocker_indices.size()); ++i) {
WriteObjLine(obj, blocker_indices[i], blocker_indices[(i + 1) % blocker_indices.size()]);
}
}
const double target_marker_radius = std::max(0.25, tool.cylinder_r * 0.15);
const int target_x0 = next_vertex_index;
WriteObjColoredVertex(obj, target_point_world - Eigen::Vector3d(target_marker_radius, 0.0, 0.0), target_color, next_vertex_index);
const int target_x1 = next_vertex_index;
WriteObjColoredVertex(obj, target_point_world + Eigen::Vector3d(target_marker_radius, 0.0, 0.0), target_color, next_vertex_index);
const int target_y0 = next_vertex_index;
WriteObjColoredVertex(obj, target_point_world - Eigen::Vector3d(0.0, target_marker_radius, 0.0), target_color, next_vertex_index);
const int target_y1 = next_vertex_index;
WriteObjColoredVertex(obj, target_point_world + Eigen::Vector3d(0.0, target_marker_radius, 0.0), target_color, next_vertex_index);
const int target_z0 = next_vertex_index;
WriteObjColoredVertex(obj, target_point_world - Eigen::Vector3d(0.0, 0.0, target_marker_radius), target_color, next_vertex_index);
const int target_z1 = next_vertex_index;
WriteObjColoredVertex(obj, target_point_world + Eigen::Vector3d(0.0, 0.0, target_marker_radius), target_color, next_vertex_index);
WriteObjLine(obj, target_x0, target_x1);
WriteObjLine(obj, target_y0, target_y1);
WriteObjLine(obj, target_z0, target_z1);
}
bool FindHighestZMappedVertex(
const Eigen::MatrixXd& original_vertices,
const std::vector<vasco::VoronoiCell>& voronoi_cells,
const std::unordered_map<int, int>& map_S_and_vertex,
const std::vector<bool>* active_cell_mask,
const std::vector<bool>* searched_s_flags,
int& s_id,
int& cell_id)
{
s_id = -1;
cell_id = -1;
double max_z = MIN_D;
for (const auto& entry : map_S_and_vertex) {
const int candidate_s_id = entry.first;
const int candidate_cell_id = entry.second;
if (searched_s_flags != nullptr
&& candidate_s_id >= 0
&& candidate_s_id < static_cast<int>(searched_s_flags->size())
&& (*searched_s_flags)[candidate_s_id]) {
continue;
}
if (candidate_cell_id < 0
|| candidate_cell_id >= original_vertices.rows()
|| candidate_cell_id >= static_cast<int>(voronoi_cells.size())
|| !voronoi_cells[candidate_cell_id].is_available
|| voronoi_cells[candidate_cell_id].all_points_in_polygon.empty()) {
continue;
}
if (active_cell_mask != nullptr
&& candidate_cell_id < static_cast<int>(active_cell_mask->size())
&& !(*active_cell_mask)[candidate_cell_id]) {
continue;
}
const double z = original_vertices(candidate_cell_id, 2);
if (z > max_z) {
max_z = z;
s_id = candidate_s_id;
cell_id = candidate_cell_id;
}
}
return s_id >= 0 && cell_id >= 0;
}
std::string MakeToolCollisionObjBasePath(const std::string& output_file)
{
const std::string extension = ".obj";
if (output_file.size() >= extension.size()
&& output_file.substr(output_file.size() - extension.size()) == extension) {
return output_file.substr(0, output_file.size() - extension.size());
}
return output_file;
}
void WriteHighestZInaccessiblePointAllOrientationToolCollisionObj(
const std::string& output_file,
const Eigen::MatrixXd& original_vertices,
const std::vector<vasco::VoronoiCell>& voronoi_cells,
const std::vector<Eigen::Vector3d>& orientation_samples,
const std::unordered_map<int, int>& map_S_and_vertex,
cutter tool,
int& selected_s_id,
int& selected_cell_id,
const std::vector<bool>* active_cell_mask,
const std::vector<bool>* searched_s_flags)
{
PrepareToolForCollision(tool);
selected_s_id = -1;
selected_cell_id = -1;
if (!FindHighestZMappedVertex(
original_vertices,
voronoi_cells,
map_S_and_vertex,
active_cell_mask,
searched_s_flags,
selected_s_id,
selected_cell_id)) {
return;
}
const std::string output_base = MakeToolCollisionObjBasePath(output_file);
const std::string index_file = output_base + "_index.csv";
std::ofstream index_report(index_file);
if (index_report.is_open()) {
index_report << std::setprecision(17);
index_report << "orientation_id,orientation_x,orientation_y,orientation_z,obj_file,blocker_cell,reason,height_diff,dist_xy_sq,center_x,center_y,center_z\n";
}
const Eigen::Vector3d target_point_world(
original_vertices(selected_cell_id, 0),
original_vertices(selected_cell_id, 1),
original_vertices(selected_cell_id, 2));
for (int ori = 0; ori < static_cast<int>(orientation_samples.size()); ++ori) {
const Eigen::Vector3d orientation_vector = orientation_samples[ori].normalized();
const Eigen::Matrix3d rot_matrix =
Eigen::Quaterniond::FromTwoVectors(Eigen::Vector3d(0, 0, 1), orientation_vector).toRotationMatrix();
const Eigen::Matrix3d rot_matrix_inverse = rot_matrix.inverse();
std::vector<Eigen::Vector3d> rotated_sites(original_vertices.rows());
for (int site_id = 0; site_id < original_vertices.rows(); ++site_id) {
rotated_sites[site_id] = rot_matrix_inverse * Eigen::Vector3d(
original_vertices(site_id, 0),
original_vertices(site_id, 1),
original_vertices(site_id, 2));
}
std::vector<std::vector<Eigen::Vector3d>> rotated_cell_vertices(voronoi_cells.size());
std::vector<double> max_z_of_cells(voronoi_cells.size(), MIN_D);
for (int other_cell_id = 0; other_cell_id < static_cast<int>(voronoi_cells.size()); ++other_cell_id) {
rotated_cell_vertices[other_cell_id].reserve(voronoi_cells[other_cell_id].all_points_in_polygon.size());
for (const auto& point : voronoi_cells[other_cell_id].all_points_in_polygon) {
Eigen::Vector3d rotated_point = rot_matrix_inverse * Eigen::Vector3d(point.x(), point.y(), point.z());
rotated_cell_vertices[other_cell_id].push_back(rotated_point);
max_z_of_cells[other_cell_id] = std::max(max_z_of_cells[other_cell_id], rotated_point.z());
}
}
Eigen::Vector3d normal(0.0, 0.0, 0.0);
const auto& selected_cell_vertices = rotated_cell_vertices[selected_cell_id];
for (int j = 0; j < static_cast<int>(selected_cell_vertices.size()); ++j) {
const Eigen::Vector3d& v1 = rotated_sites[voronoi_cells[selected_cell_id].site];
const Eigen::Vector3d& v2 = selected_cell_vertices[j];
const Eigen::Vector3d& v3 = selected_cell_vertices[(j + 1) % selected_cell_vertices.size()];
normal += (v2 - v1).cross(v3 - v1);
}
if (normal.norm() <= 1e-12) {
continue;
}
normal.normalize();
const Eigen::Vector3d center_point = ComputeToolCenter(
rotated_sites[voronoi_cells[selected_cell_id].site],
normal,
tool.cylinder_r);
int blocker_cell = -1;
ToolCollisionDebugInfo collision_info;
for (int other_cell_id = 0; other_cell_id < static_cast<int>(voronoi_cells.size()); ++other_cell_id) {
if (selected_cell_id == other_cell_id || !voronoi_cells[other_cell_id].is_available) {
continue;
}
if (active_cell_mask != nullptr
&& other_cell_id < static_cast<int>(active_cell_mask->size())
&& !(*active_cell_mask)[other_cell_id]) {
continue;
}
if (CheckToolCollisionWithCellForDebug(
center_point,
rotated_cell_vertices[other_cell_id],
max_z_of_cells[other_cell_id],
tool,
collision_info)) {
blocker_cell = other_cell_id;
break;
}
}
std::vector<Eigen::Vector3d> blocker_vertices;
if (blocker_cell >= 0) {
blocker_vertices = rotated_cell_vertices[blocker_cell];
}
else {
collision_info.reason = "no_collision";
}
const std::string orientation_obj_file = output_base + "_ori_" + std::to_string(ori) + ".obj";
std::ofstream obj(orientation_obj_file);
if (!obj.is_open()) {
std::cout << "[AccessibilityDebug] cannot open file for writing: " << orientation_obj_file << std::endl;
continue;
}
obj << std::setprecision(17);
obj << "# Tool collision visualization for the highest-z all-direction-inaccessible point\n";
obj << "# selected_s_id " << selected_s_id << "\n";
obj << "# selected_cell_id " << selected_cell_id << "\n";
obj << "# selected_point "
<< target_point_world.x() << " "
<< target_point_world.y() << " "
<< target_point_world.z() << "\n";
obj << "# orientation_id " << ori << "\n";
obj << "# orientation_vector "
<< orientation_vector.x() << " "
<< orientation_vector.y() << " "
<< orientation_vector.z() << "\n";
obj << "# Cyan line: tool axis\n";
obj << "# Green line: sampled tool direction vector from the selected point\n";
obj << "# Orange rings: cylinder radius envelope\n";
obj << "# Yellow ring: carriage radius envelope\n";
obj << "# Red loop: first blocker Voronoi cell for this orientation, if any\n";
obj << "# Magenta cross: selected inaccessible point\n";
int next_vertex_index = 1;
const std::array<double, 3> direction_color = { 0.0, 1.0, 0.25 };
const int direction_start = next_vertex_index;
WriteObjColoredVertex(obj, target_point_world, direction_color, next_vertex_index);
const int direction_end = next_vertex_index;
WriteObjColoredVertex(
obj,
target_point_world + orientation_vector * std::max(tool.total_height, tool.cylinder_r * 3.0),
direction_color,
next_vertex_index);
WriteObjLine(obj, direction_start, direction_end);
WriteToolCollisionCaseObj(
obj,
next_vertex_index,
rot_matrix,
center_point,
blocker_vertices,
target_point_world,
tool,
ori,
selected_s_id,
selected_cell_id,
ori,
blocker_cell,
collision_info);
if (index_report.is_open()) {
index_report << ori << ","
<< orientation_vector.x() << ","
<< orientation_vector.y() << ","
<< orientation_vector.z() << ","
<< orientation_obj_file << ","
<< blocker_cell << ","
<< collision_info.reason << ","
<< collision_info.height_diff << ","
<< collision_info.dist_xy_sq << ","
<< center_point.x() << ","
<< center_point.y() << ","
<< center_point.z() << "\n";
}
}
std::cout << "[AccessibilityDebug] wrote highest-z per-orientation tool collision visualizations with base "
<< output_base << "_ori_<id>.obj" << std::endl;
}
void WriteSubtractiveAllDirectionReasonDiagnostics(
const std::string& vis_dir,
const std::string& file_token,
const Eigen::MatrixXd& original_vertices,
const std::vector<vasco::VoronoiCell>& voronoi_cells,
const std::vector<Eigen::Vector3d>& orientation_samples,
const std::unordered_map<int, int>& map_S_and_vertex,
cutter tool)
{
if (map_S_and_vertex.empty() || orientation_samples.empty()) {
return;
}
PrepareToolForCollision(tool);
const double horizontal_z_threshold = 0.2;
std::vector<int> horizontal_orientation_ids;
for (int ori = 0; ori < static_cast<int>(orientation_samples.size()); ++ori) {
if (std::abs(orientation_samples[ori].z()) <= horizontal_z_threshold) {
horizontal_orientation_ids.push_back(ori);
}
}
if (horizontal_orientation_ids.empty()) {
return;
}
const std::string output_file =
JoinPath(vis_dir, "access_debug_subtractive_all_direction_reason_" + file_token + ".csv");
const std::string meta_file =
JoinPath(vis_dir, "access_debug_subtractive_all_direction_reason_" + file_token + "_meta.txt");
const std::string tool_collision_obj_file =
JoinPath(vis_dir, "access_debug_subtractive_initial_full_model_tool_collision_" + file_token + ".obj");
EnsureParentDirectory(output_file);
std::ofstream report(output_file);
if (!report.is_open()) {
std::cout << "[AccessibilityDebug] cannot open file for writing: " << output_file << std::endl;
return;
}
std::ofstream meta_report(meta_file);
report << std::setprecision(17);
const int max_diagnostic_points = 500;
int highest_z_s_id = -1;
int highest_z_cell_id = -1;
WriteHighestZInaccessiblePointAllOrientationToolCollisionObj(
tool_collision_obj_file,
original_vertices,
voronoi_cells,
orientation_samples,
map_S_and_vertex,
tool,
highest_z_s_id,
highest_z_cell_id);
if (meta_report.is_open()) {
meta_report << std::setprecision(17);
meta_report << "horizontal_z_threshold: " << horizontal_z_threshold << "\n";
meta_report << "horizontal_orientation_count: " << horizontal_orientation_ids.size() << "\n";
meta_report << "max_diagnostic_points: " << max_diagnostic_points << "\n";
meta_report << "tool_collision_obj_scope: initial full model, highest-z all-direction-inaccessible point, one OBJ per sampled orientation\n";
meta_report << "tool_collision_obj_selected_s_id: " << highest_z_s_id << "\n";
meta_report << "tool_collision_obj_selected_cell_id: " << highest_z_cell_id << "\n";
meta_report << "tool_collision_obj_base: " << MakeToolCollisionObjBasePath(tool_collision_obj_file) << "_ori_<id>.obj\n";
meta_report << "tool_collision_obj_index: " << MakeToolCollisionObjBasePath(tool_collision_obj_file) << "_index.csv\n";
meta_report << "tool.cylinder_r: " << tool.cylinder_r << "\n";
meta_report << "tool.cylinder_height: " << tool.cylinder_height << "\n";
meta_report << "tool.ball_r: " << tool.ball_r << "\n";
meta_report << "tool.carriage_r: " << tool.carriage_r << "\n";
meta_report << "tool.carriage_height: " << tool.carriage_height << "\n";
meta_report << "tool.total_height: " << tool.total_height << "\n";
}
report << "s_id,vertex_id,x,y,z,horizontal_accessible_count,"
<< "beyond_total_height_count,cylinder_collision_count,carriage_collision_count,other_collision_count,"
<< "most_horizontal_ori,orientation_x,orientation_y,orientation_z,first_blocker_cell,first_reason,"
<< "first_height_diff,first_dist_xy_sq,first_center_x,first_center_y,first_center_z\n";
std::vector<std::pair<int, int>> sorted_entries(map_S_and_vertex.begin(), map_S_and_vertex.end());
std::sort(sorted_entries.begin(), sorted_entries.end());
int diagnostic_point_count = 0;
for (const auto& entry : sorted_entries) {
if (diagnostic_point_count >= max_diagnostic_points) {
break;
}
++diagnostic_point_count;
const int s_id = entry.first;
const int cell_id = entry.second;
if (cell_id < 0 || cell_id >= static_cast<int>(voronoi_cells.size())
|| cell_id >= original_vertices.rows()
|| !voronoi_cells[cell_id].is_available
|| voronoi_cells[cell_id].all_points_in_polygon.empty()) {
continue;
}
int horizontal_accessible_count = 0;
int beyond_total_height_count = 0;
int cylinder_collision_count = 0;
int carriage_collision_count = 0;
int other_collision_count = 0;
int most_horizontal_ori = -1;
double most_horizontal_abs_z = MAX_D;
int first_blocker_cell = -1;
ToolCollisionDebugInfo first_collision_info;
Eigen::Vector3d first_center(0.0, 0.0, 0.0);
for (int ori : horizontal_orientation_ids) {
if (std::abs(orientation_samples[ori].z()) < most_horizontal_abs_z) {
most_horizontal_abs_z = std::abs(orientation_samples[ori].z());
most_horizontal_ori = ori;
}
const Eigen::Matrix3d rot_matrix =
Eigen::Quaterniond::FromTwoVectors(Eigen::Vector3d(0, 0, 1), orientation_samples[ori]).toRotationMatrix();
const Eigen::Matrix3d rot_matrix_inverse = rot_matrix.inverse();
std::vector<Eigen::Vector3d> rotated_sites(original_vertices.rows());
std::vector<std::vector<Eigen::Vector3d>> rotated_cell_vertices(voronoi_cells.size());
std::vector<double> max_z_of_cells(voronoi_cells.size(), MIN_D);
for (int site_id = 0; site_id < original_vertices.rows(); ++site_id) {
rotated_sites[site_id] = rot_matrix_inverse * Eigen::Vector3d(
original_vertices(site_id, 0),
original_vertices(site_id, 1),
original_vertices(site_id, 2));
}
for (int other_cell_id = 0; other_cell_id < static_cast<int>(voronoi_cells.size()); ++other_cell_id) {
rotated_cell_vertices[other_cell_id].reserve(voronoi_cells[other_cell_id].all_points_in_polygon.size());
for (const auto& point : voronoi_cells[other_cell_id].all_points_in_polygon) {
Eigen::Vector3d rotated_point = rot_matrix_inverse * Eigen::Vector3d(point.x(), point.y(), point.z());
rotated_cell_vertices[other_cell_id].push_back(rotated_point);
max_z_of_cells[other_cell_id] = std::max(max_z_of_cells[other_cell_id], rotated_point.z());
}
}
Eigen::Vector3d normal(0.0, 0.0, 0.0);
const auto& cell_vertices = rotated_cell_vertices[cell_id];
for (int j = 0; j < static_cast<int>(cell_vertices.size()); ++j) {
const Eigen::Vector3d& v1 = rotated_sites[voronoi_cells[cell_id].site];
const Eigen::Vector3d& v2 = cell_vertices[j];
const Eigen::Vector3d& v3 = cell_vertices[(j + 1) % cell_vertices.size()];
normal += (v2 - v1).cross(v3 - v1);
}
if (normal.norm() <= 1e-12) {
++other_collision_count;
continue;
}
normal.normalize();
const Eigen::Vector3d center_point = ComputeToolCenter(
rotated_sites[voronoi_cells[cell_id].site],
normal,
tool.cylinder_r);
bool collision = false;
ToolCollisionDebugInfo collision_info;
int blocker_cell = -1;
for (int other_cell_id = 0; other_cell_id < static_cast<int>(voronoi_cells.size()); ++other_cell_id) {
if (cell_id == other_cell_id || !voronoi_cells[other_cell_id].is_available) {
continue;
}
if (CheckToolCollisionWithCellForDebug(
center_point,
rotated_cell_vertices[other_cell_id],
max_z_of_cells[other_cell_id],
tool,
collision_info)) {
collision = true;
blocker_cell = other_cell_id;
break;
}
}
if (!collision) {
++horizontal_accessible_count;
continue;
}
if (first_blocker_cell == -1 && ori == most_horizontal_ori) {
first_blocker_cell = blocker_cell;
first_collision_info = collision_info;
first_center = center_point;
}
if (collision_info.reason == "beyond_tool_total_height") {
++beyond_total_height_count;
}
else if (collision_info.reason == "cylinder_radius_collision") {
++cylinder_collision_count;
}
else if (collision_info.reason == "carriage_radius_collision") {
++carriage_collision_count;
}
else {
++other_collision_count;
}
}
if (most_horizontal_ori != -1) {
first_blocker_cell = -1;
first_collision_info = ToolCollisionDebugInfo{};
first_center = Eigen::Vector3d(0.0, 0.0, 0.0);
const int ori = most_horizontal_ori;
const Eigen::Matrix3d rot_matrix =
Eigen::Quaterniond::FromTwoVectors(Eigen::Vector3d(0, 0, 1), orientation_samples[ori]).toRotationMatrix();
const Eigen::Matrix3d rot_matrix_inverse = rot_matrix.inverse();
std::vector<Eigen::Vector3d> rotated_sites(original_vertices.rows());
std::vector<std::vector<Eigen::Vector3d>> rotated_cell_vertices(voronoi_cells.size());
std::vector<double> max_z_of_cells(voronoi_cells.size(), MIN_D);
for (int site_id = 0; site_id < original_vertices.rows(); ++site_id) {
rotated_sites[site_id] = rot_matrix_inverse * Eigen::Vector3d(
original_vertices(site_id, 0),
original_vertices(site_id, 1),
original_vertices(site_id, 2));
}
for (int other_cell_id = 0; other_cell_id < static_cast<int>(voronoi_cells.size()); ++other_cell_id) {
for (const auto& point : voronoi_cells[other_cell_id].all_points_in_polygon) {
Eigen::Vector3d rotated_point = rot_matrix_inverse * Eigen::Vector3d(point.x(), point.y(), point.z());
rotated_cell_vertices[other_cell_id].push_back(rotated_point);
max_z_of_cells[other_cell_id] = std::max(max_z_of_cells[other_cell_id], rotated_point.z());
}
}
Eigen::Vector3d normal(0.0, 0.0, 0.0);
const auto& cell_vertices = rotated_cell_vertices[cell_id];
for (int j = 0; j < static_cast<int>(cell_vertices.size()); ++j) {
const Eigen::Vector3d& v1 = rotated_sites[voronoi_cells[cell_id].site];
const Eigen::Vector3d& v2 = cell_vertices[j];
const Eigen::Vector3d& v3 = cell_vertices[(j + 1) % cell_vertices.size()];
normal += (v2 - v1).cross(v3 - v1);
}
if (normal.norm() > 1e-12) {
normal.normalize();
first_center = ComputeToolCenter(rotated_sites[voronoi_cells[cell_id].site], normal, tool.cylinder_r);
for (int other_cell_id = 0; other_cell_id < static_cast<int>(voronoi_cells.size()); ++other_cell_id) {
if (cell_id == other_cell_id || !voronoi_cells[other_cell_id].is_available) {
continue;
}
if (CheckToolCollisionWithCellForDebug(
first_center,
rotated_cell_vertices[other_cell_id],
max_z_of_cells[other_cell_id],
tool,
first_collision_info)) {
first_blocker_cell = other_cell_id;
break;
}
}
}
}
const Eigen::Vector3d ori_vec =
(most_horizontal_ori >= 0) ? orientation_samples[most_horizontal_ori] : Eigen::Vector3d(0, 0, 0);
report << s_id << "," << cell_id << ","
<< original_vertices(cell_id, 0) << ","
<< original_vertices(cell_id, 1) << ","
<< original_vertices(cell_id, 2) << ","
<< horizontal_accessible_count << ","
<< beyond_total_height_count << ","
<< cylinder_collision_count << ","
<< carriage_collision_count << ","
<< other_collision_count << ","
<< most_horizontal_ori << ","
<< ori_vec.x() << "," << ori_vec.y() << "," << ori_vec.z() << ","
<< first_blocker_cell << ","
<< first_collision_info.reason << ","
<< first_collision_info.height_diff << ","
<< first_collision_info.dist_xy_sq << ","
<< first_center.x() << ","
<< first_center.y() << ","
<< first_center.z() << "\n";
}
std::cout << "[AccessibilityDebug] wrote subtractive all-direction reason report to "
<< output_file << std::endl;
}
void WriteSubtractiveAccessibilityDebugVisualizations(
const std::string& vis_dir,
const std::string& node_tag,
double marker_radius,
const Eigen::MatrixXd& original_vertices,
const std::vector<bool>& judge_S_be_searched,
const std::vector<bool>& judge_covering_points_be_searched,
const std::unordered_map<int, int>& map_S_and_vertex,
const std::unordered_map<int, int>& map_covering_points_and_vertex)
{
std::vector<Eigen::Vector3d> remaining_subtractive_points;
for (int point_id = 0; point_id < static_cast<int>(judge_S_be_searched.size()); ++point_id) {
if (judge_S_be_searched[point_id]) {
continue;
}
const auto map_it = map_S_and_vertex.find(point_id);
if (map_it == map_S_and_vertex.end()) {
continue;
}
const int vertex_id = map_it->second;
if (vertex_id < 0 || vertex_id >= original_vertices.rows()) {
continue;
}
remaining_subtractive_points.emplace_back(
original_vertices(vertex_id, 0),
original_vertices(vertex_id, 1),
original_vertices(vertex_id, 2));
}
WriteDebugMarkersObj(
JoinPath(vis_dir, "access_debug_subtractive_remaining_S" + node_tag + ".obj"),
remaining_subtractive_points,
{ 1.0, 0.05, 0.05 },
marker_radius);
WriteDebugMarkersObj(
JoinPath(vis_dir, "access_debug_subtractive_all_direction_remaining" + node_tag + ".obj"),
remaining_subtractive_points,
{ 0.95, 0.05, 0.85 },
marker_radius);
std::vector<Eigen::Vector3d> remaining_covering_points;
for (int point_id = 0; point_id < static_cast<int>(judge_covering_points_be_searched.size()); ++point_id) {
if (judge_covering_points_be_searched[point_id]) {
continue;
}
const auto map_it = map_covering_points_and_vertex.find(point_id);
if (map_it == map_covering_points_and_vertex.end()) {
continue;
}
const int vertex_id = map_it->second;
if (vertex_id < 0 || vertex_id >= original_vertices.rows()) {
continue;
}
remaining_covering_points.emplace_back(
original_vertices(vertex_id, 0),
original_vertices(vertex_id, 1),
original_vertices(vertex_id, 2));
}
WriteDebugMarkersObj(
JoinPath(vis_dir, "access_debug_subtractive_remaining_covering" + node_tag + ".obj"),
remaining_covering_points,
{ 0.05, 0.35, 1.0 },
marker_radius);
}
std::vector<bool> BuildActiveOriginalVertexMask(
const Eigen::MatrixXd& original_vertices,
const std::vector<Vertex>& current_vertices,
double eps)
{
std::vector<bool> active(original_vertices.rows(), false);
for (int i = 0; i < original_vertices.rows(); ++i) {
for (const auto& vertex : current_vertices) {