/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.g3d.shape; import java.util.ArrayList; import java.util.List; import javax.vecmath.AxisAngle4d; import javax.vecmath.Vector3d; import org.simantics.g3d.math.MathTools; public class Tube { List vertices; List colors; List radiis; Double radius = 1.0; int resolution = 8; public void setResolution(int resolution) { if (resolution > 2) this.resolution = resolution; } public void setVertices(List vertices) { this.vertices = vertices; } public void setColors(List colors) { this.colors = colors; } public void setRadiis(List radiis) { this.radiis = radiis; } public void setRadius(Double radius) { this.radius = radius; } public Mesh create() { if (vertices.size() < 2 ) throw new IllegalArgumentException("Tube must have at least two vertices"); Vector3d t = new Vector3d(); for (int i = 0; i < vertices.size() - 1; i++) { t.set(vertices.get(i+1)); t.sub(vertices.get(i)); if (t.lengthSquared() < 0.0001) throw new IllegalArgumentException("vertices at index " + i + " are too close to each other"); } List points = new ArrayList(vertices.size()*resolution); List normals = new ArrayList(vertices.size()*resolution); for (int i = 0; i < vertices.size(); i++) { createCircle(i,points,normals); } int index[] = new int[(vertices.size()-1)*resolution*6]; createIndices(index); List indices = new ArrayList(); for (int i = 0; i < index.length; i++) { indices.add(index[i]); } vertices.clear(); if (colors != null) colors.clear(); if (radiis != null) radiis.clear(); return new Mesh(points, normals, indices); } private void createCircle(int i, List points, List normals) { final Vector3d up = new Vector3d(0,1,0); final Vector3d up2 = new Vector3d(0,0,1); Vector3d p = vertices.get(i); Vector3d t = getTangent(i); Vector3d n = new Vector3d(); if (up.dot(t) < 0.99) { n.cross(up, t); } else { n.cross(up2, t); } n.normalize(); if (radiis != null) { n.scale(radiis.get(i)); } else { n.scale(radius); } for (int index = 0; index < resolution; index ++) { Vector3d v; if (index == 0) { v = new Vector3d(n); } else { AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)resolution); v = new Vector3d(); MathTools.rotate(MathTools.getQuat(aa), n, v); } //int vIndex = (i*resolution + index)*3; Vector3d pt = new Vector3d(p); pt.add(v); //points.set(vIndex, pt); points.add(pt); v.normalize(); //normals.set(vIndex, v); normals.add(v); } } private Vector3d getTangent(int i) { Vector3d p,n; if (i == 0) { p = vertices.get(0); n = vertices.get(1); } else if (i == vertices.size() - 1) { p = vertices.get(i-1); n = vertices.get(i); } else { p = vertices.get(i-1); n = vertices.get(i+1); } n = new Vector3d(n); n.sub(p); n.normalize(); return n; } private void createIndices(int index[]) { for (int c = 0; c < vertices.size() - 1; c++) { for (int s = 0; s < resolution; s++) { int ii = (c * resolution + s) * 6; int iv = c*resolution + s; /* iv+1 ---- iv + resolution + 1 | /| |/ | iv ---- iv + resolution */ if (s < resolution - 1) { index[ii+2] = iv; index[ii+1] = iv+resolution; index[ii+0] = iv+resolution+1; index[ii+5] = iv; index[ii+4] = iv+resolution+1; index[ii+3] = iv+1; } else { index[ii+2] = iv; index[ii+1] = iv+resolution; index[ii+0] = iv+1; index[ii+5] = iv; index[ii+4] = iv+1; index[ii+3] = iv+1-resolution; } } } } }