Skip to content

Commit

Permalink
Separate Pools to Pool class
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesTKhan committed May 15, 2023
1 parent 918a789 commit 3798dfc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 46 deletions.
43 changes: 4 additions & 39 deletions commons/src/main/com/mbrlabs/mundus/commons/utils/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,40 +26,6 @@
public class MathUtils {
private static final Vector3 tmp = new Vector3();

public final static Pool<Vector2> vector2Pool = new Pool<Vector2>() {
@Override
protected Vector2 newObject () {
return new Vector2();
}

@Override
protected void reset(Vector2 object) {
object.set(0, 0);
}
};

public final static Pool<Vector3> vector3Pool = new Pool<Vector3>() {
@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;
Expand Down Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/utils/Pools.java
Original file line number Diff line number Diff line change
@@ -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<Vector2> vector2Pool = new Pool<Vector2>() {
@Override
protected Vector2 newObject () {
return new Vector2();
}

@Override
protected void reset(Vector2 object) {
object.set(0, 0);
}
};

public final static Pool<Vector3> vector3Pool = new Pool<Vector3>() {
@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);
}
}
}
1 change: 1 addition & 0 deletions editor/CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++) {

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3798dfc

Please sign in to comment.