-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathCylinder.java
More file actions
973 lines (845 loc) · 38.6 KB
/
Cylinder.java
File metadata and controls
973 lines (845 loc) · 38.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/*
* Copyright (C) 2012 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package gov.nasa.worldwind.geom;
import gov.nasa.worldwind.View;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwind.util.*;
import com.jogamp.opengl.*;
import com.jogamp.opengl.glu.*;
import java.util.*;
/**
* Represents a geometric cylinder, most often used as a bounding volume. <code>Cylinder</code>s are immutable.
*
* @author Tom Gaskins
* @version $Id: Cylinder.java 1171 2013-02-11 21:45:02Z dcollins $
*/
public class Cylinder implements Extent, Renderable
{
protected final Vec4 bottomCenter; // point at center of cylinder base
protected final Vec4 topCenter; // point at center of cylinder top
protected final Vec4 axisUnitDirection; // axis as unit vector from bottomCenter to topCenter
protected final double cylinderRadius;
protected final double cylinderHeight;
/**
* Create a Cylinder from two points and a radius.
*
* @param bottomCenter the center point of of the cylinder's base.
* @param topCenter the center point of the cylinders top.
* @param cylinderRadius the cylinder's radius.
*
* @throws IllegalArgumentException if the radius is zero or the top or bottom point is null or they are
* coincident.
*/
public Cylinder(Vec4 bottomCenter, Vec4 topCenter, double cylinderRadius)
{
if (bottomCenter == null || topCenter == null || bottomCenter.equals(topCenter))
{
String message = Logging.getMessage(
bottomCenter == null || topCenter == null ? "nullValue.EndPointIsNull" : "generic.EndPointsCoincident");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (cylinderRadius <= 0)
{
String message = Logging.getMessage("Geom.Cylinder.RadiusIsZeroOrNegative", cylinderRadius);
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// Convert the bottom center and top center points to points in four-dimensional homogeneous coordinates to
// ensure that their w-coordinates are 1. Cylinder's intersection tests compute a dot product between these
// points and each frustum plane, which depends on a w-coordinate of 1. We convert each point at construction to
// avoid the additional overhead of converting them during every intersection test.
this.bottomCenter = bottomCenter.toHomogeneousPoint3();
this.topCenter = topCenter.toHomogeneousPoint3();
this.cylinderHeight = this.bottomCenter.distanceTo3(this.topCenter);
this.cylinderRadius = cylinderRadius;
this.axisUnitDirection = this.topCenter.subtract3(this.bottomCenter).normalize3();
}
/**
* Create a Cylinder from two points, a radius and an axis direction. Provided for use when unit axis is know and
* computation of it can be avoided.
*
* @param bottomCenter the center point of of the cylinder's base.
* @param topCenter the center point of the cylinders top.
* @param cylinderRadius the cylinder's radius.
* @param unitDirection the unit-length axis of the cylinder.
*
* @throws IllegalArgumentException if the radius is zero or the top or bottom point is null or they are
* coincident.
*/
public Cylinder(Vec4 bottomCenter, Vec4 topCenter, double cylinderRadius, Vec4 unitDirection)
{
if (bottomCenter == null || topCenter == null || bottomCenter.equals(topCenter))
{
String message = Logging.getMessage(
bottomCenter == null || topCenter == null ? "nullValue.EndPointIsNull" : "generic.EndPointsCoincident");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (cylinderRadius <= 0)
{
String message = Logging.getMessage("Geom.Cylinder.RadiusIsZeroOrNegative", cylinderRadius);
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// Convert the bottom center and top center points to points in four-dimensional homogeneous coordinates to
// ensure that their w-coordinates are 1. Cylinder's intersection tests compute a dot product between these
// points and each frustum plane, which depends on a w-coordinate of 1. We convert each point at construction to
// avoid the additional overhead of converting them during every intersection test.
this.bottomCenter = bottomCenter.toHomogeneousPoint3();
this.topCenter = topCenter.toHomogeneousPoint3();
this.cylinderHeight = this.bottomCenter.distanceTo3(this.topCenter);
this.cylinderRadius = cylinderRadius;
this.axisUnitDirection = unitDirection;
}
/**
* Returns the unit-length axis of this cylinder.
*
* @return the unit-length axis of this cylinder.
*/
public Vec4 getAxisUnitDirection()
{
return axisUnitDirection;
}
/**
* Returns the this cylinder's bottom-center point.
*
* @return this cylinder's bottom-center point.
*/
public Vec4 getBottomCenter()
{
return bottomCenter;
}
/**
* Returns the this cylinder's top-center point.
*
* @return this cylinder's top-center point.
*/
public Vec4 getTopCenter()
{
return topCenter;
}
/**
* Returns this cylinder's radius.
*
* @return this cylinder's radius.
*/
public double getCylinderRadius()
{
return cylinderRadius;
}
/**
* Returns this cylinder's height.
*
* @return this cylinder's height.
*/
public double getCylinderHeight()
{
return cylinderHeight;
}
/**
* Return this cylinder's center point.
*
* @return this cylinder's center point.
*/
public Vec4 getCenter()
{
Vec4 b = this.bottomCenter;
Vec4 t = this.topCenter;
return new Vec4(
(b.x + t.x) / 2.0,
(b.y + t.y) / 2.0,
(b.z + t.z) / 2.0);
}
/** {@inheritDoc} */
public double getDiameter()
{
return 2 * this.getRadius();
}
/** {@inheritDoc} */
public double getRadius()
{
// return the radius of the enclosing sphere
double halfHeight = this.bottomCenter.distanceTo3(this.topCenter) / 2.0;
return Math.sqrt(halfHeight * halfHeight + this.cylinderRadius * this.cylinderRadius);
}
/**
* Return this cylinder's volume.
*
* @return this cylinder's volume.
*/
public double getVolume()
{
return Math.PI * this.cylinderRadius * this.cylinderRadius * this.cylinderHeight;
}
/**
* Compute a bounding cylinder for a collection of points.
*
* @param points the points to compute a bounding cylinder for.
*
* @return a cylinder bounding all the points. The axis of the cylinder is the longest principal axis of the
* collection. (See {@link WWMath#computePrincipalAxes(Iterable)}.
*
* @throws IllegalArgumentException if the point list is null or empty.
* @see #computeVerticalBoundingCylinder(gov.nasa.worldwind.globes.Globe, double, Sector)
*/
public static Cylinder computeBoundingCylinder(Iterable<? extends Vec4> points)
{
if (points == null)
{
String message = Logging.getMessage("nullValue.PointListIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
Vec4[] axes = WWMath.computePrincipalAxes(points);
if (axes == null)
{
String message = Logging.getMessage("generic.ListIsEmpty");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
Vec4 r = axes[0];
Vec4 s = axes[1];
List<Vec4> sPlanePoints = new ArrayList<Vec4>();
double minDotR = Double.MAX_VALUE;
double maxDotR = -minDotR;
for (Vec4 p : points)
{
double pdr = p.dot3(r);
sPlanePoints.add(p.subtract3(r.multiply3(p.dot3(r))));
if (pdr < minDotR)
minDotR = pdr;
if (pdr > maxDotR)
maxDotR = pdr;
}
Vec4 minPoint = sPlanePoints.get(0);
Vec4 maxPoint = minPoint;
double minDotS = Double.MAX_VALUE;
double maxDotS = -minDotS;
for (Vec4 p : sPlanePoints)
{
double d = p.dot3(s);
if (d < minDotS)
{
minPoint = p;
minDotS = d;
}
if (d > maxDotS)
{
maxPoint = p;
maxDotS = d;
}
}
Vec4 center = minPoint.add3(maxPoint).divide3(2);
double radius = center.distanceTo3(minPoint);
for (Vec4 h : sPlanePoints)
{
Vec4 hq = h.subtract3(center);
double d = hq.getLength3();
if (d > radius)
{
Vec4 g = center.subtract3(hq.normalize3().multiply3(radius));
center = g.add3(h).divide3(2);
radius = d;
}
}
Vec4 bottomCenter = center.add3(r.multiply3(minDotR));
Vec4 topCenter = center.add3((r.multiply3(maxDotR)));
if (radius == 0)
radius = 1;
if (bottomCenter.equals(topCenter))
topCenter = bottomCenter.add3(new Vec4(1, 0, 0));
return new Cylinder(bottomCenter, topCenter, radius);
}
/** {@inheritDoc} */
public Intersection[] intersect(Line line)
{
if (line == null)
{
String message = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
double[] tVals = new double[2];
if (!intcyl(line.getOrigin(), line.getDirection(), this.bottomCenter, this.axisUnitDirection,
this.cylinderRadius, tVals))
return null;
if (!clipcyl(line.getOrigin(), line.getDirection(), this.bottomCenter, this.topCenter,
this.axisUnitDirection, tVals))
return null;
if (!Double.isInfinite(tVals[0]) && !Double.isInfinite(tVals[1]) && tVals[0] >= 0.0 && tVals[1] >= 0.0)
return new Intersection[] {new Intersection(line.getPointAt(tVals[0]), false),
new Intersection(line.getPointAt(tVals[1]), false)};
if (!Double.isInfinite(tVals[0]) && tVals[0] >= 0.0)
return new Intersection[] {new Intersection(line.getPointAt(tVals[0]), false)};
if (!Double.isInfinite(tVals[1]) && tVals[1] >= 0.0)
return new Intersection[] {new Intersection(line.getPointAt(tVals[1]), false)};
return null;
}
/** {@inheritDoc} */
public boolean intersects(Line line)
{
if (line == null)
{
String message = Logging.getMessage("nullValue.LineIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
return intersect(line) != null;
}
// Taken from "Graphics Gems IV", Section V.2, page 356.
protected boolean intcyl(Vec4 raybase, Vec4 raycos, Vec4 base, Vec4 axis, double radius, double[] tVals)
{
boolean hit; // True if ray intersects cyl
Vec4 RC; // Ray base to cylinder base
double d; // Shortest distance between the ray and the cylinder
double t, s; // Distances along the ray
Vec4 n, D, O;
double ln;
RC = raybase.subtract3(base);
n = raycos.cross3(axis);
// Ray is parallel to the cylinder's axis.
if ((ln = n.getLength3()) == 0.0)
{
d = RC.dot3(axis);
D = RC.subtract3(axis.multiply3(d));
d = D.getLength3();
tVals[0] = Double.NEGATIVE_INFINITY;
tVals[1] = Double.POSITIVE_INFINITY;
// True if ray is in cylinder.
return d <= radius;
}
n = n.normalize3();
d = Math.abs(RC.dot3(n)); // Shortest distance.
hit = (d <= radius);
// If ray hits cylinder.
if (hit)
{
O = RC.cross3(axis);
t = -O.dot3(n) / ln;
O = n.cross3(axis);
O = O.normalize3();
s = Math.abs(Math.sqrt(radius * radius - d * d) / raycos.dot3(O));
tVals[0] = t - s; // Entering distance.
tVals[1] = t + s; // Exiting distance.
}
return hit;
}
// Taken from "Graphics Gems IV", Section V.2, page 356.
protected boolean clipcyl(Vec4 raybase, Vec4 raycos, Vec4 bot, Vec4 top, Vec4 axis, double[] tVals)
{
double dc, dwb, dwt, tb, tt;
double in, out; // Object intersection distances.
in = tVals[0];
out = tVals[1];
dc = axis.dot3(raycos);
dwb = axis.dot3(raybase) - axis.dot3(bot);
dwt = axis.dot3(raybase) - axis.dot3(top);
// Ray is parallel to the cylinder end-caps.
if (dc == 0.0)
{
if (dwb <= 0.0)
return false;
if (dwt >= 0.0)
return false;
}
else
{
// Intersect the ray with the bottom end-cap.
tb = -dwb / dc;
// Intersect the ray with the top end-cap.
tt = -dwt / dc;
// Bottom is near cap, top is far cap.
if (dc >= 0.0)
{
if (tb > out)
return false;
if (tt < in)
return false;
if (tb > in && tb < out)
in = tb;
if (tt > in && tt < out)
out = tt;
}
// Bottom is far cap, top is near cap.
else
{
if (tb < in)
return false;
if (tt > out)
return false;
if (tb > in && tb < out)
out = tb;
if (tt > in && tt < out)
in = tt;
}
}
tVals[0] = in;
tVals[1] = out;
return in < out;
}
protected double intersects(Plane plane, double effectiveRadius)
{
// Test the distance from the first cylinder end-point. Assumes that bottomCenter's w-coordinate is 1.
double dq1 = plane.dot(this.bottomCenter);
boolean bq1 = dq1 <= -effectiveRadius;
// Test the distance from the top of the cylinder. Assumes that topCenter's w-coordinate is 1.
double dq2 = plane.dot(this.topCenter);
boolean bq2 = dq2 <= -effectiveRadius;
if (bq1 && bq2) // both beyond effective radius; cylinder is on negative side of plane
return -1;
if (bq1 == bq2) // both within effective radius; can't draw any conclusions
return 0;
return 1; // Cylinder almost certainly intersects
}
protected double intersectsAt(Plane plane, double effectiveRadius, Vec4[] endpoints)
{
// Test the distance from the first end-point. Assumes that the first end-point's w-coordinate is 1.
double dq1 = plane.dot(endpoints[0]);
boolean bq1 = dq1 <= -effectiveRadius;
// Test the distance from the possibly reduced second cylinder end-point. Assumes that the second end-point's
// w-coordinate is 1.
double dq2 = plane.dot(endpoints[1]);
boolean bq2 = dq2 <= -effectiveRadius;
if (bq1 && bq2) // endpoints more distant from plane than effective radius; cylinder is on neg. side of plane
return -1;
if (bq1 == bq2) // endpoints less distant from plane than effective radius; can't draw any conclusions
return 0;
// Compute and return the endpoints of the cylinder on the positive side of the plane.
double t = (effectiveRadius + dq1) / plane.getNormal().dot3(endpoints[0].subtract3(endpoints[1]));
Vec4 newEndPoint = endpoints[0].add3(endpoints[1].subtract3(endpoints[0]).multiply3(t));
if (bq1) // Truncate the lower end of the cylinder
endpoints[0] = newEndPoint;
else // Truncate the upper end of the cylinder
endpoints[1] = newEndPoint;
return t;
}
/** {@inheritDoc} */
public double getEffectiveRadius(Plane plane)
{
if (plane == null)
return 0;
// Determine the effective radius of the cylinder axis relative to the plane.
double dot = plane.getNormal().dot3(this.axisUnitDirection);
double scale = 1d - dot * dot;
if (scale <= 0)
return 0;
else
return this.cylinderRadius * Math.sqrt(scale);
}
/** {@inheritDoc} */
public boolean intersects(Plane plane)
{
if (plane == null)
{
String message = Logging.getMessage("nullValue.PlaneIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
double effectiveRadius = this.getEffectiveRadius(plane);
return this.intersects(plane, effectiveRadius) >= 0;
}
/** {@inheritDoc} */
public boolean intersects(Frustum frustum)
{
if (frustum == null)
{
String message = Logging.getMessage("nullValue.FrustumIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
double intersectionPoint;
Vec4[] endPoints = new Vec4[] {this.bottomCenter, this.topCenter};
double effectiveRadius = this.getEffectiveRadius(frustum.getNear());
intersectionPoint = this.intersectsAt(frustum.getNear(), effectiveRadius, endPoints);
if (intersectionPoint < 0)
return false;
// Near and far have the same effective radius.
intersectionPoint = this.intersectsAt(frustum.getFar(), effectiveRadius, endPoints);
if (intersectionPoint < 0)
return false;
effectiveRadius = this.getEffectiveRadius(frustum.getLeft());
intersectionPoint = this.intersectsAt(frustum.getLeft(), effectiveRadius, endPoints);
if (intersectionPoint < 0)
return false;
effectiveRadius = this.getEffectiveRadius(frustum.getRight());
intersectionPoint = this.intersectsAt(frustum.getRight(), effectiveRadius, endPoints);
if (intersectionPoint < 0)
return false;
effectiveRadius = this.getEffectiveRadius(frustum.getTop());
intersectionPoint = this.intersectsAt(frustum.getTop(), effectiveRadius, endPoints);
if (intersectionPoint < 0)
return false;
effectiveRadius = this.getEffectiveRadius(frustum.getBottom());
intersectionPoint = this.intersectsAt(frustum.getBottom(), effectiveRadius, endPoints);
return intersectionPoint >= 0;
}
/** {@inheritDoc} */
public double getProjectedArea(View view)
{
if (view == null)
{
String message = Logging.getMessage("nullValue.ViewIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// TODO: compute a more exact projected screen area for Cylinder.
return WWMath.computeSphereProjectedArea(view, this.getCenter(), this.getRadius());
}
/**
* Returns a cylinder that minimally surrounds the specified minimum and maximum elevations in the sector at a
* specified vertical exaggeration, and is oriented such that the cylinder axis is perpendicular to the globe's
* surface.
*
* @param globe The globe associated with the sector.
* @param verticalExaggeration the vertical exaggeration to apply to the minimum and maximum elevations when
* computing the cylinder.
* @param sector the sector to return the bounding cylinder for.
*
* @return The minimal bounding cylinder in Cartesian coordinates.
*
* @throws IllegalArgumentException if <code>sector</code> is null
* @see #computeBoundingCylinder(Iterable)
*/
static public Cylinder computeVerticalBoundingCylinder(Globe globe, double verticalExaggeration, Sector sector)
{
if (globe == null)
{
String msg = Logging.getMessage("nullValue.GlobeIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
if (sector == null)
{
String msg = Logging.getMessage("nullValue.SectorIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
double[] minAndMaxElevations = globe.getMinAndMaxElevations(sector);
return computeVerticalBoundingCylinder(globe, verticalExaggeration, sector,
minAndMaxElevations[0], minAndMaxElevations[1]);
}
/**
* Returns a cylinder that minimally surrounds the specified minimum and maximum elevations in the sector at a
* specified vertical exaggeration, and is oriented such that the cylinder axis is perpendicular to the globe's
* surface.
*
* @param globe The globe associated with the sector.
* @param verticalExaggeration the vertical exaggeration to apply to the minimum and maximum elevations when
* computing the cylinder.
* @param sector the sector to return the bounding cylinder for.
* @param minElevation the minimum elevation of the bounding cylinder.
* @param maxElevation the maximum elevation of the bounding cylinder.
*
* @return The minimal bounding cylinder in Cartesian coordinates.
*
* @throws IllegalArgumentException if <code>sector</code> is null
* @see #computeBoundingCylinder(Iterable)
*/
public static Cylinder computeVerticalBoundingCylinder(Globe globe, double verticalExaggeration, Sector sector,
double minElevation, double maxElevation)
{
if (sector == null)
{
String msg = Logging.getMessage("nullValue.SectorIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
// Compute the exaggerated minimum and maximum heights.
double minHeight = minElevation * verticalExaggeration;
double maxHeight = maxElevation * verticalExaggeration;
if (minHeight == maxHeight)
maxHeight = minHeight + 1; // ensure the top and bottom of the cylinder won't be coincident
// If the sector spans both poles in latitude, or spans greater than 180 degrees in longitude, we cannot use the
// sector's Cartesian quadrilateral to compute a bounding cylinde. This is because the quadrilateral is either
// smaller than the geometry defined by the sector (when deltaLon >= 180), or the quadrilateral degenerates to
// two points (when deltaLat >= 180). So we compute a bounging cylinder that spans the equator and covers the
// sector's latitude range. In some cases this cylinder may be too large, but we're typically not interested
// in culling these cylinders since the sector will span most of the globe.
if (sector.getDeltaLatDegrees() >= 180d || sector.getDeltaLonDegrees() >= 180d)
{
return computeVerticalBoundsFromSectorLatitudeRange(globe, sector, minHeight, maxHeight);
}
// Otherwise, create a standard bounding cylinder that minimally surrounds the specified sector and elevations.
else
{
return computeVerticalBoundsFromSectorQuadrilateral(globe, sector, minHeight, maxHeight);
}
}
/**
* Compute the Cylinder that surrounds the equator, and has height defined by the sector's minumum and maximum
* latitudes (including maxHeight).
*
* @param globe The globe associated with the sector.
* @param sector the sector to return the bounding cylinder for.
* @param minHeight the minimum height to include in the bounding cylinder.
* @param maxHeight the maximum height to include in the bounding cylinder.
*
* @return the minimal bounding cylinder in Cartesianl coordinates.
*
* @throws IllegalArgumentException if <code>sector</code> is null
*/
@SuppressWarnings({"UnusedDeclaration"})
protected static Cylinder computeVerticalBoundsFromSectorLatitudeRange(Globe globe, Sector sector, double minHeight,
double maxHeight)
{
if (sector == null)
{
String msg = Logging.getMessage("nullValue.SectorIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
Vec4 centerPoint = Vec4.ZERO;
Vec4 axis = Vec4.UNIT_Y;
double radius = globe.getEquatorialRadius() + maxHeight;
// Compute the sector's lowest projection along the cylinder axis. This will be a point of minimum latitude
// with maxHeight.
Vec4 extremePoint = globe.computePointFromPosition(sector.getMinLatitude(), sector.getMinLongitude(),
maxHeight);
double minProj = extremePoint.subtract3(centerPoint).dot3(axis);
// Compute the sector's lowest highest along the cylinder axis. This will be a point of maximum latitude
// with maxHeight.
extremePoint = globe.computePointFromPosition(sector.getMaxLatitude(), sector.getMaxLongitude(), maxHeight);
double maxProj = extremePoint.subtract3(centerPoint).dot3(axis);
Vec4 bottomCenterPoint = axis.multiply3(minProj).add3(centerPoint);
Vec4 topCenterPoint = axis.multiply3(maxProj).add3(centerPoint);
if (radius == 0)
radius = 1;
if (bottomCenterPoint.equals(topCenterPoint))
topCenterPoint = bottomCenterPoint.add3(new Vec4(1, 0, 0));
return new Cylinder(bottomCenterPoint, topCenterPoint, radius);
}
/**
* Returns a cylinder that minimally surrounds the specified height range in the sector.
*
* @param globe The globe associated with the sector.
* @param sector the sector to return the bounding cylinder for.
* @param minHeight the minimum height to include in the bounding cylinder.
* @param maxHeight the maximum height to include in the bounding cylinder.
*
* @return The minimal bounding cylinder in Cartesian coordinates.
*
* @throws IllegalArgumentException if <code>sector</code> is null
*/
protected static Cylinder computeVerticalBoundsFromSectorQuadrilateral(Globe globe, Sector sector, double minHeight,
double maxHeight)
{
if (sector == null)
{
String msg = Logging.getMessage("nullValue.SectorIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
// Get three non-coincident points on the sector's quadrilateral. We choose the north or south pair that is
// closest to the equator, then choose a third point from the opposite pair. We use maxHeight as elevation
// because we want to bound the largest potential quadrilateral for the sector.
Vec4 p0, p1, p2;
if (Math.abs(sector.getMinLatitude().degrees) <= Math.abs(sector.getMaxLatitude().degrees))
{
p0 = globe.computePointFromPosition(sector.getMinLatitude(), sector.getMaxLongitude(), maxHeight); // SE
p1 = globe.computePointFromPosition(sector.getMinLatitude(), sector.getMinLongitude(), maxHeight); // SW
p2 = globe.computePointFromPosition(sector.getMaxLatitude(), sector.getMinLongitude(), maxHeight); // NW
}
else
{
p0 = globe.computePointFromPosition(sector.getMaxLatitude(), sector.getMinLongitude(), maxHeight); // NW
p1 = globe.computePointFromPosition(sector.getMaxLatitude(), sector.getMaxLongitude(), maxHeight); // NE
p2 = globe.computePointFromPosition(sector.getMinLatitude(), sector.getMinLongitude(), maxHeight); // SW
}
// Compute the center, axis, and radius of the circle that circumscribes the three points.
// This circle is guaranteed to circumscribe all four points of the sector's Cartesian quadrilateral.
Vec4[] centerOut = new Vec4[1];
Vec4[] axisOut = new Vec4[1];
double[] radiusOut = new double[1];
if (!WWMath.computeCircleThroughPoints(p0, p1, p2, centerOut, axisOut, radiusOut))
{
// If the computation failed, then two of the points are coincident. Fall back to creating a bounding
// cylinder based on the vertices of the sector. This bounding cylinder won't be as tight a fit, but
// it will be correct.
return computeVerticalBoundsFromSectorVertices(globe, sector, minHeight, maxHeight);
}
Vec4 centerPoint = centerOut[0];
Vec4 axis = axisOut[0];
double radius = radiusOut[0];
// Compute the sector's lowest projection along the cylinder axis. We test opposite corners of the sector
// using minHeight. One of these will be the lowest point in the sector.
Vec4 extremePoint = globe.computePointFromPosition(sector.getMinLatitude(), sector.getMinLongitude(),
minHeight);
double minProj = extremePoint.subtract3(centerPoint).dot3(axis);
extremePoint = globe.computePointFromPosition(sector.getMaxLatitude(), sector.getMaxLongitude(), minHeight);
minProj = Math.min(minProj, extremePoint.subtract3(centerPoint).dot3(axis));
// Compute the sector's highest projection along the cylinder axis. We only need to use the point at the
// sector's centroid with maxHeight. This point is guaranteed to be the highest point in the sector.
LatLon centroid = sector.getCentroid();
extremePoint = globe.computePointFromPosition(centroid.getLatitude(), centroid.getLongitude(), maxHeight);
double maxProj = extremePoint.subtract3(centerPoint).dot3(axis);
Vec4 bottomCenterPoint = axis.multiply3(minProj).add3(centerPoint);
Vec4 topCenterPoint = axis.multiply3(maxProj).add3(centerPoint);
if (radius == 0)
radius = 1;
if (bottomCenterPoint.equals(topCenterPoint))
topCenterPoint = bottomCenterPoint.add3(new Vec4(1, 0, 0));
return new Cylinder(bottomCenterPoint, topCenterPoint, radius);
}
/**
* Returns a cylinder that surrounds the specified height range in the zero-area sector. The returned cylinder won't
* be as tight a fit as <code>computeBoundsFromSectorQuadrilateral</code>.
*
* @param globe The globe associated with the sector.
* @param sector the sector to return the bounding cylinder for.
* @param minHeight the minimum height to include in the bounding cylinder.
* @param maxHeight the maximum height to include in the bounding cylinder.
*
* @return The minimal bounding cylinder in Cartesian coordinates.
*
* @throws IllegalArgumentException if <code>sector</code> is null
*/
protected static Cylinder computeVerticalBoundsFromSectorVertices(Globe globe, Sector sector, double minHeight,
double maxHeight)
{
if (sector == null)
{
String msg = Logging.getMessage("nullValue.SectorIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
// Compute the top center point as the surface point with maxHeight at the sector's centroid.
LatLon centroid = sector.getCentroid();
Vec4 topCenterPoint = globe.computePointFromPosition(centroid.getLatitude(), centroid.getLongitude(),
maxHeight);
// Compute the axis as the surface normal at the sector's centroid.
Vec4 axis = globe.computeSurfaceNormalAtPoint(topCenterPoint);
// Compute the four corner points of the sector with minHeight.
Vec4 southwest = globe.computePointFromPosition(sector.getMinLatitude(), sector.getMinLongitude(), minHeight);
Vec4 southeast = globe.computePointFromPosition(sector.getMinLatitude(), sector.getMaxLongitude(), minHeight);
Vec4 northeast = globe.computePointFromPosition(sector.getMaxLatitude(), sector.getMaxLongitude(), minHeight);
Vec4 northwest = globe.computePointFromPosition(sector.getMaxLatitude(), sector.getMinLongitude(), minHeight);
// Compute the bottom center point as the lowest projection along the axis.
double minProj = southwest.subtract3(topCenterPoint).dot3(axis);
minProj = Math.min(minProj, southeast.subtract3(topCenterPoint).dot3(axis));
minProj = Math.min(minProj, northeast.subtract3(topCenterPoint).dot3(axis));
minProj = Math.min(minProj, northwest.subtract3(topCenterPoint).dot3(axis));
Vec4 bottomCenterPoint = axis.multiply3(minProj).add3(topCenterPoint);
// Compute the radius as the maximum distance from the top center point to any of the corner points.
double radius = topCenterPoint.distanceTo3(southwest);
radius = Math.max(radius, topCenterPoint.distanceTo3(southeast));
radius = Math.max(radius, topCenterPoint.distanceTo3(northeast));
radius = Math.max(radius, topCenterPoint.distanceTo3(northwest));
if (radius == 0)
radius = 1;
if (bottomCenterPoint.equals(topCenterPoint))
topCenterPoint = bottomCenterPoint.add3(new Vec4(1, 0, 0));
return new Cylinder(bottomCenterPoint, topCenterPoint, radius);
}
/**
* Display the cylinder.
*
* @param dc the current draw context.
*
* @throws IllegalArgumentException if the draw context is null.
*/
public void render(DrawContext dc)
{
if (dc == null)
{
String msg = Logging.getMessage("nullValue.DrawContextIsNull");
Logging.logger().severe(msg);
throw new IllegalArgumentException(msg);
}
// Compute a matrix that will transform world coordinates to cylinder coordinates. The negative z-axis
// will point from the cylinder's bottomCenter to its topCenter. The y-axis will be a vector that is
// perpendicular to the cylinder's axisUnitDirection. Because the cylinder is symmetric, it does not matter
// in what direction the y-axis points, as long as it is perpendicular to the z-axis.
double tolerance = 1e-6;
Vec4 upVector = (this.axisUnitDirection.cross3(Vec4.UNIT_Y).getLength3() <= tolerance) ?
Vec4.UNIT_NEGATIVE_Z : Vec4.UNIT_Y;
Matrix transformMatrix = Matrix.fromModelLookAt(this.bottomCenter, this.topCenter, upVector);
double[] matrixArray = new double[16];
transformMatrix.toArray(matrixArray, 0, false);
GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
OGLStackHandler ogsh = new OGLStackHandler();
ogsh.pushAttrib(gl, GL2.GL_CURRENT_BIT | GL2.GL_ENABLE_BIT | GL2.GL_TRANSFORM_BIT | GL2.GL_DEPTH_BUFFER_BIT);
try
{
// The cylinder is drawn with as a wireframe plus a center axis. It's drawn in two passes in order to
// visualize the portions of the cylinder above and below an intersecting surface.
gl.glEnable(GL.GL_BLEND);
OGLUtil.applyBlending(gl, false);
gl.glEnable(GL.GL_DEPTH_TEST);
// Draw the axis
gl.glDepthFunc(GL.GL_LEQUAL); // draw the part that would normally be visible
gl.glColor4f(1f, 1f, 1f, 0.4f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3d(this.bottomCenter.x, this.bottomCenter.y, this.bottomCenter.z);
gl.glVertex3d(this.topCenter.x, this.topCenter.y, this.topCenter.z);
gl.glEnd();
gl.glDepthFunc(GL.GL_GREATER); // draw the part that is behind an intersecting surface
gl.glColor4f(1f, 0f, 1f, 0.4f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3d(this.bottomCenter.x, this.bottomCenter.y, this.bottomCenter.z);
gl.glVertex3d(this.topCenter.x, this.topCenter.y, this.topCenter.z);
gl.glEnd();
// Draw the exterior wireframe
ogsh.pushModelview(gl);
gl.glMultMatrixd(matrixArray, 0);
GLUquadric quadric = dc.getGLU().gluNewQuadric();
dc.getGLU().gluQuadricDrawStyle(quadric, GLU.GLU_LINE);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glColor4f(1f, 1f, 1f, 0.5f);
dc.getGLU().gluCylinder(quadric, this.cylinderRadius, this.cylinderRadius, this.cylinderHeight, 30, 30);
gl.glDepthFunc(GL.GL_GREATER);
gl.glColor4f(1f, 0f, 1f, 0.4f);
dc.getGLU().gluCylinder(quadric, this.cylinderRadius, this.cylinderRadius, this.cylinderHeight, 30, 30);
dc.getGLU().gluDeleteQuadric(quadric);
}
finally
{
ogsh.pop(gl);
}
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof Cylinder))
return false;
Cylinder cylinder = (Cylinder) o;
if (Double.compare(cylinder.cylinderHeight, cylinderHeight) != 0)
return false;
if (Double.compare(cylinder.cylinderRadius, cylinderRadius) != 0)
return false;
if (axisUnitDirection != null ? !axisUnitDirection.equals(cylinder.axisUnitDirection)
: cylinder.axisUnitDirection != null)
return false;
if (bottomCenter != null ? !bottomCenter.equals(cylinder.bottomCenter) : cylinder.bottomCenter != null)
return false;
//noinspection RedundantIfStatement
if (topCenter != null ? !topCenter.equals(cylinder.topCenter) : cylinder.topCenter != null)
return false;
return true;
}
@Override
public int hashCode()
{
int result;
long temp;
result = bottomCenter != null ? bottomCenter.hashCode() : 0;
result = 31 * result + (topCenter != null ? topCenter.hashCode() : 0);
result = 31 * result + (axisUnitDirection != null ? axisUnitDirection.hashCode() : 0);
temp = cylinderRadius != +0.0d ? Double.doubleToLongBits(cylinderRadius) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = cylinderHeight != +0.0d ? Double.doubleToLongBits(cylinderHeight) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
public String toString()
{
return this.cylinderRadius + ", " + this.bottomCenter.toString() + ", " + this.topCenter.toString() + ", "
+ this.axisUnitDirection.toString();
}
}