Skip to content

Commit

Permalink
feat(iges): add boolean tree, csg box & cylinder entities
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 13, 2019
1 parent 43426e5 commit b1db275
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/iges/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@thi.ng/api": "^6.0.1",
"@thi.ng/checks": "2.1.5",
"@thi.ng/defmulti": "^1.0.6",
"@thi.ng/strings": "^1.0.7",
"@thi.ng/transducers": "^5.3.3"
Expand Down
17 changes: 16 additions & 1 deletion packages/iges/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,25 @@ export const enum PolylineMode {
FILLED
}

export const enum BooleanOp {
UNION = 1,
INTERSECTION,
DIFFERENCE
}

export type BooleanNode = number | BooleanTree;

export interface BooleanTree extends Array<BooleanNode> {
[0]: BooleanOp;
}

export const enum EntityType {
POLYLINE = 106,
LINE = 110,
POINT = 116
POINT = 116,
CSG_BOX = 150,
CSG_CYLINDER = 154,
BOOLEAN_TREE = 180
}

export interface EntityOpts {
Expand Down
102 changes: 95 additions & 7 deletions packages/iges/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isArray } from "@thi.ng/checks";
import { defmulti } from "@thi.ng/defmulti";
import {
float,
Expand All @@ -17,6 +18,8 @@ import {
wrap
} from "@thi.ng/transducers";
import {
BooleanNode,
BooleanTree,
DEFAULT_GLOBALS,
DictEntry,
EntityOpts,
Expand Down Expand Up @@ -65,7 +68,7 @@ export const newDocument = (
const $FF = float(globals.precision);
const $PARAM = defmulti<any[], string>((x) => x[1]);
$PARAM.add(Type.INT, (x) => x[0].toString());
$PARAM.add(Type.POINTER, (x) => x[0].toString());
$PARAM.add(Type.POINTER, (x) => (-x[0]).toString());
$PARAM.add(Type.FLOAT, (x) => $FF(x[0]));
$PARAM.add(Type.STR, (x) => x[0]);
$PARAM.add(Type.HSTR, (x) => hstr(x[0]));
Expand Down Expand Up @@ -246,25 +249,29 @@ const addEntity = (
})
);
doc.param.push(...fparams);
return doc;
return did + 1;
};

export const addPolyline = (
doc: IGESDocument,
pts: ArrayLike<number>[],
form = PolylineMode.OPEN,
opts?: Partial<EntityOpts>
) =>
addEntity(
) => {
const is2D = pts[0].length == 2;
const params: Param[] = [
[is2D ? 1 : 2, Type.INT],
[pts.length + (form === PolylineMode.CLOSED ? 1 : 0), Type.INT]
];
is2D && params.push([0, Type.FLOAT]);
return addEntity(
doc,
EntityType.POLYLINE,
{
form: form === PolylineMode.FILLED ? 63 : 11
},
[
[pts[0].length == 2 ? 1 : 2, Type.INT],
[pts.length + (form === PolylineMode.CLOSED ? 1 : 0), Type.INT],
[0, Type.FLOAT],
...params,
...mapcat<number[], Param>(
(p) => map((x) => <Param>[x, Type.FLOAT], p),
form === PolylineMode.CLOSED
Expand All @@ -274,6 +281,7 @@ export const addPolyline = (
],
opts
);
};

export const addPolygon = (
doc: IGESDocument,
Expand Down Expand Up @@ -320,6 +328,86 @@ export const addLine = (
opts
);

export const addBooleanTree = (
doc: IGESDocument,
tree: BooleanTree,
opts?: Partial<EntityOpts>
) => {
const params = postOrder([], tree);
return addEntity(
doc,
EntityType.BOOLEAN_TREE,
null,
[[params.length, Type.INT], ...params],
opts
);
};

const postOrder = (acc: Param[], tree: BooleanNode) => {
if (isArray(tree)) {
postOrder(acc, tree[1]);
postOrder(acc, tree[2]);
acc.push([tree[0], Type.INT]);
} else {
acc.push([tree, Type.POINTER]);
}
return acc;
};

export const addCSGBox = (
doc: IGESDocument,
pos: ArrayLike<number>,
size: ArrayLike<number>,
xaxis: ArrayLike<number> = [1, 0, 0],
zaxis: ArrayLike<number> = [0, 0, 1],
opts?: Partial<EntityOpts>
) =>
addEntity(
doc,
EntityType.CSG_BOX,
null,
[
[size[0], Type.FLOAT],
[size[1], Type.FLOAT],
[size[2], Type.FLOAT],
[pos[0], Type.FLOAT],
[pos[1], Type.FLOAT],
[pos[2], Type.FLOAT],
[xaxis[0], Type.FLOAT],
[xaxis[1], Type.FLOAT],
[xaxis[2], Type.FLOAT],
[zaxis[0], Type.FLOAT],
[zaxis[1], Type.FLOAT],
[zaxis[2], Type.FLOAT]
],
opts
);

export const addCSGCylinder = (
doc: IGESDocument,
pos: ArrayLike<number>,
normal: ArrayLike<number>,
radius: ArrayLike<number>,
height: ArrayLike<number>,
opts?: Partial<EntityOpts>
) =>
addEntity(
doc,
EntityType.CSG_CYLINDER,
null,
[
[pos[0], Type.FLOAT],
[pos[1], Type.FLOAT],
[pos[2], Type.FLOAT],
[normal[0], Type.FLOAT],
[normal[1], Type.FLOAT],
[normal[2], Type.FLOAT],
[radius, Type.FLOAT],
[height, Type.FLOAT]
],
opts
);

// sec 4.23, page 123 (type 126)
// export const addNurbsCurve2d = (doc: IGESDocument, degree: number, pts: number[][], closed = false) => {
// doc; degree; pts; closed;
Expand Down

0 comments on commit b1db275

Please sign in to comment.