|
|
@ -6,17 +6,35 @@ import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
|
|
public record Ray(@NotNull Vec3 origin, @NotNull Vec3 direction, @NotNull SampledWavelengths lambda) {
|
|
|
|
public final class Ray {
|
|
|
|
public Ray {
|
|
|
|
private final @NotNull Vec3 origin;
|
|
|
|
Objects.requireNonNull(origin, "origin");
|
|
|
|
private final @NotNull Vec3 direction;
|
|
|
|
Objects.requireNonNull(direction, "direction");
|
|
|
|
private final @NotNull SampledWavelengths lambda;
|
|
|
|
Objects.requireNonNull(lambda, "lambda");
|
|
|
|
|
|
|
|
}
|
|
|
|
private final @NotNull Vec3 inv;
|
|
|
|
|
|
|
|
private final @NotNull Vec3 negInvOrigin;
|
|
|
|
|
|
|
|
|
|
|
|
public Ray(@NotNull Vec3 origin, @NotNull Vec3 direction) {
|
|
|
|
public Ray(@NotNull Vec3 origin, @NotNull Vec3 direction) {
|
|
|
|
this(origin, direction, SampledWavelengths.EMPTY);
|
|
|
|
this(origin, direction, SampledWavelengths.EMPTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Ray(@NotNull Vec3 origin, @NotNull Vec3 direction, @NotNull SampledWavelengths lambda) {
|
|
|
|
|
|
|
|
this.origin = Objects.requireNonNull(origin, "origin");
|
|
|
|
|
|
|
|
this.direction = Objects.requireNonNull(direction, "direction");
|
|
|
|
|
|
|
|
this.lambda = Objects.requireNonNull(lambda, "lambda");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.inv = direction.inv();
|
|
|
|
|
|
|
|
this.negInvOrigin = inv.neg().times(origin);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Ray(@NotNull Vec3 origin, @NotNull Vec3 direction, @NotNull SampledWavelengths lambda, @NotNull Vec3 inv, @NotNull Vec3 negInvOrigin) {
|
|
|
|
|
|
|
|
this.origin = origin;
|
|
|
|
|
|
|
|
this.direction = direction;
|
|
|
|
|
|
|
|
this.lambda = lambda;
|
|
|
|
|
|
|
|
this.inv = inv;
|
|
|
|
|
|
|
|
this.negInvOrigin = negInvOrigin;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public @NotNull Vec3 at(double t) {
|
|
|
|
public @NotNull Vec3 at(double t) {
|
|
|
|
return Vec3.fma(t, direction, origin);
|
|
|
|
return Vec3.fma(t, direction, origin);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -30,6 +48,50 @@ public record Ray(@NotNull Vec3 origin, @NotNull Vec3 direction, @NotNull Sample
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public @NotNull Ray with(@NotNull SampledWavelengths lambda) {
|
|
|
|
public @NotNull Ray with(@NotNull SampledWavelengths lambda) {
|
|
|
|
return new Ray(origin, direction, lambda);
|
|
|
|
return new Ray(origin, direction, lambda, inv, negInvOrigin);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public @NotNull Vec3 origin() {
|
|
|
|
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public @NotNull Vec3 direction() {
|
|
|
|
|
|
|
|
return direction;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public @NotNull SampledWavelengths lambda() {
|
|
|
|
|
|
|
|
return lambda;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public @NotNull Vec3 getInvDirection() {
|
|
|
|
|
|
|
|
return inv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public @NotNull Vec3 getNegInvOrigin() {
|
|
|
|
|
|
|
|
return negInvOrigin;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
|
|
|
|
if (obj == this) return true;
|
|
|
|
|
|
|
|
if (obj == null || obj.getClass() != this.getClass()) return false;
|
|
|
|
|
|
|
|
var that = (Ray) obj;
|
|
|
|
|
|
|
|
return Objects.equals(this.origin, that.origin) &&
|
|
|
|
|
|
|
|
Objects.equals(this.direction, that.direction) &&
|
|
|
|
|
|
|
|
Objects.equals(this.lambda, that.lambda);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
|
|
|
return Objects.hash(origin, direction, lambda);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public @NotNull String toString() {
|
|
|
|
|
|
|
|
return "Ray[" +
|
|
|
|
|
|
|
|
"origin=" + origin + ", " +
|
|
|
|
|
|
|
|
"direction=" + direction + ", " +
|
|
|
|
|
|
|
|
"lambda=" + lambda + ']';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|