/usr/src/castle-game-engine-6.4/x3d/x3dloadinternalstl.pas is in castle-game-engine-src 6.4+dfsg1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | {
Copyright 2017-2017 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Load 3D models in the STL format (@link(LoadSTL)). }
unit X3DLoadInternalSTL;
interface
uses X3DNodes;
{ Load 3D model in the STL format, converting it to an X3D nodes graph.
This routine is internally used by the @link(Load3D) to load an STL file.
See https://en.wikipedia.org/wiki/STL_%28file_format%29 for
more information about STL. }
function LoadSTL(const URL: string): TX3DRootNode;
implementation
uses SysUtils, Classes,
CastleClassUtils, CastleVectors, CastleUtils, CastleDownload, CastleTriangles,
CastleLog, CastleStreamUtils;
{ Load STL text (ASCII) variation. }
procedure LoadSTLText(const Stream: TStream;
const Coordinate, Normal: TVector3List);
procedure AddTriangle(const Coordinate, Normal: TVector3List;
NormalVector: TVector3; const Triangle: TTriangle3);
begin
{ if the STL file specifies zero vector, calculate it }
if NormalVector.IsPerfectlyZero then
NormalVector := Triangle.Normal;
{ add 3 times the same NormalVector.
See TODO about TriangleSet.NormalPerVertex lower in this file. }
Normal.Add(NormalVector);
Normal.Add(NormalVector);
Normal.Add(NormalVector);
Coordinate.Add(Triangle.Data[0]);
Coordinate.Add(Triangle.Data[1]);
Coordinate.Add(Triangle.Data[2]);
end;
var
TextReader: TTextReader;
{ Read a word expecting the ExpectedValue. Returns @true if found expected,
otherwise returns @false (and the warning is already emitted). }
function ReadExpect(const ExpectedValue: string): boolean;
var
S: string;
begin
S := TextReader.Read;
Result := S = ExpectedValue;
if not Result then
begin
if S = '' then
WritelnWarning('STL', 'Unexpected end of the file, expected "' + ExpectedValue + '"')
else
WritelnWarning('STL', 'Unexpected word "' + S + '" in file, expected "' + ExpectedValue + '"');
end;
end;
var
S: string;
NormalVector: TVector3;
Triangle: TTriangle3;
I: Integer;
begin
TextReader := TTextReader.Create(Stream, false);
try
TextReader.Readln; // read header line
repeat
{ read triangle }
S := TextReader.Read;
if S = 'facet' then
begin
{ S = facet -> new triangle }
if not ReadExpect('normal') then Exit;
NormalVector := TextReader.ReadVector3;
if not ReadExpect('outer') then Exit;
if not ReadExpect('loop') then Exit;
for I := 0 to 2 do
begin
if not ReadExpect('vertex') then Exit;
Triangle.Data[I] := TextReader.ReadVector3;
end;
AddTriangle(Coordinate, Normal, NormalVector, Triangle);
if not ReadExpect('endloop') then Exit;
if not ReadExpect('endfacet') then Exit;
end else
begin
{ S = anything else -> end of reading }
if S <> 'endsolid' then
WritelnWarning('STL', 'Unexpected word "' + S + '" in file, expected "facet" or "endsolid"');
Exit;
end;
until false;
finally FreeAndNil(TextReader) end;
end;
{ Load STL binary variation. }
procedure LoadSTLBinary(const Stream: TStream;
const Coordinate, Normal: TVector3List);
var
RestOfHeader: array [0..79 - 5] of char;
TriangleCount: LongWord;
NormalVector: TVector3;
Triangle: TTriangle3;
I, J: Integer;
TriangleAttribute: Word;
begin
{ read the rest of (ignored) binary header with ReadBuffer
(to work with streams that don't support seeking). }
Stream.ReadBuffer(RestOfHeader, 80 - 5);
Stream.ReadLE(TriangleCount);
Coordinate.Count := TriangleCount * 3;
Normal.Count := TriangleCount * 3;
for I := 0 to TriangleCount - 1 do
begin
for J := 0 to 2 do
Stream.ReadLE(NormalVector.Data[J]);
for J := 0 to 2 do
begin
Stream.ReadLE(Triangle.Data[J].Data[0]);
Stream.ReadLE(Triangle.Data[J].Data[1]);
Stream.ReadLE(Triangle.Data[J].Data[2]);
end;
{ we read and ignore for now the TriangleAttribute,
see https://en.wikipedia.org/wiki/STL_%28file_format%29 for it's meaning }
Stream.ReadLE(TriangleAttribute);
{ Add the triangle to Coordinate and Normal arrays now.
We could use the same AddTriangle procedure as the LoadSTLText,
but it would be less efficient -- it causes a resize when adding each triangle.
Here, we know the triangle count beforehand. }
{ if the STL file specifies zero vector, calculate it }
if NormalVector.IsPerfectlyZero then
NormalVector := Triangle.Normal;
Coordinate[I * 3 ] := Triangle.Data[0];
Coordinate[I * 3 + 1] := Triangle.Data[1];
Coordinate[I * 3 + 2] := Triangle.Data[2];
Normal[I * 3 ] := NormalVector;
Normal[I * 3 + 1] := NormalVector;
Normal[I * 3 + 2] := NormalVector;
end;
end;
function LoadSTL(const URL: string): TX3DRootNode;
var
Header: array [0..4] of char;
Stream: TStream;
Shape: TShapeNode;
TriangleSet: TTriangleSetNode;
Coordinate: TCoordinateNode;
Normal: TNormalNode;
begin
Stream := Download(URL, []);
try
Result := TX3DRootNode.Create('', URL);
try
{ setup common X3D nodes }
Coordinate := TCoordinateNode.Create('', URL);
Normal := TNormalNode.Create('', URL);
{ The TriangleSet is perfect for STL geometry, see
http://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/rendering.html#TriangleSet
Just a list of vertexes, each 3 vertexes make a triangle. }
TriangleSet := TTriangleSetNode.Create('', URL);
TriangleSet.Coord := Coordinate;
{ TODO: NormalPerVertex := true on TriangleSet not supported (would allow
to be more compact) }
TriangleSet.NormalPerVertex := false;
TriangleSet.Normal := Normal;
Shape := TShapeNode.Create('', URL);
{ assign some Material only to make it lit }
Shape.Material := TMaterialNode.Create('', URL);
Shape.Geometry := TriangleSet;
Result.AddChildren(Shape);
{ actually read the file, filling Coordinate and Normal nodes }
Stream.ReadBuffer(Header, 5);
if Header = 'solid' then
LoadSTLText(Stream, Coordinate.FdPoint.Items, Normal.FdVector.Items)
else
LoadSTLBinary(Stream, Coordinate.FdPoint.Items, Normal.FdVector.Items);
except FreeAndNil(Result); raise end;
finally FreeAndNil(Stream) end;
end;
end.
|