From 59980888bd6bba94f84e4f63ce67e5b624df8aea Mon Sep 17 00:00:00 2001
From: Svjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Date: Thu, 6 Jul 2023 04:18:40 +0300
Subject: [PATCH] Refactoring to remove unneeded complexity.

---
 .../eu/svjatoslav/sixth/e3d/math/Vertex.java  | 16 +++++--
 .../shapes/AbstractCoordinateShape.java       |  8 +---
 .../shapes/basic/ForwardOrientedTexture.java  |  3 +-
 .../raster/shapes/basic/line/Line.java        |  6 ++-
 .../basic/solidpolygon/SolidPolygon.java      | 25 ++++++----
 .../texturedpolygon/TexturedPolygon.java      | 46 ++++++-------------
 .../shapes/composite/TexturedRectangle.java   | 18 +++++---
 .../renderer/raster/slicer/BorderLine.java    | 17 +++----
 .../raster/slicer/PolygonCoordinate.java      | 34 --------------
 .../e3d/renderer/raster/slicer/Slicer.java    | 27 ++++-------
 10 files changed, 83 insertions(+), 117 deletions(-)
 delete mode 100644 src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/PolygonCoordinate.java

diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java b/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java
index 50423b1..24c243f 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java
@@ -34,21 +34,31 @@ public class Vertex {
      */
     public Point2D onScreenCoordinate;
 
+
+    /**
+     * Coordinate within texture.
+     */
+    public Point2D textureCoordinate;
+
+
     /**
      * The frame number when this vertex was last transformed.
      */
     private int lastTransformedFrame;
 
     public Vertex() {
-        coordinate = new Point3D();
-        transformedCoordinate = new Point3D();
-        onScreenCoordinate = new Point2D();
+        this(new Point3D());
     }
 
     public Vertex(final Point3D location) {
+        this(location, null);
+    }
+
+    public Vertex(final Point3D location, Point2D textureCoordinate) {
         coordinate = location;
         transformedCoordinate = new Point3D();
         onScreenCoordinate = new Point2D();
+        this.textureCoordinate = textureCoordinate;
     }
 
 
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/AbstractCoordinateShape.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/AbstractCoordinateShape.java
index d42ac78..50e4bd9 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/AbstractCoordinateShape.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/AbstractCoordinateShape.java
@@ -4,7 +4,6 @@
  */
 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes;
 
-import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
 import eu.svjatoslav.sixth.e3d.math.TransformsStack;
 import eu.svjatoslav.sixth.e3d.math.Vertex;
@@ -27,11 +26,8 @@ public abstract class AbstractCoordinateShape extends AbstractShape {
         shapeId = lastShapeId.getAndIncrement();
     }
 
-    public AbstractCoordinateShape(final Point3D... vertexes) {
-        coordinates = new Vertex[vertexes.length];
-
-        for (int i = 0; i < vertexes.length; i++)
-            coordinates[i] = new Vertex(vertexes[i]);
+    public AbstractCoordinateShape(final Vertex... vertexes) {
+        coordinates = vertexes;
 
         shapeId = lastShapeId.getAndIncrement();
     }
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java
index cbfd7e3..9ec004b 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java
@@ -7,6 +7,7 @@ package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic;
 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
+import eu.svjatoslav.sixth.e3d.math.Vertex;
 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.TextureBitmap;
@@ -31,7 +32,7 @@ public class ForwardOrientedTexture extends AbstractCoordinateShape {
 
     public ForwardOrientedTexture(final Point3D point, final double scale,
                                   final Texture texture) {
-        super(point);
+        super(new Vertex(point));
         this.texture = texture;
         setScale(scale);
     }
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/line/Line.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/line/Line.java
index 2366e79..0ad06d8 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/line/Line.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/line/Line.java
@@ -7,6 +7,7 @@ package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line;
 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
+import eu.svjatoslav.sixth.e3d.math.Vertex;
 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
 
@@ -41,7 +42,10 @@ public class Line extends AbstractCoordinateShape {
     public Line(final Point3D point1, final Point3D point2, final Color color,
                 final double width) {
 
-        super(point1, point2);
+        super(
+                new Vertex(point1),
+                new Vertex(point2)
+        );
 
         this.color = color;
         this.width = width;
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/solidpolygon/SolidPolygon.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/solidpolygon/SolidPolygon.java
index 1b4bcfc..e941408 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/solidpolygon/SolidPolygon.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/solidpolygon/SolidPolygon.java
@@ -8,6 +8,7 @@ import eu.svjatoslav.sixth.e3d.geometry.Point2D;
 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
+import eu.svjatoslav.sixth.e3d.math.Vertex;
 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
 
@@ -25,7 +26,11 @@ public class SolidPolygon extends AbstractCoordinateShape {
 
     public SolidPolygon(final Point3D point1, final Point3D point2,
                         final Point3D point3, final Color color) {
-        super(point1, point2, point3);
+        super(
+                new Vertex(point1),
+                new Vertex(point2),
+                new Vertex(point3)
+        );
         this.color = color;
     }
 
@@ -51,7 +56,7 @@ public class SolidPolygon extends AbstractCoordinateShape {
         final int width = x2 - x1;
 
         int offset = ((y * renderBuffer.width) + x1) * 4;
-        final byte[] offSreenBufferBytes = renderBuffer.pixels;
+        final byte[] offScreenBufferBytes = renderBuffer.pixels;
 
         final int polygonAlpha = color.a;
         final int b = color.b;
@@ -60,13 +65,13 @@ public class SolidPolygon extends AbstractCoordinateShape {
 
         if (polygonAlpha == 255)
             for (int i = 0; i < width; i++) {
-                offSreenBufferBytes[offset] = (byte) 255;
+                offScreenBufferBytes[offset] = (byte) 255;
                 offset++;
-                offSreenBufferBytes[offset] = (byte) b;
+                offScreenBufferBytes[offset] = (byte) b;
                 offset++;
-                offSreenBufferBytes[offset] = (byte) g;
+                offScreenBufferBytes[offset] = (byte) g;
                 offset++;
-                offSreenBufferBytes[offset] = (byte) r;
+                offScreenBufferBytes[offset] = (byte) r;
                 offset++;
             }
         else {
@@ -77,13 +82,13 @@ public class SolidPolygon extends AbstractCoordinateShape {
             final int redWithAlpha = r * polygonAlpha;
 
             for (int i = 0; i < width; i++) {
-                offSreenBufferBytes[offset] = (byte) 255;
+                offScreenBufferBytes[offset] = (byte) 255;
                 offset++;
-                offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + blueWithAlpha) / 256);
+                offScreenBufferBytes[offset] = (byte) ((((offScreenBufferBytes[offset] & 0xff) * backgroundAlpha) + blueWithAlpha) / 256);
                 offset++;
-                offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + greenWithAlpha) / 256);
+                offScreenBufferBytes[offset] = (byte) ((((offScreenBufferBytes[offset] & 0xff) * backgroundAlpha) + greenWithAlpha) / 256);
                 offset++;
-                offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + redWithAlpha) / 256);
+                offScreenBufferBytes[offset] = (byte) ((((offScreenBufferBytes[offset] & 0xff) * backgroundAlpha) + redWithAlpha) / 256);
                 offset++;
             }
 
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java
index 2cd60e7..85760f5 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java
@@ -5,10 +5,9 @@
 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon;
 
 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
-import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
+import eu.svjatoslav.sixth.e3d.math.Vertex;
 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
-import eu.svjatoslav.sixth.e3d.renderer.raster.slicer.PolygonCoordinate;
 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.TextureBitmap;
 
@@ -31,44 +30,26 @@ import static java.util.Arrays.sort;
 public class TexturedPolygon extends AbstractCoordinateShape {
 
     public final Texture texture;
-    /**
-     * Polygon texture coordinates.
-     */
-    public Point2D texturePoint1, texturePoint2, texturePoint3;
 
     /**
      * If <code>true</code> then polygon borders will be drawn.
      * It is used for debugging purposes.
      */
     public boolean showBorders = false;
+
     private double totalTextureDistance = -1;
 
-    public TexturedPolygon(final Point3D p1, final Point3D p2,
-                           final Point3D p3, final Point2D tp1, final Point2D tp2,
-                           final Point2D tp3, final Texture texture) {
+    public TexturedPolygon(Vertex p1, Vertex p2, Vertex p3, final Texture texture) {
 
         super(p1, p2, p3);
-
-        texturePoint1 = tp1;
-        texturePoint2 = tp2;
-        texturePoint3 = tp3;
-
         this.texture = texture;
     }
 
-    public TexturedPolygon(final PolygonCoordinate pc1,
-                           final PolygonCoordinate pc2, final PolygonCoordinate pc3,
-                           final Texture texture) {
-
-        this(pc1.spaceCoordinate, pc2.spaceCoordinate, pc3.spaceCoordinate, pc1.textureCoordinate, pc2.textureCoordinate,
-                pc3.textureCoordinate, texture);
-    }
-
     private void computeTotalTextureDistance() {
         // compute total texture distance
-        totalTextureDistance = texturePoint1.getDistanceTo(texturePoint2);
-        totalTextureDistance += texturePoint1.getDistanceTo(texturePoint3);
-        totalTextureDistance += texturePoint2.getDistanceTo(texturePoint3);
+        totalTextureDistance = coordinates[0].textureCoordinate.getDistanceTo(coordinates[1].textureCoordinate);
+        totalTextureDistance += coordinates[0].textureCoordinate.getDistanceTo(coordinates[2].textureCoordinate);
+        totalTextureDistance += coordinates[1].textureCoordinate.getDistanceTo(coordinates[2].textureCoordinate);
     }
 
     private void drawHorizontalLine(final PolygonBorderInterpolator line1,
@@ -199,12 +180,15 @@ public class TexturedPolygon extends AbstractCoordinateShape {
                 new PolygonBorderInterpolator(), new PolygonBorderInterpolator(),
                 new PolygonBorderInterpolator()};
 
-        is[0].setPoints(projectedPoint1, projectedPoint2, texturePoint1,
-                texturePoint2);
-        is[1].setPoints(projectedPoint1, projectedPoint3, texturePoint1,
-                texturePoint3);
-        is[2].setPoints(projectedPoint2, projectedPoint3, texturePoint2,
-                texturePoint3);
+        is[0].setPoints(projectedPoint1, projectedPoint2,
+                coordinates[0].textureCoordinate,
+                coordinates[1].textureCoordinate);
+        is[1].setPoints(projectedPoint1, projectedPoint3,
+                coordinates[0].textureCoordinate,
+                coordinates[2].textureCoordinate);
+        is[2].setPoints(projectedPoint2, projectedPoint3,
+                coordinates[1].textureCoordinate,
+                coordinates[2].textureCoordinate);
 
         sort(is);
 
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/TexturedRectangle.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/TexturedRectangle.java
index 919a87e..e5e3e18 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/TexturedRectangle.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/TexturedRectangle.java
@@ -7,6 +7,7 @@ package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite;
 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.math.Transform;
+import eu.svjatoslav.sixth.e3d.math.Vertex;
 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon.TexturedPolygon;
 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
@@ -62,16 +63,21 @@ public class TexturedRectangle extends AbstractCompositeShape {
         textureBottomRight = new Point2D(textureWidth, textureHeight);
         textureBottomLeft = new Point2D(0, textureHeight);
 
-        final TexturedPolygon texturedPolygon1 = new TexturedPolygon(topLeft,
-                topRight, bottomRight, textureTopLeft, textureTopRight,
-                textureBottomRight, texture);
+
+
+
+        final TexturedPolygon texturedPolygon1 = new TexturedPolygon(
+                new Vertex(topLeft, textureTopLeft),
+                new Vertex(topRight, textureTopRight),
+                new Vertex(bottomRight, textureBottomRight), texture);
 
         texturedPolygon1
                 .setMouseInteractionController(mouseInteractionController);
 
-        final TexturedPolygon texturedPolygon2 = new TexturedPolygon(topLeft,
-                bottomLeft, bottomRight, textureTopLeft, textureBottomLeft,
-                textureBottomRight, texture);
+        final TexturedPolygon texturedPolygon2 = new TexturedPolygon(
+                new Vertex(topLeft, textureTopLeft),
+                new Vertex(bottomLeft, textureBottomLeft),
+                new Vertex(bottomRight, textureBottomRight), texture);
 
         texturedPolygon2
                 .setMouseInteractionController(mouseInteractionController);
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/BorderLine.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/BorderLine.java
index aebf479..b21e5ed 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/BorderLine.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/BorderLine.java
@@ -6,16 +6,17 @@ package eu.svjatoslav.sixth.e3d.renderer.raster.slicer;
 
 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
+import eu.svjatoslav.sixth.e3d.math.Vertex;
 
 import java.util.Comparator;
 
 public class BorderLine implements Comparator<BorderLine> {
 
     public final int count;
-    final PolygonCoordinate c1;
-    final PolygonCoordinate c2;
+    final Vertex c1;
+    final Vertex c2;
 
-    public BorderLine(final PolygonCoordinate c1, final PolygonCoordinate c2,
+    public BorderLine(final Vertex c1, final Vertex c2,
                       final int count) {
         this.c1 = c1;
         this.c2 = c2;
@@ -28,12 +29,12 @@ public class BorderLine implements Comparator<BorderLine> {
     }
 
     public double getLength() {
-        return c1.spaceCoordinate.getDistanceTo(c2.spaceCoordinate);
+        return c1.coordinate.getDistanceTo(c2.coordinate);
     }
 
-    public PolygonCoordinate getMiddlePoint() {
-        return new PolygonCoordinate(new Point3D().computeMiddlePoint(c1.spaceCoordinate,
-                c2.spaceCoordinate), new Point2D().setToMiddle(c1.textureCoordinate,
-                c2.textureCoordinate));
+    public Vertex getMiddlePoint() {
+        return new Vertex(
+                new Point3D().computeMiddlePoint(c1.coordinate, c2.coordinate),
+                new Point2D().setToMiddle(c1.textureCoordinate, c2.textureCoordinate));
     }
 }
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/PolygonCoordinate.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/PolygonCoordinate.java
deleted file mode 100644
index eb4745a..0000000
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/PolygonCoordinate.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Sixth 3D engine. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.sixth.e3d.renderer.raster.slicer;
-
-import eu.svjatoslav.sixth.e3d.geometry.Point2D;
-import eu.svjatoslav.sixth.e3d.geometry.Point3D;
-
-/**
- * Polygon coordinate.
- * This class is used to store coordinates of a point in 3D space and its
- * texture coordinate.
- *
- * TODO: Refactor this class out of existence. Use Vertex instead. Texture coordinates should be moved to Vertex.
- */
-public class PolygonCoordinate {
-
-    /**
-     * Space coordinate of the point.
-     */
-    public Point3D spaceCoordinate;
-
-    /**
-     * Texture coordinate of the point.
-     */
-    public Point2D textureCoordinate;
-
-    public PolygonCoordinate(final Point3D spaceCoordinate, final Point2D textureCoordinate) {
-        this.spaceCoordinate = spaceCoordinate;
-        this.textureCoordinate = textureCoordinate;
-    }
-
-}
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/Slicer.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/Slicer.java
index 183ea40..b79dc0d 100644
--- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/Slicer.java
+++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/Slicer.java
@@ -4,6 +4,7 @@
  */
 package eu.svjatoslav.sixth.e3d.renderer.raster.slicer;
 
+import eu.svjatoslav.sixth.e3d.math.Vertex;
 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon.TexturedPolygon;
 
 import java.util.ArrayList;
@@ -29,9 +30,9 @@ public class Slicer {
         this.maxDistance = maxDistance;
     }
 
-    private void considerSlicing(final PolygonCoordinate c1,
-                                 final PolygonCoordinate c2,
-                                 final PolygonCoordinate c3,
+    private void considerSlicing(final Vertex c1,
+                                 final Vertex c2,
+                                 final Vertex c3,
                                  final TexturedPolygon originalPolygon) {
 
         final BorderLine[] lines = new BorderLine[]{
@@ -52,7 +53,7 @@ public class Slicer {
             return;
         }
 
-        final PolygonCoordinate middle = longestLine.getMiddlePoint();
+        final Vertex middle = longestLine.getMiddlePoint();
 
         switch (longestLine.count) {
             case 1:
@@ -76,19 +77,11 @@ public class Slicer {
 
     public void slice(final TexturedPolygon originalPolygon) {
 
-        final PolygonCoordinate pc1 = new PolygonCoordinate(
-                originalPolygon.coordinates[0].coordinate,
-                originalPolygon.texturePoint1);
-
-        final PolygonCoordinate pc2 = new PolygonCoordinate(
-                originalPolygon.coordinates[1].coordinate,
-                originalPolygon.texturePoint2);
-
-        final PolygonCoordinate pc3 = new PolygonCoordinate(
-                originalPolygon.coordinates[2].coordinate,
-                originalPolygon.texturePoint3);
-
-        considerSlicing(pc1, pc2, pc3, originalPolygon);
+        considerSlicing(
+                originalPolygon.coordinates[0],
+                originalPolygon.coordinates[1],
+                originalPolygon.coordinates[2],
+                originalPolygon);
     }
 
 }
-- 
2.20.1