@eturnity/eturnity_maths 7.20.0 → 7.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,185 +1,283 @@
1
-
2
1
  import {
3
- getDistanceBetweenPoints,
4
- get3DDistanceBetweenPoints,
5
- getPointOnLine,
6
- isSamePoint3D,
7
- translate2D,
8
- midPoint,
9
- getDataAboutTwo3DLines,
10
- isPointBetweenSegment,
2
+ getDistanceBetweenPoints,
3
+ get3DDistanceBetweenPoints,
4
+ getPointOnLine,
5
+ isSamePoint3D,
6
+ isSamePoint2D,
7
+ translate2D,
8
+ midPoint,
9
+ getDataAboutTwo3DLines,
10
+ isPointBetweenSegment,
11
+ verticalProjectionOnPlane
11
12
  } from '../geometry'
12
13
  import { v4 as uuidv4 } from 'uuid'
13
- import {Point} from './Point'
14
- import { substractVector,normalizeVector, addVector, multiplyVector,dotProduct } from '../vector'
15
-
14
+ import { Point } from './Point'
15
+ import {
16
+ substractVector,
17
+ normalizeVector,
18
+ addVector,
19
+ multiplyVector,
20
+ dotProduct
21
+ } from '../vector'
16
22
 
17
23
  export class Line {
18
- constructor(firstPoint, lastPoint, layer, infiniteLine = false) {
19
- this.id = uuidv4()
20
- this.version = 0
21
- this.type = 'line'
22
- if (firstPoint && lastPoint) {
23
- this.outline = [firstPoint, lastPoint]
24
- } else {
25
- this.outline = []
26
- }
27
- this.layer = layer
28
- this.visible = true
29
- this.infiniteLine = infiniteLine
30
- }
31
- clone() {
32
- const line = new Line()
33
- this.outline.forEach((p) => {
34
- line.outline.push(new Point(p.x, p.y, p.z))
35
- })
36
- line.id = uuidv4()
37
- line.layer = this.layer
38
- line.infiniteLine = this.infiniteLine
39
- return line
40
- }
41
- getDirectionVector(){
42
- return normalizeVector(substractVector(this.outline[1],this.outline[0]))
43
- }
44
- getIntersections(object){
45
- const intersections=[]
46
- if(!object)return
47
- if(object.type=="point"){
48
- if(isSamePoint3D(this.getProjectedPoint(object),object)){
49
- intersections.push({...object})
50
- }
51
- }else if(object.type=="line"){
52
- const data = getDataAboutTwo3DLines(this.outline[0],this.getDirectionVector(),object.outline[0],object.getDirectionVector())
53
- if(!data){return []}
54
- let {distance,N} = data
55
- if(distance<0.1 && !!N){
56
- intersections.push({...N})
57
- }
58
- }else if(object.type=="circle"){
59
- let P=this.getProjectedPoint(object.center)
60
- let distance = get3DDistanceBetweenPoints(P,object.center)
61
- if(distance<=object.radius){
62
- let A=addVector(P,multiplyVector(Math.sqrt(Math.pow(object.radius,2)-Math.pow(distance,2)),this.getDirectionVector()))
63
- let B=addVector(P,multiplyVector(-Math.sqrt(Math.pow(object.radius,2)-Math.pow(distance,2)),this.getDirectionVector()))
64
- intersections.push({...A})
65
- intersections.push({...B})
66
- }
67
- }
68
- return intersections
69
- }
70
- pxLength(canvasContext) {
71
- return (
72
- getDistanceBetweenPoints(this.outline[0], this.outline[1]) /
24
+ constructor(firstPoint, lastPoint, layer, infiniteLine = false) {
25
+ this.id = uuidv4()
26
+ this.version = 0
27
+ this.type = 'line'
28
+ if (firstPoint && lastPoint) {
29
+ this.outline = [firstPoint, lastPoint]
30
+ } else {
31
+ this.outline = []
32
+ }
33
+ this.layer = layer
34
+ this.visible = true
35
+ this.infiniteLine = infiniteLine
36
+ }
37
+ clone() {
38
+ const line = new Line()
39
+ this.outline.forEach((p) => {
40
+ line.outline.push(new Point(p.x, p.y, p.z))
41
+ })
42
+ line.id = uuidv4()
43
+ line.layer = this.layer
44
+ line.infiniteLine = this.infiniteLine
45
+ return line
46
+ }
47
+ getDirectionVector() {
48
+ return normalizeVector(substractVector(this.outline[1], this.outline[0]))
49
+ }
50
+ getIntersections(object) {
51
+ const intersections = []
52
+ if (!object) return
53
+ if (object.type == 'point') {
54
+ if (isSamePoint3D(this.getProjectedPoint(object), object)) {
55
+ intersections.push({ ...object })
56
+ }
57
+ } else if (object.type == 'line') {
58
+ const data = getDataAboutTwo3DLines(
59
+ this.outline[0],
60
+ this.getDirectionVector(),
61
+ object.outline[0],
62
+ object.getDirectionVector()
63
+ )
64
+ if (!data) {
65
+ return []
66
+ }
67
+ let { distance, N } = data
68
+ if (distance < 0.1 && !!N) {
69
+ intersections.push({ ...N })
70
+ }
71
+ } else if (object.type == 'circle') {
72
+ let P = this.getProjectedPoint(object.center)
73
+ let distance = get3DDistanceBetweenPoints(P, object.center)
74
+ if (distance <= object.radius) {
75
+ let A = addVector(
76
+ P,
77
+ multiplyVector(
78
+ Math.sqrt(Math.pow(object.radius, 2) - Math.pow(distance, 2)),
79
+ this.getDirectionVector()
80
+ )
81
+ )
82
+ let B = addVector(
83
+ P,
84
+ multiplyVector(
85
+ -Math.sqrt(Math.pow(object.radius, 2) - Math.pow(distance, 2)),
86
+ this.getDirectionVector()
87
+ )
88
+ )
89
+ intersections.push({ ...A })
90
+ intersections.push({ ...B })
91
+ }
92
+ }
93
+ return intersections
94
+ }
95
+ pxLength(canvasContext) {
96
+ return (
97
+ getDistanceBetweenPoints(this.outline[0], this.outline[1]) /
73
98
  canvasContext.mmPerPx
74
- )
75
- }
76
- mmLength() {
77
- return get3DDistanceBetweenPoints(this.outline[0], this.outline[1])
78
- }
79
-
80
- getMidPoint() {
81
- return midPoint(this.outline[0], this.outline[1])
82
- }
83
- getAngleDeg(){
84
- const angle = Math.atan2(
85
- this.outline[1].y - this.outline[0].y,
86
- this.outline[0].x - this.outline[1].x
87
- )
88
- return (angle * 180) / Math.PI
89
- }
90
- getPxDistanceTo(point, canvasContext) {
91
- const toCanvasRef = canvasContext.toCanvasRef
92
- const toRealityRef = canvasContext.toRealityRef
99
+ )
100
+ }
101
+ mmLength() {
102
+ return get3DDistanceBetweenPoints(this.outline[0], this.outline[1])
103
+ }
104
+
105
+ getMidPoint() {
106
+ return midPoint(this.outline[0], this.outline[1])
107
+ }
108
+ getAngleDeg() {
109
+ const angle = Math.atan2(
110
+ this.outline[1].y - this.outline[0].y,
111
+ this.outline[0].x - this.outline[1].x
112
+ )
113
+ return (angle * 180) / Math.PI
114
+ }
115
+ getPxDistanceTo(point, canvasContext) {
116
+ const toCanvasRef = canvasContext.toCanvasRef
117
+ const toRealityRef = canvasContext.toRealityRef
93
118
 
94
- if (this.outline.length < 2) {
95
- console.error('line undefined')
96
- return null
97
- }
98
- if (isSamePoint3D(this.outline[0], this.outline[1])) {
99
- console.error('line undefined')
100
- return null
101
- }
119
+ if (this.outline.length < 2) {
120
+ console.error('line undefined')
121
+ return null
122
+ }
123
+ if (isSamePoint3D(this.outline[0], this.outline[1])) {
124
+ console.error('line undefined')
125
+ return null
126
+ }
102
127
 
103
- let realityRefPoint = toRealityRef(point)
104
- let A={x:this.outline[0].x,y:this.outline[0].y,z:0}
105
- let B={x:this.outline[1].x,y:this.outline[1].y,z:0}
106
- let M={x:realityRefPoint.x,y:realityRefPoint.y,z:0}
107
- let P = getPointOnLine(M, A, B)
108
- P.z = 0
109
- if(!this.infiniteLine && !isPointBetweenSegment(P,A,B)){
110
- return (Math.min(getDistanceBetweenPoints(M, A),getDistanceBetweenPoints(M, B))) / canvasContext.mmPerPx
111
- }
112
- return getDistanceBetweenPoints(M, P) / canvasContext.mmPerPx
113
- }
114
- isInPlane(plane){
115
- if(!plane) return false
116
- const {point,normalVector}=plane
117
- if(dotProduct(this.getDirectionVector(),normalVector)>0.1) return false
118
- if(dotProduct(normalizeVector(substractVector(point,this.outline[0])),normalVector)>0.1) return false
119
- return true
120
- }
121
- includesPoint2D(point,tolerance){
122
- const projection=this.getVerticalProjectedPoint(point)
123
- return this.getHorizontalDistanceToPoint(point) < tolerance && isPointBetweenSegment( projection, this.outline[0], this.outline[1])
124
- }
125
- getDistanceToPoint(point) {
126
- return get3DDistanceBetweenPoints(point, this.getProjectedPoint(point))
127
- }
128
- getHorizontalDistanceToPoint(point) {
129
- return getDistanceBetweenPoints(point, this.getVerticalProjectedPoint(point))
130
- }
131
- getVerticalProjectedPoint(point){
132
- if (this.outline.length == 0) {
133
- return null
134
- }
135
- if (isSamePoint3D(this.outline[0], this.outline[1])) {
136
- console.error("A == B Can't project point on Line")
137
- return null
138
- }
139
- let A= {...this.outline[0],z:0}
140
- let B= {...this.outline[1],z:0}
141
- let P = getPointOnLine({...point,z:0}, A,B)
142
- let AP = substractVector(P,A)
143
- let AB = substractVector(B,A)
144
- let k=dotProduct(AP,AB)/dotProduct(AB,AB)
145
- let zP=this.outline[0].z+k*(this.outline[1].z-this.outline[0].z)
146
- P.z=zP
147
- return P
148
- }
149
- getProjectedPoint(point) {
150
- if (this.outline.length == 0) {
151
- return null
152
- }
153
- if (isSamePoint3D(this.outline[0], this.outline[1])) {
154
- console.error("A == B Can't project point on Line")
155
- return null
156
- }
157
- let P = getPointOnLine(point, this.outline[0], this.outline[1])
158
- return P
159
- }
160
- translate(vectorInMm) {
161
- this.outline = this.outline.map((point) => {
162
- return translate2D(point, vectorInMm)
163
- })
164
- }
165
- serialize() {
166
- if (!this.belongsTo) {
167
- this.belongsTo = []
168
- }
169
- const belongsTo = this.belongsTo.map((item) => {
170
- return {
171
- polygon: item.polygon.serialize(),
172
- polygonId: item.polygonId,
173
- index: item.index
174
- }
175
- })
176
- const serializedEdge={
177
- outline: [this.outline[0], this.outline[1]],
178
- layer: this.layer,
179
- type: this.type,
180
- belongsTo: belongsTo || [],
181
- itemType: this.itemType
182
- }
183
- return JSON.parse(JSON.stringify(serializedEdge))
184
- }
185
- }
128
+ let realityRefPoint = toRealityRef(point)
129
+ let A = { x: this.outline[0].x, y: this.outline[0].y, z: 0 }
130
+ let B = { x: this.outline[1].x, y: this.outline[1].y, z: 0 }
131
+ let M = { x: realityRefPoint.x, y: realityRefPoint.y, z: 0 }
132
+ let P = getPointOnLine(M, A, B)
133
+ P.z = 0
134
+ if (!this.infiniteLine && !isPointBetweenSegment(P, A, B)) {
135
+ return (
136
+ Math.min(
137
+ getDistanceBetweenPoints(M, A),
138
+ getDistanceBetweenPoints(M, B)
139
+ ) / canvasContext.mmPerPx
140
+ )
141
+ }
142
+ return getDistanceBetweenPoints(M, P) / canvasContext.mmPerPx
143
+ }
144
+ isInPlane(plane) {
145
+ if (!plane) return false
146
+ const { point, normalVector } = plane
147
+ if (dotProduct(this.getDirectionVector(), normalVector) > 0.1) return false
148
+ if (
149
+ dotProduct(
150
+ normalizeVector(substractVector(point, this.outline[0])),
151
+ normalVector
152
+ ) > 0.1
153
+ )
154
+ return false
155
+ return true
156
+ }
157
+ includesPoint2D(point, tolerance) {
158
+ const projection = this.getVerticalProjectedPoint(point)
159
+ return (
160
+ this.getHorizontalDistanceToPoint(point) < tolerance &&
161
+ isPointBetweenSegment(projection, this.outline[0], this.outline[1])
162
+ )
163
+ }
164
+ getDistanceToPoint(point) {
165
+ return get3DDistanceBetweenPoints(point, this.getProjectedPoint(point))
166
+ }
167
+ getHorizontalDistanceToPoint(point) {
168
+ return getDistanceBetweenPoints(
169
+ point,
170
+ this.getVerticalProjectedPoint(point)
171
+ )
172
+ }
173
+ getVerticalProjectedPoint(point) {
174
+ if (this.outline.length == 0) {
175
+ return null
176
+ }
177
+ if (isSamePoint3D(this.outline[0], this.outline[1])) {
178
+ console.error('A == B Can\'t project point on Line')
179
+ return null
180
+ }
181
+ let A = { ...this.outline[0], z: 0 }
182
+ let B = { ...this.outline[1], z: 0 }
183
+ let P = getPointOnLine({ ...point, z: 0 }, A, B)
184
+ let AP = substractVector(P, A)
185
+ let AB = substractVector(B, A)
186
+ let k = dotProduct(AP, AB) / dotProduct(AB, AB)
187
+ let zP = this.outline[0].z + k * (this.outline[1].z - this.outline[0].z)
188
+ P.z = zP
189
+ return P
190
+ }
191
+ getProjectedPoint(point) {
192
+ if (this.outline.length == 0) {
193
+ return null
194
+ }
195
+ if (isSamePoint3D(this.outline[0], this.outline[1])) {
196
+ console.error('A == B Can\'t project point on Line')
197
+ return null
198
+ }
199
+ let P = getPointOnLine(point, this.outline[0], this.outline[1])
200
+ return P
201
+ }
202
+ isEffectivelySplit(polygons) {
203
+ if (this.belongsTo.length != 2) {
204
+ return false
205
+ }
206
+ const polygon0 = polygons.find((p) => p.id == this.belongsTo[0].polygonId)
207
+ const polygon1 = polygons.find((p) => p.id == this.belongsTo[1].polygonId)
208
+ if(!polygon0 || !polygon1){
209
+ return false
210
+ }
211
+ let A, B, C, D
212
+ if (this.belongsTo[0].index !== null) {
213
+ A = polygon0.outline[this.belongsTo[0].index]
214
+ B =
215
+ polygon0.outline[
216
+ (this.belongsTo[0].index + 1) % polygon0.outline.length
217
+ ]
218
+ }
219
+ if (this.belongsTo[1].index !== null) {
220
+ C = polygon1.outline[this.belongsTo[1].index]
221
+ D =
222
+ polygon1.outline[
223
+ (this.belongsTo[1].index + 1) % polygon1.outline.length
224
+ ]
225
+ }
226
+ if (!A || !B) {
227
+ A = verticalProjectionOnPlane(
228
+ C,
229
+ polygon0.normalVector,
230
+ polygon0.flatOutline[0]
231
+ )
232
+ B = verticalProjectionOnPlane(
233
+ D,
234
+ polygon0.normalVector,
235
+ polygon0.flatOutline[0]
236
+ )
237
+ }
238
+ if (!C || !D) {
239
+ C = verticalProjectionOnPlane(
240
+ A,
241
+ polygon1.normalVector,
242
+ polygon1.flatOutline[0]
243
+ )
244
+ D = verticalProjectionOnPlane(
245
+ B,
246
+ polygon1.normalVector,
247
+ polygon1.flatOutline[0]
248
+ )
249
+ }
250
+ if (isSamePoint2D(A, C)) {
251
+ const isSplit = Math.abs(A.z - C.z) > 10 || Math.abs(B.z - D.z) > 10
252
+ return isSplit
253
+ } else {
254
+ const isSplit = Math.abs(A.z - D.z) > 10 || Math.abs(B.z - C.z) > 10
255
+ return isSplit
256
+ }
257
+ }
258
+ translate(vectorInMm) {
259
+ this.outline = this.outline.map((point) => {
260
+ return translate2D(point, vectorInMm)
261
+ })
262
+ }
263
+ serialize() {
264
+ if (!this.belongsTo) {
265
+ this.belongsTo = []
266
+ }
267
+ const belongsTo = this.belongsTo.map((item) => {
268
+ return {
269
+ polygon: item.polygon.serialize(),
270
+ polygonId: item.polygonId,
271
+ index: item.index
272
+ }
273
+ })
274
+ const serializedEdge = {
275
+ outline: [this.outline[0], this.outline[1]],
276
+ layer: this.layer,
277
+ type: this.type,
278
+ belongsTo: belongsTo || [],
279
+ itemType: this.itemType
280
+ }
281
+ return JSON.parse(JSON.stringify(serializedEdge))
282
+ }
283
+ }
@@ -1,12 +1,11 @@
1
-
2
1
  import { v4 as uuidv4 } from 'uuid'
