Skip to content

Commit

Permalink
Add group inner block
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Jan 27, 2025
1 parent d2896aa commit c330614
Show file tree
Hide file tree
Showing 17 changed files with 6,381 additions and 472,575 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8,080 changes: 0 additions & 8,080 deletions doc/files/parsing problem/new/AF6_Sheet_2.dxf

This file was deleted.

167,210 changes: 0 additions & 167,210 deletions doc/files/parsing problem/new/LB-FileLaser-DXF.dxf

This file was deleted.

165,018 changes: 0 additions & 165,018 deletions doc/files/parsing problem/new/Nativity.dxf

This file was deleted.

110,528 changes: 0 additions & 110,528 deletions doc/files/parsing problem/new/Nest me.dxf

This file was deleted.

21,734 changes: 0 additions & 21,734 deletions doc/files/parsing problem/new/rotatedtest.dxf

This file was deleted.

6,276 changes: 6,276 additions & 0 deletions doc/files/with inner /simple inner.dxf

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions server/core/dxf/combineSegmentsIntoPolygons.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ export function combineSegmentsIntoPolygons(originOpen, tolerance = 1e-7) {
}
}

// remove dublicates points from closed polygons
closed.forEach((polygon) => {
polygon.polygon = polygon.polygon.filter(
(point, index, self) =>
index ===
self.findIndex(
(t) => t.x === point.x && t.y === point.y
)
);
});

return { closed, open };
}

Expand Down
77 changes: 77 additions & 0 deletions server/core/dxf/innercheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Takes an array of polygon objects {polygon, originEntities}
* and merges any polygons that are entirely inside another polygon.
*
* @param {Array} polygons
* @returns {Array} A new array of polygons after merging
*/
export function groupInnerPolygons(polygons) {
const nestedIndices = new Set();

for (let i = 0; i < polygons.length; i++) {
const polyA = polygons[i];

for (let j = 0; j < polygons.length; j++) {
if (i === j) continue;

const polyB = polygons[j];

// Check if polyA is fully inside polyB
if (isPolygonInsidePolygon(polyA.polygon, polyB.polygon)) {
// Move polyA's entities into polyB
polyB.originEntities.push(...polyA.originEntities);
if (!polyB.innerPolygons) {
polyB.innerPolygons = [];
}
polyB.innerPolygons.push(polyA.polygon);

// Mark polyA for removal (since it’s merged)
nestedIndices.add(i);
// Once merged, no need to check other polygons for polyA
break;
}
}
}

// Filter out polygons that got merged into others
const mergedPolygons = polygons.filter((_, idx) => !nestedIndices.has(idx));
return mergedPolygons;
}

/**
* Checks if polygon A is fully inside polygon B.
* Here we simply check that *every* vertex of A is in B.
*
* @param {{x:number,y:number}[]} polygonA
* @param {{x:number,y:number}[]} polygonB
* @returns {boolean}
*/
function isPolygonInsidePolygon(polygonA, polygonB) {
// Simple approach: verify every vertex of A is inside B
return polygonA.every((point) => isPointInPolygon(point, polygonB));
}

/**
* Ray-casting test to check if a given point is inside a polygon.
* polygon is assumed to be an array of {x, y}.
*
* @param {{x:number,y:number}} pt
* @param {{x:number,y:number}[]} polygon
* @returns {boolean} true if inside, false otherwise
*/
function isPointInPolygon(pt, polygon) {
let c = false;
// For each edge of the polygon
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const pi = polygon[i];
const pj = polygon[j];

// Check if the ray crosses this edge
const intersect =
pi.y > pt.y !== pj.y > pt.y &&
pt.x < ((pj.x - pi.x) * (pt.y - pi.y)) / (pj.y - pi.y) + pi.x;

if (intersect) c = !c;
}
return c;
}
8 changes: 7 additions & 1 deletion server/core/dxf/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { entityToPoints } from "./entityToPoints.js";
import { combineSegmentsIntoPolygons } from "./combineSegmentsIntoPolygons.js";
import { groupInnerPolygons } from "./innercheck.js";

/**
* @param {Array} dxfEntities
Expand Down Expand Up @@ -45,7 +46,12 @@ export function parseAndCombine(dxfObject, tolerance) {

const allClosed = [...originClose, ...closed];

return { closed: allClosed, open: open };
const mergedClosed = groupInnerPolygons(allClosed);

return {
closed: mergedClosed,
open: open,
};
}

/**
Expand Down
12 changes: 9 additions & 3 deletions server/core/nest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function nest(jobId) {
params.width,
params.height,
params.space,
params.tolerance,
params.tolerance
);

if (
Expand Down Expand Up @@ -145,10 +145,16 @@ async function performJaguarRequest(
width,
height,
space,
tolerance,
tolerance
) {
const db = await connectDB();
const jaguarRequest = buildNestJson(polygonesWithCount, width, height, space, tolerance);
const jaguarRequest = buildNestJson(
polygonesWithCount,
width,
height,
space,
tolerance
);

await db
.collection("nest_request")
Expand Down
2 changes: 1 addition & 1 deletion server/core/svg/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function createSVGFromPolygons(closedPolygons) {
const color = colors[index % colors.length];

// Add mirrored polygon
svgContent += ` <polygon points="${mirroredPointsStr}" stroke="#000000" fill="${color}" stroke-width="1"/>\n`;
svgContent += `<polygon points="${mirroredPointsStr}" stroke="#000000" fill="${color}" stroke-width="1"/>\n`;
});

svgContent += `</svg>`;
Expand Down

0 comments on commit c330614

Please sign in to comment.