Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dividab/abstract-visuals
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinatzo committed Sep 17, 2024
2 parents 7222046 + 538d872 commit 55583e5
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ It will build the packages and call `lerna publish` which will figure out which
[u-ac]: https://www.npmjs.com/package/abstract-chart
[i-ad]: https://img.shields.io/npm/v/abstract-document.svg?style=flat
[u-ad]: https://www.npmjs.com/package/abstract-document
[i-ad]: https://img.shields.io/npm/v/abstract-3d.svg?style=flat
[u-ad3]: https://www.npmjs.com/package/abstract-3d
2 changes: 1 addition & 1 deletion packages/abstract-3d/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abstract-3d",
"version": "0.1.21",
"version": "0.1.22",
"description": "Abstract 3D",
"author": "Divid AB <[email protected]>",
"repository": "https://github.com/dividab/abstract-visuals/tree/master/packages/abstract-3d",
Expand Down
4 changes: 2 additions & 2 deletions packages/abstract-3d/src/abstract-3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ export const vec3PosX = vec3(1, 0, 0);
export const vec3NegX = vec3(-1, 0, 0);
export const vec3PosY = vec3(0, 1, 0);
export const vec3NegY = vec3(0, -1, 0);
export const vec3PosZ = vec3(0, 1, 0);
export const vec3NegZ = vec3(0, -1, 0);
export const vec3PosZ = vec3(0, 0, 1);
export const vec3NegZ = vec3(0, 0, -1);

export const vec2Zero = vec2(0, 0);

Expand Down
1 change: 1 addition & 0 deletions packages/abstract-3d/src/renderers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./dxf";
export * from "./svg";
export * from "./stl";
export * from "./react";
export * from "./step";
1 change: 1 addition & 0 deletions packages/abstract-3d/src/renderers/step/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./step";
117 changes: 117 additions & 0 deletions packages/abstract-3d/src/renderers/step/step-encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { vec3, Vec3 } from "../../abstract-3d";

export const CARTESIAN_POINT = (v: Vec3, cartRef: number): string =>
`#${cartRef} = CARTESIAN_POINT('', (${v.x}, ${v.y}, ${v.z}));`;

export const DIRECTION = (v: Vec3, dirRef: number): string => `#${dirRef} = DIRECTION('',(${v.x}, ${v.y}, ${v.z}));`;

export const VERTEX_POINT = (cartRef: number, vertRef: number): string => `#${vertRef} = VERTEX_POINT('',#${cartRef});`;

export const LINE = (cartRef: number, vecRef: number, lineRef: number): string =>
`#${lineRef} = LINE('',#${cartRef},#${vecRef});;`;

export const VECTOR = (dirRef: number, vecRef: number): string => `#${vecRef} = VECTOR('',#${dirRef},1.);`;

export const EDGE_CURVE = (vert1Ref: number, vert2Ref: number, lineRef: number, edgeRef: number): string =>
`#${edgeRef} = EDGE_CURVE('',#${vert1Ref},#${vert2Ref},#${lineRef},.T.);`;

export const ORIENTED_EDGE = (edgeRef: number, oriRef: number): string =>
`#${oriRef} = ORIENTED_EDGE('',*,*,#${edgeRef},.F.);`;

export const ORIENTED_EDGE_big = (cord1Ref: number, vec1Ref: number, vec2Ref: number, dir: Vec3, i: number): string => {
return `${ORIENTED_EDGE(i + 1, i + 0)}
${EDGE_CURVE(vec2Ref, vec1Ref, i + 2, i + 1)}
${LINE(cord1Ref, i + 3, i + 2)}
${VECTOR(i + 4, i + 3)}
${DIRECTION(dir, i + 4)}`;
};

export const POLY_LOOP = (
cartRef1: number,
cartRef2: number,
cartRef3: number,
cartRef4: number,
polyRef: number
): string => `#${polyRef} = POLY_LOOP('', (#${cartRef1}, #${cartRef2}, #${cartRef3}, #${cartRef4}));`;

export const ADVANCED_FACE = (faceRef: number, planeRef: number, advRef: number): string =>
`#${advRef} = ADVANCED_FACE('',(#${faceRef}),#${planeRef},.T.);`;

export const ADVANCED_FACEbig = (
edge1Ref: number,
edge2Ref: number,
edge3Ref: number,
edge4Ref: number,
planeRef: number,
i: number
): string =>
`${ADVANCED_FACE(i + 1, planeRef, i + 0)}
${FACE_BOUND(i + 2, i + 1)}
${EDGE_LOOP(edge1Ref, edge2Ref, edge3Ref, edge4Ref, i + 2)}`;

