JavaCAD
Toroid.java
Go to the documentation of this file.
1 /*
2  * Cylinder.java
3  *
4  * Copyright 2014-2014 Michael Hoffer info@michaelhoffer.de. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY Michael Hoffer info@michaelhoffer.de "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL Michael Hoffer info@michaelhoffer.de OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation are
30  * those of the authors and should not be interpreted as representing official
31  * policies, either expressed or implied, of Michael Hoffer
32  * info@michaelhoffer.de.
33  */
34 package eu.mihosoft.vrl.v3d;
35 
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 
48 public class Toroid extends Primitive {
49 
52  List<Polygon> polys;
53  public Toroid(double innerRadius, double OuterRadius) {
54  this(innerRadius,OuterRadius,20,16);
55  }
56  public Toroid(double innerRadius, double OuterRadius, int numSlices, int facets) {
57  if (innerRadius < 0)
58  throw new RuntimeException("Inner radious must be positive");
59  if (innerRadius >= OuterRadius)
60  throw new RuntimeException("Outer radius must be larger than inner radius");
61  double crossSecRad = OuterRadius - innerRadius;
62  ArrayList<Vertex> vertices = new ArrayList<>();
63  double f = facets;
64 
65  for (int i = 0; i < facets; i++) {
66  double index = i;
67  double rad = index / f * 2 * Math.PI;
68  double a = Math.cos(rad) * crossSecRad;
69  double b = Math.sin(rad) * crossSecRad;
70  vertices.add(new Vertex(new Vector3d(a,b), new Vector3d(-1, 0,0)));
71  }
72  Polygon poly = new Polygon(vertices, properties);
73  ArrayList<Polygon> slices = new ArrayList<Polygon>();
74 
75  for (int i = 0; i < numSlices; i++) {
76  double angle = 360.0 / ((double) numSlices) * ((double) i);
77  slices.add(poly.transformed(new Transform().movex(innerRadius+crossSecRad).roty(angle)));
78  }
79  List<Polygon> newPolygons = new ArrayList<>();
80  for (int j = 0; j < slices.size(); j++) {
81  int next = j + 1;
82  if (next == slices.size())
83  next = 0;
84  // println "Extruding "+i+" to "+next
85  Polygon polygon1=slices.get(j);
86  Polygon polygon2=slices.get(next);
87  if (polygon1.vertices.size() != polygon2.vertices.size()) {
88  throw new RuntimeException("These polygons do not match");
89  }
90 
91  int numvertices = polygon1.vertices.size();
92  for (int i = 0; i < numvertices; i++) {
93 
94  int nexti = (i + 1) % numvertices;
95 
96  Vector3d bottomV1 = polygon1.vertices.get(i).pos;
97  Vector3d topV1 = polygon2.vertices.get(i).pos;
98  Vector3d bottomV2 = polygon1.vertices.get(nexti).pos;
99  Vector3d topV2 = polygon2.vertices.get(nexti).pos;
100 
101  List<Vector3d> pPoints = Arrays.asList(bottomV2, topV2, topV1, bottomV1);
102 
103  newPolygons.add(Polygon.fromPoints(pPoints, polygon1.getStorage()));
104 
105  }
106 
107  polygon2 = polygon2.flipped();
108 
109  }
110  polys = newPolygons;
111  }
112 
113  /*
114  * (non-Javadoc)
115  *
116  * @see eu.mihosoft.vrl.v3d.Primitive#toPolygons()
117  */
118  @Override
119  public List<Polygon> toPolygons() {
120  return polys;
121  }
122 
123  /*
124  * (non-Javadoc)
125  *
126  * @see eu.mihosoft.vrl.v3d.Primitive#getProperties()
127  */
128  @Override
130  return properties;
131  }
132 
133 }
final List< Vertex > vertices
Definition: Polygon.java:54
static Polygon fromPoints(List< Vector3d > points, PropertyStorage shared)
Definition: Polygon.java:386
PropertyStorage getStorage()
Definition: Polygon.java:534
Polygon transformed(Transform transform)
Definition: Polygon.java:375
List< Polygon > toPolygons()
Definition: Toroid.java:119
Toroid(double innerRadius, double OuterRadius)
Definition: Toroid.java:53
Toroid(double innerRadius, double OuterRadius, int numSlices, int facets)
Definition: Toroid.java:56
final PropertyStorage properties
Definition: Toroid.java:51
PropertyStorage getProperties()
Definition: Toroid.java:129
Transform roty(Number degreesToRotate)
Definition: Transform.java:705
Transform movex(Number howFarToMove)
Definition: Transform.java:653