1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.shape;
14 import java.util.ArrayList;
15 import java.util.List;
17 import javax.vecmath.Quat4d;
18 import javax.vecmath.Tuple3d;
19 import javax.vecmath.Vector3d;
21 import org.simantics.g3d.math.MathTools;
24 private List<Tuple3d> vertices;
25 private List<Tuple3d> normals;
26 private List<Color4d> colors;
27 private List<Integer> indices;
30 public Mesh(List<Tuple3d> vertices, List<Tuple3d> normals,
31 List<Color4d> colors, List<Integer> indices) {
32 this.vertices = vertices;
33 this.normals = normals;
35 this.indices = indices;
38 public Mesh(List<Tuple3d> vertices, List<Tuple3d> normals, List<Integer> indices) {
39 this.vertices = vertices;
40 this.normals = normals;
41 this.indices = indices;
44 public static Mesh create(List<Vector3d> vertices, List<Vector3d> normals, List<Integer> indices) {
46 List<Tuple3d> v = new ArrayList<Tuple3d>();
47 List<Tuple3d> n = new ArrayList<Tuple3d>();
50 return new Mesh(v, n, indices);
53 public Mesh(List<Tuple3d> vertices, List<Integer> indices) {
54 this.vertices = vertices;
55 this.indices = indices;
58 public static Mesh create(List<Vector3d> vertices, List<Integer> indices) {
59 List<Tuple3d> v = new ArrayList<Tuple3d>();
61 return new Mesh(v, indices);
64 public List<Tuple3d> getVertices() {
68 public List<Tuple3d> getNormals() {
72 public List<Integer> getIndices() {
76 public List<Color4d> getColors() {
80 public void createNormals() {
81 normals = new ArrayList<Tuple3d>(vertices.size());
82 for (int i = 0; i < vertices.size(); i++) {
83 normals.add(new Vector3d());
85 Vector3d v1 = new Vector3d();
86 Vector3d v2 = new Vector3d();
87 Vector3d v3 = new Vector3d();
88 Vector3d t1 = new Vector3d();
89 Vector3d t2 = new Vector3d();
90 Vector3d n = new Vector3d();
91 for (int i = 0; i < indices.size(); i+=3) {
92 v1.set(vertices.get(i));
93 v2.set(vertices.get(i+1));
94 v3.set(vertices.get(i+2));
98 normals.get(i).add(n);
99 normals.get(i+1).add(n);
100 normals.get(i+2).add(n);
102 for (int i = 0; i < normals.size(); i++) {
103 ((Vector3d)normals.get(i)).normalize();
107 public void translate(Vector3d v) {
108 for (int i = 0; i < vertices.size(); i++) {
109 vertices.get(i).add(v);
113 public void rotate(Quat4d q) {
114 Vector3d t = new Vector3d();
115 for (int i = 0; i < vertices.size(); i++) {
116 MathTools.rotate(q, vertices.get(i), t);
117 vertices.get(i).set(t);
120 if (normals != null) {
121 for (int i = 0; i < normals.size(); i++) {
122 MathTools.rotate(q, normals.get(i), t);
124 normals.get(i).set(t);
129 public void setColor(Color4d color) {
130 colors = new ArrayList<Color4d>(vertices.size());
131 for (int i = 0; i < vertices.size(); i++) {
136 public void add(Mesh mesh) {
137 int vindex = vertices.size();
138 int triIndex = indices.size();
139 vertices.addAll(mesh.getVertices());
140 indices.addAll(mesh.indices);
141 for (int i = triIndex; i < indices.size(); i++) {
142 indices.set(i, indices.get(i)+vindex);
144 if (normals != null) {
145 boolean hasNormals = true;
146 if (mesh.getNormals() == null) {
147 mesh.createNormals();
150 normals.addAll(mesh.getNormals());
154 if (colors != null) {
155 if (mesh.getColors() != null) {
156 colors.addAll(mesh.getColors());
158 for (int i = 0; i < mesh.getVertices().size(); i++) {
159 colors.add(new Color4d(1,1,1,0));