X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fshape%2FMesh.java;h=22355f2c421cda793229e99097a539f9654d8868;hb=53d55c24c779745f188bdb18d32f71d20acb61b2;hp=83aa36072dcca1110a63cc253e2f28a75c033c30;hpb=87b3241ec277ba3d8e414b26186a032c9cdcaeed;p=simantics%2F3d.git diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java b/org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java index 83aa3607..22355f2c 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java +++ b/org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java @@ -1,137 +1,164 @@ -package org.simantics.g3d.shape; - -import java.util.ArrayList; -import java.util.List; - -import javax.vecmath.Quat4d; -import javax.vecmath.Vector3d; - -import org.simantics.g3d.math.MathTools; - -public class Mesh { - private List vertices; - private List normals; - private List colors; - private List indices; - - - public Mesh(List vertices, List normals, - List colors, List indices) { - this.vertices = vertices; - this.normals = normals; - this.colors = colors; - this.indices = indices; - } - - public Mesh(List vertices, List normals, List indices) { - this.vertices = vertices; - this.normals = normals; - this.indices = indices; - } - - public Mesh(List vertices, List indices) { - this.vertices = vertices; - this.indices = indices; - } - - public List getVertices() { - return vertices; - } - - public List getNormals() { - return normals; - } - - public List getIndices() { - return indices; - } - - public List getColors() { - return colors; - } - - public void createNormals() { - normals = new ArrayList(vertices.size()); - for (int i = 0; i < vertices.size(); i++) { - normals.add(new Vector3d()); - } - Vector3d v1 = new Vector3d(); - Vector3d v2 = new Vector3d(); - Vector3d v3 = new Vector3d(); - Vector3d t1 = new Vector3d(); - Vector3d t2 = new Vector3d(); - Vector3d n = new Vector3d(); - for (int i = 0; i < indices.size(); i+=3) { - v1.set(vertices.get(i)); - v2.set(vertices.get(i+1)); - v3.set(vertices.get(i+2)); - t1.sub(v3,v1); - t2.sub(v2,v1); - n.cross(t2, t1); - normals.get(i).add(n); - normals.get(i+1).add(n); - normals.get(i+2).add(n); - } - for (int i = 0; i < normals.size(); i++) { - normals.get(i).normalize(); - } - } - - public void translate(Vector3d v) { - for (int i = 0; i < vertices.size(); i++) { - vertices.get(i).add(v); - } - } - - public void rotate(Quat4d q) { - Vector3d t = new Vector3d(); - for (int i = 0; i < vertices.size(); i++) { - MathTools.rotate(q, vertices.get(i), t); - vertices.get(i).set(t); - } - - if (normals != null) { - for (int i = 0; i < normals.size(); i++) { - MathTools.rotate(q, normals.get(i), t); - t.normalize(); - normals.get(i).set(t); - } - } - } - - public void setColor(Color4d color) { - colors = new ArrayList(vertices.size()); - for (int i = 0; i < vertices.size(); i++) { - colors.add(color); - } - } - - public void add(Mesh mesh) { - int vindex = vertices.size(); - int triIndex = indices.size(); - vertices.addAll(mesh.getVertices()); - indices.addAll(mesh.indices); - for (int i = triIndex; i < indices.size(); i++) { - indices.set(i, indices.get(i)+vindex); - } - if (normals != null) { - boolean hasNormals = true; - if (mesh.getNormals() == null) { - mesh.createNormals(); - hasNormals = false; - } - normals.addAll(mesh.getNormals()); - if (!hasNormals) - mesh.normals = null; - } - if (colors != null) { - if (mesh.getColors() != null) { - colors.addAll(mesh.getColors()); - } else { - for (int i = 0; i < mesh.getVertices().size(); i++) { - colors.add(new Color4d(1,1,1,0)); - } - } - } - } -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.shape; + +import java.util.ArrayList; +import java.util.List; + +import javax.vecmath.Quat4d; +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.math.MathTools; + +public class Mesh { + private List vertices; + private List normals; + private List colors; + private List indices; + + + public Mesh(List vertices, List normals, + List colors, List indices) { + this.vertices = vertices; + this.normals = normals; + this.colors = colors; + this.indices = indices; + } + + public Mesh(List vertices, List normals, List indices) { + this.vertices = vertices; + this.normals = normals; + this.indices = indices; + } + + public static Mesh create(List vertices, List normals, List indices) { + + List v = new ArrayList(); + List n = new ArrayList(); + v.addAll(vertices); + n.addAll(normals); + return new Mesh(v, n, indices); + } + + public Mesh(List vertices, List indices) { + this.vertices = vertices; + this.indices = indices; + } + + public static Mesh create(List vertices, List indices) { + List v = new ArrayList(); + v.addAll(vertices); + return new Mesh(v, indices); + } + + public List getVertices() { + return vertices; + } + + public List getNormals() { + return normals; + } + + public List getIndices() { + return indices; + } + + public List getColors() { + return colors; + } + + public void createNormals() { + normals = new ArrayList(vertices.size()); + for (int i = 0; i < vertices.size(); i++) { + normals.add(new Vector3d()); + } + Vector3d v1 = new Vector3d(); + Vector3d v2 = new Vector3d(); + Vector3d v3 = new Vector3d(); + Vector3d t1 = new Vector3d(); + Vector3d t2 = new Vector3d(); + Vector3d n = new Vector3d(); + for (int i = 0; i < indices.size(); i+=3) { + v1.set(vertices.get(i)); + v2.set(vertices.get(i+1)); + v3.set(vertices.get(i+2)); + t1.sub(v3,v1); + t2.sub(v2,v1); + n.cross(t2, t1); + normals.get(i).add(n); + normals.get(i+1).add(n); + normals.get(i+2).add(n); + } + for (int i = 0; i < normals.size(); i++) { + ((Vector3d)normals.get(i)).normalize(); + } + } + + public void translate(Vector3d v) { + for (int i = 0; i < vertices.size(); i++) { + vertices.get(i).add(v); + } + } + + public void rotate(Quat4d q) { + Vector3d t = new Vector3d(); + for (int i = 0; i < vertices.size(); i++) { + MathTools.rotate(q, vertices.get(i), t); + vertices.get(i).set(t); + } + + if (normals != null) { + for (int i = 0; i < normals.size(); i++) { + MathTools.rotate(q, normals.get(i), t); + t.normalize(); + normals.get(i).set(t); + } + } + } + + public void setColor(Color4d color) { + colors = new ArrayList(vertices.size()); + for (int i = 0; i < vertices.size(); i++) { + colors.add(color); + } + } + + public void add(Mesh mesh) { + int vindex = vertices.size(); + int triIndex = indices.size(); + vertices.addAll(mesh.getVertices()); + indices.addAll(mesh.indices); + for (int i = triIndex; i < indices.size(); i++) { + indices.set(i, indices.get(i)+vindex); + } + if (normals != null) { + boolean hasNormals = true; + if (mesh.getNormals() == null) { + mesh.createNormals(); + hasNormals = false; + } + normals.addAll(mesh.getNormals()); + if (!hasNormals) + mesh.normals = null; + } + if (colors != null) { + if (mesh.getColors() != null) { + colors.addAll(mesh.getColors()); + } else { + for (int i = 0; i < mesh.getVertices().size(); i++) { + colors.add(new Color4d(1,1,1,0)); + } + } + } + } +}