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/math/Side.js

103 lines
2.6 KiB
JavaScript

const Vector2D = require('./Vector2')
const Vertex = require('./Vertex2')
const Vertex3 = require('./Vertex3')
const Polygon = require('./Polygon3')
const {getTag} = require('../constants')
const Side = function (vertex0, vertex1) {
if (!(vertex0 instanceof Vertex)) throw new Error('Assertion failed')
if (!(vertex1 instanceof Vertex)) throw new Error('Assertion failed')
this.vertex0 = vertex0
this.vertex1 = vertex1
}
Side.fromObject = function (obj) {
var vertex0 = Vertex.fromObject(obj.vertex0)
var vertex1 = Vertex.fromObject(obj.vertex1)
return new Side(vertex0, vertex1)
}
Side._fromFakePolygon = function (polygon) {
// this can happen based on union, seems to be residuals -
// return null and handle in caller
if (polygon.vertices.length < 4) {
return null
}
var vert1Indices = []
var pts2d = polygon.vertices.filter(function (v, i) {
if (v.pos.z > 0) {
vert1Indices.push(i)
return true
}
return false
})
.map(function (v) {
return new Vector2D(v.pos.x, v.pos.y)
})
if (pts2d.length !== 2) {
throw new Error('Assertion failed: _fromFakePolygon: not enough points found')
}
var d = vert1Indices[1] - vert1Indices[0]
if (d === 1 || d === 3) {
if (d === 1) {
pts2d.reverse()
}
} else {
throw new Error('Assertion failed: _fromFakePolygon: unknown index ordering')
}
var result = new Side(new Vertex(pts2d[0]), new Vertex(pts2d[1]))
return result
}
Side.prototype = {
toString: function () {
return this.vertex0 + ' -> ' + this.vertex1
},
toPolygon3D: function (z0, z1) {
//console.log(this.vertex0.pos)
const vertices = [
new Vertex3(this.vertex0.pos.toVector3D(z0)),
new Vertex3(this.vertex1.pos.toVector3D(z0)),
new Vertex3(this.vertex1.pos.toVector3D(z1)),
new Vertex3(this.vertex0.pos.toVector3D(z1))
]
return new Polygon(vertices)
},
transform: function (matrix4x4) {
var newp1 = this.vertex0.pos.transform(matrix4x4)
var newp2 = this.vertex1.pos.transform(matrix4x4)
return new Side(new Vertex(newp1), new Vertex(newp2))
},
flipped: function () {
return new Side(this.vertex1, this.vertex0)
},
direction: function () {
return this.vertex1.pos.minus(this.vertex0.pos)
},
getTag: function () {
var result = this.tag
if (!result) {
result = getTag()
this.tag = result
}
return result
},
lengthSquared: function () {
let x = this.vertex1.pos.x - this.vertex0.pos.x
let y = this.vertex1.pos.y - this.vertex0.pos.y
return x * x + y * y
},
length: function () {
return Math.sqrt(this.lengthSquared())
}
}
module.exports = Side