UNPKG

acc-viewer

Version:

Acc Viewer

241 lines (211 loc) 7.06 kB
import { ImporterBase } from './importerbase.js'; import { Direction } from '../geometry/geometry.js'; import { Mesh } from '../model/mesh.js'; import { ArrayBufferToUtf8String } from '../io/bufferutils.js'; import { Coord3D } from '../geometry/coord3d.js'; import { Triangle } from '../model/triangle.js'; export class ImporterUnv extends ImporterBase { constructor() { super(); } CanImportExtension(extension) { return extension === 'unv'; } GetUpDirection() { return Direction.Z; } ClearContent() { this.mesh = null; this.triangle = null; } ResetContent() { this.mesh = new Mesh(); this.model.AddMeshToRootNode(this.mesh); this.triangle = null; } ImportContent(fileContent, onFinish) { if (this.IsBinaryUnvFile(fileContent)) { this.ProcessBinary(fileContent); } else { let textContent = ArrayBufferToUtf8String(fileContent); console.log(textContent); // ReadLines (textContent, (line) => { // if (!this.WasError ()) { // this.ProcessLine (line); // } // }); } onFinish(); } IsBinaryUnvFile(fileContent) { // let byteLength = fileContent.byteLength; // if (byteLength < 84) { // return false; // } // let reader = new BinaryReader (fileContent, true); // reader.Skip (80); // let triangleCount = reader.ReadUnsignedInteger32 (); // if (byteLength !== triangleCount * 50 + 84) { // return false; // } return true; } ProcessBinary(fileContent) { console.log(fileContent); console.log('process binary'); function ReadVector(reader) { let coord = new Coord3D(); coord.x = reader.ReadFloat32(); coord.y = reader.ReadFloat32(); coord.z = reader.ReadFloat32(); return coord; } function AddVertex(mesh, reader) { console.log('Add vertex'); let coord = ReadVector(reader); return mesh.AddVertex(coord); } let coord = new Coord3D(); coord.x = 1; coord.y = 0; coord.z = 0; let coord2 = new Coord3D(); coord2.x = 2; coord2.y = 0; coord2.z = 0; let coord3 = new Coord3D(); coord3.x = 0; coord3.y = 1; coord3.z = 0; let v0 = this.mesh.AddVertex(coord); let v1 = this.mesh.AddVertex(coord2); let v2 = this.mesh.AddVertex(coord3); let triangle = new Triangle(v0, v1, v2); // let normal = this.mesh.AddNormal (normalVector); // triangle.SetNormals (normal, normal, normal); this.mesh.AddTriangle(triangle); console.log(triangle); // let customMesh = new Mesh('custom'); // let positions = [-5, 2, -3, -7, -2, -3, -3, -2, -3]; // let indices = [0, 1, 2]; // let uvs = [0, 1, 0, 0, 1, 0]; // let colors = [1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1]; // let normals = []; // VertexData.ComputeNormals(positions, indices, normals); // let vertexData = new VertexData(); // vertexData.positions = positions; // vertexData.indices = indices; // vertexData.normals = normals; // vertexData.uvs = uvs; // vertexData.colors = colors; // vertexData.applyToMesh(customMesh); // let reader = new BinaryReader (fileContent, true); // reader.Skip (80); // let triangleCount = reader.ReadUnsignedInteger32 (); // for (let i = 0; i < triangleCount; i++) { // let normalVector = ReadVector (reader); // let v0 = AddVertex (this.mesh, reader); // let v1 = AddVertex (this.mesh, reader); // let v2 = AddVertex (this.mesh, reader); // reader.Skip (2); // let triangle = new Triangle (v0, v1, v2); // if (IsPositive (normalVector.Length ())) { // let normal = this.mesh.AddNormal (normalVector); // triangle.SetNormals (normal, normal, normal); // } // this.mesh.AddTriangle (triangle); // } } unvInitLoader() { console.log('unvInitLoader'); let state = { cnt: 0, start_to_read: 0, numberoftris: 0, numberofnodes: 0, numberofmedias: 1, lineNo1: 0, nodeCoords: [], trinodes: [], surfaces: [], trimedia: [], minusOneLinesCount: 0, lineIsMinusOne: false, formediaTemp2: 0, }; return state; } unvParseLine(line, state) { console.log('unvParseLine'); state.cnt = state.cnt + 1; line = line.trim(); state.lineIsMinusOne = line === '-1'; if (state.start_to_read === 0) { //: # Read empty slots unvread('mesh_deneme.unv') } else if (state.start_to_read === 1) { state.numberofnodes = state.numberofnodes + 1; if (state.numberofnodes % 2 === 0 && state.lineIsMinusOne === false) { state.nodeCoords.push(this.createNodeCoordLine(line)); } } else if (state.start_to_read === 2) { let fields = this.splitLine(line); if (state.numberoftris === 0) { state.formediaTemp2 = parseInt(fields[2]); state.surfaces.push(state.formediaTemp2); } else if ( state.numberoftris % 2 === 0 && state.lineIsMinusOne === false ) { let formediaTemp1 = state.formediaTemp2; state.formediaTemp2 = parseInt(fields[2]); state.surfaces.push(state.formediaTemp2); if (formediaTemp1 !== state.formediaTemp2) { state.numberofmedias = state.numberofmedias + 1; } } else if ( state.numberoftris % 2 === 1 && state.lineIsMinusOne === false ) { // load triangle // assert(fields.length == 3); let intFields = fields.map((s) => parseInt(s)); state.trinodes.push(intFields); state.trimedia.push(state.numberofmedias); } state.numberoftris += 1; if (state.lineIsMinusOne) { state.start_to_read = 3; state.numberoftris = Math.floor((state.numberoftris - 1) / 2); return; } } if (state.lineIsMinusOne) { state.minusOneLinesCount = state.minusOneLinesCount + 1; return; } if (line === '2411' && state.minusOneLinesCount === 2) { state.start_to_read = 1; state.numberofnodes = 0; state.lineNo1 = state.cnt; } else if (line === '2412' && state.minusOneLinesCount === 2) { state.start_to_read = 2; state.numberofnodes = Math.floor((state.numberofnodes - 2) / 2); state.numberoftris = 0; } else { state.minusOneLinesCount = 0; } } convert2float(str) { return parseFloat(str.replace('D', 'E')); } ab2str(buf) { return String.fromCharCode.apply(null, new Uint8Array(buf)); } splitLine(line) { return line.split(/\s+/); } createNodeCoordLine(line_info) { let nodeCoordsStr = this.splitLine(line_info); return nodeCoordsStr.map((s) => this.convert2float(s)); } }