From 871dcbf8fc258fb44997617b2439dbe1c40c64ea Mon Sep 17 00:00:00 2001 From: "Val Erastov (xibyte)" Date: Fri, 31 Jul 2020 02:30:50 -0700 Subject: [PATCH] translation API --- modules/engine/api.ts | 21 +++- modules/engine/data/brepInputData.ts | 94 ++++++++++++++++ .../engine/impl/wasm/GenericWASMEngine_V1.ts | 4 + modules/engine/impl/wasm/externals.d.ts | 1 + modules/math/matrix.ts | 9 ++ web/app/cad/sandbox.ts | 101 ++---------------- 6 files changed, 136 insertions(+), 94 deletions(-) diff --git a/modules/engine/api.ts b/modules/engine/api.ts index ae6e71d8..6e837000 100644 --- a/modules/engine/api.ts +++ b/modules/engine/api.ts @@ -4,6 +4,7 @@ import {Vec3} from "math/vec"; import {PrimitiveData} from "engine/data/primitiveData"; import {EdgeTessellation, FaceTessellation, Tessellation2D} from "engine/tessellation"; import {BrepInputData} from "engine/data/brepInputData"; +import {Matrix3x4FlatData} from "math/matrix"; export enum BooleanType { UNION = 1, @@ -372,11 +373,12 @@ export interface EngineAPI_V1 { /** * Load arbitrary BREP data into the engine + * See example at @BrepInputData */ loadModel(brep: BrepInputData): BrepOutputData; /** - * Load arbitrary BREP data into the engine + * Load arbitrary BREP data into the engine. */ tessellate(request: { @@ -398,6 +400,23 @@ export interface EngineAPI_V1 { }; + /** + * Applying transformation matrix + */ + transform(request: { + + /** + * Engine object reference ot transform + */ + model: Handle, + + /** + * Transformation matrix represented as a flat array + */ + matrix: Matrix3x4FlatData; + + }): BrepOutputData; + } export interface EngineSession { diff --git a/modules/engine/data/brepInputData.ts b/modules/engine/data/brepInputData.ts index 7630bd05..17bada00 100644 --- a/modules/engine/data/brepInputData.ts +++ b/modules/engine/data/brepInputData.ts @@ -2,6 +2,7 @@ import {SurfaceData} from "./surfaceData"; import {CurveData} from "./curveData"; import {Vec3} from "math/vec"; + export interface BrepInputData { vertices: { @@ -31,3 +32,96 @@ export interface BrepInputData { }[]; } + +export const CubeExample: () => BrepInputData = () => ({ + vertices: { + A: [0,0,500], + B: [500,0,500], + C: [500,500,500], + D: [0,500,500], + + AA: [0,0,0], + BB: [500,0,0], + CC: [500,500,0], + DD: [0,500,0] + }, + + // curves: {}, + surfaces: { + top: { + TYPE: 'PLANE', + normal: [0,0,1], + origin: [0,0,500] + }, + bottom: { + TYPE: 'PLANE', + normal: [0,0,-1], + origin: [0,0,0] + }, + wall1: { + TYPE: 'PLANE', + normal: [0,-1,0], + origin: [0,0,0] + }, + wall2: { + TYPE: 'PLANE', + normal: [1,0,0], + origin: [500,0,0] + }, + wall3: { + TYPE: 'PLANE', + normal: [0,1,0], + origin: [0,500,0] + }, + wall4: { + TYPE: 'PLANE', + normal: [-1,0,0], + origin: [0,0,0] + }, + }, + + edges: { + AB: {a: 'A', b: 'B'}, + BC: {a: 'B', b: 'C'}, + CD: {a: 'C', b: 'D'}, + DA: {a: 'D', b: 'A'}, + + AA_BB: {a: 'AA', b: 'BB'}, + BB_CC: {a: 'BB', b: 'CC'}, + CC_DD: {a: 'CC', b: 'DD'}, + DD_AA: {a: 'DD', b: 'AA'}, + + A_AA: {a: 'A', b: 'AA'}, + B_BB: {a: 'B', b: 'BB'}, + C_CC: {a: 'C', b: 'CC'}, + D_DD: {a: 'D', b: 'DD'}, + }, + + faces: [ + { + surface: 'top', + loops: [['AB', 'BC', 'CD', 'DA']] + }, + { + surface: 'bottom', + loops: [['AA_BB', 'BB_CC', 'CC_DD', 'DD_AA']] + }, + { + surface: 'wall1', + loops: [['AB', 'B_BB', 'AA_BB', 'A_AA']] + }, + { + surface: 'wall2', + loops: [['BC', 'C_CC', 'BB_CC', 'B_BB']] + }, + { + surface: 'wall3', + loops: [['CD', 'D_DD', 'CC_DD', 'C_CC']] + }, + { + surface: 'wall4', + loops: [['DA', 'A_AA', 'DD_AA', 'D_DD']] + }, + ] + +}); diff --git a/modules/engine/impl/wasm/GenericWASMEngine_V1.ts b/modules/engine/impl/wasm/GenericWASMEngine_V1.ts index 00d922db..262e04e8 100644 --- a/modules/engine/impl/wasm/GenericWASMEngine_V1.ts +++ b/modules/engine/impl/wasm/GenericWASMEngine_V1.ts @@ -61,4 +61,8 @@ export class GenericWASMEngine_V1 implements EngineAPI_V1 { return callEngine(params, Module._SPI_tessellate); } + transform(params) { + return callEngine(params, Module._SPI_transform); + } + } \ No newline at end of file diff --git a/modules/engine/impl/wasm/externals.d.ts b/modules/engine/impl/wasm/externals.d.ts index ff20fe56..615ecf8e 100644 --- a/modules/engine/impl/wasm/externals.d.ts +++ b/modules/engine/impl/wasm/externals.d.ts @@ -14,4 +14,5 @@ declare var Module: { _SPI_fillet: Function; _SPI_loadModel: Function; _SPI_tessellate: Function; + _SPI_transform: Function; }; diff --git a/modules/math/matrix.ts b/modules/math/matrix.ts index ee76dab5..d7b82109 100644 --- a/modules/math/matrix.ts +++ b/modules/math/matrix.ts @@ -2,6 +2,7 @@ import Vector, {AXIS} from "math/vector"; import {Vec3} from "math/vec"; export type Matrix3x4Data = [[number, number, number, number], [number, number, number, number], [number, number, number, number]]; +export type Matrix3x4FlatData = [number, number, number, number, number, number, number, number, number, number, number, number]; export class Matrix3x4 { @@ -166,6 +167,14 @@ export class Matrix3x4 { ]; }; + toFlatArray(): Matrix3x4FlatData { + return [ + this.mxx, this.mxy, this.mxz, this.tx, + this.myx, this.myy, this.myz, this.ty, + this.mzx, this.mzy, this.mzz, this.tz + ]; + }; + invert(): Matrix3x4 { return this.__invert(new Matrix3x4()); }; diff --git a/web/app/cad/sandbox.ts b/web/app/cad/sandbox.ts index e0e7efea..52805d83 100644 --- a/web/app/cad/sandbox.ts +++ b/web/app/cad/sandbox.ts @@ -7,10 +7,11 @@ import NurbsSurface from 'geom/surfaces/nurbsSurface'; import {createOctreeFromSurface, traverseOctree} from "voxels/octree"; import {Matrix3x4} from 'math/matrix'; import {AXIS, ORIGIN} from "math/vector"; -import {BrepInputData} from "engine/data/brepInputData"; +import {BrepInputData, CubeExample} from "engine/data/brepInputData"; import {Vec3} from "math/vec"; import {ApplicationContext} from "context"; import {readShellEntityFromJson} from "./scene/wrappers/entityIO"; +import {DEG_RAD} from "math/commons"; export function runSandbox(ctx: ApplicationContext) { @@ -285,100 +286,14 @@ export function runSandbox(ctx: ApplicationContext) { function testLoadBrep() { - const box: BrepInputData = { - vertices: { - A: [0,0,500], - B: [500,0,500], - C: [500,500,500], - D: [0,500,500], + const box: BrepInputData = CubeExample(); - AA: [0,0,0], - BB: [500,0,0], - CC: [500,500,0], - DD: [0,500,0] - }, + let data = ctx.craftEngine.modellingEngine.loadModel(box); - // curves: {}, - surfaces: { - top: { - TYPE: 'PLANE', - normal: [0,0,1], - origin: [0,0,500] - }, - bottom: { - TYPE: 'PLANE', - normal: [0,0,-1], - origin: [0,0,0] - }, - wall1: { - TYPE: 'PLANE', - normal: [0,-1,0], - origin: [0,0,0] - }, - wall2: { - TYPE: 'PLANE', - normal: [1,0,0], - origin: [500,0,0] - }, - wall3: { - TYPE: 'PLANE', - normal: [0,1,0], - origin: [0,500,0] - }, - wall4: { - TYPE: 'PLANE', - normal: [-1,0,0], - origin: [0,0,0] - }, - }, - - edges: { - AB: {a: 'A', b: 'B'}, - BC: {a: 'B', b: 'C'}, - CD: {a: 'C', b: 'D'}, - DA: {a: 'D', b: 'A'}, - - AA_BB: {a: 'AA', b: 'BB'}, - BB_CC: {a: 'BB', b: 'CC'}, - CC_DD: {a: 'CC', b: 'DD'}, - DD_AA: {a: 'DD', b: 'AA'}, - - A_AA: {a: 'A', b: 'AA'}, - B_BB: {a: 'B', b: 'BB'}, - C_CC: {a: 'C', b: 'CC'}, - D_DD: {a: 'D', b: 'DD'}, - }, - - faces: [ - { - surface: 'top', - loops: [['AB', 'BC', 'CD', 'DA']] - }, - { - surface: 'bottom', - loops: [['AA_BB', 'BB_CC', 'CC_DD', 'DD_AA']] - }, - { - surface: 'wall1', - loops: [['AB', 'B_BB', 'AA_BB', 'A_AA']] - }, - { - surface: 'wall2', - loops: [['BC', 'C_CC', 'BB_CC', 'B_BB']] - }, - { - surface: 'wall3', - loops: [['CD', 'D_DD', 'CC_DD', 'C_CC']] - }, - { - surface: 'wall4', - loops: [['DA', 'A_AA', 'DD_AA', 'D_DD']] - }, - ] - - }; - - const data = ctx.craftEngine.modellingEngine.loadModel(box); + data = ctx.craftEngine.modellingEngine.transform({ + model: data.ptr, + matrix: Matrix3x4.rotateMatrix(45 * DEG_RAD, AXIS.Y, ORIGIN).toFlatArray() + }); const tessellation = ctx.craftEngine.modellingEngine.tessellate({ model: data.ptr,