Xinqi Bao's Git
5 #include "BufferReadBinary.hh"
6 #include "Configurations.hh"
9 Stl::Stl() {} // Have to call the function read() later
10 Stl::Stl(std::string
& stl_target
, bool isBinary
)
12 // TODO: if the stl file does not exist
13 read(stl_target
, isBinary
);
16 double Stl::getZmax() const { return zmax
; }
17 double Stl::getZmin() const { return zmin
; }
19 int Stl::read(string
& stl_target
, bool isBinary
)
21 // TODO: read for ASCII and binary
24 numTriangles
= readBinary(stl_target
);
26 numTriangles
= readASCII(stl_target
);
28 if (needMoveToCenter
) moveToCenter();
33 // see stl format for ASCII file
34 int Stl::readASCII(string
& fileTarget
)
36 ifstream
f(fileTarget
);
37 streampos pos
= f
.tellg();
38 f
.seekg(0, ios_base::end
);
41 triangles
.reserve(len
/ 150);
44 if (!strcmp(s,"facet")) {
45 triangles.push_back(Triangle(f));
47 }*/ //old version for read stl
49 while (f
.peek() != 'e') triangles
.push_back(Triangle(f
));
51 return triangles
.size();
54 // see stl format for binary file
55 // will much faster than read in by ASCII file
56 // and using BufferReadBinary.o to reduce the I/O time
57 int Stl::readBinary(string
& fileTarget
)
59 BufferReadBinary
buffer(fileTarget
);
60 // a number in the stl file which means total number for triangles
61 uint32_t numTrianglesInFile
= buffer
.getNumTrianglesInFile();
62 triangles
.reserve(numTrianglesInFile
);
63 while (numTrianglesInFile
)
65 int numTrianglesInBlock
= buffer
.getNumTrianglesInBlock();
66 numTrianglesInFile
-= numTrianglesInBlock
;
67 while (numTrianglesInBlock
)
69 triangles
.push_back(Triangle(buffer
));
70 numTrianglesInBlock
--;
74 return triangles
.size();
77 // By using more memory to speed up the program
78 // After assign the triangles, no need to check triangles which is out of the
80 void Stl::assignTriangles()
82 double invDz
= 1.0 / dzEachLevel
;
83 numLevels
= (zmax
- zmin
) * invDz
+ 1;
84 levels
.reserve(numLevels
);
85 int meanTrianglesPerLevel
= triangles
.size() / numLevels
* 1.5;
86 for (int i
= 0; i
< numLevels
; i
++)
88 levels
.push_back(vector
<Triangle
>());
89 levels
.back().reserve(meanTrianglesPerLevel
);
91 for (const auto& t
: triangles
)
104 zmint
= zmint
< t
.v3
.z
? zmint
: t
.v3
.z
;
105 zmaxt
= zmaxt
> t
.v3
.z
? zmaxt
: t
.v3
.z
;
106 for (int slice
= int((zmint
- zmin
) * invDz
);
107 slice
<= int((zmaxt
- zmin
) * invDz
); slice
++)
108 levels
[slice
].push_back(t
);
111 // triangles.~vector();
114 // set the minimum and maximum of x, y and z, can be used by latter optimization
115 void Stl::setMinAndMax()
117 if (triangles
.empty()) return;
118 xmin
= xmax
= triangles
.front().v1
.x
;
119 ymin
= ymax
= triangles
.front().v1
.y
;
120 zmin
= zmax
= triangles
.front().v1
.z
;
122 for (const auto& t
: triangles
)
124 double xmint
= t
.v1
.x
, xmaxt
= t
.v1
.x
;
125 double ymint
= t
.v1
.y
, ymaxt
= t
.v1
.y
;
126 double zmint
= t
.v1
.z
, zmaxt
= t
.v1
.z
;
134 else if (t
.v3
.x
> xmaxt
)
143 else if (t
.v3
.y
> ymaxt
)
152 else if (t
.v3
.z
> zmaxt
)
155 if (xmint
< xmin
) xmin
= xmint
;
156 if (xmaxt
> xmax
) xmax
= xmaxt
;
157 if (ymint
< ymin
) ymin
= ymint
;
158 if (ymaxt
> ymax
) ymax
= ymaxt
;
159 if (zmint
< zmin
) zmin
= zmint
;
160 if (zmaxt
> zmax
) zmax
= zmaxt
;
164 // Sometime the model does not locate at a great position
165 // By the setted center point of printer table, move the model to a nice place
166 void Stl::moveToCenter()
168 double mvX
= centerX
- (xmax
+ xmin
) / 2;
169 double mvY
= centerY
- (ymax
+ ymin
) / 2;
170 double mvZ
= centerZ
- zmin
;
172 for (auto& t
: triangles
)
195 vector
<Triangle
>& Stl::getLevel(double z
)
197 return levels
.at(int(z
- zmin
) / dzEachLevel
);
201 { // need to comment "triangles.clear();" in assignTriangles function
202 cout
<< "number of triangles: " << triangles
.size() << '\n';
203 for (int i
= 0; i
< triangles
.size(); i
++)
204 cout
<< "# " << i
<< " :\n" << triangles
.at(i
) << '\n';