Skip to content

Commit f36152a

Browse files
committed
perf(modeling): small performance changes to plane.from functions
1 parent 5718fab commit f36152a

3 files changed

Lines changed: 15 additions & 14 deletions

File tree

packages/modeling/src/maths/plane/fromNormalAndPoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as vec3 from '../vec3/index.js'
1818
* @alias module:modeling/maths/plane.fromNormalAndPoint
1919
*/
2020
export const fromNormalAndPoint = (out, normal, point) => {
21-
const u = vec3.normalize(vec3.create(), normal)
21+
const u = vec3.normalize(out, normal)
2222
const w = vec3.dot(point, u)
2323

2424
out[0] = u[0]

packages/modeling/src/maths/plane/fromPoints.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ export const fromPoints = (out, ...vertices) => {
2626
return ba
2727
}
2828

29-
out[0] = 0
30-
out[1] = 0
31-
out[2] = 0
3229
if (len === 3) {
3330
// optimization for triangles, which are always coplanar
3431
vec3.copy(out, vertexNormal(0))
3532
} else {
3633
// sum of vertex normals
34+
out[0] = 0
35+
out[1] = 0
36+
out[2] = 0
3737
vertices.forEach((v, i) => {
3838
vec3.add(out, out, vertexNormal(i))
3939
})

packages/modeling/src/maths/plane/fromPointsRandom.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ export const fromPointsRandom = (out, a, b, c) => {
1818
let ba = vec3.subtract(vec3.create(), b, a)
1919
let ca = vec3.subtract(vec3.create(), c, a)
2020
if (vec3.length(ba) < EPS) {
21-
ba = vec3.orthogonal(ba, ca)
21+
vec3.orthogonal(ba, ca)
2222
}
2323
if (vec3.length(ca) < EPS) {
24-
ca = vec3.orthogonal(ca, ba)
24+
vec3.orthogonal(ca, ba)
2525
}
26-
let normal = vec3.cross(vec3.create(), ba, ca)
26+
27+
// calculate plane normal
28+
let normal = vec3.cross(out, ba, ca)
2729
if (vec3.length(normal) < EPS) {
2830
// this would mean that ba == ca.negated()
29-
ca = vec3.orthogonal(ca, ba)
30-
normal = vec3.cross(normal, ba, ca)
31+
vec3.orthogonal(ca, ba)
32+
vec3.cross(normal, ba, ca)
3133
}
32-
normal = vec3.normalize(normal, normal)
33-
const w = vec3.dot(normal, a)
34+
vec3.normalize(normal, normal)
3435

35-
out[0] = normal[0]
36-
out[1] = normal[1]
37-
out[2] = normal[2]
36+
// and distance
37+
const w = vec3.dot(normal, a)
3838
out[3] = w
39+
3940
return out
4041
}

0 commit comments

Comments
 (0)