diff --git a/source/io/bufferutils.js b/source/io/bufferutils.js index 2b4e71a..6e51a8d 100644 --- a/source/io/bufferutils.js +++ b/source/io/bufferutils.js @@ -27,7 +27,8 @@ OV.AsciiStringToArrayBuffer = function (str) OV.Utf8StringToArrayBuffer = function (str) { let encoder = new TextEncoder (); - return encoder.encode (str); + let uint8Array = encoder.encode (str); + return uint8Array.buffer; }; OV.Base64DataURIToArrayBuffer = function (uri) diff --git a/test/tests/exportimport_test.js b/test/tests/exportimport_test.js new file mode 100644 index 0000000..6e3b6a0 --- /dev/null +++ b/test/tests/exportimport_test.js @@ -0,0 +1,118 @@ +var assert = require ('assert'); +var testUtils = require ('../utils/testutils.js'); + +function ExportImport (model, format, extension, onReady) +{ + let exporter = new OV.Exporter (); + exporter.Export (model, format, extension, { + onSuccess : function (files) { + let fileObjects = []; + for (let file of files) { + fileObjects.push (new FileObject ('', file.name, file.content)); + } + let importer = new OV.Importer (); + importer.LoadFilesFromFileObjects (fileObjects, function () { + let settings = new OV.ImportSettings (); + importer.Import (settings, { + onSuccess : function (importResult) { + onReady (importResult.model) + }, + onError : function (importError) { + + } + }); + }); + } + }); +} + +describe ('Export-Import Test', function () { + it ('Obj Export-Import', function (done) { + let model = testUtils.GetTranslatedRotatedCubesModel (); + ExportImport (model, OV.FileFormat.Text, 'obj', (result) => { + assert.strictEqual (model.MeshInstanceCount (), 3); + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + done (); + }); + }); + + it ('Stl Ascii Export-Import', function (done) { + let model = testUtils.GetTranslatedRotatedCubesModel (); + ExportImport (model, OV.FileFormat.Text, 'stl', (result) => { + assert.strictEqual (model.MeshInstanceCount (), 3); + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + done (); + }); + }); + + it ('Stl Binary Export-Import', function (done) { + let model = testUtils.GetTranslatedRotatedCubesModel (); + ExportImport (model, OV.FileFormat.Binary, 'stl', (result) => { + assert.strictEqual (model.MeshInstanceCount (), 3); + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + done (); + }); + }); + + it ('Ply Ascii Export-Import', function (done) { + let model = testUtils.GetTranslatedRotatedCubesModel (); + ExportImport (model, OV.FileFormat.Text, 'ply', (result) => { + assert.strictEqual (model.MeshInstanceCount (), 3); + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + done (); + }); + }); + + it ('Ply Binary Export-Import', function (done) { + let model = testUtils.GetTranslatedRotatedCubesModel (); + ExportImport (model, OV.FileFormat.Binary, 'ply', (result) => { + assert.strictEqual (model.MeshInstanceCount (), 3); + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + done (); + }); + }); + + it ('glTF Ascii Export-Import', function (done) { + let model = testUtils.GetTranslatedRotatedCubesModel (); + ExportImport (model, OV.FileFormat.Text, 'gltf', (result) => { + assert.strictEqual (model.MeshInstanceCount (), 3); + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + done (); + }); + }); + + it ('glTF Binary Export-Import', function (done) { + let model = testUtils.GetTranslatedRotatedCubesModel (); + ExportImport (model, OV.FileFormat.Binary, 'glb', (result) => { + assert.strictEqual (model.MeshInstanceCount (), 3); + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + done (); + }); + }); + + + it ('Off Export-Import', function (done) { + let model = testUtils.GetTranslatedRotatedCubesModel (); + ExportImport (model, OV.FileFormat.Text, 'off', (result) => { + assert.strictEqual (model.MeshInstanceCount (), 3); + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + done (); + }); + }); +}); diff --git a/test/tests/model_test.js b/test/tests/model_test.js index 0986130..d82300a 100644 --- a/test/tests/model_test.js +++ b/test/tests/model_test.js @@ -227,6 +227,14 @@ describe ('Model Finalization', function () { }); assert.strictEqual (nodeCount, 3); }); + + it ('Remove Empty Nodes Recursively', function () { + let model = testUtils.GetHierarchicalModelNoFinalization (); + OV.FinalizeModel (model, function () { return new OV.Material (OV.MaterialType.Phong) }); + assert.strictEqual (model.MeshCount (), 0); + assert.strictEqual (model.MeshInstanceCount (), 0); + assert (model.GetRootNode ().IsEmpty ()); + }); }); describe ('Color Conversion', function () { @@ -270,7 +278,7 @@ function GetModelTree (model) describe ('Node Hierarchy', function () { it ('Enumerate hierarchy', function () { - let model = testUtils.GetHierarchicalModel (); + let model = testUtils.GetHierarchicalModelNoFinalization (); let modelTree = GetModelTree (model); assert.deepStrictEqual (modelTree, { name : '', @@ -302,7 +310,7 @@ describe ('Node Hierarchy', function () { }); it ('Remove mesh', function () { - let model = testUtils.GetHierarchicalModel (); + let model = testUtils.GetHierarchicalModelNoFinalization (); model.RemoveMesh (2); let modelTree = GetModelTree (model); assert.deepStrictEqual (modelTree, { @@ -335,7 +343,7 @@ describe ('Node Hierarchy', function () { }); it ('Add mesh to index', function () { - let model = testUtils.GetHierarchicalModel (); + let model = testUtils.GetHierarchicalModelNoFinalization (); let mesh = new OV.Mesh (); mesh.SetName ('Mesh 8'); model.AddMeshToIndex (mesh, 3); diff --git a/test/utils/globals.js b/test/utils/globals.js index 0119e21..a129766 100644 --- a/test/utils/globals.js +++ b/test/utils/globals.js @@ -16,15 +16,19 @@ global.URL = { return 'ObjectUrl:' + objectUrlCounter.toString (); }, revokeObjectURL : function () { - + } }; -global.FileObject = function (folderName, fileName) +global.FileObject = function (folderName, fileName, fileContent) { + this.name = path.join (folderName, fileName); this.folderName = folderName; this.fileName = fileName; - this.name = path.join (folderName, fileName); + this.fileContent = null; + if (fileContent !== undefined) { + this.fileContent = fileContent; + } }; global.FileReader = class @@ -48,6 +52,16 @@ global.FileReader = class readAsArrayBuffer (fileObject) { + if (fileObject.fileContent !== null) { + this.onloadend ({ + target : { + readyState : FileReader.DONE, + result : fileObject.fileContent + } + }); + return; + } + let content = testUtils.GetArrayBufferFileContent (fileObject.folderName, fileObject.fileName); if (content !== null) { this.onloadend ({ @@ -83,4 +97,4 @@ global.document = { type : type }; } -}; \ No newline at end of file +}; diff --git a/test/utils/testutils.js b/test/utils/testutils.js index 5411f51..534f474 100644 --- a/test/utils/testutils.js +++ b/test/utils/testutils.js @@ -231,7 +231,7 @@ module.exports = return model; }, - GetHierarchicalModel () + GetHierarchicalModelNoFinalization () { /* + @@ -351,6 +351,7 @@ module.exports = root.AddChildNode (rotatedNode); rotatedNode.AddChildNode (translatedRotatedNode); + OV.FinalizeModel (model, function () { return new OV.Material (OV.MaterialType.Phong) }); return model; } }