From 3798dfc51ca20b9cb8c55998ea174990f6216382 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 14 May 2023 22:16:42 -0400 Subject: [PATCH] Separate Pools to Pool class --- .../mundus/commons/utils/MathUtils.java | 43 ++-------------- .../mbrlabs/mundus/commons/utils/Pools.java | 49 +++++++++++++++++++ editor/CHANGES | 1 + .../editor/tools/brushes/TerrainBrush.java | 15 +++--- 4 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 commons/src/main/com/mbrlabs/mundus/commons/utils/Pools.java diff --git a/commons/src/main/com/mbrlabs/mundus/commons/utils/MathUtils.java b/commons/src/main/com/mbrlabs/mundus/commons/utils/MathUtils.java index 6f8ddf5ee..281059575 100644 --- a/commons/src/main/com/mbrlabs/mundus/commons/utils/MathUtils.java +++ b/commons/src/main/com/mbrlabs/mundus/commons/utils/MathUtils.java @@ -18,7 +18,6 @@ import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.utils.Pool; /** * @author Marcus Brummer @@ -27,40 +26,6 @@ public class MathUtils { private static final Vector3 tmp = new Vector3(); - public final static Pool vector2Pool = new Pool() { - @Override - protected Vector2 newObject () { - return new Vector2(); - } - - @Override - protected void reset(Vector2 object) { - object.set(0, 0); - } - }; - - public final static Pool vector3Pool = new Pool() { - @Override - protected Vector3 newObject () { - return new Vector3(); - } - - @Override - protected void reset(Vector3 object) { - object.set(0,0,0); - } - }; - - /** - * Convenience method, free array of objects - * @param objects - */ - public static void free(Vector2... objects ) { - for (Vector2 object : objects) { - vector2Pool.free(object); - } - } - public static float barryCentric(Vector3 p1, Vector3 p2, Vector3 p3, Vector2 pos) { float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z); float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det; @@ -129,21 +94,21 @@ public static boolean isPowerOfTwo(int number) { * @param out populated with the nearest point on the line */ public static void findNearestPointOnLine(Vector2 lineStart, Vector2 lineEnd, Vector2 point, Vector2 out) { - Vector2 lineDirection = vector2Pool.obtain().set(lineEnd).sub(lineStart); + Vector2 lineDirection = Pools.vector2Pool.obtain().set(lineEnd).sub(lineStart); // Calculate the length of the line. float lineLength = lineDirection.len(); lineDirection.nor(); // lineStart to point - Vector2 toPoint = vector2Pool.obtain().set(point).sub(lineStart); + Vector2 toPoint = Pools.vector2Pool.obtain().set(point).sub(lineStart); float projectedLength = lineDirection.dot(toPoint); // Calculate the coordinates of the projected point. Vector2 projectedPoint = new Vector2(lineDirection).scl(toPoint.dot(lineDirection)); - vector2Pool.free(lineDirection); - vector2Pool.free(toPoint); + Pools.vector2Pool.free(lineDirection); + Pools.vector2Pool.free(toPoint); if (projectedLength < 0) { out.set(lineStart); diff --git a/commons/src/main/com/mbrlabs/mundus/commons/utils/Pools.java b/commons/src/main/com/mbrlabs/mundus/commons/utils/Pools.java new file mode 100644 index 000000000..938aec937 --- /dev/null +++ b/commons/src/main/com/mbrlabs/mundus/commons/utils/Pools.java @@ -0,0 +1,49 @@ +package com.mbrlabs.mundus.commons.utils; + +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.utils.Pool; + +/** + * Pooling of commonly used objects. + * + * @author JamesTKhan + * @version May 14, 2023 + */ +public class Pools { + + public final static Pool vector2Pool = new Pool() { + @Override + protected Vector2 newObject () { + return new Vector2(); + } + + @Override + protected void reset(Vector2 object) { + object.set(0, 0); + } + }; + + public final static Pool vector3Pool = new Pool() { + @Override + protected Vector3 newObject () { + return new Vector3(); + } + + @Override + protected void reset(Vector3 object) { + object.set(0,0,0); + } + }; + + + /** + * Convenience method, free array of objects + * @param objects objects to free + */ + public static void free(Vector2... objects ) { + for (Vector2 object : objects) { + vector2Pool.free(object); + } + } +} diff --git a/editor/CHANGES b/editor/CHANGES index 948e48603..86eeea775 100644 --- a/editor/CHANGES +++ b/editor/CHANGES @@ -8,6 +8,7 @@ - Added args4j for command line parsing - Added command line option to enable GL30 - Added command line option to enable fullscreen +- Added Pools.java class to manage object pools - Update gradle to 7.5.1 - Updated editor to use MSAA by default, disable by command line - Terrain Assets can now be reused in the same scene and other scenes diff --git a/editor/src/main/com/mbrlabs/mundus/editor/tools/brushes/TerrainBrush.java b/editor/src/main/com/mbrlabs/mundus/editor/tools/brushes/TerrainBrush.java index fad6b399c..c67e5b7f0 100644 --- a/editor/src/main/com/mbrlabs/mundus/editor/tools/brushes/TerrainBrush.java +++ b/editor/src/main/com/mbrlabs/mundus/editor/tools/brushes/TerrainBrush.java @@ -32,6 +32,7 @@ import com.mbrlabs.mundus.commons.terrain.SplatTexture; import com.mbrlabs.mundus.commons.terrain.Terrain; import com.mbrlabs.mundus.commons.utils.MathUtils; +import com.mbrlabs.mundus.commons.utils.Pools; import com.mbrlabs.mundus.editor.Mundus; import com.mbrlabs.mundus.editor.core.project.ProjectManager; import com.mbrlabs.mundus.editor.events.GlobalBrushSettingsChangedEvent; @@ -283,11 +284,11 @@ private void createRamp() { float rampWidth = radius * 2f; float halfWidth = rampWidth * 0.5f; - Vector3 toVertex = MathUtils.vector3Pool.obtain(); - Vector2 nearestPoint = MathUtils.vector2Pool.obtain(); - Vector2 vertexPos2 = MathUtils.vector2Pool.obtain(); - Vector2 startPoint2 = MathUtils.vector2Pool.obtain().set(startPoint.x, startPoint.z); - Vector2 rampEnd2 = MathUtils.vector2Pool.obtain().set(rampEndPoint.x, rampEndPoint.z);; + Vector3 toVertex = Pools.vector3Pool.obtain(); + Vector2 nearestPoint = Pools.vector2Pool.obtain(); + Vector2 vertexPos2 = Pools.vector2Pool.obtain(); + Vector2 startPoint2 = Pools.vector2Pool.obtain().set(startPoint.x, startPoint.z); + Vector2 rampEnd2 = Pools.vector2Pool.obtain().set(rampEndPoint.x, rampEndPoint.z);; for (int i = 0; i < 1; i++) { @@ -319,8 +320,8 @@ private void createRamp() { } } - MathUtils.free(nearestPoint, vertexPos2, startPoint2, rampEnd2); - MathUtils.vector3Pool.free(toVertex); + Pools.free(nearestPoint, vertexPos2, startPoint2, rampEnd2); + Pools.vector3Pool.free(toVertex); terrain.update(); terrainHeightModified = true;