|
3 | 3 | import static micycle.pgs.PGS_Conversion.fromPShape; |
4 | 4 | import static micycle.pgs.PGS_Conversion.toPShape; |
5 | 5 |
|
| 6 | +import java.util.List; |
| 7 | + |
6 | 8 | import org.geotools.geometry.jts.JTS; |
| 9 | +import org.locationtech.jts.densify.Densifier; |
7 | 10 | import org.locationtech.jts.geom.Geometry; |
8 | 11 | import org.locationtech.jts.geom.LineString; |
| 12 | +import org.locationtech.jts.geom.Point; |
9 | 13 | import org.locationtech.jts.geom.Polygon; |
| 14 | +import org.locationtech.jts.geom.util.GeometryFixer; |
10 | 15 | import org.locationtech.jts.simplify.DouglasPeuckerSimplifier; |
11 | 16 | import org.locationtech.jts.simplify.TopologyPreservingSimplifier; |
12 | 17 | import org.locationtech.jts.simplify.VWSimplifier; |
|
17 | 22 | import micycle.pgs.utility.GaussianLineSmoothing; |
18 | 23 | import processing.core.PConstants; |
19 | 24 | import processing.core.PShape; |
| 25 | +import processing.core.PVector; |
20 | 26 | import uk.osgb.algorithm.minkowski_sum.Minkowski_Sum; |
| 27 | +import micycle.uniformnoise.UniformNoise; |
21 | 28 |
|
22 | 29 | /** |
23 | 30 | * Methods that affect the geometry or topology of shapes. |
@@ -214,4 +221,133 @@ public static PShape chaikinCut(PShape shape, double ratio, int iterations) { |
214 | 221 | return cut; |
215 | 222 | } |
216 | 223 |
|
| 224 | + /** |
| 225 | + * Warps/perturbs a shape by displacing vertices along a line between each |
| 226 | + * vertex and the shape centroid. |
| 227 | + * |
| 228 | + * <p> |
| 229 | + * Inputs may be densified before warping. |
| 230 | + * |
| 231 | + * @param shape a polygonal shape |
| 232 | + * @param magnitude magnitude of the displacement. The value defines the |
| 233 | + * maximum euclidean displacement of a vertex compared to the |
| 234 | + * shape centroid |
| 235 | + * @param warpOffset offset angle that determines at which angle to begin the |
| 236 | + * displacement. |
| 237 | + * @param densify whether to densify the shape (using distance=1) before |
| 238 | + * warping. When true, shapes with long edges will undergo |
| 239 | + * warping along the whole edge (rather than only at the |
| 240 | + * original vertices). |
| 241 | + * @return |
| 242 | + */ |
| 243 | + public static PShape radialWarp(PShape shape, double magnitude, double warpOffset, boolean densify) { |
| 244 | + Geometry g = fromPShape(shape); |
| 245 | + if (!g.getGeometryType().equals(Geometry.TYPENAME_POLYGON)) { |
| 246 | + System.err.println("radialWarp() expects (single) polygon input. The geometry resolved to a " + g.getGeometryType()); |
| 247 | + return shape; |
| 248 | + } |
| 249 | + |
| 250 | + final Point point = g.getCentroid(); |
| 251 | + final PVector c = new PVector((float) point.getX(), (float) point.getY()); |
| 252 | + |
| 253 | + final List<PVector> coords; |
| 254 | + |
| 255 | + if (densify) { |
| 256 | + final Densifier d = new Densifier(fromPShape(shape)); |
| 257 | + d.setDistanceTolerance(1); |
| 258 | + d.setValidate(false); |
| 259 | + coords = PGS_Conversion.toPVector(toPShape(d.getResultGeometry())); |
| 260 | + } else { |
| 261 | + coords = PGS_Conversion.toPVector(shape); |
| 262 | + } |
| 263 | + |
| 264 | + final UniformNoise noise = new UniformNoise(1337); |
| 265 | + coords.forEach(coord -> { |
| 266 | + PVector heading = PVector.sub(coord, c); // vector from center to each vertex |
| 267 | + final double angle = heading.heading() + warpOffset; |
| 268 | + float perturbation = noise.uniformNoise(Math.cos(angle), Math.sin(angle)); |
| 269 | + perturbation -= 0.5f; // [0...1] -> [-0.5...0.5] |
| 270 | + perturbation *= magnitude * 2; |
| 271 | + coord.add(heading.normalize().mult(perturbation)); // add perturbation to vertex |
| 272 | + }); |
| 273 | + return PGS_Conversion.fromPVector(coords); |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Warps/perturbs a shape by displacing vertices according to a 2D noise vector |
| 278 | + * field. |
| 279 | + * |
| 280 | + * <p> |
| 281 | + * Inputs may be densified before warping. |
| 282 | + * |
| 283 | + * @param shape a polygonal shape |
| 284 | + * @param magnitude magnitude of the displacement (acting as noise value |
| 285 | + * multiplier). The value defines the maximum displacement of |
| 286 | + * a vertex in the both x and y axes. |
| 287 | + * @param noiseScale the scale of the 2D noise vector field. This affects how of |
| 288 | + * the coarseness of warping. Smaller values (~0.2) lead to |
| 289 | + * more fine warping (at edges), whereas larger values (~2) |
| 290 | + * affect the shape geometry at a larger scale. |
| 291 | + * @param densify whether to densify the shape (using distance=1) before |
| 292 | + * warping. When true, shapes with long edges will undergo |
| 293 | + * warping along the whole edge (rather than only at the |
| 294 | + * original vertices). |
| 295 | + * @return |
| 296 | + * @see #fieldWarp(PShape, double, double, double, boolean, int) |
| 297 | + */ |
| 298 | + public static PShape fieldWarp(PShape shape, double magnitude, double noiseScale, boolean densify) { |
| 299 | + return fieldWarp(shape, magnitude, noiseScale, 0, densify, 1337); |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Warps/perturbs a shape by displacing vertices according to a 2D noise vector |
| 304 | + * field. |
| 305 | + * |
| 306 | + * <p> |
| 307 | + * Inputs may be densified before warping. |
| 308 | + * |
| 309 | + * @param shape a polygonal shape |
| 310 | + * @param magnitude magnitude of the displacement (acting as noise value |
| 311 | + * multiplier). The value defines the maximum displacement of |
| 312 | + * a vertex in the both x and y axes. |
| 313 | + * @param noiseScale the scale of the 2D noise vector field. This affects how of |
| 314 | + * the coarseness of warping. Smaller values (~0.2) lead to |
| 315 | + * more fine warping (at edges), whereas larger values (~2) |
| 316 | + * affect the shape geometry at a larger scale. |
| 317 | + * @param time used to offset the underlying noise field and hence animate |
| 318 | + * the warping over time |
| 319 | + * @param densify whether to densify the shape (using distance=1) before |
| 320 | + * warping. When true, shapes with long edges will undergo |
| 321 | + * warping along the whole edge (rather than only at the |
| 322 | + * original vertices). |
| 323 | + * @param noiseSeed a seed to pass to the underlying noise generator |
| 324 | + * |
| 325 | + * @see #fieldWarp(PShape, double, double, boolean) |
| 326 | + * @return |
| 327 | + */ |
| 328 | + public static PShape fieldWarp(PShape shape, double magnitude, double noiseScale, double time, boolean densify, int noiseSeed) { |
| 329 | + float scale = (float) noiseScale * 500f; |
| 330 | + final List<PVector> coords; |
| 331 | + |
| 332 | + if (densify) { |
| 333 | + final Densifier d = new Densifier(fromPShape(shape)); |
| 334 | + d.setDistanceTolerance(1); |
| 335 | + d.setValidate(false); |
| 336 | + coords = PGS_Conversion.toPVector(toPShape(d.getResultGeometry())); |
| 337 | + } else { |
| 338 | + coords = PGS_Conversion.toPVector(shape); |
| 339 | + } |
| 340 | + |
| 341 | + final UniformNoise noise = new UniformNoise(noiseSeed); |
| 342 | + |
| 343 | + coords.forEach(coord -> { |
| 344 | +// float dx = noise.uniformNoise(coord.x / scale, coord.y / scale, time) - 0.5f; |
| 345 | + float dx = noise.uniformNoise(coord.x / scale, coord.y / scale + time) - 0.5f; |
| 346 | +// float dy = noise.uniformNoise(coord.x / scale, coord.y / scale, 100 + time) - 0.5f; |
| 347 | + float dy = noise.uniformNoise(coord.x / scale + (101 + time), coord.y / scale + (101 + time)) - 0.5f; |
| 348 | + coord.add(dx * (float) magnitude * 2, dy * (float) magnitude * 2); |
| 349 | + }); |
| 350 | + return toPShape(GeometryFixer.fix(fromPShape(PGS_Conversion.fromPVector(coords)))); |
| 351 | + } |
| 352 | + |
217 | 353 | } |
0 commit comments