Skip to content

Commit 98610c9

Browse files
committed
perf(modeling): corrected test of null, and use ternary if when possible
1 parent def21b2 commit 98610c9

File tree

4 files changed

+34
-46
lines changed

4 files changed

+34
-46
lines changed

packages/modeling/src/operations/booleans/trees/Node.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class Node {
2323
let node
2424
for (let i = 0; i < queue.length; i++) {
2525
node = queue[i]
26-
if (node.plane != null) node.plane = plane.flip(plane.create(), node.plane)
27-
if (node.front != null) queue.push(node.front)
28-
if (node.back != null) queue.push(node.back)
26+
if (node.plane !== null) node.plane = plane.flip(plane.create(), node.plane)
27+
if (node.front !== null) queue.push(node.front)
28+
if (node.back !== null) queue.push(node.back)
2929
const temp = node.front
3030
node.front = node.back
3131
node.back = temp
@@ -42,7 +42,7 @@ export class Node {
4242
node = current.node
4343
polygonTreeNodes = current.polygonTreeNodes
4444

45-
if (node.plane != null) {
45+
if (node.plane !== null) {
4646
const plane = node.plane
4747

4848
const backNodes = []
@@ -57,12 +57,12 @@ export class Node {
5757
}
5858
}
5959

60-
if (node.front != null && frontNodes.length > 0) {
60+
if (node.front !== null && frontNodes.length > 0) {
6161
// add front node for further splitting
6262
stack.push({ node: node.front, polygonTreeNodes: frontNodes })
6363
}
6464
const numBackNodes = backNodes.length
65-
if (node.back != null && numBackNodes > 0) {
65+
if (node.back !== null && numBackNodes > 0) {
6666
// add back node for further splitting
6767
stack.push({ node: node.back, polygonTreeNodes: backNodes })
6868
} else {
@@ -84,8 +84,8 @@ export class Node {
8484
if (node.polygontreenodes.length > 0) {
8585
bsptree.clipPolygons(node.polygontreenodes, alsoRemoveCoplanarFront)
8686
}
87-
if (node.front != null) stack.push(node.front)
88-
if (node.back != null) stack.push(node.back)
87+
if (node.front !== null) stack.push(node.front)
88+
if (node.back !== null) stack.push(node.back)
8989
node = stack.pop()
9090
} while (node !== undefined)
9191
}
@@ -102,7 +102,7 @@ export class Node {
102102
current = stack.pop()
103103
continue
104104
}
105-
if (node.plane == null) {
105+
if (node.plane === null) {
106106
let index = 0 // default
107107
index = Math.floor(len / 2)
108108
// index = len >> 1
@@ -117,22 +117,20 @@ export class Node {
117117
}
118118

119119
if (frontNodes.length > 0) {
120-
if (node.front == null) node.front = new Node(node)
120+
if (node.front === null) node.front = new Node(node)
121121

122122
// unable to split by any of the current nodes
123123
const stopCondition = len === frontNodes.length && backNodes.length === 0
124124

125-
if (stopCondition) node.front.polygontreenodes = frontNodes
126-
else stack.push({ node: node.front, polygonTreeNodes: frontNodes })
125+
stopCondition ? node.front.polygontreenodes = frontNodes : stack.push({ node: node.front, polygonTreeNodes: frontNodes })
127126
}
128127
if (backNodes.length > 0) {
129-
if (node.back == null) node.back = new Node(node)
128+
if (node.back === null) node.back = new Node(node)
130129

131130
// unable to split by any of the current nodes
132131
const stopCondition = len === backNodes.length && frontNodes.length === 0
133132

134-
if (stopCondition) node.back.polygontreenodes = backNodes
135-
else stack.push({ node: node.back, polygonTreeNodes: backNodes })
133+
stopCondition ? node.back.polygontreenodes = backNodes : stack.push({ node: node.back, polygonTreeNodes: backNodes })
136134
}
137135

138136
current = stack.pop()

packages/modeling/src/operations/booleans/trees/PolygonTreeNode.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class PolygonTreeNode {
7575
}
7676

7777
getPolygon () {
78-
if (this.polygon == null) throw new Error('PolyTreeNode04')
78+
if (this.polygon === null) throw new Error('PolyTreeNode04')
7979
return this.polygon
8080
}
8181

@@ -90,7 +90,7 @@ export class PolygonTreeNode {
9090
children = queue[i]
9191
for (j = 0, l = children.length; j < l; j++) { // ok to cache length
9292
node = children[j]
93-
if (node.polygon != null) {
93+
if (node.polygon !== null) {
9494
// the polygon hasn't been broken yet. We can ignore the children and return our polygon
9595
result.push(node.polygon)
9696
} else {
@@ -105,7 +105,7 @@ export class PolygonTreeNode {
105105

106106
// NOTE: This version of getPolygons() is much SLOWER.
107107
getPolygonsNew (result) {
108-
if (this.polygon != null) {
108+
if (this.polygon !== null) {
109109
// the polygon hasn't been broken yet, so return the original polygon
110110
result.push(this.polygon)
111111
} else {
@@ -134,15 +134,15 @@ export class PolygonTreeNode {
134134
if (node.children.length > 0) {
135135
queue.push(node.children)
136136
} else {
137-
if (this.polygon != null) {
137+
if (this.polygon !== null) {
138138
// no children. Split the polygon:
139139
node._splitByPlane(plane, coplanarfrontnodes, coplanarbacknodes, frontnodes, backnodes)
140140
}
141141
}
142142
}
143143
}
144144
} else {
145-
if (this.polygon != null) {
145+
if (this.polygon !== null) {
146146
this._splitByPlane(plane, coplanarfrontnodes, coplanarbacknodes, frontnodes, backnodes)
147147
}
148148
}
@@ -156,7 +156,7 @@ export class PolygonTreeNode {
156156
node.splitByPlane(plane, coplanarFrontNodes, coplanarBackNodes, frontNodes, backNodes)
157157
}
158158
} else {
159-
if (this.polygon != null) {
159+
if (this.polygon !== null) {
160160
// the polygon hasn't been split, so split this node by the given plane
161161
this._splitByPlane(plane, coplanarFrontNodes, coplanarBackNodes, frontNodes, backNodes)
162162
}
@@ -206,11 +206,11 @@ export class PolygonTreeNode {
206206

207207
case 4:
208208
// spanning:
209-
if (splitResult.front != null) {
209+
if (splitResult.front !== null) {
210210
const frontNode = this.addChild(splitResult.front)
211211
frontNodes.push(frontNode)
212212
}
213-
if (splitResult.back != null) {
213+
if (splitResult.back !== null) {
214214
const backNode = this.addChild(splitResult.back)
215215
backNodes.push(backNode)
216216
}
@@ -238,7 +238,7 @@ export class PolygonTreeNode {
238238
children = queue[i]
239239
for (j = 0, l = children.length; j < l; j++) {
240240
node = children[j]
241-
if (node.polygon != null) {
241+
if (node.polygon !== null) {
242242
node.polygon = poly3.invert(node.polygon)
243243
}
244244
if (node.children.length > 0) queue.push(node.children)
@@ -248,7 +248,7 @@ export class PolygonTreeNode {
248248

249249
// NOTE: This verison is SLOWER
250250
_invertSubNew () {
251-
if (this.polygon != null) {
251+
if (this.polygon !== null) {
252252
this.polygon = poly3.invert(this.polygon)
253253
}
254254
for (let i = 0; i < this.children.length; i++) {
@@ -262,7 +262,7 @@ export class PolygonTreeNode {
262262
// called to invalidate parents of removed nodes
263263
_recursivelyInvalidatePolygon () {
264264
this.polygon = null
265-
if (this.parent != null) {
265+
if (this.parent !== null) {
266266
this.parent._recursivelyInvalidatePolygon()
267267
}
268268
}
@@ -275,7 +275,7 @@ export class PolygonTreeNode {
275275
}
276276
this.children.length = 0
277277
// unlink polygon
278-
if (this.polygon != null) {
278+
if (this.polygon !== null) {
279279
this.polygon = null
280280
}
281281
// unlink parent
@@ -293,7 +293,7 @@ export class PolygonTreeNode {
293293
for (j = 0, l = children.length; j < l; j++) { // ok to cache length
294294
node = children[j]
295295
result += `${prefix}PolygonTreeNode (${node.isRootNode()}): ${node.children.length}`
296-
if (node.polygon != null) {
296+
if (node.polygon !== null) {
297297
result += `\n ${prefix}polygon: ${node.polygon.vertices}\n`
298298
} else {
299299
result += '\n'

packages/modeling/src/operations/booleans/trees/splitLineSegmentByPlane.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import * as vec3 from '../../../maths/vec3/index.js'
33
export const splitLineSegmentByPlane = (plane, p1, p2) => {
44
const direction = vec3.subtract(vec3.create(), p2, p1)
55
let lambda = (plane[3] - vec3.dot(plane, p1)) / vec3.dot(plane, direction)
6-
if (Number.isNaN(lambda)) lambda = 0
7-
if (lambda > 1) lambda = 1
8-
if (lambda < 0) lambda = 0
6+
7+
Number.isNaN(lambda) ? lambda = 0
8+
: lambda > 1 ? lambda = 1
9+
: lambda < 0 ? lambda = 0
10+
: lambda
911

1012
vec3.scale(direction, direction, lambda)
1113
vec3.add(direction, p1, direction)

packages/modeling/src/operations/booleans/trees/splitPolygonByPlane.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ export const splitPolygonByPlane = (result, splane, polygon) => {
6969
const nextIsBack = vertexIsBack[nextVertexIndex]
7070
if (isback === nextIsBack) {
7171
// line segment is on one side of the plane
72-
if (isback) {
73-
backVertices.push(vertex)
74-
} else {
75-
frontVertices.push(vertex)
76-
}
72+
isback ? backVertices.push(vertex) : frontVertices.push(vertex)
7773
} else {
7874
// line segment spans the plane
7975
const nextPoint = vertices[nextVertexIndex]
@@ -119,17 +115,9 @@ export const splitPolygonByPlane = (result, splane, polygon) => {
119115
// assemble the result
120116
result.type = 4
121117

122-
if (frontVertices.length >= 3) {
123-
result.front = poly3.fromVerticesAndPlane(frontVertices, pplane)
124-
} else {
125-
result.front = null
126-
}
118+
frontVertices.length >= 3 ? result.front = poly3.fromVerticesAndPlane(frontVertices, pplane) : result.front = null
127119

128-
if (backVertices.length >= 3) {
129-
result.back = poly3.fromVerticesAndPlane(backVertices, pplane)
130-
} else {
131-
result.back = null
132-
}
120+
backVertices.length >= 3 ? result.back = poly3.fromVerticesAndPlane(backVertices, pplane) : result.back = null
133121
}
134122
}
135123
return result

0 commit comments

Comments
 (0)