Skip to content

Commit

Permalink
add the ConvexHull class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 9, 2025
1 parent 2f771a6 commit 1e2a532
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ $(N)/glue/co/ContactListener.cpp \
$(N)/glue/co/ContactManifold.cpp \
$(N)/glue/co/ContactSettings.cpp \
$(N)/glue/co/ConvexHullBuilder.cpp \
$(N)/glue/co/ConvexHull.cpp \
$(N)/glue/co/ConvexHullShape.cpp \
$(N)/glue/co/ConvexHullShapeSettings.cpp \
$(N)/glue/co/ConvexShape.cpp \
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/vhacd/ConvexHull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright (c) 2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.github.stephengold.joltjni.vhacd;

import com.github.stephengold.joltjni.Jolt;
import com.github.stephengold.joltjni.JoltPhysicsObject;
import java.nio.FloatBuffer;

/**
* An immutable 3-D convex hull generated by the V-HACD algorithm. (native
* class: {@code IVHACD::ConvexHull})
*/
public class ConvexHull extends JoltPhysicsObject {
// *************************************************************************
// constants

/**
* number of axes in the coordinate system
*/
final private static int numAxes = 3;
// *************************************************************************
// constructors

/**
* Instantiate a hull with the specified native object assigned.
*
* @param hullVa the virtual address of the native object to assign (not
* zero)
* @param owner {@code true} → make the JVM object the owner,
* {@code false} → it isn't the owner
*/
ConvexHull(long hullVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(hullVa) : null;
setVirtualAddress(hullVa, freeingAction);
}
// *************************************************************************
// new methods exposed

/**
* Count the points.
*
* @return the count (≥1)
*/
public int countPoints() {
long hullVa = va();
int result = countPoints(hullVa);

return result;
}

/**
* Copy the point locations to a new direct buffer.
*
* @return the new buffer (capacity a positive multiple of 3)
*/
public FloatBuffer getPointsAsBuffer() {
long hullVa = va();
int numPoints = countPoints(hullVa);
int numFloats = numPoints * numAxes;
assert numFloats > 0 : numFloats;

FloatBuffer result = Jolt.newDirectFloatBuffer(numFloats);
copyPoints(hullVa, result);

return result;
}
// *************************************************************************
// native private methods

native private static void copyPoints(long hullVa, FloatBuffer result);

native private static int countPoints(long hullVa);

native private static void free(long hullVa);
}
83 changes: 83 additions & 0 deletions src/main/native/glue/co/ConvexHull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright (c) 2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/*
* Author: Stephen Gold
*/
#include "Jolt/Jolt.h"
#include "VHACD.h"
#include "auto/com_github_stephengold_joltjni_vhacd_ConvexHull.h"
#include "glue/glue.h"

using namespace VHACD;

/*
* Class: com_github_stephengold_joltjni_vhacd_ConvexHull
* Method: copyPoints
* Signature: (JLjava/nio/FloatBuffer;)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_vhacd_ConvexHull_copyPoints
(JNIEnv *pEnv, jclass, jlong hullVa, jobject resultBuffer) {
const IVHACD::ConvexHull * const pHull
= reinterpret_cast<IVHACD::ConvexHull *> (hullVa);
jfloat * const pPoints
= (jfloat *) pEnv->GetDirectBufferAddress(resultBuffer);
JPH_ASSERT(!pEnv->ExceptionCheck());
const jlong capacityFloats = pEnv->GetDirectBufferCapacity(resultBuffer);
JPH_ASSERT(!pEnv->ExceptionCheck());
const size_t numPoints = pHull->m_points.size();
for (size_t i = 0; i < numPoints; ++i) {
const size_t baseIndex = 3 * i;
if (baseIndex + 2 >= capacityFloats) {
break;
}
pPoints[baseIndex] = pHull->m_points[i].mX;
pPoints[baseIndex + 1] = pHull->m_points[i].mY;
pPoints[baseIndex + 2] = pHull->m_points[i].mZ;
}
}

/*
* Class: com_github_stephengold_joltjni_vhacd_ConvexHull
* Method: countPoints
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_vhacd_ConvexHull_countPoints
(JNIEnv *, jclass, jlong hullVa) {
const IVHACD::ConvexHull * const pHull
= reinterpret_cast<IVHACD::ConvexHull *> (hullVa);
size_t result = pHull->m_points.size();
return result;
}

/*
* Class: com_github_stephengold_joltjni_vhacd_ConvexHull
* Method: free
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_vhacd_ConvexHull_free
(JNIEnv *, jclass, jlong hullVa) {
IVHACD::ConvexHull * const pHull
= reinterpret_cast<IVHACD::ConvexHull *> (hullVa);
TRACE_DELETE("IVHACD::ConvexHull", pHull)
delete pHull;
}

0 comments on commit 1e2a532

Please sign in to comment.