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.AxisAngle4d;
\r
18 import javax.vecmath.Vector3d;
\r
20 import org.simantics.g3d.math.MathTools;
\r
24 List<Vector3d> vertices;
\r
25 List<Color4d> colors;
\r
26 List<Double> radiis;
\r
27 Double radius = 1.0;
\r
31 public void setResolution(int resolution) {
\r
33 this.resolution = resolution;
\r
36 public void setVertices(List<Vector3d> vertices) {
\r
37 this.vertices = vertices;
\r
40 public void setColors(List<Color4d> colors) {
\r
41 this.colors = colors;
\r
44 public void setRadiis(List<Double> radiis) {
\r
45 this.radiis = radiis;
\r
48 public void setRadius(Double radius) {
\r
49 this.radius = radius;
\r
54 public Mesh create() {
\r
55 if (vertices.size() < 2 )
\r
56 throw new IllegalArgumentException("Tube must have at least two vertices");
\r
58 Vector3d t = new Vector3d();
\r
60 for (int i = 0; i < vertices.size() - 1; i++) {
\r
61 t.set(vertices.get(i+1));
\r
62 t.sub(vertices.get(i));
\r
63 if (t.lengthSquared() < 0.0001)
\r
64 throw new IllegalArgumentException("vertices at index " + i + " are too close to each other");
\r
67 List<Vector3d> points = new ArrayList<Vector3d>(vertices.size()*resolution);
\r
68 List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size()*resolution);
\r
70 for (int i = 0; i < vertices.size(); i++) {
\r
71 createCircle(i,points,normals);
\r
74 int index[] = new int[(vertices.size()-1)*resolution*6];
\r
76 createIndices(index);
\r
77 List<Integer> indices = new ArrayList<Integer>();
\r
78 for (int i = 0; i < index.length; i++) {
\r
79 indices.add(index[i]);
\r
90 return new Mesh(points, normals, indices);
\r
94 private void createCircle(int i, List<Vector3d> points, List<Vector3d> normals) {
\r
95 final Vector3d up = new Vector3d(0,1,0);
\r
96 final Vector3d up2 = new Vector3d(0,0,1);
\r
97 Vector3d p = vertices.get(i);
\r
98 Vector3d t = getTangent(i);
\r
99 Vector3d n = new Vector3d();
\r
100 if (up.dot(t) < 0.99) {
\r
106 if (radiis != null) {
\r
107 n.scale(radiis.get(i));
\r
112 for (int index = 0; index < resolution; index ++) {
\r
115 v = new Vector3d(n);
\r
118 AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)resolution);
\r
119 v = new Vector3d();
\r
120 MathTools.rotate(MathTools.getQuat(aa), n, v);
\r
122 //int vIndex = (i*resolution + index)*3;
\r
123 Vector3d pt = new Vector3d(p);
\r
125 //points.set(vIndex, pt);
\r
128 //normals.set(vIndex, v);
\r
133 private Vector3d getTangent(int i) {
\r
136 p = vertices.get(0);
\r
137 n = vertices.get(1);
\r
138 } else if (i == vertices.size() - 1) {
\r
139 p = vertices.get(i-1);
\r
140 n = vertices.get(i);
\r
142 p = vertices.get(i-1);
\r
143 n = vertices.get(i+1);
\r
145 n = new Vector3d(n);
\r
151 private void createIndices(int index[]) {
\r
152 for (int c = 0; c < vertices.size() - 1; c++) {
\r
153 for (int s = 0; s < resolution; s++) {
\r
154 int ii = (c * resolution + s) * 6;
\r
155 int iv = c*resolution + s;
\r
158 iv+1 ---- iv + resolution + 1
\r
161 iv ---- iv + resolution
\r
163 if (s < resolution - 1) {
\r
165 index[ii+1] = iv+resolution;
\r
166 index[ii+0] = iv+resolution+1;
\r
169 index[ii+4] = iv+resolution+1;
\r
170 index[ii+3] = iv+1;
\r
173 index[ii+1] = iv+resolution;
\r
174 index[ii+0] = iv+1;
\r
177 index[ii+4] = iv+1;
\r
178 index[ii+3] = iv+1-resolution;
\r