private StlTriangle ReadTriangle() { StlTriangle stlTriangle = null; if (_isAscii) { switch (PeekToken()) { case "facet": AdvanceToken(); SwallowToken("normal"); var normal = new StlNormal(ConsumeNumberToken(), ConsumeNumberToken(), ConsumeNumberToken()); SwallowToken("outer"); SwallowToken("loop"); SwallowToken("vertex"); var v1 = ConsumeVertexToken(); SwallowToken("vertex"); var v2 = ConsumeVertexToken(); SwallowToken("vertex"); var v3 = ConsumeVertexToken(); SwallowToken("endloop"); SwallowToken("endfacet"); stlTriangle = new StlTriangle(normal, v1, v2, v3); break; case "endsolid": return(null); default: throw new StlReadException("Unexpected token " + PeekToken()); } } return(stlTriangle); }
public List <StlTriangle> ReadTriangles() { var triangles = new List <StlTriangle>(); if (_isAscii) { var t = ReadTriangle(); while (t != null) { EnsureCorrectNormal(t); triangles.Add(t); t = ReadTriangle(); } } else { for (uint i = 0; i < _triangleCount; i++) { // normal should equal (v3-v2)x(v1-v1) var normal = new StlNormal(ReadFloatBinary(), ReadFloatBinary(), ReadFloatBinary()); var v1 = ReadVertexBinary(); var v2 = ReadVertexBinary(); var v3 = ReadVertexBinary(); _binReader.ReadUInt16(); // attribute byte count; garbage value var t = new StlTriangle(normal, v1, v2, v3); EnsureCorrectNormal(t); triangles.Add(t); } } return(triangles); }
private static string NormalToString(StlNormal normal) { return ($"{normal.X.ToString(FloatFormat)} {normal.Y.ToString(FloatFormat)} {normal.Z.ToString(FloatFormat)}"); }