BowlerKernel
Plane.java
Go to the documentation of this file.
1 
34 package eu.mihosoft.vrl.v3d;
35 
36 // # class Plane
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 // TODO: Auto-generated Javadoc
46 public class Plane {
47  private static IPolygonDebugger debugger = null;
48  private static boolean useDebugger = false;
55  public static final double EPSILON = 1.0e-9;
56  public static final double EPSILON_Point = EPSILON;
57  public static final double EPSILON_duplicate = 1.0e-4;
61  public static final Plane XY_PLANE = new Plane(Vector3d.Z_ONE, 1);
65  public static final Plane XZ_PLANE = new Plane(Vector3d.Y_ONE, 1);
69  public static final Plane YZ_PLANE = new Plane(Vector3d.X_ONE, 1);
70 
74  public Vector3d normal;
78  public double dist;
79 
87  public Plane(Vector3d normal, double dist) {
88  this.normal = normal.normalized();
89  this.dist = dist;
90  }
91 
101  Vector3d n = b.minus(a).cross(c.minus(a)).normalized();
102  return new Plane(n, n.dot(a));
103  }
104 
105  /* (non-Javadoc)
106  * @see java.lang.Object#clone()
107  */
108  @Override
109  public Plane clone() {
110  return new Plane(normal.clone(), dist);
111  }
112 
116  public void flip() {
117  normal = normal.negated();
118  dist = -dist;
119  }
120 
135  public void splitPolygon(
136  Polygon polygon,
137  List<Polygon> coplanarFront,
138  List<Polygon> coplanarBack,
139  List<Polygon> front,
140  List<Polygon> back) {
141  final int COPLANAR = 0;
142  final int FRONT = 1;
143  final int BACK = 2;
144  final int SPANNING = 3; // == some in the FRONT + some in the BACK
145  if(debugger!=null && useDebugger) {
146 // debugger.display(polygon);
147 // debugger.display(coplanarFront);
148 // debugger.display(coplanarBack);
149 // debugger.display(front);
150 // debugger.display(back);
151  }
152  // search for the epsilon values of the incoming plane
153  double negEpsilon = -Plane.EPSILON;
154  double posEpsilon = Plane.EPSILON;
155  for (int i = 0; i < polygon.vertices.size(); i++) {
156  double t = polygon.plane.normal.dot(polygon.vertices.get(i).pos) - polygon.plane.dist;
157  if(t>posEpsilon) {
158  //System.err.println("Non flat polygon, increasing positive epsilon "+t);
159  posEpsilon=t+Plane.EPSILON;
160  }
161  if(t<negEpsilon) {
162  //System.err.println("Non flat polygon, decreasing negative epsilon "+t);
163  negEpsilon=t-Plane.EPSILON;
164  }
165  }
166  int polygonType = 0;
167  List<Integer> types = new ArrayList<>();
168  boolean somePointsInfront = false;
169  boolean somePointsInBack = false;
170  for (int i = 0; i < polygon.vertices.size(); i++) {
171  double t = this.normal.dot(polygon.vertices.get(i).pos) - this.dist;
172  int type = (t < negEpsilon) ? BACK : (t > posEpsilon) ? FRONT : COPLANAR;
173  //polygonType |= type;
174  if(type==BACK)
175  somePointsInBack=true;
176  if(type==FRONT)
177  somePointsInfront = true;
178  types.add(type);
179  }
180  if(somePointsInBack && somePointsInfront)
181  polygonType=SPANNING;
182  else if(somePointsInBack) {
183  polygonType=BACK;
184  }else if(somePointsInfront)
185  polygonType=FRONT;
186 
187  //System.out.println("> switching");
188  // Put the polygon in the correct list, splitting it when necessary.
189  switch (polygonType) {
190  case COPLANAR:
191  //System.out.println(" -> coplanar");
192  (this.normal.dot(polygon.plane.normal) > 0 ? coplanarFront : coplanarBack).add(polygon);
193  break;
194  case FRONT:
195  //System.out.println(" -> front");
196  front.add(polygon);
197  break;
198  case BACK:
199  //System.out.println(" -> back");
200  back.add(polygon);
201  break;
202  case SPANNING:
203  //System.out.println(" -> spanning");
204  List<Vertex> f = new ArrayList<>();
205  List<Vertex> b = new ArrayList<>();
206  for (int i = 0; i < polygon.vertices.size(); i++) {
207  int j = (i + 1) % polygon.vertices.size();
208  int ti = types.get(i);
209  int tj = types.get(j);
210  Vertex vi = polygon.vertices.get(i);
211  Vertex vj = polygon.vertices.get(j);
212  if (ti != BACK) {
213  f.add(vi);
214  }
215  if (ti != FRONT) {
216  b.add(ti != BACK ? vi.clone() : vi);
217  }
218  if ((ti | tj) == SPANNING) {
219  double t = (this.dist - this.normal.dot(vi.pos))
220  / this.normal.dot(vj.pos.minus(vi.pos));
221  Vertex v = vi.interpolate(vj, t);
222  f.add(v);
223  b.add(v.clone());
224  }
225  }
226  if (f.size() >= 3) {
227  front.add(new Polygon(f, polygon.getStorage()));
228  }else {
229  System.out.println("Front Clip Fault!");
230  }
231  if (b.size() >= 3) {
232  back.add(new Polygon(b, polygon.getStorage()));
233  }else {
234  System.out.println("Back Clip Fault!");
235  }
236  break;
237  }
238  }
239 
240  public static IPolygonDebugger getDebugger() {
241  return debugger;
242  }
243 
244  public static void setDebugger(IPolygonDebugger debugger) {
246  }
247 
248  public static boolean isUseDebugger() {
249  return useDebugger;
250  }
251 
252  public static void setUseDebugger(boolean useDebugger) {
254  }
255 }
void splitPolygon(Polygon polygon, List< Polygon > coplanarFront, List< Polygon > coplanarBack, List< Polygon > front, List< Polygon > back)
Definition: Plane.java:135
static boolean isUseDebugger()
Definition: Plane.java:248
static final double EPSILON
Definition: Plane.java:55
static IPolygonDebugger getDebugger()
Definition: Plane.java:240
static final Plane XZ_PLANE
Definition: Plane.java:65
static final Plane XY_PLANE
Definition: Plane.java:61
static void setDebugger(IPolygonDebugger debugger)
Definition: Plane.java:244
static void setUseDebugger(boolean useDebugger)
Definition: Plane.java:252
static Plane createFromPoints(Vector3d a, Vector3d b, Vector3d c)
Definition: Plane.java:100
static final double EPSILON_Point
Definition: Plane.java:56
static final double EPSILON_duplicate
Definition: Plane.java:57
static final Plane YZ_PLANE
Definition: Plane.java:69
static boolean useDebugger
Definition: Plane.java:48
Plane(Vector3d normal, double dist)
Definition: Plane.java:87
static IPolygonDebugger debugger
Definition: Plane.java:47
final List< Vertex > vertices
Definition: Polygon.java:54
PropertyStorage getStorage()
Definition: Polygon.java:534
Vector3d cross(Vector3d a)
Definition: Vector3d.java:291
static final Vector3d X_ONE
Definition: Vector3d.java:63
double dot(Vector3d a)
Definition: Vector3d.java:230
Vector3d minus(Vector3d v)
Definition: Vector3d.java:178
static final Vector3d Z_ONE
Definition: Vector3d.java:69
static final Vector3d Y_ONE
Definition: Vector3d.java:66
Vertex interpolate(Vertex other, double t)
Definition: Vertex.java:107