Prechádzať zdrojové kódy

resolve issue #1499 (more constructors and methods for ArmatureMask) (#1504)

Stephen Gold 4 rokov pred
rodič
commit
216d2c4f3f

+ 80 - 0
jme3-core/src/main/java/com/jme3/anim/ArmatureMask.java

@@ -6,6 +6,57 @@ public class ArmatureMask implements AnimationMask {
 
     final private BitSet affectedJoints = new BitSet();
 
+    /**
+     * Instantiate a mask that affects no joints.
+     */
+    public ArmatureMask() {
+        // do nothing
+    }
+
+    /**
+     * Instantiate a mask that affects all joints in the specified Armature.
+     *
+     * @param armature the Armature containing the joints (not null, unaffected)
+     */
+    public ArmatureMask(Armature armature) {
+        int numJoints = armature.getJointCount();
+        affectedJoints.set(0, numJoints);
+    }
+
+    /**
+     * Remove all joints affected by the specified ArmatureMask.
+     *
+     * @param removeMask the set of joints to remove (not null, unaffected)
+     * @return this
+     */
+    public ArmatureMask remove(ArmatureMask removeMask) {
+        BitSet removeBits = removeMask.getAffectedJoints();
+        affectedJoints.andNot(removeBits);
+
+        return this;
+    }
+
+    private BitSet getAffectedJoints() {
+        return affectedJoints;
+    }
+
+    /**
+     * Remove the named joints.
+     *
+     * @param armature the Armature containing the joints (not null, unaffected)
+     * @param jointNames the names of the joints to be removed
+     * @return this
+     */
+    public ArmatureMask removeJoints(Armature armature, String... jointNames) {
+        for (String jointName : jointNames) {
+            Joint joint = findJoint(armature, jointName);
+            int jointId = joint.getId();
+            affectedJoints.clear(jointId);
+        }
+
+        return this;
+    }
+
     @Override
     public boolean contains(Object target) {
         return affectedJoints.get(((Joint) target).getId());
@@ -65,4 +116,33 @@ public class ArmatureMask implements AnimationMask {
         }
     }
 
+    /**
+     * Add the specified Joint and all its ancestors.
+     *
+     * @param start the starting point (may be null, unaffected)
+     * @return this
+     */
+    public ArmatureMask addAncestors(Joint start) {
+        for (Joint cur = start; cur != null; cur = cur.getParent()) {
+            int jointId = cur.getId();
+            affectedJoints.set(jointId);
+        }
+
+        return this;
+    }
+
+    /**
+     * Remove the specified Joint and all its ancestors.
+     *
+     * @param start the starting point (may be null, unaffected)
+     * @return this
+     */
+    public ArmatureMask removeAncestors(Joint start) {
+        for (Joint cur = start; cur != null; cur = cur.getParent()) {
+            int jointId = cur.getId();
+            affectedJoints.clear(jointId);
+        }
+
+        return this;
+    }
 }

+ 142 - 0
jme3-core/src/test/java/com/jme3/anim/ArmatureMaskTest.java

@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2021 jMonkeyEngine
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.anim;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test constructors and modification methods of the ArmatureMask class.
+ */
+public class ArmatureMaskTest {
+
+    final private Joint j0 = createJoint("j0", 0);         // leaf
+    final private Joint j1 = createJoint("j1", 1);         // leaf
+    final private Joint j2 = createJoint("j2", 2, j0, j1);
+    final private Joint j3 = createJoint("j3", 3, j2);     // root
+    final private Joint j4 = createJoint("j4", 4);         // leaf
+    final private Joint j5 = createJoint("j5", 5, j4);     // root
+    final private Joint j6 = createJoint("j6", 6);         // root and leaf
+    final private Joint[] jointList = {j0, j1, j2, j3, j4, j5, j6};
+    final private Armature arm = new Armature(jointList);
+
+    private Joint createJoint(String name, int id, Joint... children) {
+        Joint result = new Joint(name);
+        result.setId(id);
+        for (Joint child : children) {
+            result.addChild(child);
+        }
+        return result;
+    }
+
+    /**
+     * Test varous ways to instantiate a mask that affects all joints.
+     */
+    @Test
+    public void testMaskAll() {
+        ArmatureMask[] maskArray = new ArmatureMask[5];
+        maskArray[0] = new ArmatureMask(arm);
+        maskArray[1] = ArmatureMask.createMask(arm,
+                "j0", "j1", "j2", "j3", "j4", "j5", "j6");
+
+        maskArray[2] = ArmatureMask.createMask(arm, "j3");
+        maskArray[2].addFromJoint(arm, "j5");
+        maskArray[2].addFromJoint(arm, "j6");
+
+        maskArray[3] = ArmatureMask.createMask(arm, "j3")
+                .addAncestors(j4)
+                .addAncestors(j6);
+
+        maskArray[4] = ArmatureMask.createMask(arm, "j3");
+        maskArray[4].addBones(arm, "j4", "j5", "j6");
+
+        for (ArmatureMask testMask : maskArray) {
+            for (Joint testJoint : jointList) {
+                Assert.assertTrue(testMask.contains(testJoint));
+            }
+        }
+    }
+
+    /**
+     * Instantiate masks that affect no joints.
+     */
+    @Test
+    public void testMaskNone() {
+        ArmatureMask[] maskArray = new ArmatureMask[4];
+        maskArray[0] = new ArmatureMask();
+        maskArray[1] = ArmatureMask.createMask(arm);
+
+        maskArray[2] = ArmatureMask.createMask(arm, "j2")
+                .removeAncestors(j0)
+                .removeAncestors(j1);
+
+        maskArray[3] = ArmatureMask.createMask(arm, "j0", "j1")
+                .removeJoints(arm, "j0", "j1");
+
+        for (ArmatureMask testMask : maskArray) {
+            for (Joint testJoint : jointList) {
+                Assert.assertFalse(testMask.contains(testJoint));
+            }
+        }
+    }
+
+    /**
+     * Instantiate masks that affect only j1 and j2.
+     */
+    @Test
+    public void testMask12() {
+        ArmatureMask[] maskArray = new ArmatureMask[4];
+        maskArray[0] = new ArmatureMask();
+        maskArray[0].addBones(arm, "j1", "j2");
+
+        maskArray[1] = ArmatureMask.createMask(arm, "j3")
+                .removeJoints(arm, "j0", "j3");
+
+        maskArray[2] = new ArmatureMask()
+                .addAncestors(j1)
+                .removeAncestors(j3);
+
+        ArmatureMask mask0 = ArmatureMask.createMask(arm, "j0");
+        maskArray[3] = ArmatureMask.createMask(arm, "j2")
+                .remove(mask0);
+
+        for (ArmatureMask testMask : maskArray) {
+            for (Joint testJoint : jointList) {
+                if (testJoint == j1 || testJoint == j2) {
+                    Assert.assertTrue(testMask.contains(testJoint));
+                } else {
+                    Assert.assertFalse(testMask.contains(testJoint));
+                }
+            }
+        }
+    }
+}