-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paths_v2(hybrid).cpp
1511 lines (1301 loc) · 44.2 KB
/
Paths_v2(hybrid).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
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<iostream>
#include<OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/Mesh/Handles.hh>
#include <OpenMesh\Core\Utils\Property.hh>
#include <string>
#include <vector>
#include <algorithm>
#include<stdio.h>
#include "clipper.hpp"
#include"clipper.cpp"
#include<queue>
#define use_int32
using namespace ClipperLib;
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define MAX 0.6
#define MIN 0.2
#define H 0.4 //默认的路径厚度(适用于Case2)
#define Width_path 0.8 //路径宽度,可以通过这个来调整填充率
#define Extrusion_width 0.8 //实际挤出的路径宽度
#define Width_material 1.75
#define pi 3.14
#define N 1 //首层与末层的层数
#define NP 2 //隔点上升
#define V_size 0.8 //用于支撑算法的体素大小
#define With_support 0.4 //支撑的路径宽度
using namespace std;
typedef unsigned char boolean;
struct Point {
double x, y, z, t;
bool b = false;
};
vector<Point>coord; //缓存一层的轮廓线
vector<Point> buffer1;
vector<vector<Point>>buffer2;
vector<vector<vector<Point>>>paths;
vector<vector<vector<Point>>>P;
vector<vector<vector<Point>>>P_v;//支撑算法的体素点
vector<vector<vector<Point>>>Path_support;//支撑算法的路径
vector<vector<Point>>model; //slice 整个模型的,分层的轮廓线坐标,一维层数,二维此层的轮廓线点坐标
//vector<vector<Point>>model2; //slice 整个模型的,分层的轮廓线坐标,一维层数,二维此层的轮廓线点坐标
vector<Paths>model3;//从切片(model)转换类型得到的,做偏移之后画外轮廓。
vector<Point>shellpath;//存外壳上的点
vector<vector<Point> >shellpaths;//存某一层外壳上的路径
vector<vector<vector<Point> > >shell1;//存最里层轮廓上的路径 一维层数,二维每层不同的截面路径,三维路径的点坐标
vector<Paths>model2;//从切片(model)转换类型得到的,做偏移之后画外轮廓。
vector<vector<vector<Point> > >shell2; //存某一层外壳上的路径集合 一维该层变化线数量,二维壳的层数路径,三维路径坐标
vector<vector<vector<vector<Point>>>> shell;//存所有外壳上的路径 一维层数,二维每层不同的截面路径,三维不同截面路径壳的层数,四维壳路径点的坐标
typedef OpenMesh::TriMesh_ArrayKernelT<> MyMesh;//定义完整MyMesh类,用三角网格,自定的属性
MyMesh mesh;
double Bmax_x, Bmax_y, Bmax_z, Bmin_x, Bmin_y, Bmin_z, px, py, pz;//用于生成包围盒
int X, Y, Z;//包围盒内离散点的数量
int Scheme;
const string file_1 = "version1.obj";
// 读取文件的函数
void readfile(string file) {//文件读取到了mesh当中
// 请求顶点法线 vertex normals
mesh.request_vertex_normals();
//如果不存在顶点法线,则报错
if (!mesh.has_vertex_normals())
{
cout << "错误:标准定点属性 “法线”不存在" << endl;
return;
}
// 如果有顶点发现则读取文件
OpenMesh::IO::Options opt;
if (!OpenMesh::IO::read_mesh(mesh, file, opt))
{
cout << "无法读取文件:" << file << endl;
return;
}
else cout << "成功读取文件:" << file << endl;
cout << endl; // 为了ui显示好看一些
//如果不存在顶点法线,则计算出
if (!opt.check(OpenMesh::IO::Options::VertexNormal))
{
// 通过面法线计算顶点法线
mesh.request_face_normals();
// mesh计算出顶点法线
mesh.update_normals();
// 释放面法线
mesh.release_face_normals();
}
}
//截面函数
bool IntersectPlane(MyMesh::Point pt, MyMesh::Point pnorm) //pt截面上一点,pnorm截面法向
{
const float ERR = 0.001;
//参数包括 pt,pnorm,*pilist,pnum[] 具体函数原理 见 截面算法.docx
int starte, ne, ne1, nf;
MyMesh::Point vt1, vt2;
//MyMesh::Face f1;
MyMesh::HalfedgeHandle nhe;
MyMesh::FaceHandle nhf;
float d1, d2, sd1, sd2;
bool* flag, suc;
float dist, mind = 1.0e+8;
sd1 = sd2 = -10000;
int esize = mesh.n_halfedges();
flag = new bool[esize];
Point p;
suc = false;
for (MyMesh::HalfedgeIter it = mesh.halfedges_begin(); it != mesh.halfedges_end(); ++it) //遍历整个模型所有的边,有交点的把id记录在flag中
{
MyMesh::HalfedgeHandle hh = *it;
int id = hh.idx();
flag[id] = false;
auto fromVertex = mesh.from_vertex_handle(hh);
auto toVertex = mesh.to_vertex_handle(hh);
vt1 = mesh.point(fromVertex);
vt2 = mesh.point(toVertex);
//printf("$ %.3f %.3f $\n", vt1.data()[0],vt2.data()[0]);
d1 = pnorm.data()[0] * (vt1.data()[0] - pt.data()[0]) + pnorm.data()[1] * (vt1.data()[1] - pt.data()[1])
+ pnorm.data()[2] * (vt1.data()[2] - pt.data()[2]);//d1到面的距离
d2 = pnorm.data()[0] * (vt2.data()[0] - pt.data()[0]) + pnorm.data()[1] * (vt2.data()[1] - pt.data()[1])
+ pnorm.data()[2] * (vt2.data()[2] - pt.data()[2]);
if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0) || d1 > 0 && d2 == 0 || d1 == 0 && d2 > 0))//线段与面相交
{
flag[id] = true;
vt1.data()[0] = vt1.data()[0] - pt.data()[0];
vt1.data()[1] = vt1.data()[1] - pt.data()[1];
vt1.data()[2] = vt1.data()[2] - pt.data()[2]; // point date minus point date
dist = vt1.data()[0] * vt1.data()[0] + vt1.data()[1] * vt1.data()[1] + vt1.data()[2] * vt1.data()[2];
if (dist < mind)
{
nhe = hh; //最短边
mind = dist;//最短距离
ne = id; //最短所在边的编号 // printf("ne: %d \n", ne);
suc = true;
}
}
}
if (!suc)
{
delete[]flag;
return false; //没有交点,这里return false,跳出整个函数
}
starte = ne;//标记循环起始的边
suc = false;
nhf = mesh.face_handle(nhe);//最短边所在面
while (!suc)
{
//printf("%%%%");
auto fromVertex = mesh.from_vertex_handle(nhe);
auto toVertex = mesh.to_vertex_handle(nhe);
vt1 = mesh.point(fromVertex);
vt2 = mesh.point(toVertex);
d1 = pnorm.data()[0] * (vt1.data()[0] - pt.data()[0]) + pnorm.data()[1] * (vt1.data()[1] - pt.data()[1])
+ pnorm.data()[2] * (vt1.data()[2] - pt.data()[2]);
d2 = pnorm.data()[0] * (vt2.data()[0] - pt.data()[0]) + pnorm.data()[1] * (vt2.data()[1] - pt.data()[1])
+ pnorm.data()[2] * (vt2.data()[2] - pt.data()[2]);
//printf("$$$%lf %lf \n", d1, d2);
if ((sd1 == d1) && (sd2 == d2))
{
flag[ne] = false;
}
sd1 = d1; sd2 = d2;
//pilist[num].data()[0] = (float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2))*(vt2.data()[0] - vt1.data()[0]) + vt1.data()[0];
//pilist[num].data()[1] = (float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2))*(vt2.data()[1] - vt1.data()[1]) + vt1.data()[1];
//pilist[num].data()[2] = (float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2))*(vt2.data()[2] - vt1.data()[2]) + vt1.data()[2];
p = { (float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2)) * (vt2.data()[0] - vt1.data()[0]) + vt1.data()[0] ,(float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2)) * (vt2.data()[1] - vt1.data()[1]) + vt1.data()[1] };
coord.push_back(p);
do {
for (auto it = mesh.fh_begin(nhf); it != mesh.fh_end(nhf); ++it) //nhf最短边所在面,迭代这个面的边,只有3个
{
MyMesh::HalfedgeHandle halfnow = *it;
const int ne1 = halfnow.idx();
if (flag[ne1] == false || ne == ne1) continue;
MyMesh::VertexHandle fromV = mesh.from_vertex_handle(halfnow);
MyMesh::VertexHandle toV = mesh.to_vertex_handle(halfnow);
vt1 = mesh.point(fromV);
vt2 = mesh.point(toV);
d1 = pnorm.data()[0] * (vt1.data()[0] - pt.data()[0]) + pnorm.data()[1] * (vt1.data()[1] - pt.data()[1])
+ pnorm.data()[2] * (vt1.data()[2] - pt.data()[2]);
d2 = pnorm.data()[0] * (vt2.data()[0] - pt.data()[0]) + pnorm.data()[1] * (vt2.data()[1] - pt.data()[1])
+ pnorm.data()[2] * (vt2.data()[2] - pt.data()[2]);
p = { (float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2)) * (vt2.data()[0] - vt1.data()[0]) + vt1.data()[0] ,(float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2)) * (vt2.data()[1] - vt1.data()[1]) + vt1.data()[1] };
coord.push_back(p);
MyMesh::HalfedgeHandle halfnext = mesh.opposite_halfedge_handle(halfnow);//获取反向半边
nhf = mesh.face_handle(halfnext);//返回这个边所在的面
int ne2 = halfnext.idx();
flag[ne1] = flag[ne2] = false;//ne1,ne2是对向的两个半边,存过其中一个的交点就都变为false
if (nhf.idx() == -1)
{
starte = ne;
flag[ne] = false;
break;
}
ne = ne2;//以对边的那个面再找下一个与面相交的线
//pilist[num].data()[0] = (float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2))*(vt2.data()[0] - vt1.data()[0]) + vt1.data()[0];
//pilist[num].data()[1] = (float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2))*(vt2.data()[1] - vt1.data()[1]) + vt1.data()[1];
//pilist[num].data()[2] = (float)fabs(d1) / ((float)fabs(d1) + (float)fabs(d2))*(vt2.data()[2] - vt1.data()[2]) + vt1.data()[2];
//printf("##%lf %lf %lf\n", pilist[num].data()[0], pilist[num].data()[1], pilist[num].data()[2]);
break;
}
} while (ne != starte);
suc = true;
for (auto it = mesh.halfedges_begin(); it != mesh.halfedges_end(); ++it) //检索有没有第二个环
{
MyMesh::HalfedgeHandle hh = *it;
int id = hh.idx();
if (flag[id] == true)
{
ne = id;
starte = ne;
nhe = hh;
nhf = mesh.face_handle(nhe);
if (nhf.idx() == -1)
{
flag[ne] = false;
continue;
}
//pilist[num].data()[0] = -10000;
//pilist[num].data()[1] = -10000;
//pilist[num].data()[2] = -10000;
p = { -10000,-10000 };//两个环中间的间隔数据
coord.push_back(p);
suc = false;
break;
}
}
};
delete[]flag;
return true;
}
//通过遍历点获取模型的包围盒
void BoundingBox() {
MyMesh::Point pt;
int st = 0;
for (auto it = mesh.vertices_begin(); it != mesh.vertices_end(); ++it) {
MyMesh::VertexHandle vh_i = *it;
pt = mesh.point(vh_i);
px = pt.data()[0];
py = pt.data()[1];
pz = pt.data()[2];
if (st == 0) {
Bmax_x = Bmin_x = px;
Bmax_y = Bmin_y = py;
Bmax_z = Bmin_z = pz;
st++;
}
else {
if (px > Bmax_x)Bmax_x = px; else if (px < Bmin_x)Bmin_x = px;
if (py > Bmax_y)Bmax_y = py; else if (py < Bmin_y)Bmin_y = py;
if (pz > Bmax_z)Bmax_z = pz; else if (pz < Bmin_z)Bmin_z = pz;
}
}
Bmin_x -= Width_path / 2;
Bmin_y -= Width_path / 2;
//画图用
/*Bmin_x -= Width_path / 2;
Bmin_y -= Width_path / 2;
Bmax_y += Width_path;
Bmax_x += Width_path;*/
X = (Bmax_x - Bmin_x) / Width_path;
Y = (Bmax_y - Bmin_y) / Width_path;
}
double distance(Point a, Point b) //两点间距离
{
return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2) + pow((a.z - b.z), 2));
}
//示例一路径生成
void Case1_paths() {
Point s;
//生成离散点,距离为路径宽度
Scheme = 1;
s.x = Bmin_x; s.y = Bmin_y; s.z = 0; s.t = 0;
Z = (Bmax_z - Bmin_z) / (MAX / 2 + MIN / 2) - 1;
for (int i = 0; i < Z; i++) {
if (i == 0) {//第一层
for (int j = 0; j < X; j++) {
s.x += Width_path;
for (int m = 0; m < Y; m++) {
s.y += Width_path;
(i + 1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = Bmin_z + MIN / 2 : s.z = Bmin_z + MAX / 2;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.y = Bmin_y;
}
s.x = Bmin_x;
P.push_back(buffer2);
buffer2.clear();
}
else if (i == Z - 1) {//最后一层
if ((i + 1) % 2 != 0) {//假如为奇数层
for (int j = 0; j < X; j++) {
s.x += Width_path;
for (int m = 0; m < Y; m++) {
s.y += Width_path;
(i + 1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = P[i - 1][m][j].z + MIN / 2 : s.z = P[i - 1][m][j].z + MAX / 2;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.y = Bmin_y;
}
P.push_back(buffer2);
buffer2.clear();
s.x = Bmin_x;
}
else {
for (int j = 0; j < Y; j++) {
s.y += Width_path;
for (int m = 0; m < X; m++) {
s.x += Width_path;
(i + 1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = P[i - 1][m][j].z + MIN / 2 : s.z = P[i - 1][m][j].z + MAX / 2;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.x = Bmin_x;
}
P.push_back(buffer2);
buffer2.clear();
s.y = Bmin_y;
}
}
else {//中间层
if ((i + 1) % 2 != 0) {//假如为奇数层
for (int j = 0; j < X; j++) {
s.x += Width_path;
for (int m = 0; m < Y; m++) {
s.y += Width_path;
(i + 1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = P[i - 1][m][j].z + MIN : s.z = P[i - 1][m][j].z + MAX;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.y = Bmin_y;
}
P.push_back(buffer2);
buffer2.clear();
s.x = Bmin_x;
}
else {
for (int j = 0; j < Y; j++) {
s.y += Width_path;
for (int m = 0; m < X; m++) {
s.x += Width_path;
(i + 1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = P[i - 1][m][j].z + MIN : s.z = P[i - 1][m][j].z + MAX;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.x = Bmin_x;
}
P.push_back(buffer2);
buffer2.clear();
s.y = Bmin_y;
}
}
}
}
//示例二路径生成
void Case2_paths() {
Point s;
Scheme = 2;
s.x = Bmin_x; s.y = Bmin_y; s.z = 0; s.t = 0;
Z = (Bmax_z - Bmin_z) / (MAX / 2 + MIN / 2) - 1;
for (int i = 0; i < Z; i++) {
if (i < N) {//首层
if ((i + 1) % 2 != 0) {
for (int j = 0; j < X; j++) {
s.x += Width_path;//假如为奇数层
for (int m = 0; m < Y; m++) {
s.y += Width_path;
(1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = Bmin_z + MIN : s.z = Bmin_z + MAX;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.y = Bmin_y;
}
P.push_back(buffer2);
buffer2.clear();
s.x = Bmin_x;
}
else {
for (int j = 0; j < Y; j++) {
s.y += Width_path;//假如为偶数层
for (int m = 0; m < X; m++) {
s.x += Width_path;
(1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = P[i - 1][m][j].z + MIN : s.z = P[i - 1][m][j].z + MAX;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.x = Bmin_x;
}
P.push_back(buffer2);
buffer2.clear();
s.y = Bmin_y;
}
}
else if (i >= Z - N) {//末层//假如为奇数层
if ((i + 1) % 2 != 0) {
for (int j = 0; j < X; j++) {
s.x += Width_path;//假如为奇数层
for (int m = 0; m < Y; m++) {
s.y += Width_path;
(1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = P[i - 1][m][j].z + MAX : s.z = P[i - 1][m][j].z + MIN;//不用考虑奇偶层,应该与第一层互补
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.y = Bmin_y;
}
P.push_back(buffer2);
buffer2.clear();
s.x = Bmin_x;
}
else {
for (int j = 0; j < Y; j++) {
s.y += Width_path;
for (int m = 0; m < X; m++) {
s.x += Width_path;
(1 + j / NP + 1 + m / NP + 1) % 2 != 0 ? s.z = P[i - 1][m][j].z + MAX : s.z = P[i - 1][m][j].z + MIN;//不用考虑奇偶层,应该与第一层互补
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.x = Bmin_x;
}
P.push_back(buffer2);
buffer2.clear();
s.y = Bmin_y;
}
}
else {//中间层//假如为奇数层
if ((i + 1) % 2 != 0) {
for (int j = 0; j < X; j++) {
s.x += Width_path;
for (int m = 0; m < Y; m++) {
s.y += Width_path;
s.z = P[i - 1][m][j].z + H;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.y = Bmin_y;
}
P.push_back(buffer2);
buffer2.clear();
s.x = Bmin_x;
}
else {
for (int j = 0; j < Y; j++) {
s.y += Width_path;
for (int m = 0; m < X; m++) {
s.x += Width_path;
s.z = P[i - 1][m][j].z + H;
buffer1.push_back(s);
}
buffer2.push_back(buffer1);
buffer1.clear();
s.x = Bmin_x;
}
P.push_back(buffer2);
buffer2.clear();
s.y = Bmin_y;
}
}
}
}
//判断点是否在多边形内,在true,否false,使用的是引射线法
bool InOrOutPolygon(Point a, vector<Point> polypoint) {
double x0 = a.x;
double y0 = a.y;
int crossings = 0;
int n = polypoint.size();
for (int i = 0; i < n; i++)
{
// 点在两个x之间 且以点垂直y轴向上做射线
double slope = (polypoint[(i + 1 + n) % n].y - polypoint[i].y) / (polypoint[(i + 1 + n) % n].x - polypoint[i].x);
boolean cond1 = (polypoint[i].x <= x0) && (x0 < polypoint[(i + 1 + n) % n].x);
boolean cond2 = (polypoint[(i + 1 + n) % n].x <= x0) && (x0 < polypoint[i].x);
boolean above = (y0 < slope* (x0 - polypoint[i].x) + polypoint[i].y);
if ((cond1 || cond2) && above) crossings++;
}
if (crossings % 2 != 0&&crossings!=0) {
return true;
}
else {
return false;
}
}
//判断生成的离散点是否在模型内
void findIntersect() {
Path way;
Paths layer;//每层路径集合
IntPoint p1;
Point p;
for (int i = 0; i < P.size(); i++) {
printf("求交算法:%.2lf%%\r", i * 100.0 / P.size());
for (int j = 0; j < P[i].size(); j++) {
for (int m = 0; m < P[i][j].size(); m++) {
MyMesh::Normal vf(0, 0, 1);//截面法线
MyMesh::Point pt;//截面上一点
pt.data()[0] = 0; pt.data()[1] = 0; pt.data()[2] = P[i][j][m].z;
IntersectPlane(pt, vf);//生成一层轮廓线,存入coord
for (int k = 0; k < coord.size(); k++)
{
if (coord[k].x != -10000)
{
p1.X = coord[k].x * 100;
p1.Y = coord[k].y * 100;
way.push_back(p1);
}
else
{
layer.push_back(way);
way.clear();
}
}
layer.push_back(way);
way.clear();
coord.clear();
for (int k1 = 0; k1 < layer.size(); k1++)
{
for (int k = 0; k < layer[k1].size(); k++)
{
p.x = double(layer[k1][k].X) / 100;
p.y = double(layer[k1][k].Y) / 100;
shellpath.push_back(p);
}
shellpaths.push_back(shellpath);
shellpath.clear();
}
layer.clear();
for (int k = 0; k < shellpaths.size(); k++) {
if (InOrOutPolygon(P[i][j][m], shellpaths[k])) {
P[i][j][m].b = true;
}
}
shellpaths.clear();
}
}
}
}
void Contour() {
double z = Bmin_z;
for (int i = 0; i < Z; i++) {
z += (MIN + MAX) / 2;
MyMesh::Normal vf(0, 0, 1);//截面法向
MyMesh::Point pt; //截面上一点
pt.data()[0] = 0; pt.data()[1] = 0; pt.data()[2] = z;
IntersectPlane(pt, vf);//生成一层轮廓线,存入coord
model.push_back(coord);
coord.clear();
}
Path way;
Paths layer;//每层路径集合
IntPoint p1;
ClipperOffset co;
Paths solution;
Point p;
for (int i = 0; i < model.size(); i++)//遍历model,第i层
{
for (int j = 0; j < model[i].size(); j++)//遍历model,第i层中的第j个点
{
if (model[i][j].x != -10000)
{
p1.X = model[i][j].x * 100;
p1.Y = model[i][j].y * 100;
way.push_back(p1);
}
else
{
layer.push_back(way);
way.clear();
}
}
layer.push_back(way);
way.clear();
model2.push_back(layer);
layer.clear();
//*********************将最内圈的点赋给shell1***********************************
for (int j = 0; j < model2[i].size(); j++)
{
for (int k = 0; k < model2[i][j].size(); k++)
{
p.x = double(model2[i][j][k].X) / 100;
p.y = double(model2[i][j][k].Y) / 100;
shellpath.push_back(p);
}
shellpaths.push_back(shellpath);
shellpath.clear();
}
shell1.push_back(shellpaths);
shellpaths.clear();
//******************************做偏移***********************************************
printf("%.2lf%%\r", i * 100.0 / model.size());
for (int j = 0; j < model2[i].size(); j++)//此时遍历的是model2,某层里的第j条path做偏移
{
double area = abs(Area(model2[i][j]) / 10000);
co.Clear();
co.AddPath(model2[i][j], jtRound, etClosedPolygon); //设置准备偏移的路径
int times = 1; //偏移次数,即外壳打印的层数
int sh = Extrusion_width * 100; //偏移厚度
for (int a = 0; a < times; a++) //进行偏移
{
co.Execute(solution, sh); //每次偏移的厚度
for (int n = 0; n < solution[0].size(); n++)
{
p.x = double(solution[0][n].X) / 100;
p.y = double(solution[0][n].Y) / 100;
shellpath.push_back(p);
}
//p.x = double(solution[0][0].X) / 100;
//p.y = double(solution[0][0].Y) / 100;
shellpath.push_back(p);
co.Clear();
co.AddPath(solution[0], jtRound, etClosedPolygon); //准备下一次偏移
solution.clear();
shellpaths.push_back(shellpath);
shellpath.clear();
}
shell2.push_back(shellpaths);
shellpaths.clear();
}
//solution.clear();
shell.push_back(shell2);
shell2.clear();
}
}
void Optimization() {
Point s;
//double k1,k2; //斜率
double g;//插值间隔
for (int i = 0; i < P.size(); i++) {//中间插入过渡插值
for (int j = 0; j < P[i].size(); j++) {
for (int m = 1; m < P[i][j].size(); m++) {
if (P[i][j][m].z != P[i][j][m - 1].z) {
//k1 = (P[i][j][m].z - P[i][j][m - 1].z) / Width_path;
//k2 = (P[i][j][m].t - P[i][j][m - 1].t) / Width_path;
Scheme == 1 ? g = 0.025 : g = 0.05;
for (int c = 1; c < 8; c++) {//
P[i][j][m - 1].x == P[i][j][m].x ? s.y = P[i][j][m - 1].y + Width_path / 8 : s.x = P[i][j][m - 1].x + Width_path / 8;
P[i][j][m - 1].x == P[i][j][m].x ? s.x = P[i][j][m - 1].x : s.y = P[i][j][m - 1].y;
P[i][j][m].z > P[i][j][m - 1].z ? s.z = P[i][j][m - 1].z + g : s.z = P[i][j][m - 1].z - g;
//s.z = P[i][j][m - 1].z + g * k1;
//s.t = P[i][j][m - 1].t + 0.1 * k2;
(P[i][j][m - 1].b == true && P[i][j][m].b == true) ? s.b = true : s.b = false;
P[i][j].insert(P[i][j].begin() + m, s);
m += 1;
}
}
}
}
}
}
void Case1() {
Case1_paths();
findIntersect(); //前三个函数切片,产生model
Optimization();
double k = (MAX / 2 - MIN / 2) / Width_path;//存储斜率
double z = (MIN + MAX) / 2;
for (int i = 0; i < P.size(); i++) {
for (int j = 0; j < P[i].size(); j++) {
for (int m = 1; m < P[i][j].size(); m++) {
//首层
if (i == 0) {
P[i][j][m].t = 2 * (Extrusion_width) * (P[i][j][m].z + P[i][j][m - 1].z) / (pow(Width_material, 2) * pi);
}
else if (i == 1) {//第二层
if (P[i][j][m].z != P[i][j][m - 1].z) {
P[i][j][m].t = 2 * (Extrusion_width) * 2 *(P[i][j][m].z - 0.5 + MIN / 2 + P[i][j][m - 1].z - 0.5 + MIN / 2) / (pow(Width_material, 2) * pi);
}
/*if (P[i][j][m].z > P[i][j][m - 1].z) {
P[i][j][m].t = 2 * (Extrusion_width) * (P[i][j][m].z - 0.5 + MIN / 2 + P[i][j][m - 1].z - 0.5 + MIN / 2) / (pow(Width_material, 2) * pi);
}
else if (P[i][j][m].z < P[i][j][m - 1].z) {
P[i][j][m].t = 2 * (Extrusion_width) * ((MAX - 2 * ((m % 8) * (Width_path / 8) * k)) + (MAX - 2 * (((m - 1) % 8) * (Width_path / 8) * k))) / (pow(Width_material, 2) * pi);
}*/
else {
if (P[i][j][m].z == 0.7)
P[i][j][m].t = 4 * (Extrusion_width)*MAX / (pow(Width_material, 2) * pi);
else
P[i][j][m].t = 4 * (Extrusion_width)*MIN / (pow(Width_material, 2) * pi);
}
}//末层(奇数)
else if (i == Z - 1 && (i + 1) % 2 != 0) {
P[i][j][m].t = P[0][j][m].t;
}//末层(偶数)
else if (i == Z - 1 && (i + 1) % 2 == 0) {
P[i][j][m].t = P[1][j][m].t / 2;
}//中间层
else {
if ((i + 1) % 2 != 0) {
P[i][j][m].t = P[0][j][m].t * 2;
}
else {
P[i][j][m].t = P[1][j][m].t;
}
}
}
}
}
}
void Case2() {
Case2_paths();
findIntersect(); //前三个函数切片,产生model
Optimization();
for (int i = 0; i < P.size(); i++) {
for (int j = 0; j < P[i].size(); j++) {
for (int m = 1; m < P[i][j].size(); m++) {
//首层
if (i < N) {
if (i == 0) {
P[i][j][m].t = 2 * (Extrusion_width) * (P[i][j][m].z + P[i][j][m - 1].z) / (pow(Width_material, 2) * pi);
}
else {
P[i][j][m].t = 2 * (Extrusion_width) * (P[i][j][m].z/2 + P[i][j][m - 1].z/2) / (pow(Width_material, 2) * pi);
}
}
//末层(奇数)
else if (i >= Z - N && (i + 1) % 2 != 0) {
P[i][j][m].t = 2 * (Extrusion_width) * (MAX + MIN - P[0][j][m - 1].z + MAX + MIN - P[0][j][m].z) / (pow(Width_material, 2) * pi);
} // 末层(偶数)
else if (i >= Z - N && (i + 1) % 2 == 0) {
P[i][j][m].t = 2 * (Extrusion_width) * (MAX + MIN - (P[1][j][m - 1].z - H) + MAX + MIN - (P[1][j][m].z - H)) / (pow(Width_material, 2) * pi);
}//中间层
else {
P[i][j][m].t = 4 * (Extrusion_width) * H / (pow(Width_material, 2) * pi);
}
}
}
}
}
void Paths_Optimization() {
Point s;
for (int i = 0; i < P.size(); i++) {
for (int j = 0; j < P[i].size(); j++) {
for (int m = 0; m < P[i][j].size(); m++) {
if (P[i][j][m].b == true) {
buffer1.push_back(P[i][j][m]);
}
else if (P[i][j][m].b == false && buffer1.size() != 0) {
buffer2.push_back(buffer1);
buffer1.clear();
}
}
if (buffer1.size() != 0) {
buffer2.push_back(buffer1);
buffer1.clear();
}
}
paths.push_back(buffer2);
buffer2.clear();
}
for (int i = 0; i < paths.size(); i++) {
for (int j = 0; j < paths[i].size(); j++) {
if (i % 2 == 0) {
paths[i][j][0].y -= Width_path / 2;
paths[i][j][paths[i][j].size() - 1].y += Width_path / 2;
}
else {
paths[i][j][0].x -= Width_path / 2;
paths[i][j][paths[i][j].size() - 1].x += Width_path / 2;
}
}
}
for (int i = 0; i < paths.size(); i++) {
for (int j = 0; j < paths[i].size(); j++) {
if (j % 2 == 0) {
reverse(paths[i][j].begin(), paths[i][j].end());
}
}
}
double arcs, s_begin, s_end;
for (int i = 0; i < paths.size(); i++) {
for (int j = 0; j < paths[i].size() - 1; j++) {
arcs = distance(paths[i][j][paths[i][j].size() - 1], paths[i][j + 1][0]);
for (int m = j + 1; m < paths[i].size(); m++) {
s_begin = distance(paths[i][j][paths[i][j].size() - 1], paths[i][m][0]);
s_end = distance(paths[i][j][paths[i][j].size() - 1], paths[i][m][paths[i][m].size() - 1]);
if (s_begin < arcs && s_begin < s_end) {
arcs = s_begin;
buffer1 = paths[i][m];
paths[i][m] = paths[i][j + 1];
paths[i][j + 1] = buffer1;
buffer1.clear();
}
else if (s_end < arcs && s_end < s_begin) {
arcs = s_end;
reverse(paths[i][m].begin(), paths[i][m].end());
buffer1 = paths[i][m];
paths[i][m] = paths[i][j + 1];
paths[i][j + 1] = buffer1;
buffer1.clear();
}
}
}
}
}
void ContourGcodePrint() {
FILE* fp;
errno_t err; //判断此文件流是否存在 存在返回1
err = fopen_s(&fp, "ls2.gcode", "a"); //若return 1 , 则将指向这个文件的文件流给
double t = (4 * ((MIN + MAX) / 2) * Extrusion_width) / (pow(Width_material, 2) * pi);
double E = 0;
double r;//回抽
int L = 50;//偏移量
fprintf(fp, ";FLAVOR:Marlin\n");
fprintf(fp, ";Generated with Cura_SteamEngine 4.10.0\n");
fprintf(fp, "M140 S50\n");
fprintf(fp, "M105\n");
fprintf(fp, "M190 S50\n");
fprintf(fp, "M104 S210\n");
fprintf(fp, "M105\n");
fprintf(fp, "M109 S210\n");
fprintf(fp, "M82 ;absolute extrusion mode\n");
fprintf(fp, "M201 X500.00 Y500.00 Z100.00 E5000.00 ;Setup machine max acceleration\n");
fprintf(fp, "M203 X500.00 Y500.00 Z10.00 E50.00 ;Setup machine max feedrate\n");
fprintf(fp, "M204 P500.00 R1000.00 T500.00 ;Setup Print/Retract/Travel acceleration\n");
fprintf(fp, "M205 X8.00 Y8.00 Z0.40 E5.00 ;Setup Jerk\n");
fprintf(fp, "M220 S100 ;Reset Feedrate\n");
fprintf(fp, "M221 S100 ;Reset Flowrate\n");
fprintf(fp, "G28 ;Home\n");
fprintf(fp, "G92 E0\n");
fprintf(fp, "G92 E0\n");
fprintf(fp, "G1 F2700 E-5\n");
fprintf(fp, "M107\n");
double z = Bmin_z;
for (int i = 0; i < shell1.size(); i++) {
fprintf(fp, ";LAYER:%d\n", i);
fprintf(fp, "M73 P%.f\n", float((100 * i) / paths.size()));
fprintf(fp, ";TYPE:OUTLINE\n");
z += (MIN + MAX) / 2;
fprintf(fp, "G0 F4800 X%f Y%f Z%f\n", shell1[i][0][0].x + L, shell1[i][0][0].y + L, z);
if (i > 0) {
fprintf(fp, "G1 F2400 E%f\n", E += 1);
}
for (int j = 0; j < shell1[i].size(); j++) {
fprintf(fp, "G0 X%f Y%f Z%f\n", shell1[i][j][0].x + L, shell1[i][j][0].y + L, z);
fprintf(fp, "G1 F1200 X%f Y%f Z%f E%f\n", shell1[i][j][1].x + L, shell1[i][j][1].y + L,z, E += distance(shell1[i][j][0], shell1[i][j][1]) * t);
for (int k = 2; k < shell1[i][j].size(); k++)
{
fprintf(fp, "G1 X%.3f Y%.3f Z%f E%.5f\n", shell1[i][j][k].x + L, shell1[i][j][k].y + L,z, E += distance(shell1[i][j][k - 1], shell1[i][j][k]) * t);
}
for (int k = 0; k < shell[i][j].size(); k++)
{
fprintf(fp, "G0 F4800 X%.3f Y%.3f Z%.1f\n", shell[i][j][k][0].x + L, shell[i][j][k][0].y + L, z);
fprintf(fp, "G1 F1200 X%.3f Y%.3f Z%f E%.5f\n", shell[i][j][k][1].x + L, shell[i][j][k][1].y + L,z, E += distance(shell[i][j][k][0], shell[i][j][k][1]) * t);
for (int m = 2; m < shell[i][j][k].size(); m++)
{
fprintf(fp, "G1 X%.3f Y%.3f Z%f E%.5f\n", shell[i][j][k][m].x + L, shell[i][j][k][m].y + L,z, E += distance(shell[i][j][k][m - 1], shell[i][j][k][m]) * t);
}
}
}
fprintf(fp, ";TYPE:FILL\n");
if (i <paths.size() && paths.size() != 0) {
for (int j = 0; j < paths[i].size(); j++) {
r = E - 1;
fprintf(fp, "G1 F2400 E%f\n", r);//回抽一定距离,避免拉丝
fprintf(fp, "G0 X%f Y%f Z%f\n", paths[i][j][0].x + L, paths[i][j][0].y + L, paths[i][j][0].z);
fprintf(fp, "G1 F2400 E%f\n", E);
for (int m = 1; m < paths[i][j].size(); m++) {