This commit is contained in:
Халимов Рустам
2026-01-11 15:27:12 +03:00
parent 454e6cf822
commit 272b2b6f77

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 sortedY = useMemo(() => [0, ...safeY, 1].sort((a, b) => a - b), [safeY]);
// -- HELPER: Get Selected --
// -- HELPERS --
const getSelectedPartition = () => {
if (!selectedPartitionId) return null;
for (const key in safePartitions) {
@@ -61,11 +61,12 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
};
const selectedData = getSelectedPartition();
// --- SOLVER (Копия из geometryGenerator) ---
// --- SOLVER (Пересчет границ) ---
const solveWallLimits = (parts: Partition[]): LimitMap => {
const limits: LimitMap = {};
parts.forEach(p => { limits[p.id] = { min: 0, max: 1 }; });
// 4 прохода для сложных вложений
for (let pass = 0; pass < 4; pass++) {
parts.forEach(target => {
let newMin = 0;
@@ -78,8 +79,9 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const obsMin = limits[obstacle.id].min;
const obsMax = limits[obstacle.id].max;
// Используем >= и <= для надежности
if (target.offset >= obsMin - 0.001 && target.offset <= obsMax + 0.001) {
// Допуск (epsilon) чуть больше, чтобы надежнее ловить пересечения
const EPS = 0.005;
if (target.offset >= obsMin - EPS && target.offset <= obsMax + EPS) {
if (obstacle.offset < center) newMin = Math.max(newMin, 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;
};
// --- RAYCASTING (Исправленный поиск свободного места) ---
// --- RAYCASTING (Поиск коробки под курсором) ---
const getCursorBox = (lx: number, ly: number, parts: Partition[], limitMap: LimitMap) => {
let minX = 0, maxX = 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];
if (pMax - pMin < 0.001) return;
// Используем те же допуски, что и в Solver
const EPSILON = 0.001;
// Используем небольшой допуск, чтобы мышь точно "видела" стенку
const EPS = 0.002;
if (p.axis === 'x') {
// Вертикальная преграда (X)
// Проверяем, перекрывает ли она Y курсора
if (ly >= pMin - EPSILON && ly <= pMax + EPSILON) {
// Вертикальная преграда
if (ly >= pMin - EPS && ly <= pMax + EPS) {
if (p.offset < lx) minX = Math.max(minX, p.offset);
if (p.offset > lx) maxX = Math.min(maxX, p.offset);
}
} else {
// Горизонтальная преграда (Y)
// Проверяем, перекрывает ли она X курсора
if (lx >= pMin - EPSILON && lx <= pMax + EPSILON) {
// Горизонтальная преграда
if (lx >= pMin - EPS && lx <= pMax + EPS) {
if (p.offset < ly) minY = Math.max(minY, 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 };
};
// Поиск соседей для отображения размеров
// Поиск соседей для размеров
const getNeighborOffsets = (offset: number, crossPos: number, axis: Axis, parts: Partition[], limitMap: LimitMap) => {
let min = 0;
let max = 1;
const EPSILON = 0.001;
const EPS = 0.001;
parts.forEach(p => {
if (p.axis === axis) {
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) max = Math.min(max, p.offset);
}
@@ -220,6 +220,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
setPhantomPartition(null);
setHoveredCell(null);
// 1. Находим ячейку
let cellIdx = null;
for (let i = 0; i < sortedX.length - 1; i++) {
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 ly = (ny - cy1) / ch;
// Реальные размеры ячейки в мм (для корректного расчета дистанции мыши)
const realCellW = cw * drawerW;
const realCellH = ch * drawerD;
let found = null;
const SNAP = 0.05;
for (const p of parts) {
@@ -257,20 +262,34 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
if (found) {
setHoveredPartition({ id: found.id, cellKey: key });
} else {
// --- ВАЖНО: Получаем корректные границы с учетом Solver ---
// Вычисляем бокс под курсором
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 newAxis = (minD === distL || minD === distR) ? 'x' : 'y';
const width = box.maxX - box.minX;
const height = box.maxY - box.minY;
if ((newAxis === 'y' && height > 0.05) || (newAxis === 'x' && width > 0.05)) {
// Если мы близко к бокам -> ставим вертикальную (x)
// Если близко к верху/низу -> ставим горизонтальную (y)
let newAxis: Axis = (minD === distL || minD === distR) ? 'x' : 'y';
// Но если мы в центре, выбираем ось, которая делит длинную сторону
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 min = newAxis === 'x' ? box.minY : box.minX;
const max = newAxis === 'x' ? box.maxY : box.maxX;