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.Vector3d;
20 import org.simantics.g3d.math.MathTools;
24 public static Mesh create(double radius, int s, int p) {
25 if (s < 3 || p < 3 || radius < MathTools.NEAR_ZERO)
26 throw new IllegalArgumentException();
27 List<Vector3d> vertices = new ArrayList<Vector3d>((s-2)*p + 2);
28 List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size());
29 List<Integer> indices = new ArrayList<Integer>(((s-3)*p*2 + p * 2)*3);
30 Vector3d v = new Vector3d(0.0,-radius,0.0);
31 Vector3d vp = new Vector3d();
32 for (int ip = 0; ip < p; ip++) {
34 vertices.add(new Vector3d(0.0,-radius,0.0));
35 } else if (ip == p - 1) {
36 vertices.add(new Vector3d(0.0, radius,0.0));
37 int off = 1 + (ip-2)*s;
38 for (int is = 0; is < s; is++) {
39 indices.add(vertices.size() - 1);
42 indices.add(is+off+1);
49 AxisAngle4d aa = new AxisAngle4d(1, 0, 0, ((double)ip/(double)(p-1))*Math.PI);
50 MathTools.rotate(MathTools.getQuat(aa), v, vp);
51 for (int is = 0; is < s; is++) {
52 aa = new AxisAngle4d(0, 1, 0, ((double)is/(double)s)*Math.PI*2);
53 Vector3d vs = new Vector3d();
54 MathTools.rotate(MathTools.getQuat(aa), vp, vs);
58 for (int is = 0; is < s; is++) {
67 int off = 1 + (ip-1)*s;
68 for (int is = 0; is < s-1; is++) {
69 indices.add(off + is - s);
70 indices.add(off + is+1);
71 indices.add(off + is);
74 indices.add(off + is - s);
75 indices.add(off + is + 1 - s);
76 indices.add(off + is + 1);
81 indices.add(off + s - 1);
90 for (int i = 0; i < vertices.size(); i++) {
91 Vector3d n = new Vector3d(vertices.get(i));
96 return Mesh.create(vertices,normals,indices);
100 public static void main(String arg[]) {
101 Mesh s1 = create(1.0, 3, 3);
102 Mesh s2 = create(1.0, 4, 4);
103 System.out.println("debug " + s1 + " " + s2);