4
This commit is contained in:
+115
-170
@@ -13,11 +13,9 @@ const cleanPoints = (points: number[]) => {
|
|||||||
|
|
||||||
const getAllPartitions = (splits: LayoutSplits): Partition[] => {
|
const getAllPartitions = (splits: LayoutSplits): Partition[] => {
|
||||||
if (!splits || !splits.partitions) return [];
|
if (!splits || !splits.partitions) return [];
|
||||||
// Собираем все массивы перегородок в один плоский массив
|
|
||||||
return Object.values(splits.partitions).flat();
|
return Object.values(splits.partitions).flat();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Функция для предпросмотра (шаг 3 - цветные блоки)
|
|
||||||
export const calculateParts = (config: AppConfig, splits: LayoutSplits): GeneratedPart[] => {
|
export const calculateParts = (config: AppConfig, splits: LayoutSplits): GeneratedPart[] => {
|
||||||
const parts: GeneratedPart[] = [];
|
const parts: GeneratedPart[] = [];
|
||||||
const safeX = Array.isArray(splits?.x) ? splits.x : [];
|
const safeX = Array.isArray(splits?.x) ? splits.x : [];
|
||||||
@@ -61,7 +59,7 @@ export const calculateParts = (config: AppConfig, splits: LayoutSplits): Generat
|
|||||||
return parts;
|
return parts;
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- ГЕНЕРАЦИЯ ГЕОМЕТРИИ (CSG FIXED) ---
|
// --- ГЕНЕРАЦИЯ ГЕОМЕТРИИ (ПОСЛЕДОВАТЕЛЬНЫЙ CSG) ---
|
||||||
|
|
||||||
export const createBinGeometry = (
|
export const createBinGeometry = (
|
||||||
width: number, depth: number, height: number, thickness: number, radius: number = 0,
|
width: number, depth: number, height: number, thickness: number, radius: number = 0,
|
||||||
@@ -71,134 +69,34 @@ export const createBinGeometry = (
|
|||||||
|
|
||||||
const safeConfig = config || { perforation: { enabled: false } } as AppConfig;
|
const safeConfig = config || { perforation: { enabled: false } } as AppConfig;
|
||||||
const evaluator = new Evaluator();
|
const evaluator = new Evaluator();
|
||||||
evaluator.useGroups = false; // Важно для корректного слияния материалов и нормалей
|
evaluator.useGroups = false;
|
||||||
|
|
||||||
// Массивы для хранения геометрии перед слиянием
|
|
||||||
const solidGeometries: THREE.BufferGeometry[] = [];
|
|
||||||
const holeGeometries: THREE.BufferGeometry[] = [];
|
|
||||||
const filletGeometries: THREE.BufferGeometry[] = [];
|
|
||||||
|
|
||||||
// ==========================================
|
|
||||||
// 1. СБОРКА ТВЕРДЫХ ТЕЛ (ПОЛ + СТЕНЫ)
|
|
||||||
// ==========================================
|
|
||||||
|
|
||||||
// 1.1 ПОЛ
|
|
||||||
const floorGeo = new THREE.BoxGeometry(width, thickness, depth);
|
|
||||||
floorGeo.translate(0, thickness / 2, 0);
|
|
||||||
solidGeometries.push(floorGeo);
|
|
||||||
|
|
||||||
const wallH = height - thickness;
|
const wallH = height - thickness;
|
||||||
const innerW = width - 2 * thickness;
|
const innerW = width - 2 * thickness;
|
||||||
const innerD = depth - 2 * thickness;
|
const innerD = depth - 2 * thickness;
|
||||||
|
|
||||||
// Функция добавления геометрии стены
|
// 1. Базовая геометрия - ПОЛ
|
||||||
const addWallGeometry = (w: number, h: number, d: number, x: number, y: number, z: number) => {
|
const floorGeo = new THREE.BoxGeometry(width, thickness, depth);
|
||||||
const geo = new THREE.BoxGeometry(w, h, d);
|
floorGeo.translate(0, thickness / 2, 0);
|
||||||
geo.translate(x, y, z);
|
let mainBrush = new Brush(floorGeo);
|
||||||
solidGeometries.push(geo);
|
mainBrush.updateMatrixWorld();
|
||||||
|
|
||||||
|
// --- ФУНКЦИИ ОПЕРАЦИЙ ---
|
||||||
|
|
||||||
|
// Добавить твердое тело (стену/скругление)
|
||||||
|
const addSolid = (geo: THREE.BufferGeometry) => {
|
||||||
|
const brush = new Brush(geo);
|
||||||
|
brush.updateMatrixWorld();
|
||||||
|
mainBrush = evaluator.evaluate(mainBrush, brush, ADDITION);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 1.2 ВНЕШНИЕ СТЕНЫ
|
// Вычесть отверстия для конкретной зоны
|
||||||
// Front & Back (Вдоль X)
|
const subtractHoles = (W: number, H: number, startX: number, startY: number, startZ: number, axis: 'x' | 'z') => {
|
||||||
addWallGeometry(innerW, wallH, thickness, 0, thickness + wallH/2, depth/2 - thickness/2);
|
if (!safeConfig.perforation?.enabled) return;
|
||||||
addWallGeometry(innerW, wallH, thickness, 0, thickness + wallH/2, -depth/2 + thickness/2);
|
|
||||||
// Left & Right (Вдоль Z)
|
|
||||||
addWallGeometry(thickness, wallH, depth, -width/2 + thickness/2, thickness + wallH/2, 0);
|
|
||||||
addWallGeometry(thickness, wallH, depth, width/2 - thickness/2, thickness + wallH/2, 0);
|
|
||||||
|
|
||||||
// 1.3 ВНУТРЕННИЕ ПЕРЕГОРОДКИ
|
|
||||||
let partitions: Partition[] = [];
|
|
||||||
if (Array.isArray(splits)) {
|
|
||||||
partitions = splits;
|
|
||||||
} else if (splits && splits.partitions) {
|
|
||||||
partitions = getAllPartitions(splits);
|
|
||||||
}
|
|
||||||
|
|
||||||
partitions.forEach(p => {
|
|
||||||
const pMin = p.min ?? 0;
|
|
||||||
const pMax = p.max ?? 1;
|
|
||||||
|
|
||||||
// Игнорируем ошибки данных
|
|
||||||
if (Math.abs(pMax - pMin) < 0.001) return;
|
|
||||||
|
|
||||||
let w=0, h=p.height, d=0, x=0, z=0;
|
|
||||||
|
|
||||||
if (p.axis === 'x') { // Вертикальная (Вдоль Z)
|
|
||||||
w = thickness;
|
|
||||||
d = (pMax - pMin) * innerD;
|
|
||||||
x = (-innerW/2) + (p.offset * innerW);
|
|
||||||
const midZ = (pMin + pMax) / 2;
|
|
||||||
z = (-innerD/2) + (midZ * innerD);
|
|
||||||
} else { // Горизонтальная (Вдоль X)
|
|
||||||
w = (pMax - pMin) * innerW;
|
|
||||||
d = thickness;
|
|
||||||
const midX = (pMin + pMax) / 2;
|
|
||||||
x = (-innerW/2) + (midX * innerW);
|
|
||||||
z = (-innerD/2) + (p.offset * innerD);
|
|
||||||
}
|
|
||||||
addWallGeometry(w, h, d, x, thickness + h/2, z);
|
|
||||||
});
|
|
||||||
|
|
||||||
// ==========================================
|
|
||||||
// 2. СБОРКА СКРУГЛЕНИЙ (ADDITION)
|
|
||||||
// ==========================================
|
|
||||||
|
|
||||||
if (radius > 0) {
|
|
||||||
const fRad = Math.min(radius, 5);
|
|
||||||
// Используем цилиндр для сглаживания углов (выпуклое скругление внутренних углов)
|
|
||||||
// Для настоящего вогнутого fillet в CSG нужно вычитать обратную форму, но для FDM печати
|
|
||||||
// добавление материала в угол (chamfer/fillet) часто лучше.
|
|
||||||
// Делаем просто цилиндры в местах стыков.
|
|
||||||
const filletCyl = new THREE.CylinderGeometry(fRad, fRad, 1, 16);
|
|
||||||
|
|
||||||
const addFillet = (x: number, z: number, h: number) => {
|
|
||||||
const f = filletCyl.clone();
|
|
||||||
f.scale(1, h, 1);
|
|
||||||
f.translate(x, thickness + h/2, z);
|
|
||||||
filletGeometries.push(f);
|
|
||||||
};
|
|
||||||
|
|
||||||
partitions.forEach(p => {
|
|
||||||
const pMin = p.min ?? 0;
|
|
||||||
const pMax = p.max ?? 1;
|
|
||||||
if (Math.abs(pMax - pMin) < 0.001) return;
|
|
||||||
|
|
||||||
if (!p.rounded) return; // Пропускаем если скругление выключено для этой стенки
|
|
||||||
|
|
||||||
if (p.axis === 'x') { // Vert
|
|
||||||
const xPos = (-innerW/2) + (p.offset * innerW);
|
|
||||||
const zStart = (-innerD/2) + (pMin * innerD);
|
|
||||||
const zEnd = (-innerD/2) + (pMax * innerD);
|
|
||||||
addFillet(xPos, zStart, p.height);
|
|
||||||
addFillet(xPos, zEnd, p.height);
|
|
||||||
} else { // Horiz
|
|
||||||
const zPos = (-innerD/2) + (p.offset * innerD);
|
|
||||||
const xStart = (-innerW/2) + (pMin * innerW);
|
|
||||||
const xEnd = (-innerW/2) + (pMax * innerW);
|
|
||||||
addFillet(xStart, zPos, p.height);
|
|
||||||
addFillet(xEnd, zPos, p.height);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==========================================
|
|
||||||
// 3. СБОРКА ОТВЕРСТИЙ (SUBTRACTION)
|
|
||||||
// ==========================================
|
|
||||||
|
|
||||||
if (safeConfig.perforation?.enabled) {
|
|
||||||
const { pattern, diameter, spacing } = safeConfig.perforation;
|
const { pattern, diameter, spacing } = safeConfig.perforation;
|
||||||
const margin = 4;
|
const margin = 4;
|
||||||
const step = diameter + Math.max(2, spacing);
|
const step = diameter + Math.max(2, spacing);
|
||||||
|
|
||||||
// Базовые сверла (длинные, чтобы пробить насквозь)
|
|
||||||
const drillLength = thickness * 4;
|
|
||||||
const drillZ = new THREE.CylinderGeometry(diameter/2, diameter/2, drillLength, 12);
|
|
||||||
drillZ.rotateX(Math.PI / 2); // Вдоль Z
|
|
||||||
const drillX = new THREE.CylinderGeometry(diameter/2, diameter/2, drillLength, 12);
|
|
||||||
drillX.rotateZ(Math.PI / 2); // Вдоль X
|
|
||||||
|
|
||||||
// Функция генерации массива сверл для плоскости
|
|
||||||
const createDrills = (W: number, H: number, startX: number, startY: number, startZ: number, axis: 'x' | 'z') => {
|
|
||||||
const cols = Math.floor((W - margin*2) / step);
|
const cols = Math.floor((W - margin*2) / step);
|
||||||
const rowH = pattern === 'circle' ? step : step * 0.866;
|
const rowH = pattern === 'circle' ? step : step * 0.866;
|
||||||
const rows = Math.floor((H - margin*2) / rowH);
|
const rows = Math.floor((H - margin*2) / rowH);
|
||||||
@@ -208,83 +106,130 @@ export const createBinGeometry = (
|
|||||||
const offsetX = (W - cols * step) / 2;
|
const offsetX = (W - cols * step) / 2;
|
||||||
const offsetY = (H - rows * rowH) / 2;
|
const offsetY = (H - rows * rowH) / 2;
|
||||||
|
|
||||||
|
// Базовое сверло
|
||||||
|
const drillBase = new THREE.CylinderGeometry(diameter/2, diameter/2, thickness * 3, 12);
|
||||||
|
if (axis === 'x') drillBase.rotateX(Math.PI / 2); // Вдоль Z
|
||||||
|
else drillBase.rotateZ(Math.PI / 2); // Вдоль X
|
||||||
|
|
||||||
|
const drills: THREE.BufferGeometry[] = [];
|
||||||
|
|
||||||
for(let r=0; r<rows; r++) {
|
for(let r=0; r<rows; r++) {
|
||||||
const isOdd = r % 2 !== 0;
|
const isOdd = r % 2 !== 0;
|
||||||
for(let c=0; c<cols; c++) {
|
for(let c=0; c<cols; c++) {
|
||||||
let u = offsetX + c * step + diameter/2;
|
let u = offsetX + c * step + diameter/2;
|
||||||
let v = offsetY + r * rowH + diameter/2;
|
let v = offsetY + r * rowH + diameter/2;
|
||||||
|
|
||||||
if ((pattern === 'hexagon' || pattern === 'triangle') && isOdd) u += step/2;
|
if ((pattern === 'hexagon' || pattern === 'triangle') && isOdd) u += step/2;
|
||||||
if (u > W - margin || v > H - margin) continue;
|
if (u > W - margin || v > H - margin) continue;
|
||||||
|
|
||||||
let drill: THREE.BufferGeometry;
|
const drill = drillBase.clone();
|
||||||
if (axis === 'x') {
|
if (axis === 'x') drill.translate(startX + u, startY + v, startZ);
|
||||||
// Стена вдоль X. Сверлим ВДОЛЬ Z.
|
else drill.translate(startX, startY + v, startZ + u);
|
||||||
drill = drillZ.clone();
|
drills.push(drill);
|
||||||
drill.translate(startX + u, startY + v, startZ);
|
|
||||||
} else {
|
|
||||||
// Стена вдоль Z. Сверлим ВДОЛЬ X.
|
|
||||||
drill = drillX.clone();
|
|
||||||
drill.translate(startX, startY + v, startZ + u);
|
|
||||||
}
|
}
|
||||||
holeGeometries.push(drill);
|
}
|
||||||
|
|
||||||
|
if (drills.length > 0) {
|
||||||
|
const mergedDrills = mergeBufferGeometries(drills);
|
||||||
|
if (mergedDrills) {
|
||||||
|
const drillBrush = new Brush(mergedDrills);
|
||||||
|
drillBrush.updateMatrixWorld();
|
||||||
|
mainBrush = evaluator.evaluate(mainBrush, drillBrush, SUBTRACTION);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 3.1 Сверлим внешние стены
|
|
||||||
// Front & Back
|
|
||||||
createDrills(innerW, wallH, -innerW/2, thickness, depth/2, 'x');
|
|
||||||
createDrills(innerW, wallH, -innerW/2, thickness, -depth/2, 'x');
|
|
||||||
// Left & Right
|
|
||||||
createDrills(depth, wallH, -width/2, thickness, -depth/2, 'z');
|
|
||||||
createDrills(depth, wallH, width/2, thickness, -depth/2, 'z');
|
|
||||||
|
|
||||||
// 3.2 Сверлим внутренние перегородки
|
// 2. СБОРКА СТЕН (ADDITION)
|
||||||
|
|
||||||
|
// Внешние стены
|
||||||
|
// Front
|
||||||
|
addSolid(new THREE.BoxGeometry(innerW, wallH, thickness).translate(0, thickness + wallH/2, depth/2 - thickness/2));
|
||||||
|
// Back
|
||||||
|
addSolid(new THREE.BoxGeometry(innerW, wallH, thickness).translate(0, thickness + wallH/2, -depth/2 + thickness/2));
|
||||||
|
// Left
|
||||||
|
addSolid(new THREE.BoxGeometry(thickness, wallH, depth).translate(-width/2 + thickness/2, thickness + wallH/2, 0));
|
||||||
|
// Right
|
||||||
|
addSolid(new THREE.BoxGeometry(thickness, wallH, depth).translate(width/2 - thickness/2, thickness + wallH/2, 0));
|
||||||
|
|
||||||
|
// Внутренние перегородки
|
||||||
|
let partitions: Partition[] = [];
|
||||||
|
if (Array.isArray(splits)) partitions = splits;
|
||||||
|
else if (splits && splits.partitions) partitions = getAllPartitions(splits);
|
||||||
|
|
||||||
partitions.forEach(p => {
|
partitions.forEach(p => {
|
||||||
const pMin = p.min ?? 0;
|
const pMin = p.min ?? 0; const pMax = p.max ?? 1;
|
||||||
const pMax = p.max ?? 1;
|
|
||||||
if (Math.abs(pMax - pMin) < 0.001) return;
|
if (Math.abs(pMax - pMin) < 0.001) return;
|
||||||
|
|
||||||
if (p.axis === 'x') { // Vert wall (Z-axis)
|
let w=0, h=p.height, d=0, x=0, z=0;
|
||||||
const len = (pMax - pMin) * innerD;
|
if (p.axis === 'x') { // Vert (Z-axis)
|
||||||
|
w = thickness; d = (pMax - pMin) * innerD;
|
||||||
|
x = (-innerW/2) + (p.offset * innerW);
|
||||||
|
z = (-innerD/2) + ((pMin + pMax)/2 * innerD);
|
||||||
|
} else { // Horiz (X-axis)
|
||||||
|
w = (pMax - pMin) * innerW; d = thickness;
|
||||||
|
x = (-innerW/2) + ((pMin + pMax)/2 * innerW);
|
||||||
|
z = (-innerD/2) + (p.offset * innerD);
|
||||||
|
}
|
||||||
|
addSolid(new THREE.BoxGeometry(w, h, d).translate(x, thickness + h/2, z));
|
||||||
|
});
|
||||||
|
|
||||||
|
// 3. СКРУГЛЕНИЯ (ADDITION - цилиндры в углы)
|
||||||
|
if (radius > 0) {
|
||||||
|
const fRad = Math.min(radius, 5);
|
||||||
|
const filletBase = new THREE.CylinderGeometry(fRad, fRad, 1, 16);
|
||||||
|
filletBase.translate(0, 0.5, 0); // Pivot at bottom
|
||||||
|
|
||||||
|
partitions.forEach(p => {
|
||||||
|
if (!p.rounded) return;
|
||||||
|
const pMin = p.min ?? 0; const pMax = p.max ?? 1;
|
||||||
|
if (Math.abs(pMax - pMin) < 0.001) return;
|
||||||
|
|
||||||
|
const addF = (x: number, z: number) => {
|
||||||
|
const f = filletBase.clone();
|
||||||
|
f.scale(1, p.height, 1);
|
||||||
|
f.translate(x, thickness, z);
|
||||||
|
addSolid(f);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (p.axis === 'x') { // Vert
|
||||||
const xPos = (-innerW/2) + (p.offset * innerW);
|
const xPos = (-innerW/2) + (p.offset * innerW);
|
||||||
const zStart = (-innerD/2) + (pMin * innerD);
|
addF(xPos, (-innerD/2) + (pMin * innerD)); // Start Z
|
||||||
createDrills(len, p.height, xPos, thickness, zStart, 'z');
|
addF(xPos, (-innerD/2) + (pMax * innerD)); // End Z
|
||||||
} else { // Horiz wall (X-axis)
|
} else { // Horiz
|
||||||
const len = (pMax - pMin) * innerW;
|
|
||||||
const xStart = (-innerW/2) + (pMin * innerW);
|
|
||||||
const zPos = (-innerD/2) + (p.offset * innerD);
|
const zPos = (-innerD/2) + (p.offset * innerD);
|
||||||
createDrills(len, p.height, xStart, thickness, zPos, 'x');
|
addF((-innerW/2) + (pMin * innerW), zPos); // Start X
|
||||||
|
addF((-innerW/2) + (pMax * innerW), zPos); // End X
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// 4. ПЕРФОРАЦИЯ (SUBTRACTION)
|
||||||
// 4. ФИНАЛЬНЫЕ ОПЕРАЦИИ CSG
|
if (safeConfig.perforation?.enabled) {
|
||||||
// ==========================================
|
// Внешние стены
|
||||||
|
subtractHoles(innerW, wallH, -innerW/2, thickness, depth/2, 'x'); // Front
|
||||||
|
subtractHoles(innerW, wallH, -innerW/2, thickness, -depth/2, 'x'); // Back
|
||||||
|
subtractHoles(depth, wallH, -width/2, thickness, -depth/2, 'z'); // Left
|
||||||
|
subtractHoles(depth, wallH, width/2, thickness, -depth/2, 'z'); // Right
|
||||||
|
|
||||||
// Объединяем всю твердую геометрию
|
// Внутренние перегородки
|
||||||
const mergedSolids = mergeBufferGeometries([...solidGeometries, ...filletGeometries]);
|
partitions.forEach(p => {
|
||||||
if (!mergedSolids) return new THREE.BoxGeometry(1,1,1); // Fallback
|
const pMin = p.min ?? 0; const pMax = p.max ?? 1;
|
||||||
|
if (Math.abs(pMax - pMin) < 0.001) return;
|
||||||
let finalBrush = new Brush(mergedSolids);
|
if (p.axis === 'x') {
|
||||||
finalBrush.updateMatrixWorld();
|
const len = (pMax - pMin) * innerD;
|
||||||
|
const xPos = (-innerW/2) + (p.offset * innerW);
|
||||||
// Если есть отверстия, вычитаем их
|
const zStart = (-innerD/2) + (pMin * innerD);
|
||||||
if (holeGeometries.length > 0) {
|
subtractHoles(len, p.height, xPos, thickness, zStart, 'z');
|
||||||
const mergedHoles = mergeBufferGeometries(holeGeometries);
|
} else {
|
||||||
if (mergedHoles) {
|
const len = (pMax - pMin) * innerW;
|
||||||
const holeBrush = new Brush(mergedHoles);
|
const xStart = (-innerW/2) + (pMin * innerW);
|
||||||
holeBrush.updateMatrixWorld();
|
const zPos = (-innerD/2) + (p.offset * innerD);
|
||||||
finalBrush = evaluator.evaluate(finalBrush, holeBrush, SUBTRACTION);
|
subtractHoles(len, p.height, xStart, thickness, zPos, 'x');
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Обновляем нормали для корректного отображения света (убирает прозрачность)
|
return mainBrush.geometry;
|
||||||
finalBrush.geometry.computeVertexNormals();
|
|
||||||
|
|
||||||
return finalBrush.geometry;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const generateSTL = (mesh: THREE.Object3D): Uint8Array | string => {
|
export const generateSTL = (mesh: THREE.Object3D): Uint8Array | string => {
|
||||||
|
|||||||
Reference in New Issue
Block a user