export const ADVANCED_FACE2 = (polyRef: number, advRef: number): string =>
`#${advRef} = ADVANCED_FACE('', (#${polyRef}), .T.);`;

export const OPEN_SHELL = (advRef: number, openRef: number): string => `#${openRef} = OPEN_SHELL('',(#${advRef}));`;

export const FACE_BOUND = (edgeRef: number, faceRef: number): string => `#${faceRef} = FACE_BOUND('',#${edgeRef},.T.);`;

export const EDGE_LOOP = (
edge1Ref: number,
edge2Ref: number,
edge3Ref: number,
edge4Ref: number,
loopRef: number
): string => `#${loopRef} = EDGE_LOOP('',(#${edge1Ref},#${edge2Ref},#${edge3Ref},#${edge4Ref}));`;

export const SHELL_BASED_SURFACE_MODEL = (openShellRef: number, shellRef: number): string =>
`#${shellRef} = SHELL_BASED_SURFACE_MODEL('',(#${openShellRef}));`;

export const SHELL_BASED_SURFACE_MODEL_big = (
edge1: number,
edge2: number,
edge3: number,
edge4: number,
planeRef: number,
i: number
): string =>
`${SHELL_BASED_SURFACE_MODEL(i + 1, i + 0)}
${OPEN_SHELL(i + 2, i + 1)}
${ADVANCED_FACEbig(edge1, edge2, edge3, edge4, planeRef, i + 2)}`;

export const MANIFOLD_SURFACE_SHAPE_REPRESENTATION = (axisRef: number, shellRef: number, maniFoldRef: number): string =>
`#${maniFoldRef} = MANIFOLD_SURFACE_SHAPE_REPRESENTATION('',(#${axisRef},#${shellRef}),#1);`;

export const ADVANCED_BREP_SHAPE_REPRESENTATION = (axisRef: number, maniFoldRef: number, advRef: number): string =>
`#${advRef} = ADVANCED_BREP_SHAPE_REPRESENTATION('',(#${axisRef},#${maniFoldRef}),#1);`;

export const CLOSED_SHELL = (
advRef1: number,
advRef2: number,
advRef3: number,
advRef4: number,
advRef5: number,
advRef6: number,
closedRef: number
): string =>
`#${closedRef} = CLOSED_SHELL('', (#${advRef1}, #${advRef2}, #${advRef3}, #${advRef4}, #${advRef5}, #${advRef6}));`;

export const CLOSED_SHELL2 = (
advRef1: number,

closedRef: number
): string => `#${closedRef} = CLOSED_SHELL('', (#${advRef1}));`;

export const MANIFOLD_SOLID_BREP = (closedRef: number, maniRef: number): string =>
`#${maniRef} = MANIFOLD_SOLID_BREP('', #${closedRef});`;

export const AXIS2_PLACEMENT_3D = (cartRef: number, dir1Ref: number, dir2Ref: number, axisRef: number): string =>
`#${axisRef} = AXIS2_PLACEMENT_3D('',#${cartRef},#${dir1Ref},#${dir2Ref});`;

export const PLANE = (axisRef: number, planeRef: number): string => `#${planeRef} = PLANE('',#${axisRef});`;

export const PLANEbig = (vec1: Vec3, vec2: Vec3, i: number): string => `${PLANE(i + 1, i + 0)}
${AXIS2_PLACEMENT_3D(i + 2, i + 3, i + 4, i + 1)}
${CARTESIAN_POINT(vec3(0, 0, 0), i + 2)}
${DIRECTION(vec1, i + 3)}
${DIRECTION(vec2, i + 4)}`;
104 changes: 104 additions & 0 deletions packages/abstract-3d/src/renderers/step/step-geometries/step-box.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import * as A3D from "../../../abstract-3d";
import {
ADVANCED_BREP_SHAPE_REPRESENTATION,
ADVANCED_FACE,
ADVANCED_FACEbig,
AXIS2_PLACEMENT_3D,
CARTESIAN_POINT,
CLOSED_SHELL,
CLOSED_SHELL2,
DIRECTION,
EDGE_CURVE,
EDGE_LOOP,
FACE_BOUND,
LINE,
MANIFOLD_SOLID_BREP,
MANIFOLD_SURFACE_SHAPE_REPRESENTATION,
OPEN_SHELL,
ORIENTED_EDGE,
ORIENTED_EDGE_big,
PLANE,
PLANEbig,
SHELL_BASED_SURFACE_MODEL,
SHELL_BASED_SURFACE_MODEL_big,
VECTOR,
VERTEX_POINT,
} from "../step-encoding";

