From 672dc6af8be868ad1c2dd0a2b2af601a7891baa3 Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Sat, 3 Aug 2024 02:11:35 +0200 Subject: [PATCH] refactor Shape --- .../java/eu/jonahbauer/raytracing/Main.java | 4 ++-- .../jonahbauer/raytracing/render/Camera.java | 5 +++-- .../{shape => scene}/HitResult.java | 2 +- .../{shape/Shape.java => scene/Hittable.java} | 4 ++-- .../raytracing/{shape => scene}/Scene.java | 21 +++++++++---------- .../raytracing/{shape => scene}/Sphere.java | 4 ++-- .../{shape => scene}/SphereTest.java | 2 +- 7 files changed, 21 insertions(+), 21 deletions(-) rename src/main/java/eu/jonahbauer/raytracing/{shape => scene}/HitResult.java (91%) rename src/main/java/eu/jonahbauer/raytracing/{shape/Shape.java => scene/Hittable.java} (84%) rename src/main/java/eu/jonahbauer/raytracing/{shape => scene}/Scene.java (57%) rename src/main/java/eu/jonahbauer/raytracing/{shape => scene}/Sphere.java (96%) rename src/test/java/eu/jonahbauer/raytracing/{shape => scene}/SphereTest.java (94%) diff --git a/src/main/java/eu/jonahbauer/raytracing/Main.java b/src/main/java/eu/jonahbauer/raytracing/Main.java index deabfbf..b95a834 100644 --- a/src/main/java/eu/jonahbauer/raytracing/Main.java +++ b/src/main/java/eu/jonahbauer/raytracing/Main.java @@ -2,8 +2,8 @@ package eu.jonahbauer.raytracing; import eu.jonahbauer.raytracing.render.Camera; import eu.jonahbauer.raytracing.render.ImageIO; -import eu.jonahbauer.raytracing.shape.Scene; -import eu.jonahbauer.raytracing.shape.Sphere; +import eu.jonahbauer.raytracing.scene.Scene; +import eu.jonahbauer.raytracing.scene.Sphere; import java.io.IOException; import java.nio.file.Path; diff --git a/src/main/java/eu/jonahbauer/raytracing/render/Camera.java b/src/main/java/eu/jonahbauer/raytracing/render/Camera.java index 7f95397..1a60890 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/Camera.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/Camera.java @@ -1,8 +1,9 @@ package eu.jonahbauer.raytracing.render; +import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Ray; import eu.jonahbauer.raytracing.math.Vec3; -import eu.jonahbauer.raytracing.shape.Scene; +import eu.jonahbauer.raytracing.scene.Scene; import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -52,7 +53,7 @@ public record Camera( var y = pixel.y(); var ray = pixel.ray(); - var result = scene.hit(ray); + var result = scene.hit(ray, Range.NON_NEGATIVE); if (result.isPresent()) { var normal = result.get().normal(); image.set(x, y, getNormalColor(normal)); diff --git a/src/main/java/eu/jonahbauer/raytracing/shape/HitResult.java b/src/main/java/eu/jonahbauer/raytracing/scene/HitResult.java similarity index 91% rename from src/main/java/eu/jonahbauer/raytracing/shape/HitResult.java rename to src/main/java/eu/jonahbauer/raytracing/scene/HitResult.java index b5c519f..4d23584 100644 --- a/src/main/java/eu/jonahbauer/raytracing/shape/HitResult.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/HitResult.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.raytracing.shape; +package eu.jonahbauer.raytracing.scene; import eu.jonahbauer.raytracing.math.Vec3; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/eu/jonahbauer/raytracing/shape/Shape.java b/src/main/java/eu/jonahbauer/raytracing/scene/Hittable.java similarity index 84% rename from src/main/java/eu/jonahbauer/raytracing/shape/Shape.java rename to src/main/java/eu/jonahbauer/raytracing/scene/Hittable.java index 43574d5..9dd1bd1 100644 --- a/src/main/java/eu/jonahbauer/raytracing/shape/Shape.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/Hittable.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.raytracing.shape; +package eu.jonahbauer.raytracing.scene; import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Ray; @@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Optional; -public sealed interface Shape permits Sphere { +public interface Hittable { /** * {@return the value t such that ray.at(t) is the intersection of this shaped closest to diff --git a/src/main/java/eu/jonahbauer/raytracing/shape/Scene.java b/src/main/java/eu/jonahbauer/raytracing/scene/Scene.java similarity index 57% rename from src/main/java/eu/jonahbauer/raytracing/shape/Scene.java rename to src/main/java/eu/jonahbauer/raytracing/scene/Scene.java index add4160..ffb475c 100644 --- a/src/main/java/eu/jonahbauer/raytracing/shape/Scene.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/Scene.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.raytracing.shape; +package eu.jonahbauer.raytracing.scene; import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Ray; @@ -8,24 +8,23 @@ import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.Optional; -public record Scene(@NotNull List<@NotNull Shape> shapes) { +public record Scene(@NotNull List<@NotNull Hittable> objects) implements Hittable { public Scene { - shapes = List.copyOf(shapes); + objects = List.copyOf(objects); } - public Scene(@NotNull Shape @NotNull ... shapes) { - this(List.of(shapes)); + public Scene(@NotNull Hittable @NotNull ... objects) { + this(List.of(objects)); } - public @NotNull Optional hit(@NotNull Ray ray) { - var range = new Range(0, Double.POSITIVE_INFINITY); + public @NotNull Optional hit(@NotNull Ray ray, @NotNull Range range) { var result = (HitResult) null; - for (var shape : shapes) { - var r = shape.hit(ray, range); - if (r.isPresent() && (result == null || r.get().t() < result.t())) { + for (var object : objects) { + var r = object.hit(ray, range); + if (r.isPresent() && range.surrounds(r.get().t())) { result = r.get(); - range = new Range(0, result.t()); + range = new Range(range.min(), result.t()); } } return Optional.ofNullable(result); diff --git a/src/main/java/eu/jonahbauer/raytracing/shape/Sphere.java b/src/main/java/eu/jonahbauer/raytracing/scene/Sphere.java similarity index 96% rename from src/main/java/eu/jonahbauer/raytracing/shape/Sphere.java rename to src/main/java/eu/jonahbauer/raytracing/scene/Sphere.java index c1763f7..a52d9bc 100644 --- a/src/main/java/eu/jonahbauer/raytracing/shape/Sphere.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/Sphere.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.raytracing.shape; +package eu.jonahbauer.raytracing.scene; import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Ray; @@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Objects; import java.util.Optional; -public record Sphere(@NotNull Vec3 center, double radius) implements Shape { +public record Sphere(@NotNull Vec3 center, double radius) implements Hittable { public static final @NotNull Sphere UNIT = new Sphere(Vec3.ZERO, 1.0); public Sphere { diff --git a/src/test/java/eu/jonahbauer/raytracing/shape/SphereTest.java b/src/test/java/eu/jonahbauer/raytracing/scene/SphereTest.java similarity index 94% rename from src/test/java/eu/jonahbauer/raytracing/shape/SphereTest.java rename to src/test/java/eu/jonahbauer/raytracing/scene/SphereTest.java index ac3fe8a..68c1bdb 100644 --- a/src/test/java/eu/jonahbauer/raytracing/shape/SphereTest.java +++ b/src/test/java/eu/jonahbauer/raytracing/scene/SphereTest.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.raytracing.shape; +package eu.jonahbauer.raytracing.scene; import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Ray;