mesh-analysis
Analyzes 3D mesh files (STL) to calculate geometric properties (volume, components) and extract attribute data. Use this skill to process noisy 3D scan data...
Documentation & Preview
import collections import os import struct
class MeshAnalyzer: def __init__(self, filepath): self.filepath = filepath
Store triangles as tuples: (v1, v2, v3, mat_id)
self.triangles = [] self._parse()
def _parse(self): if not os.path.exists(self.filepath): raise FileNotFoundError(f"{self.filepath} not found.")
Try binary first (expected format for this task)
try: self._parse_binary() except Exception as e: print(f"Binary parse failed: {e}. Falling back to ASCII (no material IDs).") self.triangles = [] self._parse_ascii()
def _parse_ascii(self): current_triangle = [] with open(self.filepath) as f: for line in f: parts = line.strip().split() if not parts: continue if parts[0] == "vertex": v = (float(parts[1]), float(parts[2]), float(parts[3])) current_triangle.append(v) elif parts[0] == "endfacet": if len(current_triangle) == 3:
Default ID 0
self.triangles.append((current_triangle[0], current_triangle[1], current_triangle[2], 0)) current_triangle = []
def _parse_binary(self): with open(self.filepath, "rb") as f: f.read(80) # Skip header count_data = f.read(4) if not count_data: return count = struct.unpack("<I", count_data)[0]
file_size = os.path.getsize(self.filepath) expected_size = 80 + 4 + (50 * count) if file_size != expected_size: raise ValueError("Size mismatch")
for _ in range(count): data = f.read(50) floats = struct.unpack("<3f3f3f3f", data[:48]) attr = struct.unpack("<H", data[48:50])[0]
v1 = (floats[3], floats[4], floats[5]) v2 = (floats[6], floats[7], floats[8]) v3 = (floats[9], floats[10], floats[11])
self.triangles.append((v1, v2, v3, attr))
def get_volume(self, triangles=None): tris = triangles if triangles is not None else self.triangles total_volume = 0.0 for t in tris: v1, v2, v3 = t[0], t[1], t[2] cp_x = v2[1] * v3[2] - v2[2] * v3[1] cp_y = v2[2] * v3[0] - v2[0] * v3[2] cp_z = v2[0] * v3[1] - v2[1] * v3[0] dot_p = v1[0] * cp_x + v1[1] * cp_y + v1[2] * cp_z total_volume += dot_p
return abs(total_volume) / 6.0
def get_components(self): def quantize(v): return (round(v[0], 5), round(v[1], 5), round(v[2], 5))
vertex_map = collections.defaultdict(list) for i, t in enumerate(self.triangles):
t is (v1, v2, v3, id)
for v in t[:3]: vertex_map[quantize(v)].append(i)
visited_tris = set() components = []
for i in range(len(self.triangles)): if i in visited_tris: continue
comp_indices = [] queue = collections.deque([i]) visited_tris.add(i)
while queue: curr_idx = queue.popleft() comp_indices.append(curr_idx)
curr_tri = self.triangles[curr_idx] for v in curr_tri[:3]: qv = quantize(v) for n_idx in vertex_map[qv]: if n_idx not in visited_tris: visited_tris.add(n_idx) queue.append(n_idx)
components.append([self.triangles[idx] for idx in comp_indices])
return components
def analyze_largest_component(self): components = self.get_components() if not components: return {"main_part_volume": 0.0, "main_part_material_id": 0, "total_components": 0}
Sort by volume
comp_vols = [] for c in components: vol = self.get_volume(c) comp_vols.append((vol, c))
comp_vols.sort(key=lambda x: x[0], reverse=True) largest_vol, largest_tris = comp_vols[0]
Get Material ID (assume uniform per component, take first tri)
mat_id = largest_tris[0][3]
return {"main_part_volume": largest_vol, "main_part_material_id": mat_id, "total_components": len(components)}
if __name__ == "__main__": import sys
if len(sys.argv) > 1: analyzer = MeshAnalyzer(sys.argv[1]) print(analyzer.analyze_largest_component())