From 76cb3c5124616e16128487973f17bbaa0caaca6e Mon Sep 17 00:00:00 2001 From: vadim Date: Sun, 19 Feb 2023 20:23:43 +0300 Subject: [PATCH] Fix tabulation --- src/main/java/App.java | 7 ++-- src/main/java/Convertor.java | 2 +- src/main/java/Perlin.java | 18 +++++----- src/main/java/camera/BlurCamera.java | 9 ++--- src/main/java/camera/ClearCamera.java | 6 ++-- src/main/java/camera/MotionBlurCamera.java | 4 +-- src/main/java/image/ArrayImage.java | 4 +-- src/main/java/image/ColorImage.java | 4 +-- src/main/java/image/Image.java | 3 +- src/main/java/image/ImageWriter.java | 4 +-- src/main/java/image/ListImage.java | 8 ++--- src/main/java/image/OutputFormat.java | 1 + src/main/java/material/Dielectric.java | 19 +++++----- src/main/java/material/DiffuseLight.java | 2 +- src/main/java/material/Lambertian.java | 5 +-- src/main/java/material/Material.java | 2 +- src/main/java/material/Metal.java | 13 +++---- src/main/java/math/Color.java | 25 ++++++------- src/main/java/math/Colors.java | 10 +++--- src/main/java/math/HitRecord.java | 9 ++--- src/main/java/math/Interval.java | 14 ++++---- src/main/java/math/Point.java | 4 +-- src/main/java/math/Points.java | 5 +-- src/main/java/math/Ray.java | 3 +- src/main/java/math/Vectors.java | 36 +++++++++++-------- src/main/java/objects/AABB.java | 13 ++++--- src/main/java/objects/BVHNode.java | 12 +++---- src/main/java/objects/MovingSphere.java | 15 ++++---- src/main/java/objects/Quad.java | 6 ++-- src/main/java/objects/Quadrilateral.java | 4 +-- src/main/java/objects/Sphere.java | 19 +++++----- .../java/objects/comparator/XComparator.java | 1 + .../java/objects/comparator/YComparator.java | 1 + .../java/objects/comparator/ZComparator.java | 1 + src/main/java/raytracer/RayTracerImpl.java | 21 +++++------ src/main/java/raytracer/RayTracerLights.java | 23 ++++++------ src/main/java/raytracer/scene/BallsScene.java | 7 ++-- .../java/raytracer/scene/LightsScene.java | 10 ++++-- src/main/java/raytracer/scene/Scene.java | 8 +++++ src/main/java/texture/CheckerTexture.java | 2 +- src/main/java/texture/GradientTexture.java | 2 +- src/main/java/texture/ImageTexture.java | 17 ++++----- src/main/java/texture/MarbleTexture.java | 3 +- src/main/java/texture/PerlinTexture.java | 8 ++--- src/main/java/texture/SinCheckerTexture.java | 4 +-- src/main/java/texture/SolidColorTexture.java | 2 +- src/main/java/util/Dimension.java | 4 +-- src/main/java/util/Pair.java | 2 +- src/main/java/util/PerlinNoise.java | 23 ++++++------ src/main/java/util/TriFunction.java | 2 +- src/main/java/util/Util.java | 7 ++-- .../collections/AbstractBoundableList.java | 1 - .../collections/impl/BoundableArrayList.java | 14 ++++---- .../collections/impl/HittableArrayList.java | 9 +++-- src/test/java/AABBTest.java | 9 ++--- src/test/java/ColorTest.java | 4 +-- src/test/java/PointTest.java | 6 ++-- src/test/java/QuadrilateralTest.java | 2 -- src/test/java/VectorTest.java | 8 ++--- src/test/java/VectorsTest.java | 4 +-- 60 files changed, 254 insertions(+), 237 deletions(-) diff --git a/src/main/java/App.java b/src/main/java/App.java index 6ad7484..b8c2382 100644 --- a/src/main/java/App.java +++ b/src/main/java/App.java @@ -2,19 +2,18 @@ import image.ImageWriter; import raytracer.RayTracer; import raytracer.RayTracerConfig; -import raytracer.RayTracerImpl; import raytracer.RayTracerLights; -import raytracer.scene.BallsScene; import raytracer.scene.LightsScene; import util.Dimension; + import java.nio.file.Paths; import java.util.concurrent.TimeUnit; public class App { - public static void main(String[] args) throws Exception{ + public static void main(String[] args) throws Exception { ImageWriter writer = new ImageWriter(Math::sqrt); // RayTracer rayTracer = new RayTracerImpl(new RayTracerConfig(new Dimension(400, 16d/9d), 50, 50), new BallsScene()); - RayTracer rayTracer = new RayTracerLights(new RayTracerConfig(new Dimension(400, 16d/9d), 50, 50), new LightsScene()); + RayTracer rayTracer = new RayTracerLights(new RayTracerConfig(new Dimension(400, 16d / 9d), 50, 50), new LightsScene()); long st = System.currentTimeMillis(); Image image = rayTracer.render(); System.out.printf("Processing time: %d seconds\n", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - st)); diff --git a/src/main/java/Convertor.java b/src/main/java/Convertor.java index 5e3aaa9..a22a7d8 100644 --- a/src/main/java/Convertor.java +++ b/src/main/java/Convertor.java @@ -12,7 +12,7 @@ public static void main(String[] args) { final String destFilename = "image.png"; // try to get hold of the file. - try(BufferedReader reader = new BufferedReader(new FileReader(sourceFilename))) { + try (BufferedReader reader = new BufferedReader(new FileReader(sourceFilename))) { // validate file header final String FileHeader = "P3"; String line = reader.readLine(); diff --git a/src/main/java/Perlin.java b/src/main/java/Perlin.java index 816bc27..784a321 100644 --- a/src/main/java/Perlin.java +++ b/src/main/java/Perlin.java @@ -3,31 +3,29 @@ import math.Points; import util.PerlinNoise; -import javax.imageio.ImageIO; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Arrays; import java.util.function.Function; import static util.Util.clamp; public class Perlin { - public static void main(String[] args) throws Exception{ + public static void main(String[] args) throws Exception { try (OutputStream stream = Files.newOutputStream(Paths.get("image.ppm")); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(stream); - BufferedWriter writer = new BufferedWriter(outputStreamWriter)){ + BufferedWriter writer = new BufferedWriter(outputStreamWriter)) { long start = System.currentTimeMillis(); new Perlin(writer); - System.out.println("Processing time: " + (((double)(System.currentTimeMillis() - start))/1000)); + System.out.println("Processing time: " + (((double) (System.currentTimeMillis() - start)) / 1000)); } Convertor.main(args); } - Perlin(BufferedWriter writer) throws Exception{ + Perlin(BufferedWriter writer) throws Exception { int width = 100; int height = 100; @@ -37,9 +35,9 @@ public static void main(String[] args) throws Exception{ PerlinNoise b = new PerlinNoise(); writer.write("P3\n" + width + " " + height + "\n255\n"); - for(int j = 0; j < height; j++){ + for (int j = 0; j < height; j++) { System.out.print("Lines remaining " + (height - j) + "\r"); - for(int i = 0; i < width; i++){ + for (int i = 0; i < width; i++) { Color red = new Color(1, 0, 0).scale(15 * r.turbulence(Points.scale(new Point(i, 0, j), 0.1), 1)); Color green = new Color(0, 1, 0).scale(10 * g.turbulence(Points.scale(new Point(i, 0, j), 0.1), 1)); Color blue = new Color(0, 0, 1).scale(10 * b.turbulence(Points.scale(new Point(i, 0, j), 0.1), 1)); @@ -49,7 +47,7 @@ public static void main(String[] args) throws Exception{ Color mixed = new Color(start.getRed() + red.getRed() + green.getRed() + blue.getRed(), start.getGreen() + red.getGreen() + green.getGreen() + blue.getGreen(), start.getBlue() + red.getBlue() + green.getBlue() + blue.getBlue()); - mixed = mixed.scale((1d/4d)); + mixed = mixed.scale((1d / 4d)); double scaling = perlinNoise.noise(Points.scale(new Point(i, 0, j), 0.1)); Color pixelColor = new Color(1, 1, 1).scale(scaling); writeColor(writer, mixed, x -> x); @@ -62,7 +60,7 @@ private void writeColor(BufferedWriter writer, Color color, Function getSize() { @Override public Color getColor(int x, int y) { - if(x >= xLen || y >= yLen){ + if (x >= xLen || y >= yLen) { throw new IllegalArgumentException("Coordinate out of image size"); } return colors[x][y]; diff --git a/src/main/java/image/ColorImage.java b/src/main/java/image/ColorImage.java index 7c3bf81..51ede2c 100644 --- a/src/main/java/image/ColorImage.java +++ b/src/main/java/image/ColorImage.java @@ -7,7 +7,7 @@ import java.util.Iterator; import java.util.List; -public class ColorImage implements Image{ +public class ColorImage implements Image { private final int x; private final int y; private final Color color; @@ -35,7 +35,7 @@ public Color getColor(int x, int y) { @Override public Iterator iterator() { List pixels = new ArrayList<>(); - for (int i = 0; i < x; i++){ + for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { pixels.add(new Pixel(i, j, color)); } diff --git a/src/main/java/image/Image.java b/src/main/java/image/Image.java index 8c40a4c..0294d92 100644 --- a/src/main/java/image/Image.java +++ b/src/main/java/image/Image.java @@ -3,7 +3,8 @@ import math.Color; import util.Pair; -public interface Image extends Iterable{ +public interface Image extends Iterable { Pair getSize(); + Color getColor(int x, int y); } diff --git a/src/main/java/image/ImageWriter.java b/src/main/java/image/ImageWriter.java index cb6a4cc..85c03a1 100644 --- a/src/main/java/image/ImageWriter.java +++ b/src/main/java/image/ImageWriter.java @@ -19,7 +19,7 @@ public ImageWriter(Function gammaCorrection) { } public void writeToBuffer(BufferedImage imageBuf, Image image) { - for (Pixel px: image) { + for (Pixel px : image) { Color now = px.getColor(); Color corrected = new Color(gammaCorrection.apply(now.getRed()), gammaCorrection.apply(now.getGreen()), gammaCorrection.apply(now.getBlue())); imageBuf.setRGB(px.getX(), px.getY(), corrected.toRGB()); @@ -41,7 +41,7 @@ public void writeToFile(Path file, BufferedImage bufferedImage) throws IOExcepti writeToFile(file, bufferedImage, OutputFormat.PNG); } - public void writeToFile(Path file, Image image) throws IOException{ + public void writeToFile(Path file, Image image) throws IOException { writeToFile(file, write(image)); } diff --git a/src/main/java/image/ListImage.java b/src/main/java/image/ListImage.java index 56b05d3..1f999cc 100644 --- a/src/main/java/image/ListImage.java +++ b/src/main/java/image/ListImage.java @@ -8,7 +8,7 @@ import java.util.LinkedList; import java.util.List; -public class ListImage implements Image{ +public class ListImage implements Image { private final int xLen; private final int yLen; private final List list = new LinkedList<>(); @@ -30,11 +30,11 @@ public Pair getSize() { @Override public Color getColor(int x, int y) { - if(x >= xLen || y >= yLen){ + if (x >= xLen || y >= yLen) { throw new IllegalArgumentException("Coordinate out of image size"); } - for (Pixel px: list) { - if(px.getX() == x && px.getY() == y){ + for (Pixel px : list) { + if (px.getX() == x && px.getY() == y) { return px.getColor(); } } diff --git a/src/main/java/image/OutputFormat.java b/src/main/java/image/OutputFormat.java index 5e25a75..57c4cae 100644 --- a/src/main/java/image/OutputFormat.java +++ b/src/main/java/image/OutputFormat.java @@ -6,6 +6,7 @@ public enum OutputFormat { BMP("bmp"); public final String type; + OutputFormat(String type) { this.type = type; } diff --git a/src/main/java/material/Dielectric.java b/src/main/java/material/Dielectric.java index 033f91f..7267f1b 100644 --- a/src/main/java/material/Dielectric.java +++ b/src/main/java/material/Dielectric.java @@ -6,9 +6,10 @@ import math.Color; import math.HitRecord; -public class Dielectric implements Material{ +public class Dielectric implements Material { private final double refractionIndex; private final Color albedo; + public Dielectric(double refractionIndex) { this(refractionIndex, new Color(1, 1, 1)); } @@ -22,17 +23,17 @@ public Dielectric(double refractionIndex, Color albedo) { @Override public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered) { attenuation.set(albedo); - double refractionRatio = record.isFrontFace() ? (1.0/refractionIndex) : refractionIndex; + double refractionRatio = record.isFrontFace() ? (1.0 / refractionIndex) : refractionIndex; Vector unitDirection = rayIn.getDirection().unit(); double cosTheta = Math.min(1d, Vectors.dot(unitDirection.negate(), record.getNormal())); - double sinTheta = Math.sqrt(1d - cosTheta*cosTheta); + double sinTheta = Math.sqrt(1d - cosTheta * cosTheta); boolean cannotRefract = refractionRatio * sinTheta > 1.0; Vector direction; - if(cannotRefract || reflectance(cosTheta, refractionRatio) > Math.random()){ + if (cannotRefract || reflectance(cosTheta, refractionRatio) > Math.random()) { direction = Vectors.reflect(unitDirection, record.getNormal()); - }else{ + } else { direction = Vectors.refract(unitDirection, record.getNormal(), refractionRatio); } scattered.setOrigin(record.getPoint()).setDirection(direction).setTimeMoment(rayIn.getTimeMoment()); @@ -40,11 +41,11 @@ public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scatt } /** - *@see Schlick approximation + * @see Schlick approximation */ - private double reflectance(double cos, double refRat){ - double R0 = Math.pow((1d - refRat)/(1d + refRat), 2); - return R0 + (1 - R0)*Math.pow((1 - cos), 5); + private double reflectance(double cos, double refRat) { + double R0 = Math.pow((1d - refRat) / (1d + refRat), 2); + return R0 + (1 - R0) * Math.pow((1 - cos), 5); } } diff --git a/src/main/java/material/DiffuseLight.java b/src/main/java/material/DiffuseLight.java index c26247e..8a35180 100644 --- a/src/main/java/material/DiffuseLight.java +++ b/src/main/java/material/DiffuseLight.java @@ -4,7 +4,7 @@ import texture.SolidColorTexture; import texture.Texture; -public class DiffuseLight implements Material{ +public class DiffuseLight implements Material { private final Texture texture; public DiffuseLight(Color color) { diff --git a/src/main/java/material/Lambertian.java b/src/main/java/material/Lambertian.java index ce581a0..84fdbe9 100644 --- a/src/main/java/material/Lambertian.java +++ b/src/main/java/material/Lambertian.java @@ -11,8 +11,9 @@ /** * @see Lambertian reflection */ -public class Lambertian implements Material{ +public class Lambertian implements Material { private final Texture albedo; + public Lambertian(Color albedo) { this.albedo = new SolidColorTexture(albedo); } @@ -24,7 +25,7 @@ public Lambertian(Texture texture) { @Override public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered) { Vector scatteredDirection = record.getNormal().add(Vectors.randomUnitVector()); - if(scatteredDirection.nearZero()){ + if (scatteredDirection.nearZero()) { scatteredDirection = record.getNormal(); } scattered.setDirection(scatteredDirection).setOrigin(record.getPoint()).setTimeMoment(rayIn.getTimeMoment()); diff --git a/src/main/java/material/Material.java b/src/main/java/material/Material.java index c436b98..1449516 100644 --- a/src/main/java/material/Material.java +++ b/src/main/java/material/Material.java @@ -8,7 +8,7 @@ public interface Material { boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered); - default Color emitted(double u, double v, Point p){ + default Color emitted(double u, double v, Point p) { return new Color(0, 0, 0); } } diff --git a/src/main/java/material/Metal.java b/src/main/java/material/Metal.java index 405d379..b5037c0 100644 --- a/src/main/java/material/Metal.java +++ b/src/main/java/material/Metal.java @@ -8,7 +8,7 @@ import texture.Texture; import math.HitRecord; -public class Metal implements Material{ +public class Metal implements Material { private final Texture albedo; //Для создания нечеткого отражения private final double fuzz; @@ -24,12 +24,12 @@ public Metal(Color albedo, double fuzz) { this.fuzz = fuzz; } - public Metal(Texture texture){ + public Metal(Texture texture) { this.albedo = texture; this.fuzz = 0d; } - public Metal(Texture texture, double fuzz){ + public Metal(Texture texture, double fuzz) { this.albedo = texture; this.fuzz = fuzz; } @@ -37,13 +37,10 @@ public Metal(Texture texture, double fuzz){ @Override public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered) { Vector reflected = Vectors.reflect(rayIn.getDirection().unit(), record.getNormal()); - scattered.setOrigin(record.getPoint()) - .setDirection(reflected.add(Vectors.randomInUnitSphere().multiply(fuzz))) - .setTimeMoment(rayIn.getTimeMoment()); + scattered.setOrigin(record.getPoint()).setDirection(reflected.add(Vectors.randomInUnitSphere().multiply(fuzz))).setTimeMoment(rayIn.getTimeMoment()); boolean scatter = Vectors.dot(scattered.getDirection(), record.getNormal()) > 0; - if (scatter) - attenuation.set(albedo.value(record.getU(), record.getV(), record.getPoint())); + if (scatter) attenuation.set(albedo.value(record.getU(), record.getV(), record.getPoint())); return scatter; } } diff --git a/src/main/java/math/Color.java b/src/main/java/math/Color.java index 73dbd50..d70c289 100644 --- a/src/main/java/math/Color.java +++ b/src/main/java/math/Color.java @@ -19,19 +19,19 @@ public Color(double red, double green, double blue) { this.blue = blue; } - public Color(Color color){ + public Color(Color color) { this.red = color.getRed(); this.green = color.getGreen(); this.blue = color.getBlue(); } - public void set(Color color){ + public void set(Color color) { this.red = color.getRed(); this.green = color.getGreen(); this.blue = color.getBlue(); } - public static Color getByRGB(int rgb){ + public static Color getByRGB(int rgb) { int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = (rgb) & 0xff; @@ -39,10 +39,10 @@ public static Color getByRGB(int rgb){ } - public int toRGB(){ - int r = (int)(256 * clamp(red, 0d, 0.999)); - int g = (int)(256 * clamp(green, 0d, 0.999)); - int b = (int)(256 * clamp(blue, 0d, 0.999)); + public int toRGB() { + int r = (int) (256 * clamp(red, 0d, 0.999)); + int g = (int) (256 * clamp(green, 0d, 0.999)); + int b = (int) (256 * clamp(blue, 0d, 0.999)); int ans = 0; ans += (r << 16); @@ -52,11 +52,11 @@ public int toRGB(){ return ans & 0xffffff; } - public static Color getByRGB(int r, int g, int b){ - return new Color((float)r/255, (float)g/255, (float)b/255); + public static Color getByRGB(int r, int g, int b) { + return new Color((float) r / 255, (float) g / 255, (float) b / 255); } - public Color scale(double t){ + public Color scale(double t) { return new Color(red * t, green * t, blue * t); } @@ -96,9 +96,10 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(red, green, blue);} + return Objects.hash(red, green, blue); + } - private boolean inRange(double d){ + private boolean inRange(double d) { return d <= 1d && d >= 0; } diff --git a/src/main/java/math/Colors.java b/src/main/java/math/Colors.java index 12aa9a8..4196c7f 100644 --- a/src/main/java/math/Colors.java +++ b/src/main/java/math/Colors.java @@ -6,17 +6,17 @@ public class Colors { public final static Color GREEN = new ImmutableColor(0, 1, 0); public final static Color BLUE = new ImmutableColor(0, 0, 1); public final static Color BLACK = new ImmutableColor(0, 0, 0); - public final static Color PURPLE = ImmutableColor.getByRGB(128,0,128); - - public static Color add(Color a, Color b){ + public final static Color PURPLE = ImmutableColor.getByRGB(128, 0, 128); + + public static Color add(Color a, Color b) { return new Color(a.getRed() + b.getRed(), a.getGreen() + b.getGreen(), a.getBlue() + b.getBlue()); } - public static Color multiply(Color a, Color b){ + public static Color multiply(Color a, Color b) { return new Color(a.getRed() * b.getRed(), a.getGreen() * b.getGreen(), a.getBlue() * b.getBlue()); } - public static class ImmutableColor extends Color{ + public static class ImmutableColor extends Color { public ImmutableColor() { } diff --git a/src/main/java/math/HitRecord.java b/src/main/java/math/HitRecord.java index 696dbce..9988335 100644 --- a/src/main/java/math/HitRecord.java +++ b/src/main/java/math/HitRecord.java @@ -3,7 +3,7 @@ import material.Material; import util.Pair; -public class HitRecord{ +public class HitRecord { private Point point = new Point(); private Vector normal = new Vector(); private double t = 0d; @@ -15,7 +15,7 @@ public class HitRecord{ public HitRecord() { } - public HitRecord(HitRecord record){ + public HitRecord(HitRecord record) { this.point = new Point(record.getPoint()); this.normal = new Vector(record.getNormal()); this.t = record.getT(); @@ -25,7 +25,7 @@ public HitRecord(HitRecord record){ this.material = record.getMaterial(); } - public HitRecord set(HitRecord record){ + public HitRecord set(HitRecord record) { point = record.point; normal = record.normal; t = record.t; @@ -36,7 +36,7 @@ public HitRecord set(HitRecord record){ return this; } - public HitRecord setFaceNormal(Ray r, Vector outwardNormal){ + public HitRecord setFaceNormal(Ray r, Vector outwardNormal) { frontFace = Vectors.dot(r.getDirection(), outwardNormal) < 0; normal = frontFace ? outwardNormal : outwardNormal.negate(); return this; @@ -96,6 +96,7 @@ public HitRecord setV(double v) { this.v = v; return this; } + public HitRecord setUV(Pair uv) { this.u = uv.getFirst(); this.v = uv.getSecond(); diff --git a/src/main/java/math/Interval.java b/src/main/java/math/Interval.java index 31dd682..edda20c 100644 --- a/src/main/java/math/Interval.java +++ b/src/main/java/math/Interval.java @@ -11,29 +11,29 @@ public Interval(double min, double max) { this.max = max; } - public Interval(Interval interval){ + public Interval(Interval interval) { this.min = interval.getMin(); this.max = interval.getMax(); } - public Interval(Interval a, Interval b){ + public Interval(Interval a, Interval b) { this.min = Math.min(a.getMin(), b.getMin()); this.max = Math.max(a.getMax(), b.getMax()); } - public boolean contains(double x){ + public boolean contains(double x) { return min <= x && x <= max; } - public Interval expand(double delta){ - return new Interval(min - delta/2, max + delta/2); + public Interval expand(double delta) { + return new Interval(min - delta / 2, max + delta / 2); } - public Interval scale(double t){ + public Interval scale(double t) { return new Interval(min * t, max * t); } - public double size(){ + public double size() { return max - min; } diff --git a/src/main/java/math/Point.java b/src/main/java/math/Point.java index 8ac8f4f..9590107 100644 --- a/src/main/java/math/Point.java +++ b/src/main/java/math/Point.java @@ -16,13 +16,13 @@ public Point(double x, double y, double z) { this.z = z; } - public Point(Point point){ + public Point(Point point) { this.x = point.getX(); this.y = point.getY(); this.z = point.getZ(); } - public Point move(Vector vector){ + public Point move(Vector vector) { return new Point(x + vector.getX(), y + vector.getY(), z + vector.getZ()); } diff --git a/src/main/java/math/Points.java b/src/main/java/math/Points.java index 67d373e..e36ed5e 100644 --- a/src/main/java/math/Points.java +++ b/src/main/java/math/Points.java @@ -1,10 +1,11 @@ package math; public class Points { - public static double[] toArray(Point point){ + public static double[] toArray(Point point) { return new double[]{point.getX(), point.getY(), point.getZ()}; } - public static Point scale(Point point, double t){ + + public static Point scale(Point point, double t) { return new Point(point.getX() * t, point.getY() * t, point.getZ() * t); } } diff --git a/src/main/java/math/Ray.java b/src/main/java/math/Ray.java index 1ec1f17..27c09e1 100644 --- a/src/main/java/math/Ray.java +++ b/src/main/java/math/Ray.java @@ -4,6 +4,7 @@ public class Ray { private Point origin = new Point(); private Vector direction = new Vector(); private double timeMoment = 0; + public Ray() { } @@ -17,7 +18,7 @@ public Ray(Point origin, Vector direction, double timeMoment) { this.timeMoment = timeMoment; } - public Point at(double t){ + public Point at(double t) { return origin.move(direction.multiply(t)); } diff --git a/src/main/java/math/Vectors.java b/src/main/java/math/Vectors.java index 55d80a1..634b5fe 100644 --- a/src/main/java/math/Vectors.java +++ b/src/main/java/math/Vectors.java @@ -9,57 +9,63 @@ public class Vectors { //Скалярное произведение - public static double dot(Vector vector1, Vector vector2){ - return vector1.getX()*vector2.getX() + vector1.getY()*vector2.getY() + vector1.getZ()*vector2.getZ(); + public static double dot(Vector vector1, Vector vector2) { + return vector1.getX() * vector2.getX() + vector1.getY() * vector2.getY() + vector1.getZ() * vector2.getZ(); } + //Векторное произведение - public static Vector cross(Vector vector1, Vector vector2){ + public static Vector cross(Vector vector1, Vector vector2) { return new Vector(vector1.getY() * vector2.getZ() - vector1.getZ() * vector2.getY(), vector1.getZ() * vector2.getX() - vector1.getX() * vector2.getZ(), vector1.getX() * vector2.getY() - vector1.getY() * vector2.getX()); } - public static Vector getRandom(){ + + public static Vector getRandom() { Random random = new Random(); return new Vector(random.nextDouble(), random.nextDouble(), random.nextDouble()); } - public static Vector getRandom(double min, double max){ + public static Vector getRandom(double min, double max) { Random random = new Random(); return new Vector(random.nextDouble(min, max), random.nextDouble(min, max), random.nextDouble(min, max)); } - public static Vector randomInUnitSphere(){ - while (true){ + + public static Vector randomInUnitSphere() { + while (true) { Vector vector = getRandom(-1, 1); - if(vector.lengthSquared() >= 1){continue;}; + if (vector.lengthSquared() >= 1) { + continue; + } + ; return vector; } } - public static Vector reflect(Vector vector, Vector normal){ + public static Vector reflect(Vector vector, Vector normal) { return vector.subtract(normal.multiply(dot(vector, normal) * 2)); } - public static Vector refract(Vector vector, Vector normal, double etai){ + public static Vector refract(Vector vector, Vector normal, double etai) { double cosTheta = Math.min(dot(vector.negate(), normal), 1d); Vector perpendicular = vector.add(normal.multiply(cosTheta)).multiply(etai); Vector parallel = normal.multiply(-sqrt(abs(1d - perpendicular.lengthSquared()))); return perpendicular.add(parallel); } - public static Vector randomInUnitDisk(){ + public static Vector randomInUnitDisk() { Random random = new Random(); - while (true){ + while (true) { Vector vector = new Vector(random.nextDouble(-1, 1), random.nextDouble(-1, 1), 0); - if(vector.lengthSquared() >= 1) continue; + if (vector.lengthSquared() >= 1) continue; return vector; } } - public static double[] toArray(Vector vector){ + public static double[] toArray(Vector vector) { return new double[]{vector.getX(), vector.getY(), vector.getZ()}; } - public static Vector randomUnitVector(){ + public static Vector randomUnitVector() { return randomInUnitSphere().unit(); } diff --git a/src/main/java/objects/AABB.java b/src/main/java/objects/AABB.java index c5c3f4a..03d7829 100644 --- a/src/main/java/objects/AABB.java +++ b/src/main/java/objects/AABB.java @@ -16,7 +16,6 @@ public AABB() { } - public AABB(Point min, Point max) { xInterval = new Interval(Math.min(min.getX(), max.getX()), Math.max(min.getX(), max.getX())); yInterval = new Interval(Math.min(min.getY(), max.getY()), Math.max(min.getY(), max.getY())); @@ -29,14 +28,14 @@ public AABB(Interval xInterval, Interval yInterval, Interval zInterval) { this.zInterval = zInterval; } - public boolean hit(Ray r, Interval tInterval){ + public boolean hit(Ray r, Interval tInterval) { Interval[] axis = {xInterval, yInterval, zInterval}; double[] vecAxis = Vectors.toArray(r.getDirection()); double[] pointCo = Points.toArray(r.getOrigin()); Interval tmpInterval = new Interval(tInterval); - for (int i = 0; i < 3; i++){ - double t0 = Math.min((axis[i].getMin() - pointCo[i])/ vecAxis[i], (axis[i].getMax() - pointCo[i])/vecAxis[i]); - double t1 = Math.max((axis[i].getMin() - pointCo[i])/vecAxis[i], (axis[i].getMax() - pointCo[i])/vecAxis[i]); + for (int i = 0; i < 3; i++) { + double t0 = Math.min((axis[i].getMin() - pointCo[i]) / vecAxis[i], (axis[i].getMax() - pointCo[i]) / vecAxis[i]); + double t1 = Math.max((axis[i].getMin() - pointCo[i]) / vecAxis[i], (axis[i].getMax() - pointCo[i]) / vecAxis[i]); tmpInterval.setMin(Math.max(t0, tmpInterval.getMin())); tmpInterval.setMax(Math.min(t1, tmpInterval.getMax())); @@ -59,14 +58,14 @@ public Interval getZ() { return zInterval; } - public static AABB surroundingBox(AABB box0, AABB box1){ + public static AABB surroundingBox(AABB box0, AABB box1) { Interval x = new Interval(box0.xInterval, box1.xInterval); Interval y = new Interval(box0.yInterval, box1.yInterval); Interval z = new Interval(box0.zInterval, box1.zInterval); return new AABB(x, y, z); } - public AABB pad(){ + public AABB pad() { double delta = 0.1; Interval newX = (xInterval.size() >= delta) ? xInterval : xInterval.expand(delta); Interval newY = (yInterval.size() >= delta) ? yInterval : yInterval.expand(delta); diff --git a/src/main/java/objects/BVHNode.java b/src/main/java/objects/BVHNode.java index 0dc7dd5..349fd3e 100644 --- a/src/main/java/objects/BVHNode.java +++ b/src/main/java/objects/BVHNode.java @@ -20,11 +20,11 @@ public BVHNode(BoundableList list) { } public BVHNode(BoundableList list, int begin, int end) { - if(list.isEmpty()){ + if (list.isEmpty()) { left = new Boundable() { @Override public AABB boundingBox() { - return new AABB(){ + return new AABB() { @Override public boolean hit(Ray r, Interval tInterval) { return false; @@ -45,13 +45,13 @@ public boolean hit(Ray r, Interval tInterval, HitRecord rec) { List> comparators = List.of(new XComparator(), new YComparator(), new ZComparator()); Comparator comparator = comparators.get(random.nextInt(0, 3)); int span = end - begin; - if(span == 1){ + if (span == 1) { left = list.get(begin); right = left; - }else if(span == 2){ + } else if (span == 2) { left = list.get(begin); right = list.get(begin + 1); - }else{ + } else { list.subList(begin, end).sort(comparator); int mid = begin + span / 2; left = new BVHNode(list, begin, mid); @@ -68,7 +68,7 @@ public AABB boundingBox() { @Override public boolean hit(Ray r, Interval tInterval, HitRecord rec) { - if(!box.hit(r, tInterval)) + if (!box.hit(r, tInterval)) return false; boolean hitLeft = left.hit(r, tInterval, rec); boolean hitRight = right.hit(r, new Interval(tInterval.getMin(), hitLeft ? rec.getT() : tInterval.getMax()), rec); diff --git a/src/main/java/objects/MovingSphere.java b/src/main/java/objects/MovingSphere.java index 4475cce..71d4177 100644 --- a/src/main/java/objects/MovingSphere.java +++ b/src/main/java/objects/MovingSphere.java @@ -11,7 +11,8 @@ public class MovingSphere implements Boundable { private final double radius; private final Material material; private final AABB box; - public MovingSphere(Point centerStart, Point centerFinish, double radius, double timeStart, double timeFinish, Material material) { + + public MovingSphere(Point centerStart, Point centerFinish, double radius, double timeStart, double timeFinish, Material material) { this.centerStart = centerStart; this.centerFinish = centerFinish; this.timeStart = timeStart; @@ -31,9 +32,9 @@ public boolean hit(Ray r, Interval tInterval, HitRecord rec) { Vector oc = new Vector(center(r.getTimeMoment()), r.getOrigin()); double A = r.getDirection().lengthSquared(); double halfB = Vectors.dot(oc, r.getDirection()); - double C = oc.lengthSquared() - radius*radius; - double discriminant = halfB*halfB - A*C; - if(discriminant < 0d){ + double C = oc.lengthSquared() - radius * radius; + double discriminant = halfB * halfB - A * C; + if (discriminant < 0d) { return false; } double sqrtd = Math.sqrt(discriminant); @@ -58,11 +59,11 @@ public AABB boundingBox() { return box; } - private Point center(double time){ - if(Math.abs(timeFinish - timeStart) <= 1e-10){ + private Point center(double time) { + if (Math.abs(timeFinish - timeStart) <= 1e-10) { return centerStart; } - return centerStart.move(new Vector(centerStart, centerFinish).multiply((time - timeStart)/(timeFinish - timeStart))); + return centerStart.move(new Vector(centerStart, centerFinish).multiply((time - timeStart) / (timeFinish - timeStart))); } diff --git a/src/main/java/objects/Quad.java b/src/main/java/objects/Quad.java index 41cf504..b7580c2 100644 --- a/src/main/java/objects/Quad.java +++ b/src/main/java/objects/Quad.java @@ -4,7 +4,7 @@ import math.*; @Deprecated -public class Quad implements Hittable, Boundable{ +public class Quad implements Hittable, Boundable { private final Point Q; private final Vector u; private final Vector v; @@ -37,11 +37,11 @@ public AABB boundingBox() { @Override public boolean hit(Ray r, Interval tInterval, HitRecord rec) { double denom = Vectors.dot(normal, r.getDirection()); - if(Math.abs(denom) < 1e-8) + if (Math.abs(denom) < 1e-8) return false; double t = (D - Vectors.dot(normal, new Vector(r.getOrigin()))) / denom; - if(!tInterval.contains(t)){ + if (!tInterval.contains(t)) { return false; } diff --git a/src/main/java/objects/Quadrilateral.java b/src/main/java/objects/Quadrilateral.java index c32d629..6ccac3b 100644 --- a/src/main/java/objects/Quadrilateral.java +++ b/src/main/java/objects/Quadrilateral.java @@ -3,7 +3,7 @@ import material.Material; import math.*; -public class Quadrilateral extends Plane implements Boundable{ +public class Quadrilateral extends Plane implements Boundable { private final AABB box; private final Vector w; @@ -31,7 +31,7 @@ public boolean hit(Ray r, Interval tInterval, HitRecord rec) { double beta = Vectors.dot(w, Vectors.cross(u, hitVector)); Interval testInterval = new Interval(0, 1); - if(!testInterval.contains(alpha) || !testInterval.contains(beta)) + if (!testInterval.contains(alpha) || !testInterval.contains(beta)) return false; rec.set(record).setU(alpha).setV(beta); diff --git a/src/main/java/objects/Sphere.java b/src/main/java/objects/Sphere.java index 8105568..1347efb 100644 --- a/src/main/java/objects/Sphere.java +++ b/src/main/java/objects/Sphere.java @@ -20,24 +20,21 @@ public boolean hit(Ray r, Interval tInterval, HitRecord rec) { Vector oc = new Vector(center, r.getOrigin()); double A = r.getDirection().lengthSquared(); double halfB = Vectors.dot(oc, r.getDirection()); - double C = oc.lengthSquared() - radius*radius; - double discriminant = halfB*halfB - A*C; - if(discriminant < 0d){ + double C = oc.lengthSquared() - radius * radius; + double discriminant = halfB * halfB - A * C; + if (discriminant < 0d) { return false; } double sqrtd = Math.sqrt(discriminant); double root = (-halfB - sqrtd) / A; if (!tInterval.contains(root)) { root = (-halfB + sqrtd) / A; - if (!tInterval.contains(root)) - return false; + if (!tInterval.contains(root)) return false; } - rec.setT(root) - .setPoint(r.at(rec.getT())); + rec.setT(root).setPoint(r.at(rec.getT())); Vector outwardNormal = new Vector(center, rec.getPoint()).divide(radius); - rec.setFaceNormal(r, outwardNormal) - .setMaterial(this.material).setUV(getHitCoordinates(new Point().move(outwardNormal))); + rec.setFaceNormal(r, outwardNormal).setMaterial(this.material).setUV(getHitCoordinates(new Point().move(outwardNormal))); return true; } @@ -46,9 +43,9 @@ public AABB boundingBox() { return new AABB(center.move(new Vector(radius, radius, radius).negate()), center.move(new Vector(radius, radius, radius))); } - private Pair getHitCoordinates(Point point){ + private Pair getHitCoordinates(Point point) { double u = (Math.atan2(-point.getZ(), point.getX()) + Math.PI) / (2 * Math.PI); - double v = Math.acos(-point.getY())/Math.PI; + double v = Math.acos(-point.getY()) / Math.PI; return new Pair<>(u, v); } diff --git a/src/main/java/objects/comparator/XComparator.java b/src/main/java/objects/comparator/XComparator.java index 314cf8f..b971a20 100644 --- a/src/main/java/objects/comparator/XComparator.java +++ b/src/main/java/objects/comparator/XComparator.java @@ -8,6 +8,7 @@ public class XComparator implements Comparator { BoxComparator boxComparator = new BoxComparator(); + @Override public int compare(Boundable a, Boundable b) { return boxComparator.apply(a, b, 0); diff --git a/src/main/java/objects/comparator/YComparator.java b/src/main/java/objects/comparator/YComparator.java index 9215b6b..472bb67 100644 --- a/src/main/java/objects/comparator/YComparator.java +++ b/src/main/java/objects/comparator/YComparator.java @@ -8,6 +8,7 @@ public class YComparator implements Comparator { BoxComparator boxComparator = new BoxComparator(); + @Override public int compare(Boundable a, Boundable b) { return boxComparator.apply(a, b, 1); diff --git a/src/main/java/objects/comparator/ZComparator.java b/src/main/java/objects/comparator/ZComparator.java index 4841378..9aec2b6 100644 --- a/src/main/java/objects/comparator/ZComparator.java +++ b/src/main/java/objects/comparator/ZComparator.java @@ -8,6 +8,7 @@ public class ZComparator implements Comparator { BoxComparator boxComparator = new BoxComparator(); + @Override public int compare(Boundable a, Boundable b) { return boxComparator.apply(a, b, 2); diff --git a/src/main/java/raytracer/RayTracerImpl.java b/src/main/java/raytracer/RayTracerImpl.java index 127eb9b..98e20d0 100644 --- a/src/main/java/raytracer/RayTracerImpl.java +++ b/src/main/java/raytracer/RayTracerImpl.java @@ -9,19 +9,20 @@ import java.util.Random; -public class RayTracerImpl implements RayTracer{ +public class RayTracerImpl implements RayTracer { private final Camera camera; private final Hittable world; private final RayTracerConfig config; private final Color[][] colorMatrix; - public RayTracerImpl (RayTracerConfig config, Scene scene){ + public RayTracerImpl(RayTracerConfig config, Scene scene) { this.config = config; this.camera = scene.getCamera(); this.world = scene.getWorld(); this.colorMatrix = new Color[config.imageDimension().getWidth()][config.imageDimension().getHeight()]; } + @Override public Image render() { Random random = new Random(); @@ -30,10 +31,10 @@ public Image render() { for (int i = 0; i < config.getWidth(); i++) { Color pixelColor = new Color(); for (int s = 0; s < config.samplesPerPixel(); s++) { - double u = ((double) i + random.nextDouble())/(config.getWidth() - 1); - double v = ((double) j + random.nextDouble())/(config.getHeight() - 1); + double u = ((double) i + random.nextDouble()) / (config.getWidth() - 1); + double v = ((double) j + random.nextDouble()) / (config.getHeight() - 1); Ray ray = camera.getRay(u, v); - Color color = rayColor(ray, config.depth()).scale(1d/config.samplesPerPixel()); + Color color = rayColor(ray, config.depth()).scale(1d / config.samplesPerPixel()); pixelColor = Colors.add(pixelColor, color); } colorMatrix[i][j] = pixelColor; @@ -42,17 +43,17 @@ public Image render() { return new ArrayImage(colorMatrix); } - private Color rayColor(Ray r, int aDepth){ - if (aDepth <= 0){ + private Color rayColor(Ray r, int aDepth) { + if (aDepth <= 0) { return new Color(0, 0, 0); } HitRecord record = new HitRecord(); - if(world.hit(r, new Interval(0.00001, Double.POSITIVE_INFINITY), record)){ + if (world.hit(r, new Interval(0.00001, Double.POSITIVE_INFINITY), record)) { Ray scattered = new Ray(); Color attenuation = new Color(); - if(record.getMaterial().scatter(r, record, attenuation, scattered)){ - Color newColor = rayColor(scattered,aDepth - 1); + if (record.getMaterial().scatter(r, record, attenuation, scattered)) { + Color newColor = rayColor(scattered, aDepth - 1); return attenuation.setRed(attenuation.getRed() * newColor.getRed()) .setGreen(attenuation.getGreen() * newColor.getGreen()) .setBlue(attenuation.getBlue() * newColor.getBlue()); diff --git a/src/main/java/raytracer/RayTracerLights.java b/src/main/java/raytracer/RayTracerLights.java index b2dc4b2..1439d3d 100644 --- a/src/main/java/raytracer/RayTracerLights.java +++ b/src/main/java/raytracer/RayTracerLights.java @@ -9,19 +9,22 @@ import java.util.Random; -public class RayTracerLights implements RayTracer{ +public class RayTracerLights implements RayTracer { private final Camera camera; private final Hittable world; + private final Color backgroundColor; private final RayTracerConfig config; private final Color[][] colorMatrix; - public RayTracerLights (RayTracerConfig config, Scene scene){ + public RayTracerLights(RayTracerConfig config, Scene scene) { this.config = config; this.camera = scene.getCamera(); this.world = scene.getWorld(); this.colorMatrix = new Color[config.imageDimension().getWidth()][config.imageDimension().getHeight()]; + this.backgroundColor = scene.getBackground(); } + @Override public Image render() { Random random = new Random(); @@ -30,10 +33,10 @@ public Image render() { for (int i = 0; i < config.getWidth(); i++) { Color pixelColor = new Color(); for (int s = 0; s < config.samplesPerPixel(); s++) { - double u = ((double) i + random.nextDouble())/(config.getWidth() - 1); - double v = ((double) j + random.nextDouble())/(config.getHeight() - 1); + double u = ((double) i + random.nextDouble()) / (config.getWidth() - 1); + double v = ((double) j + random.nextDouble()) / (config.getHeight() - 1); Ray ray = camera.getRay(u, v); - Color color = rayColor(ray, config.depth()).scale(1d/config.samplesPerPixel()); + Color color = rayColor(ray, config.depth()).scale(1d / config.samplesPerPixel()); pixelColor = Colors.add(pixelColor, color); } colorMatrix[i][j] = pixelColor; @@ -42,20 +45,20 @@ public Image render() { return new ArrayImage(colorMatrix); } - private Color rayColor(Ray r, int aDepth){ + private Color rayColor(Ray r, int aDepth) { HitRecord record = new HitRecord(); - if(aDepth <= 0){ + if (aDepth <= 0) { return new Color(0, 0, 0); } - if(!world.hit(r, new Interval(0.00001, Double.POSITIVE_INFINITY), record)){ - return Colors.BLACK; + if (!world.hit(r, new Interval(0.00001, Double.POSITIVE_INFINITY), record)) { + return backgroundColor; } Ray scattered = new Ray(); Color attenuation = new Color(); Color colorFromEmission = record.getMaterial().emitted(record.getU(), record.getV(), record.getPoint()); - if(!record.getMaterial().scatter(r, record, attenuation, scattered)){ + if (!record.getMaterial().scatter(r, record, attenuation, scattered)) { return colorFromEmission; } diff --git a/src/main/java/raytracer/scene/BallsScene.java b/src/main/java/raytracer/scene/BallsScene.java index a2e4aac..8931659 100644 --- a/src/main/java/raytracer/scene/BallsScene.java +++ b/src/main/java/raytracer/scene/BallsScene.java @@ -22,19 +22,18 @@ import java.nio.file.Path; import java.nio.file.Paths; -public class BallsScene extends Scene{ +public class BallsScene extends Scene { @Override public void init() { Path earth = Paths.get("src", "main", "resources", "earthrealistic.jpg"); Texture earthTexture = new ImageTexture(earth); - - boundableObjects.add(new MovingSphere(new Point(-1, 1, 0), new Point(1, 1, 0), 1, 2,5, new Lambertian(Colors.PURPLE))); + boundableObjects.add(new MovingSphere(new Point(-1, 1, 0), new Point(1, 1, 0), 1, 2, 5, new Lambertian(Colors.PURPLE))); boundableObjects.add(new Sphere(new Point(0, -1000, 0), 1000, new Lambertian(new Color(0.8, 0.7, 0.8)))); boundableObjects.add(new Sphere(new Point(0, 3, 0), 1, new Lambertian(earthTexture))); - Camera camera = new ClearCamera(new Point(8, 4, 8), new Point(0, 2, 0), new Vector(0, 1, 0), 20, 16d/9d); + Camera camera = new ClearCamera(new Point(8, 4, 8), new Point(0, 2, 0), new Vector(0, 1, 0), 20, 16d / 9d); this.camera = new MotionBlurCamera(camera, 1, 2); } } diff --git a/src/main/java/raytracer/scene/LightsScene.java b/src/main/java/raytracer/scene/LightsScene.java index 9bd640d..4aac786 100644 --- a/src/main/java/raytracer/scene/LightsScene.java +++ b/src/main/java/raytracer/scene/LightsScene.java @@ -6,6 +6,7 @@ import material.DiffuseLight; import material.Lambertian; import material.Material; +import material.Metal; import math.Color; import math.Colors; import math.Point; @@ -18,18 +19,21 @@ import texture.Texture; import util.PerlinNoise; -public class LightsScene extends Scene{ +public class LightsScene extends Scene { @Override public void init() { Texture perlin = new MarbleTexture(); boundableObjects.add(new Sphere(new Point(0, -1000, 0), 1000, new Lambertian(perlin))); - boundableObjects.add(new Sphere(new Point(0, 2, 0), 2, new Lambertian(perlin))); +// boundableObjects.add(new Sphere(new Point(0, 2, 0), 2, new Lambertian(perlin))); + boundableObjects.add(new Sphere(new Point(0, 2, 0), 2, new Metal(new Color(1, 1, 1)))); + this.background = new Color(0, 0, 0); Material diffuseLight = new DiffuseLight(new Color(1, 0.2, 0.4).scale(5)); boundableObjects.add(new Quadrilateral(new Point(3, 1, -2), new Vector(2, 0, 0), new Vector(0, 2, 0), diffuseLight)); // boundableObjects.add(new Quadrilateral(new Point(3, 1, -2), new Vector(2, 0, 0), new Vector(0, 2, 0), new Lambertian(new Color(2, 0.4, 1)))); - Camera camera = new ClearCamera(new Point(26, 3, 6), new Point(0, 2, 0), new Vector(0, 1, 0), 20, 16d/9d); + Camera camera = new ClearCamera(new Point(26, 3, 6), new Point(0, 2, 0), new Vector(0, 1, 0), 20, 16d / 9d); + // Camera camera = new ClearCamera(new Point(26, 3, 26), new Point(0, 2, 0), new Vector(0, 1, 0), 20, 16d/9d); this.camera = new MotionBlurCamera(camera, 1, 2); } diff --git a/src/main/java/raytracer/scene/Scene.java b/src/main/java/raytracer/scene/Scene.java index 08556dc..e077c18 100644 --- a/src/main/java/raytracer/scene/Scene.java +++ b/src/main/java/raytracer/scene/Scene.java @@ -1,6 +1,8 @@ package raytracer.scene; import camera.Camera; +import math.Color; +import math.Colors; import objects.BVHNode; import objects.Hittable; import util.collections.BoundableList; @@ -10,6 +12,7 @@ public abstract class Scene { protected Camera camera; + protected Color background = Colors.WHITE; protected final HittableList world = new HittableArrayList(); protected final BoundableList boundableObjects = new BoundableArrayList(); @@ -24,7 +27,12 @@ public Scene() { public Camera getCamera() { return camera; } + public Hittable getWorld() { return world; } + + public Color getBackground() { + return background; + } } diff --git a/src/main/java/texture/CheckerTexture.java b/src/main/java/texture/CheckerTexture.java index 38a9c89..0d1e32b 100644 --- a/src/main/java/texture/CheckerTexture.java +++ b/src/main/java/texture/CheckerTexture.java @@ -7,7 +7,7 @@ import static java.lang.Math.sin; -public class CheckerTexture implements Texture{ +public class CheckerTexture implements Texture { private final Texture odd; private final Texture even; private final double scale; diff --git a/src/main/java/texture/GradientTexture.java b/src/main/java/texture/GradientTexture.java index dbcb7dd..c30dbae 100644 --- a/src/main/java/texture/GradientTexture.java +++ b/src/main/java/texture/GradientTexture.java @@ -4,7 +4,7 @@ import math.Colors; import math.Point; -public class GradientTexture implements Texture{ +public class GradientTexture implements Texture { private final Color begin; private final Color end; diff --git a/src/main/java/texture/ImageTexture.java b/src/main/java/texture/ImageTexture.java index 5165da0..5115c3d 100644 --- a/src/main/java/texture/ImageTexture.java +++ b/src/main/java/texture/ImageTexture.java @@ -9,13 +9,13 @@ import java.io.IOException; import java.nio.file.Path; -public class ImageTexture implements Texture{ +public class ImageTexture implements Texture { private BufferedImage bufferedImage; private int width; private int height; - public ImageTexture(Path image){ - try{ + public ImageTexture(Path image) { + try { bufferedImage = ImageIO.read(image.toFile()); width = bufferedImage.getWidth(); height = bufferedImage.getHeight(); @@ -26,20 +26,21 @@ public ImageTexture(Path image){ } } + @Override public Color value(double u, double v, Point p) { - if(bufferedImage == null){ + if (bufferedImage == null) { return new Color(0, 1, 1); } double tu = Util.clamp(u, 0d, 1d); double tv = 1d - Util.clamp(v, 0d, 1d); - int i = (int)(tu * width); - int j = (int)(tv * height); + int i = (int) (tu * width); + int j = (int) (tv * height); - if (i >= width) i = width-1; - if (j >= height) j = height-1; + if (i >= width) i = width - 1; + if (j >= height) j = height - 1; return Color.getByRGB(bufferedImage.getRGB(i, j)); } diff --git a/src/main/java/texture/MarbleTexture.java b/src/main/java/texture/MarbleTexture.java index 8b3c24a..129e989 100644 --- a/src/main/java/texture/MarbleTexture.java +++ b/src/main/java/texture/MarbleTexture.java @@ -7,6 +7,7 @@ public class MarbleTexture extends PerlinTexture { private final Function coordinate; + public MarbleTexture() { super(); coordinate = Point::getZ; @@ -24,6 +25,6 @@ public MarbleTexture(Color color, Function coordinate, double sca @Override public Color value(double u, double v, Point p) { - return color.scale(0.5 * (1 + Math.sin(scale * coordinate.apply(p) + 10 * noise.turbulence(p)))); + return color.scale(0.5 * (1 + Math.sin(scale * coordinate.apply(p) + 10 * noise.turbulence(p)))); } } diff --git a/src/main/java/texture/PerlinTexture.java b/src/main/java/texture/PerlinTexture.java index 97d931e..6446730 100644 --- a/src/main/java/texture/PerlinTexture.java +++ b/src/main/java/texture/PerlinTexture.java @@ -5,14 +5,12 @@ import math.Points; import util.PerlinNoise; -import java.util.function.Function; -import java.util.function.Supplier; - -public class PerlinTexture implements Texture{ +public class PerlinTexture implements Texture { protected final PerlinNoise noise = new PerlinNoise(); protected final double scale; protected final int depth; protected final Color color; + public PerlinTexture() { this(new Color(1, 1, 1), 4, 1); } @@ -21,7 +19,7 @@ public PerlinTexture(double scale, int depth) { this(new Color(1, 1, 1), scale, depth); } - public PerlinTexture(Color color, double scale, int depth){ + public PerlinTexture(Color color, double scale, int depth) { this.color = color; this.scale = scale; this.depth = depth; diff --git a/src/main/java/texture/SinCheckerTexture.java b/src/main/java/texture/SinCheckerTexture.java index e30a345..eddaa66 100644 --- a/src/main/java/texture/SinCheckerTexture.java +++ b/src/main/java/texture/SinCheckerTexture.java @@ -3,7 +3,7 @@ import math.Color; import math.Point; -public class SinCheckerTexture implements Texture{ +public class SinCheckerTexture implements Texture { private final Texture odd; private final Texture even; @@ -12,7 +12,7 @@ public SinCheckerTexture(Texture odd, Texture even) { this.even = even; } - public SinCheckerTexture(Color odd, Color even){ + public SinCheckerTexture(Color odd, Color even) { this.odd = new SolidColorTexture(odd); this.even = new SolidColorTexture(even); } diff --git a/src/main/java/texture/SolidColorTexture.java b/src/main/java/texture/SolidColorTexture.java index 21c1ac0..1e66f2a 100644 --- a/src/main/java/texture/SolidColorTexture.java +++ b/src/main/java/texture/SolidColorTexture.java @@ -3,7 +3,7 @@ import math.Color; import math.Point; -public class SolidColorTexture implements Texture{ +public class SolidColorTexture implements Texture { private final Color color; public SolidColorTexture(Color color) { diff --git a/src/main/java/util/Dimension.java b/src/main/java/util/Dimension.java index bfcd1d2..99b58d8 100644 --- a/src/main/java/util/Dimension.java +++ b/src/main/java/util/Dimension.java @@ -1,7 +1,5 @@ package util; -import raytracer.RayTracerConfig; - import java.util.Objects; public class Dimension { @@ -12,7 +10,7 @@ public class Dimension { public Dimension(int width, int height) { this.height = height; this.width = width; - this.aspectRatio = (double) width / (double)height; + this.aspectRatio = (double) width / (double) height; } public Dimension(int width, double aspectRatio) { diff --git a/src/main/java/util/Pair.java b/src/main/java/util/Pair.java index dc9d854..f37208c 100644 --- a/src/main/java/util/Pair.java +++ b/src/main/java/util/Pair.java @@ -2,7 +2,7 @@ import java.util.Objects; -public class Pair { +public class Pair { private F first; private S second; diff --git a/src/main/java/util/PerlinNoise.java b/src/main/java/util/PerlinNoise.java index 0b2526f..5037d31 100644 --- a/src/main/java/util/PerlinNoise.java +++ b/src/main/java/util/PerlinNoise.java @@ -8,7 +8,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Random; import static java.lang.Math.floor; @@ -31,11 +30,11 @@ public PerlinNoise() { Collections.shuffle(permZ); } - public double turbulence(Point p){ + public double turbulence(Point p) { return turbulence(p, 4); } - public double turbulence(Point p, int depth){ + public double turbulence(Point p, int depth) { double accum = 0.0; Point tmp = new Point(p); double weight = 1.0; @@ -48,20 +47,20 @@ public double turbulence(Point p, int depth){ return Math.abs(accum); } - public double noise(Point p){ + public double noise(Point p) { double u = p.getX() - floor(p.getX()); double v = p.getY() - floor(p.getY()); double w = p.getZ() - floor(p.getZ()); - int i = (int)floor(p.getX()); - int j = (int)floor(p.getY()); - int k = (int)floor(p.getZ()); + int i = (int) floor(p.getX()); + int j = (int) floor(p.getY()); + int k = (int) floor(p.getZ()); Vector[][][] c = new Vector[2][2][2]; - for (int di=0; di < 2; di++) - for (int dj=0; dj < 2; dj++) - for (int dk=0; dk < 2; dk++) + for (int di = 0; di < 2; di++) + for (int dj = 0; dj < 2; dj++) + for (int dk = 0; dk < 2; dk++) c[di][dj][dk] = randomVectors[ - permX.get((i + di) & 255) ^ + permX.get((i + di) & 255) ^ permY.get((j + dj) & 255) ^ permZ.get((k + dk) & 255) ]; @@ -69,7 +68,7 @@ public double noise(Point p){ return interpolation(c, u, v, w); } - private double interpolation(Vector[][][] c, double u, double v, double w){ + private double interpolation(Vector[][][] c, double u, double v, double w) { double accum = 0d; double uu = u * u * (3 - 2 * u); double vv = v * v * (3 - 2 * v); diff --git a/src/main/java/util/TriFunction.java b/src/main/java/util/TriFunction.java index 550d8a5..6aeb4aa 100644 --- a/src/main/java/util/TriFunction.java +++ b/src/main/java/util/TriFunction.java @@ -4,7 +4,7 @@ import java.util.function.Function; @FunctionalInterface -public interface TriFunction { +public interface TriFunction { R apply(A a, B b, C c); diff --git a/src/main/java/util/Util.java b/src/main/java/util/Util.java index 7960bf2..45ffc1b 100644 --- a/src/main/java/util/Util.java +++ b/src/main/java/util/Util.java @@ -1,16 +1,15 @@ package util; -import math.*; -import util.collections.BoundableList; -import util.collections.HittableList; import material.Dielectric; import material.Lambertian; import material.Material; import material.Metal; +import math.*; import objects.Hittable; import objects.Sphere; +import util.collections.BoundableList; -import static java.lang.Math.*; +import static java.lang.Math.random; public class Util { public static double clamp(double x, double min, double max) { diff --git a/src/main/java/util/collections/AbstractBoundableList.java b/src/main/java/util/collections/AbstractBoundableList.java index 0587df8..20b8e9b 100644 --- a/src/main/java/util/collections/AbstractBoundableList.java +++ b/src/main/java/util/collections/AbstractBoundableList.java @@ -11,7 +11,6 @@ public AbstractBoundableList(List list) { this.list = list; } - @Override public int size() { return list.size(); diff --git a/src/main/java/util/collections/impl/BoundableArrayList.java b/src/main/java/util/collections/impl/BoundableArrayList.java index 1122b9c..b9a3d81 100644 --- a/src/main/java/util/collections/impl/BoundableArrayList.java +++ b/src/main/java/util/collections/impl/BoundableArrayList.java @@ -1,9 +1,9 @@ package util.collections.impl; +import math.HitRecord; import math.Interval; import math.Ray; import objects.AABB; -import math.HitRecord; import objects.Boundable; import objects.Hittable; import util.collections.AbstractBoundableList; @@ -18,17 +18,17 @@ public BoundableArrayList() { @Override public AABB boundingBox() { - if(this.list.isEmpty()){ + if (this.list.isEmpty()) { throw new RuntimeException("Empty BoundingArrayList"); } AABB tmpBox = new AABB(); boolean firstBox = true; - for(Boundable i : this){ - if(firstBox){ + for (Boundable i : this) { + if (firstBox) { tmpBox = i.boundingBox(); firstBox = false; - }else{ + } else { tmpBox = (AABB.surroundingBox(i.boundingBox(), tmpBox)); } } @@ -41,8 +41,8 @@ public boolean hit(Ray r, Interval tInterval, HitRecord rec) { boolean hitAnything = false; double closestSoFar = tInterval.getMax(); - for (Hittable i: this) { - if(i.hit(r, new Interval(tInterval.getMin(), closestSoFar), tempHit)){ + for (Hittable i : this) { + if (i.hit(r, new Interval(tInterval.getMin(), closestSoFar), tempHit)) { hitAnything = true; closestSoFar = tempHit.getT(); rec.set(tempHit); diff --git a/src/main/java/util/collections/impl/HittableArrayList.java b/src/main/java/util/collections/impl/HittableArrayList.java index 56cea18..29e6200 100644 --- a/src/main/java/util/collections/impl/HittableArrayList.java +++ b/src/main/java/util/collections/impl/HittableArrayList.java @@ -1,10 +1,9 @@ package util.collections.impl; -import math.Interval; -import objects.AABB; import math.HitRecord; -import objects.Hittable; +import math.Interval; import math.Ray; +import objects.Hittable; import util.collections.AbstractHittableList; import java.util.ArrayList; @@ -20,8 +19,8 @@ public boolean hit(Ray r, Interval tInterval, HitRecord rec) { boolean hitAnything = false; double closestSoFar = tInterval.getMax(); - for (Hittable i: this) { - if(i.hit(r, new Interval(tInterval.getMin(), closestSoFar), tempHit)){ + for (Hittable i : this) { + if (i.hit(r, new Interval(tInterval.getMin(), closestSoFar), tempHit)) { hitAnything = true; closestSoFar = tempHit.getT(); rec.set(tempHit); diff --git a/src/test/java/AABBTest.java b/src/test/java/AABBTest.java index 00b695b..3e1be9e 100644 --- a/src/test/java/AABBTest.java +++ b/src/test/java/AABBTest.java @@ -4,11 +4,12 @@ import objects.AABB; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; class AABBTest { @Test - void testIntervals(){ + void testIntervals() { AABB aabb = new AABB(new Point(1, 2, 3), new Point(3, 4, 5)); Interval x = aabb.getX(); Interval y = aabb.getY(); @@ -20,8 +21,8 @@ void testIntervals(){ } @Test - void padTest(){ - AABB aabb = new AABB(new Point(0, 0, 0), new Point(0, 0, 0).move(new Vector(0, 0, 2)).move(new Vector(2, 0, 0))); + void padTest() { + AABB aabb = new AABB(new Point(0, 0, 0), new Point(0, 0, 0).move(new Vector(0, 0, 2)).move(new Vector(2, 0, 0))); AABB padded = aabb.pad(); assertNotEquals(padded.getY().size(), 0d); } diff --git a/src/test/java/ColorTest.java b/src/test/java/ColorTest.java index c1ef0d9..ac34b4b 100644 --- a/src/test/java/ColorTest.java +++ b/src/test/java/ColorTest.java @@ -1,7 +1,7 @@ import math.Color; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; class ColorTest { @@ -24,7 +24,7 @@ void scale() { } @Test - void toRGBfromRGB(){ + void toRGBfromRGB() { Color color = new Color(1, 0, 1); assertEquals(color, Color.getByRGB(color.toRGB())); } diff --git a/src/test/java/PointTest.java b/src/test/java/PointTest.java index 4305325..f91e926 100644 --- a/src/test/java/PointTest.java +++ b/src/test/java/PointTest.java @@ -1,8 +1,8 @@ -import org.junit.jupiter.api.Test; -import math.Vector; import math.Point; +import math.Vector; +import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; class PointTest { diff --git a/src/test/java/QuadrilateralTest.java b/src/test/java/QuadrilateralTest.java index 9cc3d80..6c44075 100644 --- a/src/test/java/QuadrilateralTest.java +++ b/src/test/java/QuadrilateralTest.java @@ -7,8 +7,6 @@ import org.junit.jupiter.api.Test; import util.collections.impl.BoundableArrayList; -import static org.junit.jupiter.api.Assertions.*; - class QuadrilateralTest { @Test diff --git a/src/test/java/VectorTest.java b/src/test/java/VectorTest.java index e3899bf..ad968b5 100644 --- a/src/test/java/VectorTest.java +++ b/src/test/java/VectorTest.java @@ -1,13 +1,13 @@ -import org.junit.jupiter.api.Test; -import math.Vector; import math.Point; +import math.Vector; +import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; class VectorTest { @Test - void constructorTest(){ + void constructorTest() { Point point1 = new Point(0, 0, 0); Point point2 = new Point(5, 1, 7); diff --git a/src/test/java/VectorsTest.java b/src/test/java/VectorsTest.java index f653913..3c7e7d5 100644 --- a/src/test/java/VectorsTest.java +++ b/src/test/java/VectorsTest.java @@ -1,8 +1,8 @@ -import org.junit.jupiter.api.Test; import math.Vector; import math.Vectors; +import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; class VectorsTest {