From 3f589bbc56ebfb1bd35b538b8a77d4019396b5aa Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sat, 4 Mar 2023 00:01:03 +0200 Subject: [PATCH] Updated readability of the code. --- .../e3d/renderer/octree/OctreeVolume.java | 141 ++++++++---------- .../e3d/renderer/octree/raytracer/Ray.java | 27 +--- .../renderer/octree/raytracer/RayTracer.java | 77 +++++++--- .../octree/raytracer/package-info.java | 2 +- 4 files changed, 128 insertions(+), 119 deletions(-) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/OctreeVolume.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/OctreeVolume.java index 27b9488..e1b7269 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/OctreeVolume.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/OctreeVolume.java @@ -4,6 +4,7 @@ */ package eu.svjatoslav.sixth.e3d.renderer.octree; +import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.Ray; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance; @@ -34,17 +35,15 @@ public class OctreeVolume { // unused cell private static final int CELL_STATE_UNUSED = -1; - final LineAppearance factory = new LineAppearance(); - - public int ce1[]; - public int ce2[]; - public int ce3[]; - public int ce4[]; - public int ce5[]; - public int ce6[]; - public int ce7[]; - public int ce8[]; + public int[] ce1; + public int[] ce2; + public int[] ce3; + public int[] ce4; + public int[] ce5; + public int[] ce6; + public int[] ce7; + public int[] ce8; public int cellAllocationPointer = 0; public int usedCellsCount = 0; public int masterCellSize; @@ -89,120 +88,112 @@ public class OctreeVolume { final int cubeSize, final Ray r) { // ray starts inside the cube - if ((cubeX - cubeSize) < r.x) - if ((cubeX + cubeSize) > r.x) - if ((cubeY - cubeSize) < r.y) - if ((cubeY + cubeSize) > r.y) - if ((cubeZ - cubeSize) < r.z) - if ((cubeZ + cubeSize) > r.z) { - r.hitX = r.x; - r.hitY = r.y; - r.hitZ = r.z; + if ((cubeX - cubeSize) < r.origin.x) + if ((cubeX + cubeSize) > r.origin.x) + if ((cubeY - cubeSize) < r.origin.y) + if ((cubeY + cubeSize) > r.origin.y) + if ((cubeZ - cubeSize) < r.origin.z) + if ((cubeZ + cubeSize) > r.origin.z) { + r.hitPoint = r.origin.clone(); return 1; } // back face - if (r.zp > 0) - if ((cubeZ - cubeSize) > r.z) { - final double mult = ((cubeZ - cubeSize) - r.z) / r.zp; - final double hitX = (r.xp * mult) + r.x; + if (r.direction.z > 0) + if ((cubeZ - cubeSize) > r.origin.z) { + final double mult = ((cubeZ - cubeSize) - r.origin.z) / r.direction.z; + final double hitX = (r.direction.x * mult) + r.origin.x; if ((cubeX - cubeSize) < hitX) if ((cubeX + cubeSize) > hitX) { - final double hitY = (r.yp * mult) + r.y; + final double hitY = (r.direction.y * mult) + r.origin.y; if ((cubeY - cubeSize) < hitY) if ((cubeY + cubeSize) > hitY) { - r.hitX = hitX; - r.hitY = hitY; - r.hitZ = cubeZ - cubeSize; + r.hitPoint = new Point3D(hitX, hitY, cubeZ + - cubeSize); return 2; } } } // up face - if (r.yp > 0) - if ((cubeY - cubeSize) > r.y) { - final double mult = ((cubeY - cubeSize) - r.y) / r.yp; - final double hitX = (r.xp * mult) + r.x; + if (r.direction.y > 0) + if ((cubeY - cubeSize) > r.origin.y) { + final double mult = ((cubeY - cubeSize) - r.origin.y) / r.direction.y; + final double hitX = (r.direction.x * mult) + r.origin.x; if ((cubeX - cubeSize) < hitX) if ((cubeX + cubeSize) > hitX) { - final double hitZ = (r.zp * mult) + r.z; + final double hitZ = (r.direction.z * mult) + r.origin.z; if ((cubeZ - cubeSize) < hitZ) if ((cubeZ + cubeSize) > hitZ) { - r.hitX = hitX; - r.hitY = cubeY - cubeSize; - r.hitZ = hitZ; + r.hitPoint = new Point3D(hitX, cubeY - cubeSize, + hitZ); return 3; } } } // left face - if (r.xp > 0) - if ((cubeX - cubeSize) > r.x) { - final double mult = ((cubeX - cubeSize) - r.x) / r.xp; - final double hitY = (r.yp * mult) + r.y; + if (r.direction.x > 0) + if ((cubeX - cubeSize) > r.origin.x) { + final double mult = ((cubeX - cubeSize) - r.origin.x) / r.direction.x; + final double hitY = (r.direction.y * mult) + r.origin.y; if ((cubeY - cubeSize) < hitY) if ((cubeY + cubeSize) > hitY) { - final double hitZ = (r.zp * mult) + r.z; + final double hitZ = (r.direction.z * mult) + r.origin.z; if ((cubeZ - cubeSize) < hitZ) if ((cubeZ + cubeSize) > hitZ) { - r.hitX = cubeX - cubeSize; - r.hitY = hitY; - r.hitZ = hitZ; + r.hitPoint = new Point3D(cubeX - cubeSize, hitY, + hitZ); return 4; } } } // front face - if (r.zp < 0) - if ((cubeZ + cubeSize) < r.z) { - final double mult = ((cubeZ + cubeSize) - r.z) / r.zp; - final double hitX = (r.xp * mult) + r.x; + if (r.direction.z < 0) + if ((cubeZ + cubeSize) < r.origin.z) { + final double mult = ((cubeZ + cubeSize) - r.origin.z) / r.direction.z; + final double hitX = (r.direction.x * mult) + r.origin.x; if ((cubeX - cubeSize) < hitX) if ((cubeX + cubeSize) > hitX) { - final double hitY = (r.yp * mult) + r.y; + final double hitY = (r.direction.y * mult) + r.origin.y; if ((cubeY - cubeSize) < hitY) if ((cubeY + cubeSize) > hitY) { - r.hitX = hitX; - r.hitY = hitY; - r.hitZ = cubeZ + cubeSize; + r.hitPoint = new Point3D(hitX, hitY, cubeZ + + cubeSize); return 5; } } } // down face - if (r.yp < 0) - if ((cubeY + cubeSize) < r.y) { - final double mult = ((cubeY + cubeSize) - r.y) / r.yp; - final double hitX = (r.xp * mult) + r.x; + if (r.direction.y < 0) + if ((cubeY + cubeSize) < r.origin.y) { + final double mult = ((cubeY + cubeSize) - r.origin.y) / r.direction.y; + final double hitX = (r.direction.x * mult) + r.origin.x; if ((cubeX - cubeSize) < hitX) if ((cubeX + cubeSize) > hitX) { - final double hitZ = (r.zp * mult) + r.z; + final double hitZ = (r.direction.z * mult) + r.origin.z; if ((cubeZ - cubeSize) < hitZ) if ((cubeZ + cubeSize) > hitZ) { - r.hitX = hitX; - r.hitY = cubeY + cubeSize; - r.hitZ = hitZ; + r.hitPoint = new Point3D(hitX, cubeY + cubeSize, + hitZ); return 6; } } } // right face - if (r.xp < 0) - if ((cubeX + cubeSize) < r.x) { - final double mult = ((cubeX + cubeSize) - r.x) / r.xp; - final double hitY = (r.yp * mult) + r.y; + if (r.direction.x < 0) + if ((cubeX + cubeSize) < r.origin.x) { + final double mult = ((cubeX + cubeSize) - r.origin.x) / r.direction.x; + final double hitY = (r.direction.y * mult) + r.origin.y; if ((cubeY - cubeSize) < hitY) if ((cubeY + cubeSize) > hitY) { - final double hitZ = (r.zp * mult) + r.z; + final double hitZ = (r.direction.z * mult) + r.origin.z; if ((cubeZ - cubeSize) < hitZ) if ((cubeZ + cubeSize) > hitZ) { - r.hitX = cubeX + cubeSize; - r.hitY = hitY; - r.hitZ = hitZ; + r.hitPoint = new Point3D(cubeX + cubeSize, hitY, + hitZ); return 7; } } @@ -328,7 +319,7 @@ public class OctreeVolume { } // decide witch subcube to use - int subCubeArray[]; + int[] subCubeArray; int subX, subY, subZ; if (x > cellX) { @@ -431,9 +422,9 @@ public class OctreeVolume { final int halfOfCellSize = cellSize / 2; int rayIntersectionResult; - if (ray.x > cellX) { - if (ray.y > cellY) { - if (ray.z > cellZ) { + if (ray.origin.x > cellX) { + if (ray.origin.y > cellY) { + if (ray.origin.z > cellZ) { // 7 // 6 8 3 5 2 4 1 @@ -578,7 +569,7 @@ public class OctreeVolume { } } - } else if (ray.z > cellZ) { + } else if (ray.origin.z > cellZ) { // 6 // 5 2 7 8 1 3 4 if (ce6[pointer] != 0) { @@ -720,8 +711,8 @@ public class OctreeVolume { } } - } else if (ray.y > cellY) { - if (ray.z > cellZ) { + } else if (ray.origin.y > cellY) { + if (ray.origin.z > cellZ) { // 8 // 5 7 4 1 6 3 2 @@ -866,7 +857,7 @@ public class OctreeVolume { } } - } else if (ray.z > cellZ) { + } else if (ray.origin.z > cellZ) { // 5 // 1 6 8 4 2 7 3 diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/Ray.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/Ray.java index 02f21a7..659a989 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/Ray.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/Ray.java @@ -4,37 +4,26 @@ */ package eu.svjatoslav.sixth.e3d.renderer.octree.raytracer; +import eu.svjatoslav.sixth.e3d.geometry.Point3D; + public class Ray { // ray origin - public double x, y, z; + public Point3D origin; // ray direction - public double xp, yp, zp; + public Point3D direction; // ray hit point - public double hitX, hitY, hitZ; - + public Point3D hitPoint; public int hitCellSize; public int hitCellX, hitCellY, hitCellZ; - public Ray(final double X, final double Y, final double Z, final double Xp, - final double Yp, final double Zp) { - x = X; - y = Y; - z = Z; - xp = Xp; - yp = Yp; - zp = Zp; + public Ray(Point3D origin, Point3D direction) { + this.origin = origin; + this.direction = direction; } - - @Override - public String toString() { - return "Ray \n" + " x " + x + "\n" + " y " + y + "\n" + " z " + z - + "\n" + " xp " + xp + "\n" + " yp " + yp + "\n" + " zp " + zp - + "\n"; - } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/RayTracer.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/RayTracer.java index 8f7923f..d389a82 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/RayTracer.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/RayTracer.java @@ -4,6 +4,7 @@ */ package eu.svjatoslav.sixth.e3d.renderer.octree.raytracer; +import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.renderer.octree.OctreeVolume; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; @@ -81,11 +82,15 @@ public class RayTracer implements Runnable { final double cy3 = cy1 + ((y3p * x) / width); final double cz3 = cz1 + ((z3p * x) / width); - final Ray r = new Ray(cameraView.cameraCenter.x, - cameraView.cameraCenter.y, cameraView.cameraCenter.z, cx3 - - cameraView.cameraCenter.x, cy3 + final Ray r = new Ray( + new Point3D(cameraView.cameraCenter.x, + cameraView.cameraCenter.y, + cameraView.cameraCenter.z), + new Point3D( + cx3 - cameraView.cameraCenter.x, cy3 - cameraView.cameraCenter.y, cz3 - - cameraView.cameraCenter.z); + - cameraView.cameraCenter.z) + ); final int c = traceRay(r); final Color color = new Color(c); @@ -133,12 +138,16 @@ public class RayTracer implements Runnable { + (yDist * yDist) + (zDist * zDist)); distance = (distance / 3) + 1; - final Ray r1 = new Ray(ray.hitCellX, ray.hitCellY - - (float) 1.5, ray.hitCellZ, + final Ray r1 = new Ray( + new Point3D( + ray.hitCellX, + ray.hitCellY - (float) 1.5, + ray.hitCellZ), - (float) l.location.x - (float) ray.hitCellX, l.location.y + new Point3D((float) l.location.x - (float) ray.hitCellX, l.location.y - (ray.hitCellY - (float) 1.5), (float) l.location.z - - (float) ray.hitCellZ); + - (float) ray.hitCellZ) + ); final int rt1 = octreeVolume.traceCell(0, 0, 0, octreeVolume.masterCellSize, 0, r1); @@ -149,12 +158,16 @@ public class RayTracer implements Runnable { newBlue = (l.color.b * l.brightness) / distance; } - final Ray r2 = new Ray(ray.hitCellX - (float) 1.5, - ray.hitCellY, ray.hitCellZ, + final Ray r2 = new Ray( + new Point3D( + ray.hitCellX - (float) 1.5, + ray.hitCellY, ray.hitCellZ), + new Point3D( l.location.x - (ray.hitCellX - (float) 1.5), (float) l.location.y - (float) ray.hitCellY, (float) l.location.z - - (float) ray.hitCellZ); + - (float) ray.hitCellZ) + ); final int rt2 = octreeVolume.traceCell(0, 0, 0, octreeVolume.masterCellSize, 0, r2); @@ -172,12 +185,15 @@ public class RayTracer implements Runnable { newBlue = tempBlue; } - final Ray r3 = new Ray(ray.hitCellX, ray.hitCellY, - ray.hitCellZ - (float) 1.5, - + final Ray r3 = new Ray( + new Point3D ( + ray.hitCellX, ray.hitCellY, + ray.hitCellZ - (float) 1.5), + new Point3D( (float) l.location.x - (float) ray.hitCellX, (float) l.location.y - (float) ray.hitCellY, l.location.z - - (ray.hitCellZ - (float) 1.5)); + - (ray.hitCellZ - (float) 1.5)) + ); final int rt3 = octreeVolume.traceCell(0, 0, 0, octreeVolume.masterCellSize, 0, r3); @@ -194,12 +210,17 @@ public class RayTracer implements Runnable { newBlue = tempBlue; } - final Ray r4 = new Ray(ray.hitCellX, ray.hitCellY - + (float) 1.5, ray.hitCellZ, + final Ray r4 = new Ray( + new Point3D( + ray.hitCellX, + ray.hitCellY+ (float) 1.5, + ray.hitCellZ), + new Point3D( (float) l.location.x - (float) ray.hitCellX, l.location.y - (ray.hitCellY + (float) 1.5), (float) l.location.z - - (float) ray.hitCellZ); + - (float) ray.hitCellZ) + ); final int rt4 = octreeVolume.traceCell(0, 0, 0, octreeVolume.masterCellSize, 0, r4); @@ -216,12 +237,16 @@ public class RayTracer implements Runnable { newBlue = tempBlue; } - final Ray r5 = new Ray(ray.hitCellX + (float) 1.5, - ray.hitCellY, ray.hitCellZ, + final Ray r5 = new Ray( + new Point3D( + ray.hitCellX + (float) 1.5, + ray.hitCellY, ray.hitCellZ), + new Point3D( l.location.x - (ray.hitCellX + (float) 1.5), (float) l.location.y - (float) ray.hitCellY, (float) l.location.z - - (float) ray.hitCellZ); + - (float) ray.hitCellZ) + ); final int rt5 = octreeVolume.traceCell(0, 0, 0, octreeVolume.masterCellSize, 0, r5); @@ -238,12 +263,16 @@ public class RayTracer implements Runnable { newBlue = tempBlue; } - final Ray r6 = new Ray(ray.hitCellX, ray.hitCellY, - ray.hitCellZ + (float) 1.5, + final Ray r6 = new Ray( + new Point3D( + ray.hitCellX, ray.hitCellY, + ray.hitCellZ + (float) 1.5), + + new Point3D( (float) l.location.x - (float) ray.hitCellX, (float) l.location.y - (float) ray.hitCellY, l.location.z - - (ray.hitCellZ + (float) 1.5)); + - (ray.hitCellZ + (float) 1.5))); final int rt6 = octreeVolume.traceCell(0, 0, 0, octreeVolume.masterCellSize, 0, r6); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/package-info.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/package-info.java index 431f7c0..66ff9d3 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/package-info.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/package-info.java @@ -3,7 +3,7 @@ * This project is released under Creative Commons Zero (CC0) license. * * - * Raytracer through voxel data. + * Raytracer through voxel data that is stored in octree. */ package eu.svjatoslav.sixth.e3d.renderer.octree.raytracer; -- 2.20.1