diff --git a/jme3-core/src/main/java/com/jme3/math/ColorRGBA.java b/jme3-core/src/main/java/com/jme3/math/ColorRGBA.java index 89df1bb2b9..c276225c7b 100644 --- a/jme3-core/src/main/java/com/jme3/math/ColorRGBA.java +++ b/jme3-core/src/main/java/com/jme3/math/ColorRGBA.java @@ -632,4 +632,22 @@ public ColorRGBA getAsSrgb() { return srgb; } + /** + * Gets the colors that are stored in the given data array. + * @param data an array of doubles. The amount of entries should be divisable by 4. + * @return an array of ColorRGBA objects + */ + public static ColorRGBA[] toColorRGBA(double[] data) { + assert data.length % 4 == 0; + ColorRGBA[] colors = new ColorRGBA[data.length / 4]; + for (int i = 0; i < colors.length; i++) { + float r = (float) data[i * 4]; + float g = (float) data[i * 4 + 1]; + float b = (float) data[i * 4 + 2]; + float a = (float) data[i * 4 + 3]; + colors[i] = new ColorRGBA(r, g, b, a); + } + return colors; + } + } diff --git a/jme3-core/src/main/java/com/jme3/math/Vector2f.java b/jme3-core/src/main/java/com/jme3/math/Vector2f.java index c1c2bcba49..1055b37c73 100644 --- a/jme3-core/src/main/java/com/jme3/math/Vector2f.java +++ b/jme3-core/src/main/java/com/jme3/math/Vector2f.java @@ -753,4 +753,15 @@ public void rotateAroundOrigin(float angle, boolean cw) { x = newX; y = newY; } + + public static Vector2f[] toVector2(double[] data) { + assert data.length % 2 == 0; + Vector2f[] vectors = new Vector2f[data.length / 2]; + for (int i = 0; i < vectors.length; i++) { + float x = (float) data[i * 2]; + float y = (float) data[i * 2 + 1]; + vectors[i] = new Vector2f(x, y); + } + return vectors; + } } diff --git a/jme3-core/src/main/java/com/jme3/math/Vector3f.java b/jme3-core/src/main/java/com/jme3/math/Vector3f.java index b5d2d8fc6e..4e51e90a9e 100644 --- a/jme3-core/src/main/java/com/jme3/math/Vector3f.java +++ b/jme3-core/src/main/java/com/jme3/math/Vector3f.java @@ -1079,4 +1079,16 @@ public void set(int index, float value) { throw new IllegalArgumentException("index must be either 0, 1 or 2"); } + + public static Vector3f[] toVector3(double[] data) { + assert data.length % 3 == 0; + Vector3f[] vectors = new Vector3f[data.length / 3]; + for (int i = 0; i < vectors.length; i++) { + float x = (float) data[i * 3]; + float y = (float) data[i * 3 + 1]; + float z = (float) data[i * 3 + 2]; + vectors[i] = new Vector3f(x, y, z); + } + return vectors; + } } diff --git a/jme3-core/src/test/java/com/jme3/math/ColorRGBATest.java b/jme3-core/src/test/java/com/jme3/math/ColorRGBATest.java new file mode 100644 index 0000000000..2f1ef34d1b --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/math/ColorRGBATest.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2009-2016 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.math; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class ColorRGBATest { + + @Test + public void testEmptyArray() { + double[] data = new double[0]; + ColorRGBA[] colorArr = ColorRGBA.toColorRGBA(data); + assertEquals(0, colorArr.length); + } + + @Test(expected=AssertionError.class) + public void testArrayWithWrongLengthElements() { + double[] data = {1.0, 3.0, 1.5}; + ColorRGBA[] colorArr = ColorRGBA.toColorRGBA(data); + } + + @Test + public void testCorrectWorkingOneColor() { + double[] data = {0.1, 0.2, 0.3, 0.4}; + ColorRGBA[] colorArr = ColorRGBA.toColorRGBA(data); + assertEquals(1, colorArr.length); + ColorRGBA res = colorArr[0]; + double delta = 0.000001; + assertEquals(data[0], res.r, delta); + assertEquals(data[1], res.g, delta); + assertEquals(data[2], res.b, delta); + assertEquals(data[3], res.a, delta); + } + + @Test + public void testCorrectWorkingMultipleColors() { + double[] data = { + 0.1, 0.2, 0.3, 0.4, + 0.5, 0.6, 0.7, 0.8, + 0.9, 1.0, 1.1, 1.2, + }; + ColorRGBA[] colorArr = ColorRGBA.toColorRGBA(data); + assertEquals(3,colorArr.length); + + ColorRGBA expected1 = new ColorRGBA(0.1f, 0.2f, 0.3f, 0.4f); + ColorRGBA expected2 = new ColorRGBA(0.5f, 0.6f, 0.7f, 0.8f); + ColorRGBA expected3 = new ColorRGBA(0.9f, 1.0f, 1.1f, 1.2f); + + assertTrue(expected1.equals(colorArr[0])); + assertTrue(expected2.equals(colorArr[1])); + assertTrue(expected3.equals(colorArr[2])); + } +} diff --git a/jme3-core/src/test/java/com/jme3/math/Vector2fTest.java b/jme3-core/src/test/java/com/jme3/math/Vector2fTest.java new file mode 100644 index 0000000000..0a582ba90e --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/math/Vector2fTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2009-2016 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.math; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class Vector2fTest { + @Test + public void testEmptyArray() { + double[] data = new double[0]; + Vector2f[] vectorArr = Vector2f.toVector2(data); + assertEquals(0, vectorArr.length); + } + + @Test(expected=AssertionError.class) + public void testArrayWithWrongLength() { + double[] data = {1.0, 2.1, 3.4}; + Vector2f.toVector2(data); + } + + @Test + public void testArrayWithOneVector() { + double[] data = {1.0, 2.0,}; + Vector2f[] vectorArr = Vector2f.toVector2(data); + assertEquals(1, vectorArr.length); + Vector2f expected = new Vector2f(1.0f, 2.0f); + assertTrue(expected.equals(vectorArr[0])); + } + + @Test + public void testArrayWithMultipleVectors() { + double[] data = { + 1.0, 1.1, + 2.0, 2.1, + 3.0, 3.1, + }; + Vector2f[] vectorArr = Vector2f.toVector2(data); + assertEquals(3, vectorArr.length); + Vector2f expected1 = new Vector2f(1.0f, 1.1f); + Vector2f expected2 = new Vector2f(2.0f, 2.1f); + Vector2f expected3 = new Vector2f(3.0f, 3.1f); + + assertTrue(expected1.equals(vectorArr[0])); + assertTrue(expected2.equals(vectorArr[1])); + assertTrue(expected3.equals(vectorArr[2])); + } +} diff --git a/jme3-core/src/test/java/com/jme3/math/Vector3fTest.java b/jme3-core/src/test/java/com/jme3/math/Vector3fTest.java new file mode 100644 index 0000000000..85d42f9086 --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/math/Vector3fTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2009-2016 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.math; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class Vector3fTest { + + @Test + public void testEmptyArray() { + double[] data = new double[0]; + Vector3f[] vectorArr = Vector3f.toVector3(data); + assertEquals(0, vectorArr.length); + } + + @Test(expected=AssertionError.class) + public void testArrayWithWrongLength() { + double[] data = {1.0, 5.3}; + Vector3f.toVector3(data); + } + + @Test + public void testArrayWithOneVector() { + double[] data = {1.0, 5.3, 4.2}; + Vector3f[] vectorArr = Vector3f.toVector3(data); + assertEquals(1, vectorArr.length); + Vector3f expected = new Vector3f(1.0f, 5.3f, 4.2f); + assertTrue(expected.equals(vectorArr[0])); + } + + @Test + public void testArrayWithMultipleVectors() { + double[] data = { + 1.0, 1.1, 1.2, + 2.0, 2.1, 2.2, + 3.0, 3.1, 3.2, + }; + Vector3f[] vectorArr = Vector3f.toVector3(data); + assertEquals(3, vectorArr.length); + Vector3f expected1 = new Vector3f(1.0f, 1.1f, 1.2f); + Vector3f expected2 = new Vector3f(2.0f, 2.1f, 2.2f); + Vector3f expected3 = new Vector3f(3.0f, 3.1f, 3.2f); + + assertTrue(expected1.equals(vectorArr[0])); + assertTrue(expected2.equals(vectorArr[1])); + assertTrue(expected3.equals(vectorArr[2])); + } +} diff --git a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/mesh/FbxLayerElement.java b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/mesh/FbxLayerElement.java index bffd94c658..e24c8ee3dc 100644 --- a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/mesh/FbxLayerElement.java +++ b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/mesh/FbxLayerElement.java @@ -152,7 +152,7 @@ public static FbxLayerElement fromPositions(double[] positionData) { layerElement.type = Type.Position; layerElement.mapInfoType = MappingInformationType.ByVertex; layerElement.refInfoType = ReferenceInformationType.Direct; - layerElement.data = toVector3(positionData); + layerElement.data = Vector3f.toVector3(positionData); layerElement.dataIndices = null; return layerElement; } @@ -184,11 +184,11 @@ public static FbxLayerElement fromElement(FbxElement element) { } layerElement.refInfoType = ReferenceInformationType.valueOf(refInfoTypeVal); } else if (child.id.equals("Normals") || child.id.equals("Tangents") || child.id.equals("Binormals")) { - layerElement.data = toVector3(FbxMeshUtil.getDoubleArray(child)); + layerElement.data = Vector3f.toVector3(FbxMeshUtil.getDoubleArray(child)); } else if (child.id.equals("Colors")) { - layerElement.data = toColorRGBA(FbxMeshUtil.getDoubleArray(child)); + layerElement.data = ColorRGBA.toColorRGBA(FbxMeshUtil.getDoubleArray(child)); } else if (child.id.equals("UV")) { - layerElement.data = toVector2(FbxMeshUtil.getDoubleArray(child)); + layerElement.data = Vector2f.toVector2(FbxMeshUtil.getDoubleArray(child)); } else if (indexTypes.contains(child.id)) { layerElement.dataIndices = FbxMeshUtil.getIntArray(child); } else if (child.id.equals("Name")) { @@ -206,38 +206,4 @@ public static FbxLayerElement fromElement(FbxElement element) { } return layerElement; } - - static Vector3f[] toVector3(double[] data) { - Vector3f[] vectors = new Vector3f[data.length / 3]; - for (int i = 0; i < vectors.length; i++) { - float x = (float) data[i * 3]; - float y = (float) data[i * 3 + 1]; - float z = (float) data[i * 3 + 2]; - vectors[i] = new Vector3f(x, y, z); - } - return vectors; - } - - static Vector2f[] toVector2(double[] data) { - Vector2f[] vectors = new Vector2f[data.length / 2]; - for (int i = 0; i < vectors.length; i++) { - float x = (float) data[i * 2]; - float y = (float) data[i * 2 + 1]; - vectors[i] = new Vector2f(x, y); - } - return vectors; - } - - static ColorRGBA[] toColorRGBA(double[] data) { - ColorRGBA[] colors = new ColorRGBA[data.length / 4]; - for (int i = 0; i < colors.length; i++) { - float r = (float) data[i * 4]; - float g = (float) data[i * 4 + 1]; - float b = (float) data[i * 4 + 2]; - float a = (float) data[i * 4 + 3]; - colors[i] = new ColorRGBA(r, g, b, a); - } - return colors; - } -} - +} \ No newline at end of file diff --git a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/mesh/FbxMesh.java b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/mesh/FbxMesh.java index 5dd911bed6..30e1599d36 100644 --- a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/mesh/FbxMesh.java +++ b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/mesh/FbxMesh.java @@ -162,7 +162,7 @@ public void connectObjectProperty(FbxObject object, String property) { } private void setPositions(double[] positions) { - this.positions = FbxLayerElement.toVector3(positions); + this.positions = Vector3f.toVector3(positions); } private void setEdges(int[] edges) {