TriangleClosestPoint.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. (function() {
  2. THREE = Bench.THREE;
  3. // these vertices and triangles are those of a unit icosahedron centered at the origin
  4. var phi = 1.618;
  5. var verts = [
  6. [phi, 1, 0], [-phi, 1, 0], [phi, -1, 0], [-phi, -1, 0],
  7. [1, 0, phi], [1, 0, -phi], [-1, 0, phi], [-1, 0, -phi],
  8. [0, phi, 1], [0, -phi, 1], [0, phi, -1], [0, -phi, -1],
  9. ];
  10. var createVertex = function(c) {
  11. return new THREE.Vector3(c[0], c[1], c[2]);
  12. };
  13. var createTriangle = function(i0, i1, i2) {
  14. return new THREE.Triangle(createVertex(verts[i0]), createVertex(verts[i1]), createVertex(verts[i2]));
  15. };
  16. var triangles = [
  17. createTriangle(0, 8, 4),
  18. createTriangle(0, 5, 10),
  19. createTriangle(2, 4, 9),
  20. createTriangle(2, 11, 5),
  21. createTriangle(1, 6, 8),
  22. createTriangle(1, 10, 7),
  23. createTriangle(3, 9, 6),
  24. createTriangle(3, 7, 11),
  25. createTriangle(0, 10, 8),
  26. createTriangle(1, 8, 10),
  27. createTriangle(2, 9, 11),
  28. createTriangle(3, 9, 11),
  29. createTriangle(4, 2, 0),
  30. createTriangle(5, 0, 2),
  31. createTriangle(6, 1, 3),
  32. createTriangle(7, 3, 1),
  33. createTriangle(8, 6, 4),
  34. createTriangle(9, 4, 6),
  35. createTriangle(10, 5, 7),
  36. createTriangle(11, 7, 5),
  37. ];
  38. // test a variety of points all in and around the icosahedron
  39. var testPoints = [];
  40. for (var x = -2; x <= 2; x += 0.5) {
  41. for (var y = -2; y <= 2; y += 0.5) {
  42. for (var z = -2; z <= 2; z += 0.5) {
  43. testPoints.push(new THREE.Vector3(x, y, z));
  44. }
  45. }
  46. }
  47. var s = Bench.newSuite("Clamping point into triangles");
  48. s.add('9^3 points, 20 triangles', function() {
  49. var target = new THREE.Vector3();
  50. for (var tidx = 0; tidx < triangles.length; tidx++) {
  51. var triangle = triangles[tidx];
  52. for (var pidx = 0; pidx < testPoints.length; pidx++) {
  53. triangle.closestPointToPoint(testPoints[pidx], target);
  54. }
  55. }
  56. });
  57. })();