3
2
  import {
4
3
  getDistanceBetweenPoints,
5
4
  get3DDistanceBetweenPoints,
6
5
  isSamePoint3D,
7
- translate2D,
6
+ translate2D
8
7
  } from '../geometry'
9
- import { substractVector,dotProduct } from '../vector'
8
+ import { substractVector, dotProduct } from '../vector'
10
9
 
11
10
  export class Point {
12
11
  constructor(x = 0, y = 0, z = 0, layer = null) {
@@ -21,21 +20,21 @@ export class Point {
21
20
  this.selected = false
22
21
  this.visible = true
23
22
  this.hasWarning = false
24
- }
25
- getIntersections(object){
26
- const intersections=[]
27
- if(!object)return
28
- if(object.type=="line"){
29
- if(isSamePoint3D(object.getProjectedPoint(this),this)){
30
- intersections.push({...this})
23
+ }
24
+ getIntersections(object) {
25
+ const intersections = []
26
+ if (!object) return
27
+ if (object.type == 'line') {
28
+ if (isSamePoint3D(object.getProjectedPoint(this), this)) {
29
+ intersections.push({ ...this })
30
+ }
31
+ } else if (object.type == 'point') {
32
+ if (isSamePoint3D(object, this)) {
33
+ intersections.push({ ...object })
31
34
  }
32
- }else if(object.type=="point"){
33
- if(isSamePoint3D(object,this)){
34
- intersections.push({...object})
35
- }
36
- }else if(object.type=="circle"){
37
- if(get3DDistanceBetweenPoints(this,object.center)==object.radius){
38
- intersections.push({...this})
35
+ } else if (object.type == 'circle') {
36
+ if (get3DDistanceBetweenPoints(this, object.center) == object.radius) {
37
+ intersections.push({ ...this })
39
38
  }
40
39
  }
41
40
  return intersections
@@ -49,20 +48,24 @@ export class Point {
49
48
  return this
50
49
  }
51
50
  getVerticalProjectedPoint(point) {
52
- return this
51
+ return this
53
52
  }
54
53
  getDistanceToPoint(point) {
55
54
  return get3DDistanceBetweenPoints(point, this.getProjectedPoint(point))
56
- }
57
- getHorizontalDistanceToPoint(point) {
58
- return getDistanceBetweenPoints(point, this.getVerticalProjectedPoint(point))
59
55
  }
60
- isInPlane(plane){
61
- if(!plane) return false
62
- const {point,normalVector}=plane
63
- if(dotProduct(substractVector(point,this),normalVector)>0.1) return false
56
+ getHorizontalDistanceToPoint(point) {
57
+ return getDistanceBetweenPoints(
58
+ point,
59
+ this.getVerticalProjectedPoint(point)
60
+ )
61
+ }
62
+ isInPlane(plane) {
63
+ if (!plane) return false
64
+ const { point, normalVector } = plane
65
+ if (dotProduct(substractVector(point, this), normalVector) > 0.1)
66
+ return false
64
67
  return true
65
- }
68
+ }
66
69
  translate(vectorInMm) {
67
70
  let translatedPoint = translate2D({ x: this.x, y: this.y }, vectorInMm)
68
71
  this.x = translatedPoint.x
@@ -90,4 +93,4 @@ export class Point {
90
93
  itemType: this.itemType
91
94
  }
92
95
  }
93
- }
96
+ }