Search in sources :

Example 31 with Face

use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.

the class Face method findClosestVertex.

/**
	 * The method finds the closest vertex to the one specified by <b>index</b>.
	 * If the vertexToIgnore is positive than it will be ignored in the result.
	 * The closest vertex must be able to create an edge that is fully contained
	 * within the face and does not cross any other edges. Also if the
	 * vertexToIgnore is not negative then the condition that the edge between
	 * the found index and the one to ignore is inside the face must also be
	 * met.
	 * 
	 * @param index
	 *            the index of the vertex that needs to have found the nearest
	 *            neighbour
	 * @param indexToIgnore
	 *            the index to ignore in the result (pass -1 if none is to be
	 *            ignored)
	 * @return the index of the closest vertex to the given one
	 */
private int findClosestVertex(int index, int indexToIgnore) {
    int result = -1;
    List<Vector3f> vertices = temporalMesh.getVertices();
    Vector3f v1 = vertices.get(index);
    float distance = Float.MAX_VALUE;
    for (int i : indexes) {
        if (i != index && i != indexToIgnore) {
            Vector3f v2 = vertices.get(i);
            float d = v2.distance(v1);
            if (d < distance && this.contains(new Edge(index, i, 0, true, temporalMesh)) && (indexToIgnore < 0 || this.contains(new Edge(indexToIgnore, i, 0, true, temporalMesh)))) {
                result = i;
                distance = d;
            }
        }
    }
    return result;
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 32 with Face

use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.

the class Face method computeCentroid.

/**
     * @return the centroid of the face
     */
public Vector3f computeCentroid() {
    Vector3f result = new Vector3f();
    List<Vector3f> vertices = temporalMesh.getVertices();
    for (Integer index : indexes) {
        result.addLocal(vertices.get(index));
    }
    return result.divideLocal(indexes.size());
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 33 with Face

use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.

the class IndexesLoop method findPath.

/**
     * The method finds the path between the given indexes.
     * @param start
     *            the start index
     * @param end
     *            the end index
     * @param result
     *            a list containing indexes on the path from start to end (inclusive)
     * @throws IllegalStateException
     *             an exception is thrown when the loop is not normalized (at least one
     *             index has more than 2 neighbours)
     * @throws BlenderFileException
     *             an exception is thrown if the vertices of a face create more than one loop; this is thrown
     *             to prevent lack of memory errors during triangulation
     */
public void findPath(Integer start, Integer end, List<Integer> result) throws BlenderFileException {
    result.clear();
    Integer node = start;
    while (!node.equals(end)) {
        if (result.contains(node)) {
            throw new BlenderFileException("Indexes of face have infinite loops!");
        }
        result.add(node);
        List<Integer> nextSteps = edges.get(node);
        if (nextSteps == null || nextSteps.size() == 0) {
            // no directed path from start to end
            result.clear();
            return;
        } else if (nextSteps.size() == 1) {
            node = nextSteps.get(0);
        } else {
            throw new BlenderFileException("Triangulation failed. Face has ambiguous indexes loop. Please triangulate your model in Blender as a workaround.");
        }
    }
    result.add(end);
}
Also used : BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException)

Example 34 with Face

use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.

the class SubdivisionSurfaceModifier method computeFaceUVs.

/**
     * The method computes the UV coordinates of the face middle point.
     * @param face
     *            the face of the mesh
     * @return a map whose key is the name of the UV set and value is the UV coordinate of the face's middle point
     */
private Map<String, Vector2f> computeFaceUVs(Face face) {
    Map<String, Vector2f> result = null;
    Map<String, List<Vector2f>> uvSets = face.getUvSets();
    if (uvSets != null && uvSets.size() > 0) {
        result = new HashMap<String, Vector2f>(uvSets.size());
        for (Entry<String, List<Vector2f>> entry : uvSets.entrySet()) {
            Vector2f faceUV = new Vector2f();
            for (Vector2f uv : entry.getValue()) {
                faceUV.addLocal(uv);
            }
            faceUV.divideLocal(entry.getValue().size());
            result.put(entry.getKey(), faceUV);
        }
    }
    return result;
}
Also used : Vector2f(com.jme3.math.Vector2f) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with Face

use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.

the class Edge method loadAll.

/**
     * The method loads all edges from the given mesh structure that does not belong to any face.
     * @param meshStructure
     *            the mesh structure
     * @param temporalMesh
     *            the owner of the edges
     * @return all edges without faces
     * @throws BlenderFileException
     *             an exception is thrown when problems with file reading occur
     */
public static List<Edge> loadAll(Structure meshStructure, TemporalMesh temporalMesh) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Loading all edges that do not belong to any face from mesh: {0}", meshStructure.getName());
    List<Edge> result = new ArrayList<Edge>();
    Pointer pMEdge = (Pointer) meshStructure.getFieldValue("medge");
    if (pMEdge.isNotNull()) {
        List<Structure> edges = pMEdge.fetchData();
        for (Structure edge : edges) {
            int flag = ((Number) edge.getFieldValue("flag")).intValue();
            int v1 = ((Number) edge.getFieldValue("v1")).intValue();
            int v2 = ((Number) edge.getFieldValue("v2")).intValue();
            // I do not know why, but blender stores (possibly only sometimes) crease as negative values and shows positive in the editor
            float crease = Math.abs(((Number) edge.getFieldValue("crease")).floatValue());
            boolean edgeInFace = (flag & Edge.FLAG_EDGE_NOT_IN_FACE) == 0;
            result.add(new Edge(v1, v2, crease, edgeInFace, temporalMesh));
        }
    }
    LOGGER.log(Level.FINE, "Loaded {0} edges.", result.size());
    return result;
}
Also used : ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

Aggregations

Vector3f (com.jme3.math.Vector3f)24 ArrayList (java.util.ArrayList)13 ColorRGBA (com.jme3.math.ColorRGBA)8 Face (com.jme3.scene.plugins.blender.meshes.Face)8 List (java.util.List)8 FloatBuffer (java.nio.FloatBuffer)7 Vector2f (com.jme3.math.Vector2f)6 Structure (com.jme3.scene.plugins.blender.file.Structure)6 VertexBuffer (com.jme3.scene.VertexBuffer)5 Pointer (com.jme3.scene.plugins.blender.file.Pointer)5 Edge (com.jme3.scene.plugins.blender.meshes.Edge)5 TextureCubeMap (com.jme3.texture.TextureCubeMap)5 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)4 ByteBuffer (java.nio.ByteBuffer)4 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)3 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)3 Image (com.jme3.texture.Image)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 CubeMapWrapper (com.jme3.environment.util.CubeMapWrapper)2