export function stepBox(
b: A3D.Box,
_m: A3D.Material,
parentPos: A3D.Vec3,
parentRot: A3D.Vec3,
i: number
): readonly [string, number] {
const half = A3D.vec3Scale(b.size, 0.5);
const pos = A3D.vec3TransRot(b.pos, parentPos, parentRot);
const rot = A3D.vec3RotCombine(parentRot, b.rot ?? A3D.vec3Zero);
const vec3tr = (x: number, y: number, z: number): A3D.Vec3 => A3D.vec3TransRot(A3D.vec3(x, y, z), pos, rot);

const v1 = vec3tr(-half.x, -half.y, -half.z);
const v2 = vec3tr(half.x, -half.y, -half.z);
const v3 = vec3tr(half.x, half.y, -half.z);
const v4 = vec3tr(-half.x, half.y, -half.z);
const v5 = vec3tr(-half.x, -half.y, half.z);
const v6 = vec3tr(half.x, -half.y, half.z);
const v7 = vec3tr(half.x, half.y, half.z);
const v8 = vec3tr(-half.x, half.y, half.z);

const step = `
${CARTESIAN_POINT(v1, i + 1)}
${CARTESIAN_POINT(v2, i + 2)}
${CARTESIAN_POINT(v3, i + 3)}
${CARTESIAN_POINT(v4, i + 4)}
${CARTESIAN_POINT(v5, i + 5)}
${CARTESIAN_POINT(v6, i + 6)}
${CARTESIAN_POINT(v7, i + 7)}
${CARTESIAN_POINT(v8, i + 8)}
${VERTEX_POINT(i + 1, i + 9)}
${VERTEX_POINT(i + 2, i + 10)}
${VERTEX_POINT(i + 3, i + 11)}
${VERTEX_POINT(i + 4, i + 12)}
${VERTEX_POINT(i + 5, i + 13)}
${VERTEX_POINT(i + 6, i + 14)}
${VERTEX_POINT(i + 7, i + 15)}
${VERTEX_POINT(i + 8, i + 16)}
${ORIENTED_EDGE_big(i + 1, i + 9, i + 10, A3D.vec3PosX, i + 17)}
${ORIENTED_EDGE_big(i + 2, i + 10, i + 11, A3D.vec3PosY, i + 22)}
${ORIENTED_EDGE_big(i + 3, i + 11, i + 12, A3D.vec3PosX, i + 27)}
${ORIENTED_EDGE_big(i + 4, i + 12, i + 9, A3D.vec3PosY, i + 32)}
${ORIENTED_EDGE_big(i + 8, i + 16, i + 15, A3D.vec3NegX, i + 37)}
${ORIENTED_EDGE_big(i + 7, i + 15, i + 14, A3D.vec3NegY, i + 42)}
${ORIENTED_EDGE_big(i + 6, i + 14, i + 13, A3D.vec3NegX, i + 47)}
${ORIENTED_EDGE_big(i + 5, i + 13, i + 16, A3D.vec3NegY, i + 52)}
${ORIENTED_EDGE_big(i + 1, i + 9, i + 13, A3D.vec3PosZ, i + 57)}
${ORIENTED_EDGE_big(i + 5, i + 13, i + 16, A3D.vec3PosY, i + 62)}
${ORIENTED_EDGE_big(i + 8, i + 16, i + 12, A3D.vec3PosZ, i + 67)}
${ORIENTED_EDGE_big(i + 4, i + 12, i + 9, A3D.vec3PosY, i + 72)}
${ORIENTED_EDGE_big(i + 2, i + 10, i + 14, A3D.vec3PosZ, i + 77)}
${ORIENTED_EDGE_big(i + 6, i + 14, i + 15, A3D.vec3PosY, i + 82)}
${ORIENTED_EDGE_big(i + 7, i + 15, i + 11, A3D.vec3PosZ, i + 87)}
${ORIENTED_EDGE_big(i + 3, i + 11, i + 10, A3D.vec3PosY, i + 92)}
${ORIENTED_EDGE_big(i + 4, i + 12, i + 16, A3D.vec3PosZ, i + 97)}
${ORIENTED_EDGE_big(i + 8, i + 16, i + 15, A3D.vec3PosX, i + 102)}
${ORIENTED_EDGE_big(i + 7, i + 15, i + 11, A3D.vec3PosZ, i + 107)}
${ORIENTED_EDGE_big(i + 3, i + 11, i + 12, A3D.vec3PosX, i + 112)}
${ORIENTED_EDGE_big(i + 1, i + 9, i + 10, A3D.vec3PosX, i + 117)}
${ORIENTED_EDGE_big(i + 2, i + 10, i + 14, A3D.vec3PosZ, i + 122)}
${ORIENTED_EDGE_big(i + 6, i + 14, i + 13, A3D.vec3PosX, i + 127)}
${ORIENTED_EDGE_big(i + 5, i + 13, i + 9, A3D.vec3PosZ, i + 132)}
${ADVANCED_FACEbig(i + 17, i + 22, i + 27, i + 32, i + 170, i + 137)}
${ADVANCED_FACEbig(i + 37, i + 42, i + 47, i + 52, i + 170, i + 142)}
${ADVANCED_FACEbig(i + 57, i + 62, i + 67, i + 72, i + 175, i + 147)}
${ADVANCED_FACEbig(i + 77, i + 82, i + 87, i + 92, i + 175, i + 152)}
${ADVANCED_FACEbig(i + 97, i + 102, i + 107, i + 112, i + 180, i + 157)}
${ADVANCED_FACEbig(i + 117, i + 122, i + 127, i + 132, i + 180, i + 162)}
${ADVANCED_BREP_SHAPE_REPRESENTATION(i + 171, i + 168, i + 169)}
${MANIFOLD_SOLID_BREP(i + 167, i + 168)}
${CLOSED_SHELL(i + 137, i + 142, i + 147, i + 152, i + 157, i + 162, i + 167)}
${PLANEbig(A3D.vec3PosZ, A3D.vec3PosX, i + 170)}
${PLANEbig(A3D.vec3PosX, A3D.vec3PosY, i + 175)}
${PLANEbig(A3D.vec3PosY, A3D.vec3PosZ, i + 180)}`;

return [step, 184];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as A3D from "../../../abstract-3d";
import {
ADVANCED_FACE,
AXIS2_PLACEMENT_3D,
CARTESIAN_POINT,
DIRECTION,
EDGE_CURVE,
EDGE_LOOP,
FACE_BOUND,
LINE,
MANIFOLD_SURFACE_SHAPE_REPRESENTATION,
OPEN_SHELL,
ORIENTED_EDGE,
ORIENTED_EDGE_big,
PLANE,
PLANEbig,
SHELL_BASED_SURFACE_MODEL,
SHELL_BASED_SURFACE_MODEL_big,
VECTOR,
VERTEX_POINT,
} from "../step-encoding";

export function stepPlane(
p: A3D.Plane,
_m: A3D.Material,
parentPos: A3D.Vec3,
parentRot: A3D.Vec3,
i: number
): readonly [string, number] {
const half = A3D.vec2Scale(p.size, 0.5);
const pos = A3D.vec3TransRot(p.pos, parentPos, parentRot);
const rot = A3D.vec3RotCombine(parentRot, p.rot ?? A3D.vec3Zero);
const vec3tr = (x: number, y: number): A3D.Vec3 => A3D.vec3TransRot(A3D.vec3(x, y, 0), pos, rot);

const v1 = vec3tr(-half.x, -half.y);
const v2 = vec3tr(half.x, -half.y);
const v3 = vec3tr(half.x, half.y);
const v4 = vec3tr(-half.x, half.y);

const step = `
${CARTESIAN_POINT(v1, i + 1)}
${CARTESIAN_POINT(v2, i + 2)}
${CARTESIAN_POINT(v3, i + 3)}
${CARTESIAN_POINT(v4, i + 4)}
${VERTEX_POINT(i + 1, i + 5)}
${VERTEX_POINT(i + 2, i + 6)}
${VERTEX_POINT(i + 3, i + 7)}
${VERTEX_POINT(i + 4, i + 8)}
${ORIENTED_EDGE_big(i + 1, i + 5, i + 6, A3D.vec3PosX, i + 9)}
${ORIENTED_EDGE_big(i + 2, i + 6, i + 7, A3D.vec3PosY, i + 14)}
${ORIENTED_EDGE_big(i + 3, i + 7, i + 8, A3D.vec3PosX, i + 19)}
${ORIENTED_EDGE_big(i + 4, i + 8, i + 5, A3D.vec3PosY, i + 24)}
${SHELL_BASED_SURFACE_MODEL_big(i + 9, i + 14, i + 19, i + 24, i + 38, i + 33)}
${MANIFOLD_SURFACE_SHAPE_REPRESENTATION(i + 39, i + 33, i + 43)}
${PLANEbig(A3D.vec3PosZ, A3D.vec3PosX, i + 38)}`;
return [step, 43];
}
82 changes: 82 additions & 0 deletions packages/abstract-3d/src/renderers/step/step.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as A3D from "../../abstract-3d";
import { stepBox } from "./step-geometries/step-box";
import { stepPlane } from "./step-geometries/step-plane";

export const toStep = (scene: A3D.Scene): string => {
let step = "";
let nbrRefs = 14;

for (const g of scene.groups ?? []) {
const [newStep, newNbrRefs] = stepGroup(
g,
scene.center_deprecated ?? A3D.vec3Zero,
scene.rotation_deprecated ?? A3D.vec3Zero,
nbrRefs
);
step += newStep;
nbrRefs += newNbrRefs;
}

return `ISO-10303-21;
HEADER;
FILE_DESCRIPTION(('FreeCAD Model'),'2;1');
FILE_NAME('Open CASCADE Shape Model','2024-09-10T08:42:01',('Author'),(
''),'Open CASCADE STEP processor 7.6','FreeCAD','Unknown');
FILE_SCHEMA(('AUTOMOTIVE_DESIGN'));
ENDSEC;
DATA;${step}
#1 = ( GEOMETRIC_REPRESENTATION_CONTEXT(3)
GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#5)) GLOBAL_UNIT_ASSIGNED_CONTEXT(
(#2,#3,#4)) REPRESENTATION_CONTEXT('Context #1',
'3D Context with UNIT and UNCERTAINTY') );
#2 = ( LENGTH_UNIT() NAMED_UNIT(*) SI_UNIT(.MILLI.,.METRE.) );
#3 = ( NAMED_UNIT(*) PLANE_ANGLE_UNIT() SI_UNIT($,.RADIAN.) );
#4 = ( NAMED_UNIT(*) SI_UNIT($,.STERADIAN.) SOLID_ANGLE_UNIT() );
#5 = UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.E-07),#2,
'distance_accuracy_value','confusion accuracy');
#6 = SURFACE_STYLE_USAGE(.BOTH.,#7);
#7 = SURFACE_SIDE_STYLE('',(#8));
#8 = SURFACE_STYLE_FILL_AREA(#9);
#9 = FILL_AREA_STYLE('',(#10));
#10 = FILL_AREA_STYLE_COLOUR('',#11);
#11 = COLOUR_RGB('',0.800000010877,0.800000010877,0.800000010877);
#12 = CURVE_STYLE('',#13,POSITIVE_LENGTH_MEASURE(0.1),#14);
#13 = DRAUGHTING_PRE_DEFINED_CURVE_FONT('continuous');
#14 = COLOUR_RGB('',9.803921802644E-02,9.803921802644E-02,
9.803921802644E-02);
ENDSEC;
END-ISO-10303-21;`;
};

function stepGroup(g: A3D.Group, parentPos: A3D.Vec3, parentRot: A3D.Vec3, refIdx: number): [string, number] {
let step = "";
let nbrRefs = 0;
const pos = A3D.vec3TransRot(g.pos, parentPos, parentRot);
const rot = A3D.vec3RotCombine(parentRot, g.rot ?? A3D.vec3Zero);
for (const m of g.meshes ?? []) {
switch (m.geometry.type) {
case "Box": {
const [newStep, newNbrRefs] = stepBox(m.geometry, m.material, pos, rot, refIdx + nbrRefs);
step += newStep;
nbrRefs += newNbrRefs;
break;
}
case "Plane": {
const [newStep, newNbrRefs] = stepPlane(m.geometry, m.material, pos, rot, refIdx + nbrRefs);
step += newStep;
nbrRefs += newNbrRefs;
break;
}
default:
break;
}
}

for (const c of g.groups ?? []) {
const [newStep, newNbrRefs] = stepGroup(c, pos, rot, refIdx + nbrRefs);
step += newStep;
nbrRefs += newNbrRefs;
}

return [step, nbrRefs];
}
Loading

0 comments on commit 55583e5

Please sign in to comment.