This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/papercraft/openjscad/node_modules/@jscad/csg/src/FuzzyFactory3d.js

69 lines
2.2 KiB
JavaScript

const {EPS} = require('./constants')
const Polygon = require('./math/Polygon3')
const FuzzyFactory = require('./FuzzyFactory')
// ////////////////////////////////////
const FuzzyCSGFactory = function () {
this.vertexfactory = new FuzzyFactory(3, EPS)
this.planefactory = new FuzzyFactory(4, EPS)
this.polygonsharedfactory = {}
}
FuzzyCSGFactory.prototype = {
getPolygonShared: function (sourceshared) {
let hash = sourceshared.getHash()
if (hash in this.polygonsharedfactory) {
return this.polygonsharedfactory[hash]
} else {
this.polygonsharedfactory[hash] = sourceshared
return sourceshared
}
},
getVertex: function (sourcevertex) {
let elements = [sourcevertex.pos._x, sourcevertex.pos._y, sourcevertex.pos._z]
let result = this.vertexfactory.lookupOrCreate(elements, function (els) {
return sourcevertex
})
return result
},
getPlane: function (sourceplane) {
let elements = [sourceplane.normal._x, sourceplane.normal._y, sourceplane.normal._z, sourceplane.w]
let result = this.planefactory.lookupOrCreate(elements, function (els) {
return sourceplane
})
return result
},
getPolygon: function (sourcepolygon) {
let newplane = this.getPlane(sourcepolygon.plane)
let newshared = this.getPolygonShared(sourcepolygon.shared)
let _this = this
let newvertices = sourcepolygon.vertices.map(function (vertex) {
return _this.getVertex(vertex)
})
// two vertices that were originally very close may now have become
// truly identical (referring to the same Vertex object).
// Remove duplicate vertices:
let newverticesDedup = []
if (newvertices.length > 0) {
let prevvertextag = newvertices[newvertices.length - 1].getTag()
newvertices.forEach(function (vertex) {
let vertextag = vertex.getTag()
if (vertextag !== prevvertextag) {
newverticesDedup.push(vertex)
}
prevvertextag = vertextag
})
}
// If it's degenerate, remove all vertices:
if (newverticesDedup.length < 3) {
newverticesDedup = []
}
return new Polygon(newverticesDedup, newshared, newplane)
}
}
module.exports = FuzzyCSGFactory