forked from Ultimaker/CuraEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layerPart.h
99 lines (87 loc) · 4.11 KB
/
layerPart.h
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
/** Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License */
#ifndef LAYERPART_H
#define LAYERPART_H
/*
The layer-part creation step is the first step in creating actual useful data for 3D printing.
It takes the result of the Slice step, which is an unordered list of polygons, and makes groups of polygons,
each of these groups is called a "part", which sometimes are also known as "islands". These parts represent
isolated areas in the 2D layer with possible holes.
Creating "parts" is an important step, as all elements in a single part should be printed before going to another part.
And all every bit inside a single part can be printed without the nozzle leaving the boundery of this part.
It's also the first step that stores the result in the "data storage" so all other steps can access it.
*/
void createLayerWithParts(SliceLayer& storageLayer, SlicerLayer* layer, int unionAllType)
{
ClipperLib::Polygons polyList;
for(unsigned int i=0; i<layer->polygonList.size(); i++)
{
ClipperLib::Polygon p;
p.push_back(layer->polygonList[i][0]);
for(unsigned int j=1; j<layer->polygonList[i].size(); j++)
{
p.push_back(layer->polygonList[i][j]);
}
if ((unionAllType & 0x02) && ClipperLib::Orientation(p))
ClipperLib::ReversePolygon(p);
polyList.push_back(p);
}
ClipperLib::ExPolygons resultPolys;
ClipperLib::Clipper clipper;
clipper.AddPolygons(polyList, ClipperLib::ptSubject);
if (unionAllType)
clipper.Execute(ClipperLib::ctUnion, resultPolys, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
else
clipper.Execute(ClipperLib::ctUnion, resultPolys);
for(unsigned int i=0; i<resultPolys.size(); i++)
{
storageLayer.parts.push_back(SliceLayerPart());
storageLayer.parts[i].outline.push_back(resultPolys[i].outer);
for(unsigned int j=0; j<resultPolys[i].holes.size(); j++)
{
storageLayer.parts[i].outline.push_back(resultPolys[i].holes[j]);
}
storageLayer.parts[i].boundaryBox.calculate(storageLayer.parts[i].outline);
}
}
void createLayerParts(SliceVolumeStorage& storage, Slicer* slicer, int unionAllType)
{
for(unsigned int layerNr = 0; layerNr < slicer->layers.size(); layerNr++)
{
storage.layers.push_back(SliceLayer());
createLayerWithParts(storage.layers[layerNr], &slicer->layers[layerNr], unionAllType);
//LayerPartsLayer(&slicer->layers[layerNr])
}
}
void dumpLayerparts(SliceDataStorage& storage, const char* filename)
{
FILE* out = fopen(filename, "w");
fprintf(out, "<!DOCTYPE html><html><body>");
Point3 modelSize = storage.modelSize;
Point3 modelMin = storage.modelMin;
for(unsigned int volumeIdx=0; volumeIdx<storage.volumes.size(); volumeIdx++)
{
for(unsigned int layerNr=0;layerNr<storage.volumes[volumeIdx].layers.size(); layerNr++)
{
fprintf(out, "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" style=\"width: 500px; height:500px\">\n");
SliceLayer* layer = &storage.volumes[volumeIdx].layers[layerNr];
for(unsigned int i=0;i<layer->parts.size();i++)
{
SliceLayerPart* part = &layer->parts[i];
for(unsigned int j=0;j<part->outline.size();j++)
{
fprintf(out, "<polygon points=\"");
for(unsigned int k=0;k<part->outline[j].size();k++)
fprintf(out, "%f,%f ", float(part->outline[j][k].X - modelMin.x)/modelSize.x*500, float(part->outline[j][k].Y - modelMin.y)/modelSize.y*500);
if (j == 0)
fprintf(out, "\" style=\"fill:gray; stroke:black;stroke-width:1\" />\n");
else
fprintf(out, "\" style=\"fill:red; stroke:black;stroke-width:1\" />\n");
}
}
fprintf(out, "</svg>\n");
}
}
fprintf(out, "</body></html>");
fclose(out);
}
#endif//LAYERPART_H