Skip to content

Commit

Permalink
Reduces the number of resources used to draw arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Mar 4, 2022
1 parent c023c8c commit 07abedd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 63 deletions.
65 changes: 36 additions & 29 deletions msi.gama.core/src/msi/gama/common/geometry/GamaGeometryFactory.java
@@ -1,22 +1,23 @@
/*******************************************************************************************************
*
* GamaGeometryFactory.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* GamaGeometryFactory.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gama.common.geometry;

import java.util.List;

import org.apache.commons.lang.ArrayUtils;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.geom.CoordinateSequenceFactory;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.LinearRing;
Expand All @@ -32,7 +33,7 @@ public class GamaGeometryFactory extends GeometryFactory {

/** The Constant COORDINATES_FACTORY. */
public static final GamaCoordinateSequenceFactory COORDINATES_FACTORY = new GamaCoordinateSequenceFactory();

/** The Constant JTS_COORDINATES_FACTORY. */
public static final CoordinateSequenceFactory JTS_COORDINATES_FACTORY = CoordinateArraySequenceFactory.instance();

Expand All @@ -43,28 +44,39 @@ public GamaGeometryFactory() {
super(COORDINATES_FACTORY);
}

/**
* Creates a new GamaGeometry object.
*
* @param geometries
* the geometries
* @return the geometry collection
*/
public GeometryCollection createCollection(final Geometry... geometries) {
return new GeometryCollection(geometries, this);
}

/**
* Checks if is ring.
*
* @param pts the pts
* @param pts
* the pts
* @return true, if is ring
*/
public static boolean isRing(final Coordinate[] pts) {
if (pts.length < 4) { return false; }
if (!pts[0].equals(pts[pts.length - 1])) { return false; }
if (pts.length < 4 || !pts[0].equals(pts[pts.length - 1])) return false;
return true;
}

/**
* Checks if is ring.
*
* @param pts the pts
* @param pts
* the pts
* @return true, if is ring
*/
public static boolean isRing(final List<GamaPoint> pts) {
final int size = pts.size();
if (size < 4) { return false; }
if (!pts.get(0).equals(pts.get(size - 1))) { return false; }
if (size < 4 || !pts.get(0).equals(pts.get(size - 1))) return false;
return true;
}

Expand All @@ -81,24 +93,22 @@ public static boolean isRing(final List<GamaPoint> pts) {
@Override
public LinearRing createLinearRing(final Coordinate[] coordinates) {
Coordinate[] coords = coordinates;
if (!isRing(coords)) {
coords = (Coordinate[]) ArrayUtils.add(coords, coords[0]);
}
if (!isRing(coords)) { coords = (Coordinate[]) ArrayUtils.add(coords, coords[0]); }
return createLinearRing(JTS_COORDINATES_FACTORY.create(coords));
}

/**
* Builds the rectangle.
*
* @param points the points
* @param points
* the points
* @return the polygon
*/
public Polygon buildRectangle(final Coordinate[] points) {
final CoordinateSequenceFactory fact = GamaGeometryFactory.COORDINATES_FACTORY;
final CoordinateSequence cs = fact.create(points);
final LinearRing geom = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(cs);
final Polygon p = GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null);
return p;
return GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null);
}

/**
Expand All @@ -108,35 +118,32 @@ public Polygon buildRectangle(final Coordinate[] points) {
@Override
public Polygon createPolygon(final LinearRing shell, final LinearRing[] holes) {
final LinearRing shellClockwise = turnClockwise(shell);
if (holes != null) {
for (int i = 0; i < holes.length; i++) {
holes[i] = turnClockwise(holes[i]);
}
}
if (holes != null) { for (int i = 0; i < holes.length; i++) { holes[i] = turnClockwise(holes[i]); } }
return super.createPolygon(shellClockwise, holes);
}

/**
* Turn clockwise.
*
* @param ring the ring
* @param ring
* the ring
* @return the linear ring
*/
private LinearRing turnClockwise(final LinearRing ring) {
if (ring == null || ring.isEmpty()) { return ring; }
if (ring == null || ring.isEmpty()) return ring;
return createLinearRing(COORDINATES_FACTORY.create(ring.getCoordinateSequence()));
}

@Override
public GamaCoordinateSequenceFactory getCoordinateSequenceFactory() {
return COORDINATES_FACTORY;
}
public GamaCoordinateSequenceFactory getCoordinateSequenceFactory() { return COORDINATES_FACTORY; }

