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.

78 lines
2.9 KiB

  1. import Sphere from "../src/05/sphere";
  2. import Intersection from "../src/05/intersection";
  3. import { assert, expect } from 'chai';
  4. import Vector from "../src/05/vector";
  5. import Ray from "../src/05/ray";
  6. describe('Sphere', () => {
  7. it('can be initialized with center, radius and color', () => {
  8. const s: Sphere = new Sphere(new Vector(0, 0, 0, 1), 1, new Vector(0, 0, 0, 0));
  9. expect(s).to.be.an('object');
  10. });
  11. it('a sphere at origin and radius 1 can be intersected correctly with the x axis', () => {
  12. const s: Sphere = new Sphere(new Vector(0, 0, 0, 1), 1, new Vector(0, 0, 0, 0));
  13. const i: Intersection = s.intersect(new Ray(new Vector(-10, 0, 0, 1), new Vector(1, 0, 0, 0)));
  14. expect(s).to.be.an('object');
  15. expect(i).to.be.an('object');
  16. expect(i.point.x).to.equal(-1);
  17. expect(i.point.y).to.equal(0);
  18. expect(i.point.z).to.equal(0);
  19. });
  20. it('a sphere at origin and radius 1 can be intersected correctly with the y axis', () => {
  21. const s: Sphere = new Sphere(new Vector(0, 0, 0, 1), 1, new Vector(0, 0, 0, 0));
  22. const i: Intersection = s.intersect(new Ray(new Vector(0, -10, 0, 1), new Vector(0, 1, 0, 0)));
  23. expect(s).to.be.an('object');
  24. expect(i).to.be.an('object');
  25. expect(i.point.x).to.equal(0);
  26. expect(i.point.y).to.equal(-1);
  27. expect(i.point.z).to.equal(0);
  28. });
  29. it('a sphere at origin and radius 1 can be intersected correctly with the z axis', () => {
  30. const s: Sphere = new Sphere(new Vector(0, 0, 0, 1), 1, new Vector(0, 0, 0, 0));
  31. const i: Intersection = s.intersect(new Ray(new Vector(0, 0, -10, 1), new Vector(0, 0, 1, 0)));
  32. expect(s).to.be.an('object');
  33. expect(i).to.be.an('object');
  34. expect(i.point.x).to.equal(0);
  35. expect(i.point.y).to.equal(0);
  36. expect(i.point.z).to.equal(-1);
  37. });
  38. it('intersection is correct when radius != 1', () => {
  39. const s: Sphere = new Sphere(new Vector(0, 0, 0, 1), 2.5, new Vector(0, 0, 0, 0));
  40. const i: Intersection = s.intersect(new Ray(new Vector(-10, 0, 0, 1), new Vector(1, 0, 0, 0)));
  41. expect(s).to.be.an('object');
  42. expect(i).to.be.an('object');
  43. expect(i.point.x).to.equal(-2.5);
  44. expect(i.point.y).to.equal(0);
  45. expect(i.point.z).to.equal(0);
  46. expect(i.t).to.equal(7.5);
  47. });
  48. it('intersection is correct when center != (0, 0, 0, 1)', () => {
  49. const s: Sphere = new Sphere(new Vector(1, 0, 0, 1), 2.5, new Vector(0, 0, 0, 0));
  50. const i: Intersection = s.intersect(new Ray(new Vector(-10, 0, 0, 1), new Vector(1, 0, 0, 0)));
  51. expect(s).to.be.an('object');
  52. expect(i).to.be.an('object');
  53. expect(i.point.x).to.equal(-1.5);
  54. expect(i.point.y).to.equal(0);
  55. expect(i.point.z).to.equal(0);
  56. expect(i.t).to.equal(8.5);
  57. });
  58. });
  59. // TODO: Test normalization of normal