You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
3.4 KiB

  1. import Ray from "../src/05/ray";
  2. import Camera from "../src/05/camera";
  3. import { assert, expect } from 'chai';
  4. import { GroupNode, SphereNode } from "../src/08/nodes";
  5. import { Translation, Rotation } from "../src/08/transformation";
  6. import Vector from "../src/05/vector";
  7. import Sphere from "../src/05/sphere";
  8. import Intersection from "../src/05/intersection";
  9. import { Traversal } from "../src/08/traversal";
  10. describe('Traversal', () => {
  11. it('Moved & rotated SphereNode can be intersected correctly', () => {
  12. let t: GroupNode = new GroupNode(new Translation(new Vector(0.0, 0.0, -5.0, 0.0)));
  13. let r: GroupNode = new GroupNode(new Rotation(new Vector(0.0, 1.0, 0.0, 0.0), Math.PI / 180 * 90));
  14. t.add(r);
  15. let s: SphereNode = new SphereNode(new Translation(new Vector(0.0, 0.0, 0.0, 0.0)), new Vector(0.0, 0.0, 0.0, 0.0));
  16. r.add(s);
  17. let ray: Ray = new Ray(new Vector(0.0, 0.0, 0.0, 1.0), new Vector(0.0, 0.0, -1.0, 0.0));
  18. let intersection = new Array<Intersection>();
  19. let intersectionObjects = new Array<SphereNode>();
  20. Traversal.traverse(t, ray, t.transform, intersection, intersectionObjects);
  21. // rotation should not move sphere
  22. expect(intersection.length).to.equal(1);
  23. expect(intersection[0].point.x).to.be.closeTo(0, 0.00001);
  24. expect(intersection[0].point.y).to.be.closeTo(0, 0.00001);
  25. expect(intersection[0].point.z).to.be.closeTo(-4, 0.00001);
  26. expect(intersection[0].point.w).to.be.closeTo(1, 0.00001);
  27. r.children.pop();
  28. let s1: SphereNode = new SphereNode(new Translation(new Vector(1.0, 0.0, 0.0, 0.0)), new Vector(0.0, 0.0, 0.0, 0.0));
  29. r.add(s1);
  30. intersection = new Array<Intersection>();
  31. intersectionObjects = new Array<SphereNode>();
  32. Traversal.traverse(t, ray, t.transform, intersection, intersectionObjects);
  33. // rotating 90° around y and translating in x direction should move in z direction
  34. expect(intersection.length).to.equal(1);
  35. expect(intersection[0].point.x).to.be.closeTo(0, 0.00001);
  36. expect(intersection[0].point.y).to.be.closeTo(0, 0.00001);
  37. expect(intersection[0].point.z).to.be.closeTo(-5, 0.00001);
  38. expect(intersection[0].point.w).to.be.closeTo(1, 0.00001);
  39. t.children.pop();
  40. r = new GroupNode(new Rotation(new Vector(0.0, 0.0, 1.0, 0.0), Math.PI / 180 * 90));
  41. t.add(r);
  42. let s2 = new SphereNode(new Translation(new Vector(0.999999999999, 0.0, 0.0, 0.0)), new Vector(0.0, 0.0, 0.0, 0.0));
  43. r.add(s2);
  44. intersection = new Array<Intersection>();
  45. intersectionObjects = new Array<SphereNode>();
  46. Traversal.traverse(t, ray, t.transform, intersection, intersectionObjects);
  47. // rotating 90° around z and translating in x direction should move in y direction
  48. expect(intersection.length).to.equal(1);
  49. expect(intersection[0].point.x).to.be.closeTo(0, 0.00001);
  50. expect(intersection[0].point.y).to.be.closeTo(0, 0.00001);
  51. expect(intersection[0].point.z).to.be.closeTo(-5, 0.00001);
  52. expect(intersection[0].point.w).to.be.closeTo(1, 0.00001);
  53. expect(intersection[0].normal.x).to.be.closeTo(0, 0.00001);
  54. expect(intersection[0].normal.y).to.be.closeTo(-1, 0.00001);
  55. expect(intersection[0].normal.z).to.be.closeTo(0, 0.00001);
  56. expect(intersection[0].normal.w).to.be.closeTo(0, 0.00001);
  57. });
  58. });