]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/shape/Sphere.java
Mesh API to use Tuple3d instead of Vector3d
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / shape / Sphere.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 Sphere {
23
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++) {
33                         if (ip == 0) {
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);
40                                         indices.add(is+off);
41                                         if (is < s -1)
42                                                 indices.add(is+off+1);
43                                         else
44                                                 indices.add(off);
45                                         
46                                         
47                                 }
48                         } else {
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);
55                                         vertices.add(vs);
56                                 }
57                                 if (ip == 1) {
58                                         for (int is = 0; is < s; is++) {
59                                                 indices.add(0);
60                                                 if (is < s -1)
61                                                         indices.add(is+2);
62                                                 else
63                                                         indices.add(1);
64                                                 indices.add(is+1);
65                                         }
66                                 } else {
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);
72                                                 
73                                                 
74                                                 indices.add(off + is - s);
75                                                 indices.add(off + is + 1 - s);
76                                                 indices.add(off + is + 1);
77                                                 
78                                         }
79                                         indices.add(off - 1);
80                                         indices.add(off);
81                                         indices.add(off + s - 1);
82                                         
83                                         indices.add(off -1);
84                                         indices.add(off - s);
85                                         indices.add(off);
86                                         
87                                 }
88                         }
89                 }
90                 for (int i = 0; i < vertices.size(); i++) {
91                         Vector3d n = new Vector3d(vertices.get(i));
92                         n.normalize();
93                         normals.add(n);
94                 }
95                 
96                 return Mesh.create(vertices,normals,indices);
97                 
98         }
99         
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);
104         }
105 }