|
| 1 | +package com.sovdee.shapes.sampling; |
| 2 | + |
| 3 | +import com.sovdee.shapes.shapes.Shape; |
| 4 | +import org.joml.Quaterniond; |
| 5 | +import org.joml.Vector3d; |
| 6 | + |
| 7 | +import java.util.Comparator; |
| 8 | +import java.util.LinkedHashSet; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.TreeSet; |
| 11 | +import java.util.UUID; |
| 12 | + |
| 13 | +/** |
| 14 | + * Default implementation of {@link PointSampler} with hash-based caching. |
| 15 | + */ |
| 16 | +public class DefaultPointSampler implements PointSampler { |
| 17 | + |
| 18 | + private SamplingStyle style = SamplingStyle.OUTLINE; |
| 19 | + private double density = 0.25; |
| 20 | + private Comparator<Vector3d> ordering; |
| 21 | + private final UUID uuid; |
| 22 | + private DrawContext drawContext; |
| 23 | + |
| 24 | + // Cache |
| 25 | + private Set<Vector3d> cachedPoints = new LinkedHashSet<>(); |
| 26 | + private CacheState lastState; |
| 27 | + private boolean needsUpdate = false; |
| 28 | + |
| 29 | + public DefaultPointSampler() { |
| 30 | + this.uuid = UUID.randomUUID(); |
| 31 | + this.lastState = new CacheState(style, 0, 1.0, 0, density, 0); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Set<Vector3d> getPoints(Shape shape) { |
| 36 | + return getPoints(shape, shape.getOrientation()); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Set<Vector3d> getPoints(Shape shape, Quaterniond orientation) { |
| 41 | + CacheState state = new CacheState(style, orientation.hashCode(), |
| 42 | + shape.getScale(), shape.getOffset().hashCode(), density, shape.getVersion()); |
| 43 | + if (shape.isDynamic() || needsUpdate || !state.equals(lastState) || cachedPoints.isEmpty()) { |
| 44 | + Set<Vector3d> points = (ordering != null) ? new TreeSet<>(ordering) : new LinkedHashSet<>(); |
| 45 | + |
| 46 | + shape.beforeSampling(density); |
| 47 | + switch (style) { |
| 48 | + case OUTLINE -> shape.generateOutline(points, density); |
| 49 | + case SURFACE -> shape.generateSurface(points, density); |
| 50 | + case FILL -> shape.generateFilled(points, density); |
| 51 | + } |
| 52 | + shape.afterSampling(points); |
| 53 | + |
| 54 | + for (Vector3d point : points) { |
| 55 | + orientation.transform(point); |
| 56 | + point.mul(shape.getScale()); |
| 57 | + point.add(shape.getOffset()); |
| 58 | + } |
| 59 | + cachedPoints = points; |
| 60 | + lastState = state; |
| 61 | + needsUpdate = false; |
| 62 | + } |
| 63 | + return cachedPoints; |
| 64 | + } |
| 65 | + |
| 66 | + public void markDirty() { |
| 67 | + needsUpdate = true; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public SamplingStyle getStyle() { return style; } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void setStyle(SamplingStyle style) { |
| 75 | + this.style = style; |
| 76 | + this.needsUpdate = true; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public double getDensity() { return density; } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void setDensity(double density) { |
| 84 | + this.density = Math.max(density, Shape.EPSILON); |
| 85 | + this.needsUpdate = true; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Comparator<Vector3d> getOrdering() { return ordering; } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void setOrdering(Comparator<Vector3d> ordering) { |
| 93 | + this.ordering = ordering; |
| 94 | + this.needsUpdate = true; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public UUID getUUID() { return uuid; } |
| 99 | + |
| 100 | + @Override |
| 101 | + public DrawContext getDrawContext() { return drawContext; } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void setDrawContext(DrawContext context) { this.drawContext = context; } |
| 105 | + |
| 106 | + @Override |
| 107 | + public DefaultPointSampler clone() { |
| 108 | + try { |
| 109 | + DefaultPointSampler copy = (DefaultPointSampler) super.clone(); |
| 110 | + // Don't share the cache |
| 111 | + copy.cachedPoints = new LinkedHashSet<>(); |
| 112 | + copy.needsUpdate = true; |
| 113 | + if (drawContext != null) |
| 114 | + copy.drawContext = drawContext.copy(); |
| 115 | + return copy; |
| 116 | + } catch (CloneNotSupportedException e) { |
| 117 | + throw new AssertionError(e); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private record CacheState(SamplingStyle style, int orientationHash, double scale, |
| 122 | + int offsetHash, double density, long shapeVersion) {} |
| 123 | +} |
0 commit comments