-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f771a6
commit 91729b3
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/com/github/stephengold/joltjni/vhacd/ConvexHull.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
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); | ||
const 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; | ||
} |