Skip to content

Commit

Permalink
Adds a common IIntersectable interface to GamaPoint and Envelope3D
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Jul 31, 2021
1 parent e40f2cb commit 7a717ec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
16 changes: 6 additions & 10 deletions msi.gama.core/src/msi/gama/common/geometry/Envelope3D.java
Expand Up @@ -20,7 +20,6 @@
import msi.gama.common.util.PoolUtils;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.metamodel.shape.GamaShape;

import msi.gama.metamodel.shape.IShape;
import msi.gaml.operators.Comparison;
import msi.gaml.types.GamaGeometryType;
Expand All @@ -35,10 +34,10 @@
* @adapted for GAMA by A. Drogoul
*
*/
public class Envelope3D extends Envelope implements IDisposable {
public class Envelope3D extends Envelope implements IDisposable, IIntersectable {

private final static PoolUtils.ObjectPool<Envelope3D> POOL =
PoolUtils.create("Envelope 3D", true, () -> new Envelope3D(), (from, to) -> to.set(from), null);
PoolUtils.create("Envelope 3D", true, Envelope3D::new, (from, to) -> to.set(from), null);

public static final Envelope3D EMPTY = create();

Expand Down Expand Up @@ -380,7 +379,7 @@ public GamaPoint centre() {
@Override
public boolean intersects(final Envelope other) {
if (!super.intersects(other)) return false;
return !(getMinZOf(other) > maxz || getMaxZOf(other) < minz);
return getMinZOf(other) <= maxz && getMaxZOf(other) >= minz;
}

/**
Expand Down Expand Up @@ -408,7 +407,7 @@ public boolean intersects(final Coordinate p) {
*/
protected boolean intersects(final double x, final double y, final double z) {
if (isNull()) return false;
return intersects(x, y) && !(z < minz || z > maxz);
return intersects(x, y) && z >= minz && z <= maxz;
}

/**
Expand Down Expand Up @@ -448,8 +447,7 @@ public boolean covers(final Coordinate p) {
*/
@Override
public boolean covers(final Envelope other) {
if (isNull() || other.isNull()) return false;
if (!super.covers(other)) return false;
if (isNull() || other.isNull() || !super.covers(other)) return false;
return getMinZOf(other) >= minz && getMaxZOf(other) <= maxz;
}

Expand Down Expand Up @@ -488,9 +486,7 @@ public double distance(final Envelope env) {

// ---------------------------------------------------------------------------------------------------------------

private Envelope3D() {
super();
}
private Envelope3D() {}

/**
* Computes the intersection of two {@link Envelope}s.
Expand Down
14 changes: 14 additions & 0 deletions msi.gama.core/src/msi/gama/common/geometry/IIntersectable.java
@@ -0,0 +1,14 @@
package msi.gama.common.geometry;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;

import msi.gama.common.interfaces.IDisposable;

public interface IIntersectable extends IDisposable {

boolean intersects(Envelope env);

boolean intersects(Coordinate env);

}
14 changes: 13 additions & 1 deletion msi.gama.core/src/msi/gama/metamodel/shape/GamaPoint.java
Expand Up @@ -14,11 +14,13 @@
import static msi.gaml.operators.Maths.round;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.util.NumberUtil;

import msi.gama.common.geometry.Envelope3D;
import msi.gama.common.geometry.GeometryUtils;
import msi.gama.common.geometry.IIntersectable;
import msi.gama.common.interfaces.BiConsumerWithPruning;
import msi.gama.common.interfaces.IAttributed;
import msi.gama.common.interfaces.IKeyword;
Expand Down Expand Up @@ -56,7 +58,7 @@
name = IKeyword.Z,
type = IType.FLOAT,
doc = { @doc ("Returns the z ordinate of this point") }) })
public class GamaPoint extends Coordinate implements IShape, ILocation {
public class GamaPoint extends Coordinate implements IShape, ILocation, IIntersectable {

public GamaPoint() {
x = 0.0d;
Expand Down Expand Up @@ -664,4 +666,14 @@ public GamaPoint toGamaPoint() {
return this;
}

@Override
public boolean intersects(final Envelope env) {
return env.intersects(this);
}

@Override
public boolean intersects(final Coordinate env) {
return this.equals3D(env);
}

}

0 comments on commit 7a717ec

Please sign in to comment.