diff --git a/src/main/java/com/github/stephengold/joltjni/ScaledShape.java b/src/main/java/com/github/stephengold/joltjni/ScaledShape.java new file mode 100644 index 00000000..3f620cca --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/ScaledShape.java @@ -0,0 +1,76 @@ +/* +Copyright (c) 2024 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; + +/** + * Apply scaling to an existing {@code Shape}. + * + * @author Stephen Gold sgold@sonic.net + */ +public class ScaledShape extends DecoratedShape { + // ************************************************************************* + // constructors + + /** + * Instantiate a shape based on the specified shape and scale factors. + * + * @param baseShape the unscaled base shape (not null) + * @param scaleFactors the desired scale factors (not null) + */ + public ScaledShape(Shape baseShape, Vec3 scaleFactors) { + long baseShapeVa = baseShape.va(); + float scaleX = scaleFactors.getX(); + float scaleY = scaleFactors.getY(); + float scaleZ = scaleFactors.getZ(); + long scaledShapeVa + = createScaledShape(baseShapeVa, scaleX, scaleY, scaleZ); + setVirtualAddress(scaledShapeVa, true); + } + // ************************************************************************* + // new methods exposed + + /** + * Copy the shape's scale factors. + * + * @return a new vector + */ + public Vec3 getScale() { + long scaledVa = va(); + float scaleX = getScaleX(scaledVa); + float scaleY = getScaleY(scaledVa); + float scaleZ = getScaleZ(scaledVa); + Vec3 result = new Vec3(scaleX, scaleY, scaleZ); + + return result; + } + // ************************************************************************* + // native private methods + + native private static long createScaledShape( + long baseShapeVa, float scaleX, float scaleY, float scaleZ); + + native private static float getScaleX(long scaledVa); + + native private static float getScaleY(long scaledVa); + + native private static float getScaleZ(long scaledVa); +} diff --git a/src/main/native/glue/com_github_stephengold_joltjni_ScaledShape.cpp b/src/main/native/glue/com_github_stephengold_joltjni_ScaledShape.cpp new file mode 100644 index 00000000..f977c353 --- /dev/null +++ b/src/main/native/glue/com_github_stephengold_joltjni_ScaledShape.cpp @@ -0,0 +1,85 @@ +/* +Copyright (c) 2024 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 +#include +#include "auto/com_github_stephengold_joltjni_ScaledShape.h" + +using namespace JPH; + +/* + * Class: com_github_stephengold_joltjni_ScaledShape + * Method: createScaledShape + * Signature: (JFFF)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_ScaledShape_createScaledShape + (JNIEnv *, jclass, jlong baseShapeVa, jfloat x, jfloat y, jfloat z) { + const Shape * const pBase = reinterpret_cast (baseShapeVa); + Vec3 factors(x, y, z); + ScaledShape * const pResult = new ScaledShape(pBase, factors); + return reinterpret_cast (pResult); +} + +inline static const Vec3& getScale(jlong scaledVa) { + const ScaledShape * const pScaled + = reinterpret_cast (scaledVa); + const Vec3& result = pScaled->GetScale(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_ScaledShape + * Method: getScaleX + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_ScaledShape_getScaleX + (JNIEnv *, jclass, jlong scaledVa) { + const Vec3& factors = getScale(scaledVa); + float result = factors.GetX(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_ScaledShape + * Method: getScaleY + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_ScaledShape_getScaleY + (JNIEnv *, jclass, jlong scaledVa) { + const Vec3& factors = getScale(scaledVa); + float result = factors.GetY(); + return result; +} +/* + * Class: com_github_stephengold_joltjni_ScaledShape + * Method: getScaleZ + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_ScaledShape_getScaleZ + (JNIEnv *, jclass, jlong scaledVa) { + const Vec3& factors = getScale(scaledVa); + float result = factors.GetZ(); + return result; +}