Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: x3d defs caching #1338

Merged
merged 3 commits into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions packages/io/x3d-deserializer/src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,6 @@ const {
x3dMaterial
} = require('./objects')

let x3dLast = null // last object found
let x3dDefinition = x3dTypes.X3D // what kind of object beinging created

// high level elements / definitions
const x3dObjects = [] // list of objects
const x3dDefs = new Map() // list of named objects

const x3dMaterials = [] // list of materials
const x3dTextures = [] // list of textures

const x3dLength = { factor: 1.0, name: 'meters' }
const x3dAngle = { factor: 1.0, name: 'radians' }

let x3dObj = null // x3d in object form

const nodeToObjectMap = {
X3D: x3dX3D,
UNIT: x3dUnit,
Expand Down Expand Up @@ -110,15 +95,31 @@ const getObjectId = () => ('0000' + objectId++).slice(-4)
const createX3DParser = (src, pxPmm) => {
// create a parser for the XML
const parser = new saxes.SaxesParser()
const x3dDefs = new Map() // list of named objects
let x3dLast = null // last object found
let x3dDefinition = x3dTypes.X3D // what kind of object beinging created
let x3dObj = null // x3d in object form

// high level elements / definitions
const x3dObjects = [] // list of objects
const x3dMaterials = [] // list of materials
const x3dTextures = [] // list of textures

const x3dLength = { factor: 1.0, name: 'meters' }
const x3dAngle = { factor: 1.0, name: 'radians' }

parser.on('error', (e) => {
console.log(`error: line ${e.line}, column ${e.column}, bad character [${e.c}]`)
console.log(
`error: line ${e.line}, column ${e.column}, bad character [${e.c}]`
)
})

parser.on('opentag', (node) => {
// convert known XML tags to objects
const elementname = node.name.toUpperCase()
let obj = nodeToObjectMap[elementname] ? nodeToObjectMap[elementname](node.attributes, { x3dObjects }) : null
let obj = nodeToObjectMap[elementname]
? nodeToObjectMap[elementname](node.attributes, { x3dObjects })
: null

if (obj) {
obj.id = getObjectId()
Expand All @@ -129,17 +130,23 @@ const createX3DParser = (src, pxPmm) => {
if (x3dDefs.has(objectname)) {
const def = x3dDefs.get(objectname)
if (def.definition !== obj.definition) {
console.log(`WARNING: using a definition "${objectname}" of a different type; ${obj.definition} vs ${def.definition}`)
console.log(
`WARNING: using a definition "${objectname}" of a different type; ${obj.definition} vs ${def.definition}`
)
}
obj = def
} else {
console.log(`WARNING: definition "${objectname}" does not exist, using default for ${obj.definition}`)
console.log(
`WARNING: definition "${objectname}" does not exist, using default for ${obj.definition}`
)
}
} else {
if (node.attributes.DEF) {
const objectname = node.attributes.DEF
if (x3dDefs.has(objectname)) {
console.log(`WARNING: redefintion of ${objectname} has been ignored`)
console.log(
`WARNING: redefintion of ${objectname} has been ignored`
)
} else {
x3dDefs.set(objectname, obj)
}
Expand Down Expand Up @@ -331,12 +338,12 @@ const createX3DParser = (src, pxPmm) => {

// start the parser
parser.write(src).close()
}

const parse = (src, pxPmm) => {
createX3DParser(src, pxPmm)
// console.log(JSON.stringify(x3dObj))
return { x3dObj, x3dMaterials, x3dTextures }
return {
x3dObj,
x3dMaterials,
x3dTextures
}
}

module.exports = parse
module.exports = createX3DParser
Loading