File size: 4,408 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * VRML file processing utilities for parsing, modifying,
 * and reconstructing VRML content during mesh optimization.
 */

import { truncateValues } from "./Math";
import { mergeVertices, updateIndices } from "./MergeVerticesByDistance";
import { parseVRML, replaceVerticesAndIndicesInVRML } from "./ParseVRML";
import { simplifyMesh } from "./QEM";

// Helper function to count faces
/**
 * Count the number of faces in a mesh.
 * 
 * @param {number[]} indices  The indices of the mesh.
 * @returns {number}  The number of faces in the mesh.
 */
function countFaces(indices: number[]): number {
    return indices.filter((index) => index === -1 && index !== undefined)
        .length;
}

/**
 * Process a VRML file by merging vertices, simplifying the mesh, and replacing
 * the vertices and indices in the VRML content.
 *
 * @param {string}  content           The content of the VRML file.
 * @param {number}  mergeCutoff       The distance cutoff for merging vertices.
 * @param {number}  reductionFraction The fraction of vertices to reduce to.
 * @returns {Promise<string>}  The processed VRML content.
 */
export async function processVRML(
    content: string,
    mergeCutoff: number,
    reductionFraction: number
): Promise<string> {
    const { chunkDatas, firstChunkContent } = parseVRML(content);

    const newVRMLContents: string[] = [];

    for (const chunkDataIdx in chunkDatas) {
        const chunkData = chunkDatas[chunkDataIdx];
        const { vertices, indices, colors, normals, shapeChunkContent } =
            chunkData;

        console.log("Original mesh:");
        console.log(`  Vertices: ${vertices.length}`);
        console.log(`  Faces: ${countFaces(indices)}`);

        // Merge vertices by distance and color
        const { mergedVertices, mergedColors, mapping } = mergeVertices(
            vertices,
            colors,
            mergeCutoff
        );
        const updatedIndices = updateIndices(indices, mapping);
        console.log("After vertex merging:");
        console.log(`  Vertices: ${mergedVertices.length}`);
        console.log(`  Faces: ${countFaces(updatedIndices)}`);

        // Calculate target vertex count based on reduction fraction
        let targetVertexCount = null;
        if (reductionFraction !== null) {
            targetVertexCount = Math.round(
                mergedVertices.length * reductionFraction
            );
            // console.log(`Target vertex count: ${targetVertexCount} (${reductionFraction * 100}% of merged vertices)`);
        }

        // Simplify the mesh using Vertex Clustering if targetVertexCount is provided
        let simplifiedVertices = mergedVertices;
        let simplifiedIndices = updatedIndices;
        let simplifiedColors = mergedColors;

        if (targetVertexCount) {
            const simplifiedMesh = simplifyMesh(
                mergedVertices,
                updatedIndices,
                mergedColors,
                targetVertexCount
            );
            simplifiedVertices = simplifiedMesh.vertices.map(truncateValues);
            simplifiedIndices = simplifiedMesh.indices;
            simplifiedColors = simplifiedMesh.colors.map(truncateValues);
            console.log("After Vertex Clustering simplification:");
            console.log(`  Vertices: ${simplifiedVertices.length}`);
            console.log(`  Faces: ${countFaces(simplifiedIndices)}`);
        }

        // Truncate normals if they exist
        const mergedNormals =
            normals.length > 0 ? normals.map(truncateValues) : normals;

        // Replace vertices, colors, normals, and indices in VRML
        const newVRMLContent = replaceVerticesAndIndicesInVRML(
            shapeChunkContent,
            simplifiedVertices,
            simplifiedColors,
            mergedNormals,
            simplifiedIndices
        );

        // console.log(newVRMLContent.slice(0, 1000));

        newVRMLContents.push(newVRMLContent);

        // fs.writeFileSync(outputFile + "_" + vrmlChunkIdx.toString() + ".wrl", newVRMLContent);
        // console.log(`Processed VRML file saved as ${outputFile}`);

        //   console.log(newVRMLContent);
        console.log("");
    }

    const newVRMLContent =
        firstChunkContent + "Shape {" + newVRMLContents.join("Shape {");

    console.log(newVRMLContents.length);

    console.log(newVRMLContent);

    return newVRMLContent;
}