modeling/src/maths/plane/fromPointsRandom.js

  1. const { EPS } = require('../constants')
  2. const vec3 = require('../vec3')
  3. /**
  4. * Create a new plane from the given points like fromPoints,
  5. * but allow the vectors to be on one point or one line.
  6. * In such a case, a random plane through the given points is constructed.
  7. *
  8. * @param {plane} out - receiving plane
  9. * @param {vec3} a - 3D point
  10. * @param {vec3} b - 3D point
  11. * @param {vec3} c - 3D point
  12. * @returns {plane} out
  13. * @alias module:modeling/maths/plane.fromPointsRandom
  14. */
  15. const fromPointsRandom = (out, a, b, c) => {
  16. let ba = vec3.subtract(vec3.create(), b, a)
  17. let ca = vec3.subtract(vec3.create(), c, a)
  18. if (vec3.length(ba) < EPS) {
  19. ba = vec3.orthogonal(ba, ca)
  20. }
  21. if (vec3.length(ca) < EPS) {
  22. ca = vec3.orthogonal(ca, ba)
  23. }
  24. let normal = vec3.cross(vec3.create(), ba, ca)
  25. if (vec3.length(normal) < EPS) {
  26. // this would mean that ba == ca.negated()
  27. ca = vec3.orthogonal(ca, ba)
  28. normal = vec3.cross(normal, ba, ca)
  29. }
  30. normal = vec3.normalize(normal, normal)
  31. const w = vec3.dot(normal, a)
  32. out[0] = normal[0]
  33. out[1] = normal[1]
  34. out[2] = normal[2]
  35. out[3] = w
  36. return out
  37. }
  38. module.exports = fromPointsRandom