Skip to content

Commit d05173d

Browse files
additional edge unit tests
1 parent 146909c commit d05173d

File tree

1 file changed

+306
-0
lines changed

1 file changed

+306
-0
lines changed

packages/dev/occt/lib/services/shapes/edge.test.ts

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,4 +1259,310 @@ describe("OCCT edge unit tests", () => {
12591259
circle.delete();
12601260
edges.forEach(e => e.delete());
12611261
};
1262+
1263+
// fromBaseLine tests
1264+
it("should create an edge from base line", async () => {
1265+
const line: Inputs.Base.Line3 = { start: [0, 0, 0], end: [1, 1, 1] };
1266+
const e = edge.fromBaseLine({ line });
1267+
const length = edge.getEdgeLength({ shape: e });
1268+
expect(length).toBeCloseTo(Math.sqrt(3), closeToNr);
1269+
e.delete();
1270+
});
1271+
1272+
// fromBaseLines tests
1273+
it("should create edges from base lines", async () => {
1274+
const lines: Inputs.Base.Line3[] = [
1275+
{ start: [0, 0, 0], end: [1, 0, 0] },
1276+
{ start: [0, 0, 0], end: [0, 2, 0] },
1277+
{ start: [0, 0, 0], end: [0, 0, 3] }
1278+
];
1279+
const edges = edge.fromBaseLines({ lines });
1280+
expect(edges.length).toBe(3);
1281+
const lengths = edge.getEdgesLengths({ shapes: edges });
1282+
expect(lengths[0]).toBeCloseTo(1, closeToNr);
1283+
expect(lengths[1]).toBeCloseTo(2, closeToNr);
1284+
expect(lengths[2]).toBeCloseTo(3, closeToNr);
1285+
edges.forEach(e => e.delete());
1286+
});
1287+
1288+
// fromBaseSegment tests
1289+
it("should create an edge from base segment", async () => {
1290+
const segment: Inputs.Base.Segment3 = [[0, 0, 0], [2, 2, 2]];
1291+
const e = edge.fromBaseSegment({ segment });
1292+
const length = edge.getEdgeLength({ shape: e });
1293+
expect(length).toBeCloseTo(Math.sqrt(12), closeToNr);
1294+
e.delete();
1295+
});
1296+
1297+
// fromBaseSegments tests
1298+
it("should create edges from base segments", async () => {
1299+
const segments: Inputs.Base.Segment3[] = [
1300+
[[0, 0, 0], [1, 0, 0]],
1301+
[[0, 0, 0], [0, 2, 0]],
1302+
[[0, 0, 0], [0, 0, 3]]
1303+
];
1304+
const edges = edge.fromBaseSegments({ segments });
1305+
expect(edges.length).toBe(3);
1306+
const lengths = edge.getEdgesLengths({ shapes: edges });
1307+
expect(lengths[0]).toBeCloseTo(1, closeToNr);
1308+
expect(lengths[1]).toBeCloseTo(2, closeToNr);
1309+
expect(lengths[2]).toBeCloseTo(3, closeToNr);
1310+
edges.forEach(e => e.delete());
1311+
});
1312+
1313+
// fromPoints tests
1314+
it("should create edges from points", async () => {
1315+
const points: Inputs.Base.Point3[] = [
1316+
[0, 0, 0],
1317+
[1, 0, 0],
1318+
[1, 1, 0],
1319+
[0, 1, 0]
1320+
];
1321+
const edges = edge.fromPoints({ points });
1322+
expect(edges.length).toBe(3);
1323+
const lengths = edge.getEdgesLengths({ shapes: edges });
1324+
expect(lengths[0]).toBeCloseTo(1, closeToNr);
1325+
expect(lengths[1]).toBeCloseTo(1, closeToNr);
1326+
expect(lengths[2]).toBeCloseTo(1, closeToNr);
1327+
edges.forEach(e => e.delete());
1328+
});
1329+
1330+
it("should create no edges from single point", async () => {
1331+
const points: Inputs.Base.Point3[] = [[0, 0, 0]];
1332+
const edges = edge.fromPoints({ points });
1333+
expect(edges.length).toBe(0);
1334+
});
1335+
1336+
// fromBasePolyline tests
1337+
it("should create edges from open polyline", async () => {
1338+
const polyline: Inputs.Base.Polyline3 = {
1339+
points: [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]],
1340+
isClosed: false
1341+
};
1342+
const edges = edge.fromBasePolyline({ polyline });
1343+
expect(edges.length).toBe(3);
1344+
edges.forEach(e => e.delete());
1345+
});
1346+
1347+
it("should create edges from closed polyline", async () => {
1348+
const polyline: Inputs.Base.Polyline3 = {
1349+
points: [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]],
1350+
isClosed: true
1351+
};
1352+
const edges = edge.fromBasePolyline({ polyline });
1353+
expect(edges.length).toBe(4);
1354+
const totalLength = edge.getEdgesLengths({ shapes: edges }).reduce((a, b) => a + b, 0);
1355+
expect(totalLength).toBeCloseTo(4, closeToNr);
1356+
edges.forEach(e => e.delete());
1357+
});
1358+
1359+
// fromBaseTriangle tests
1360+
it("should create edges from triangle", async () => {
1361+
const triangle: Inputs.Base.Triangle3 = [
1362+
[0, 0, 0],
1363+
[1, 0, 0],
1364+
[0.5, 1, 0]
1365+
];
1366+
const edges = edge.fromBaseTriangle({ triangle });
1367+
expect(edges.length).toBe(3);
1368+
edges.forEach(e => e.delete());
1369+
});
1370+
1371+
// fromBaseMesh tests
1372+
it("should create edges from mesh", async () => {
1373+
const mesh: Inputs.Base.Mesh3 = [
1374+
[[0, 0, 0], [1, 0, 0], [0.5, 1, 0]],
1375+
[[1, 0, 0], [2, 0, 0], [1.5, 1, 0]]
1376+
];
1377+
const edges = edge.fromBaseMesh({ mesh });
1378+
expect(edges.length).toBe(6); // 2 triangles * 3 edges each
1379+
edges.forEach(e => e.delete());
1380+
});
1381+
1382+
// pointsOnEdgesAtParam tests
1383+
it("should get points on multiple edges at param", async () => {
1384+
const e1 = edge.line({ start: [0, 0, 0], end: [2, 0, 0] });
1385+
const e2 = edge.line({ start: [0, 0, 0], end: [0, 4, 0] });
1386+
const e3 = edge.line({ start: [0, 0, 0], end: [0, 0, 6] });
1387+
const points = edge.pointsOnEdgesAtParam({ shapes: [e1, e2, e3], param: 0.5 });
1388+
expect(points.length).toBe(3);
1389+
expect(points[0][0]).toBeCloseTo(1, closeToNr);
1390+
expect(points[1][1]).toBeCloseTo(2, closeToNr);
1391+
expect(points[2][2]).toBeCloseTo(3, closeToNr);
1392+
e1.delete();
1393+
e2.delete();
1394+
e3.delete();
1395+
});
1396+
1397+
// tangentsOnEdgesAtParam tests
1398+
it("should get tangents on multiple edges at param", async () => {
1399+
const e1 = edge.line({ start: [0, 0, 0], end: [2, 0, 0] });
1400+
const e2 = edge.line({ start: [0, 0, 0], end: [0, 4, 0] });
1401+
const tangents = edge.tangentsOnEdgesAtParam({ shapes: [e1, e2], param: 0.5 });
1402+
expect(tangents.length).toBe(2);
1403+
// Tangent on a line points in direction of the line (normalized unit vectors)
1404+
expect(tangents[0][0]).toBeCloseTo(1, closeToNr);
1405+
expect(tangents[0][1]).toBeCloseTo(0, closeToNr);
1406+
expect(tangents[0][2]).toBeCloseTo(0, closeToNr);
1407+
expect(tangents[1][0]).toBeCloseTo(0, closeToNr);
1408+
expect(tangents[1][1]).toBeCloseTo(1, closeToNr);
1409+
expect(tangents[1][2]).toBeCloseTo(0, closeToNr);
1410+
e1.delete();
1411+
e2.delete();
1412+
});
1413+
1414+
// startPointsOnEdges tests
1415+
it("should get start points on multiple edges", async () => {
1416+
const e1 = edge.line({ start: [1, 2, 3], end: [4, 5, 6] });
1417+
const e2 = edge.line({ start: [7, 8, 9], end: [10, 11, 12] });
1418+
const startPoints = edge.startPointsOnEdges({ shapes: [e1, e2] });
1419+
expect(startPoints.length).toBe(2);
1420+
expect(startPoints[0]).toEqual([1, 2, 3]);
1421+
expect(startPoints[1]).toEqual([7, 8, 9]);
1422+
e1.delete();
1423+
e2.delete();
1424+
});
1425+
1426+
// endPointsOnEdges tests
1427+
it("should get end points on multiple edges", async () => {
1428+
const e1 = edge.line({ start: [1, 2, 3], end: [4, 5, 6] });
1429+
const e2 = edge.line({ start: [7, 8, 9], end: [10, 11, 12] });
1430+
const endPoints = edge.endPointsOnEdges({ shapes: [e1, e2] });
1431+
expect(endPoints.length).toBe(2);
1432+
expect(endPoints[0]).toEqual([4, 5, 6]);
1433+
expect(endPoints[1]).toEqual([10, 11, 12]);
1434+
e1.delete();
1435+
e2.delete();
1436+
});
1437+
1438+
// pointsOnEdgesAtLength tests
1439+
it("should get points on multiple edges at length", async () => {
1440+
const e1 = edge.line({ start: [0, 0, 0], end: [4, 0, 0] });
1441+
const e2 = edge.line({ start: [0, 0, 0], end: [0, 4, 0] });
1442+
const points = edge.pointsOnEdgesAtLength({ shapes: [e1, e2], length: 2 });
1443+
expect(points.length).toBe(2);
1444+
expect(points[0][0]).toBeCloseTo(2, closeToNr);
1445+
expect(points[0][1]).toBeCloseTo(0, closeToNr);
1446+
expect(points[1][0]).toBeCloseTo(0, closeToNr);
1447+
expect(points[1][1]).toBeCloseTo(2, closeToNr);
1448+
e1.delete();
1449+
e2.delete();
1450+
});
1451+
1452+
// tangentsOnEdgesAtLength tests
1453+
it("should get tangents on multiple edges at length", async () => {
1454+
const e1 = edge.line({ start: [0, 0, 0], end: [4, 0, 0] });
1455+
const e2 = edge.line({ start: [0, 0, 0], end: [0, 4, 0] });
1456+
const tangents = edge.tangentsOnEdgesAtLength({ shapes: [e1, e2], length: 1 });
1457+
expect(tangents.length).toBe(2);
1458+
expect(tangents[0][0]).toBeCloseTo(4, closeToNr);
1459+
expect(tangents[0][1]).toBeCloseTo(0, closeToNr);
1460+
expect(tangents[1][0]).toBeCloseTo(0, closeToNr);
1461+
expect(tangents[1][1]).toBeCloseTo(4, closeToNr);
1462+
e1.delete();
1463+
e2.delete();
1464+
});
1465+
1466+
// divideEdgesByParamsToPoints tests
1467+
it("should divide multiple edges by params to points", async () => {
1468+
const e1 = edge.createCircleEdge({ radius: 1, center: [0, 0, 0], direction: [0, 1, 0] });
1469+
const e2 = edge.createCircleEdge({ radius: 2, center: [0, 0, 0], direction: [0, 1, 0] });
1470+
const pointsArrays = edge.divideEdgesByParamsToPoints({
1471+
shapes: [e1, e2],
1472+
nrOfDivisions: 4,
1473+
removeEndPoint: false,
1474+
removeStartPoint: false
1475+
});
1476+
expect(pointsArrays.length).toBe(2);
1477+
expect(pointsArrays[0].length).toBe(5);
1478+
expect(pointsArrays[1].length).toBe(5);
1479+
e1.delete();
1480+
e2.delete();
1481+
});
1482+
1483+
// divideEdgesByEqualDistanceToPoints tests
1484+
it("should divide multiple edges by equal distance to points", async () => {
1485+
const e1 = edge.createCircleEdge({ radius: 1, center: [0, 0, 0], direction: [0, 1, 0] });
1486+
const e2 = edge.createCircleEdge({ radius: 2, center: [0, 0, 0], direction: [0, 1, 0] });
1487+
const pointsArrays = edge.divideEdgesByEqualDistanceToPoints({
1488+
shapes: [e1, e2],
1489+
nrOfDivisions: 4,
1490+
removeEndPoint: false,
1491+
removeStartPoint: false
1492+
});
1493+
expect(pointsArrays.length).toBe(2);
1494+
expect(pointsArrays[0].length).toBe(5);
1495+
expect(pointsArrays[1].length).toBe(5);
1496+
e1.delete();
1497+
e2.delete();
1498+
});
1499+
1500+
// isEdgeLinear tests
1501+
it("should return true for linear edge", async () => {
1502+
const e = edge.line({ start: [0, 0, 0], end: [1, 1, 1] });
1503+
const isLinear = edge.isEdgeLinear({ shape: e });
1504+
expect(isLinear).toBe(true);
1505+
e.delete();
1506+
});
1507+
1508+
it("should return false for circular edge when checking if linear", async () => {
1509+
const e = edge.createCircleEdge({ radius: 1, center: [0, 0, 0], direction: [0, 1, 0] });
1510+
const isLinear = edge.isEdgeLinear({ shape: e });
1511+
expect(isLinear).toBe(false);
1512+
e.delete();
1513+
});
1514+
1515+
it("should return false for arc edge when checking if linear", async () => {
1516+
const e = edge.arcThroughThreePoints({ start: [0, 0, 0], middle: [1, 1, 0], end: [2, 0, 0] });
1517+
const isLinear = edge.isEdgeLinear({ shape: e });
1518+
expect(isLinear).toBe(false);
1519+
e.delete();
1520+
});
1521+
1522+
// isEdgeCircular tests
1523+
it("should return true for circular edge", async () => {
1524+
const e = edge.createCircleEdge({ radius: 1, center: [0, 0, 0], direction: [0, 1, 0] });
1525+
const isCircular = edge.isEdgeCircular({ shape: e });
1526+
expect(isCircular).toBe(true);
1527+
e.delete();
1528+
});
1529+
1530+
it("should return true for arc edge when checking if circular", async () => {
1531+
const e = edge.arcThroughThreePoints({ start: [0, 0, 0], middle: [1, 1, 0], end: [2, 0, 0] });
1532+
const isCircular = edge.isEdgeCircular({ shape: e });
1533+
expect(isCircular).toBe(true);
1534+
e.delete();
1535+
});
1536+
1537+
it("should return false for linear edge when checking if circular", async () => {
1538+
const e = edge.line({ start: [0, 0, 0], end: [1, 1, 1] });
1539+
const isCircular = edge.isEdgeCircular({ shape: e });
1540+
expect(isCircular).toBe(false);
1541+
e.delete();
1542+
});
1543+
1544+
// getEdgeLengthsOfShape tests
1545+
it("should get edge lengths of a box shape", async () => {
1546+
const box = occHelper.entitiesService.bRepPrimAPIMakeBox(2, 3, 4, [0, 0, 0]);
1547+
const lengths = edge.getEdgeLengthsOfShape({ shape: box });
1548+
expect(lengths.length).toBe(12);
1549+
// Box has 4 edges of each dimension
1550+
const sortedLengths = lengths.sort((a, b) => a - b);
1551+
expect(sortedLengths[0]).toBeCloseTo(2, closeToNr);
1552+
expect(sortedLengths[4]).toBeCloseTo(3, closeToNr);
1553+
expect(sortedLengths[8]).toBeCloseTo(4, closeToNr);
1554+
box.delete();
1555+
});
1556+
1557+
it("should get edge lengths of a cylinder shape", async () => {
1558+
const cylinder = occHelper.entitiesService.bRepPrimAPIMakeCylinder([0, 0, 0], [0, 1, 0], 1, 2, Math.PI * 2);
1559+
const lengths = edge.getEdgeLengthsOfShape({ shape: cylinder });
1560+
expect(lengths.length).toBe(3);
1561+
// Cylinder has 2 circular edges and 1 linear edge (seam)
1562+
const sortedLengths = lengths.sort((a, b) => a - b);
1563+
expect(sortedLengths[0]).toBeCloseTo(2, closeToNr); // height
1564+
expect(sortedLengths[1]).toBeCloseTo(2 * Math.PI, closeToNr); // circumference
1565+
expect(sortedLengths[2]).toBeCloseTo(2 * Math.PI, closeToNr); // circumference
1566+
cylinder.delete();
1567+
});
12621568
});

0 commit comments

Comments
 (0)