add @Contract annotations to vector-like objects

feature/spectral
jbb01 5 months ago
parent 791ee606c4
commit 903ab1409b

@ -1,5 +1,6 @@
package eu.jonahbauer.raytracing.math; package eu.jonahbauer.raytracing.math;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -7,15 +8,26 @@ import org.jetbrains.annotations.NotNull;
* @param <T> the type * @param <T> the type
*/ */
public interface IVec<T extends IVec<T>> { public interface IVec<T extends IVec<T>> {
@Contract(pure = true)
double get(int i); double get(int i);
@Contract(pure = true)
@NotNull T plus(@NotNull T other); @NotNull T plus(@NotNull T other);
@Contract(pure = true)
@NotNull T minus(@NotNull T other); @NotNull T minus(@NotNull T other);
@Contract(pure = true)
@NotNull T times(@NotNull T other); @NotNull T times(@NotNull T other);
@Contract(pure = true)
@NotNull T times(double d); @NotNull T times(double d);
@Contract(pure = true)
default @NotNull T div(double d) { default @NotNull T div(double d) {
return times(1 / d); return times(1 / d);
} }
@Contract(pure = true)
double @NotNull[] toArray(); double @NotNull[] toArray();
} }

@ -1,5 +1,6 @@
package eu.jonahbauer.raytracing.math; package eu.jonahbauer.raytracing.math;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -7,17 +8,23 @@ import org.jetbrains.annotations.NotNull;
* @param <T> the type * @param <T> the type
*/ */
public interface IVec3<T extends Record & IVec3<T>> extends IVec<T> { public interface IVec3<T extends Record & IVec3<T>> extends IVec<T> {
@Contract(pure = true)
default double component1() { default double component1() {
return toVec3().x(); return toVec3().x();
} }
@Contract(pure = true)
default double component2() { default double component2() {
return toVec3().y(); return toVec3().y();
} }
@Contract(pure = true)
default double component3() { default double component3() {
return toVec3().z(); return toVec3().z();
} }
@Override @Override
@Contract(pure = true)
default double get(int i) { default double get(int i) {
return switch (i) { return switch (i) {
case 0 -> component1(); case 0 -> component1();
@ -27,9 +34,13 @@ public interface IVec3<T extends Record & IVec3<T>> extends IVec<T> {
}; };
} }
@NotNull Vec3 toVec3(); @Contract(pure = true)
default @NotNull Vec3 toVec3() {
return new Vec3(component1(), component2(), component3());
}
@Override @Override
@Contract(pure = true)
default double @NotNull [] toArray() { default double @NotNull [] toArray() {
return new double[] {component1(), component2(), component3()}; return new double[] {component1(), component2(), component3()};
} }

@ -3,6 +3,7 @@ package eu.jonahbauer.raytracing.render.spectral;
import eu.jonahbauer.raytracing.render.spectral.colors.ColorXYZ; import eu.jonahbauer.raytracing.render.spectral.colors.ColorXYZ;
import eu.jonahbauer.raytracing.render.spectral.spectrum.Spectra; import eu.jonahbauer.raytracing.render.spectral.spectrum.Spectra;
import eu.jonahbauer.raytracing.render.spectral.spectrum.Spectrum; import eu.jonahbauer.raytracing.render.spectral.spectrum.Spectrum;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
@ -46,10 +47,12 @@ public final class SampledWavelengths {
this.pdf = pdf; this.pdf = pdf;
} }
@Contract(pure = true)
public double get(int index) { public double get(int index) {
return lambdas[index]; return lambdas[index];
} }
@Contract(pure = true)
public int size() { public int size() {
return lambdas.length; 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 * 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. * refraction that makes it incorrect to track multiple wavelengths together.
*/ */
@Contract(mutates = "this")
public double collapse() { public double collapse() {
if (pdf.length >= 2 || pdf[1] != 0) { if (pdf.length >= 2 || pdf[1] != 0) {
Arrays.fill(pdf, 1, pdf.length, 0d); Arrays.fill(pdf, 1, pdf.length, 0d);

@ -1,13 +1,10 @@
package eu.jonahbauer.raytracing.render.spectral.colors; package eu.jonahbauer.raytracing.render.spectral.colors;
import eu.jonahbauer.raytracing.math.IVec3; import eu.jonahbauer.raytracing.math.IVec3;
import eu.jonahbauer.raytracing.math.Vec3;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Random; import java.util.Random;
import static eu.jonahbauer.raytracing.Main.DEBUG;
public record ColorRGB(double r, double g, double b) implements IVec3<ColorRGB> { public record ColorRGB(double r, double g, double b) implements IVec3<ColorRGB> {
public static final @NotNull ColorRGB BLACK = new ColorRGB(0.0, 0.0, 0.0); 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); 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<ColorRGB>
} }
public ColorRGB { public ColorRGB {
if (DEBUG && (!Double.isFinite(r) || !Double.isFinite(g) || !Double.isFinite(b))) { assert Double.isFinite(r) : "r must be finite";
throw new IllegalArgumentException("r, g and b 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<ColorRGB>
return new ColorRGB(r * other.r, g * other.g, b * other.b); 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 * Accessors
*/ */

@ -61,7 +61,7 @@ public final class ColorSpace {
public @NotNull ColorXYZ toXYZ(@NotNull ColorRGB rgb) { public @NotNull ColorXYZ toXYZ(@NotNull ColorRGB rgb) {
var out = XYZfromRGB.times(rgb.toVec3()); var out = XYZfromRGB.times(rgb.toVec3());
return ColorXYZ.fromVec3(out); return new ColorXYZ(out);
} }
public @NotNull Vec3 toCIELab(@NotNull ColorRGB rgb) { public @NotNull Vec3 toCIELab(@NotNull ColorRGB rgb) {

@ -25,6 +25,16 @@ public record ColorXYZ(double x, double y, double z) implements IVec3<ColorXYZ>
); );
} }
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 * Math
*/ */
@ -71,19 +81,6 @@ public record ColorXYZ(double x, double y, double z) implements IVec3<ColorXYZ>
return new ColorXYZ(x * d, y * d, z * d); 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 * Accessors
*/ */

Loading…
Cancel
Save