2026-05-07-16-53-23 修正3D模型中心和坐标轴

This commit is contained in:
2026-05-07 16:59:33 +08:00
parent e1c34f27bb
commit bbc7d215e9
6 changed files with 322 additions and 26 deletions

View File

@@ -705,22 +705,40 @@ function createStlPreview(filePath: string, fileName: string, limit: number) {
const step = Math.max(1, Math.ceil(triangleCount / sampleLimit));
const vertices: number[] = [];
let sampledTriangles = 0;
const bounds = {
min: { x: Infinity, y: Infinity, z: Infinity },
max: { x: -Infinity, y: -Infinity, z: -Infinity },
};
for (let triangleIndex = 0; triangleIndex < triangleCount; triangleIndex += step) {
for (let triangleIndex = 0; triangleIndex < triangleCount; triangleIndex += 1) {
const offset = 84 + triangleIndex * 50;
if (offset + 50 > buffer.length) {
break;
}
const shouldSample = triangleIndex % step === 0;
for (let vertex = 0; vertex < 3; vertex += 1) {
const vertexOffset = offset + 12 + vertex * 12;
vertices.push(
Number(buffer.readFloatLE(vertexOffset).toFixed(3)),
Number(buffer.readFloatLE(vertexOffset + 4).toFixed(3)),
Number(buffer.readFloatLE(vertexOffset + 8).toFixed(3)),
);
const x = buffer.readFloatLE(vertexOffset);
const y = buffer.readFloatLE(vertexOffset + 4);
const z = buffer.readFloatLE(vertexOffset + 8);
bounds.min.x = Math.min(bounds.min.x, x);
bounds.min.y = Math.min(bounds.min.y, y);
bounds.min.z = Math.min(bounds.min.z, z);
bounds.max.x = Math.max(bounds.max.x, x);
bounds.max.y = Math.max(bounds.max.y, y);
bounds.max.z = Math.max(bounds.max.z, z);
if (shouldSample) {
vertices.push(
Number(x.toFixed(3)),
Number(y.toFixed(3)),
Number(z.toFixed(3)),
);
}
}
if (shouldSample) {
sampledTriangles += 1;
}
sampledTriangles += 1;
}
const payload = {
@@ -728,6 +746,18 @@ function createStlPreview(filePath: string, fileName: string, limit: number) {
triangleCount,
sampledTriangles,
vertices,
bounds: {
min: {
x: Number(bounds.min.x.toFixed(3)),
y: Number(bounds.min.y.toFixed(3)),
z: Number(bounds.min.z.toFixed(3)),
},
max: {
x: Number(bounds.max.x.toFixed(3)),
y: Number(bounds.max.y.toFixed(3)),
z: Number(bounds.max.z.toFixed(3)),
},
},
};
modelPreviewCache.set(cacheKey, payload);
return payload;