diff --git a/src/main/java/eu/jonahbauer/raytracing/math/IVec.java b/src/main/java/eu/jonahbauer/raytracing/math/IVec.java index 0aad0da..3e5497f 100644 --- a/src/main/java/eu/jonahbauer/raytracing/math/IVec.java +++ b/src/main/java/eu/jonahbauer/raytracing/math/IVec.java @@ -1,5 +1,6 @@ package eu.jonahbauer.raytracing.math; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; /** @@ -7,15 +8,26 @@ import org.jetbrains.annotations.NotNull; * @param the type */ public interface IVec> { + @Contract(pure = true) double get(int i); + @Contract(pure = true) @NotNull T plus(@NotNull T other); + + @Contract(pure = true) @NotNull T minus(@NotNull T other); + + @Contract(pure = true) @NotNull T times(@NotNull T other); + + @Contract(pure = true) @NotNull T times(double d); + + @Contract(pure = true) default @NotNull T div(double d) { return times(1 / d); } + @Contract(pure = true) double @NotNull[] toArray(); } diff --git a/src/main/java/eu/jonahbauer/raytracing/math/IVec3.java b/src/main/java/eu/jonahbauer/raytracing/math/IVec3.java index c1bff7f..c3f4f52 100644 --- a/src/main/java/eu/jonahbauer/raytracing/math/IVec3.java +++ b/src/main/java/eu/jonahbauer/raytracing/math/IVec3.java @@ -1,5 +1,6 @@ package eu.jonahbauer.raytracing.math; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; /** @@ -7,17 +8,23 @@ import org.jetbrains.annotations.NotNull; * @param the type */ public interface IVec3> extends IVec { + @Contract(pure = true) default double component1() { return toVec3().x(); } + + @Contract(pure = true) default double component2() { return toVec3().y(); } + + @Contract(pure = true) default double component3() { return toVec3().z(); } @Override + @Contract(pure = true) default double get(int i) { return switch (i) { case 0 -> component1(); @@ -27,9 +34,13 @@ public interface IVec3> extends IVec { }; } - @NotNull Vec3 toVec3(); + @Contract(pure = true) + default @NotNull Vec3 toVec3() { + return new Vec3(component1(), component2(), component3()); + } @Override + @Contract(pure = true) default double @NotNull [] toArray() { return new double[] {component1(), component2(), component3()}; } diff --git a/src/main/java/eu/jonahbauer/raytracing/render/spectral/SampledWavelengths.java b/src/main/java/eu/jonahbauer/raytracing/render/spectral/SampledWavelengths.java index 18374d0..1621abb 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/spectral/SampledWavelengths.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/spectral/SampledWavelengths.java @@ -3,6 +3,7 @@ package eu.jonahbauer.raytracing.render.spectral; import eu.jonahbauer.raytracing.render.spectral.colors.ColorXYZ; import eu.jonahbauer.raytracing.render.spectral.spectrum.Spectra; import eu.jonahbauer.raytracing.render.spectral.spectrum.Spectrum; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import java.util.Arrays; @@ -46,10 +47,12 @@ public final class SampledWavelengths { this.pdf = pdf; } + @Contract(pure = true) public double get(int index) { return lambdas[index]; } + @Contract(pure = true) public int size() { return lambdas.length; } @@ -58,6 +61,7 @@ public final class SampledWavelengths { * Terminates the secondary wavelengths. This method should be called after a wavelength-dependent operation like * refraction that makes it incorrect to track multiple wavelengths together. */ + @Contract(mutates = "this") public double collapse() { if (pdf.length >= 2 || pdf[1] != 0) { Arrays.fill(pdf, 1, pdf.length, 0d); diff --git a/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorRGB.java b/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorRGB.java index 3013e96..28f0af5 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorRGB.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorRGB.java @@ -1,13 +1,10 @@ package eu.jonahbauer.raytracing.render.spectral.colors; import eu.jonahbauer.raytracing.math.IVec3; -import eu.jonahbauer.raytracing.math.Vec3; import org.jetbrains.annotations.NotNull; import java.util.Random; -import static eu.jonahbauer.raytracing.Main.DEBUG; - public record ColorRGB(double r, double g, double b) implements IVec3 { public static final @NotNull ColorRGB BLACK = new ColorRGB(0.0, 0.0, 0.0); public static final @NotNull ColorRGB WHITE = new ColorRGB(1.0, 1.0, 1.0); @@ -34,9 +31,9 @@ public record ColorRGB(double r, double g, double b) implements IVec3 } public ColorRGB { - if (DEBUG && (!Double.isFinite(r) || !Double.isFinite(g) || !Double.isFinite(b))) { - throw new IllegalArgumentException("r, g and b must be finite"); - } + assert Double.isFinite(r) : "r must be finite"; + assert Double.isFinite(g) : "g must be finite"; + assert Double.isFinite(b) : "b must be finite"; } /* @@ -103,19 +100,6 @@ public record ColorRGB(double r, double g, double b) implements IVec3 return new ColorRGB(r * other.r, g * other.g, b * other.b); } - /* - * Vec3 - */ - - @Override - public @NotNull Vec3 toVec3() { - return new Vec3(r, g, b); - } - - public static @NotNull ColorRGB fromVec3(@NotNull Vec3 vec) { - return new ColorRGB(vec.x(), vec.y(), vec.z()); - } - /* * Accessors */ diff --git a/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorSpace.java b/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorSpace.java index f281136..4fb5629 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorSpace.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorSpace.java @@ -61,7 +61,7 @@ public final class ColorSpace { public @NotNull ColorXYZ toXYZ(@NotNull ColorRGB rgb) { var out = XYZfromRGB.times(rgb.toVec3()); - return ColorXYZ.fromVec3(out); + return new ColorXYZ(out); } public @NotNull Vec3 toCIELab(@NotNull ColorRGB rgb) { diff --git a/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorXYZ.java b/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorXYZ.java index c015e1b..4e9bbf7 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorXYZ.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/spectral/colors/ColorXYZ.java @@ -25,6 +25,16 @@ public record ColorXYZ(double x, double y, double z) implements IVec3 ); } + public ColorXYZ(@NotNull Vec3 vec) { + this(vec.x(), vec.y(), vec.z()); + } + + public ColorXYZ { + assert Double.isFinite(x) : "x must be finite"; + assert Double.isFinite(y) : "y must be finite"; + assert Double.isFinite(z) : "z must be finite"; + } + /* * Math */ @@ -71,19 +81,6 @@ public record ColorXYZ(double x, double y, double z) implements IVec3 return new ColorXYZ(x * d, y * d, z * d); } - /* - * Vec3 - */ - - @Override - public @NotNull Vec3 toVec3() { - return new Vec3(x, y, z); - } - - public static @NotNull ColorXYZ fromVec3(@NotNull Vec3 vec) { - return new ColorXYZ(vec.x(), vec.y(), vec.z()); - } - /* * Accessors */