1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.shape;
\r
14 import java.util.ArrayList;
\r
15 import java.util.List;
\r
17 import javax.vecmath.Quat4d;
\r
18 import javax.vecmath.Vector3d;
\r
20 import org.simantics.g3d.math.MathTools;
\r
23 private List<Vector3d> vertices;
\r
24 private List<Vector3d> normals;
\r
25 private List<Color4d> colors;
\r
26 private List<Integer> indices;
\r
29 public Mesh(List<Vector3d> vertices, List<Vector3d> normals,
\r
30 List<Color4d> colors, List<Integer> indices) {
\r
31 this.vertices = vertices;
\r
32 this.normals = normals;
\r
33 this.colors = colors;
\r
34 this.indices = indices;
\r
37 public Mesh(List<Vector3d> vertices, List<Vector3d> normals, List<Integer> indices) {
\r
38 this.vertices = vertices;
\r
39 this.normals = normals;
\r
40 this.indices = indices;
\r
43 public Mesh(List<Vector3d> vertices, List<Integer> indices) {
\r
44 this.vertices = vertices;
\r
45 this.indices = indices;
\r
48 public List<Vector3d> getVertices() {
\r
52 public List<Vector3d> getNormals() {
\r
56 public List<Integer> getIndices() {
\r
60 public List<Color4d> getColors() {
\r
64 public void createNormals() {
\r
65 normals = new ArrayList<Vector3d>(vertices.size());
\r
66 for (int i = 0; i < vertices.size(); i++) {
\r
67 normals.add(new Vector3d());
\r
69 Vector3d v1 = new Vector3d();
\r
70 Vector3d v2 = new Vector3d();
\r
71 Vector3d v3 = new Vector3d();
\r
72 Vector3d t1 = new Vector3d();
\r
73 Vector3d t2 = new Vector3d();
\r
74 Vector3d n = new Vector3d();
\r
75 for (int i = 0; i < indices.size(); i+=3) {
\r
76 v1.set(vertices.get(i));
\r
77 v2.set(vertices.get(i+1));
\r
78 v3.set(vertices.get(i+2));
\r
82 normals.get(i).add(n);
\r
83 normals.get(i+1).add(n);
\r
84 normals.get(i+2).add(n);
\r
86 for (int i = 0; i < normals.size(); i++) {
\r
87 normals.get(i).normalize();
\r
91 public void translate(Vector3d v) {
\r
92 for (int i = 0; i < vertices.size(); i++) {
\r
93 vertices.get(i).add(v);
\r
97 public void rotate(Quat4d q) {
\r
98 Vector3d t = new Vector3d();
\r
99 for (int i = 0; i < vertices.size(); i++) {
\r
100 MathTools.rotate(q, vertices.get(i), t);
\r
101 vertices.get(i).set(t);
\r
104 if (normals != null) {
\r
105 for (int i = 0; i < normals.size(); i++) {
\r
106 MathTools.rotate(q, normals.get(i), t);
\r
108 normals.get(i).set(t);
\r
113 public void setColor(Color4d color) {
\r
114 colors = new ArrayList<Color4d>(vertices.size());
\r
115 for (int i = 0; i < vertices.size(); i++) {
\r
120 public void add(Mesh mesh) {
\r
121 int vindex = vertices.size();
\r
122 int triIndex = indices.size();
\r
123 vertices.addAll(mesh.getVertices());
\r
124 indices.addAll(mesh.indices);
\r
125 for (int i = triIndex; i < indices.size(); i++) {
\r
126 indices.set(i, indices.get(i)+vindex);
\r
128 if (normals != null) {
\r
129 boolean hasNormals = true;
\r
130 if (mesh.getNormals() == null) {
\r
131 mesh.createNormals();
\r
132 hasNormals = false;
\r
134 normals.addAll(mesh.getNormals());
\r
136 mesh.normals = null;
\r
138 if (colors != null) {
\r
139 if (mesh.getColors() != null) {
\r
140 colors.addAll(mesh.getColors());
\r
142 for (int i = 0; i < mesh.getVertices().size(); i++) {
\r
143 colors.add(new Color4d(1,1,1,0));
\r