1 package org.simantics.g3d.shape;
3 import java.util.ArrayList;
6 import javax.vecmath.AxisAngle4d;
7 import javax.vecmath.Point3d;
8 import javax.vecmath.Quat4d;
9 import javax.vecmath.Tuple3d;
10 import javax.vecmath.Vector3d;
12 import org.simantics.g3d.math.MathTools;
14 public class ArcCylinder {
16 public Mesh create(Point3d s, Point3d v, Point3d e, double r, int res) {
17 return create(s, v, e, r, res, 0.15);
20 public Mesh create(Point3d s, Point3d v, Point3d e, double r, int res, double res2) {
22 Vector3d v1 = new Vector3d(s);
25 Vector3d v2 = new Vector3d(e);
28 double a = v2.angle(v1);
32 Vector3d rn = new Vector3d();
36 if ((a +0.0001) > Math.PI) {
42 c.scale(v1.length() * (1.0/Math.cos(a*0.5)));
48 Vector3d r2 = new Vector3d(e);
56 steps = (int)(Math.ceil(a/res2));
63 List<Tuple3d> vertices = new ArrayList<Tuple3d>(res * (steps+1));
64 List<Tuple3d> normals = new ArrayList<Tuple3d>(res * (steps+1));
65 List<Integer> indices = new ArrayList<Integer>();
67 for (int i = 0; i <= steps; i++) {
75 } else if (i == steps) {
82 Quat4d q = MathTools.getQuat(new AxisAngle4d(rn, ca));
83 MathTools.rotate(q, r1, p);
90 createCircle(vertices, normals, p, t, rn, res, r);
92 int count = steps*res*6;
93 for (int i = 0; i < count; i++) {
96 createIndices(steps, res, indices);
97 return new Mesh(vertices, normals, indices);
100 public Mesh create(Point3d c, Vector3d axis, Vector3d an, double R, double r, double a, int res, double res2) {
107 Vector3d rn = new Vector3d(axis);
109 Vector3d r1 = new Vector3d(an);
114 if ((a +0.0001) > Math.PI) {
117 steps = (int)(Math.ceil(a/res2));
123 List<Tuple3d> vertices = new ArrayList<Tuple3d>(res * (steps+1));
124 List<Tuple3d> normals = new ArrayList<Tuple3d>(res * (steps+1));
125 List<Integer> indices = new ArrayList<Integer>();
127 for (int i = 0; i <= steps; i++) {
132 Quat4d q = MathTools.getQuat(new AxisAngle4d(rn, ca));
133 MathTools.rotate(q, r1, p);
140 createCircle(vertices, normals, p, t, rn, res, r);
142 int count = steps*res*6;
143 for (int i = 0; i < count; i++) {
146 createIndices(steps, res, indices);
147 return new Mesh(vertices, normals, indices);
150 public Mesh create(Point3d c, Vector3d axis, Vector3d an, double R, double r, double starta, double a, int res, double res2) {
157 Vector3d rn = new Vector3d(axis);
159 Vector3d r1 = new Vector3d(an);
164 if ((a +0.0001) > Math.PI) {
167 steps = (int)(Math.ceil(a/res2));
173 List<Tuple3d> vertices = new ArrayList<Tuple3d>(res * (steps+1));
174 List<Tuple3d> normals = new ArrayList<Tuple3d>(res * (steps+1));
175 List<Integer> indices = new ArrayList<Integer>();
177 for (int i = 0; i <= steps; i++) {
181 double ca = sa * i + starta;
182 Quat4d q = MathTools.getQuat(new AxisAngle4d(rn, ca));
183 MathTools.rotate(q, r1, p);
190 createCircle(vertices, normals, p, t, rn, res, r);
192 int count = steps*res*6;
193 for (int i = 0; i < count; i++) {
196 createIndices(steps, res, indices);
197 return new Mesh(vertices, normals, indices);
200 private static void createCircle(List<Tuple3d> points, List<Tuple3d> normals, Tuple3d p, Vector3d t, Vector3d n, int res, double radius) {
204 for (int index = 0; index < res; index ++) {
210 AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)res);
212 MathTools.rotate(MathTools.getQuat(aa), n, v);
214 //int vIndex = (i*resolution + index)*3;
215 Vector3d pt = new Vector3d(p);
217 //points.set(vIndex, pt);
220 //normals.set(vIndex, v);
224 private static void createIndices(int steps, int resolution, List<Integer> index) {
225 for (int c = 0; c < steps; c++) {
226 for (int s = 0; s < resolution; s++) {
227 int ii = (c * resolution + s) * 6;
228 int iv = c*resolution + s;
231 iv+1 ---- iv + resolution + 1
234 iv ---- iv + resolution
236 if (s < resolution - 1) {
238 index.set(ii+1,iv+resolution);
239 index.set(ii+0,iv+resolution+1);
242 index.set(ii+4,iv+resolution+1);
243 index.set(ii+3,iv+1);
246 index.set(ii+1,iv+resolution);
247 index.set(ii+0,iv+1);
250 index.set(ii+4,iv+1);
251 index.set(ii+3,iv+1-resolution);