add bounding box to sphere

main
jbb01 6 months ago
parent 8b7b99b184
commit a84ed5c050

@ -0,0 +1,10 @@
package eu.jonahbauer.raytracing.math;
import org.jetbrains.annotations.NotNull;
public record BoundingBox(@NotNull Vec3 min, @NotNull Vec3 max) {
public @NotNull Vec3 center() {
return Vec3.average(min, max, 2);
}
}

@ -1,5 +1,6 @@
package eu.jonahbauer.raytracing.scene; package eu.jonahbauer.raytracing.scene;
import eu.jonahbauer.raytracing.math.BoundingBox;
import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Range;
import eu.jonahbauer.raytracing.math.Ray; import eu.jonahbauer.raytracing.math.Ray;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -14,4 +15,8 @@ public interface Hittable {
* @param ray a ray * @param ray a ray
*/ */
@NotNull Optional<HitResult> hit(@NotNull Ray ray, @NotNull Range range); @NotNull Optional<HitResult> hit(@NotNull Ray ray, @NotNull Range range);
default @NotNull Optional<BoundingBox> getBoundingBox() {
return Optional.empty();
}
} }

@ -1,6 +1,7 @@
package eu.jonahbauer.raytracing.scene; package eu.jonahbauer.raytracing.scene;
import eu.jonahbauer.raytracing.material.Material; import eu.jonahbauer.raytracing.material.Material;
import eu.jonahbauer.raytracing.math.BoundingBox;
import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Range;
import eu.jonahbauer.raytracing.math.Ray; import eu.jonahbauer.raytracing.math.Ray;
import eu.jonahbauer.raytracing.math.Vec3; import eu.jonahbauer.raytracing.math.Vec3;
@ -44,6 +45,14 @@ public record Sphere(@NotNull Vec3 center, double radius, @NotNull Material mate
return Optional.of(new HitResult(t, position, frontFace ? normal : normal.times(-1), material, frontFace)); return Optional.of(new HitResult(t, position, frontFace ? normal : normal.times(-1), material, frontFace));
} }
@Override
public @NotNull Optional<BoundingBox> getBoundingBox() {
return Optional.of(new BoundingBox(
center.minus(radius, radius, radius),
center.plus(radius, radius, radius)
));
}
public @NotNull Sphere withCenter(@NotNull Vec3 center) { public @NotNull Sphere withCenter(@NotNull Vec3 center) {
return new Sphere(center, radius, material); return new Sphere(center, radius, material);
} }

Loading…
Cancel
Save