Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ObjArray from Dx(Trimesh)Heightfield #106

Merged
merged 2 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ DxHeightfield.tempPlaneBuffer should probably just be a simple array. ALso check
DxTrimeshHeightfield.tempHeightBuffer??
DxGimpactData.check().edges

Bug: Dropping first box on Heightfield is wrong.
-> Cylinder is also often iffy, but that is the same in C++ -> e.g. 7. cylinder (java) or 11th (c++)!

Fix ENABLE_CONTACT_SORTING / Issue #22

0.5.0 (unreleased)
=====
- Removed unnecessary ObjArray from heightfield. [#106](https://github.com/tzaeschke/ode4j/pull/106)
- Added linter and fixed some lint. [#105](https://github.com/tzaeschke/ode4j/pull/105)
- Cleaned up Junit 3 style tests. [#104](https://github.com/tzaeschke/ode4j/pull/104)
- Moved tests into core/core-cpp. [#103](https://github.com/tzaeschke/ode4j/pull/103)
Expand Down
65 changes: 30 additions & 35 deletions core/src/main/java/org/ode4j/ode/internal/DxHeightfield.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.ode4j.ode.DContactGeomBuffer;
import org.ode4j.ode.DGeom;
import org.ode4j.ode.DHeightfieldData;
import org.ode4j.ode.internal.cpp4j.java.ObjArray;
import org.ode4j.ode.internal.trimesh.DxTriMesh;

/**
Expand All @@ -55,16 +54,16 @@ public class DxHeightfield extends DxAbstractHeightfield {
static final int HEIGHTFIELDMAXCONTACTPERCELL = 10;

//#define dMIN(A,B) ((A)>(B) ? (B) : (A))
private static final double dMIN(double A, double B) { return ((A)>(B) ? (B) : (A)); }
private static double dMIN(double A, double B) { return (Math.min((A), (B))); }
//#define dMAX(A,B) ((A)>(B) ? (A) : (B))
private static final double dMAX(double A, double B) { return ((A)>(B) ? (A) : (B)); }
private static double dMAX(double A, double B) { return (Math.max((A), (B))); }
//
//
////Three-way MIN and MAX
//#define dMIN3(A,B,C) ( (A)<(B) ? dMIN((A),(C)) : dMIN((B),(C)) )
private final double dMIN3(double A, double B, double C) { return A<B ? dMIN(A,C) : dMIN(B,C); }
private double dMIN3(double A, double B, double C) { return A<B ? dMIN(A,C) : dMIN(B,C); }
//#define dMAX3(A,B,C) ( (A)>(B) ? dMAX((A),(C)) : dMAX((B),(C)) )
private final double dMAX3(double A, double B, double C) { return A>B ? dMAX(A,C) : dMAX(B,C); }
private double dMAX3(double A, double B, double C) { return A>B ? dMAX(A,C) : dMAX(B,C); }
//
//#define dOPESIGN(a, op1, op2,b) \
//(a)[0] op1 op2 ((b)[0]); \
Expand Down Expand Up @@ -175,15 +174,16 @@ private static int AlignBufferSize(int value, int alignment) {
// size_t tempHeightBufferSizeX;
// size_t tempHeightBufferSizeZ;
//private HeightFieldPlane[] tempPlaneBuffer;
private ObjArray<HeightFieldPlane>[] tempPlaneBuffer;
private HeightFieldPlane[] tempPlaneBuffer;
private HeightFieldPlane[] tempPlaneInstances;
private int tempPlaneBufferSize;

private HeightFieldTriangle[] tempTriangleBuffer;
private int tempTriangleBufferSize;

// TZ: Instead of pointers into tempHeightInstances[], we store indexes in tempHeightInstances[].
//private HeightFieldVertex[] tempHeightBuffer;
private ObjArray<HeightFieldVertex>[] tempHeightBuffer;
private int[] tempHeightBuffer;
private HeightFieldVertex[] tempHeightInstances;
private int tempHeightBufferSizeX;
private int tempHeightBufferSizeZ;
Expand Down Expand Up @@ -389,7 +389,7 @@ private void allocatePlaneBuffer(int numTri)
int alignedNumTri = AlignBufferSize(numTri, TEMP_PLANE_BUFFER_ELEMENT_COUNT_ALIGNMENT);
tempPlaneBufferSize = alignedNumTri;
//tempPlaneBuffer = new HeightFieldPlane [alignedNumTri];
tempPlaneBuffer = new ObjArray[alignedNumTri];
tempPlaneBuffer = new HeightFieldPlane[alignedNumTri];
tempPlaneInstances = new HeightFieldPlane[alignedNumTri];
for (int indexTri = 0; indexTri != alignedNumTri; indexTri++)
tempPlaneInstances[indexTri] = new HeightFieldPlane();
Expand All @@ -399,7 +399,7 @@ private void allocatePlaneBuffer(int numTri)
{
//tempPlaneBuffer[indexTri] = tempPlaneInstances[indexTri];//ptrPlaneMatrix;
//ptrPlaneMatrix += 1;
tempPlaneBuffer[indexTri] = new ObjArray<HeightFieldPlane>(tempPlaneInstances, indexTri);//ptrPlaneMatrix;
tempPlaneBuffer[indexTri] = tempPlaneInstances[indexTri];
}
}

Expand All @@ -419,7 +419,7 @@ private void allocateHeightBuffer(int numX, int numZ)
tempHeightBufferSizeX = alignedNumX;
tempHeightBufferSizeZ = alignedNumZ;
//tempHeightBuffer = new HeightFieldVertex *[alignedNumX];
tempHeightBuffer = new ObjArray[alignedNumX];
tempHeightBuffer = new int[alignedNumX];
int numCells = alignedNumX * alignedNumZ;
tempHeightInstances = new HeightFieldVertex [numCells];
for (int i = 0; i < tempHeightInstances.length; i++) {
Expand All @@ -431,8 +431,7 @@ private void allocateHeightBuffer(int numX, int numZ)
{
// tempHeightBuffer[indexX] = ptrHeightMatrix;
// ptrHeightMatrix += alignedNumZ;
//tempHeightBuffer[indexX] = tempHeightInstances[indexX];
tempHeightBuffer[indexX] = new ObjArray<HeightFieldVertex>(tempHeightInstances, indexX*alignedNumZ);
tempHeightBuffer[indexX] = indexX * alignedNumZ;
}
}

Expand Down Expand Up @@ -623,11 +622,11 @@ void sortPlanes(final int numPlanes)
for (int i = 0; i < numPlanes - 1; i++)
{
//if they are in the wrong order
if (DescendingPlaneSort(tempPlaneBuffer[i].at0(), tempPlaneBuffer[i + 1].at0()))
if (DescendingPlaneSort(tempPlaneBuffer[i], tempPlaneBuffer[i + 1]))
{
//exchange them
//HeightFieldPlane * tempPlane = tempPlaneBuffer[i];
ObjArray<HeightFieldPlane> tempPlane = tempPlaneBuffer[i];
HeightFieldPlane tempPlane = tempPlaneBuffer[i];
tempPlaneBuffer[i] = tempPlaneBuffer[i + 1];
tempPlaneBuffer[i + 1] = tempPlane;

Expand Down Expand Up @@ -720,7 +719,8 @@ int dCollideHeightfieldZone( final int minX, final int maxX, final int minZ, fin
final double c_Xpos = Xpos;
//HeightFieldVertex HeightFieldRow = tempHeightBuffer[x_local];
//ObjArray<HeightFieldVertex> HeightFieldRow = new ObjArray<HeightFieldVertex>(tempHeightBuffer, x_local);
ObjArray<HeightFieldVertex> HeightFieldRow = tempHeightBuffer[x_local];
int posHeightFieldRow = tempHeightBuffer[x_local];
//HeightFieldVertex HeightFieldRow = tempHeightBuffer[x_local];
for ( z = minZ, z_local = 0; z_local < numZ; z++, z_local++)
{
Ypos = z * cfSampleDepth; // Always calculate pos via multiplication to avoid computational error accumulation during multiple additions
Expand All @@ -729,9 +729,10 @@ int dCollideHeightfieldZone( final int minX, final int maxX, final int minZ, fin
// HeightFieldRow.at(z_local).vertex[0] = c_Xpos;
// HeightFieldRow.at(z_local).vertex[1] = h;
// HeightFieldRow.at(z_local).vertex[2] = Ypos;
HeightFieldRow.at(z_local).vertex.set( c_Xpos, h, Ypos);
HeightFieldRow.at(z_local).coords0 = x;
HeightFieldRow.at(z_local).coords1 = z;
HeightFieldVertex HeightFieldRowZ = tempHeightInstances[posHeightFieldRow + z_local];
HeightFieldRowZ.vertex.set( c_Xpos, h, Ypos);
HeightFieldRowZ.coords0 = x;
HeightFieldRowZ.coords1 = z;

maxY = dMAX(maxY, h);
minY = dMIN(minY, h);
Expand Down Expand Up @@ -1033,31 +1034,25 @@ discontinuities at the grid edges (causing small jumps when the contact
{
// HeightFieldVertex HeightFieldRow = tempHeightBuffer[x_local];
// HeightFieldVertex HeightFieldNextRow = tempHeightBuffer[x_local + 1];
// int posHeightFieldRow = x_local;
// int posHeightFieldNextRow = x_local + 1;
ObjArray<HeightFieldVertex> HeightFieldRow = tempHeightBuffer[x_local];
ObjArray<HeightFieldVertex> HeightFieldNextRow = tempHeightBuffer[x_local + 1];
int posHeightFieldRow = tempHeightBuffer[x_local];
int posHeightFieldNextRow = tempHeightBuffer[x_local + 1];

// First A
//C = &HeightFieldRow [0];
//C = tempHeightBuffer[posHeightFieldRow];// [0];
C = HeightFieldRow.at(0);
C = tempHeightInstances[posHeightFieldRow];// [0];
// First B
//D = &HeightFieldNextRow[0];
//D = tempHeightBuffer[posHeightFieldNextRow];//[0];
D = HeightFieldNextRow.at(0);
D = tempHeightInstances[posHeightFieldNextRow];//[0];

for ( z_local = 0; z_local < maxZ_local; z_local++)
{
A = C;
B = D;

// C = &HeightFieldRow [z_local + 1];
// D = &HeightFieldNextRow[z_local + 1];
// C = tempHeightBuffer[posHeightFieldRow + z_local + 1];// [z_local + 1];
// D = tempHeightBuffer[posHeightFieldNextRow + z_local + 1];//[z_local + 1];
C = HeightFieldRow.at(z_local + 1);// [z_local + 1];
D = HeightFieldNextRow.at(z_local + 1);//[z_local + 1];
// C = &HeightFieldRow [z_local + 1];
// D = &HeightFieldNextRow[z_local + 1];
C = tempHeightInstances[posHeightFieldRow + z_local + 1];// [z_local + 1];
D = tempHeightInstances[posHeightFieldNextRow + z_local + 1];//[z_local + 1];

final double AHeight = A.vertex.get1();
final double BHeight = B.vertex.get1();
Expand Down Expand Up @@ -1138,7 +1133,7 @@ discontinuities at the grid edges (causing small jumps when the contact
//compute all triangles normals.
for (int k = 0; k < numTri; k++)
{
HeightFieldTriangle itTriangle = tempTriangleBuffer[k]; // final? TZ
HeightFieldTriangle itTriangle = tempTriangleBuffer[k];

// define 2 edges and a point that will define collision plane
//dVector3Subtract(itTriangle.vertices[2].vertex, itTriangle.vertices[0].vertex, Edge1);
Expand Down Expand Up @@ -1186,7 +1181,7 @@ discontinuities at the grid edges (causing small jumps when the contact
continue;// already tested or added to plane list.

//HeightFieldPlane * const currPlane = tempPlaneBuffer[numPlanes];
HeightFieldPlane currPlane = tempPlaneBuffer[numPlanes].at0();// final ? TZ
HeightFieldPlane currPlane = tempPlaneBuffer[numPlanes];// final ? TZ
currPlane.resetTriangleListSize(numTri - k);
currPlane.addTriangle(tri_base);
// saves normal for collision check (planes, triangles, vertices and edges.)
Expand Down Expand Up @@ -1257,7 +1252,7 @@ additionally repeated after some contacts have been generated (in "if (didCollid
for (int k = 0; k < numPlanes; k++)
{
//HeightFieldPlane * const itPlane = tempPlaneBuffer[k];
HeightFieldPlane itPlane = tempPlaneBuffer[k].at0();//final TZ
HeightFieldPlane itPlane = tempPlaneBuffer[k];

//set Geom
dGeomPlaneSetNoNormalize (sliding_plane, itPlane.planeDefV, itPlane.planeDefD);
Expand Down
54 changes: 31 additions & 23 deletions core/src/main/java/org/ode4j/ode/internal/DxTrimeshHeightfield.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.ode4j.ode.DGeom;
import org.ode4j.ode.DHeightfieldData;
import org.ode4j.ode.DSpace;
import org.ode4j.ode.internal.cpp4j.java.ObjArray;

/**
*
Expand Down Expand Up @@ -125,7 +124,7 @@ private static int AlignBufferSize(int value, int alignment) {
private int tempTriangleBufferSize;

//private HeightFieldVertex[] tempHeightBuffer;
private ObjArray<HeightFieldVertex>[] tempHeightBuffer;
private int[] tempHeightBuffer;
private HeightFieldVertex[] tempHeightInstances;
private int tempHeightBufferSizeX;
private int tempHeightBufferSizeZ;
Expand Down Expand Up @@ -333,7 +332,7 @@ private void allocateHeightBuffer(int numX, int numZ)
tempHeightBufferSizeX = alignedNumX;
tempHeightBufferSizeZ = alignedNumZ;
//tempHeightBuffer = new HeightFieldVertex *[alignedNumX];
tempHeightBuffer = new ObjArray[alignedNumX];
tempHeightBuffer = new int[alignedNumX];
int numCells = alignedNumX * alignedNumZ;
tempHeightInstances = new HeightFieldVertex [numCells];
for (int i = 0; i < tempHeightInstances.length; i++) {
Expand All @@ -346,7 +345,7 @@ private void allocateHeightBuffer(int numX, int numZ)
// tempHeightBuffer[indexX] = ptrHeightMatrix;
// ptrHeightMatrix += alignedNumZ;
//tempHeightBuffer[indexX] = tempHeightInstances[indexX];
tempHeightBuffer[indexX] = new ObjArray<HeightFieldVertex>(tempHeightInstances, indexX*alignedNumZ);
tempHeightBuffer[indexX] = indexX * alignedNumZ;
}
}

Expand Down Expand Up @@ -414,7 +413,8 @@ class HeightFieldVertex
// HeightFieldEdge(){};
//
// //HeightFieldVertex *vertices[2];
// HeightFieldVertex[] vertices = new HeightFieldVertex[2]; //TODO v1/v1 (TZ)
// HeightFieldVertex[] vertices = new HeightFieldVertex[2];
// HeightFieldVertex[] virtices0, vertices1; // TZ: This is better than an array[2]
// };

private class HeightFieldTriangle
Expand All @@ -423,7 +423,8 @@ private class HeightFieldTriangle
// HeightFieldTriangle(){};

//HeightFieldVertex *vertices[3];
HeightFieldVertex[] vertices = new HeightFieldVertex[3]; //TODO c1, c2, c3 (TZ)
// HeightFieldVertex[] vertices = new HeightFieldVertex[3]; //TODO c1, c2, c3 (TZ)
HeightFieldVertex vertices0, vertices1, vertices2;
//double[] planeDef=new double[4];
}

Expand Down Expand Up @@ -474,7 +475,7 @@ int dCollideHeightfieldZone( final int minX, final int maxX, final int minZ, fin
final double c_Xpos = Xpos;
//HeightFieldVertex HeightFieldRow = tempHeightBuffer[x_local];
//ObjArray<HeightFieldVertex> HeightFieldRow = new ObjArray<HeightFieldVertex>(tempHeightBuffer, x_local);
ObjArray<HeightFieldVertex> HeightFieldRow = tempHeightBuffer[x_local];
int posHeightFieldRow = tempHeightBuffer[x_local];
for ( z = minZ, z_local = 0; z_local < numZ; z++, z_local++)
{
Ypos = z * cfSampleDepth; // Always calculate pos via multiplication to avoid computational error accumulation during multiple additions
Expand All @@ -483,9 +484,10 @@ int dCollideHeightfieldZone( final int minX, final int maxX, final int minZ, fin
// HeightFieldRow.at(z_local).vertex[0] = c_Xpos;
// HeightFieldRow.at(z_local).vertex[1] = h;
// HeightFieldRow.at(z_local).vertex[2] = Ypos;
HeightFieldRow.at(z_local).vertex.set( c_Xpos, h, Ypos);
HeightFieldRow.at(z_local).coords0 = x;
HeightFieldRow.at(z_local).coords1 = z;
HeightFieldVertex HeightFieldRowZ = tempHeightInstances[posHeightFieldRow + z_local];
HeightFieldRowZ.vertex.set( c_Xpos, h, Ypos);
HeightFieldRowZ.coords0 = x;
HeightFieldRowZ.coords1 = z;

maxY = dMAX(maxY, h);
minY = dMIN(minY, h);
Expand Down Expand Up @@ -542,18 +544,22 @@ int dCollideHeightfieldZone( final int minX, final int maxX, final int minZ, fin

for ( x_local = 0; x_local < maxX_local; x_local++)
{
ObjArray<HeightFieldVertex> HeightFieldRow = tempHeightBuffer[x_local];
ObjArray<HeightFieldVertex> HeightFieldNextRow = tempHeightBuffer[x_local + 1];
// ObjArray<HeightFieldVertex> HeightFieldRow = tempHeightBuffer[x_local];
// ObjArray<HeightFieldVertex> HeightFieldNextRow = tempHeightBuffer[x_local + 1];
int posHeightFieldRow = tempHeightBuffer[x_local];
int posHeightFieldNextRow = tempHeightBuffer[x_local + 1];

C = HeightFieldRow.at(0);
D = HeightFieldNextRow.at(0);
C = tempHeightInstances[posHeightFieldRow];//.at(0);
D = tempHeightInstances[posHeightFieldNextRow];//.at(0);

for ( z_local = 0; z_local < maxZ_local; z_local++)
{
A = C;
B = D;
C = HeightFieldRow.at(z_local + 1);// [z_local + 1];
D = HeightFieldNextRow.at(z_local + 1);//[z_local + 1];
// C = HeightFieldRow.at(z_local + 1);// [z_local + 1];
// D = HeightFieldNextRow.at(z_local + 1);//[z_local + 1];
C = tempHeightInstances[posHeightFieldRow + z_local + 1];// [z_local + 1];
D = tempHeightInstances[posHeightFieldNextRow + z_local + 1];//[z_local + 1];

final double AHeight = A.vertex.get1();
final double BHeight = B.vertex.get1();
Expand All @@ -571,16 +577,16 @@ int dCollideHeightfieldZone( final int minX, final int maxX, final int minZ, fin
if (isACollide || isBCollide || isCCollide)
{
HeightFieldTriangle CurrTriUp = tempTriangleBuffer[numTri++];// final ?? TZ
CurrTriUp.vertices[0] = A;
CurrTriUp.vertices[1] = C;
CurrTriUp.vertices[2] = B;
CurrTriUp.vertices0 = A;
CurrTriUp.vertices1 = C;
CurrTriUp.vertices2 = B;
}
if (isBCollide || isCCollide || isDCollide)
{
HeightFieldTriangle CurrTriDown = tempTriangleBuffer[numTri++];//final ?? TZ
CurrTriDown.vertices[0] = D;
CurrTriDown.vertices[1] = B;
CurrTriDown.vertices[2] = C;
CurrTriDown.vertices0 = D;
CurrTriDown.vertices1 = B;
CurrTriDown.vertices2 = C;
}
}
}
Expand All @@ -592,8 +598,10 @@ int dCollideHeightfieldZone( final int minX, final int maxX, final int minZ, fin
for (int k = 0; k < numTri; k++) {
HeightFieldTriangle itTriangle = tempTriangleBuffer[k];
for (int j = 0; j < 3; j ++) {
HeightFieldVertex hFVertex = j == 0 ? itTriangle.vertices0 : j == 1 ? itTriangle.vertices1 : itTriangle.vertices2;
for (int i = 0; i < 3; i ++) {
vertices[k * 9 + j * 3 + i] = (float) itTriangle.vertices[j].vertex.get(i);
// vertices[k * 9 + j * 3 + i] = (float) itTriangle.vertices[j].vertex.get(i);
vertices[k * 9 + j * 3 + i] = (float) hFVertex.vertex.get(i);
}
faces[k * 3 + j] = k * 3 + j;
}
Expand Down