]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/shape/Cone.java
Mesh API to use Tuple3d instead of Vector3d
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / shape / Cone.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.shape;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import javax.vecmath.AxisAngle4d;
18 import javax.vecmath.Vector3d;
19
20 import org.simantics.g3d.math.MathTools;
21
22 public class Cone {
23
24         public static Mesh create(double radius, int s) {
25                 if (s < 3 || radius < MathTools.NEAR_ZERO)
26                         throw new IllegalArgumentException();
27                 List<Vector3d> vertices = new ArrayList<Vector3d>(s+2);
28                 List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size());
29                 List<Integer> indices = new ArrayList<Integer>();
30                 
31                 vertices.add(new Vector3d(0.0,0.0,0.0));
32                 normals.add(new Vector3d(0.0,-1.0,0.0));
33                 vertices.add(new Vector3d(0.0,radius*2.0,0.0));
34                 normals.add(new Vector3d(0.0,1.0,0.0));
35                 
36                 Vector3d v = new Vector3d(radius,0,0);
37                 for (int i = 0; i < s; i++) {
38                         AxisAngle4d aa = new AxisAngle4d(0,1,0,((double)i/(double)s)*Math.PI * 2);
39                         Vector3d t = new Vector3d();
40                         MathTools.rotate(MathTools.getQuat(aa), v, t);
41                         vertices.add(t);
42                         Vector3d n = new Vector3d(t);
43                         n.normalize();
44                         normals.add(n);
45                 }
46                 
47                 for (int i = 0; i < s; i++) {
48                         indices.add(0);
49                         
50                         if (i < s - 1)
51                                 indices.add(i + 3);
52                         else
53                                 indices.add(2);
54                         indices.add(i + 2);
55                 }
56                 
57                 for (int i = 0; i < s; i++) {
58                         indices.add(1);
59                         indices.add(i + 2);
60                         if (i < s - 1)
61                                 indices.add(i + 3);
62                         else
63                                 indices.add(2);
64                         
65                 }
66                 return Mesh.create(vertices,normals,indices);
67                 
68         }
69         
70         public static void main(String arg[]) {
71                 Mesh s1 = create(1.0, 3);
72                 Mesh s2 = create(1.0, 4);
73                 System.out.println("debug " + s1 + "\n" + s2);
74         }
75 }