Possibility to retrieve objects by group. Reuse glowing point textures.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 13 Jun 2017 07:53:20 +0000 (10:53 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 13 Jun 2017 07:53:20 +0000 (10:53 +0300)
src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/Color.java
src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java
src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/GlowingPoint.java
src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java

index 7081837..e21de2d 100644 (file)
@@ -135,4 +135,25 @@ public final class Color {
         return toAwtColor().getRGB();
     }
 
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        Color color = (Color) o;
+
+        if (r != color.r) return false;
+        if (g != color.g) return false;
+        if (b != color.b) return false;
+        return a == color.a;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = r;
+        result = 31 * result + g;
+        result = 31 * result + b;
+        result = 31 * result + a;
+        return result;
+    }
 }
index 3025596..798dcef 100644 (file)
@@ -105,4 +105,8 @@ public class ForwardOrientedTexture extends AbstractCoordinateShape {
         this.scale = scale * SIZE_MULTIPLIER;
     }
 
+    public Point3D getLocation(){
+        return coordinates[0].coordinate;
+    }
+
 }
index 2a11748..4dd454f 100644 (file)
@@ -13,21 +13,33 @@ import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
 
+import java.util.Collections;
+import java.util.Set;
+import java.util.WeakHashMap;
+
 public class GlowingPoint extends ForwardOrientedTexture {
 
     private static final int TEXTURE_SIZE = 50;
     private final Color color;
 
+    private static final Set<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<GlowingPoint, Boolean>());
+
     public GlowingPoint(final Point3D point, final double pointSize,
                         final Color color) {
         super(point, pointSize, getTexture(color));
         this.color = color;
+    }
 
-        getTexture(color);
+    private static Texture getTexture(final Color color) {
+        // attempt to reuse texture from existing glowing point of the same color
+        for (GlowingPoint glowingPoint : glowingPoints)
+            if (color.equals(glowingPoint.color)) return glowingPoint.texture;
 
+        // existing texture not found, creating new one
+        return createTexture(color);
     }
 
-    public static Texture getTexture(final Color color) {
+    private static Texture createTexture(final Color color) {
         final Texture texture = new Texture(TEXTURE_SIZE, TEXTURE_SIZE, 1);
         for (int x = 0; x < TEXTURE_SIZE; x++)
             for (int y = 0; y < TEXTURE_SIZE; y++) {
index 54729ae..99f21c3 100644 (file)
@@ -25,6 +25,7 @@ import eu.svjatoslav.sixth.e3d.renderer.raster.slicer.Slicer;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * In order to get perspective correct textures, large textured polygons are
@@ -119,6 +120,12 @@ public class AbstractCompositeShape extends AbstractShape {
         }
     }
 
+    public List<SubShape> getGroup(final String groupIdentifier) {
+        return originalSubShapes.stream().filter(
+                subShape -> subShape.matchesGroup(groupIdentifier))
+                .collect(Collectors.toList());
+    }
+
     private void resliceIfNeeded() {
 
         final double proposedSliceFactor = relativityTracker