MCPcopy Index your code
hub / github.com/processing/p5.js / parseASCIISTL

Function parseASCIISTL

src/webgl/loading.js:830–965  ·  view source on GitHub ↗

* @private * ASCII STL file starts with `solid 'nameOfFile'` * Then contain the normal of the face, starting with `facet normal` * Next contain a keyword indicating the start of face vertex, `outer loop` * Next comes the three vertex, starting with `vertex x y z` * Vertices ends with

(model, lines)

Source from the content-addressed store, hash-verified

828 * The end of the file is indicated by `endsolid`
829 */
830 function parseASCIISTL(model, lines) {
831 let state = '';
832 let curVertexIndex = [];
833 let newNormal, newVertex;
834
835 for (let iterator = 0; iterator < lines.length; ++iterator) {
836 const line = lines[iterator].trim();
837 const parts = line.split(' ');
838
839 for (
840 let partsiterator = 0;
841 partsiterator < parts.length;
842 ++partsiterator
843 ) {
844 if (parts[partsiterator] === '') {
845 // Ignoring multiple whitespaces
846 parts.splice(partsiterator, 1);
847 }
848 }
849
850 if (parts.length === 0) {
851 // Remove newline
852 continue;
853 }
854
855 switch (state) {
856 case '': // First run
857 if (parts[0] !== 'solid') {
858 // Invalid state
859 console.error(line);
860 console.error(`Invalid state "${parts[0]}", should be "solid"`);
861 return;
862 } else {
863 state = 'solid';
864 }
865 break;
866
867 case 'solid': // First face
868 if (parts[0] !== 'facet' || parts[1] !== 'normal') {
869 // Invalid state
870 console.error(line);
871 console.error(
872 `Invalid state "${parts[0]}", should be "facet normal"`
873 );
874 return;
875 } else {
876 // Push normal for first face
877 newNormal = new Vector(
878 parseFloat(parts[2]),
879 parseFloat(parts[3]),
880 parseFloat(parts[4])
881 );
882 model.vertexNormals.push(newNormal, newNormal, newNormal);
883 state = 'facet normal';
884 }
885 break;
886
887 case 'facet normal': // After normal is defined

Callers 1

parseSTLFunction · 0.85

Calls 3

trimMethod · 0.80
splitMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected