forked from Esri/geometry-api-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvelope.java
More file actions
1098 lines (963 loc) · 29 KB
/
Envelope.java
File metadata and controls
1098 lines (963 loc) · 29 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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 1995-2013 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373
email: contracts@esri.com
*/
package com.esri.core.geometry;
import java.io.Serializable;
import com.esri.core.geometry.VertexDescription.Semantics;
/**
* An envelope is an axis-aligned rectangle.
*/
public final class Envelope extends Geometry implements Serializable {
private static final long serialVersionUID = 2L;
Envelope2D m_envelope = new Envelope2D();
double[] m_attributes;// use doubles to store everything (int64 are bitcast)
/**
* Creates an envelope by defining its center, width, and height.
*
* @param center
* The center point of the envelope.
* @param width
* The width of the envelope.
* @param height
* The height of the envelope.
*/
public Envelope(Point center, double width, double height) {
m_description = VertexDescriptionDesignerImpl.getDefaultDescriptor2D();
m_envelope.setEmpty();
if (center.isEmpty())
return;
_setFromPoint(center, width, height);
}
Envelope(Envelope2D env2D) {
m_description = VertexDescriptionDesignerImpl.getDefaultDescriptor2D();
m_envelope.setCoords(env2D);
m_envelope.normalize();
}
Envelope(VertexDescription vd) {
if (vd == null)
throw new IllegalArgumentException();
m_description = vd;
m_envelope.setEmpty();
}
Envelope(VertexDescription vd, Envelope2D env2D) {
if (vd == null)
throw new IllegalArgumentException();
m_description = vd;
m_envelope.setCoords(env2D);
m_envelope.normalize();
}
/**
* Constructs an empty envelope.
*/
public Envelope() {
m_description = VertexDescriptionDesignerImpl.getDefaultDescriptor2D();
m_envelope.setEmpty();
}
/**
* Constructs an envelope that covers the given point. The coordinates of
* the point are used to set the extent of the envelope.
*
* @param point The point that the envelope covers.
*/
public Envelope(Point point) {
m_description = VertexDescriptionDesignerImpl.getDefaultDescriptor2D();
m_envelope.setEmpty();
if (point.isEmpty())
return;
_setFromPoint(point);
}
/**
* Constructs an envelope with the specified X and Y extents.
*
* @param xmin
* The minimum x-coordinate of the envelope.
* @param ymin
* The minimum y-coordinate of the envelope.
* @param xmax
* The maximum x-coordinate of the envelope.
* @param ymax
* The maximum y-coordinate of the envelope.
*/
public Envelope(double xmin, double ymin, double xmax, double ymax) {
m_description = VertexDescriptionDesignerImpl.getDefaultDescriptor2D();
setCoords(xmin, ymin, xmax, ymax);
}
/**
* Sets the 2-dimensional extents of the envelope.
*
* @param xmin
* The minimum x-coordinate of the envelope.
* @param ymin
* The minimum y-coordinate of the envelope.
* @param xmax
* The maximum x-coordinate of the envelope.
* @param ymax
* The maximum y-coordinate of the envelope.
*/
public void setCoords(double xmin, double ymin, double xmax, double ymax) {
_touch();
m_envelope.setCoords(xmin, ymin, xmax, ymax);
}
void setCoords(Point[] points) {
_touch();
setEmpty();
for (int i = 0, n = points.length; i < n; i++)
merge(points[i]);
}
void setEnvelope2D(Envelope2D e2d) {
_touch();
if (!e2d.isValid())
throw new IllegalArgumentException();
m_envelope.setCoords(e2d);
}
/**
* Removes all points from this geometry.
*/
@Override
public void setEmpty() {
_touch();
m_envelope.setEmpty();
}
/**
* Indicates whether this envelope contains any points.
*
* @return boolean Returns true if the envelope is empty.
*/
@Override
public boolean isEmpty() {
return m_envelope.isEmpty();
}
/**
* The width of the envelope.
*
* @return The width of the envelope.
*/
public double getWidth() {
return m_envelope.getWidth();
}
/**
* The height of the envelope.
*
* @return The height of the envelope.
*/
public double getHeight() {
return m_envelope.getHeight();
}
/**
* The x-coordinate of the center of the envelope.
*
* @return The x-coordinate of the center of the envelope.
*/
public double getCenterX() {
return m_envelope.getCenterX();
}
/**
* The y-coordinate of center of the envelope.
*
* @return The y-coordinate of center of the envelope.
*/
public double getCenterY() {
return m_envelope.getCenterY();
}
/**
* The x and y-coordinates of the center of the envelope.
*
* @return A point whose x and y-coordinates are that of the center of the envelope.
*/
Point2D getCenterXY() {
return m_envelope.getCenter();
}
void getCenter(Point point_out) {
point_out.assignVertexDescription(m_description);
if (isEmpty()) {
point_out.setEmpty();
return;
}
int nattrib = m_description.getAttributeCount();
for (int i = 1; i < nattrib; i++) {
int semantics = m_description.getSemantics(i);
int ncomp = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomp; iord++) {
double v = 0.5 * (getAttributeAsDblImpl_(0, semantics, iord) + getAttributeAsDblImpl_(
1, semantics, iord));
point_out.setAttribute(semantics, iord, v);
}
}
point_out.setXY(m_envelope.getCenter());
}
void merge(Point2D pt) {
_touch();
m_envelope.merge(pt);
}
/**
* Merges this envelope with the extent of the given envelope. If this
* envelope is empty, the coordinates of the given envelope
* are assigned. If the given envelope is empty, this envelope is unchanged.
*
* @param other
* The envelope to merge.
*/
public void merge(Envelope other) {
_touch();
if (other.isEmpty())
return;
VertexDescription otherVD = other.m_description;
if (otherVD != m_description)
mergeVertexDescription(otherVD);
m_envelope.merge(other.m_envelope);
for (int iattrib = 1, nattrib = otherVD.getAttributeCount(); iattrib < nattrib; iattrib++) {
int semantics = otherVD.getSemantics(iattrib);
int ncomps = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomps; iord++) {
Envelope1D intervalOther = other.queryInterval(semantics, iord);
Envelope1D interval = queryInterval(semantics, iord);
interval.merge(intervalOther);
setInterval(semantics, iord, interval);
}
}
}
/**
* Merges this envelope with the point. The boundary of the envelope is
* increased to include the point. If the envelope is empty, the coordinates
* of the point to merge are assigned. If the point is empty, the original
* envelope is unchanged.
*
* @param point
* The point to be merged.
*/
public void merge(Point point) {
_touch();
if (point.isEmptyImpl())
return;
VertexDescription pointVD = point.m_description;
if (m_description != pointVD)
mergeVertexDescription(pointVD);
if (isEmpty()) {
_setFromPoint(point);
return;
}
m_envelope.merge(point.getXY());
for (int iattrib = 1, nattrib = pointVD.getAttributeCount(); iattrib < nattrib; iattrib++) {
int semantics = pointVD._getSemanticsImpl(iattrib);
int ncomps = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomps; iord++) {
double v = point.getAttributeAsDbl(semantics, iord);
Envelope1D interval = queryInterval(semantics, iord);
interval.merge(v);
setInterval(semantics, iord, interval);
}
}
}
void _setFromPoint(Point centerPoint, double width, double height) {
m_envelope.setCoords(centerPoint.getXY(), width, height);
VertexDescription pointVD = centerPoint.m_description;
for (int iattrib = 1, nattrib = pointVD.getAttributeCount(); iattrib < nattrib; iattrib++) {
int semantics = pointVD._getSemanticsImpl(iattrib);
int ncomps = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomps; iord++) {
double v = centerPoint.getAttributeAsDbl(semantics, iord);
setInterval(semantics, iord, v, v);
}
}
}
void _setFromPoint(Point centerPoint) {
m_envelope.setCoords(centerPoint.m_attributes[0],
centerPoint.m_attributes[1]);
VertexDescription pointVD = centerPoint.m_description;
for (int iattrib = 1, nattrib = pointVD.getAttributeCount(); iattrib < nattrib; iattrib++) {
int semantics = pointVD._getSemanticsImpl(iattrib);
int ncomps = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomps; iord++) {
double v = centerPoint.getAttributeAsDbl(semantics, iord);
setInterval(semantics, iord, v, v);
}
}
}
void merge(Envelope2D other) {
_touch();
m_envelope.merge(other);
}
public void setInterval(int semantics, int ordinate, double vmin,
double vmax) {
setInterval(semantics, ordinate, new Envelope1D(vmin, vmax));
}
/**
* Re-aspects this envelope to fit within the specified width and height.
*
* @param arWidth
* The width within which to fit the envelope.
* @param arHeight
* The height within which to fit the envelope.
*/
public void reaspect(double arWidth, double arHeight) {
_touch();
m_envelope.reaspect(arWidth, arHeight);
}
/**
* Changes the dimensions of the envelope while preserving the center. New width
* is Width + 2 * dx, new height is Height + 2 * dy. If the result envelope
* width or height becomes negative, the envelope is set to be empty.
*
* @param dx
* The inflation along the x-axis.
* @param dy
* The inflation along the y-axis.
*/
public void inflate(double dx, double dy) {
_touch();
m_envelope.inflate(dx, dy);
}
@Override
public void applyTransformation(Transformation2D transform) {
_touch();
transform.transform(m_envelope);
}
@Override
void applyTransformation(Transformation3D transform) {
_touch();
if (!m_envelope.isEmpty()) {
Envelope3D env = new Envelope3D();
queryEnvelope3D(env);
if (env.isEmptyZ())
env.setEmpty(); // Z components is empty, the
// AffineTransformation3D makes the whole
// envelope empty. Consider
// throwing an assert instead.
else
transform.transform(env);
}
}
@Override
public void copyTo(Geometry dst) {
if (dst.getType() != getType())
throw new IllegalArgumentException();
Envelope envDst = (Envelope) dst;
dst._touch();
envDst.m_description = m_description;
envDst.m_envelope.setCoords(m_envelope);
envDst._resizeAttributes(m_description._getTotalComponents() - 2);
_attributeCopy(m_attributes, 0, envDst.m_attributes, 0,
(m_description._getTotalComponents() - 2) * 2);
}
@Override
public Geometry createInstance() {
return new Envelope(m_description);
}
@Override
public double calculateArea2D() {
return m_envelope.getArea();
}
@Override
public double calculateLength2D() {
return m_envelope.getLength();
}
@Override
public Geometry.Type getType() {
return Type.Envelope;
}
@Override
public int getDimension() {
return 2;
}
@Override
public void queryEnvelope(Envelope env) {
copyTo(env);
}
@Override
public void queryEnvelope2D(Envelope2D env) {
env.xmin = m_envelope.xmin;
env.ymin = m_envelope.ymin;
env.xmax = m_envelope.xmax;
env.ymax = m_envelope.ymax;
}
@Override
void queryEnvelope3D(Envelope3D env) {
env.xmin = m_envelope.xmin;
env.ymin = m_envelope.ymin;
env.xmax = m_envelope.xmax;
env.ymax = m_envelope.ymax;
env.setCoords(m_envelope.xmin, m_envelope.ymin,
_getAttributeAsDbl(0, Semantics.Z, 0), m_envelope.xmax,
m_envelope.ymax, _getAttributeAsDbl(1, Semantics.Z, 0));
}
@Override
public Envelope1D queryInterval(int semantics, int ordinate) {
Envelope1D env = new Envelope1D();
env.setCoords(_getAttributeAsDbl(0, semantics, ordinate),
_getAttributeAsDbl(1, semantics, ordinate));
return env;
}
public void setInterval(int semantics, int ordinate, Envelope1D env) {
_touch();
if (semantics == Semantics.POSITION) {
if (ordinate == 0) {
m_envelope.xmin = env.vmin;
m_envelope.xmax = env.vmax;
} else if (ordinate == 1) {
m_envelope.ymin = env.vmin;
m_envelope.ymax = env.vmax;
} else
throw new IndexOutOfBoundsException();
} else {
_setAttributeAsDbl(0, semantics, ordinate, env.vmin);
_setAttributeAsDbl(1, semantics, ordinate, env.vmax);
}
}
void queryCoordinates(Point2D[] dst) {
if (dst == null || dst.length < 4 || m_envelope.isEmpty())
throw new IllegalArgumentException();
m_envelope.queryCorners(dst);
}
/**
* Sets the point's coordinates to the coordinates of the envelope at the
* given corner.
*
* @param index
* The index of the envelope's corners from 0 to 3.
* <p>
* 0 = lower left corner
* <p>
* 1 = top-left corner
* <p>
* 2 = top right corner
* <p>
* 3 = bottom right corner
* @param ptDst
* The point whose coordinates are used to set the envelope's
* coordinate at a specified corner.
*/
public void queryCornerByVal(int index, Point ptDst) {
ptDst.assignVertexDescription(m_description);
int nattrib = getDescription().getAttributeCount() - 1;
switch (index) {
case 0: {
for (int i = 0; i < nattrib; i++) {
int semantics = m_description.getSemantics(i);
int ncomp = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomp; iord++)
ptDst.setAttribute(semantics, iord,
_getAttributeAsDbl(0, semantics, iord));
}
ptDst.setXY(m_envelope.xmin, m_envelope.ymin);
return;
}
case 1: {
for (int i = 0; i < nattrib; i++) {
int semantics = m_description.getSemantics(i);
int ncomp = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomp; iord++)
ptDst.setAttribute(semantics, iord,
_getAttributeAsDbl(1, semantics, iord));
}
ptDst.setXY(m_envelope.xmin, m_envelope.ymax);
return;
}
case 2: {
for (int i = 0; i < nattrib; i++) {
int semantics = m_description.getSemantics(i);
int ncomp = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomp; iord++)
ptDst.setAttribute(semantics, iord,
_getAttributeAsDbl(0, semantics, iord));
}
ptDst.setXY(m_envelope.xmax, m_envelope.ymax);
return;
}
case 3: {
for (int i = 0; i < nattrib; i++) {
int semantics = m_description.getSemantics(i);
int ncomp = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomp; iord++)
ptDst.setAttribute(semantics, iord,
_getAttributeAsDbl(1, semantics, iord));
}
ptDst.setXY(m_envelope.xmax, m_envelope.ymin);
return;
}
default:
throw new IndexOutOfBoundsException();
}
}
void queryCorner(int index, Point2D ptDst) {
Point2D p = m_envelope.queryCorner(index);
ptDst.setCoords(p.x, p.y);
}
int getEndPointOffset(VertexDescription descr, int end_point) {
return end_point * (descr.getTotalComponentCount() - 2);
}
double getAttributeAsDblImpl_(int end_point, int semantics, int ordinate) {
if (m_envelope.isEmpty())
throw new GeometryException("empty geometry");
assert (end_point == 0 || end_point == 1);
if (semantics == VertexDescription.Semantics.POSITION) {
if (end_point != 0) {
return ordinate != 0 ? m_envelope.ymax : m_envelope.xmax;
} else {
return ordinate != 0 ? m_envelope.ymin : m_envelope.xmin;
}
}
int ncomps = VertexDescription.getComponentCount(semantics);
if (ordinate >= ncomps)
throw new IllegalArgumentException();
int attribute_index = m_description.getAttributeIndex(semantics);
if (attribute_index >= 0) {
return m_attributes[getEndPointOffset(m_description, end_point)
+ m_description.getPointAttributeOffset_(attribute_index)
- 2 + ordinate];
}
return VertexDescription.getDefaultValue(semantics);
}
void setAttributeAsDblImpl_(int end_point, int semantics, int ordinate,
double value) {
assert (end_point == 0 || end_point == 1);
if (semantics == VertexDescription.Semantics.POSITION) {
if (end_point != 0) {
if (ordinate != 0)
m_envelope.ymax = value;
else
m_envelope.xmax = value;
} else {
if (ordinate != 0)
m_envelope.ymin = value;
else
m_envelope.xmin = value;
}
}
int ncomps = VertexDescription.getComponentCount(semantics);
if (ordinate >= ncomps)
throw new IllegalArgumentException();
addAttribute(semantics);
int attribute_index = m_description.getAttributeIndex(semantics);
m_attributes[getEndPointOffset(m_description, end_point)
+ m_description.getPointAttributeOffset_(attribute_index) - 2
+ ordinate] = value;
}
void _resizeAttributes(int newSize) {// copied from
// Segment::_ResizeAttributes
_touch();
if (m_attributes == null) {
m_attributes = new double[newSize * 2];
} else if (m_attributes.length < newSize * 2) {
double[] newBuffer = new double[newSize * 2];
System.arraycopy(m_attributes, 0, newBuffer, 0, m_attributes.length);
m_attributes = newBuffer;
}
}
@Override
void _beforeDropAttributeImpl(int semantics) {// copied from
// Segment::_BeforeDropAttributeImpl
if (m_envelope.isEmpty())
return;
// _ASSERT(semantics != enum_value2(VertexDescription, Semantics,
// POSITION));
int attributeIndex = m_description.getAttributeIndex(semantics);
int offset = m_description._getPointAttributeOffset(attributeIndex) - 2;
int comps = VertexDescription.getComponentCount(semantics);
int totalCompsOld = m_description._getTotalComponents() - 2;
if (totalCompsOld > comps) {
int offset0 = _getEndPointOffset(0);
for (int i = offset + comps; i < totalCompsOld * 2; i++)
m_attributes[offset0 + i - comps] = m_attributes[offset0 + i];
int offset1 = _getEndPointOffset(1) - comps; // -comp is for deleted
// attribute of
// start vertex
for (int i = offset + comps; i < totalCompsOld; i++)
m_attributes[offset1 + i - comps] = m_attributes[offset1 + i];
}
}
@Override
void _afterAddAttributeImpl(int semantics) {// copied from
// Segment::_AfterAddAttributeImpl
int attributeIndex = m_description.getAttributeIndex(semantics);
int offset = m_description._getPointAttributeOffset(attributeIndex) - 2;
int comps = VertexDescription.getComponentCount(semantics);
int totalComps = m_description._getTotalComponents() - 2;
_resizeAttributes(totalComps);
int totalCompsOld = totalComps - comps; // the total number of
// components before resize.
int offset0 = _getEndPointOffset(0);
int offset1 = _getEndPointOffset(1);
int offset1old = offset1 - comps;
for (int i = totalCompsOld - 1; i >= 0; i--) {// correct the position of
// the End attributes
m_attributes[offset1 + i] = m_attributes[offset1old + i];
}
// move attributes for start end end points that go after the insertion
// point
for (int i = totalComps - 1; i >= offset + comps; i--) {
m_attributes[offset0 + i] = m_attributes[offset0 + i - comps];
m_attributes[offset1 + i] = m_attributes[offset1 + i - comps];
}
// initialize added attribute to the default value.
double dv = VertexDescription.getDefaultValue(semantics);
for (int i = 0; i < comps; i++) {
m_attributes[offset0 + offset + i] = dv;
m_attributes[offset1 + offset + i] = dv;
}
}
static void _attributeCopy(double[] src, int srcStart, double[] dst,
int dstStart, int count) {
// FIXME performance!!!!
// System.arraycopy(src, srcStart, dst, dstStart, count);
for (int i = 0; i < count; i++)
dst[dstStart + i] = src[i + srcStart];
}
double _getAttributeAsDbl(int endPoint, int semantics, int ordinate) {
if (m_envelope.isEmpty())
throw new GeometryException(
"This operation was performed on an Empty Geometry.");
// _ASSERT(endPoint == 0 || endPoint == 1);
if (semantics == Semantics.POSITION) {
if (endPoint != 0) {
return ordinate != 0 ? m_envelope.ymax : m_envelope.xmax;
} else {
return ordinate != 0 ? m_envelope.ymin : m_envelope.xmin;
}
}
int ncomps = VertexDescription.getComponentCount(semantics);
if (ordinate >= ncomps)
throw new IndexOutOfBoundsException();
int attributeIndex = m_description.getAttributeIndex(semantics);
if (attributeIndex >= 0) {
if (null != m_attributes)
_resizeAttributes(m_description._getTotalComponents() - 2);
return m_attributes[_getEndPointOffset(endPoint)
+ m_description._getPointAttributeOffset(attributeIndex)
- 2 + ordinate];
} else
return VertexDescription.getDefaultValue(semantics);
}
void _setAttributeAsDbl(int endPoint, int semantics, int ordinate,
double value) {
_touch();
// _ASSERT(endPoint == 0 || endPoint == 1);
if (semantics == Semantics.POSITION) {
if (endPoint != 0) {
if (ordinate != 0)
m_envelope.ymax = value;
else
m_envelope.xmax = value;
} else {
if (ordinate != 0)
m_envelope.ymin = value;
else
m_envelope.xmin = value;
}
}
int ncomps = VertexDescription.getComponentCount(semantics);
if (ordinate >= ncomps)
throw new IndexOutOfBoundsException();
if (!hasAttribute(semantics)) {
if (VertexDescription.isDefaultValue(semantics, value))
return;
addAttribute(semantics);
}
int attributeIndex = m_description.getAttributeIndex(semantics);
if (null == m_attributes)
_resizeAttributes(m_description._getTotalComponents() - 2);
m_attributes[_getEndPointOffset(endPoint)
+ m_description._getPointAttributeOffset(attributeIndex) - 2
+ ordinate] = value;
}
int _getAttributeAsInt(int endPoint, int semantics, int ordinate) {
return (int) _getAttributeAsDbl(endPoint, semantics, ordinate);
}
int _getEndPointOffset(int endPoint) {
return endPoint * (m_description._getTotalComponents() - 2);
}
boolean isIntersecting(Envelope2D other) {
return m_envelope.isIntersecting(other);
}
/**
* Changes this envelope to be the intersection of itself with the other
* envelope.
*
* @param other
* The envelope to intersect.
* @return Returns true if the result is not empty.
*/
public boolean intersect(Envelope other) {
_touch();
Envelope2D e2d = new Envelope2D();
other.queryEnvelope2D(e2d);
return m_envelope.intersect(e2d);
}
/**
* Returns true if the envelope and the other given envelope intersect.
*
* @param other
* The envelope to with which to test intersection.
* @return Returns true if the two envelopes intersect.
*/
public boolean isIntersecting(Envelope other) {// TODO: attributes.
return m_envelope.isIntersecting(other.m_envelope);
}
/**
* Sets the envelope's corners to be centered around the specified point,
* using its center, width, and height.
*
* @param c
* The point around which to center the envelope.
* @param w
* The width to be set for the envelope.
* @param h
* The height to be set for this envelope.
*/
public void centerAt(Point c, double w, double h) {
_touch();
if (c.isEmpty()) {
setEmpty();
return;
}
_setFromPoint(c, w, h);
}
/**
* Offsets the envelope by the specified distances along x and y-coordinates.
*
* @param dx
* The X offset to be applied.
* @param dy
* The Y offset to be applied.
*/
public void offset(double dx, double dy) {
_touch();
m_envelope.offset(dx, dy);
}
/**
* Normalizes envelopes if the minimum dimension is larger than the
* maximum dimension.
*/
public void normalize() {// TODO: attributes
_touch();
m_envelope.normalize();
}
/**
* Gets the center point of the envelope. The center point occurs at: ((XMin
* + XMax) / 2, (YMin + YMax) / 2).
*
* @return The center point of the envelope.
*/
Point2D getCenter2D() {
return m_envelope.getCenter();
}
/**
* Returns the center point of the envelope.
*
* @return The center point of the envelope.
*/
public Point getCenter() {
Point pointOut = new Point(m_description);
if (isEmpty()) {
return pointOut;
}
int nattrib = m_description.getAttributeCount();
for (int i = 1; i < nattrib; i++) {
int semantics = m_description._getSemanticsImpl(i);
int ncomp = VertexDescription.getComponentCount(semantics);
for (int iord = 0; iord < ncomp; iord++) {
double v = 0.5 * (_getAttributeAsDbl(0, semantics, iord) + _getAttributeAsDbl(
1, semantics, iord));
pointOut.setAttribute(semantics, iord, v);
}
}
pointOut.setXY(m_envelope.getCenterX(), m_envelope.getCenterY());
return pointOut;
}
/**
* Centers the envelope around the specified point preserving the envelope's
* width and height.
*
* @param c
* The new center point.
*/
public void centerAt(Point c) {
_touch();
if (c.isEmpty()) {
setEmpty();
return;
}
m_envelope.centerAt(c.getX(), c.getY());
}
/**
* Returns the envelope's lower left corner point.
*
* @return Returns the lower left corner point.
*/
public Point getLowerLeft() {
return m_envelope.getLowerLeft();
}
/**
* Returns the envelope's upper right corner point.
*
* @return Returns the upper right corner point.
*/
public Point getUpperRight() {
return m_envelope.getUpperRight();
}
/**
* Returns the envelope's lower right corner point.
*
* @return Returns the lower right corner point.
*/
public Point getLowerRight() {
return m_envelope.getLowerRight();
}
/**
* Returns the envelope's upper left corner point.
*
* @return Returns the upper left corner point.
*/
public Point getUpperLeft() {
return m_envelope.getUpperLeft();
}
/**
* Checks if this envelope contains (covers) the specified point.
*
* @param p
* The Point to be tested for coverage.
* @return TRUE if this envelope contains (covers) the specified point.
*/
public boolean contains(Point p) {
if (p.isEmpty())
return false;
return m_envelope.contains(p.getX(), p.getY());
}
/**
* Checks if this envelope contains (covers) other envelope.
*
* @param env
* The envelope to be tested for coverage.
* @return TRUE if this envelope contains (covers) the specified envelope.
*/
public boolean contains(Envelope env) {
return m_envelope.contains(env.m_envelope);
}
/**
* Returns TRUE when this geometry has exactly same type, properties, and
* coordinates as the other geometry.
*/
@Override
public boolean equals(Object _other) {
if (_other == this)
return true;
if (!(_other instanceof Envelope))
return false;
Envelope other = (Envelope) _other;
if (m_description != other.m_description)
return false;
if (isEmpty())
if (other.isEmpty())
return true;
else
return false;
if (!this.m_envelope.equals(other.m_envelope))
return false;
for (int i = 0, n = (m_description._getTotalComponents() - 2) * 2; i < n; i++)
if (m_attributes[i] != other.m_attributes[i])
return false;
return true;
}