Always return arraybuffer during export.

This commit is contained in:
kovacsv 2021-10-27 09:55:01 +02:00
parent d78feaceee
commit fc2dd12aa0
5 changed files with 151 additions and 9 deletions

View File

@ -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)

View File

@ -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 ();
});
});
});

View File

@ -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 : '<Root>',
@ -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);

View File

@ -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
};
}
};
};

View File

@ -231,7 +231,7 @@ module.exports =
return model;
},
GetHierarchicalModel ()
GetHierarchicalModelNoFinalization ()
{
/*
+ <Root>
@ -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;
}
}