Корректная расстановка стенок #7

Merged
rust merged 15 commits from test into main 2026-01-11 15:58:40 +03:00
Showing only changes of commit 272b2b6f77 - Show all commits
+43 -24
View File
@@ -50,7 +50,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const sortedX = useMemo(() => [0, ...safeX, 1].sort((a, b) => a - b), [safeX]); const sortedX = useMemo(() => [0, ...safeX, 1].sort((a, b) => a - b), [safeX]);
const sortedY = useMemo(() => [0, ...safeY, 1].sort((a, b) => a - b), [safeY]); const sortedY = useMemo(() => [0, ...safeY, 1].sort((a, b) => a - b), [safeY]);
// -- HELPER: Get Selected -- // -- HELPERS --
const getSelectedPartition = () => { const getSelectedPartition = () => {
if (!selectedPartitionId) return null; if (!selectedPartitionId) return null;
for (const key in safePartitions) { for (const key in safePartitions) {
@@ -61,11 +61,12 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
}; };
const selectedData = getSelectedPartition(); const selectedData = getSelectedPartition();
// --- SOLVER (Копия из geometryGenerator) --- // --- SOLVER (Пересчет границ) ---
const solveWallLimits = (parts: Partition[]): LimitMap => { const solveWallLimits = (parts: Partition[]): LimitMap => {
const limits: LimitMap = {}; const limits: LimitMap = {};
parts.forEach(p => { limits[p.id] = { min: 0, max: 1 }; }); parts.forEach(p => { limits[p.id] = { min: 0, max: 1 }; });
// 4 прохода для сложных вложений
for (let pass = 0; pass < 4; pass++) { for (let pass = 0; pass < 4; pass++) {
parts.forEach(target => { parts.forEach(target => {
let newMin = 0; let newMin = 0;
@@ -78,8 +79,9 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const obsMin = limits[obstacle.id].min; const obsMin = limits[obstacle.id].min;
const obsMax = limits[obstacle.id].max; const obsMax = limits[obstacle.id].max;
// Используем >= и <= для надежности // Допуск (epsilon) чуть больше, чтобы надежнее ловить пересечения
if (target.offset >= obsMin - 0.001 && target.offset <= obsMax + 0.001) { const EPS = 0.005;
if (target.offset >= obsMin - EPS && target.offset <= obsMax + EPS) {
if (obstacle.offset < center) newMin = Math.max(newMin, obstacle.offset); if (obstacle.offset < center) newMin = Math.max(newMin, obstacle.offset);
else if (obstacle.offset > center) newMax = Math.min(newMax, obstacle.offset); else if (obstacle.offset > center) newMax = Math.min(newMax, obstacle.offset);
} }
@@ -90,7 +92,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
return limits; return limits;
}; };
// --- RAYCASTING (Исправленный поиск свободного места) --- // --- RAYCASTING (Поиск коробки под курсором) ---
const getCursorBox = (lx: number, ly: number, parts: Partition[], limitMap: LimitMap) => { const getCursorBox = (lx: number, ly: number, parts: Partition[], limitMap: LimitMap) => {
let minX = 0, maxX = 1; let minX = 0, maxX = 1;
let minY = 0, maxY = 1; let minY = 0, maxY = 1;
@@ -99,20 +101,18 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const { min: pMin, max: pMax } = limitMap[p.id]; const { min: pMin, max: pMax } = limitMap[p.id];
if (pMax - pMin < 0.001) return; if (pMax - pMin < 0.001) return;
// Используем те же допуски, что и в Solver // Используем небольшой допуск, чтобы мышь точно "видела" стенку
const EPSILON = 0.001; const EPS = 0.002;
if (p.axis === 'x') { if (p.axis === 'x') {
// Вертикальная преграда (X) // Вертикальная преграда
// Проверяем, перекрывает ли она Y курсора if (ly >= pMin - EPS && ly <= pMax + EPS) {
if (ly >= pMin - EPSILON && ly <= pMax + EPSILON) {
if (p.offset < lx) minX = Math.max(minX, p.offset); if (p.offset < lx) minX = Math.max(minX, p.offset);
if (p.offset > lx) maxX = Math.min(maxX, p.offset); if (p.offset > lx) maxX = Math.min(maxX, p.offset);
} }
} else { } else {
// Горизонтальная преграда (Y) // Горизонтальная преграда
// Проверяем, перекрывает ли она X курсора if (lx >= pMin - EPS && lx <= pMax + EPS) {
if (lx >= pMin - EPSILON && lx <= pMax + EPSILON) {
if (p.offset < ly) minY = Math.max(minY, p.offset); if (p.offset < ly) minY = Math.max(minY, p.offset);
if (p.offset > ly) maxY = Math.min(maxY, p.offset); if (p.offset > ly) maxY = Math.min(maxY, p.offset);
} }
@@ -121,16 +121,16 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
return { minX, maxX, minY, maxY }; return { minX, maxX, minY, maxY };
}; };
// Поиск соседей для отображения размеров // Поиск соседей для размеров
const getNeighborOffsets = (offset: number, crossPos: number, axis: Axis, parts: Partition[], limitMap: LimitMap) => { const getNeighborOffsets = (offset: number, crossPos: number, axis: Axis, parts: Partition[], limitMap: LimitMap) => {
let min = 0; let min = 0;
let max = 1; let max = 1;
const EPSILON = 0.001; const EPS = 0.001;
parts.forEach(p => { parts.forEach(p => {
if (p.axis === axis) { if (p.axis === axis) {
const { min: pMin, max: pMax } = limitMap[p.id]; const { min: pMin, max: pMax } = limitMap[p.id];
if (crossPos >= pMin - EPSILON && crossPos <= pMax + EPSILON) { if (crossPos >= pMin - EPS && crossPos <= pMax + EPS) {
if (p.offset < offset) min = Math.max(min, p.offset); if (p.offset < offset) min = Math.max(min, p.offset);
if (p.offset > offset) max = Math.min(max, p.offset); if (p.offset > offset) max = Math.min(max, p.offset);
} }
@@ -220,6 +220,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
setPhantomPartition(null); setPhantomPartition(null);
setHoveredCell(null); setHoveredCell(null);
// 1. Находим ячейку
let cellIdx = null; let cellIdx = null;
for (let i = 0; i < sortedX.length - 1; i++) { for (let i = 0; i < sortedX.length - 1; i++) {
if (nx >= sortedX[i] && nx <= sortedX[i+1]) { if (nx >= sortedX[i] && nx <= sortedX[i+1]) {
@@ -243,6 +244,10 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const lx = (nx - cx1) / cw; const lx = (nx - cx1) / cw;
const ly = (ny - cy1) / ch; const ly = (ny - cy1) / ch;
// Реальные размеры ячейки в мм (для корректного расчета дистанции мыши)
const realCellW = cw * drawerW;
const realCellH = ch * drawerD;
let found = null; let found = null;
const SNAP = 0.05; const SNAP = 0.05;
for (const p of parts) { for (const p of parts) {
@@ -257,20 +262,34 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
if (found) { if (found) {
setHoveredPartition({ id: found.id, cellKey: key }); setHoveredPartition({ id: found.id, cellKey: key });
} else { } else {
// --- ВАЖНО: Получаем корректные границы с учетом Solver --- // Вычисляем бокс под курсором
const box = getCursorBox(lx, ly, parts, limitMap); const box = getCursorBox(lx, ly, parts, limitMap);
const distL = lx - box.minX; const distR = box.maxX - lx; // --- ВАЖНО: Расчет дистанции с учетом РЕАЛЬНОГО размера ячейки (мм) ---
const distT = ly - box.minY; const distB = box.maxY - ly; // Ранее считалось в относительных 0-1, что ломало логику в узких/широких ячейках
const distL = (lx - box.minX) * realCellW;
const distR = (box.maxX - lx) * realCellW;
const distT = (ly - box.minY) * realCellH;
const distB = (box.maxY - ly) * realCellH;
// Выбираем ось перпендикулярно ближайшей стороне // Выбираем ось, к которой мы БЛИЖЕ ВИЗУАЛЬНО
const minD = Math.min(distL, distR, distT, distB); const minD = Math.min(distL, distR, distT, distB);
const newAxis = (minD === distL || minD === distR) ? 'x' : 'y';
const width = box.maxX - box.minX; // Если мы близко к бокам -> ставим вертикальную (x)
const height = box.maxY - box.minY; // Если близко к верху/низу -> ставим горизонтальную (y)
let newAxis: Axis = (minD === distL || minD === distR) ? 'x' : 'y';
if ((newAxis === 'y' && height > 0.05) || (newAxis === 'x' && width > 0.05)) { // Но если мы в центре, выбираем ось, которая делит длинную сторону
const boxW = (box.maxX - box.minX) * realCellW;
const boxH = (box.maxY - box.minY) * realCellH;
// Если мы не "прилипли" к краю (находимся далеко от всего > 20мм), то делим длинную сторону
if (minD > 20) {
newAxis = boxW > boxH ? 'x' : 'y';
}
// Финальная проверка, есть ли место для стенки (минимум 10% от размера коробки)
if ((newAxis === 'y' && (box.maxY - box.minY) > 0.1) || (newAxis === 'x' && (box.maxX - box.minX) > 0.1)) {
const offset = newAxis === 'x' ? lx : ly; const offset = newAxis === 'x' ? lx : ly;
const min = newAxis === 'x' ? box.minY : box.minX; const min = newAxis === 'x' ? box.minY : box.minX;
const max = newAxis === 'x' ? box.maxY : box.maxX; const max = newAxis === 'x' ? box.maxY : box.maxX;