small performance improvements

main
jbb01 6 months ago
parent d5173c2d5a
commit 2c28b10a6e

@ -12,9 +12,9 @@ public record Ray(@NotNull Vec3 origin, @NotNull Vec3 direction) {
public @NotNull Vec3 at(double t) { public @NotNull Vec3 at(double t) {
return new Vec3( return new Vec3(
origin().x() + t * direction.x(), Math.fma(t, direction.x(), origin.x()),
origin().y() + t * direction.y(), Math.fma(t, direction.y(), origin.y()),
origin().z() + t * direction.z() Math.fma(t, direction.z(), origin.z())
); );
} }

@ -23,15 +23,20 @@ public record Vec3(double x, double y, double z) {
public static @NotNull Vec3 random(@NotNull RandomGenerator random, boolean unit) { public static @NotNull Vec3 random(@NotNull RandomGenerator random, boolean unit) {
var vec = new Vec3( var vec = new Vec3(
2 * random.nextDouble() - 1, Math.fma(2, random.nextDouble(), -1),
2 * random.nextDouble() - 1, Math.fma(2, random.nextDouble(), -1),
2 * random.nextDouble() - 1 Math.fma(2, random.nextDouble(), -1)
); );
return unit ? vec.unit() : vec; return unit ? vec.unit() : vec;
} }
public static @NotNull Vec3 reflect(@NotNull Vec3 vec, @NotNull Vec3 normal) { public static @NotNull Vec3 reflect(@NotNull Vec3 vec, @NotNull Vec3 normal) {
return vec.minus(normal.times(2 * normal.times(vec))); var factor = - 2 * normal.times(vec);
return new Vec3(
Math.fma(factor, normal.x(), vec.x()),
Math.fma(factor, normal.y(), vec.y()),
Math.fma(factor, normal.z(), vec.z())
);
} }
public static @NotNull Optional<Vec3> refract(@NotNull Vec3 vec, @NotNull Vec3 normal, double ri) { public static @NotNull Optional<Vec3> refract(@NotNull Vec3 vec, @NotNull Vec3 normal, double ri) {
@ -56,10 +61,11 @@ public record Vec3(double x, double y, double z) {
} }
public static @NotNull Vec3 average(@NotNull Vec3 current, @NotNull Vec3 next, int index) { public static @NotNull Vec3 average(@NotNull Vec3 current, @NotNull Vec3 next, int index) {
var factor = 1d / index;
return new Vec3( return new Vec3(
current.x() + (next.x() - current.x()) / index, Math.fma(factor, next.x() - current.x(), current.x()),
current.y() + (next.y() - current.y()) / index, Math.fma(factor, next.y() - current.y(), current.y()),
current.z() + (next.z() - current.z()) / index Math.fma(factor, next.z() - current.z(), current.z())
); );
} }

@ -5,6 +5,7 @@ import eu.jonahbauer.raytracing.math.Ray;
import eu.jonahbauer.raytracing.scene.Hittable; import eu.jonahbauer.raytracing.scene.Hittable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public final class HittableList extends HittableCollection { public final class HittableList extends HittableCollection {
@ -12,7 +13,7 @@ public final class HittableList extends HittableCollection {
private final @NotNull AABB bbox; private final @NotNull AABB bbox;
public HittableList(@NotNull List<? extends @NotNull Hittable> objects) { public HittableList(@NotNull List<? extends @NotNull Hittable> objects) {
this.objects = List.copyOf(objects); this.objects = new ArrayList<>(objects);
this.bbox = AABB.getBoundingBox(this.objects).orElse(AABB.EMPTY); this.bbox = AABB.getBoundingBox(this.objects).orElse(AABB.EMPTY);
} }

Loading…
Cancel
Save