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;
33 public void setResolution(int resolution) {
35 this.resolution = resolution;
38 public void setVertices(List<Tuple3d> vertices) {
39 this.vertices = vertices;
42 public void setTangents(List<Vector3d> tangents) {
43 this.tangents = tangents;
46 public void setColors(List<Color4d> colors) {
50 public void setRadiis(List<Double> radiis) {
54 public void setRadius(Double radius) {
60 public Mesh create() {
61 if (vertices.size() < 2 )
62 throw new IllegalArgumentException("Tube must have at least two vertices");
64 Vector3d t = new Vector3d();
66 for (int i = 0; i < vertices.size() - 1; i++) {
67 t.set(vertices.get(i+1));
68 t.sub(vertices.get(i));
69 if (t.lengthSquared() < 0.0001)
70 throw new IllegalArgumentException("vertices at index " + i + " are too close to each other");
73 List<Vector3d> points = new ArrayList<Vector3d>(vertices.size()*resolution);
74 List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size()*resolution);
76 for (int i = 0; i < vertices.size(); i++) {
77 createCircle(i,points,normals);
80 int index[] = new int[(vertices.size()-1)*resolution*6];
83 List<Integer> indices = new ArrayList<Integer>();
84 for (int i = 0; i < index.length; i++) {
85 indices.add(index[i]);
96 return Mesh.create(points, normals, indices);
100 private void createCircle(int i, List<Vector3d> points, List<Vector3d> normals) {
101 final Vector3d up = new Vector3d(0,1,0);
102 final Vector3d up2 = new Vector3d(0,0,1);
103 Tuple3d p = vertices.get(i);
104 Vector3d t = getTangent(i);
105 Vector3d n = new Vector3d();
106 if (Math.abs(up.dot(t)) < 0.99) {
112 if (radiis != null) {
113 n.scale(radiis.get(i));
118 for (int index = 0; index < resolution; index ++) {
124 AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)resolution);
126 MathTools.rotate(MathTools.getQuat(aa), n, v);
128 //int vIndex = (i*resolution + index)*3;
129 Vector3d pt = new Vector3d(p);
131 //points.set(vIndex, pt);
134 //normals.set(vIndex, v);
139 private Vector3d getTangent(int i) {
141 if (tangents != null)
142 return tangents.get(i);
146 } else if (i == vertices.size() - 1) {
147 p = vertices.get(i-1);
150 p = vertices.get(i-1);
151 n = vertices.get(i+1);
153 Vector3d nn = new Vector3d(n);
159 private void createIndices(int index[]) {
160 for (int c = 0; c < vertices.size() - 1; c++) {
161 for (int s = 0; s < resolution; s++) {
162 int ii = (c * resolution + s) * 6;
163 int iv = c*resolution + s;
166 iv+1 ---- iv + resolution + 1
169 iv ---- iv + resolution
171 if (s < resolution - 1) {
173 index[ii+1] = iv+resolution;
174 index[ii+0] = iv+resolution+1;
177 index[ii+4] = iv+resolution+1;
181 index[ii+1] = iv+resolution;
186 index[ii+3] = iv+1-resolution;