/**
* Creates a new GamaGeometry object.
*
* @param coordinates the coordinates
* @param copyPoints the copy points
* @param coordinates
* the coordinates
* @param copyPoints
* the copy points
* @return the line string
*/
public LineString createLineString(final GamaPoint[] coordinates, final boolean copyPoints) {
Expand Down
61 changes: 36 additions & 25 deletions msi.gama.core/src/msi/gaml/statements/draw/ShapeExecuter.java
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* ShapeExecuter.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* ShapeExecuter.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gaml.statements.draw;

Expand All @@ -22,7 +22,6 @@
import static msi.gaml.types.GamaGeometryType.buildArrow;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import org.locationtech.jts.geom.Envelope;
Expand All @@ -49,26 +48,30 @@ class ShapeExecuter extends DrawExecuter {

/** The begin arrow. */
final IExpression endArrow, beginArrow;

/** The constant shape. */
final IShape constantShape;

/** The constant begin. */
final Double constantEnd, constantBegin;

/** The has arrows. */
final boolean hasArrows;

/** The center. */
final GamaPoint center = new GamaPoint();

/**
* Instantiates a new shape executer.
*
* @param item the item
* @param beginArrow the begin arrow
* @param endArrow the end arrow
* @throws GamaRuntimeException the gama runtime exception
* @param item
* the item
* @param beginArrow
* the begin arrow
* @param endArrow
* the end arrow
* @throws GamaRuntimeException
* the gama runtime exception
*/
ShapeExecuter(final IExpression item, final IExpression beginArrow, final IExpression endArrow)
throws GamaRuntimeException {
Expand Down Expand Up @@ -145,7 +148,7 @@ Rectangle2D executeOn(final IGraphicsScope scope, final IGraphics gr, final Draw
final Envelope3D e = shape.getEnvelope();
try {
final Envelope visible = gr.getVisibleRegion();
if ((visible != null) && !visible.intersects(e)) return null;
if (visible != null && !visible.intersects(e)) return null;
} finally {
e.dispose();
}
Expand All @@ -160,9 +163,12 @@ Rectangle2D executeOn(final IGraphicsScope scope, final IGraphics gr, final Draw
/**
* Compute attributes.
*
* @param scope the scope
* @param data the data
* @param shape the shape
* @param scope
* the scope
* @param data
* the data
* @param shape
* the shape
* @return the drawing attributes
*/
DrawingAttributes computeAttributes(final IScope scope, final DrawingData data, final IShape shape) {
Expand Down Expand Up @@ -212,37 +218,42 @@ private Geometry addToroidalParts(final IScope scope, final Geometry shape) {
}

/** The temp arrow list. */
private final List<Geometry> tempArrowList = new ArrayList<>();
// private final List<Geometry> tempArrowList = new ArrayList<>();

/**
* Adds the arrows.
*
* @param scope the scope
* @param g1 the g 1
* @param fill the fill
* @param scope
* the scope
* @param g1
* the g 1
* @param fill
* the fill
* @return the geometry
*/
private Geometry addArrows(final IScope scope, final Geometry g1, final Boolean fill) {
if (g1 == null) return g1;
final GamaPoint[] points = getPointsOf(g1);
final int size = points.length;
if (size < 2) return g1;
tempArrowList.clear();
tempArrowList.add(g1);
Geometry end = null, begin = null;
if (endArrow != null || constantEnd != null) {
final double width = constantEnd == null ? asFloat(scope, endArrow.value(scope)) : constantEnd;
if (width > 0) {
end = buildArrow(points[size - 2], points[size - 1], width, width + width / 3, fill).getInnerGeometry();
tempArrowList.add(end);
}
}
if (beginArrow != null || constantBegin != null) {
final double width = constantBegin == null ? asFloat(scope, beginArrow.value(scope)) : constantBegin;
if (width > 0) {
begin = buildArrow(points[1], points[0], width, width + width / 3, fill).getInnerGeometry();
tempArrowList.add(begin);
}
}
return GEOMETRY_FACTORY.createGeometryCollection(tempArrowList.toArray(new Geometry[tempArrowList.size()]));
if (end == null) {
if (begin == null) return g1;
return GEOMETRY_FACTORY.createCollection(g1, begin);
}
if (begin == null) return GEOMETRY_FACTORY.createCollection(g1, end);
return g1;
}
}
14 changes: 8 additions & 6 deletions ummisco.gama.opengl/src/ummisco/gama/opengl/OpenGL.java
Expand Up @@ -430,14 +430,16 @@ public void updatePerspective(final GL2 gl) {
} catch (final BufferOverflowException e) {
DEBUG.ERR("Buffer overflow exception");
}
} else if (aspect >= 1.0) {
gl.glOrtho(-maxDim * aspect, maxDim * aspect, -maxDim, maxDim, maxDim * 10, -maxDim * 10);
} else {
if (aspect >= 1.0) {
gl.glOrtho(-maxDim * aspect, maxDim * aspect, -maxDim, maxDim, maxDim * 10, -maxDim * 10);
} else {
gl.glOrtho(-maxDim, maxDim, -maxDim / aspect, maxDim / aspect, maxDim, -maxDim);
}
gl.glTranslated(0d, 0d, maxDim * 0.05);
gl.glOrtho(-maxDim, maxDim, -maxDim / aspect, maxDim / aspect, maxDim * 10, -maxDim * 10);
}

// else {
// gl.glOrtho(-maxDim, maxDim, -maxDim, maxDim, maxDim * 10, -maxDim * 10);
// }
gl.glTranslated(0d, 0d, maxDim * 0.2);
getRenderer().getCameraHelper().animate();
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
gl.glGetDoublev(GLMatrixFunc.GL_MODELVIEW_MATRIX, mvmatrix, 0);
Expand Down
Expand Up @@ -116,10 +116,10 @@ public void wipe(final OpenGL gl) {
public void draw(final OpenGL gl) {

gl.push(GLMatrixFunc.GL_MODELVIEW);
gl.setZIncrement(zIncrement);
gl.setZIncrement(renderer.getData().isOrtho() ? 0D : zIncrement);
// AD called here so that it is inside the keystone drawing. See #3285
gl.rotateModel();
layers.forEach((name, layer) -> {
for (LayerObject layer : layers.values()) {
if (layer != null && !layer.isInvalid()) {
try {
layer.lock();
Expand All @@ -129,7 +129,8 @@ public void draw(final OpenGL gl) {
r.printStackTrace();
}
}
});
}

gl.setZIncrement(0);
rendered = true;
IDisplaySynchronizer sync = gl.getRenderer().getSurface().synchronizer;
Expand Down

0 comments on commit 07abedd

Please sign in to comment.