Size: 2608 bytes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// cs/renderer/shapes/sphere_test.cc
#include "cs/renderer/shapes/sphere.hh"

#include "cs/renderer/math/constants.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using p3 = ::cs::renderer::geo::Point3;
using v3 = ::cs::renderer::geo::Vector3;
using r3 = ::cs::renderer::geo::Ray3;

TEST(Sphere, IntersectionOnXAxis) {
  r3 ray(p3(0, 0, 0), p3(1, 0, 0));
  cs::renderer::shapes::Sphere sphere(p3(5, 0, 0), 0.5);

  p3 intersection;
  v3 normal;
  EXPECT_TRUE(
      sphere.intersected_by(ray, &intersection, &normal));

  EXPECT_EQ(intersection, p3(4.5, 0, 0));
  EXPECT_EQ(normal, v3(p3(-1, 0, 0)));
}

TEST(Sphere, IntersectionOnYAxis) {
  r3 ray(p3(0, 0, 0), p3(0, 1, 0));
  cs::renderer::shapes::Sphere sphere(p3(0, 5, 0), 1);

  p3 intersection;
  v3 normal;
  EXPECT_TRUE(
      sphere.intersected_by(ray, &intersection, &normal));

  EXPECT_EQ(intersection, p3(0, 4, 0));
  EXPECT_EQ(normal, v3(p3(0, -1, 0)));
}

TEST(Sphere, IntersectionOnZAxis) {
  r3 ray(p3(0, 0, 0), p3(0, 0, 1));
  cs::renderer::shapes::Sphere sphere(p3(0, 0, 5), 1);

  p3 intersection;
  v3 normal;
  EXPECT_TRUE(
      sphere.intersected_by(ray, &intersection, &normal));

  EXPECT_EQ(intersection, p3(0, 0, 4));
  EXPECT_EQ(normal, v3(p3(0, 0, -1)));
}

TEST(Sphere, IntersectionOnXYPlane) {
  r3 ray(p3(0, 0, 0), p3(1, 1, 0));
  cs::renderer::shapes::Sphere sphere(p3(5, 5, 0), 1);

  p3 intersection;
  v3 normal;
  EXPECT_TRUE(
      sphere.intersected_by(ray, &intersection, &normal));

  EXPECT_EQ(intersection,
            p3(5 - sqrtf(2) / 2, 5 - sqrtf(2) / 2, 0));
  EXPECT_EQ(normal,
            v3(p3(-sqrtf(2) / 2, -sqrtf(2) / 2, 0)));
}

TEST(Sphere, IntersectionInQuadrantOne) {
  r3 ray(p3(0, 0, 0), p3(1, 1, 1));
  cs::renderer::shapes::Sphere sphere(p3(5, 5, 5), 1);

  p3 intersection;
  v3 normal;
  EXPECT_TRUE(
      sphere.intersected_by(ray, &intersection, &normal));

  EXPECT_EQ(intersection,
            p3(5 - sqrtf(3) / 3, 5 - sqrtf(3) / 3,
               5 - sqrtf(3) / 3));
  EXPECT_EQ(normal, v3(p3(-sqrtf(3) / 3, -sqrtf(3) / 3,
                          -sqrtf(3) / 3)));
}

TEST(Sphere, RejectsNonUnitRay) {
  r3 ray(p3(0, 0, 0), p3(1, 0, 0));
  ray.direction = v3(2.0f, 0.0f, 0.0f);
  cs::renderer::shapes::Sphere sphere(p3(5, 0, 0), 1);
  p3 intersection;
  v3 normal;
  EXPECT_FALSE(
      sphere.intersected_by(ray, &intersection, &normal));
}

TEST(Sphere, RejectsMissingSphere) {
  r3 ray(p3(0, 0, 0), p3(1, 0, 0));
  cs::renderer::shapes::Sphere sphere(p3(0, 0, 5), 1);
  p3 intersection;
  v3 normal;
  EXPECT_FALSE(
      sphere.intersected_by(ray, &intersection, &normal));
}
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.