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.AxisAngle4d;
18 import javax.vecmath.Tuple3d;
19 import javax.vecmath.Vector3d;
21 import org.simantics.g3d.math.MathTools;
25 List<Tuple3d> vertices;
26 List<Vector3d> tangents;
35 public void setResolution(int resolution) {
37 this.resolution = resolution;
40 public void setVertices(List<Tuple3d> vertices) {
41 this.vertices = vertices;
44 public void setTangents(List<Vector3d> tangents) {
45 this.tangents = tangents;
48 public void setColors(List<Color4d> colors) {
52 public void setRadiis(List<Double> radiis) {
56 public void setRadius(Double radius) {
61 public void setCap(boolean cap) {
66 public Mesh create() {
67 if (vertices.size() < 2 )
68 throw new IllegalArgumentException("Tube must have at least two vertices");
70 Vector3d t = new Vector3d();
72 for (int i = 0; i < vertices.size() - 1; i++) {
73 t.set(vertices.get(i+1));
74 t.sub(vertices.get(i));
75 if (t.lengthSquared() < 0.000001)
76 throw new IllegalArgumentException("vertices at index " + i + " are too close to each other");
79 int vcount = vertices.size()*resolution;
80 int icount = (vertices.size()-1)*resolution*6;
84 icount+=resolution * 6;
87 List<Vector3d> points = new ArrayList<Vector3d>(vcount);
88 List<Vector3d> normals = new ArrayList<Vector3d>(vcount);
90 for (int i = 0; i < vertices.size(); i++) {
91 createCircle(i,points,normals);
94 int l = vertices.size()-1;
95 points.add(new Vector3d(vertices.get(0)));
96 points.add(new Vector3d(vertices.get(l)));
97 Vector3d n1 = new Vector3d(vertices.get(1));
98 n1.sub(vertices.get(0));
102 Vector3d n2 = new Vector3d(vertices.get(l-1));
103 n2.sub(vertices.get(l));
111 int index[] = new int[icount];
113 createIndices(index);
114 List<Integer> indices = new ArrayList<Integer>();
115 for (int i = 0; i < index.length; i++) {
116 indices.add(index[i]);
127 return Mesh.create(points, normals, indices);
131 private void createCircle(int i, List<Vector3d> points, List<Vector3d> normals) {
132 final Vector3d up = new Vector3d(0,1,0);
133 final Vector3d up2 = new Vector3d(0,0,1);
134 Tuple3d p = vertices.get(i);
135 Vector3d t = getTangent(i);
136 Vector3d n = new Vector3d();
137 if (Math.abs(up.dot(t)) < 0.99) {
143 if (radiis != null) {
144 n.scale(radiis.get(i));
149 for (int index = 0; index < resolution; index ++) {
155 AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)resolution);
157 MathTools.rotate(MathTools.getQuat(aa), n, v);
159 //int vIndex = (i*resolution + index)*3;
160 Vector3d pt = new Vector3d(p);
162 //points.set(vIndex, pt);
165 //normals.set(vIndex, v);
170 private Vector3d getTangent(int i) {
172 if (tangents != null)
173 return tangents.get(i);
177 } else if (i == vertices.size() - 1) {
178 p = vertices.get(i-1);
181 p = vertices.get(i-1);
182 n = vertices.get(i+1);
184 Vector3d nn = new Vector3d(n);
190 private void createIndices(int index[]) {
191 for (int c = 0; c < vertices.size() - 1; c++) {
192 for (int s = 0; s < resolution; s++) {
193 int ii = (c * resolution + s) * 6;
194 int iv = c*resolution + s;
197 iv+1 ---- iv + resolution + 1
200 iv ---- iv + resolution
202 if (s < resolution - 1) {
204 index[ii+1] = iv+resolution;
205 index[ii+0] = iv+resolution+1;
208 index[ii+4] = iv+resolution+1;
212 index[ii+1] = iv+resolution;
217 index[ii+3] = iv+1-resolution;
223 int isi = ((vertices.size()-1) * resolution) * 6;
224 int ivi = vertices.size() * resolution;
225 for (int s = 0; s < resolution; s++) {
227 int iv = c*resolution + s;
228 if (s < resolution - 1) {
234 index[ii+1] = iv+1-resolution;
239 isi += resolution * 3;
240 c = (vertices.size()-1);
241 for (int s = 0; s < resolution; s++) {
243 int iv = c*resolution + s;
244 if (s < resolution - 1) {
250 index[ii+1] = iv+1-resolution;