| import { mergeVertices, updateIndices } from "./MergeVerticesByDistance"; |
| import { truncateValues } from "./Math"; |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function mergeChunks( |
| chunkDatas: { |
| vertices: [number, number, number][]; |
| indices: number[]; |
| colors: [number, number, number][]; |
| normals: [number, number, number][]; |
| shapeChunkContent: string; |
| }[], |
| mergeCutoff: number |
| ) { |
| |
| const allVertices: [number, number, number][] = []; |
| const allIndices: number[] = []; |
| const allColors: [number, number, number][] = []; |
| const allNormals: [number, number, number][] = []; |
| let vertexOffset = 0; |
|
|
| |
| for (const chunk of chunkDatas) { |
| |
| allVertices.push(...chunk.vertices); |
| allColors.push(...chunk.colors); |
| if (chunk.normals.length > 0) { |
| allNormals.push(...chunk.normals); |
| } |
|
|
| |
| for (let i = 0; i < chunk.indices.length; i++) { |
| const idx = chunk.indices[i]; |
| allIndices.push(idx === -1 ? -1 : idx + vertexOffset); |
| } |
|
|
| |
| vertexOffset += chunk.vertices.length; |
| } |
|
|
| |
| const { mergedVertices, mergedColors, mapping } = mergeVertices( |
| allVertices, |
| allColors, |
| mergeCutoff |
| ); |
|
|
| |
| const mergedIndices = updateIndices(allIndices, mapping); |
|
|
| |
| const mergedNormals = |
| allNormals.length > 0 ? allNormals.map(truncateValues) : []; |
|
|
| return { |
| vertices: mergedVertices, |
| indices: mergedIndices, |
| colors: mergedColors, |
| normals: mergedNormals, |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function parseVRML(fileContent: string): { |
| chunkDatas: { |
| vertices: [number, number, number][]; |
| indices: number[]; |
| colors: [number, number, number][]; |
| normals: [number, number, number][]; |
| shapeChunkContent: string; |
| }[]; |
| firstChunkContent: string | undefined; |
| } { |
| |
| const shapeChunkContents = fileContent.split("Shape {"); |
| const firstChunkContent = shapeChunkContents.shift(); |
|
|
| let chunkDatas = []; |
| for (const chunkIdx in shapeChunkContents) { |
| const shapeChunkContent = shapeChunkContents[chunkIdx]; |
|
|
| const vertexRegex = /point \[([\s\S]*?)\]/; |
| const indexRegex = /coordIndex \[([\s\S]*?)\]/; |
| const colorRegex = /color \[([\s\S]*?)\]/; |
| const normalRegex = /vector \[([\s\S]*?)\]/; |
|
|
| const vertexBlock = shapeChunkContent.match(vertexRegex); |
| const indexBlock = shapeChunkContent.match(indexRegex); |
| const colorBlock = shapeChunkContent.match(colorRegex); |
| const normalBlock = shapeChunkContent.match(normalRegex); |
|
|
| if (!vertexBlock || !indexBlock) { |
| console.error( |
| "No vertices or indices found in chunk " + |
| chunkIdx.toString() + |
| " VRML file." |
| ); |
| continue; |
| } |
|
|
| console.log(`Processing chunk ${chunkIdx}`); |
|
|
| |
| const vertexString = vertexBlock[1]; |
| if (!vertexString) { |
| console.error( |
| "No vertices found in chunk " + |
| chunkIdx.toString() + |
| " VRML file." |
| ); |
| continue; |
| } |
| const vertexNumberStrings = vertexString.match( |
| /-?\d+(\.\d+)?([eE][+-]?\d+)?/g |
| ); |
| if (!vertexNumberStrings) { |
| console.error( |
| "No vertex numbers found in chunk " + |
| chunkIdx.toString() + |
| " VRML file." |
| ); |
| continue; |
| } |
| const vertexNumbers = vertexNumberStrings.map(Number); |
| if (vertexNumbers.length % 3 !== 0) { |
| console.error("Vertex data is not a multiple of 3."); |
| process.exit(1); |
| } |
| const vertices: [number, number, number][] = []; |
| for (let i = 0; i < vertexNumbers.length; i += 3) { |
| vertices.push([ |
| vertexNumbers[i], |
| vertexNumbers[i + 1], |
| vertexNumbers[i + 2], |
| ]); |
| } |
|
|
| |
| const indexString = indexBlock[1]; |
| const indexNumberStrings = indexString.match(/-?\d+/g); |
| if (!indexNumberStrings) { |
| console.error( |
| "No index numbers found in chunk " + |
| chunkIdx.toString() + |
| " VRML file." |
| ); |
| continue; |
| } |
| const indexNumbers = indexNumberStrings.map(Number); |
| const indices = indexNumbers; |
|
|
| |
| let colors: [number, number, number][] = []; |
| if (colorBlock) { |
| const colorString = colorBlock[1]; |
| const colorNumberStrings = colorString.match( |
| /-?\d+(\.\d+)?([eE][+-]?\d+)?/g |
| ); |
| if (!colorNumberStrings) { |
| console.error( |
| "No color numbers found in chunk " + |
| chunkIdx.toString() + |
| " VRML file." |
| ); |
| continue; |
| } |
| const colorNumbers = colorNumberStrings.map(Number); |
| if (colorNumbers.length % 3 !== 0) { |
| console.error("Color data is not a multiple of 3."); |
| process.exit(1); |
| } |
| for (let i = 0; i < colorNumbers.length; i += 3) { |
| colors.push([ |
| colorNumbers[i], |
| colorNumbers[i + 1], |
| colorNumbers[i + 2], |
| ]); |
| } |
| } else { |
| |
| colors = Array(vertices.length).fill([1.0, 1.0, 1.0]); |
| } |
|
|
| |
| const normals: [number, number, number][] = []; |
| if (normalBlock) { |
| const normalString = normalBlock[1]; |
| const normalNumberStrings = normalString.match( |
| /-?\d+(\.\d+)?([eE][+-]?\d+)?/g |
| ); |
| if (!normalNumberStrings) { |
| console.error( |
| "No normal numbers found in chunk " + |
| chunkIdx.toString() + |
| " VRML file." |
| ); |
| continue; |
| } |
| const normalNumbers = normalNumberStrings.map(Number); |
| if (normalNumbers.length % 3 !== 0) { |
| console.error("Normal data is not a multiple of 3."); |
| process.exit(1); |
| } |
| for (let i = 0; i < normalNumbers.length; i += 3) { |
| normals.push([ |
| normalNumbers[i], |
| normalNumbers[i + 1], |
| normalNumbers[i + 2], |
| ]); |
| } |
| } |
|
|
| |
| chunkDatas.push({ |
| vertices, |
| indices, |
| colors, |
| normals, |
| shapeChunkContent, |
| }); |
| } |
|
|
| if (chunkDatas.length > 1) { |
| const mergedData = mergeChunks(chunkDatas, 0.001); |
| chunkDatas = [chunkDatas[0]]; |
| chunkDatas[0].vertices = mergedData.vertices; |
| chunkDatas[0].indices = mergedData.indices; |
| chunkDatas[0].colors = mergedData.colors; |
| chunkDatas[0].normals = mergedData.normals; |
|
|
|
|
| |
| |
| |
| } |
| |
|
|
| return { chunkDatas, firstChunkContent }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function replaceVerticesAndIndicesInVRML( |
| fileContent: string, |
| mergedVertices: number[][], |
| mergedColors: number[][], |
| mergedNormals: number[][], |
| updatedIndices: number[] |
| ): string { |
| const vertexString = mergedVertices.map((v) => v.join(" ")).join(",\n"); |
| const indexString = updatedIndices.join(",\n"); |
| const colorString = mergedColors.map((c) => c.join(" ")).join(",\n"); |
| const normalString = |
| mergedNormals.length > 0 |
| ? mergedNormals.map((n: number[]) => n.join(" ")).join(",\n") |
| : ""; |
|
|
| let updatedContent = fileContent.replace( |
| /point \[([\s\S]*?)\]/, |
| `point [\n${vertexString}\n]` |
| ); |
| updatedContent = updatedContent.replace( |
| /coordIndex \[([\s\S]*?)\]/, |
| `coordIndex [\n${indexString}\n]` |
| ); |
|
|
| if (fileContent.match(/color \[([\s\S]*?)\]/)) { |
| updatedContent = updatedContent.replace( |
| /color \[([\s\S]*?)\]/, |
| `color [\n${colorString}\n]` |
| ); |
| } else { |
| |
| updatedContent = updatedContent.replace( |
| /(coordIndex \[[\s\S]*?\])/, |
| `$1\ncolor [\n${colorString}\n]` |
| ); |
| } |
|
|
| if (fileContent.match(/vector \[([\s\S]*?)\]/)) { |
| updatedContent = updatedContent.replace( |
| /vector \[([\s\S]*?)\]/, |
| `vector [\n${normalString}\n]` |
| ); |
| } |
|
|
| return updatedContent; |
| } |
|
|