-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimate_stl.bash
More file actions
executable file
·85 lines (69 loc) · 3.96 KB
/
Copy pathdecimate_stl.bash
File metadata and controls
executable file
·85 lines (69 loc) · 3.96 KB
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
#!/usr/bin/env bash
# Clean up and decimate all STL files in a folder using MeshLab.
# Usage: ./decimate_stl.sh <input_dir> <output_dir>
set -euo pipefail
if [ $# -lt 2 ]; then
echo "Usage: $0 <input_dir> <output_dir>"
exit 1
fi
INPUT_DIR="$1"
OUTPUT_DIR="$2"
mkdir -p "$OUTPUT_DIR"
# Write the MeshLab filter script to a temp file
MLX=$(mktemp /tmp/decimate_XXXXXX.mlx)
cat > "$MLX" <<'EOF'
<!DOCTYPE FilterScript>
<FilterScript>
<!-- 1. Weld near-coincident vertices to collapse overlapping surfaces -->
<filter name="Remove Duplicate Vertices"/>
<filter name="Merge Close Vertices">
<Param name="Threshold" type="RichAbsPerc" description="Merging distance" tooltip="" value="0.0001" min="0" max="0"/>
</filter>
<!-- 2. Remove degenerate/duplicate geometry (after merge so overlaps are caught) -->
<filter name="Remove Duplicate Faces"/>
<filter name="Remove Zero Area Faces"/>
<!-- 3. Fix topology -->
<filter name="Repair non Manifold Edges by removing faces"/>
<filter name="Repair non Manifold Vertices by splitting">
<Param name="VertDispRatio" type="RichFloat" description="Vertex Displacement Ratio" tooltip="" value="0"/>
</filter>
<!-- 4. Remove floating fragments (less than 25 faces) -->
<filter name="Remove Isolated pieces (wrt Face Num.)">
<Param name="MinComponentSize" type="RichInt" description="Enter minimum conn. comp size:" tooltip="" value="25"/>
<Param name="removeUnref" type="RichBool" description="Remove unfreferenced vertices" tooltip="" value="true"/>
</filter>
<!-- 5. Clean up any orphaned vertices -->
<filter name="Remove Unreferenced Vertices"/>
<!-- 6. Decimate -->
<filter name="Simplification: Quadric Edge Collapse Decimation">
<Param name="TargetFaceNum" type="RichInt" description="Target number of faces" tooltip="The desired final number of faces." value="3000"/>
<Param name="TargetPerc" type="RichFloat" description="Percentage reduction (0..1)" tooltip="If non zero, this parameter specifies the desired final size of the mesh as a percentage of the initial size." value="0"/>
<Param name="QualityThr" type="RichFloat" description="Quality threshold" tooltip="Quality threshold for penalizing bad shaped faces." value="0.3"/>
<Param name="PreserveBoundary" type="RichBool" description="Preserve Boundary of the mesh" tooltip="" value="false"/>
<Param name="BoundaryWeight" type="RichFloat" description="Boundary Preserving Weight" tooltip="" value="1"/>
<Param name="PreserveNormal" type="RichBool" description="Preserve Normal" tooltip="" value="false"/>
<Param name="PreserveTopology" type="RichBool" description="Preserve Topology" tooltip="" value="false"/>
<Param name="OptimalPlacement" type="RichBool" description="Optimal position of simplified vertices" tooltip="" value="true"/>
<Param name="PlanarQuadric" type="RichBool" description="Planar Simplification" tooltip="" value="false"/>
<Param name="PlanarWeight" type="RichFloat" description="Planar Simp. Weight" tooltip="" value="0.001"/>
<Param name="QualityWeight" type="RichBool" description="Weighted Simplification" tooltip="" value="false"/>
<Param name="AutoClean" type="RichBool" description="Post-simplification cleaning" tooltip="" value="true"/>
<Param name="Selected" type="RichBool" description="Simplify only selected faces" tooltip="" value="false"/>
</filter>
<!-- 7. Recompute normals after all modifications -->
<filter name="Re-Compute Face Normals"/>
<filter name="Re-Compute Vertex Normals">
<Param name="weightMode" type="RichEnum" description="Weighting Mode:" tooltip="" value="2" enum_val0="Simple Average" enum_val1="By Area" enum_val2="By Angle" enum_val3="As defined by N. Max"/>
</filter>
</FilterScript>
EOF
trap 'rm -f "$MLX"' EXIT
count=0
for stl in "$INPUT_DIR"/*.stl "$INPUT_DIR"/*.STL; do
[ -f "$stl" ] || continue
name="$(basename "$stl")"
echo "Decimating: $name"
meshlabserver -i "$stl" -o "$OUTPUT_DIR/$name" -s "$MLX"
count=$((count + 1))
done
echo "Done. Decimated $count file(s) into $OUTPUT_DIR"