Skip to content

Commit

Permalink
fix(all): sync changes to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
z3dev authored Jun 4, 2024
2 parents 8870b43 + 5cb1d64 commit c263907
Show file tree
Hide file tree
Showing 22 changed files with 388 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
There are many ways to use JSCAD:

An [online version](https://openjscad.xyz/), [self hosteable web based ui](./packages/web), as [CLI](./packages/cli) (command-line interface) for server-side computations with Node.js, as well as an experimental [desktop app](./packages/desktop) or [individual Node.js packages](./packages/README.md)!!
An [online version](https://openjscad.xyz/), [self hostable web based ui](./packages/web), as [CLI](./packages/cli) (command-line interface) for server-side computations with Node.js, as well as an experimental [desktop app](./packages/desktop) or [individual Node.js packages](./packages/README.md)!!

This repository is a [monorepo](https://medium.com/@maoberlehner/monorepos-in-the-wild-33c6eb246cb9) (container of multiple packages and tools) maintained with [PNPM](https://pnpm.io/) and [Lerna-Lite](https://github.com/ghiscoding/lerna-lite)

Expand Down
83 changes: 45 additions & 38 deletions packages/io/x3d-deserializer/src/objects.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { poly2 } from '@jscad/modeling'
import { area, poly2 } from '@jscad/modeling'

export const x3dTypes = {
X3D: 0,
Expand Down Expand Up @@ -89,31 +89,31 @@ export const x3dTransform = (element) => {
}

if (element.center) {
const values = element.center.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.center)
if (values.length > 2) {
obj.center = values
}
}
if (element.rotation) {
const values = element.rotation.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.rotation)
if (values.length > 3) {
obj.rotation = values
}
}
if (element.scale) {
const values = element.scale.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.scale)
if (values.length > 2) {
obj.scale = values
}
}
if (element.scaleorientation) {
const values = element.scaleorientation.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.scaleorientation)
if (values.length > 3) {
obj.scaleOrientation = values
}
}
if (element.translation) {
const values = element.translation.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.translation)
if (values.length > 2) {
obj.translation = values
}
Expand All @@ -138,7 +138,7 @@ export const x3dBox = (element) => {
const obj = { definition: x3dTypes.BOX, size: [2, 2, 2] }

if (element.size) {
const values = element.size.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.size)
if (values.length > 2) {
obj.size = values
}
Expand Down Expand Up @@ -187,7 +187,7 @@ export const x3dSphere = (element) => {
obj.radius = parseFloat(element.radius)
}
if (element.subdivision) {
const values = element.subdivision.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.subdivision)
if (values.length > 1) {
obj.subdivision = Math.max(...values)
}
Expand Down Expand Up @@ -217,18 +217,18 @@ export const x3dExtrusion = (element) => {
obj.endCap = element.endCap.includes('TRUE') || element.endCap.includes('true')
}
if (element.crossSection) {
const values = element.crossSection.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.crossSection)
const numpoints = Math.trunc(values.length / 2)
const points = []
for (let i = 0; i < numpoints; i++) {
const vi = i * 2
points.push([values[vi], values[vi + 1]])
}
obj.ccw = (poly2.measureArea(poly2.create(points)) < 0) // WHAT!!!! X3D IS SICK!!!
obj.ccw = (area(points) < 0) // WHAT!!!! X3D IS SICK!!!
obj.crossSection = points
}
if (element.orientation) {
const values = element.orientation.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.orientation)
const numpoints = Math.trunc(values.length / 4)
const points = []
for (let i = 0; i < numpoints; i++) {
Expand All @@ -238,7 +238,7 @@ export const x3dExtrusion = (element) => {
obj.orientations = points
}
if (element.scale) {
const values = element.scale.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.scale)
const numpoints = Math.trunc(values.length / 2)
const points = []
for (let i = 0; i < numpoints; i++) {
Expand All @@ -251,7 +251,7 @@ export const x3dExtrusion = (element) => {
obj.scales = points
}
if (element.spine) {
const values = element.spine.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.spine)
const numpoints = Math.trunc(values.length / 3)
const points = []
for (let i = 0; i < numpoints; i++) {
Expand Down Expand Up @@ -337,7 +337,7 @@ export const x3dPolyline2D = (element) => {
const obj = { definition: x3dTypes.POLYLINE2D, lineSegments: [] }

if (element.lineSegments) {
const values = element.lineSegments.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.lineSegments)
for (let i = 0; i < values.length; i = i + 2) {
const point = [values[i], values[i + 1]]
obj.lineSegments.push(point)
Expand All @@ -350,7 +350,7 @@ export const x3dRectangle2D = (element) => {
const obj = { definition: x3dTypes.RECTANGLE2D, size: [2, 2] }

if (element.size) {
const values = element.size.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.size)
if (values.length > 1) {
obj.size = values
}
Expand All @@ -362,7 +362,7 @@ export const x3dTriangleSet2D = (element) => {
const obj = { definition: x3dTypes.TRIANGLESET2D, vertices: [] }

if (element.vertices) {
const values = element.vertices.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.vertices)
for (let i = 0; i < values.length; i = i + 2) {
const point = [values[i], values[i + 1]]
obj.vertices.push(point)
Expand All @@ -379,7 +379,7 @@ export const x3dLineSet = (element) => {
const obj = { definition: x3dTypes.LINESET, vertexCount: [], colorPerVertex: true }

if (element.vertexCount) {
obj.vertexCount = element.vertexCount.trim().split(/ +/).map((v) => parseFloat(v))
obj.vertexCount = parseNumbers(element.vertexCount)
}
// color attributes
if (element.colorPerVertex) {
Expand All @@ -393,9 +393,8 @@ export const x3dIndexedLineSet = (element) => {
const obj = { definition: x3dTypes.INDEXEDLINESET, indexes: [], colorPerVertex: true }

if (element.coordIndex) {
const indexes = element.coordIndex.trim().split(/ -1/)
obj.indexes = indexes.map((index) => index.trim().split(/ +/).map((v) => parseFloat(v)))
obj.indexes = obj.indexes.filter((index) => index.length > 1)
const indexes = parseIndices(element.coordIndex)
obj.indexes = indexes.filter((index) => index.length > 1)
}
// color attributes
if (element.colorPerVertex) {
Expand All @@ -413,7 +412,7 @@ export const x3dColor = (element) => {
const obj = { definition: x3dTypes.COLOR, colors: [] }

if (element.color) {
const values = element.color.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.color)
const numvalues = values.length
const numcolors = Math.trunc(numvalues / 3)
for (let i = 0; i < numcolors; i++) {
Expand All @@ -428,7 +427,7 @@ export const x3dCoordinate = (element) => {
const obj = { definition: x3dTypes.COORDINATE, points: [] }

if (element.point) {
const values = element.point.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.point)
const numvalues = values.length
const numpoints = Math.trunc(numvalues / 3)
for (let i = 0; i < numpoints; i++) {
Expand Down Expand Up @@ -456,7 +455,7 @@ export const x3dTriangleFanSet = (element) => {
obj.ccw = element.ccw.includes('TRUE') || element.ccw.includes('true')
}
if (element.fanCount) {
obj.fanCount = element.fanCount.trim().split(/ +/).map((v) => parseFloat(v))
obj.fanCount = parseNumbers(element.fanCount)
}
obj.objects = []
return obj
Expand All @@ -469,7 +468,7 @@ export const x3dTriangleStripSet = (element) => {
obj.ccw = element.ccw.includes('TRUE') || element.ccw.includes('true')
}
if (element.stripCount) {
obj.stripCount = element.stripCount.trim().split(/ +/).map((v) => parseFloat(v))
obj.stripCount = parseNumbers(element.stripCount)
}
obj.objects = []
return obj
Expand All @@ -492,7 +491,7 @@ export const x3dIndexedTriangleSet = (element) => {
obj.ccw = element.ccw.includes('TRUE') || element.ccw.includes('true')
}
if (element.index) {
obj.index = element.index.trim().split(/ +/).map((v) => parseFloat(v))
obj.index = parseNumbers(element.index)
}
obj.objects = []
return obj
Expand All @@ -505,8 +504,8 @@ export const x3dIndexedTriangleFanSet = (element) => {
obj.ccw = element.ccw.includes('TRUE') || element.ccw.includes('true')
}
if (element.index) {
const indexes = element.index.trim().split(/ -1/)
obj.fans = indexes.map((index) => index.trim().split(/ +/).map((v) => parseFloat(v))).filter((index) => index.length > 2)
const indexes = parseIndices(element.index)
obj.fans = indexes.filter((index) => index.length > 2)
}
obj.objects = []
return obj
Expand All @@ -520,8 +519,8 @@ export const x3dIndexedTriangleStripSet = (element) => {
obj.ccw = element.ccw.includes('TRUE') || element.ccw.includes('true')
}
if (element.index) {
const indexes = element.index.trim().split(/ -1/)
obj.strips = indexes.map((index) => index.trim().split(/ +/).map((v) => parseFloat(v))).filter((index) => index.length > 2)
const indexes = parseIndices(element.index)
obj.strips = indexes.filter((index) => index.length > 2)
}
return obj
}
Expand All @@ -533,7 +532,7 @@ export const x3dIndexedQuadSet = (element) => {
obj.ccw = element.ccw.includes('TRUE') || element.ccw.includes('true')
}
if (element.index) {
obj.index = element.index.trim().split(/ +/).map((v) => parseFloat(v))
obj.index = parseNumbers(element.index)
}
obj.objects = []
return obj
Expand All @@ -549,8 +548,8 @@ export const x3dIndexedFaceSet = (element) => {
obj.convex = element.convex.includes('TRUE') || element.convex.includes('true')
}
if (element.coordIndex) {
const indexes = element.coordIndex.trim().split(/ -1/)
obj.faces = indexes.map((index) => index.trim().split(/ +/).map((v) => parseFloat(v))).filter((index) => index.length > 2)
const indexes = parseIndices(element.coordIndex)
obj.faces = indexes.filter((index) => index.length > 2)
}
// color attributes
if (element.colorPerVertex) {
Expand All @@ -559,11 +558,11 @@ export const x3dIndexedFaceSet = (element) => {
if (element.colorIndex) {
if (obj.colorPerVertex) {
// indexes are provided for each VERTEX
const indexes = element.colorIndex.trim().split(/ -1/)
obj.colorIndex = indexes.map((index) => index.trim().split(/ +/).map((v) => parseFloat(v))).filter((index) => index.length > 2)
const indexes = parseIndices(element.colorIndex)
obj.colorIndex = indexes.filter((index) => index.length > 2)
} else {
// indexes are provided for each FACE
obj.colorIndex = element.colorIndex.trim().split(/ +/).map((v) => parseFloat(v))
obj.colorIndex = parseNumbers(element.colorIndex)
}
} else {
// reuse the indexes for the FACES
Expand All @@ -589,7 +588,7 @@ export const x3dElevationGrid = (element) => {
obj.zSpacing = parseFloat(element.zSpacing)
}
if (element.height) {
obj.height = element.height.trim().split(/ +/).map((v) => parseFloat(v))
obj.height = parseNumbers(element.height)
}
if (element.ccw) {
obj.ccw = element.ccw.includes('TRUE') || element.ccw.includes('true')
Expand Down Expand Up @@ -632,14 +631,14 @@ export const x3dMaterial = (element) => {
alpha = 1.0 - element.transparency
}
if (element.diffuseColor) {
const values = element.diffuseColor.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.diffuseColor)
if (values.length > 2) {
if (values.length < 4) values.push(alpha)
obj.color = values
}
}
if (element.emissiveColor) {
const values = element.emissiveColor.trim().split(/ +/).map((v) => parseFloat(v))
const values = parseNumbers(element.emissiveColor)
if (values.length > 2) {
if (values.length < 4) values.push(alpha)
obj.color = values
Expand All @@ -656,3 +655,11 @@ export const x3dGroup = (element) => {
obj.objects = []
return obj
}

export const parseNumbers = (attribute) =>
attribute.trim().replace(/,/g, ' ').split(/ +/).map((v) => parseFloat(v))

export const parseIndices = (attribute) => {
const indexes = attribute.replace(/,/g, ' ').trim().split(/ -1/)
return indexes.map((index) => parseNumbers(index))
}
36 changes: 17 additions & 19 deletions packages/io/x3d-deserializer/src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,6 @@ import {
x3dMaterial
} from './objects.js'

let x3dLast = null // last object found
let x3dDefinition = x3dTypes.X3D // what kind of object being 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 @@ -107,10 +92,25 @@ const nodeToObjectMap = {
let objectId = 1
const getObjectId = () => ('0000' + objectId++).slice(-4)

const createX3DParser = (src) => {
export const parse = (src) => {
// create a parser for the XML
const parser = new saxes.SaxesParser()

// high level elements / definitions
let x3dLast = null // last object found
let x3dDefinition = x3dTypes.X3D // what kind of object being created

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

parser.on('error', (e) => {
console.log(`error: line ${e.line}, column ${e.column}, bad character [${e.c}]`)
})
Expand Down Expand Up @@ -331,10 +331,8 @@ const createX3DParser = (src) => {

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

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

0 comments on commit c263907

Please sign in to comment.