diff --git a/test/helpers.ts b/test/helpers.ts index 24285e7..3e0fa7d 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -3,18 +3,22 @@ import os from "node:os"; import crypto from "node:crypto"; import path from "node:path"; - -export function getTemporaryFilePath({ prefix, suffix }: { prefix?: string, suffix: string }) { - const name = crypto.randomUUID(); - return path.join(os.tmpdir(), (prefix || "") + name + suffix); -}; +export function getTemporaryFilePath({ + prefix, + suffix, +}: { + prefix?: string; + suffix: string; +}) { + const name = crypto.randomUUID(); + return path.join(os.tmpdir(), (prefix || "") + name + suffix); +} export function removeFile(filename: string) { - if (fs.existsSync(filename)) { - fs.unlinkSync(filename); - } else { - //Show in red - console.log("File " + filename + " not found, so not deleting."); - } + if (fs.existsSync(filename)) { + fs.unlinkSync(filename); + } else { + //Show in red + console.log("File " + filename + " not found, so not deleting."); + } } - diff --git a/test/test_BREP.ts b/test/test_BREP.ts index de53d38..cf2a401 100644 --- a/test/test_BREP.ts +++ b/test/test_BREP.ts @@ -2,138 +2,132 @@ import should from "should"; import { IShape, makeLegoBrick } from ".."; import { ISolid, occ } from ".."; - import { removeFile, getTemporaryFilePath } from "./helpers"; import assert from "assert"; - describe("testing BREP input output ", function () { - - let b1_brep: string; - let b2_brep: string; - let b3_brep: string; - let b1_volume = 0; - let b1_area = 0; - before(() => { - - b1_brep = getTemporaryFilePath({ prefix: "b1_", suffix: ".brep" }); - b2_brep = getTemporaryFilePath({ prefix: "b2_", suffix: ".brep" }); - b3_brep = getTemporaryFilePath({ prefix: "b3_", suffix: ".brep" }); - - create_shapes(); - - }); - after((done) => { - removeFile(b1_brep); - removeFile(b2_brep); - removeFile(b3_brep); - done(); + let b1_brep: string; + let b2_brep: string; + let b3_brep: string; + let b1_volume = 0; + let b1_area = 0; + before(() => { + b1_brep = getTemporaryFilePath({ prefix: "b1_", suffix: ".brep" }); + b2_brep = getTemporaryFilePath({ prefix: "b2_", suffix: ".brep" }); + b3_brep = getTemporaryFilePath({ prefix: "b3_", suffix: ".brep" }); + + create_shapes(); + }); + after((done) => { + removeFile(b1_brep); + removeFile(b2_brep); + removeFile(b3_brep); + done(); + }); + + function create_shapes() { + let box = occ.makeBox([0, 0, 0], [100, 200, 300]); + let b1_result = occ.writeBREP(b1_brep, box); + b1_volume = box.volume; + b1_area = box.area; + + let cyl = occ.makeCylinder([0, 0, 0], [0, 0, 10], 5); + let b2_result = occ.writeBREP(b2_brep, cyl); + + let b3_result = occ.writeBREP(b3_brep, [box, cyl]); + + b1_result.should.eql(true); + b2_result.should.eql(true); + b3_result.should.eql(true); + } + + it("should write a simple shape", function () { + create_shapes(); + }); + + describe(" readBREP ", function () { + it("ZZ1 - should throw an error if used with no argument", function () { + should(function () { + (occ.readBREP as any)(null, (err: Error) => { + err!.message.should.match(/expecting a filename/); + }); + }).throwError(); }); - function create_shapes() { - - let box = occ.makeBox([0, 0, 0], [100, 200, 300]); - let b1_result = occ.writeBREP(b1_brep, box); - b1_volume = box.volume; - b1_area = box.area; - - let cyl = occ.makeCylinder([0, 0, 0], [0, 0, 10], 5); - let b2_result = occ.writeBREP(b2_brep, cyl); - - let b3_result = occ.writeBREP(b3_brep, [box, cyl]); - - b1_result.should.eql(true); - b2_result.should.eql(true); - b3_result.should.eql(true); - - } - - it("should write a simple shape", function () { - create_shapes(); + it("ZZ2 - should call the callback method with an error if used with an invalid arguments", (done) => { + occ.readBREP( + "||this is a invalid filename||", + (err?: Error | null, _shapes?: ISolid[]) => { + err!.message.should.match(/cannot read/); + done(); + } + ); }); - describe(" readBREP ", function () { - - it("ZZ1 - should throw an error if used with no argument", function () { - - should(function () { - (occ.readBREP as any)(null, (err: Error) => { - err!.message.should.match(/expecting a filename/); - }); - }).throwError(); - }); - - it("ZZ2 - should call the callback method with an error if used with an invalid arguments", (done) => { - - occ.readBREP("||this is a invalid filename||", (err?: Error | null, _shapes?: ISolid[]) => { - err!.message.should.match(/cannot read/); - done(); - }); - }); - - it("ZZ3 - should call the callback with an error if the file doesn't exist", (done) => { - occ.readBREP("invalid file name", (err?: Error | null, _shapes?: ISolid[]) => { - console.log(" intercepting error ", err); - assert(err !== undefined); - done(); - }); - }); - - it("ZZ4 - should read the shape back", (done) => { - - occ.readBREP(b1_brep, (err?: Error | null, shapes?: ISolid[]) => { - - should(err).eql(null); - - if (!err && shapes) { - shapes.length.should.equal(1); - shapes[0].numFaces.should.equal(6); + it("ZZ3 - should call the callback with an error if the file doesn't exist", (done) => { + occ.readBREP( + "invalid file name", + (err?: Error | null, _shapes?: ISolid[]) => { + console.log(" intercepting error ", err); + assert(err !== undefined); + done(); + } + ); + }); - shapes[0].volume.should.equal(b1_volume); - shapes[0].area.should.equal(b1_area); - } - done(err); - }); - }); + it("ZZ4 - should read the shape back", (done) => { + occ.readBREP(b1_brep, (err?: Error | null, shapes?: ISolid[]) => { + should(err).eql(null); - it("ZZ5 - should read the shape back", (done) => { + if (!err && shapes) { + shapes.length.should.equal(1); + shapes[0].numFaces.should.equal(6); - occ.readBREP(b2_brep, (err?: Error | null, shapes?: ISolid[]) => { - if (!err && shapes) { - shapes.length.should.equal(1); - shapes[0].numFaces.should.equal(3); - } - done(err); - }); + shapes[0].volume.should.equal(b1_volume); + shapes[0].area.should.equal(b1_area); + } + done(err); + }); + }); - }); - it("ZZ6 - should read the shape back", (done) => { - occ.readBREP(b3_brep, (err?: Error | null, shapes?: ISolid[]) => { - if (!err && shapes) { - shapes.length.should.equal(2); - shapes[0].numFaces.should.equal(6); - shapes[1].numFaces.should.equal(3); - } - done(); - }); - }); + it("ZZ5 - should read the shape back", (done) => { + occ.readBREP(b2_brep, (err?: Error | null, shapes?: ISolid[]) => { + if (!err && shapes) { + shapes.length.should.equal(1); + shapes[0].numFaces.should.equal(3); + } + done(err); + }); + }); + it("ZZ6 - should read the shape back", (done) => { + occ.readBREP(b3_brep, (err?: Error | null, shapes?: ISolid[]) => { + if (!err && shapes) { + shapes.length.should.equal(2); + shapes[0].numFaces.should.equal(6); + shapes[1].numFaces.should.equal(3); + } + done(); + }); }); + }); }); function build_large_part() { - - let lego_filename = getTemporaryFilePath({ prefix: "legoPlate3x2_2x2", suffix: "" }); - - let legoPlate = makeLegoBrick(occ, 3, 2, "thin"); - let solids: ISolid[] = []; - for (let x = 0; x < 100; x += 50) { - for (let y = 0; y < 100; y += 50) { - solids.push(legoPlate.translate([x, y, 0])); - } + let lego_filename = getTemporaryFilePath({ + prefix: "legoPlate3x2_2x2", + suffix: "", + }); + + let legoPlate = makeLegoBrick(occ, 3, 2, "thin"); + let solids: ISolid[] = []; + for (let x = 0; x < 100; x += 50) { + for (let y = 0; y < 100; y += 50) { + solids.push(legoPlate.translate([x, y, 0])); } - occ.writeBREP(lego_filename + ".brep", solids); + } + occ.writeBREP(lego_filename + ".brep", solids); - /* + /* occ.writeSTL(lego_filename + ".stl", solids); let obj = {solids: []}; @@ -148,30 +142,27 @@ function build_large_part() { }); */ - return lego_filename; + return lego_filename; } describe("it should write and read a large brep file", function (this: Mocha.Suite) { + this.timeout(15000); - this.timeout(15000); - - let filename = build_large_part(); - - it("should read a large BREP file quickly", (done) => { - - console.log(" lego file ", filename); + let filename = build_large_part(); - occ.readBREP(filename + ".brep", (err?: Error | null, solids?: ISolid[]) => { - - console.log(" read !!!"); - - if (!err && solids) { - console.log(" num Faces = ", solids[0].numFaces); - } - done(err); - }); - - }); + it("should read a large BREP file quickly", (done) => { + console.log(" lego file ", filename); + occ.readBREP( + filename + ".brep", + (err?: Error | null, solids?: ISolid[]) => { + console.log(" read !!!"); + if (!err && solids) { + console.log(" num Faces = ", solids[0].numFaces); + } + done(err); + } + ); + }); }); diff --git a/test/test_BoundingBox.ts b/test/test_BoundingBox.ts index c20a756..c4c1342 100644 --- a/test/test_BoundingBox.ts +++ b/test/test_BoundingBox.ts @@ -2,69 +2,67 @@ import { IBoundingBox, BoundingBox } from ".."; import "should"; describe("testing bounding box", function () { - describe("an empty BoundingBox", function () { - let bbox: IBoundingBox; - before(function () { - bbox = new BoundingBox(); - }); - it("should be void", function () { - bbox.isVoid.should.equal(true); - }); + describe("an empty BoundingBox", function () { + let bbox: IBoundingBox; + before(function () { + bbox = new BoundingBox(); }); - describe("an BoundingBox built with a single point in constructor", function () { - let bbox: IBoundingBox; - before(function () { - bbox = new BoundingBox([10, 20, 30]); - }); - it("should not be void", function () { - bbox.isVoid.should.equal(false); - }); - it("should have nearPt to be correct", function () { - bbox.nearPt.x.should.equal(10); - bbox.nearPt.y.should.equal(20); - bbox.nearPt.z.should.equal(30); - }); - it("should have farPt to be correct", function () { - bbox.farPt.x.should.equal(10); - bbox.farPt.y.should.equal(20); - bbox.farPt.z.should.equal(30); - }); + it("should be void", function () { + bbox.isVoid.should.equal(true); }); - describe("adding a single point to an empty bounding box", function () { - let bbox: IBoundingBox; - before(function () { - bbox = new BoundingBox(); - bbox.addPoint([10, 20, 30]); - }); - it("should not be void", function () { - bbox.isVoid.should.equal(false); - }); - it("should have nearPt to be correct", function () { - bbox.nearPt.x.should.equal(10); - bbox.nearPt.y.should.equal(20); - bbox.nearPt.z.should.equal(30); - }); - it("should have farPt to be correct", function () { - bbox.farPt.x.should.equal(10); - bbox.farPt.y.should.equal(20); - bbox.farPt.z.should.equal(30); - }); + }); + describe("an BoundingBox built with a single point in constructor", function () { + let bbox: IBoundingBox; + before(function () { + bbox = new BoundingBox([10, 20, 30]); }); - describe("checking calling isOut on a empty box", function () { - it("should return isOut = true for any point ", function () { - let bbox = new BoundingBox(); - bbox.isOut([10, 20, 30]).should.equal(true); - }); + it("should not be void", function () { + bbox.isVoid.should.equal(false); }); - describe("checking calling isOut this box [-10,-10,-10],[5,5,5]", function () { - let bbox = new BoundingBox([-10, -10, -10], [5, 5, 5]); - it("should return isOut = true for [10,20,30] ", function () { - bbox.isOut([10, 20, 30]).should.equal(true); - }); - it("should return isOut = false for [1,2,3] ", function () { - bbox.isOut([1, 2, 3]).should.equal(false); - }); + it("should have nearPt to be correct", function () { + bbox.nearPt.x.should.equal(10); + bbox.nearPt.y.should.equal(20); + bbox.nearPt.z.should.equal(30); }); - - + it("should have farPt to be correct", function () { + bbox.farPt.x.should.equal(10); + bbox.farPt.y.should.equal(20); + bbox.farPt.z.should.equal(30); + }); + }); + describe("adding a single point to an empty bounding box", function () { + let bbox: IBoundingBox; + before(function () { + bbox = new BoundingBox(); + bbox.addPoint([10, 20, 30]); + }); + it("should not be void", function () { + bbox.isVoid.should.equal(false); + }); + it("should have nearPt to be correct", function () { + bbox.nearPt.x.should.equal(10); + bbox.nearPt.y.should.equal(20); + bbox.nearPt.z.should.equal(30); + }); + it("should have farPt to be correct", function () { + bbox.farPt.x.should.equal(10); + bbox.farPt.y.should.equal(20); + bbox.farPt.z.should.equal(30); + }); + }); + describe("checking calling isOut on a empty box", function () { + it("should return isOut = true for any point ", function () { + let bbox = new BoundingBox(); + bbox.isOut([10, 20, 30]).should.equal(true); + }); + }); + describe("checking calling isOut this box [-10,-10,-10],[5,5,5]", function () { + let bbox = new BoundingBox([-10, -10, -10], [5, 5, 5]); + it("should return isOut = true for [10,20,30] ", function () { + bbox.isOut([10, 20, 30]).should.equal(true); + }); + it("should return isOut = false for [1,2,3] ", function () { + bbox.isOut([1, 2, 3]).should.equal(false); + }); + }); }); diff --git a/test/test_ReadWriteSTEP.ts b/test/test_ReadWriteSTEP.ts index a0402cb..dfc4710 100644 --- a/test/test_ReadWriteSTEP.ts +++ b/test/test_ReadWriteSTEP.ts @@ -7,112 +7,101 @@ import { occ } from ".."; import { getTemporaryFilePath, removeFile } from "./helpers"; describe("testing STEP input output ", function () { - - let b1_step: string; - let b2_step: string; - let b3_step: string; - - before(function () { - - b1_step = getTemporaryFilePath({ prefix: "b1_", suffix: ".step" }); - b2_step = getTemporaryFilePath({ prefix: "b2_", suffix: ".step" }); - b3_step = getTemporaryFilePath({ prefix: "b3_", suffix: ".step" }); - - let box = occ.makeBox([0, 0, 0], [100, 200, 300]); - let b1 = occ.writeSTEP(b1_step, box); - - - let cyl = occ.makeCylinder([0, 0, 0], [0, 0, 10], 5); - let b2 = occ.writeSTEP(b2_step, cyl); - let b3 = occ.writeSTEP(b3_step, [box, cyl]); - - b1.should.eql(true); - b2.should.eql(true); - b3.should.eql(true); + let b1_step: string; + let b2_step: string; + let b3_step: string; + + before(function () { + b1_step = getTemporaryFilePath({ prefix: "b1_", suffix: ".step" }); + b2_step = getTemporaryFilePath({ prefix: "b2_", suffix: ".step" }); + b3_step = getTemporaryFilePath({ prefix: "b3_", suffix: ".step" }); + + let box = occ.makeBox([0, 0, 0], [100, 200, 300]); + let b1 = occ.writeSTEP(b1_step, box); + + let cyl = occ.makeCylinder([0, 0, 0], [0, 0, 10], 5); + let b2 = occ.writeSTEP(b2_step, cyl); + let b3 = occ.writeSTEP(b3_step, [box, cyl]); + + b1.should.eql(true); + b2.should.eql(true); + b3.should.eql(true); + }); + after(function () { + removeFile(b1_step); + removeFile(b2_step); + removeFile(b3_step); + }); + + it("AZ0 - should write a simple shape", function (done) { + let box = occ.makeBox([0, 0, 0], [100, 200, 300]); + let b1 = occ.writeSTEP(b1_step, box); + done(); + }); + + it("AZ1 - readSTEP with callback ", function (done) { + occ.readSTEP(b3_step, (err, shapes) => { + console.log(err, shapes); + shapes.length.should.equal(2); + shapes[0].numFaces.should.equal(6); + shapes[1].numFaces.should.equal(3); + done(); }); - after(function () { - removeFile(b1_step); - removeFile(b2_step); - removeFile(b3_step); + }); + + it("AZ2 - should raise an exception with invalid arguments", function () { + (function () { + (occ as any).readSTEP(); + }).should.throwError(); + + (function () { + (occ as any).readSTEP("filename"); + }).should.throwError(); + }); + + it("AZ3 - should call the callback with an error if the file doesn't exist", function (done) { + occ.readSTEP("invalid file name", function (err, shapes) { + if (err) { + err.message.should.match(/invalid file name/); + } else { + return done(new Error("Expecting Error")); + } + done(); }); - - it("AZ0 - should write a simple shape", function (done) { - let box = occ.makeBox([0, 0, 0], [100, 200, 300]); - let b1 = occ.writeSTEP(b1_step, box); - done(); + }); + it("AZ4 - should read file one", function (done) { + occ.readSTEP(b1_step, function (err, shapes) { + if (err) { + console.log(" err = ", err, shapes); + } + assert(!err); + shapes.length.should.equal(1); + shapes[0].numFaces.should.equal(6); + done(); }); - - it("AZ1 - readSTEP with callback ", function (done) { - - occ.readSTEP(b3_step, (err, shapes) => { - console.log(err, shapes); - shapes.length.should.equal(2); - shapes[0].numFaces.should.equal(6); - shapes[1].numFaces.should.equal(3); - done(); - }); + }); + + it("AZ5 - should read file two", function (done) { + occ.readSTEP(b2_step, function (err, shapes) { + if (err) { + console.log(" err = ", err, shapes); + } + assert(!err); + shapes.length.should.equal(1); + shapes[0].numFaces.should.equal(3); + done(); }); - - - it("AZ2 - should raise an exception with invalid arguments", function () { - (function () { - (occ as any).readSTEP(); - }).should.throwError(); - - (function () { - (occ as any).readSTEP("filename"); - }).should.throwError(); - - }); - - it("AZ3 - should call the callback with an error if the file doesn't exist", function (done) { - - occ.readSTEP("invalid file name", function (err, shapes) { - if (err) { - err.message.should.match(/invalid file name/); - } else { - return done(new Error("Expecting Error")); - } - done(); - }); - }); - it("AZ4 - should read file one", function (done) { - - occ.readSTEP(b1_step, function (err, shapes) { - if (err) { - console.log(" err = ", err, shapes); - } - assert(!err); - shapes.length.should.equal(1); - shapes[0].numFaces.should.equal(6); - done(); - }); + }); + it("AZ6 - should read file three", function (done) { + occ.readSTEP(b3_step, function (err, shapes) { + if (err) { + console.log(" err = ", err, shapes); + } + assert(!err); + shapes.length.should.equal(2); + shapes[0].numFaces.should.equal(6); + shapes[1].numFaces.should.equal(3); + done(); }); - - it("AZ5 - should read file two", function (done) { - - occ.readSTEP(b2_step, function (err, shapes) { - if (err) { - console.log(" err = ", err, shapes); - } - assert(!err); - shapes.length.should.equal(1); - shapes[0].numFaces.should.equal(3); - done(); - }); - - }); - it("AZ6 - should read file three", function (done) { - occ.readSTEP(b3_step, function (err, shapes) { - if (err) { - console.log(" err = ", err, shapes); - } - assert(!err); - shapes.length.should.equal(2); - shapes[0].numFaces.should.equal(6); - shapes[1].numFaces.should.equal(3); - done(); - }); - }); - + }); }); diff --git a/test/test_applyTransform.ts b/test/test_applyTransform.ts index 0732d77..abdcf55 100644 --- a/test/test_applyTransform.ts +++ b/test/test_applyTransform.ts @@ -1,38 +1,32 @@ - import { IBoundingBox, ISolid, Transformation, Triplet, occ } from ".."; import "should"; describe("testing various transformation", function () { - - - function getVerticeData(box: IBoundingBox | ISolid): Triplet[] { - let vert = box.getVertices(); - const triplets = vert.map((v) => [v.x, v.y, v.z] as Triplet); - triplets.length.should.eql(8); - return triplets; - } - function add(v1: Triplet, v2: Triplet): Triplet { - return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]]; - } - - it("#applyTransform", function () { - - let box = occ.makeBox([0, 0, 0], [100, 200, 300]); - - let trsf = new Transformation(); - trsf.makeTranslation([10, 20, 30]); - - let vert = getVerticeData(box); - - vert[1].should.eql([0, 0, 0]); - box.applyTransform(trsf); - - let vert_after = getVerticeData(box); - - // translate vertex - vert = vert.map((v) => add(v, [10, 20, 30])); - vert_after.should.eql(vert); - - }); - -}); \ No newline at end of file + function getVerticeData(box: IBoundingBox | ISolid): Triplet[] { + let vert = box.getVertices(); + const triplets = vert.map((v) => [v.x, v.y, v.z] as Triplet); + triplets.length.should.eql(8); + return triplets; + } + function add(v1: Triplet, v2: Triplet): Triplet { + return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]]; + } + + it("#applyTransform", function () { + let box = occ.makeBox([0, 0, 0], [100, 200, 300]); + + let trsf = new Transformation(); + trsf.makeTranslation([10, 20, 30]); + + let vert = getVerticeData(box); + + vert[1].should.eql([0, 0, 0]); + box.applyTransform(trsf); + + let vert_after = getVerticeData(box); + + // translate vertex + vert = vert.map((v) => add(v, [10, 20, 30])); + vert_after.should.eql(vert); + }); +}); diff --git a/test/test_create_proxy_object.ts b/test/test_create_proxy_object.ts index a88220e..8eb2152 100644 --- a/test/test_create_proxy_object.ts +++ b/test/test_create_proxy_object.ts @@ -1,42 +1,40 @@ - import should from "should"; import { calculateOperationHash, createProxyObject } from "../dist/fastbuilder"; describe("testing calculateOperationHash", function () { - - it("should calculate the hash of 10,20,30", function () { - calculateOperationHash("myFunc", [10, 20, 30], false)[1].should.equal("myFunc(10,20,30)"); - }); - it("should calculate the hash of [10,20,30]", function () { - calculateOperationHash("myFunc", [[10, 20, 30]], false)[1].should.equal("myFunc([10,20,30])"); - }); - it("should calculate the hash of [10,20,30], \"Hello\"", function () { - calculateOperationHash("myFunc", [[10, 20, 30], "Hello"], false)[1].should.equal("myFunc([10,20,30],\"Hello\")"); - }); + it("should calculate the hash of 10,20,30", function () { + calculateOperationHash("myFunc", [10, 20, 30], false)[1].should.equal( + "myFunc(10,20,30)" + ); + }); + it("should calculate the hash of [10,20,30]", function () { + calculateOperationHash("myFunc", [[10, 20, 30]], false)[1].should.equal( + "myFunc([10,20,30])" + ); + }); + it('should calculate the hash of [10,20,30], "Hello"', function () { + calculateOperationHash( + "myFunc", + [[10, 20, 30], "Hello"], + false + )[1].should.equal('myFunc([10,20,30],"Hello")'); + }); }); describe("testing createProxyObject", function () { + it("CF1 should ", () => { + const a = { + add(a1: number, a2: number): number { + return a1 + a2; + }, + }; + const b = createProxyObject(a); + const resultFromB = b.add(1, 2); + const resultFromA = a.add(1, 2); - - - it("CF1 should ", () => { - - const a = { - add(a1: number, a2: number): number { - return a1 + a2; - } - } - - const b = createProxyObject(a); - - const resultFromB = b.add(1, 2); - - const resultFromA = a.add(1, 2); - - should(resultFromA).eql(resultFromB); - }) - -}); \ No newline at end of file + should(resultFromA).eql(resultFromB); + }); +}); diff --git a/test/test_edge.ts b/test/test_edge.ts index b016aeb..d58b033 100644 --- a/test/test_edge.ts +++ b/test/test_edge.ts @@ -1,148 +1,154 @@ import should from "should"; -import { occ, Edge, BoundingBox, IEdge, Vertex, IWire, Point } from ".." +import { occ, Edge, BoundingBox, IEdge, Vertex, IWire, Point } from ".."; const { makeLine } = occ; describe("testing Edges ", function () { - - describe("EDGE0 - constructing an empty Edge", function () { - let edge: IEdge; - before(function () { - edge = new Edge() - should.exist(edge); - }); - it("should be valid", function () { - edge.isNull.should.equal(true); - }); - it("should have a zero length", function () { - edge.length.should.equal(0); - }); - it("should have a zero vertices", function () { - edge.numVertices.should.equal(0); - }); - it("should be degenerated", function () { - edge.isDegenerated.should.equal(true); - }); - it("shouldn't be closed", function () { - edge.isClosed.should.equal(false); - }); - it("should provide a bounding box", function () { - edge.getBoundingBox().should.be.instanceOf(BoundingBox); - edge.getBoundingBox().isVoid.should.eql(true); - }); - }); - describe("Wire1 - an Wire constructed as as a linear Segment between (10,20,30) and (-30,20,30) ", function () { - let wire: IWire; - before(function () { - let v1 = new Vertex(10, 20, 30); - let v2 = new Vertex(-30, 20, 30); - wire = makeLine(v1, v2); - wire.should.be.instanceOf(Edge); - - }); - it("should have a length of 40.0 ", function () { - wire.length.should.equal(40.0); - }); - it("should have two vertices ", function () { - wire.numVertices.should.equal(2.0); - }); - it("shouldn't be closed", function () { - wire.isClosed.should.equal(false); - }); - it("shouldn't be degenerated", function () { - wire.isDegenerated.should.equal(false); - }); - it("should provide a bounding box", function () { - wire.getBoundingBox().should.be.instanceOf(BoundingBox); - wire.getBoundingBox().isVoid.should.eql(false); - - wire.getBoundingBox().nearPt.equals(new Point(-30, 20, 30)).should.eql(true); - wire.getBoundingBox().nearPt.equals([-30, 20, 30]).should.eql(true); - wire.getBoundingBox().farPt.equals([10, 20, 30]).should.eql(true); - - let extra = 0.000000000001; - wire.getBoundingBox().farPt.asArray().should.eql([10 + extra, 20 + extra, 30 + extra]); - }); - it("should polygonize a segment with two points", function () { - console.log("polyligonize"); - let polyline = wire.polygonize(); - console.log("polyligonize2"); - polyline.length.should.eql(3 * 2); - [polyline[0], polyline[1], polyline[2]].should.eql([10, 20, 30]); - let l = polyline.length; - - (l % 3).should.eql(0); - [polyline[l - 3], polyline[l - 2], polyline[l - 1]].should.eql([-30, 20, 30]); - }); + describe("EDGE0 - constructing an empty Edge", function () { + let edge: IEdge; + before(function () { + edge = new Edge(); + should.exist(edge); }); - - describe("EDGE2 - an Edge constructed as as a linear Segment between (10,20,30) and (-30,20,30) and translated by [1,2,3]", function () { - let wire: IWire; - before(function () { - let v1 = occ.makeVertex(10, 20, 30); - let v2 = occ.makeVertex(-30, 20, 30); - wire = occ.makeLine(v1, v2); - wire = wire.translate([1, 2, 3]); - }); - it("should have a length of 40.0 ", function () { - wire.length.should.equal(40.0); - }); - it("should provide a bounding box", function () { - wire.getBoundingBox().nearPt.equals([-29, 22, 33]).should.eql(true); - wire.getBoundingBox().farPt.equals([11, 22, 33]).should.eql(true); - }); - it("should polygonize a segment with two points", function () { - let polyline = wire.polygonize(); - polyline.length.should.eql(3 * 2); - [polyline[0], polyline[1], polyline[2]].should.eql([11, 22, 33]); - let l = polyline.length; - (l % 3).should.eql(0); - [polyline[l - 3], polyline[l - 2], polyline[l - 1]].should.eql([-29, 22, 33]); - }); - + it("should be valid", function () { + edge.isNull.should.equal(true); + }); + it("should have a zero length", function () { + edge.length.should.equal(0); + }); + it("should have a zero vertices", function () { + edge.numVertices.should.equal(0); + }); + it("should be degenerated", function () { + edge.isDegenerated.should.equal(true); + }); + it("shouldn't be closed", function () { + edge.isClosed.should.equal(false); + }); + it("should provide a bounding box", function () { + edge.getBoundingBox().should.be.instanceOf(BoundingBox); + edge.getBoundingBox().isVoid.should.eql(true); + }); + }); + describe("Wire1 - an Wire constructed as as a linear Segment between (10,20,30) and (-30,20,30) ", function () { + let wire: IWire; + before(function () { + let v1 = new Vertex(10, 20, 30); + let v2 = new Vertex(-30, 20, 30); + wire = makeLine(v1, v2); + wire.should.be.instanceOf(Edge); + }); + it("should have a length of 40.0 ", function () { + wire.length.should.equal(40.0); + }); + it("should have two vertices ", function () { + wire.numVertices.should.equal(2.0); + }); + it("shouldn't be closed", function () { + wire.isClosed.should.equal(false); + }); + it("shouldn't be degenerated", function () { + wire.isDegenerated.should.equal(false); + }); + it("should provide a bounding box", function () { + wire.getBoundingBox().should.be.instanceOf(BoundingBox); + wire.getBoundingBox().isVoid.should.eql(false); + + wire + .getBoundingBox() + .nearPt.equals(new Point(-30, 20, 30)) + .should.eql(true); + wire.getBoundingBox().nearPt.equals([-30, 20, 30]).should.eql(true); + wire.getBoundingBox().farPt.equals([10, 20, 30]).should.eql(true); + + let extra = 0.000000000001; + wire + .getBoundingBox() + .farPt.asArray() + .should.eql([10 + extra, 20 + extra, 30 + extra]); + }); + it("should polygonize a segment with two points", function () { + console.log("polyligonize"); + let polyline = wire.polygonize(); + console.log("polyligonize2"); + polyline.length.should.eql(3 * 2); + [polyline[0], polyline[1], polyline[2]].should.eql([10, 20, 30]); + let l = polyline.length; + + (l % 3).should.eql(0); + [polyline[l - 3], polyline[l - 2], polyline[l - 1]].should.eql([ + -30, 20, 30, + ]); + }); + }); + + describe("EDGE2 - an Edge constructed as as a linear Segment between (10,20,30) and (-30,20,30) and translated by [1,2,3]", function () { + let wire: IWire; + before(function () { + let v1 = occ.makeVertex(10, 20, 30); + let v2 = occ.makeVertex(-30, 20, 30); + wire = occ.makeLine(v1, v2); + wire = wire.translate([1, 2, 3]); + }); + it("should have a length of 40.0 ", function () { + wire.length.should.equal(40.0); + }); + it("should provide a bounding box", function () { + wire.getBoundingBox().nearPt.equals([-29, 22, 33]).should.eql(true); + wire.getBoundingBox().farPt.equals([11, 22, 33]).should.eql(true); + }); + it("should polygonize a segment with two points", function () { + let polyline = wire.polygonize(); + polyline.length.should.eql(3 * 2); + [polyline[0], polyline[1], polyline[2]].should.eql([11, 22, 33]); + let l = polyline.length; + (l % 3).should.eql(0); + [polyline[l - 3], polyline[l - 2], polyline[l - 1]].should.eql([ + -29, 22, 33, + ]); + }); + }); + describe("Wire - an Wire constructed as a Circle on the Z+ plan with a radius of 20", function () { + let wire: IWire; + before(function () { + wire = occ.makeCircle([10, 10, 10], [0, 0, 1], 20); }); - describe("Wire - an Wire constructed as a Circle on the Z+ plan with a radius of 20", function () { - let wire: IWire; - before(function () { - wire = occ.makeCircle([10, 10, 10], [0, 0, 1], 20); - }); - - it("should have a length of 2*PI*20.0 ", function () { - let epsilon = 1E-2; - let PI = 3.1415; - wire.length.should.be.within(2 * PI * 20.0 - epsilon, 2 * PI * 20.0 + epsilon); - }); - it("should have a unique vertex ", function () { - wire.numVertices.should.equal(1); - }); - it("should be closed", function () { - wire.isClosed.should.equal(true); - }); - it("shouldn't be degenerated", function () { - wire.isDegenerated.should.equal(false); - }); - - it("should provide a bounding box", function () { - wire.getBoundingBox().should.be.instanceOf(BoundingBox); - wire.getBoundingBox().isVoid.should.eql(false); - //xx console.log(JSON.stringify(edge.getBoundingBox()));//.toString()); - wire.getBoundingBox().nearPt.equals([-11.647844, -11.647844, 10]); - wire.getBoundingBox().farPt.equals([31.647844, 31.647844, 10]); - }); - - it("should polygonize a edge", function () { - let a = wire.polygonize(); + it("should have a length of 2*PI*20.0 ", function () { + let epsilon = 1e-2; + let PI = 3.1415; + wire.length.should.be.within( + 2 * PI * 20.0 - epsilon, + 2 * PI * 20.0 + epsilon + ); + }); + it("should have a unique vertex ", function () { + wire.numVertices.should.equal(1); + }); + it("should be closed", function () { + wire.isClosed.should.equal(true); + }); + it("shouldn't be degenerated", function () { + wire.isDegenerated.should.equal(false); + }); - [a[0], a[1], a[2]].should.eql([30, 10, 10]); - let l = a.length; + it("should provide a bounding box", function () { + wire.getBoundingBox().should.be.instanceOf(BoundingBox); + wire.getBoundingBox().isVoid.should.eql(false); + //xx console.log(JSON.stringify(edge.getBoundingBox()));//.toString()); + wire.getBoundingBox().nearPt.equals([-11.647844, -11.647844, 10]); + wire.getBoundingBox().farPt.equals([31.647844, 31.647844, 10]); + }); - (l % 3).should.eql(0); - [a[l - 3], a[l - 2], a[l - 1]].should.eql([30, 10, 10]); - //xx console.log(a); + it("should polygonize a edge", function () { + let a = wire.polygonize(); - }); - }); - describe("EDGE4 - an Edge constructed as as a linear Segment between (10,20,30) and (-30,20,30) ", function () { + [a[0], a[1], a[2]].should.eql([30, 10, 10]); + let l = a.length; + (l % 3).should.eql(0); + [a[l - 3], a[l - 2], a[l - 1]].should.eql([30, 10, 10]); + //xx console.log(a); }); + }); + describe("EDGE4 - an Edge constructed as as a linear Segment between (10,20,30) and (-30,20,30) ", function () {}); }); diff --git a/test/test_extrudeFace.ts b/test/test_extrudeFace.ts index 60ed9e6..50d338a 100644 --- a/test/test_extrudeFace.ts +++ b/test/test_extrudeFace.ts @@ -1,34 +1,30 @@ - import { Triplet, occ } from ".."; import "should"; describe("demonstrate how to extrude a face", function () { - - it("extrudeFace", function () { - - // create a planar wire close line. - - const aPnt1: Triplet = [0, 0.0, 0]; - const aPnt2: Triplet = [10, 1.0, 0]; - const aPnt3: Triplet = [10, 9.0, 0]; - const aPnt4: Triplet = [0, 10.0, 0]; - const aSegment1 = occ.makeLine(aPnt1, aPnt2); - const aSegment2 = occ.makeLine(aPnt2, aPnt3); - const aSegment3 = occ.makeLine(aPnt3, aPnt4); - const aSegment4 = occ.makeLine(aPnt4, aPnt1); - - const aWire = occ.makeWire(aSegment1, aSegment2, aSegment3, aSegment4); - aWire.isClosed.should.equal(true); - aWire.numEdges.should.equal(4); - aWire.numVertices.should.equal(4); - - // the vector to extrude the face along. - const aVector: Triplet = [-2, -2.0, 10]; - const aFace = occ.makeFace(aWire); - - const myBody = occ.makePrism(aFace, aVector); - - occ.writeSTEP("extrudedFace.step", [myBody]); - - }); + it("extrudeFace", function () { + // create a planar wire close line. + + const aPnt1: Triplet = [0, 0.0, 0]; + const aPnt2: Triplet = [10, 1.0, 0]; + const aPnt3: Triplet = [10, 9.0, 0]; + const aPnt4: Triplet = [0, 10.0, 0]; + const aSegment1 = occ.makeLine(aPnt1, aPnt2); + const aSegment2 = occ.makeLine(aPnt2, aPnt3); + const aSegment3 = occ.makeLine(aPnt3, aPnt4); + const aSegment4 = occ.makeLine(aPnt4, aPnt1); + + const aWire = occ.makeWire(aSegment1, aSegment2, aSegment3, aSegment4); + aWire.isClosed.should.equal(true); + aWire.numEdges.should.equal(4); + aWire.numVertices.should.equal(4); + + // the vector to extrude the face along. + const aVector: Triplet = [-2, -2.0, 10]; + const aFace = occ.makeFace(aWire); + + const myBody = occ.makePrism(aFace, aVector); + + occ.writeSTEP("extrudedFace.step", [myBody]); + }); }); diff --git a/test/test_face.ts b/test/test_face.ts index a7d3be9..53e8565 100644 --- a/test/test_face.ts +++ b/test/test_face.ts @@ -4,61 +4,62 @@ import { Vertex, occ } from ".."; // see https://npmjs.org/package/should describe("testing face mesh ", function () { + it("Face#mesh - should not have a mesh unless the parent solid has been meshed", function () { + // given a box solid + let solid = occ.makeBox([0, 0, 0], [10, 10, 10]); - it("Face#mesh - should not have a mesh unless the parent solid has been meshed", function () { + solid.faces.should.have.property("top"); - // given a box solid - let solid = occ.makeBox([0, 0, 0], [10, 10, 10]); + let topFace = solid.faces.top; + should.exist(topFace); - solid.faces.should.have.property("top"); + topFace.area.should.be.within(99.99, 100.0, "face area shall be 100.00"); - let topFace = solid.faces.top; - should.exist(topFace); + topFace.hasMesh.should.equal(false); - topFace.area.should.be.within(99.99, 100.0, "face area shall be 100.00"); + // now mesh the solid + let m = solid.mesh; + m.vertices.length.should.eql( + 8 * 3, + "expecting 8 vertices (made of 3 floats) in solid mesh" + ); + m.edgeIndices.length.should.eql(24); + m.triangles.length.should.eql(36); - topFace.hasMesh.should.equal(false); + //xx console.log(m.toJSON()); - // now mesh the solid - let m = solid.mesh; - m.vertices.length.should.eql(8 * 3, "expecting 8 vertices (made of 3 floats) in solid mesh"); - m.edgeIndices.length.should.eql(24); - m.triangles.length.should.eql(36); + // meshing the solid should cause each of its faces to be meshed + topFace.hasMesh.should.equal( + true, + "Face must have a mesh when parent solid is meshed" + ); - //xx console.log(m.toJSON()); + //xx m.normals.length.should.eql(72); + //xx console.log(topFace.mesh.toJSON()); + let faceMesh = topFace.mesh; - // meshing the solid should cause each of its faces to be meshed - topFace.hasMesh.should.equal(true, "Face must have a mesh when parent solid is meshed"); - - //xx m.normals.length.should.eql(72); - //xx console.log(topFace.mesh.toJSON()); - let faceMesh = topFace.mesh; - - faceMesh.vertices.length.should.eql(3 * 4, "we expect 4 points "); - //xx faceMesh.normals.length.should.eql(3 * 4); - faceMesh.edgeIndices.length.should.eql(2 * 4); - faceMesh.triangles.length.should.eql(2 * 3); - - }); + faceMesh.vertices.length.should.eql(3 * 4, "we expect 4 points "); + //xx faceMesh.normals.length.should.eql(3 * 4); + faceMesh.edgeIndices.length.should.eql(2 * 4); + faceMesh.triangles.length.should.eql(2 * 3); + }); }); describe("testing face#getWire ", function () { - - - it("Face#getWire", function () { - - let solid = occ.makeBox([0, 0, 0], [10, 10, 10]); - solid.faces.should.have.property("top"); - let topFace = solid.faces.top; - should.exist(topFace); - - let wires = topFace.getWires(); - wires.length.should.eql(1); - - wires[0].getEdges().length.should.eql(4); - - wires[0].getEdges()[0].getVertices()[0].should.eql(new Vertex({ x: 0, y: 10, z: 10 })); - - - }); + it("Face#getWire", function () { + let solid = occ.makeBox([0, 0, 0], [10, 10, 10]); + solid.faces.should.have.property("top"); + let topFace = solid.faces.top; + should.exist(topFace); + + let wires = topFace.getWires(); + wires.length.should.eql(1); + + wires[0].getEdges().length.should.eql(4); + + wires[0] + .getEdges()[0] + .getVertices()[0] + .should.eql(new Vertex({ x: 0, y: 10, z: 10 })); + }); }); diff --git a/test/test_fastbuilder.ts b/test/test_fastbuilder.ts index 72389dc..ce7cde6 100644 --- a/test/test_fastbuilder.ts +++ b/test/test_fastbuilder.ts @@ -4,199 +4,185 @@ import "should"; const fast_occ = fastBuilder; function makeShape() { - let e = 20; - let s1 = fast_occ.makeBox([10, e, 30], [110, 120, 130]); - let s2 = fast_occ.makeBox(100, 200, 300); - let s3 = fast_occ.fuse(s1, s2); - s3 = s3.translate([0, 20, 30]); - s3 = s3.translate([0, 20, 30]); - return s3; + let e = 20; + let s1 = fast_occ.makeBox([10, e, 30], [110, 120, 130]); + let s2 = fast_occ.makeBox(100, 200, 300); + let s3 = fast_occ.fuse(s1, s2); + s3 = s3.translate([0, 20, 30]); + s3 = s3.translate([0, 20, 30]); + return s3; } function startChronometer() { - return process.hrtime(); + return process.hrtime(); } function stopChronometer(time1: [number, number]) { - let diff0 = process.hrtime(time1); - let diff1 = (diff0[0] * 1E9 + diff0[1]); // in nanoseconds - diff1 /= 1000.0; // in microseconds - diff1 /= 1000.0; // in miliseconds - diff1 /= 1000.0; // in seconds - return diff1; + let diff0 = process.hrtime(time1); + let diff1 = diff0[0] * 1e9 + diff0[1]; // in nanoseconds + diff1 /= 1000.0; // in microseconds + diff1 /= 1000.0; // in miliseconds + diff1 /= 1000.0; // in seconds + return diff1; } - - describe("testing geometry builder", function () { - - before(function () { - fastBuilder.$.resetCache(); - }); - it("should create a bottle faster the second time ", function () { - - fastBuilder.$.mapQueryCount.should.equal(0); - fastBuilder.$.mapHit.should.equal(0); - - let c1 = startChronometer(); - makeShape(); - let diff1 = stopChronometer(c1); - - fastBuilder.$.mapQueryCount.should.equal(5); - fastBuilder.$.mapHit.should.equal(0); - - let c2 = startChronometer(); - makeShape(); - let diff2 = stopChronometer(c2); - - - fastBuilder.$.mapQueryCount.should.equal(10); - fastBuilder.$.mapHit.should.equal(5); - - console.log(" time to compute first box = ", diff1, " seconds"); - console.log(" time to compute second box = ", diff2, " seconds"); - console.log(" speed up = ", Math.round((diff1 - diff2) / diff2 * 100), "%"); - - diff1.should.be.greaterThan(diff2); - - }); + before(function () { + fastBuilder.$.resetCache(); + }); + it("should create a bottle faster the second time ", function () { + fastBuilder.$.mapQueryCount.should.equal(0); + fastBuilder.$.mapHit.should.equal(0); + + let c1 = startChronometer(); + makeShape(); + let diff1 = stopChronometer(c1); + + fastBuilder.$.mapQueryCount.should.equal(5); + fastBuilder.$.mapHit.should.equal(0); + + let c2 = startChronometer(); + makeShape(); + let diff2 = stopChronometer(c2); + + fastBuilder.$.mapQueryCount.should.equal(10); + fastBuilder.$.mapHit.should.equal(5); + + console.log(" time to compute first box = ", diff1, " seconds"); + console.log(" time to compute second box = ", diff2, " seconds"); + console.log( + " speed up = ", + Math.round(((diff1 - diff2) / diff2) * 100), + "%" + ); + + diff1.should.be.greaterThan(diff2); + }); }); - - - describe("testing fast builder with array of shape", function () { - - before(() => { - fastBuilder.$.resetCache(); - }); - it("should create a bottle faster the second time ", function () { - - fastBuilder.$.mapQueryCount.should.equal(0); - fastBuilder.$.mapHit.should.equal(0); - let a: ISolid[] = []; - a.push(makeShape()); - a.push(makeShape().translate(10, 20, 30)); - a.push(makeShape().translate(30, 20, 30)); - - let compound = fast_occ.compound(a); - - }); + before(() => { + fastBuilder.$.resetCache(); + }); + it("should create a bottle faster the second time ", function () { + fastBuilder.$.mapQueryCount.should.equal(0); + fastBuilder.$.mapHit.should.equal(0); + let a: ISolid[] = []; + a.push(makeShape()); + a.push(makeShape().translate(10, 20, 30)); + a.push(makeShape().translate(30, 20, 30)); + + let compound = fast_occ.compound(a); + }); }); describe("testing fast builder with makeThickSolid", function () { - let s1: ISolid; - let s2: ISolid; - before(function () { - s1 = fast_occ.makeBox([10, 20, 30], [110, 120, 130]); - s1 = fast_occ.makeThickSolid(s1, s1.faces.top, 6); - s2 = occ.makeBox([10, 20, 30], [110, 120, 130]); - s2 = occ.makeThickSolid(s2, s2.faces.top, 6); - - }); - it(" should construct the same object as if using 'occ' ", function () { - s1.numFaces.should.equal(s2.numFaces); - }); + let s1: ISolid; + let s2: ISolid; + before(function () { + s1 = fast_occ.makeBox([10, 20, 30], [110, 120, 130]); + s1 = fast_occ.makeThickSolid(s1, s1.faces.top, 6); + s2 = occ.makeBox([10, 20, 30], [110, 120, 130]); + s2 = occ.makeThickSolid(s2, s2.faces.top, 6); + }); + it(" should construct the same object as if using 'occ' ", function () { + s1.numFaces.should.equal(s2.numFaces); + }); }); - - describe("testing fast builder with some built-in shapes", function () { - - it("should create the bottle..", function () { - let s1 = shapeFactory.makeBottle(fastBuilder, { filletRadius: 1, height: 100 }); - s1.numFaces.should.be.greaterThan(16); + it("should create the bottle..", function () { + let s1 = shapeFactory.makeBottle(fastBuilder, { + filletRadius: 1, + height: 100, }); + s1.numFaces.should.be.greaterThan(16); + }); }); describe("testing fast builder with some shapes", function () { - - it("should create the piston..", function () { - - let s1 = shapeFactory.makePiston(fastBuilder); - s1.numFaces.should.be.greaterThan(7); - }); + it("should create the piston..", function () { + let s1 = shapeFactory.makePiston(fastBuilder); + s1.numFaces.should.be.greaterThan(7); + }); }); - - describe("testing fast builder get Common Edge", function () { - let solid1: ISolid; - let solid2: ISolid; - - function buildFilletOnTopLeftEdge() { - let s1 = fast_occ.makeBox([10, 20, 30], [110, 120, 130]); - let edges = s1.getCommonEdges(s1.faces.front, s1.faces.left); - s1 = fast_occ.makeFillet(s1, edges, 10); - s1 = fast_occ.makeDraftAngle(s1, s1.faces["mleft:0"], 0.1, s1.faces["mbottom:0"]); - return s1; - } - - before(function () { - solid1 = buildFilletOnTopLeftEdge(); - solid2 = buildFilletOnTopLeftEdge(); - }); - it("should have 7 faces", function () { - solid1.numFaces.should.be.equal(7); - solid2.numFaces.should.be.equal(7); - }); + let solid1: ISolid; + let solid2: ISolid; + + function buildFilletOnTopLeftEdge() { + let s1 = fast_occ.makeBox([10, 20, 30], [110, 120, 130]); + let edges = s1.getCommonEdges(s1.faces.front, s1.faces.left); + s1 = fast_occ.makeFillet(s1, edges, 10); + s1 = fast_occ.makeDraftAngle( + s1, + s1.faces["mleft:0"], + 0.1, + s1.faces["mbottom:0"] + ); + return s1; + } + + before(function () { + solid1 = buildFilletOnTopLeftEdge(); + solid2 = buildFilletOnTopLeftEdge(); + }); + it("should have 7 faces", function () { + solid1.numFaces.should.be.equal(7); + solid2.numFaces.should.be.equal(7); + }); }); - describe("testing fast-builder with impossible cone", function () { - let solid1: ISolid; - before(function () { - // this cone cannot be built : it has PI/2 for half-angle ! - }); - it("should have no solid", function () { - (function () { - solid1 = fast_occ.makeCone([0, 0, 0], [0, 0, 1], 1.5707963267948966, 10); - }).should.throwError(); - }); + let solid1: ISolid; + before(function () { + // this cone cannot be built : it has PI/2 for half-angle ! + }); + it("should have no solid", function () { + (function () { + solid1 = fast_occ.makeCone([0, 0, 0], [0, 0, 1], 1.5707963267948966, 10); + }).should.throwError(); + }); }); describe("testing fast-builder with LEGO brick", function () { + this.timeout(10000); - this.timeout(10000); - - it("should produce a LEGO brick", function () { + it("should produce a LEGO brick", function () { + function buildBrick() { + let nx = 3; + let ny = 6; + let brick24 = shapeFactory.makeLegoBrick(fast_occ, nx, ny, "thick"); + brick24.numFaces.should.be.greaterThan(40); - function buildBrick() { + // now check with bounding box + let bbox = brick24.getBoundingBox(); - let nx = 3; - let ny = 6; - let brick24 = shapeFactory.makeLegoBrick(fast_occ, nx, ny, "thick"); + let eps = 0.01; + bbox.nearPt.x.should.be.within(0 - eps, 0 + eps); + bbox.nearPt.y.should.be.within(0 - eps, 0 + eps); + bbox.nearPt.z.should.be.within(0 - eps, 0 + eps); - brick24.numFaces.should.be.greaterThan(40); - - // now check with bounding box - let bbox = brick24.getBoundingBox(); - - let eps = 0.01; - bbox.nearPt.x.should.be.within(0 - eps, 0 + eps); - bbox.nearPt.y.should.be.within(0 - eps, 0 + eps); - bbox.nearPt.z.should.be.within(0 - eps, 0 + eps); - - bbox.farPt.x.should.be.within(nx * 8 - eps, nx * 8 + eps); - bbox.farPt.y.should.be.within(ny * 8 - eps, ny * 8 + eps); - bbox.farPt.z.should.be.within(11.2 - eps, 11.2 + eps); - } - - let c1 = startChronometer(); - buildBrick(); - let diff1 = stopChronometer(c1); + bbox.farPt.x.should.be.within(nx * 8 - eps, nx * 8 + eps); + bbox.farPt.y.should.be.within(ny * 8 - eps, ny * 8 + eps); + bbox.farPt.z.should.be.within(11.2 - eps, 11.2 + eps); + } - let c2 = startChronometer(); - buildBrick(); - let diff2 = stopChronometer(c2); + let c1 = startChronometer(); + buildBrick(); + let diff1 = stopChronometer(c1); - console.log(" time to compute first box = ", diff1, " seconds"); - console.log(" time to compute second box = ", diff2, " seconds"); - let speedup = Math.round((diff1 - diff2) / diff2 * 100); - console.log(" speed up = ", speedup, "%"); + let c2 = startChronometer(); + buildBrick(); + let diff2 = stopChronometer(c2); - diff1.should.be.greaterThan(diff2); - speedup.should.be.greaterThan(100); //"%" + console.log(" time to compute first box = ", diff1, " seconds"); + console.log(" time to compute second box = ", diff2, " seconds"); + let speedup = Math.round(((diff1 - diff2) / diff2) * 100); + console.log(" speed up = ", speedup, "%"); - }); + diff1.should.be.greaterThan(diff2); + speedup.should.be.greaterThan(100); //"%" + }); }); diff --git a/test/test_geometry.ts b/test/test_geometry.ts index 017fe42..5de5f5a 100644 --- a/test/test_geometry.ts +++ b/test/test_geometry.ts @@ -1,14 +1,14 @@ - import { occ } from ".."; import { getTemporaryFilePath } from "./helpers"; import { makeBottle } from ".."; describe("testing geometry builder", function () { - it("should create a bottle", function () { - - let bottle_brep = getTemporaryFilePath({ prefix: "bottle", suffix: ".brep" }); - let bottle = makeBottle(occ, { height: 100, filletRadius: 2 }); - occ.writeBREP(bottle_brep, bottle); + it("should create a bottle", function () { + let bottle_brep = getTemporaryFilePath({ + prefix: "bottle", + suffix: ".brep", }); + let bottle = makeBottle(occ, { height: 100, filletRadius: 2 }); + occ.writeBREP(bottle_brep, bottle); + }); }); - diff --git a/test/test_issue17_mesh_not_invalidated_when_face_moved.ts b/test/test_issue17_mesh_not_invalidated_when_face_moved.ts index d6009ab..096695d 100644 --- a/test/test_issue17_mesh_not_invalidated_when_face_moved.ts +++ b/test/test_issue17_mesh_not_invalidated_when_face_moved.ts @@ -4,87 +4,81 @@ import "should"; const doDebug = false; function debugLog(...args: any[]) { - if (doDebug) { - console.log.apply(console, args); - } + if (doDebug) { + console.log.apply(console, args); + } } describe("issue#17 testing that mesh get invalidated ", function () { + function constructFaceWithWire() { + const aPnt1: Triplet = [0, 0.0, 0]; + const aPnt2: Triplet = [10, 1.0, 0]; + const aPnt3: Triplet = [10, 9.0, 0]; + const aPnt4: Triplet = [0, 10.0, 0]; + const segment1 = occ.makeLine(aPnt1, aPnt2); + const segment2 = occ.makeLine(aPnt2, aPnt3); + const segment3 = occ.makeLine(aPnt3, aPnt4); + const segment4 = occ.makeLine(aPnt4, aPnt1); + const wire = occ.makeWire(segment1, segment2, segment3, segment4); + wire.isClosed.should.equal(true); + wire.numEdges.should.equal(4); + wire.numVertices.should.equal(4); - function constructFaceWithWire() { - const aPnt1: Triplet = [0, 0.0, 0]; - const aPnt2: Triplet = [10, 1.0, 0]; - const aPnt3: Triplet = [10, 9.0, 0]; - const aPnt4: Triplet = [0, 10.0, 0]; - const segment1 = occ.makeLine(aPnt1, aPnt2); - const segment2 = occ.makeLine(aPnt2, aPnt3); - const segment3 = occ.makeLine(aPnt3, aPnt4); - const segment4 = occ.makeLine(aPnt4, aPnt1); + // the vector to extrude the face along. + const face = occ.makeFace(wire); - const wire = occ.makeWire(segment1, segment2, segment3, segment4); - wire.isClosed.should.equal(true); - wire.numEdges.should.equal(4); - wire.numVertices.should.equal(4); + face.getWires().length.should.eql(1); - // the vector to extrude the face along. - const face = occ.makeFace(wire); + face.getWires()[0].getVertices().length.should.eql(4); + return face; + } - face.getWires().length.should.eql(1); + it("should translate a face", function (done) { + let face = constructFaceWithWire(); - face.getWires()[0].getVertices().length.should.eql(4); - return face; - } + const vertices_before = face.getWires()[0].getVertices(); - it("should translate a face", function (done) { - let face = constructFaceWithWire(); + face = face.translate([20, 30, 40]); + const vertices_after = face.getWires()[0].getVertices(); - const vertices_before = face.getWires()[0].getVertices(); + vertices_after[0].x.should.eql(vertices_before[0].x + 20); + vertices_after[0].y.should.eql(vertices_before[0].y + 30); + vertices_after[0].z.should.eql(vertices_before[0].z + 40); - face = face.translate([20, 30, 40]); - const vertices_after = face.getWires()[0].getVertices(); + vertices_after[1].x.should.eql(vertices_before[1].x + 20); + vertices_after[1].y.should.eql(vertices_before[1].y + 30); + vertices_after[1].z.should.eql(vertices_before[1].z + 40); - vertices_after[0].x.should.eql(vertices_before[0].x + 20); - vertices_after[0].y.should.eql(vertices_before[0].y + 30); - vertices_after[0].z.should.eql(vertices_before[0].z + 40); + vertices_after[2].x.should.eql(vertices_before[2].x + 20); + vertices_after[2].y.should.eql(vertices_before[2].y + 30); + vertices_after[2].z.should.eql(vertices_before[2].z + 40); - vertices_after[1].x.should.eql(vertices_before[1].x + 20); - vertices_after[1].y.should.eql(vertices_before[1].y + 30); - vertices_after[1].z.should.eql(vertices_before[1].z + 40); + vertices_after[3].x.should.eql(vertices_before[3].x + 20); + vertices_after[3].y.should.eql(vertices_before[3].y + 30); + vertices_after[3].z.should.eql(vertices_before[3].z + 40); - vertices_after[2].x.should.eql(vertices_before[2].x + 20); - vertices_after[2].y.should.eql(vertices_before[2].y + 30); - vertices_after[2].z.should.eql(vertices_before[2].z + 40); + done(); + }); - vertices_after[3].x.should.eql(vertices_before[3].x + 20); - vertices_after[3].y.should.eql(vertices_before[3].y + 30); - vertices_after[3].z.should.eql(vertices_before[3].z + 40); + it("#17-B should provide a translated mesh when face is translated", function (done) { + let face = constructFaceWithWire(); + face.hasMesh.should.equal(false); - done(); - }); + // now mesh the faces + face.createMesh(0.1); + face.hasMesh.should.equal(true); - it("#17-B should provide a translated mesh when face is translated", function (done) { + debugLog("face mesh vertices =", face.mesh.vertices.toString()); - let face = constructFaceWithWire(); - face.hasMesh.should.equal(false); + const vertices_before = face.mesh.vertices; + face = face.translate([20, 30, 40]); - // now mesh the faces - face.createMesh(0.1); - face.hasMesh.should.equal(true); + debugLog("face mesh vertices =", face.mesh.vertices.toString()); - debugLog("face mesh vertices =", face.mesh.vertices.toString()); + const vertices_after = face.mesh.vertices; - - const vertices_before = face.mesh.vertices; - face = face.translate([20, 30, 40]); - - debugLog("face mesh vertices =", face.mesh.vertices.toString()); - - const vertices_after = face.mesh.vertices; - - vertices_before.toString().should.eql("0,0,0,10,1,0,10,9,0,0,10,0"); - vertices_after.toString().should.eql("20,30,40,30,31,40,30,39,40,20,40,40"); - done(); - }); + vertices_before.toString().should.eql("0,0,0,10,1,0,10,9,0,0,10,0"); + vertices_after.toString().should.eql("20,30,40,30,31,40,30,39,40,20,40,40"); + done(); + }); }); - - diff --git a/test/test_meshSolid.ts b/test/test_meshSolid.ts index 793c4c8..ff6842b 100644 --- a/test/test_meshSolid.ts +++ b/test/test_meshSolid.ts @@ -4,325 +4,321 @@ import { IMesh, ISolid, Real, occ, shapeFactory } from ".."; const doDebug = false; function debugLog(...args: any[]) { - arguments; - /* implement me*/ + arguments; + /* implement me*/ } describe("TBugLinux- testing mesh on a simple cone shape with radius2 = 0 returns 2 Faces (latteral+bottom)", function () { - // cf. https://github.com/antonymarion/node-occ-csg-editor-display/runs/1517044830?check_suite_focus=true - let shape: ISolid; - let mesh: IMesh; - before(function () { - shape = occ.makeCone([0, 0, 0], 2, [0, 0, 2], 0); - mesh = shape.createMesh(0.1); - }); - it("solid should have 2 faces", function () { - const myFaces = shape.getFaces(); - console.log("shape", shape); - console.log("cone Faces", myFaces); - myFaces.length.should.eql(2); - - // meshing should work on Linux ! (was not working w/ Linux pre compiled occ.node) - shape - .hasMesh.should.be.eql(true); - shape.faces["lateral"] - .hasMesh - .should.be.eql(true); - shape.faces["bottom"] - .hasMesh - .should.be.eql(true); - - }); + // cf. https://github.com/antonymarion/node-occ-csg-editor-display/runs/1517044830?check_suite_focus=true + let shape: ISolid; + let mesh: IMesh; + before(function () { + shape = occ.makeCone([0, 0, 0], 2, [0, 0, 2], 0); + mesh = shape.createMesh(0.1); + }); + it("solid should have 2 faces", function () { + const myFaces = shape.getFaces(); + console.log("shape", shape); + console.log("cone Faces", myFaces); + myFaces.length.should.eql(2); + + // meshing should work on Linux ! (was not working w/ Linux pre compiled occ.node) + shape.hasMesh.should.be.eql(true); + shape.faces["lateral"].hasMesh.should.be.eql(true); + shape.faces["bottom"].hasMesh.should.be.eql(true); + }); }); describe("T1- testing mesh on a simple box shape", function () { - - let shape: ISolid; - let mesh: IMesh; - before(function () { - shape = occ.makeBox(10, 40, 100); - mesh = shape.createMesh(0.1); - }); - it("solid should have 6 faces", function () { - shape.getFaces().length.should.eql(6); - }); - it("Mesh#vertices - mesh should provide a vertex array", function () { - // ----------------------------------- testing vertices - mesh.vertices.length.should.eql(3 * 8); - - mesh.vertices[0].should.eql(0.0); - mesh.vertices[1].should.eql(0.0); - mesh.vertices[2].should.eql(0.0); - - mesh.vertices[3].should.eql(0.0); - mesh.vertices[4].should.eql(0.0); - mesh.vertices[5].should.eql(100.0); - - mesh.vertices[6].should.eql(0.0); - mesh.vertices[7].should.eql(40.0); - mesh.vertices[8].should.eql(0.0); - - mesh.vertices[9].should.eql(0.0); - mesh.vertices[10].should.eql(40.0); - mesh.vertices[11].should.eql(100.0); - - mesh.vertices[12].should.eql(10.0); - mesh.vertices[13].should.eql(0.0); - mesh.vertices[14].should.eql(0.0); - - }); - it("Mesh#triangle - mesh should provide triangles indexes", function () { - // --------------------------------------- triangles - mesh.triangles.length.should.eql(6 * 2 * 3, "it should provide 2 triangles per face = 2 *4 *3"); - }); - it("Mesh#faceRanges - mesh should provide a mechanism to easily identify triangle faces", function () { - mesh.faceRanges.length.should.eql(6 * 2, "it should 6 pairs of index ( 6 faces, 2 values per face)"); - mesh.edgeRanges.length.should.eql(12 * 2, "it should 12 pairs of index ( 12 edges, 2 values per face)"); - - // --------------------------------------- faces - // testing face 1 - mesh.faceRanges[0].should.eql(0); // start in triangle index - mesh.faceRanges[1].should.eql(2); // nb of triangles - - // testing face 2 - mesh.faceRanges[2].should.eql(2); // start in triangle index - mesh.faceRanges[3].should.eql(2); // nb of triangles - - // testing face 3 - mesh.faceRanges[4].should.eql(4); // start in triangle index - mesh.faceRanges[5].should.eql(2); // nb of triangles - - // testing face 4 - mesh.faceRanges[6].should.eql(6); // start in triangle index - mesh.faceRanges[7].should.eql(2); // nb of triangles - - // testing face 5 - mesh.faceRanges[8].should.eql(8); // start in triangle index - mesh.faceRanges[9].should.eql(2); // nb of triangles - - // testing face 6 - mesh.faceRanges[10].should.eql(10); // start in triangle index - mesh.faceRanges[11].should.eql(2); // nb of triangles - }); - it("Mesh#getFaceTriangles - mesh should provide a mechanism to extract triangles indexes of a given shape face", function () { - // --------------------------------------- face accessor - let arr = mesh.getFaceTriangles(shape.getFaces()[0]); - arr.should.eql(new Uint8Array([0, 1, 2, 2, 1, 3])); - - arr = mesh.getFaceTriangles(shape.getFaces()[1]); - arr.should.eql(new Uint8Array([5, 4, 6, 5, 6, 7])); - // etc... - }); - it("Mesh#edgeRanges - mesh should provide a mechanism to easily identify edges", function () { - // edge index - // --------------------------------------- edges - // testing edge 1 - mesh.edgeRanges[0].should.eql(0); // start in triangle index - mesh.edgeRanges[1].should.eql(2); // nb of triangles - - // testing edge 2 - mesh.edgeRanges[2].should.eql(2); // start in triangle index - mesh.edgeRanges[3].should.eql(2); // nb of triangles - - // testing edge 3 - mesh.edgeRanges[4].should.eql(4); // start in triangle index - mesh.edgeRanges[5].should.eql(2); // nb of triangles - - // testing edge 4 - mesh.edgeRanges[6].should.eql(6); // start in triangle index - mesh.edgeRanges[7].should.eql(2); // nb of triangles - - // testing edge 5 - mesh.edgeRanges[8].should.eql(8); // start in triangle index - mesh.edgeRanges[9].should.eql(2); // nb of triangles - - // testing edge 6 - mesh.edgeRanges[10].should.eql(10); // start in triangle index - mesh.edgeRanges[11].should.eql(2); // nb of triangles - - // testing edge 7 - mesh.edgeRanges[12].should.eql(12); // start in triangle index - mesh.edgeRanges[13].should.eql(2); // nb of triangles - - // testing edge 8 - mesh.edgeRanges[14].should.eql(14); // start in triangle index - mesh.edgeRanges[15].should.eql(2); // nb of triangles - - // testing edge 9 - mesh.edgeRanges[16].should.eql(16); // start in triangle index - mesh.edgeRanges[17].should.eql(2); // nb of triangles - - // testing edge 10 - mesh.edgeRanges[18].should.eql(18); // start in triangle index - mesh.edgeRanges[19].should.eql(2); // nb of triangles - - // testing edge 11 - mesh.edgeRanges[20].should.eql(20); // start in triangle index - mesh.edgeRanges[21].should.eql(2); // nb of triangles - - // testing edge 12 - mesh.edgeRanges[22].should.eql(22); // start in triangle index - mesh.edgeRanges[23].should.eql(2); // nb of triangles - - }); - - it("Mesh#getEdgeIndices - mesh should provide a mechanism to extract the Polygon of a given edge", function () { - //xx console.log(mesh); - const arr = mesh.getEdgeIndices(shape.getEdges()[0]); - arr.should.eql(new Uint8Array([0, 1])); - - }); - it("Mesh#triangleNormals - mesh should provide ", function () { - - // face 0 - triangle 0 - mesh.triangleNormals[0].should.eql(0); - mesh.triangleNormals[1].should.eql(0); - mesh.triangleNormals[2].should.eql(0); - // face 0 - triangle 1 - mesh.triangleNormals[3].should.eql(0); - mesh.triangleNormals[4].should.eql(0); - mesh.triangleNormals[5].should.eql(0); - - // face 1 - triangle 0 - mesh.triangleNormals[6].should.eql(1); - mesh.triangleNormals[7].should.eql(1); - mesh.triangleNormals[8].should.eql(1); - // face 1 - triangle 1 - mesh.triangleNormals[9].should.eql(1); - mesh.triangleNormals[10].should.eql(1); - mesh.triangleNormals[11].should.eql(1); - - // face 2 - triangle 0= - mesh.triangleNormals[12].should.eql(2); - mesh.triangleNormals[13].should.eql(2); - mesh.triangleNormals[14].should.eql(2); - // face 2 - triangle 1 - mesh.triangleNormals[15].should.eql(2); - mesh.triangleNormals[16].should.eql(2); - mesh.triangleNormals[17].should.eql(2); - - // face 3 - triangle 0= - mesh.triangleNormals[18].should.eql(3); - mesh.triangleNormals[19].should.eql(3); - mesh.triangleNormals[20].should.eql(3); - // face 3 - triangle 1 - mesh.triangleNormals[21].should.eql(3); - mesh.triangleNormals[22].should.eql(3); - mesh.triangleNormals[23].should.eql(3); - - // etc... - }); - it("Mesh#normals - mesh should provide a normal array", function () { - // in the case of a simple box we expect to have 6 different normals - mesh.normals.length.should.eql(3 * 6); - - // first normal - mesh.normals[0].should.eql(-1); - mesh.normals[1].should.eql(0); - mesh.normals[2].should.eql(0); - - mesh.normals[3].should.eql(1); - mesh.normals[4].should.eql(0); - mesh.normals[5].should.eql(0); - - mesh.normals[6].should.eql(0); - mesh.normals[7].should.eql(-1); - mesh.normals[8].should.eql(0); - - mesh.normals[9].should.eql(0); - mesh.normals[10].should.eql(1); - mesh.normals[11].should.eql(0); - - mesh.normals[12].should.eql(0); - mesh.normals[13].should.eql(0); - mesh.normals[14].should.eql(-1); - - mesh.normals[15].should.eql(0); - mesh.normals[16].should.eql(0); - mesh.normals[17].should.eql(1); - }); - + let shape: ISolid; + let mesh: IMesh; + before(function () { + shape = occ.makeBox(10, 40, 100); + mesh = shape.createMesh(0.1); + }); + it("solid should have 6 faces", function () { + shape.getFaces().length.should.eql(6); + }); + it("Mesh#vertices - mesh should provide a vertex array", function () { + // ----------------------------------- testing vertices + mesh.vertices.length.should.eql(3 * 8); + + mesh.vertices[0].should.eql(0.0); + mesh.vertices[1].should.eql(0.0); + mesh.vertices[2].should.eql(0.0); + + mesh.vertices[3].should.eql(0.0); + mesh.vertices[4].should.eql(0.0); + mesh.vertices[5].should.eql(100.0); + + mesh.vertices[6].should.eql(0.0); + mesh.vertices[7].should.eql(40.0); + mesh.vertices[8].should.eql(0.0); + + mesh.vertices[9].should.eql(0.0); + mesh.vertices[10].should.eql(40.0); + mesh.vertices[11].should.eql(100.0); + + mesh.vertices[12].should.eql(10.0); + mesh.vertices[13].should.eql(0.0); + mesh.vertices[14].should.eql(0.0); + }); + it("Mesh#triangle - mesh should provide triangles indexes", function () { + // --------------------------------------- triangles + mesh.triangles.length.should.eql( + 6 * 2 * 3, + "it should provide 2 triangles per face = 2 *4 *3" + ); + }); + it("Mesh#faceRanges - mesh should provide a mechanism to easily identify triangle faces", function () { + mesh.faceRanges.length.should.eql( + 6 * 2, + "it should 6 pairs of index ( 6 faces, 2 values per face)" + ); + mesh.edgeRanges.length.should.eql( + 12 * 2, + "it should 12 pairs of index ( 12 edges, 2 values per face)" + ); + + // --------------------------------------- faces + // testing face 1 + mesh.faceRanges[0].should.eql(0); // start in triangle index + mesh.faceRanges[1].should.eql(2); // nb of triangles + + // testing face 2 + mesh.faceRanges[2].should.eql(2); // start in triangle index + mesh.faceRanges[3].should.eql(2); // nb of triangles + + // testing face 3 + mesh.faceRanges[4].should.eql(4); // start in triangle index + mesh.faceRanges[5].should.eql(2); // nb of triangles + + // testing face 4 + mesh.faceRanges[6].should.eql(6); // start in triangle index + mesh.faceRanges[7].should.eql(2); // nb of triangles + + // testing face 5 + mesh.faceRanges[8].should.eql(8); // start in triangle index + mesh.faceRanges[9].should.eql(2); // nb of triangles + + // testing face 6 + mesh.faceRanges[10].should.eql(10); // start in triangle index + mesh.faceRanges[11].should.eql(2); // nb of triangles + }); + it("Mesh#getFaceTriangles - mesh should provide a mechanism to extract triangles indexes of a given shape face", function () { + // --------------------------------------- face accessor + let arr = mesh.getFaceTriangles(shape.getFaces()[0]); + arr.should.eql(new Uint8Array([0, 1, 2, 2, 1, 3])); + + arr = mesh.getFaceTriangles(shape.getFaces()[1]); + arr.should.eql(new Uint8Array([5, 4, 6, 5, 6, 7])); + // etc... + }); + it("Mesh#edgeRanges - mesh should provide a mechanism to easily identify edges", function () { + // edge index + // --------------------------------------- edges + // testing edge 1 + mesh.edgeRanges[0].should.eql(0); // start in triangle index + mesh.edgeRanges[1].should.eql(2); // nb of triangles + + // testing edge 2 + mesh.edgeRanges[2].should.eql(2); // start in triangle index + mesh.edgeRanges[3].should.eql(2); // nb of triangles + + // testing edge 3 + mesh.edgeRanges[4].should.eql(4); // start in triangle index + mesh.edgeRanges[5].should.eql(2); // nb of triangles + + // testing edge 4 + mesh.edgeRanges[6].should.eql(6); // start in triangle index + mesh.edgeRanges[7].should.eql(2); // nb of triangles + + // testing edge 5 + mesh.edgeRanges[8].should.eql(8); // start in triangle index + mesh.edgeRanges[9].should.eql(2); // nb of triangles + + // testing edge 6 + mesh.edgeRanges[10].should.eql(10); // start in triangle index + mesh.edgeRanges[11].should.eql(2); // nb of triangles + + // testing edge 7 + mesh.edgeRanges[12].should.eql(12); // start in triangle index + mesh.edgeRanges[13].should.eql(2); // nb of triangles + + // testing edge 8 + mesh.edgeRanges[14].should.eql(14); // start in triangle index + mesh.edgeRanges[15].should.eql(2); // nb of triangles + + // testing edge 9 + mesh.edgeRanges[16].should.eql(16); // start in triangle index + mesh.edgeRanges[17].should.eql(2); // nb of triangles + + // testing edge 10 + mesh.edgeRanges[18].should.eql(18); // start in triangle index + mesh.edgeRanges[19].should.eql(2); // nb of triangles + + // testing edge 11 + mesh.edgeRanges[20].should.eql(20); // start in triangle index + mesh.edgeRanges[21].should.eql(2); // nb of triangles + + // testing edge 12 + mesh.edgeRanges[22].should.eql(22); // start in triangle index + mesh.edgeRanges[23].should.eql(2); // nb of triangles + }); + + it("Mesh#getEdgeIndices - mesh should provide a mechanism to extract the Polygon of a given edge", function () { + //xx console.log(mesh); + const arr = mesh.getEdgeIndices(shape.getEdges()[0]); + arr.should.eql(new Uint8Array([0, 1])); + }); + it("Mesh#triangleNormals - mesh should provide ", function () { + // face 0 - triangle 0 + mesh.triangleNormals[0].should.eql(0); + mesh.triangleNormals[1].should.eql(0); + mesh.triangleNormals[2].should.eql(0); + // face 0 - triangle 1 + mesh.triangleNormals[3].should.eql(0); + mesh.triangleNormals[4].should.eql(0); + mesh.triangleNormals[5].should.eql(0); + + // face 1 - triangle 0 + mesh.triangleNormals[6].should.eql(1); + mesh.triangleNormals[7].should.eql(1); + mesh.triangleNormals[8].should.eql(1); + // face 1 - triangle 1 + mesh.triangleNormals[9].should.eql(1); + mesh.triangleNormals[10].should.eql(1); + mesh.triangleNormals[11].should.eql(1); + + // face 2 - triangle 0= + mesh.triangleNormals[12].should.eql(2); + mesh.triangleNormals[13].should.eql(2); + mesh.triangleNormals[14].should.eql(2); + // face 2 - triangle 1 + mesh.triangleNormals[15].should.eql(2); + mesh.triangleNormals[16].should.eql(2); + mesh.triangleNormals[17].should.eql(2); + + // face 3 - triangle 0= + mesh.triangleNormals[18].should.eql(3); + mesh.triangleNormals[19].should.eql(3); + mesh.triangleNormals[20].should.eql(3); + // face 3 - triangle 1 + mesh.triangleNormals[21].should.eql(3); + mesh.triangleNormals[22].should.eql(3); + mesh.triangleNormals[23].should.eql(3); + + // etc... + }); + it("Mesh#normals - mesh should provide a normal array", function () { + // in the case of a simple box we expect to have 6 different normals + mesh.normals.length.should.eql(3 * 6); + + // first normal + mesh.normals[0].should.eql(-1); + mesh.normals[1].should.eql(0); + mesh.normals[2].should.eql(0); + + mesh.normals[3].should.eql(1); + mesh.normals[4].should.eql(0); + mesh.normals[5].should.eql(0); + + mesh.normals[6].should.eql(0); + mesh.normals[7].should.eql(-1); + mesh.normals[8].should.eql(0); + + mesh.normals[9].should.eql(0); + mesh.normals[10].should.eql(1); + mesh.normals[11].should.eql(0); + + mesh.normals[12].should.eql(0); + mesh.normals[13].should.eql(0); + mesh.normals[14].should.eql(-1); + + mesh.normals[15].should.eql(0); + mesh.normals[16].should.eql(0); + mesh.normals[17].should.eql(1); + }); }); describe("testing performance of meshing algorithms with various parameters", function () { + this.timeout(30000); + function makeUnitBox() { + return occ.makeBox([0, 0, 0], [100, 100, 100]); + } - this.timeout(30000); - - function makeUnitBox() { - return occ.makeBox([0, 0, 0], [100, 100, 100]); - } - - function makeSphere() { - return occ.makeSphere([0, 0, 0], 100); - } + function makeSphere() { + return occ.makeSphere([0, 0, 0], 100); + } - function makeLegoBrick() { - return shapeFactory.makeLegoBrick(occ, 4, 2, "thin"); - } + function makeLegoBrick() { + return shapeFactory.makeLegoBrick(occ, 4, 2, "thin"); + } - function installFor(name: string, makeShape: () => ISolid) { - - let shape2: ISolid; - beforeEach(function () { - shape2 = makeShape(); - }); + function installFor(name: string, makeShape: () => ISolid) { + let shape2: ISolid; + beforeEach(function () { + shape2 = makeShape(); + }); - function test_with(tol: Real, angle: Real) { - it(name + " testing with parameter : deflection : " + tol + " angle :" + angle, function () { - const mesh1 = shape2.createMesh(tol, angle); - debugLog(" vertices = ", mesh1.vertices.length); - debugLog(" triangles = ", mesh1.triangles.length); - }); + function test_with(tol: Real, angle: Real) { + it( + name + + " testing with parameter : deflection : " + + tol + + " angle :" + + angle, + function () { + const mesh1 = shape2.createMesh(tol, angle); + debugLog(" vertices = ", mesh1.vertices.length); + debugLog(" triangles = ", mesh1.triangles.length); } - - test_with(0.01, 0.5); - test_with(0.01, 5); - test_with(0.01, 10); - - test_with(0.1, 0.5); - test_with(0.1, 1); - test_with(0.1, 5); - test_with(0.1, 10); - test_with(0.1, 20); - - test_with(1, 0.5); - test_with(1, 1); - test_with(1, 5); - test_with(1, 10); - test_with(1, 20); - - test_with(2, 0.5); - test_with(2, 1); - test_with(2, 5); - test_with(2, 10); - test_with(2, 20); - - describe(name + " : comparing JSON ", function () { - - let shape2: ISolid; - beforeEach(function () { - shape2 = makeUnitBox(); - }); - it("should create default JSON file with acceptable size", function () { - shape2.name = "shape2"; - const obj1 = occ.buildSolidMesh(shape2); - debugLog("json json1", JSON.stringify(obj1, null, "\t")); - debugLog("json json1", JSON.stringify(obj1).length); - - }); - it("should create default JSON file with acceptable size", function () { - shape2.name = "shape2"; - const obj2 = occ.buildSolidMeshNew(shape2); - debugLog("json json1", JSON.stringify(obj2, null, "\t")); - debugLog("json json2", JSON.stringify(obj2).length); - - }); - }); - + ); } - installFor("makeLegoBrick", makeLegoBrick); - installFor("makeSphere", makeSphere); + test_with(0.01, 0.5); + test_with(0.01, 5); + test_with(0.01, 10); + + test_with(0.1, 0.5); + test_with(0.1, 1); + test_with(0.1, 5); + test_with(0.1, 10); + test_with(0.1, 20); + + test_with(1, 0.5); + test_with(1, 1); + test_with(1, 5); + test_with(1, 10); + test_with(1, 20); + + test_with(2, 0.5); + test_with(2, 1); + test_with(2, 5); + test_with(2, 10); + test_with(2, 20); + + describe(name + " : comparing JSON ", function () { + let shape2: ISolid; + beforeEach(function () { + shape2 = makeUnitBox(); + }); + it("should create default JSON file with acceptable size", function () { + shape2.name = "shape2"; + const obj1 = occ.buildSolidMesh(shape2); + debugLog("json json1", JSON.stringify(obj1, null, "\t")); + debugLog("json json1", JSON.stringify(obj1).length); + }); + it("should create default JSON file with acceptable size", function () { + shape2.name = "shape2"; + const obj2 = occ.buildSolidMeshNew(shape2); + debugLog("json json1", JSON.stringify(obj2, null, "\t")); + debugLog("json json2", JSON.stringify(obj2).length); + }); + }); + } + installFor("makeLegoBrick", makeLegoBrick); + installFor("makeSphere", makeSphere); }); diff --git a/test/test_named_faces.ts b/test/test_named_faces.ts index a88b3e6..886a44e 100644 --- a/test/test_named_faces.ts +++ b/test/test_named_faces.ts @@ -1,361 +1,378 @@ - import * as should from "should"; import { IBoundingBox, ISolid, occ } from ".."; const doDebug = false; function dumpSolid(b: ISolid) { - if (doDebug) { - console.log(" faces = ", b.getFaces().map((e) => b.getShapeName(e)).join(", ")); - console.log(" edges = ", b.getEdges().map((e) => b.getShapeName(e)).join(", ")); - console.log(" vertices = ", b.getVertices().map((e) => b.getShapeName(e)).join(", ")); - } + if (doDebug) { + console.log( + " faces = ", + b + .getFaces() + .map((e) => b.getShapeName(e)) + .join(", ") + ); + console.log( + " edges = ", + b + .getEdges() + .map((e) => b.getShapeName(e)) + .join(", ") + ); + console.log( + " vertices = ", + b + .getVertices() + .map((e) => b.getShapeName(e)) + .join(", ") + ); + } } // see https://npmjs.org/package/should describe("testing face naming on simple box", () => { + let solid: ISolid; + before(() => { + solid = occ.makeBox([0, 0, 0], [10, 20, 30]); + }); + it("should have only six named faces", () => { + Object.keys(solid.faces).length.should.equal(6); + }); + it("should have a face called 'top'", () => { + solid.faces.should.have.property("top"); + }); + it("should have a face called 'bottom'", () => { + solid.faces.should.have.property("bottom"); + }); + it("should have a face called 'front'", () => { + solid.faces.should.have.property("front"); + }); + it("should have a face called 'back'", () => { + solid.faces.should.have.property("back"); + }); + it("should have a face called 'left'", () => { + solid.faces.should.have.property("left"); + }); + it("should have a face called 'right'", () => { + solid.faces.should.have.property("right"); + }); + it("should provide a service to retrieve the name of its shapes", () => { + solid.getShapeName(solid.faces.top).should.equal("top"); + solid.getShapeName(solid.faces.bottom).should.equal("bottom"); + solid.getShapeName(solid.faces.back).should.equal("back"); + solid.getShapeName(solid.faces.front).should.equal("front"); + solid.getShapeName(solid.faces.left).should.equal("left"); + solid.getShapeName(solid.faces.right).should.equal("right"); + }); + it("should return undefined when shape cannot be found", () => { + const solid2 = occ.makeBox(10, 20, 30); + should.exist(solid.getShapeName(solid.faces.right)); + should.not.exist(solid.getShapeName(solid2.faces.right)); + }); - let solid: ISolid; - before(() => { - solid = occ.makeBox([0, 0, 0], [10, 20, 30]); - }); - it("should have only six named faces", () => { - Object.keys(solid.faces).length.should.equal(6); - }); - it("should have a face called 'top'", () => { - solid.faces.should.have.property("top"); - }); - it("should have a face called 'bottom'", () => { - solid.faces.should.have.property("bottom"); - }); - it("should have a face called 'front'", () => { - solid.faces.should.have.property("front"); - }); - it("should have a face called 'back'", () => { - solid.faces.should.have.property("back"); - }); - it("should have a face called 'left'", () => { - solid.faces.should.have.property("left"); - }); - it("should have a face called 'right'", () => { - solid.faces.should.have.property("right"); - }); - it("should provide a service to retrieve the name of its shapes", () => { - solid.getShapeName(solid.faces.top).should.equal("top"); - solid.getShapeName(solid.faces.bottom).should.equal("bottom"); - solid.getShapeName(solid.faces.back).should.equal("back"); - solid.getShapeName(solid.faces.front).should.equal("front"); - solid.getShapeName(solid.faces.left).should.equal("left"); - solid.getShapeName(solid.faces.right).should.equal("right"); - }); - it("should return undefined when shape cannot be found", () => { - const solid2 = occ.makeBox(10, 20, 30); - should.exist(solid.getShapeName(solid.faces.right)); - should.not.exist(solid.getShapeName(solid2.faces.right)); + it("should have 'top' face planar and at z=30", () => { + solid.faces.top.isPlanar.should.equal(true); + solid.faces.top.centreOfMass.z.should.equal(30); - }); + solid.faces.top.centreOfMass.x.should.equal(5); + solid.faces.top.centreOfMass.y.should.equal(10); + }); + it("should have 'bottom' face planar and at z=0", () => { + solid.faces.bottom.isPlanar.should.equal(true); + solid.faces.bottom.centreOfMass.z.should.equal(0); - it("should have 'top' face planar and at z=30", () => { - solid.faces.top.isPlanar.should.equal(true); - solid.faces.top.centreOfMass.z.should.equal(30); + solid.faces.bottom.centreOfMass.x.should.equal(5); + solid.faces.bottom.centreOfMass.y.should.equal(10); + }); + it("should have 'right' face planar and at y=20", () => { + solid.faces.right.isPlanar.should.equal(true); + solid.faces.right.centreOfMass.y.should.equal(20); - solid.faces.top.centreOfMass.x.should.equal(5); - solid.faces.top.centreOfMass.y.should.equal(10); - }); - it("should have 'bottom' face planar and at z=0", () => { - solid.faces.bottom.isPlanar.should.equal(true); - solid.faces.bottom.centreOfMass.z.should.equal(0); + solid.faces.right.centreOfMass.z.should.equal(15); + solid.faces.right.centreOfMass.x.should.equal(5); + }); + it("should have 'left' face planar and at y=0", () => { + solid.faces.left.isPlanar.should.equal(true); + solid.faces.left.centreOfMass.y.should.equal(0); - solid.faces.bottom.centreOfMass.x.should.equal(5); - solid.faces.bottom.centreOfMass.y.should.equal(10); - }); - it("should have 'right' face planar and at y=20", () => { - solid.faces.right.isPlanar.should.equal(true); - solid.faces.right.centreOfMass.y.should.equal(20); + solid.faces.left.centreOfMass.z.should.equal(15); + solid.faces.left.centreOfMass.x.should.equal(5); + }); + it("should have 'front' face planar and at x=10", () => { + solid.faces.front.isPlanar.should.equal(true); + solid.faces.front.centreOfMass.x.should.equal(10); - solid.faces.right.centreOfMass.z.should.equal(15); - solid.faces.right.centreOfMass.x.should.equal(5); - }); - it("should have 'left' face planar and at y=0", () => { - solid.faces.left.isPlanar.should.equal(true); - solid.faces.left.centreOfMass.y.should.equal(0); + solid.faces.front.centreOfMass.y.should.equal(10); + solid.faces.front.centreOfMass.z.should.equal(15); + }); + it("should have 'back' face planar and at x=0", () => { + solid.faces.back.isPlanar.should.equal(true); + solid.faces.back.centreOfMass.x.should.equal(0); - solid.faces.left.centreOfMass.z.should.equal(15); - solid.faces.left.centreOfMass.x.should.equal(5); - }); - it("should have 'front' face planar and at x=10", () => { - solid.faces.front.isPlanar.should.equal(true); - solid.faces.front.centreOfMass.x.should.equal(10); - - solid.faces.front.centreOfMass.y.should.equal(10); - solid.faces.front.centreOfMass.z.should.equal(15); - }); - it("should have 'back' face planar and at x=0", () => { - solid.faces.back.isPlanar.should.equal(true); - solid.faces.back.centreOfMass.x.should.equal(0); - - solid.faces.back.centreOfMass.y.should.equal(10); - solid.faces.back.centreOfMass.z.should.equal(15); - }); - it("should have named edges", () => { - - const test = solid.getEdges().map((e) => { - return solid.getShapeName(e); - }); - test.sort().join(" ").should.equal("EXY EXZ EXy EXz EYZ EYz ExY ExZ Exy Exz EyZ Eyz"); - }); - - it("should have named vertex", () => { - const test = solid.getVertices().map((e) => { - return solid.getShapeName(e); - }); - test.sort().join(" ").should.equal("VXYZ VXYz VXyZ VXyz VxYZ VxYz VxyZ Vxyz"); - }); + solid.faces.back.centreOfMass.y.should.equal(10); + solid.faces.back.centreOfMass.z.should.equal(15); + }); + it("should have named edges", () => { + const test = solid.getEdges().map((e) => { + return solid.getShapeName(e); + }); + test + .sort() + .join(" ") + .should.equal("EXY EXZ EXy EXz EYZ EYz ExY ExZ Exy Exz EyZ Eyz"); + }); + it("should have named vertex", () => { + const test = solid.getVertices().map((e) => { + return solid.getShapeName(e); + }); + test + .sort() + .join(" ") + .should.equal("VXYZ VXYz VXyZ VXyz VxYZ VxYz VxyZ Vxyz"); + }); }); describe("testing face naming on simple sphere", () => { - let solid: ISolid; - before(() => { - solid = occ.makeSphere([0, 0, 0], 30); - }); - it("should have only one named face", () => { - Object.keys(solid.faces).length.should.equal(1); - }); - it("should have a face called 'lateral'", () => { - solid.faces.should.have.property("lateral"); - solid.getShapeName(solid.faces.lateral).should.equal("lateral"); - }); - it("should have not have a face called 'top' ", () => { - solid.faces.should.not.have.property("top"); - }); + let solid: ISolid; + before(() => { + solid = occ.makeSphere([0, 0, 0], 30); + }); + it("should have only one named face", () => { + Object.keys(solid.faces).length.should.equal(1); + }); + it("should have a face called 'lateral'", () => { + solid.faces.should.have.property("lateral"); + solid.getShapeName(solid.faces.lateral).should.equal("lateral"); + }); + it("should have not have a face called 'top' ", () => { + solid.faces.should.not.have.property("top"); + }); }); describe("testing face naming on combined boxes", () => { - let solid: ISolid; - let box1: ISolid; - let box2: ISolid; - before(() => { - box1 = occ.makeBox([0, 0, 0], [10, 10, 10]); - box2 = occ.makeBox([5, 5, 5], [15, 15, 15]); - solid = occ.fuse(box1, box2); - dumpSolid(solid); - - }); - it(" should expose 12 named faces", () => { - Object.keys(solid.faces).length.should.equal(12); - }); - it("should preserve bottom/left/back faces of solid1", () => { - should.exist(solid.getShapeName(box1.faces.bottom)); - should.exist(solid.getShapeName(box1.faces.left)); - should.exist(solid.getShapeName(box1.faces.back)); - }); - it("should replace top/right/front faces of solid1", () => { - should.not.exist(solid.getShapeName(box1.faces.top)); - should.not.exist(solid.getShapeName(box1.faces.right)); - should.not.exist(solid.getShapeName(box1.faces.front)); - }); - it("should replace bottom/left/back faces of solid2", () => { - should.not.exist(solid.getShapeName(box2.faces.bottom)); - should.not.exist(solid.getShapeName(box2.faces.left)); - should.not.exist(solid.getShapeName(box2.faces.back)); - }); - it("should preserve top/right/front faces of solid2", () => { - should.exist(solid.getShapeName(box2.faces.top)); - should.exist(solid.getShapeName(box2.faces.right)); - should.exist(solid.getShapeName(box2.faces.front)); - }); + let solid: ISolid; + let box1: ISolid; + let box2: ISolid; + before(() => { + box1 = occ.makeBox([0, 0, 0], [10, 10, 10]); + box2 = occ.makeBox([5, 5, 5], [15, 15, 15]); + solid = occ.fuse(box1, box2); + dumpSolid(solid); + }); + it(" should expose 12 named faces", () => { + Object.keys(solid.faces).length.should.equal(12); + }); + it("should preserve bottom/left/back faces of solid1", () => { + should.exist(solid.getShapeName(box1.faces.bottom)); + should.exist(solid.getShapeName(box1.faces.left)); + should.exist(solid.getShapeName(box1.faces.back)); + }); + it("should replace top/right/front faces of solid1", () => { + should.not.exist(solid.getShapeName(box1.faces.top)); + should.not.exist(solid.getShapeName(box1.faces.right)); + should.not.exist(solid.getShapeName(box1.faces.front)); + }); + it("should replace bottom/left/back faces of solid2", () => { + should.not.exist(solid.getShapeName(box2.faces.bottom)); + should.not.exist(solid.getShapeName(box2.faces.left)); + should.not.exist(solid.getShapeName(box2.faces.back)); + }); + it("should preserve top/right/front faces of solid2", () => { + should.exist(solid.getShapeName(box2.faces.top)); + should.exist(solid.getShapeName(box2.faces.right)); + should.exist(solid.getShapeName(box2.faces.front)); + }); }); describe("testing face naming on a box whose top/right/front corner is carved with a other box", () => { - let solid: ISolid; - let box1: ISolid; - let box2: ISolid; - before(() => { - box1 = occ.makeBox([0, 0, 0], [10, 10, 10]); - box2 = occ.makeBox([5, 5, 5], [15, 15, 15]); - solid = occ.cut(box1, box2); - }); - it(" should expose 9 named faces", () => { - Object.keys(solid.faces).length.should.equal(9); - }); - it("should preserve bottom/left/back faces of original box", () => { - should.exist(solid.getShapeName(box1.faces.bottom)); - should.exist(solid.getShapeName(box1.faces.left)); - should.exist(solid.getShapeName(box1.faces.back)); - }); - it("should replace top/right/front faces of original box", () => { - should.not.exist(solid.getShapeName(box1.faces.top)); - should.not.exist(solid.getShapeName(box1.faces.right)); - should.not.exist(solid.getShapeName(box1.faces.front)); - }); - it("should replace bottom/left/back faces of cutting box", () => { - should.not.exist(solid.getShapeName(box2.faces.bottom)); - should.not.exist(solid.getShapeName(box2.faces.left)); - should.not.exist(solid.getShapeName(box2.faces.back)); - }); - it("should delete top/right/front faces of cutting box", () => { - should.not.exist(solid.getShapeName(box2.faces.top)); - should.not.exist(solid.getShapeName(box2.faces.right)); - should.not.exist(solid.getShapeName(box2.faces.front)); - }); + let solid: ISolid; + let box1: ISolid; + let box2: ISolid; + before(() => { + box1 = occ.makeBox([0, 0, 0], [10, 10, 10]); + box2 = occ.makeBox([5, 5, 5], [15, 15, 15]); + solid = occ.cut(box1, box2); + }); + it(" should expose 9 named faces", () => { + Object.keys(solid.faces).length.should.equal(9); + }); + it("should preserve bottom/left/back faces of original box", () => { + should.exist(solid.getShapeName(box1.faces.bottom)); + should.exist(solid.getShapeName(box1.faces.left)); + should.exist(solid.getShapeName(box1.faces.back)); + }); + it("should replace top/right/front faces of original box", () => { + should.not.exist(solid.getShapeName(box1.faces.top)); + should.not.exist(solid.getShapeName(box1.faces.right)); + should.not.exist(solid.getShapeName(box1.faces.front)); + }); + it("should replace bottom/left/back faces of cutting box", () => { + should.not.exist(solid.getShapeName(box2.faces.bottom)); + should.not.exist(solid.getShapeName(box2.faces.left)); + should.not.exist(solid.getShapeName(box2.faces.back)); + }); + it("should delete top/right/front faces of cutting box", () => { + should.not.exist(solid.getShapeName(box2.faces.top)); + should.not.exist(solid.getShapeName(box2.faces.right)); + should.not.exist(solid.getShapeName(box2.faces.front)); + }); }); describe("testing face naming on a box with a split face ('top' face)", () => { - let solid: ISolid; - let block: ISolid; - let cutting_solid: ISolid; - before(() => { - block = occ.makeBox([0, 0, 0], [100, 100, 25]); - cutting_solid = occ.makeBox([40, -10, 10], [60, 110, 50]); - solid = occ.cut(block, cutting_solid); - }); - it(" should expose 10 named faces", () => { - Object.keys(solid.faces).length.should.equal(10); - solid.getFaces().length.should.equal(10); - }); - it("should have a preserved front/bottom/back faces ", () => { - should.exist(solid.getShapeName(block.faces.front)); - should.exist(solid.getShapeName(block.faces.bottom)); - should.exist(solid.getShapeName(block.faces.back)); - }); - it("should have modified right/top/left faces ", () => { - should.not.exist(solid.getShapeName(block.faces.right)); - should.not.exist(solid.getShapeName(block.faces.top)); - should.not.exist(solid.getShapeName(block.faces.left)); - }); - - + let solid: ISolid; + let block: ISolid; + let cutting_solid: ISolid; + before(() => { + block = occ.makeBox([0, 0, 0], [100, 100, 25]); + cutting_solid = occ.makeBox([40, -10, 10], [60, 110, 50]); + solid = occ.cut(block, cutting_solid); + }); + it(" should expose 10 named faces", () => { + Object.keys(solid.faces).length.should.equal(10); + solid.getFaces().length.should.equal(10); + }); + it("should have a preserved front/bottom/back faces ", () => { + should.exist(solid.getShapeName(block.faces.front)); + should.exist(solid.getShapeName(block.faces.bottom)); + should.exist(solid.getShapeName(block.faces.back)); + }); + it("should have modified right/top/left faces ", () => { + should.not.exist(solid.getShapeName(block.faces.right)); + should.not.exist(solid.getShapeName(block.faces.top)); + should.not.exist(solid.getShapeName(block.faces.left)); + }); }); describe("testing face naming on a box fused with a box that have a common face , leading to 4 merging faces", () => { - let box1: ISolid; - let box2: ISolid - let solid: ISolid; - before(() => { - // +------+ +------+ - // |`. `. |\ \ - // | `+------+ | +------+ - // | | | | | | - // + | | + | | - // `. | | \| | - // `+------+ +------+ - // - box1 = occ.makeBox([0, 0, 0], [10, 10, 10]); - box2 = occ.makeBox([10, 0, 0], [20, 10, 10]); // box2 back face is same as box1.front frace - solid = occ.fuse(box1, box2); - }); - it("should expose 10 faces", () => { - // - // +----++----+ +----++----+ - // | || | | | - // | || | => | | - // +----++----+ +----++----+ - // - // Although the side surface could be merged - // - Object.keys(solid.faces).length.should.equal(10); - solid.getFaces().length.should.equal(10); - occ.gc(); - const faces = solid.getFaces(); - faces.forEach((face, i) => { - const bbox = face.getBoundingBox(); - console.log("face i ", i, solid.getShapeName(face), bbox.toString()); - }); - }); -}); -describe("testing face naming on a box with a top face split twice leading to 4 isolated corners)", () => { + let box1: ISolid; + let box2: ISolid; + let solid: ISolid; + before(() => { + // +------+ +------+ + // |`. `. |\ \ + // | `+------+ | +------+ + // | | | | | | + // + | | + | | + // `. | | \| | + // `+------+ +------+ // - // // in this sample, the top face of the block will be split in two pieces - // during the first cut operation.Then each part will be split in two pieces again - // during the second cut operation. + box1 = occ.makeBox([0, 0, 0], [10, 10, 10]); + box2 = occ.makeBox([10, 0, 0], [20, 10, 10]); // box2 back face is same as box1.front frace + solid = occ.fuse(box1, box2); + }); + it("should expose 10 faces", () => { // - let solid: ISolid, block: ISolid, cutting_solid1: ISolid, cutting_solid2: ISolid; - before(() => { - block = occ.makeBox([0, 0, 0], [100, 100, 25]); - cutting_solid1 = occ.makeBox([40, -10, 10], [60, 110, 50]); - cutting_solid2 = occ.makeBox([-10, 40, 10], [110, 60, 50]); - solid = occ.cut(block, cutting_solid1); - solid = occ.cut(solid, cutting_solid2); - }); - it("should expose 22 named faces", () => { - Object.keys(solid.faces).length.should.equal(22); - solid.getFaces().length.should.equal(22); - }); - it("should have a preserved bottom face", () => { - should.exist(solid.getShapeName(block.faces.bottom)); - }); - it("should have modified front/back/right/top/left faces ", () => { - should.not.exist(solid.getShapeName(block.faces.front)); - should.not.exist(solid.getShapeName(block.faces.back)); - should.not.exist(solid.getShapeName(block.faces.right)); - should.not.exist(solid.getShapeName(block.faces.top)); - should.not.exist(solid.getShapeName(block.faces.left)); - }); - - -}); -describe("testing face naming on a box with a top face split by a cross shape leading to 4 isolated corners", () => { + // +----++----+ +----++----+ + // | || | | | + // | || | => | | + // +----++----+ +----++----+ // - // in this sample, the top face of the block will be split in 4 pieces - // during the single cut operation + // Although the side surface could be merged // - let solid: ISolid, block: ISolid, cutting_solid1: ISolid, cutting_solid2: ISolid, cutting_solid: ISolid; - before(() => { - block = occ.makeBox([0, 0, 0], [100, 100, 25]); - cutting_solid1 = occ.makeBox([40, -10, 10], [60, 110, 50]); - cutting_solid2 = occ.makeBox([-10, 40, 10], [110, 60, 50]); - cutting_solid = occ.fuse(cutting_solid1, cutting_solid2); - solid = occ.cut(block, cutting_solid); - }); - it("should expose 22 named faces", () => { - Object.keys(solid.faces).length.should.equal(22); - solid.getFaces().length.should.equal(22); - }); - it("should have a preserved bottom face", () => { - should.exist(solid.getShapeName(block.faces.bottom)); - }); - it("should have modified front/back/right/top/left faces ", () => { - should.not.exist(solid.getShapeName(block.faces.front)); - should.not.exist(solid.getShapeName(block.faces.back)); - should.not.exist(solid.getShapeName(block.faces.right)); - should.not.exist(solid.getShapeName(block.faces.top)); - should.not.exist(solid.getShapeName(block.faces.left)); - - solid.faces.should.have.property("m1:" + "front" + ":0"); - solid.faces.should.have.property("m1:" + "back" + ":0"); - solid.faces.should.have.property("m1:" + "top" + ":0"); - solid.faces.should.have.property("m1:" + "left" + ":0"); + Object.keys(solid.faces).length.should.equal(10); + solid.getFaces().length.should.equal(10); + occ.gc(); + const faces = solid.getFaces(); + faces.forEach((face, i) => { + const bbox = face.getBoundingBox(); + console.log("face i ", i, solid.getShapeName(face), bbox.toString()); + }); + }); +}); +describe("testing face naming on a box with a top face split twice leading to 4 isolated corners)", () => { + // + // // in this sample, the top face of the block will be split in two pieces + // during the first cut operation.Then each part will be split in two pieces again + // during the second cut operation. + // + let solid: ISolid, + block: ISolid, + cutting_solid1: ISolid, + cutting_solid2: ISolid; + before(() => { + block = occ.makeBox([0, 0, 0], [100, 100, 25]); + cutting_solid1 = occ.makeBox([40, -10, 10], [60, 110, 50]); + cutting_solid2 = occ.makeBox([-10, 40, 10], [110, 60, 50]); + solid = occ.cut(block, cutting_solid1); + solid = occ.cut(solid, cutting_solid2); + }); + it("should expose 22 named faces", () => { + Object.keys(solid.faces).length.should.equal(22); + solid.getFaces().length.should.equal(22); + }); + it("should have a preserved bottom face", () => { + should.exist(solid.getShapeName(block.faces.bottom)); + }); + it("should have modified front/back/right/top/left faces ", () => { + should.not.exist(solid.getShapeName(block.faces.front)); + should.not.exist(solid.getShapeName(block.faces.back)); + should.not.exist(solid.getShapeName(block.faces.right)); + should.not.exist(solid.getShapeName(block.faces.top)); + should.not.exist(solid.getShapeName(block.faces.left)); + }); +}); +describe("testing face naming on a box with a top face split by a cross shape leading to 4 isolated corners", () => { + // + // in this sample, the top face of the block will be split in 4 pieces + // during the single cut operation + // + let solid: ISolid, + block: ISolid, + cutting_solid1: ISolid, + cutting_solid2: ISolid, + cutting_solid: ISolid; + before(() => { + block = occ.makeBox([0, 0, 0], [100, 100, 25]); + cutting_solid1 = occ.makeBox([40, -10, 10], [60, 110, 50]); + cutting_solid2 = occ.makeBox([-10, 40, 10], [110, 60, 50]); + cutting_solid = occ.fuse(cutting_solid1, cutting_solid2); + solid = occ.cut(block, cutting_solid); + }); + it("should expose 22 named faces", () => { + Object.keys(solid.faces).length.should.equal(22); + solid.getFaces().length.should.equal(22); + }); + it("should have a preserved bottom face", () => { + should.exist(solid.getShapeName(block.faces.bottom)); + }); + it("should have modified front/back/right/top/left faces ", () => { + should.not.exist(solid.getShapeName(block.faces.front)); + should.not.exist(solid.getShapeName(block.faces.back)); + should.not.exist(solid.getShapeName(block.faces.right)); + should.not.exist(solid.getShapeName(block.faces.top)); + should.not.exist(solid.getShapeName(block.faces.left)); - solid.faces.should.not.have.property("m1:" + "front" + ":1"); - solid.faces.should.not.have.property("m1:" + "back" + ":1"); - solid.faces.should.not.have.property("m1:" + "left" + ":1"); + solid.faces.should.have.property("m1:" + "front" + ":0"); + solid.faces.should.have.property("m1:" + "back" + ":0"); + solid.faces.should.have.property("m1:" + "top" + ":0"); + solid.faces.should.have.property("m1:" + "left" + ":0"); - }); + solid.faces.should.not.have.property("m1:" + "front" + ":1"); + solid.faces.should.not.have.property("m1:" + "back" + ":1"); + solid.faces.should.not.have.property("m1:" + "left" + ":1"); + }); - it("should have 4 (and only 4) faces that have been generated from the top face of the original block", () => { - // this could be tested using face names - const name = block.getShapeName(block.faces.top); + it("should have 4 (and only 4) faces that have been generated from the top face of the original block", () => { + // this could be tested using face names + const name = block.getShapeName(block.faces.top); - solid.faces.should.have.property("m1:" + name + ":0"); - solid.faces.should.have.property("m1:" + name + ":1"); - solid.faces.should.have.property("m1:" + name + ":2"); - solid.faces.should.have.property("m1:" + name + ":3"); - solid.faces.should.not.have.property("m1:" + name + ":4"); - }); + solid.faces.should.have.property("m1:" + name + ":0"); + solid.faces.should.have.property("m1:" + name + ":1"); + solid.faces.should.have.property("m1:" + name + ":2"); + solid.faces.should.have.property("m1:" + name + ":3"); + solid.faces.should.not.have.property("m1:" + name + ":4"); + }); }); describe("testing naming with makeFillet operation", () => { + let solid: ISolid; + before(() => { + solid = occ.makeBox([10, 20, 30], [100, 200, 300]); + dumpSolid(solid); + const edges = solid.getCommonEdges(solid.faces.front, solid.faces.left)!; + edges.should.be.instanceOf(Array); - let solid: ISolid; - before(() => { - solid = occ.makeBox([10, 20, 30], [100, 200, 300]); - dumpSolid(solid); - - const edges = solid.getCommonEdges(solid.faces.front, solid.faces.left)!; - edges.should.be.instanceOf(Array); - - solid = occ.makeFillet(solid, edges[0], 10); - dumpSolid(solid); - - }); - it("should have numFaces with expected number of faces", () => { - //xx console.log(Object.keys(solid.faces).join(", ")); - Object.keys(solid.faces).length.should.equal(solid.numFaces); - }); + solid = occ.makeFillet(solid, edges[0], 10); + dumpSolid(solid); + }); + it("should have numFaces with expected number of faces", () => { + //xx console.log(Object.keys(solid.faces).join(", ")); + Object.keys(solid.faces).length.should.equal(solid.numFaces); + }); }); diff --git a/test/test_relativePerformanceBREPSTEP.ts b/test/test_relativePerformanceBREPSTEP.ts index 0cff7ba..576563c 100644 --- a/test/test_relativePerformanceBREPSTEP.ts +++ b/test/test_relativePerformanceBREPSTEP.ts @@ -1,4 +1,3 @@ - import path from "path"; import fs from "fs"; import "should"; @@ -9,102 +8,102 @@ import { removeFile, getTemporaryFilePath } from "./helpers"; const ProgressBar = require("progress"); async function myReadStep(filename: string): Promise { + let brepFilename = getTemporaryFilePath({ suffix: ".brep" }); - let brepFilename = getTemporaryFilePath({ suffix: ".brep" }); - - console.log("brepFilename", brepFilename); - - let bar = new ProgressBar("reading file [:bar] :percent elapsed :elapseds ETA :etas", { - complete: "=", - incomplete: "-", - width: 100, - total: 1000 - }); - - let solids: ISolid[] = []; - - function progressFunc(percent: number) { - bar.tick(percent); - } - - function performMesh(solids: ISolid[]) { - let bar = new ProgressBar("meshing solids [:bar] :percent elapsed :elapseds ETA :etas", { - complete: "=", - incomplete: "-", - width: 100, - total: solids.length - }); - for (let i in solids) { - bar.tick(); - let solid = solids[i]; - solid.numFaces.should.be.greaterThan(1); - - solid.name = "solid_" + i; - try { - occ.buildSolidMesh(solid); - let mesh = solid.mesh; - solid.mesh.numVertices.should.be.greaterThan(3); - } - catch (err) { - - } - } - console.log("\n"); - } - - - function chrono(async_func: (callback: (err?: Error | null) => void) => void, message: string, callback: (err?: Error | null) => void) { - - let t1: Date; - let t2: Date; - let elapsed: number; - t1 = new Date(); + console.log("brepFilename", brepFilename); - async_func((err?: Error | null) => { - - t2 = new Date(); - elapsed = Math.round((t2.getTime() - t1.getTime()) / 10) / 100; - - console.log("\ndone " + message + " in ", elapsed, " seconds"); - callback(err); - }); - } - - - async function read_original_step_file(): Promise { - let t1 = new Date(); - return await promisify(occ.readSTEP)(filename); + let bar = new ProgressBar( + "reading file [:bar] :percent elapsed :elapseds ETA :etas", + { + complete: "=", + incomplete: "-", + width: 100, + total: 1000, } + ); - async function perform_mesh_on_solids() { - // make sure we have a mesh, so it can be saved in the BREP file - performMesh(solids); - } + let solids: ISolid[] = []; - async function write_solids_to_brep() { - occ.writeBREP(brepFilename, solids); - } + function progressFunc(percent: number) { + bar.tick(percent); + } - async function read_brep_file_again() { - return await promisify(occ.readSTEP)(filename); + function performMesh(solids: ISolid[]) { + let bar = new ProgressBar( + "meshing solids [:bar] :percent elapsed :elapseds ETA :etas", + { + complete: "=", + incomplete: "-", + width: 100, + total: solids.length, + } + ); + for (let i in solids) { + bar.tick(); + let solid = solids[i]; + solid.numFaces.should.be.greaterThan(1); + + solid.name = "solid_" + i; + try { + occ.buildSolidMesh(solid); + let mesh = solid.mesh; + solid.mesh.numVertices.should.be.greaterThan(3); + } catch (err) {} } - - await read_original_step_file(); - await perform_mesh_on_solids(); - await write_solids_to_brep(); - await read_brep_file_again(); - await perform_mesh_on_solids(); - removeFile(brepFilename); - - + console.log("\n"); + } + + function chrono( + async_func: (callback: (err?: Error | null) => void) => void, + message: string, + callback: (err?: Error | null) => void + ) { + let t1: Date; + let t2: Date; + let elapsed: number; + t1 = new Date(); + + async_func((err?: Error | null) => { + t2 = new Date(); + elapsed = Math.round((t2.getTime() - t1.getTime()) / 10) / 100; + + console.log("\ndone " + message + " in ", elapsed, " seconds"); + callback(err); + }); + } + + async function read_original_step_file(): Promise { + let t1 = new Date(); + return await promisify(occ.readSTEP)(filename); + } + + async function perform_mesh_on_solids() { + // make sure we have a mesh, so it can be saved in the BREP file + performMesh(solids); + } + + async function write_solids_to_brep() { + occ.writeBREP(brepFilename, solids); + } + + async function read_brep_file_again() { + return await promisify(occ.readSTEP)(filename); + } + + await read_original_step_file(); + await perform_mesh_on_solids(); + await write_solids_to_brep(); + await read_brep_file_again(); + await perform_mesh_on_solids(); + removeFile(brepFilename); } // myReadStep("test/kuka.stp"); describe("testing relative performance of BREP and STEP I/O", function () { - this.timeout(40000); - it("should read kuka robot", async () => { - let filename = path.join(__dirname, "kuka.stp"); - fs.existsSync(filename).should.eql(true, "kuka.stp should exists"); - await myReadStep(filename); - }); + this.timeout(40000); + it("should read kuka robot", async () => { + let filename = path.join(__dirname, "kuka.stp"); + fs.existsSync(filename).should.eql(true, "kuka.stp should exists"); + await myReadStep(filename); + }); }); diff --git a/test/test_scriptrunner.ts b/test/test_scriptrunner.ts index c93c3e5..be61444 100644 --- a/test/test_scriptrunner.ts +++ b/test/test_scriptrunner.ts @@ -2,58 +2,60 @@ import should from "should"; import { ScriptRunner } from ".."; describe("Script Runner", function () { - let scriptRunner: ScriptRunner; - let myEnv = { foo: "bar" }; - - before(() => { - scriptRunner = new ScriptRunner(myEnv); - }); - - it("SR1 - should not be possible load external module with require", function (done) { - - scriptRunner.run("let fs= require('fs');", () => { - should.fail(false, "done callback"); - done(new Error("test has failed: 'require' call hasn't been intercepted")); - }, (err: Error) => { - should.exist(err); - err.message.should.equal("require is forbidden"); - done(null); // - }); - }); - it("should not be possible to use eval", function (done) { - - scriptRunner.run("eval('a=10');", - - function () { - should.fail(false, "done callback"); - done(new Error("test has failed: 'eval' call hasn't been intercepted")); - }, - function error_callback(err: Error) { - should.exist(err); - err.message.should.equal("eval is forbidden"); - done(null); // - } - ); - - }); - it("should expose environment provided by the caller", function (done) { - - scriptRunner.env.logs = []; // purge log - scriptRunner.env.logs.length.should.equal(0); - scriptRunner.env.foo.should.equal("bar"); - scriptRunner.run("console.log(foo); foo='baz'", - - function () { - - scriptRunner.env.foo.should.equal("baz"); - scriptRunner.env.logs.length.should.equal(1); - - done(); - }, - function error_callback(err: Error) { - done(err); // should not fail - } + let scriptRunner: ScriptRunner; + let myEnv = { foo: "bar" }; + + before(() => { + scriptRunner = new ScriptRunner(myEnv); + }); + + it("SR1 - should not be possible load external module with require", function (done) { + scriptRunner.run( + "let fs= require('fs');", + () => { + should.fail(false, "done callback"); + done( + new Error("test has failed: 'require' call hasn't been intercepted") ); - - }); + }, + (err: Error) => { + should.exist(err); + err.message.should.equal("require is forbidden"); + done(null); // + } + ); + }); + it("should not be possible to use eval", function (done) { + scriptRunner.run( + "eval('a=10');", + + function () { + should.fail(false, "done callback"); + done(new Error("test has failed: 'eval' call hasn't been intercepted")); + }, + function error_callback(err: Error) { + should.exist(err); + err.message.should.equal("eval is forbidden"); + done(null); // + } + ); + }); + it("should expose environment provided by the caller", function (done) { + scriptRunner.env.logs = []; // purge log + scriptRunner.env.logs.length.should.equal(0); + scriptRunner.env.foo.should.equal("bar"); + scriptRunner.run( + "console.log(foo); foo='baz'", + + function () { + scriptRunner.env.foo.should.equal("baz"); + scriptRunner.env.logs.length.should.equal(1); + + done(); + }, + function error_callback(err: Error) { + done(err); // should not fail + } + ); + }); }); diff --git a/test/test_solid.ts b/test/test_solid.ts index b95206e..3a86891 100644 --- a/test/test_solid.ts +++ b/test/test_solid.ts @@ -1,757 +1,777 @@ import should from "should"; -import { EdgeIterator, FaceIterator, IBoundingBox, IEdge, IFace, IShapeIterator, ISolid, Solid, Triplet, occ } from ".."; +import { + EdgeIterator, + FaceIterator, + IBoundingBox, + IEdge, + IFace, + IShapeIterator, + ISolid, + Solid, + Triplet, + occ, +} from ".."; const DEG2RAD = Math.PI / 180; // see https://npmjs.org/package/should describe("testing solid construction", () => { - - - describe("empty solid", () => { - let solid: ISolid; - before(() => { - solid = new Solid(); - }); - it("should have no faces", () => { - solid.numFaces.should.equal(0); - Object.keys(solid.faces).length.should.equal(0); - }); - it("should have no solid", () => { - solid.numSolids.should.equal(0); - }); - it("should have no shell", () => { - solid.numShells.should.equal(0); - }); - it("should have no outerShell", () => { - should.not.exist(solid.outerShell); - }); - }); - describe("makeBox with 2 points", () => { - let solid: ISolid; - before(() => { - solid = occ.makeBox([10, 20, 30], [20, 40, 60]); - }); - it("should be a SOLID", () => { - solid.shapeType.should.equal("SOLID"); - }); - it("should have 1 solid", () => { - solid.numSolids.should.equal(1); - }); - it("should have 1 shell", () => { - solid.numShells.should.equal(1); - }); - it("should have 6 faces", () => { - solid.numFaces.should.equal(6); - Object.keys(solid.faces).length.should.equal(6); - }); - it("should have an outerShell with 6 faces", () => { - should.exist(solid.getOuterShell()); - solid.getOuterShell().numFaces.should.equal(6); - // Object.keys(solid.getOuterShell().faces).length.should.equal(6); - }); - it("should have an outerShell with a forward orientation", () => { - solid.getOuterShell().orientation.should.equal("FORWARD"); - }); - it("should have (20-10)*(40-20)*(60-30) as a volume", () => { - solid.volume.should.equal((20 - 10) * (40 - 20) * (60 - 30)); - }); - - it("should have ~ 2*((20-10)*(40-20)+(20-10)*(60-30)+(40-20)*(60-30)) as a area", () => { - let expectedArea = 2 * ((20 - 10) * (40 - 20) + (20 - 10) * (60 - 30) + (40 - 20) * (60 - 30)); - let eps = 0.001; - solid.area.should.be.within(expectedArea - eps, expectedArea + eps); - }); - - it("should have the sum(face area) === area of solid ", () => { - - let epsilon = 1E-3; - - let shapeIt = new FaceIterator(solid); - let cumulated_face_area = 0; - while (shapeIt.more) { - cumulated_face_area += shapeIt.next().area; - } - let expectedArea = solid.area; - cumulated_face_area.should.be.within(expectedArea - epsilon, expectedArea + epsilon); - }); - }); - describe("makeBox with invalid arguments", () => { - - it("should raise an exception when invalid arguments are passed to makeBox", () => { - (function failing_func() { - let solid = (occ.makeBox as any)([10, 20, 30], 10, 10, 10); - }).should.throwError(); - }); - }); - describe("fuse 2 overlapping boxes", () => { - let solid1: ISolid; - let solid2: ISolid; - before(() => { - solid1 = occ.makeBox([10, 20, 30], [20, 40, 60]); - solid2 = occ.makeBox([15, 25, 35], [-20, -40, -60]); - solid1 = occ.fuse(solid1, solid2); - }); - it("should be a SOLID", () => { - solid1.shapeType.should.equal("SOLID"); - }); - it("should have 1 solid", () => { - solid1.numSolids.should.equal(1); - }); - it("should have 1 shell", () => { - solid1.numShells.should.equal(1); - }); - it("should have 12 faces", () => { - solid1.numFaces.should.equal(12); - Object.keys(solid1.faces).length.should.equal(12); - }); - it("should have an outerShell with 12 faces", () => { - should.exist(solid1.getOuterShell()); - solid1.getOuterShell().numFaces.should.equal(12); - }); - }); - describe("cut a corner of a box", () => { - let solid1: ISolid; - let solid2: ISolid; - before(() => { - // - // +------+ - // +-----|`. `. +------+ - // |`. + `+-----`+ |`+--+ `. - // | `+--` | | | | |`+--`+ - // | | + | | => | +--+ | | - // + | ` +------+ + `+-`+ | - // `. | | `. | | - // `+------+ `+------+ - solid1 = occ.makeBox([10, 20, 30], [20, 40, 60]); - solid2 = occ.makeBox([15, 25, 35], [-20, -40, -60]); - solid1 = occ.cut(solid1, solid2); - }); - it("should be a SOLID", () => { - solid1.shapeType.should.equal("SOLID"); - }); - it("should have 9 faces", () => { - solid1.numFaces.should.equal(9); - Object.keys(solid1.faces).length.should.equal(9); - }); - it("should have 1 solid", () => { - solid1.numSolids.should.equal(1); - }); - it("should have 1 shell", () => { - solid1.numShells.should.equal(1); - }); - - - }); - describe("Hollow box ( 1 solid with 2 shells )", () => { - let solid1: ISolid; - let solid2: ISolid; - before(() => { - solid1 = occ.makeBox([0, 0, 0], [20, 20, 20]); - solid2 = occ.makeBox([10, 10, 10], [15, 15, 15]); - - solid1 = occ.cut(solid1, solid2); - - }); - it("should be a SOLID", () => { - solid1.shapeType.should.equal("SOLID"); - }); - it("should have 12 faces", () => { - solid1.numFaces.should.equal(12); - Object.keys(solid1.faces).length.should.equal(12); - - }); - it("should have 1 solid", () => { - solid1.numSolids.should.equal(1); - }); - it("should have 2 shells", () => { - solid1.numShells.should.equal(2); - }); - it("should have an outer shell with 6 faces", () => { - let outerShell = solid1.getOuterShell(); - outerShell.numFaces.should.equal(6); - }); - it("should have an outer shell with 6 faces", () => { - let outerShell = solid1.getOuterShell(); - outerShell.orientation.should.equal("FORWARD"); - }); - it("should expose 2 shells (getOuterShell)", () => { - - let shells = solid1.getShells(); - - let outerShell = solid1.getOuterShell(); - should.exist(outerShell); - - shells.length.should.equal(2); - for (let i in shells) { - let shell = shells[i]; - if (outerShell.hashCode !== shell.hashCode) { - shell.orientation.should.equal("FORWARD"); - } - } - }); - }); - describe("split boxes", () => { - - let solid1: ISolid; - let solid2: ISolid; - let splitBoxes: ISolid; - before(() => { - // cutting a square box in 2 boxes - solid1 = occ.makeBox([0, 0, 0], [20, 20, 20]); - solid2 = occ.makeBox([-100, -100, 10], [100, 100, 15]); - - splitBoxes = occ.cut(solid1, solid2); - - }); - it("should be a COMPOUND", () => { - splitBoxes.shapeType.should.equal("COMPOUND"); - }); - it("should have 12 faces", () => { - //console.log( splitBoxes.faces); - splitBoxes.numFaces.should.equal(12); - Object.keys(splitBoxes.faces).length.should.equal(12); - }); - it("should have 2 solids", () => { - splitBoxes.numSolids.should.equal(2); - }); - it("should have 2 shells", () => { - splitBoxes.numShells.should.equal(2); - }); - it("should have an outer shell with 6 faces", () => { - let solids = splitBoxes.getSolids(); - - let outerShell1 = solids[0].getOuterShell(); - outerShell1.numFaces.should.equal(6); - - let outerShell2 = solids[1].getOuterShell(); - outerShell2.numFaces.should.equal(6); - - }); - - }); - - describe("creating a compound", () => { - let compound: ISolid; - before(() => { - let solids: ISolid[] = []; - let solid1 = occ.makeBox(10, 20, 30); - for (let i = 0; i < 10; i++) { - let s = solid1.rotate([0, 0, 0], [0, 0, 1], i * 15); - s.numFaces.should.equal(6); - solids.push(s); - } - compound = occ.compound(solids); - }); - - it("should be a compound", () => { - compound.shapeType.should.equal("COMPOUND"); - }); - it("should have 10 solids", () => { - console.log(compound); - compound.numSolids.should.equal(10); - }); - }); - - describe("Meshing a simple solid", () => { - describe("Meshing a box", () => { - let solid: ISolid; - before(() => { - solid = occ.makeBox([10, 20, 30], [20, 30, 40]); - }); - it("should have a mesh with 8 vertices", () => { - solid.mesh.numVertices.should.equal(8); - }); - it("should have a mesh with 4+4+4=12 edges", () => { - solid.mesh.numEdges.should.equal(12); - }); - it("should have a mesh with 2*6 triangles", () => { - solid.mesh.numTriangles.should.equal(12); - }); - }); - }); - describe("Testing Shape __prototype", () => { - let solid: ISolid; - before(() => { - solid = occ.makeBox([10, 20, 30], [20, 30, 40]); - }); - it("should expose the expected properties ", () => { - let expected = ["shapeType", "numFaces", "isNull", "isValid"]; - let actual: string[] = []; - for (let j in Solid.prototype) { - actual.push(j.toString()); - } - let missing: string[] = []; - for (const e of expected) { - if (actual.indexOf(e) == -1) { - missing.push(e); - } - } - missing.should.have.lengthOf(0); - - }); - }); - describe("exporting a solid to STEP ", () => { - - let step_filename1 = "toto1.step"; - let step_filename2 = "toto2.step"; - let solid1: ISolid, solid2: ISolid; - before(() => { - solid1 = occ.makeBox([10, 20, 30], [20, 30, 40]); - solid1 = occ.makeBox([20, 30, 50], [110, 40, 0]); - }); - it("should export a single solid to STEP", () => { - occ.writeSTEP(step_filename1, solid1); - }); - it("should export many solids to STEP", () => { - occ.writeSTEP(step_filename2, solid1, solid2); - }); - }); - describe("testing ShapeIterator on solid", () => { - let solid: ISolid; - let shapeIt: IShapeIterator | IShapeIterator | IShapeIterator; - before(() => { - solid = occ.makeBox([10, 20, 30], [20, 40, 60]); - }); - it("should iterate on 6 faces", () => { - - shapeIt = new FaceIterator(solid); - shapeIt.more.should.be.equal(true); - should.not.exist(shapeIt.current); - let counter = 0; - while (shapeIt.more) { - shapeIt.more.should.be.equal(true); - shapeIt.next(); - should.exists(shapeIt.current); - counter += 1; - } - counter.should.equal(6); - shapeIt.more.should.be.equal(false); - should.exists(shapeIt.current); - - }); - it("should iterate on 24 edges ( 4 on each of the 6 faces", () => { - shapeIt = new EdgeIterator(solid); - shapeIt.more.should.be.equal(true); - should.not.exist(shapeIt.current); - let counter = 0; - while (shapeIt.more) { - shapeIt.more.should.be.equal(true); - shapeIt.next(); - should.exists(shapeIt.current); - counter += 1; - } - counter.should.equal(24); - shapeIt.more.should.be.equal(false); - should.exists(shapeIt.current); - - }); - - }); - describe("testing fillet on a box..", () => { - - let solid: ISolid; - - before(() => { - solid = occ.makeBox([10, 20, 30], [30, 40, 50]); - solid.numFaces.should.equal(6); - - solid = occ.makeFillet(solid, solid.getEdges(), 2.0); - - }); - it("should be possible to round the corner...", () => { - - // 6 flat surfaces -> 6*4 edges - // + 12 rounded corners -> shared - // + 8 corners -> 8*3 edges - //==> 26 faces - solid.numFaces.should.be.equal(26); - solid.getEdges().length.should.be.equal(6 * 4 + 8 * 3); - - }); - - }); - describe("makeCylinder (variation 1)", () => { - let solid: ISolid; - before(() => { - let radius = 50; - let height = 100; - solid = occ.makeCylinder(radius, height); - }); - it("should have 3 faces", () => { - solid.numFaces.should.equal(3); - }); - }); - - describe("makeCylinder (variation 2)", () => { - let solid: ISolid; - before(() => { - let position: [Triplet, Triplet] = [[0, 0, 1], [0, 1, 0]]; - let radius = 50; - let height = 100; - solid = occ.makeCylinder(position, radius, height); - }); - it("should have 3 faces", () => { - solid.numFaces.should.equal(3); - }); - }); - describe("makeCylinder (variation 3 : with 2 points and a radius)", () => { - let solid: ISolid; - let bbox: IBoundingBox; - before(() => { - let startPoint: Triplet = [-100, 20, 40]; - let endPoint: Triplet = [100, 20, 40]; - let radius = 20; - solid = occ.makeCylinder(startPoint, endPoint, radius); - bbox = solid.getBoundingBox(); - - }); - it("should have 3 faces", () => { - solid.numFaces.should.equal(3); - }); - it("should have a bounding box that includes X=-100,20,40", () => { - bbox.nearPt.y.should.be.within(-2, 0); - bbox.farPt.y.should.be.within(40, 42); - - bbox.nearPt.x.should.be.within(-101, -100); - bbox.farPt.x.should.be.within(100, 101); - }); - }); - describe("makeCone - variation 1", () => { - let solid: ISolid; - before(() => { - let radius1 = 50; - let radius2 = 70; - let height = 30; - solid = occ.makeCone(radius1, radius2, height); - }); - it("should have 3 faces", () => { - solid.numFaces.should.equal(3); - }); - }); - describe("makeCone - variation 2 ( point,R1, point, R2 )", () => { - let solid: ISolid; - let radius1 = 50; - let radius2 = 70; - before(() => { - let height = 30; - solid = occ.makeCone([0, 0, 0], radius1, [0, 0, height], radius2); - }); - it("should have 3 faces", () => { - solid.numFaces.should.equal(3); - should.exist(solid.faces.top); - should.exist(solid.faces.lateral); - should.exist(solid.faces.bottom); - }); - it("top face should have a area of radius**2*pi", () => { - let expectedArea = radius2 * radius2 * Math.PI; - let eps = 1.0; - solid.faces.top.area.should.be.within(expectedArea - eps, expectedArea + eps); - }); - it("bottom face should have a area of radius**2*pi", () => { - let expectedArea = radius1 * radius1 * Math.PI; - let eps = 1.0; - solid.faces.bottom.area.should.be.within(expectedArea - eps, expectedArea + eps); - }); - }); - describe("makeCone - variation 3 ( axpex,dir, half_angle, height )", () => { - let solid: ISolid; - let radius = 50; - let height = 30; - before(() => { - let angle = Math.atan(radius / height); - solid = occ.makeCone([0, 0, 0], [0, 0, 1], angle, height); - }); - it("should have 2 faces", () => { - solid.numFaces.should.equal(2); - should.exist(solid.faces.top); - should.exist(solid.faces.lateral); - should.not.exist(solid.faces.bottom); - }); - it("top face should have a area of radius**2*pi", () => { - let expectedArea = radius * radius * Math.PI; - let eps = 1.0; - solid.faces.top.area.should.be.within(expectedArea - eps, expectedArea + eps); - }); - - }); - - describe("makeSphere", () => { - let solid: ISolid; - let radius = 10; - let epsilon = radius * 1E-1; - before(() => { - let center: Triplet = [10, 20, 30]; - solid = occ.makeSphere(center, radius); - }); - it("should have 1 face and one egde", () => { - solid.numFaces.should.equal(1); - solid.getEdges().length.should.equal(1); - let edges = solid.getEdges(); - for (let edge in edges) { - // todo : do some investigation - } - }); - it("should have a area of 4*Pi*R", () => { - let expected_area = 4 * 3.14159265 * radius * radius; - solid.area.should.be.within(expected_area - epsilon, expected_area + epsilon); - }); - it("should have a volume of 4/3*Pi*R*2", () => { - - let expected_volume = 4.0 / 3.0 * 3.14159265 * radius * radius * radius; - solid.volume.should.be.within(expected_volume - epsilon, expected_volume + epsilon); - - }); - }); - describe("makeTorus", () => { - let solid: ISolid; - before(() => { - solid = occ.makeTorus([0, 0, 0], [0, 0, 1], 100, 10); - }); - it("should have one single face", () => { - //console.log(solid.faces); - solid.numFaces.should.equal(1); - should.exist(solid.faces.lateral); - }); - - }); - describe("makeTorus with invalid arguments", () => { - - it("should not crash if torus is created with invalid arguments", () => { - should(() => { - const solid = occ.makeTorus([0, 0, 0], [0, 0, 0], 10, 100); - }).throwError(); - }); - }); - describe("makeCylinder with invalid arguments", () => { - - it("should not crash if Cylinder is created with invalid arguments", () => { - should(() => { - const solid = occ.makeCylinder([0, 0, 0], [0, 0, 0], 10); - }).throwError(); - }); - }); - - describe("makeCone with invalid arguments", () => { - - it("should not crash if Cone is created with invalid arguments", () => { - should(() => { - const solid = (occ.makeCone as any)([0, 0, 0], [0, 0, 0], 10); - }).throwError(); - - should(() => { - const solid = (occ.makeCone as any)([0, 0, 0], [0, 0, 0], 0); - }).throwError(); - }); - }); - - - describe("rotate apply on a solid", () => { - let solid: ISolid; - before(() => { - solid = occ.makeBox([10, 10, 0], [20, 20, 10]); - - }); - it("should expose a rotated box", () => { - - let epsilon = 0.1; - let bbox = solid.getBoundingBox(); - bbox.farPt.x.should.be.lessThan(20.0 + epsilon); - bbox.farPt.y.should.be.lessThan(20.0 + epsilon); - bbox.nearPt.x.should.be.greaterThan(10.0 - epsilon); - bbox.nearPt.y.should.be.greaterThan(10.0 - epsilon); - - solid = solid.rotate([0, 0, 0], [0, 0, 1], 90); - - bbox = solid.getBoundingBox(); - bbox.farPt.x.should.be.within(-10.0 - epsilon, -10 + epsilon); - bbox.farPt.y.should.be.within(20.0 - epsilon, 20 + epsilon); - bbox.nearPt.x.should.be.within(-20.0 - epsilon, -20 + epsilon); - bbox.nearPt.y.should.be.within(10.0 - epsilon, 10 + epsilon); - - }); - }); - describe(" making a illegal solid ( with bad arguments) shall raise exception", () => { - it("should raise exception when trying to build a box with illegal arguments", () => { - (() => { - let solid = (occ.makeBox as any)("illegal"); - }).should.throwError(); - - }); - }); - describe("test adjacent faces", () => { - let solid: ISolid; - before(() => { - solid = occ.makeBox([0, 0, 0], [100, 100, 100]); - }); - it("should have back/front/left/right faces adjacent to face 'top'", () => { - let adjFaces = solid.getAdjacentFaces(solid.faces.top); - - adjFaces.length.should.equal(4); - - let names = adjFaces.map(function (f) { - return solid.getShapeName(f); - }).sort(); - - names.join("/").should.equal("back/front/left/right"); - - }); - it("should have back/front/left/right faces adjacent to face 'bottom'", () => { - - let adjFaces = solid.getAdjacentFaces(solid.faces.bottom); - - adjFaces.length.should.equal(4); - - let names = adjFaces.map(function (f) { - return solid.getShapeName(f); - }).sort(); - - names.join("/").should.equal("back/front/left/right"); - }); - - it("should have bottom/left/right/top faces adjacent to face 'back'", () => { - - let adjFaces = solid.getAdjacentFaces(solid.faces.back); - - adjFaces.length.should.equal(4); - - let names = adjFaces.map(function (f) { - return solid.getShapeName(f); - }).sort(); - - names.join("/").should.equal("bottom/left/right/top"); - }); - - it("should have bottom/left/right/top faces adjacent to face 'front'", () => { - - let adjFaces = solid.getAdjacentFaces(solid.faces.front); - - adjFaces.length.should.equal(4); - - let names = adjFaces.map(function (f) { - return solid.getShapeName(f); - }).sort(); - - names.join("/").should.equal("bottom/left/right/top"); - }); - it("should have back/bottom/front/top faces adjacent to face 'left'", () => { - - let adjFaces = solid.getAdjacentFaces(solid.faces.left); - - adjFaces.length.should.equal(4); - - let names = adjFaces.map(function (f) { - return solid.getShapeName(f); - }).sort(); - - names.join("/").should.equal("back/bottom/front/top"); - }); - - it("should have back/bottom/front/top faces adjacent to face 'right'", () => { - - let adjFaces = solid.getAdjacentFaces(solid.faces.right); - - adjFaces.length.should.equal(4); - - let names = adjFaces.map(function (f) { - return solid.getShapeName(f); - }).sort(); - - names.join("/").should.equal("back/bottom/front/top"); - }); - }); - - describe("makeThickSolid (external ) on box", () => { - let initialBox: ISolid; - let thickSolid: ISolid; - before(() => { - initialBox = occ.makeBox(100, 200, 300); - thickSolid = occ.makeThickSolid(initialBox, initialBox.faces.top, 10); - }); - it("should have 23 (6 + 4 vertical faces + 4 vertical fillets + 1 horizontal face + 4 horizontal fillets + 4 rounded corners) faces", () => { - console.log(Object.keys(thickSolid.getFaces().map(function (el) { - return thickSolid.getShapeName(el); - })).join(" ")); - //xx console.log( Object.keys(thickSolid.faces).join(" ")); - initialBox.numFaces.should.equal(6); - thickSolid.numFaces.should.equal(23); - }); - }); - describe("makeThickSolid (internal) on box", () => { - let initialBox: ISolid; - let thickSolid: ISolid; - before(() => { - initialBox = occ.makeBox(100, 200, 300); - thickSolid = occ.makeThickSolid(initialBox, initialBox.faces.top, -10); - }); - it("should have 1 (1 top face modified + 5 old + 5 new) faces", () => { - console.log(Object.keys(thickSolid.getFaces().map(function (el) { - return thickSolid.getShapeName(el); - })).join(" ")); - //xx console.log( Object.keys(thickSolid.faces).join(" ")); - initialBox.numFaces.should.equal(6); - thickSolid.numFaces.should.equal(11); - }); - }); - describe("finding common edge of 2 faces", () => { - let box: ISolid; - before(() => { - box = occ.makeBox(100, 200, 300); - }); - it("should find a common edge between 'top' face and 'left' face", () => { - let edges = box.getCommonEdges(box.faces.top, box.faces.left); - edges.length.should.be.equal(1); - - }); - it("should not find a common edge between 'top' face and 'bottom' face", () => { - let edges = box.getCommonEdges(box.faces.top, box.faces.bottom); - edges.length.should.be.equal(0); - }); - }); - describe("makeDraftAngle", () => { - let box: ISolid; - let boxWithDraftFace: ISolid; - before(() => { - box = occ.makeBox(100, 200, 300); - boxWithDraftFace = occ.makeDraftAngle(box, box.faces.right, 20 * DEG2RAD, box.faces.bottom); - }); - it("should have 6 faces", () => { - boxWithDraftFace.numFaces.should.equal(6); - }); - it("should have a smaller volume", () => { - boxWithDraftFace.volume.should.be.lessThan(box.volume); - - }); - }); - - describe("makeDraftAngle on a box with a rounded corner", () => { - let box: ISolid; - let boxWithDraftFace: ISolid; - before(() => { - box = occ.makeBox(100, 200, 300); - let edges = box.getCommonEdges(box.faces.left, box.faces.front)[0]; - // console.log("edge = ",edges); - box = occ.makeFillet(box, edges, 10); - - // note: left , front , top and bottom faces have been modified by the fillet - // operation.; - - let faceToDraft = box.faces["mleft:0"]; - let neutralFace = box.faces["mbottom:0"]; - - console.log(Object.keys(box.faces).join(" ")); - should.exist(faceToDraft); - should.exist(neutralFace); - boxWithDraftFace = occ.makeDraftAngle(box, faceToDraft, 5 * DEG2RAD, neutralFace); - - }); - it("should have 7 faces", () => { - boxWithDraftFace.numFaces.should.equal(7); - }); - it("should have a smaller volume", () => { - boxWithDraftFace.volume.should.be.lessThan(box.volume); - - }); - }); + describe("empty solid", () => { + let solid: ISolid; + before(() => { + solid = new Solid(); + }); + it("should have no faces", () => { + solid.numFaces.should.equal(0); + Object.keys(solid.faces).length.should.equal(0); + }); + it("should have no solid", () => { + solid.numSolids.should.equal(0); + }); + it("should have no shell", () => { + solid.numShells.should.equal(0); + }); + it("should have no outerShell", () => { + should.not.exist(solid.outerShell); + }); + }); + describe("makeBox with 2 points", () => { + let solid: ISolid; + before(() => { + solid = occ.makeBox([10, 20, 30], [20, 40, 60]); + }); + it("should be a SOLID", () => { + solid.shapeType.should.equal("SOLID"); + }); + it("should have 1 solid", () => { + solid.numSolids.should.equal(1); + }); + it("should have 1 shell", () => { + solid.numShells.should.equal(1); + }); + it("should have 6 faces", () => { + solid.numFaces.should.equal(6); + Object.keys(solid.faces).length.should.equal(6); + }); + it("should have an outerShell with 6 faces", () => { + should.exist(solid.getOuterShell()); + solid.getOuterShell().numFaces.should.equal(6); + // Object.keys(solid.getOuterShell().faces).length.should.equal(6); + }); + it("should have an outerShell with a forward orientation", () => { + solid.getOuterShell().orientation.should.equal("FORWARD"); + }); + it("should have (20-10)*(40-20)*(60-30) as a volume", () => { + solid.volume.should.equal((20 - 10) * (40 - 20) * (60 - 30)); + }); + + it("should have ~ 2*((20-10)*(40-20)+(20-10)*(60-30)+(40-20)*(60-30)) as a area", () => { + let expectedArea = + 2 * + ((20 - 10) * (40 - 20) + (20 - 10) * (60 - 30) + (40 - 20) * (60 - 30)); + let eps = 0.001; + solid.area.should.be.within(expectedArea - eps, expectedArea + eps); + }); + + it("should have the sum(face area) === area of solid ", () => { + let epsilon = 1e-3; + + let shapeIt = new FaceIterator(solid); + let cumulated_face_area = 0; + while (shapeIt.more) { + cumulated_face_area += shapeIt.next().area; + } + let expectedArea = solid.area; + cumulated_face_area.should.be.within( + expectedArea - epsilon, + expectedArea + epsilon + ); + }); + }); + describe("makeBox with invalid arguments", () => { + it("should raise an exception when invalid arguments are passed to makeBox", () => { + (function failing_func() { + let solid = (occ.makeBox as any)([10, 20, 30], 10, 10, 10); + }).should.throwError(); + }); + }); + describe("fuse 2 overlapping boxes", () => { + let solid1: ISolid; + let solid2: ISolid; + before(() => { + solid1 = occ.makeBox([10, 20, 30], [20, 40, 60]); + solid2 = occ.makeBox([15, 25, 35], [-20, -40, -60]); + solid1 = occ.fuse(solid1, solid2); + }); + it("should be a SOLID", () => { + solid1.shapeType.should.equal("SOLID"); + }); + it("should have 1 solid", () => { + solid1.numSolids.should.equal(1); + }); + it("should have 1 shell", () => { + solid1.numShells.should.equal(1); + }); + it("should have 12 faces", () => { + solid1.numFaces.should.equal(12); + Object.keys(solid1.faces).length.should.equal(12); + }); + it("should have an outerShell with 12 faces", () => { + should.exist(solid1.getOuterShell()); + solid1.getOuterShell().numFaces.should.equal(12); + }); + }); + describe("cut a corner of a box", () => { + let solid1: ISolid; + let solid2: ISolid; + before(() => { + // + // +------+ + // +-----|`. `. +------+ + // |`. + `+-----`+ |`+--+ `. + // | `+--` | | | | |`+--`+ + // | | + | | => | +--+ | | + // + | ` +------+ + `+-`+ | + // `. | | `. | | + // `+------+ `+------+ + solid1 = occ.makeBox([10, 20, 30], [20, 40, 60]); + solid2 = occ.makeBox([15, 25, 35], [-20, -40, -60]); + solid1 = occ.cut(solid1, solid2); + }); + it("should be a SOLID", () => { + solid1.shapeType.should.equal("SOLID"); + }); + it("should have 9 faces", () => { + solid1.numFaces.should.equal(9); + Object.keys(solid1.faces).length.should.equal(9); + }); + it("should have 1 solid", () => { + solid1.numSolids.should.equal(1); + }); + it("should have 1 shell", () => { + solid1.numShells.should.equal(1); + }); + }); + describe("Hollow box ( 1 solid with 2 shells )", () => { + let solid1: ISolid; + let solid2: ISolid; + before(() => { + solid1 = occ.makeBox([0, 0, 0], [20, 20, 20]); + solid2 = occ.makeBox([10, 10, 10], [15, 15, 15]); + + solid1 = occ.cut(solid1, solid2); + }); + it("should be a SOLID", () => { + solid1.shapeType.should.equal("SOLID"); + }); + it("should have 12 faces", () => { + solid1.numFaces.should.equal(12); + Object.keys(solid1.faces).length.should.equal(12); + }); + it("should have 1 solid", () => { + solid1.numSolids.should.equal(1); + }); + it("should have 2 shells", () => { + solid1.numShells.should.equal(2); + }); + it("should have an outer shell with 6 faces", () => { + let outerShell = solid1.getOuterShell(); + outerShell.numFaces.should.equal(6); + }); + it("should have an outer shell with 6 faces", () => { + let outerShell = solid1.getOuterShell(); + outerShell.orientation.should.equal("FORWARD"); + }); + it("should expose 2 shells (getOuterShell)", () => { + let shells = solid1.getShells(); + + let outerShell = solid1.getOuterShell(); + should.exist(outerShell); + + shells.length.should.equal(2); + for (let i in shells) { + let shell = shells[i]; + if (outerShell.hashCode !== shell.hashCode) { + shell.orientation.should.equal("FORWARD"); + } + } + }); + }); + describe("split boxes", () => { + let solid1: ISolid; + let solid2: ISolid; + let splitBoxes: ISolid; + before(() => { + // cutting a square box in 2 boxes + solid1 = occ.makeBox([0, 0, 0], [20, 20, 20]); + solid2 = occ.makeBox([-100, -100, 10], [100, 100, 15]); + + splitBoxes = occ.cut(solid1, solid2); + }); + it("should be a COMPOUND", () => { + splitBoxes.shapeType.should.equal("COMPOUND"); + }); + it("should have 12 faces", () => { + //console.log( splitBoxes.faces); + splitBoxes.numFaces.should.equal(12); + Object.keys(splitBoxes.faces).length.should.equal(12); + }); + it("should have 2 solids", () => { + splitBoxes.numSolids.should.equal(2); + }); + it("should have 2 shells", () => { + splitBoxes.numShells.should.equal(2); + }); + it("should have an outer shell with 6 faces", () => { + let solids = splitBoxes.getSolids(); + + let outerShell1 = solids[0].getOuterShell(); + outerShell1.numFaces.should.equal(6); + + let outerShell2 = solids[1].getOuterShell(); + outerShell2.numFaces.should.equal(6); + }); + }); + + describe("creating a compound", () => { + let compound: ISolid; + before(() => { + let solids: ISolid[] = []; + let solid1 = occ.makeBox(10, 20, 30); + for (let i = 0; i < 10; i++) { + let s = solid1.rotate([0, 0, 0], [0, 0, 1], i * 15); + s.numFaces.should.equal(6); + solids.push(s); + } + compound = occ.compound(solids); + }); + + it("should be a compound", () => { + compound.shapeType.should.equal("COMPOUND"); + }); + it("should have 10 solids", () => { + console.log(compound); + compound.numSolids.should.equal(10); + }); + }); + + describe("Meshing a simple solid", () => { + describe("Meshing a box", () => { + let solid: ISolid; + before(() => { + solid = occ.makeBox([10, 20, 30], [20, 30, 40]); + }); + it("should have a mesh with 8 vertices", () => { + solid.mesh.numVertices.should.equal(8); + }); + it("should have a mesh with 4+4+4=12 edges", () => { + solid.mesh.numEdges.should.equal(12); + }); + it("should have a mesh with 2*6 triangles", () => { + solid.mesh.numTriangles.should.equal(12); + }); + }); + }); + describe("Testing Shape __prototype", () => { + let solid: ISolid; + before(() => { + solid = occ.makeBox([10, 20, 30], [20, 30, 40]); + }); + it("should expose the expected properties ", () => { + let expected = ["shapeType", "numFaces", "isNull", "isValid"]; + let actual: string[] = []; + for (let j in Solid.prototype) { + actual.push(j.toString()); + } + let missing: string[] = []; + for (const e of expected) { + if (actual.indexOf(e) == -1) { + missing.push(e); + } + } + missing.should.have.lengthOf(0); + }); + }); + describe("exporting a solid to STEP ", () => { + let step_filename1 = "toto1.step"; + let step_filename2 = "toto2.step"; + let solid1: ISolid, solid2: ISolid; + before(() => { + solid1 = occ.makeBox([10, 20, 30], [20, 30, 40]); + solid1 = occ.makeBox([20, 30, 50], [110, 40, 0]); + }); + it("should export a single solid to STEP", () => { + occ.writeSTEP(step_filename1, solid1); + }); + it("should export many solids to STEP", () => { + occ.writeSTEP(step_filename2, solid1, solid2); + }); + }); + describe("testing ShapeIterator on solid", () => { + let solid: ISolid; + let shapeIt: + | IShapeIterator + | IShapeIterator + | IShapeIterator; + before(() => { + solid = occ.makeBox([10, 20, 30], [20, 40, 60]); + }); + it("should iterate on 6 faces", () => { + shapeIt = new FaceIterator(solid); + shapeIt.more.should.be.equal(true); + should.not.exist(shapeIt.current); + let counter = 0; + while (shapeIt.more) { + shapeIt.more.should.be.equal(true); + shapeIt.next(); + should.exists(shapeIt.current); + counter += 1; + } + counter.should.equal(6); + shapeIt.more.should.be.equal(false); + should.exists(shapeIt.current); + }); + it("should iterate on 24 edges ( 4 on each of the 6 faces", () => { + shapeIt = new EdgeIterator(solid); + shapeIt.more.should.be.equal(true); + should.not.exist(shapeIt.current); + let counter = 0; + while (shapeIt.more) { + shapeIt.more.should.be.equal(true); + shapeIt.next(); + should.exists(shapeIt.current); + counter += 1; + } + counter.should.equal(24); + shapeIt.more.should.be.equal(false); + should.exists(shapeIt.current); + }); + }); + describe("testing fillet on a box..", () => { + let solid: ISolid; + + before(() => { + solid = occ.makeBox([10, 20, 30], [30, 40, 50]); + solid.numFaces.should.equal(6); + + solid = occ.makeFillet(solid, solid.getEdges(), 2.0); + }); + it("should be possible to round the corner...", () => { + // 6 flat surfaces -> 6*4 edges + // + 12 rounded corners -> shared + // + 8 corners -> 8*3 edges + //==> 26 faces + solid.numFaces.should.be.equal(26); + solid.getEdges().length.should.be.equal(6 * 4 + 8 * 3); + }); + }); + describe("makeCylinder (variation 1)", () => { + let solid: ISolid; + before(() => { + let radius = 50; + let height = 100; + solid = occ.makeCylinder(radius, height); + }); + it("should have 3 faces", () => { + solid.numFaces.should.equal(3); + }); + }); + + describe("makeCylinder (variation 2)", () => { + let solid: ISolid; + before(() => { + let position: [Triplet, Triplet] = [ + [0, 0, 1], + [0, 1, 0], + ]; + let radius = 50; + let height = 100; + solid = occ.makeCylinder(position, radius, height); + }); + it("should have 3 faces", () => { + solid.numFaces.should.equal(3); + }); + }); + describe("makeCylinder (variation 3 : with 2 points and a radius)", () => { + let solid: ISolid; + let bbox: IBoundingBox; + before(() => { + let startPoint: Triplet = [-100, 20, 40]; + let endPoint: Triplet = [100, 20, 40]; + let radius = 20; + solid = occ.makeCylinder(startPoint, endPoint, radius); + bbox = solid.getBoundingBox(); + }); + it("should have 3 faces", () => { + solid.numFaces.should.equal(3); + }); + it("should have a bounding box that includes X=-100,20,40", () => { + bbox.nearPt.y.should.be.within(-2, 0); + bbox.farPt.y.should.be.within(40, 42); + + bbox.nearPt.x.should.be.within(-101, -100); + bbox.farPt.x.should.be.within(100, 101); + }); + }); + describe("makeCone - variation 1", () => { + let solid: ISolid; + before(() => { + let radius1 = 50; + let radius2 = 70; + let height = 30; + solid = occ.makeCone(radius1, radius2, height); + }); + it("should have 3 faces", () => { + solid.numFaces.should.equal(3); + }); + }); + describe("makeCone - variation 2 ( point,R1, point, R2 )", () => { + let solid: ISolid; + let radius1 = 50; + let radius2 = 70; + before(() => { + let height = 30; + solid = occ.makeCone([0, 0, 0], radius1, [0, 0, height], radius2); + }); + it("should have 3 faces", () => { + solid.numFaces.should.equal(3); + should.exist(solid.faces.top); + should.exist(solid.faces.lateral); + should.exist(solid.faces.bottom); + }); + it("top face should have a area of radius**2*pi", () => { + let expectedArea = radius2 * radius2 * Math.PI; + let eps = 1.0; + solid.faces.top.area.should.be.within( + expectedArea - eps, + expectedArea + eps + ); + }); + it("bottom face should have a area of radius**2*pi", () => { + let expectedArea = radius1 * radius1 * Math.PI; + let eps = 1.0; + solid.faces.bottom.area.should.be.within( + expectedArea - eps, + expectedArea + eps + ); + }); + }); + describe("makeCone - variation 3 ( axpex,dir, half_angle, height )", () => { + let solid: ISolid; + let radius = 50; + let height = 30; + before(() => { + let angle = Math.atan(radius / height); + solid = occ.makeCone([0, 0, 0], [0, 0, 1], angle, height); + }); + it("should have 2 faces", () => { + solid.numFaces.should.equal(2); + should.exist(solid.faces.top); + should.exist(solid.faces.lateral); + should.not.exist(solid.faces.bottom); + }); + it("top face should have a area of radius**2*pi", () => { + let expectedArea = radius * radius * Math.PI; + let eps = 1.0; + solid.faces.top.area.should.be.within( + expectedArea - eps, + expectedArea + eps + ); + }); + }); + + describe("makeSphere", () => { + let solid: ISolid; + let radius = 10; + let epsilon = radius * 1e-1; + before(() => { + let center: Triplet = [10, 20, 30]; + solid = occ.makeSphere(center, radius); + }); + it("should have 1 face and one egde", () => { + solid.numFaces.should.equal(1); + solid.getEdges().length.should.equal(1); + let edges = solid.getEdges(); + for (let edge in edges) { + // todo : do some investigation + } + }); + it("should have a area of 4*Pi*R", () => { + let expected_area = 4 * 3.14159265 * radius * radius; + solid.area.should.be.within( + expected_area - epsilon, + expected_area + epsilon + ); + }); + it("should have a volume of 4/3*Pi*R*2", () => { + let expected_volume = (4.0 / 3.0) * 3.14159265 * radius * radius * radius; + solid.volume.should.be.within( + expected_volume - epsilon, + expected_volume + epsilon + ); + }); + }); + describe("makeTorus", () => { + let solid: ISolid; + before(() => { + solid = occ.makeTorus([0, 0, 0], [0, 0, 1], 100, 10); + }); + it("should have one single face", () => { + //console.log(solid.faces); + solid.numFaces.should.equal(1); + should.exist(solid.faces.lateral); + }); + }); + describe("makeTorus with invalid arguments", () => { + it("should not crash if torus is created with invalid arguments", () => { + should(() => { + const solid = occ.makeTorus([0, 0, 0], [0, 0, 0], 10, 100); + }).throwError(); + }); + }); + describe("makeCylinder with invalid arguments", () => { + it("should not crash if Cylinder is created with invalid arguments", () => { + should(() => { + const solid = occ.makeCylinder([0, 0, 0], [0, 0, 0], 10); + }).throwError(); + }); + }); + + describe("makeCone with invalid arguments", () => { + it("should not crash if Cone is created with invalid arguments", () => { + should(() => { + const solid = (occ.makeCone as any)([0, 0, 0], [0, 0, 0], 10); + }).throwError(); + + should(() => { + const solid = (occ.makeCone as any)([0, 0, 0], [0, 0, 0], 0); + }).throwError(); + }); + }); + + describe("rotate apply on a solid", () => { + let solid: ISolid; + before(() => { + solid = occ.makeBox([10, 10, 0], [20, 20, 10]); + }); + it("should expose a rotated box", () => { + let epsilon = 0.1; + let bbox = solid.getBoundingBox(); + bbox.farPt.x.should.be.lessThan(20.0 + epsilon); + bbox.farPt.y.should.be.lessThan(20.0 + epsilon); + bbox.nearPt.x.should.be.greaterThan(10.0 - epsilon); + bbox.nearPt.y.should.be.greaterThan(10.0 - epsilon); + + solid = solid.rotate([0, 0, 0], [0, 0, 1], 90); + + bbox = solid.getBoundingBox(); + bbox.farPt.x.should.be.within(-10.0 - epsilon, -10 + epsilon); + bbox.farPt.y.should.be.within(20.0 - epsilon, 20 + epsilon); + bbox.nearPt.x.should.be.within(-20.0 - epsilon, -20 + epsilon); + bbox.nearPt.y.should.be.within(10.0 - epsilon, 10 + epsilon); + }); + }); + describe(" making a illegal solid ( with bad arguments) shall raise exception", () => { + it("should raise exception when trying to build a box with illegal arguments", () => { + (() => { + let solid = (occ.makeBox as any)("illegal"); + }).should.throwError(); + }); + }); + describe("test adjacent faces", () => { + let solid: ISolid; + before(() => { + solid = occ.makeBox([0, 0, 0], [100, 100, 100]); + }); + it("should have back/front/left/right faces adjacent to face 'top'", () => { + let adjFaces = solid.getAdjacentFaces(solid.faces.top); + + adjFaces.length.should.equal(4); + + let names = adjFaces + .map(function (f) { + return solid.getShapeName(f); + }) + .sort(); + + names.join("/").should.equal("back/front/left/right"); + }); + it("should have back/front/left/right faces adjacent to face 'bottom'", () => { + let adjFaces = solid.getAdjacentFaces(solid.faces.bottom); + + adjFaces.length.should.equal(4); + + let names = adjFaces + .map(function (f) { + return solid.getShapeName(f); + }) + .sort(); + + names.join("/").should.equal("back/front/left/right"); + }); + + it("should have bottom/left/right/top faces adjacent to face 'back'", () => { + let adjFaces = solid.getAdjacentFaces(solid.faces.back); + + adjFaces.length.should.equal(4); + + let names = adjFaces + .map(function (f) { + return solid.getShapeName(f); + }) + .sort(); + + names.join("/").should.equal("bottom/left/right/top"); + }); + + it("should have bottom/left/right/top faces adjacent to face 'front'", () => { + let adjFaces = solid.getAdjacentFaces(solid.faces.front); + + adjFaces.length.should.equal(4); + + let names = adjFaces + .map(function (f) { + return solid.getShapeName(f); + }) + .sort(); + + names.join("/").should.equal("bottom/left/right/top"); + }); + it("should have back/bottom/front/top faces adjacent to face 'left'", () => { + let adjFaces = solid.getAdjacentFaces(solid.faces.left); + + adjFaces.length.should.equal(4); + + let names = adjFaces + .map(function (f) { + return solid.getShapeName(f); + }) + .sort(); + + names.join("/").should.equal("back/bottom/front/top"); + }); + + it("should have back/bottom/front/top faces adjacent to face 'right'", () => { + let adjFaces = solid.getAdjacentFaces(solid.faces.right); + + adjFaces.length.should.equal(4); + + let names = adjFaces + .map(function (f) { + return solid.getShapeName(f); + }) + .sort(); + + names.join("/").should.equal("back/bottom/front/top"); + }); + }); + + describe("makeThickSolid (external ) on box", () => { + let initialBox: ISolid; + let thickSolid: ISolid; + before(() => { + initialBox = occ.makeBox(100, 200, 300); + thickSolid = occ.makeThickSolid(initialBox, initialBox.faces.top, 10); + }); + it("should have 23 (6 + 4 vertical faces + 4 vertical fillets + 1 horizontal face + 4 horizontal fillets + 4 rounded corners) faces", () => { + console.log( + Object.keys( + thickSolid.getFaces().map(function (el) { + return thickSolid.getShapeName(el); + }) + ).join(" ") + ); + //xx console.log( Object.keys(thickSolid.faces).join(" ")); + initialBox.numFaces.should.equal(6); + thickSolid.numFaces.should.equal(23); + }); + }); + describe("makeThickSolid (internal) on box", () => { + let initialBox: ISolid; + let thickSolid: ISolid; + before(() => { + initialBox = occ.makeBox(100, 200, 300); + thickSolid = occ.makeThickSolid(initialBox, initialBox.faces.top, -10); + }); + it("should have 1 (1 top face modified + 5 old + 5 new) faces", () => { + console.log( + Object.keys( + thickSolid.getFaces().map(function (el) { + return thickSolid.getShapeName(el); + }) + ).join(" ") + ); + //xx console.log( Object.keys(thickSolid.faces).join(" ")); + initialBox.numFaces.should.equal(6); + thickSolid.numFaces.should.equal(11); + }); + }); + describe("finding common edge of 2 faces", () => { + let box: ISolid; + before(() => { + box = occ.makeBox(100, 200, 300); + }); + it("should find a common edge between 'top' face and 'left' face", () => { + let edges = box.getCommonEdges(box.faces.top, box.faces.left); + edges.length.should.be.equal(1); + }); + it("should not find a common edge between 'top' face and 'bottom' face", () => { + let edges = box.getCommonEdges(box.faces.top, box.faces.bottom); + edges.length.should.be.equal(0); + }); + }); + describe("makeDraftAngle", () => { + let box: ISolid; + let boxWithDraftFace: ISolid; + before(() => { + box = occ.makeBox(100, 200, 300); + boxWithDraftFace = occ.makeDraftAngle( + box, + box.faces.right, + 20 * DEG2RAD, + box.faces.bottom + ); + }); + it("should have 6 faces", () => { + boxWithDraftFace.numFaces.should.equal(6); + }); + it("should have a smaller volume", () => { + boxWithDraftFace.volume.should.be.lessThan(box.volume); + }); + }); + + describe("makeDraftAngle on a box with a rounded corner", () => { + let box: ISolid; + let boxWithDraftFace: ISolid; + before(() => { + box = occ.makeBox(100, 200, 300); + let edges = box.getCommonEdges(box.faces.left, box.faces.front)[0]; + // console.log("edge = ",edges); + box = occ.makeFillet(box, edges, 10); + + // note: left , front , top and bottom faces have been modified by the fillet + // operation.; + + let faceToDraft = box.faces["mleft:0"]; + let neutralFace = box.faces["mbottom:0"]; + + console.log(Object.keys(box.faces).join(" ")); + should.exist(faceToDraft); + should.exist(neutralFace); + boxWithDraftFace = occ.makeDraftAngle( + box, + faceToDraft, + 5 * DEG2RAD, + neutralFace + ); + }); + it("should have 7 faces", () => { + boxWithDraftFace.numFaces.should.equal(7); + }); + it("should have a smaller volume", () => { + boxWithDraftFace.volume.should.be.lessThan(box.volume); + }); + }); }); diff --git a/test/test_transform.ts b/test/test_transform.ts index fbc3654..c44a8b9 100644 --- a/test/test_transform.ts +++ b/test/test_transform.ts @@ -1,31 +1,29 @@ import should from "should"; import { ITransform, occ, Transformation, ZDir } from ".."; - describe("testing transformation object", function () { + let trsf: ITransform; + before(function () { + trsf = new Transformation(); + should.exist(trsf); + }); + describe("a empty transformation", function () { + it("should have a scale factor of 1.0", function () { + trsf.scaleFactor.should.equal(1.0); + }); + it("should have a rotation axis of 0,0,1", function () { + trsf.scaleFactor.should.equal(1.0); + }); + }); - let trsf: ITransform; + describe("testing translation [10,20,30]", function () { before(function () { - trsf = new Transformation(); - should.exist(trsf); + trsf.makeTranslation([10, 20, 30]); }); - describe("a empty transformation", function () { - it("should have a scale factor of 1.0", function () { - trsf.scaleFactor.should.equal(1.0); - }); - it("should have a rotation axis of 0,0,1", function () { - trsf.scaleFactor.should.equal(1.0); - }); + it("should have a scale factor of 1.0", function () { + trsf.scaleFactor.should.equal(1.0); }); - - describe("testing translation [10,20,30]", function () { - before(function () { - trsf.makeTranslation([10, 20, 30]); - }); - it("should have a scale factor of 1.0", function () { - trsf.scaleFactor.should.equal(1.0); - }); - /* + /* it("should have a rotation axis of 0,0,1", function() { trsf.rotationAxis.i.should.equal(0.0); trsf.rotationAxis.j.should.equal(0.0); @@ -37,25 +35,22 @@ describe("testing transformation object", function () { trsf.translationPart.z.should.equal(30.0); }); */ - }); - describe("testing planeMirror o=[10,20,30] dir=[0,0,1", function () { - - before(function () { - - ZDir.should.eql([0, 0, 1]); - - trsf.makePlaneMirror([10, 20, 30], ZDir); + }); + describe("testing planeMirror o=[10,20,30] dir=[0,0,1", function () { + before(function () { + ZDir.should.eql([0, 0, 1]); - }); + trsf.makePlaneMirror([10, 20, 30], ZDir); + }); - it("should have a scale factor of -1.0", function () { - trsf.scaleFactor.should.eql(-1); - }); + it("should have a scale factor of -1.0", function () { + trsf.scaleFactor.should.eql(-1); + }); - it("should flip coord on the Z axis", function () { - let v = occ.makeVertex(10, 10, 40); - let v2 = v.transformed(trsf); - v2.x.should.eql(v.x); - }) + it("should flip coord on the Z axis", function () { + let v = occ.makeVertex(10, 10, 40); + let v2 = v.transformed(trsf); + v2.x.should.eql(v.x); }); + }); }); diff --git a/test/test_vertex.ts b/test/test_vertex.ts index 0e94620..eaa83fc 100644 --- a/test/test_vertex.ts +++ b/test/test_vertex.ts @@ -3,172 +3,163 @@ import { IVertex, occ } from ".."; import { Vertex, Edge, Wire, Solid, BoundingBox, makeVertex } from ".."; const doDebug = false; - describe("testing Vertex ", function () { - - describe("constructing a empty vertex ", function () { - let vertex: IVertex; - before(function () { - vertex = occ.makeVertex(); - }); - it("should be (0,0,0)", function () { - vertex.x.should.equal(0); - vertex.y.should.equal(0); - vertex.z.should.equal(0); - }); - }); - describe("constructing a vertex with a {x:..., y..,z: ...}", function () { - let vertex: IVertex; - before(function () { - vertex = occ.makeVertex({ x: 10, y: 20, z: 30 }); - }); - it("should be (10,20,30)", function () { - vertex.x.should.equal(10); - vertex.y.should.equal(20); - vertex.z.should.equal(30); - }); - }); - describe("constructing a vertex with {x:..., y..,z: ...} (property in random order)", function () { - let vertex: IVertex; - before(function () { - vertex = occ.makeVertex({ a: 10, y: 20, z: 30, x: 10 } as any); - }); - it("should be (10,20,30)", function () { - vertex.x.should.equal(10); - vertex.y.should.equal(20); - vertex.z.should.equal(30); - }); + describe("constructing a empty vertex ", function () { + let vertex: IVertex; + before(function () { + vertex = occ.makeVertex(); }); - - describe("constructing a vertex build by passing x,y,z coordinates to constructor", function () { - let vertex: IVertex; - before(function () { - vertex = occ.makeVertex(10, 20, 30); - }); - it("should be (10,20,30)", function () { - vertex.x.should.equal(10); - vertex.y.should.equal(20); - vertex.z.should.equal(30); - }); - + it("should be (0,0,0)", function () { + vertex.x.should.equal(0); + vertex.y.should.equal(0); + vertex.z.should.equal(0); }); - describe("constructing a vertex build by passing [x,y,z] coordinates to constructor", function () { - let vertex: IVertex; - before(function () { - vertex = occ.makeVertex([10, 20, 30]); - }); - it("should be (10,20,30)", function () { - vertex.x.should.equal(10); - vertex.y.should.equal(20); - vertex.z.should.equal(30); - }); - it("should be valid", function () { - vertex.isValid.should.equal(true); - }); + }); + describe("constructing a vertex with a {x:..., y..,z: ...}", function () { + let vertex: IVertex; + before(function () { + vertex = occ.makeVertex({ x: 10, y: 20, z: 30 }); }); + it("should be (10,20,30)", function () { + vertex.x.should.equal(10); + vertex.y.should.equal(20); + vertex.z.should.equal(30); + }); + }); + describe("constructing a vertex with {x:..., y..,z: ...} (property in random order)", function () { + let vertex: IVertex; + before(function () { + vertex = occ.makeVertex({ a: 10, y: 20, z: 30, x: 10 } as any); + }); + it("should be (10,20,30)", function () { + vertex.x.should.equal(10); + vertex.y.should.equal(20); + vertex.z.should.equal(30); + }); + }); - describe("constructing a vertex and applying a translation", function () { - let vertex_org: IVertex; - before(function () { - vertex_org = occ.makeVertex([10, 20, 30]); - }); - - it("should be translated", function () { - let vertex: IVertex; - vertex = vertex_org.translate([10, 20, 30]); - - vertex_org.x.should.equal(10); - vertex_org.y.should.equal(20); - vertex_org.z.should.equal(30); - - vertex.x.should.equal(20); - vertex.y.should.equal(40); - vertex.z.should.equal(60); - }); - it("should be translated - second form ", function () { - let vertex: IVertex; - vertex = vertex_org.translate(/*[*/10, 20, 30/*]*/); - - vertex_org.x.should.equal(10); - vertex_org.y.should.equal(20); - vertex_org.z.should.equal(30); - - vertex.x.should.equal(20); - vertex.y.should.equal(40); - vertex.z.should.equal(60); - }); - it("should be mirrored", function () { + describe("constructing a vertex build by passing x,y,z coordinates to constructor", function () { + let vertex: IVertex; + before(function () { + vertex = occ.makeVertex(10, 20, 30); + }); + it("should be (10,20,30)", function () { + vertex.x.should.equal(10); + vertex.y.should.equal(20); + vertex.z.should.equal(30); + }); + }); + describe("constructing a vertex build by passing [x,y,z] coordinates to constructor", function () { + let vertex: IVertex; + before(function () { + vertex = occ.makeVertex([10, 20, 30]); + }); + it("should be (10,20,30)", function () { + vertex.x.should.equal(10); + vertex.y.should.equal(20); + vertex.z.should.equal(30); + }); + it("should be valid", function () { + vertex.isValid.should.equal(true); + }); + }); - const trsf = occ.makePlaneMirror([0, 0, 0], [0, 1, 0]); + describe("constructing a vertex and applying a translation", function () { + let vertex_org: IVertex; + before(function () { + vertex_org = occ.makeVertex([10, 20, 30]); + }); - const vertex_dest = vertex_org.transformed(trsf); - vertex_org.x.should.equal(10); - vertex_org.y.should.equal(20); - vertex_org.z.should.equal(30); + it("should be translated", function () { + let vertex: IVertex; + vertex = vertex_org.translate([10, 20, 30]); - vertex_dest.x.should.equal(10); - vertex_dest.y.should.equal(-20); - vertex_dest.z.should.equal(30); + vertex_org.x.should.equal(10); + vertex_org.y.should.equal(20); + vertex_org.z.should.equal(30); - }) + vertex.x.should.equal(20); + vertex.y.should.equal(40); + vertex.z.should.equal(60); }); + it("should be translated - second form ", function () { + let vertex: IVertex; + vertex = vertex_org.translate(/*[*/ 10, 20, 30 /*]*/); + vertex_org.x.should.equal(10); + vertex_org.y.should.equal(20); + vertex_org.z.should.equal(30); - describe("edge cases: bad use of constructor shall not cause software to crash ", function () { - - it("Edge#constructor - should not crash if new is omitted", function () { - should(function () { - const tmp = /* new */ (Edge as any)(); - tmp; - }).throwError(" use new occ.Edge() to construct a Edge"); - }); - it("Vertex#constructor - should not crash if new is omitted", function () { - should(function () { - const tmp = /* new */ (Vertex as any)(10, 20, 30); - tmp; - }).throwError(" use occ.makeVertex() to construct a Vertex"); - }); - it("Wire#constructor - should not crash if new is omitted", function () { - should(function () { - const tmp = /* new */ (Wire as any)(); - tmp; - }).throwError(" use new occ.Wire() to construct a Wire"); - }); - it("Solid#constructor - should not crash if new is omitted", function () { - should(function () { - const tmp = /* new */ (Solid as any)(); - tmp; - }).throwError(" use new occ.Solid() to construct a Solid"); - }); - it("BoundingBox#constructor - should not crash if new is omitted", function () { - should(function () { - const tmp = /* new */ (BoundingBox as any)(10, 20, 30); - tmp; - }).throwError(" use new occ.BoundingBox() to construct a BoundingBox"); - }); - - it("Vertex#constructor should not crash if wrong argument are provided", function () { - const tmp = makeVertex({ x: 10, y: 20, z: 30 }); - tmp; - }); + vertex.x.should.equal(20); + vertex.y.should.equal(40); + vertex.z.should.equal(60); }); + it("should be mirrored", function () { + const trsf = occ.makePlaneMirror([0, 0, 0], [0, 1, 0]); + const vertex_dest = vertex_org.transformed(trsf); + vertex_org.x.should.equal(10); + vertex_org.y.should.equal(20); + vertex_org.z.should.equal(30); - describe("should provide a way to compare vertex", function () { - it("should compare 2 vertices with same coordinates", function () { - const vertex1 = occ.makeVertex(10, 20, 30); - const vertex2 = occ.makeVertex(10, 20, 30); - should(vertex1).eql(vertex2); - if (doDebug) { - console.log("vertex1 ", vertex1); - } - should(vertex1).containEql({ x: 10, y: 20, z: 30 }); + vertex_dest.x.should.equal(10); + vertex_dest.y.should.equal(-20); + vertex_dest.z.should.equal(30); + }); + }); + + describe("edge cases: bad use of constructor shall not cause software to crash ", function () { + it("Edge#constructor - should not crash if new is omitted", function () { + should(function () { + const tmp = /* new */ (Edge as any)(); + tmp; + }).throwError(" use new occ.Edge() to construct a Edge"); + }); + it("Vertex#constructor - should not crash if new is omitted", function () { + should(function () { + const tmp = /* new */ (Vertex as any)(10, 20, 30); + tmp; + }).throwError(" use occ.makeVertex() to construct a Vertex"); + }); + it("Wire#constructor - should not crash if new is omitted", function () { + should(function () { + const tmp = /* new */ (Wire as any)(); + tmp; + }).throwError(" use new occ.Wire() to construct a Wire"); + }); + it("Solid#constructor - should not crash if new is omitted", function () { + should(function () { + const tmp = /* new */ (Solid as any)(); + tmp; + }).throwError(" use new occ.Solid() to construct a Solid"); + }); + it("BoundingBox#constructor - should not crash if new is omitted", function () { + should(function () { + const tmp = /* new */ (BoundingBox as any)(10, 20, 30); + tmp; + }).throwError(" use new occ.BoundingBox() to construct a BoundingBox"); + }); - }); - it("should compare 2 vertices with different coordinates", function () { - const vertex1 = occ.makeVertex(10, 20, 30); - const vertex2 = occ.makeVertex(110, 220, 330); - should(vertex1).not.eql(vertex2); - }); + it("Vertex#constructor should not crash if wrong argument are provided", function () { + const tmp = makeVertex({ x: 10, y: 20, z: 30 }); + tmp; + }); + }); + + describe("should provide a way to compare vertex", function () { + it("should compare 2 vertices with same coordinates", function () { + const vertex1 = occ.makeVertex(10, 20, 30); + const vertex2 = occ.makeVertex(10, 20, 30); + should(vertex1).eql(vertex2); + if (doDebug) { + console.log("vertex1 ", vertex1); + } + should(vertex1).containEql({ x: 10, y: 20, z: 30 }); + }); + it("should compare 2 vertices with different coordinates", function () { + const vertex1 = occ.makeVertex(10, 20, 30); + const vertex2 = occ.makeVertex(110, 220, 330); + should(vertex1).not.eql(vertex2); }); + }); }); diff --git a/test/test_wire.ts b/test/test_wire.ts index d779411..3068b89 100644 --- a/test/test_wire.ts +++ b/test/test_wire.ts @@ -1,56 +1,48 @@ - - import "should"; import { IWire, occ } from ".."; describe("testing Wire ", function () { + describe("empty wire", function () { + let wire: IWire; + before(function () { + wire = occ.makeWire(); + }); + it("should have no edges", function () { + wire.numEdges.should.equal(0); + }); + it("should have no vertex", function () { + wire.numVertices.should.equal(0); + }); + it("should not be closed", function () { + wire.isClosed.should.equal(false); + }); + }); + describe("wire with three segments", function () { + let wire1: IWire, wire2: IWire, wire3: IWire; + let wire: IWire; + before(function () { + let v1 = occ.makeVertex(0, 0, 0); + let v2 = occ.makeVertex(10, 10, 0); + let v3 = occ.makeVertex(20, 0, 0); + + wire1 = occ.makeLine(v1, v2); + wire2 = occ.makeLine(v2, v3); + wire3 = occ.makeLine(v3, v1); + + wire = occ.makeWire(wire1, wire2, wire3); + }); - describe("empty wire", function () { - let wire: IWire; - before(function () { - wire = occ.makeWire(); - }); - it("should have no edges", function () { - wire.numEdges.should.equal(0); - }); - it("should have no vertex", function () { - wire.numVertices.should.equal(0); - }); - it("should not be closed", function () { - wire.isClosed.should.equal(false); - }); - }); - describe("wire with three segments", function () { - - let wire1: IWire, wire2: IWire, wire3: IWire; - let wire: IWire; - before(function () { - let v1 = occ.makeVertex(0, 0, 0); - let v2 = occ.makeVertex(10, 10, 0); - let v3 = occ.makeVertex(20, 0, 0); - - wire1 = occ.makeLine(v1, v2); - wire2 = occ.makeLine(v2, v3); - wire3 = occ.makeLine(v3, v1); - - wire = occ.makeWire(wire1, wire2, wire3); - }); - - it("should have three edges", function () { - wire.numEdges.should.equal(3); - }); - - it("should have three vertive", function () { - wire.numVertices.should.equal(3); - }); + it("should have three edges", function () { + wire.numEdges.should.equal(3); + }); - it("should be closed", function () { - wire.isClosed.should.equal(true); - }); + it("should have three vertive", function () { + wire.numVertices.should.equal(3); }); + it("should be closed", function () { + wire.isClosed.should.equal(true); + }); + }); }); - - -