Face.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { HalfEdge } from './HalfEdge';
  2. import { Vector3 } from '../Vector3';
  3. import { Triangle } from '../Triangle';
  4. import { Visible, Deleted } from '../../constants';
  5. /**
  6. * @author Mugen87 / https://github.com/Mugen87
  7. *
  8. */
  9. function Face() {
  10. this.normal = new Vector3();
  11. this.midpoint = new Vector3();
  12. this.area = 0;
  13. this.constant = 0; // signed distance from face to the origin
  14. this.outside = null; // reference to a vertex in a vertex list this face can see
  15. this.mark = Visible;
  16. this.edge = null;
  17. }
  18. Object.assign( Face, {
  19. create: function( a, b, c ) {
  20. var face = new Face();
  21. var e0 = new HalfEdge( a, face );
  22. var e1 = new HalfEdge( b, face );
  23. var e2 = new HalfEdge( c, face );
  24. // join edges
  25. e0.next = e2.prev = e1;
  26. e1.next = e0.prev = e2;
  27. e2.next = e1.prev = e0;
  28. // main half edge reference
  29. face.edge = e0;
  30. return face.compute();
  31. }
  32. } );
  33. Object.assign( Face.prototype, {
  34. getEdge: function ( i ) {
  35. var edge = this.edge;
  36. while ( i > 0 ) {
  37. edge = edge.next;
  38. i --;
  39. }
  40. while ( i < 0 ) {
  41. edge = edge.prev;
  42. i ++;
  43. }
  44. return edge;
  45. },
  46. compute: function () {
  47. var triangle;
  48. return function compute () {
  49. if ( triangle === undefined ) triangle = new Triangle();
  50. var a = this.edge.tail();
  51. var b = this.edge.head();
  52. var c = this.edge.next.head();
  53. triangle.set( a.point, b.point, c.point );
  54. triangle.normal( this.normal );
  55. triangle.midpoint( this.midpoint );
  56. this.area = triangle.area();
  57. this.constant = this.normal.dot( this.midpoint );
  58. return this;
  59. };
  60. }(),
  61. distanceToPoint: function ( point ) {
  62. return this.normal.dot( point ) - this.constant;
  63. }
  64. } );
  65. export { Face };