/
GeometricalPredicates.jl
1192 lines (1007 loc) · 41.9 KB
/
GeometricalPredicates.jl
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
module GeometricalPredicates
# Fast, robust 2D and 3D geometrical predicates on generic point types.
# Implementation follows algorithms described in http://arxiv.org/abs/0901.4107
# and used (for e.g.) in the Illustris Simulation
# http://www.illustris-project.org/
# Author: Ariel Keselman (skariel@gmail.com)
# License: MIT
# Bug reports welcome!
export
min_coord, max_coord,
AbstractPoint,
AbstractPoint2D,
AbstractPoint3D,
AbstractPositivelyOrientedPrimitive,
AbstractPositivelyOrientedTriangle,
AbstractPositivelyOrientedTetrahedron,
AbstractNegativelyOrientedPrimitive,
AbstractNegativelyOrientedTriangle,
AbstractNegativelyOrientedTetrahedron,
AbstractTriangleUnOriented,
AbstractTetrahedronUnOriented,
TriangleTypes, TetrahedronTypes,
positivelyoriented, negativelyoriented, unoriented, orientation,
Point, Point2D, Point3D, Line, Polygon, getx, gety, getz, geta, getb, getc, getd,
seta, setb, setc, setd, setabc, setabcd,
setab, setbc, setcd, setac, setad, setbd,
setabd, setacd, setbcd,
getlines, getpoints,
Line2D, Polygon2D, Primitive, Triangle, Tetrahedron,
length2, area, volume, centroid, circumcenter, circumradius2, incircle, intriangle, inpolygon,
peanokey, hilbertsort!, mssort!, clean!
const _float_err = eps(Float64)
const _abs_err_incircle_2d = 12*_float_err
const _abs_err_incircle_3d = 48*_float_err
const _abs_err_orientation_2d = 2*_float_err
const _abs_err_orientation_3d = 6*_float_err
const _abs_err_intriangle = 6*_float_err
const _abs_err_intriangle_zero = 2*_float_err
const _abs_err_intetra = 24*_float_err
const _abs_err_intetra_zero = 6*_float_err
const min_coord = 1.0
const max_coord = 2.0 - eps(Float64)
abstract type AbstractPoint end
abstract type AbstractPoint2D <: AbstractPoint end
abstract type AbstractPoint3D <: AbstractPoint end
abstract type AbstractLine2D end
abstract type AbstractPolygon2D end
abstract type AbstractPrimitive end
abstract type AbstractUnOrientedPrimitive <: AbstractPrimitive end
abstract type AbstractOrientedPrimitive <: AbstractPrimitive end
abstract type AbstractPositivelyOrientedPrimitive <: AbstractOrientedPrimitive end
abstract type AbstractNegativelyOrientedPrimitive <: AbstractOrientedPrimitive end
abstract type AbstractTriangleUnOriented <: AbstractUnOrientedPrimitive end
abstract type AbstractTetrahedronUnOriented <: AbstractUnOrientedPrimitive end
abstract type AbstractPositivelyOrientedTriangle <: AbstractPositivelyOrientedPrimitive end
abstract type AbstractNegativelyOrientedTriangle <: AbstractNegativelyOrientedPrimitive end
abstract type AbstractPositivelyOrientedTetrahedron <: AbstractPositivelyOrientedPrimitive end
abstract type AbstractNegativelyOrientedTetrahedron <: AbstractNegativelyOrientedPrimitive end
const TriangleTypes = Union{AbstractTriangleUnOriented, AbstractPositivelyOrientedTriangle, AbstractNegativelyOrientedTriangle}
const TetrahedronTypes = Union{AbstractTetrahedronUnOriented, AbstractPositivelyOrientedTetrahedron, AbstractNegativelyOrientedTetrahedron}
# standard 2D point
struct Point2D <: AbstractPoint2D
_x::Float64
_y::Float64
end
Point2D() = Point2D(0., 0.)
getx(p::Point2D) = p._x
gety(p::Point2D) = p._y
# standard 3D point
struct Point3D <: AbstractPoint3D
_x::Float64
_y::Float64
_z::Float64
end
Point3D() = Point3D(0., 0., 0.)
getx(p::Point3D) = p._x
gety(p::Point3D) = p._y
getz(p::Point3D) = p._z
Point(x::Real, y::Real) = Point2D(Float64(x), Float64(y))
Point(x::Real, y::Real, z::Real) = Point3D(Float64(x), Float64(y), Float64(z))
struct Line2D{T<:AbstractPoint2D} <: AbstractLine2D
_a::T
_b::T
_bx::Float64
_by::Float64
end
function Line2D(a::T, b::T) where {T<:AbstractPoint2D}
bx = getx(b) - getx(a)
by = gety(b) - gety(a)
Line2D(a, b, bx, by)
end
Line(a::T, b::T) where {T<:AbstractPoint2D} = Line2D(a, b)
geta(l::Line2D) = l._a
getb(l::Line2D) = l._b
length2(l::Line2D) = l._bx*l._bx + l._by*l._by
# fine filtered orientation
function _sz_orientation(l::Line2D, p::AbstractPoint2D)
cx = getx(p) - getx(geta(l))
cy = gety(p) - gety(geta(l))
_pr2 = -l._bx*cy + l._by*cx
sz = abs(cx) + abs(cy) + abs(l._bx) + abs(l._by)
if _pr2 < -_abs_err_orientation_2d*sz
1
elseif _pr2 > _abs_err_orientation_2d*sz
-1
else
_exact_sign_orientation_determinant!(
_extract_bigint(getx(geta(l))), _extract_bigint(gety(geta(l))),
_extract_bigint(getx(getb(l))), _extract_bigint(gety(getb(l))),
_extract_bigint(getx(p)), _extract_bigint(gety(p)))
end
end
# gross filtered orientation, asumming maximal line size (=1.0)
function orientation(l::Line2D, p::AbstractPoint2D)
cx = getx(p) - getx(geta(l))
cy = gety(p) - gety(geta(l))
_pr2 = -l._bx*cy + l._by*cx
if _pr2 < -_abs_err_orientation_2d
1
elseif _pr2 > _abs_err_orientation_2d
-1
else
_sz_orientation(l, p)
end
end
"""
struct Polygon2D <: AbstractPolygon2D
Two-dimensional polygon type.
"""
struct Polygon2D{T<:AbstractPoint2D} <: AbstractPolygon2D
_p::Vector{T}
_l::Vector{AbstractLine2D}
function Polygon2D{T}(p::T...) where {T<:AbstractPoint2D}
l = Vector{Line2D}(undef, length(p)-1)
for i in range(1, stop=length(p)-1)
l[i] = Line2D(p[i], p[i+1])
end
push!(l, Line2D(p[end], p[1]))
new([p...;], l)
end
end
Polygon2D(p::T...) where {T<:AbstractPoint2D} = Polygon2D{T}(p...)
Polygon(p::T...) where {T<:AbstractPoint2D} = Polygon2D(p...)
"""
getpoints(polygon)
Return the points of a polygon.
"""
getpoints(polygon::Polygon2D) = polygon._p
"""
getlines(polygon)
Return the lines of a polygon.
"""
getlines(polygon::Polygon2D) = polygon._l
"""
inpolygon(polygon, point)
Return true if `point` is inside `polygon`, which is assumed to be convex.
"""
function inpolygon(polygon::Polygon2D, point::AbstractPoint2D)
lines = getlines(polygon)
side = orientation(lines[1], point)
for i = 2:length(lines)
orientation(lines[i], point) == side || return false
end
true
end
macro _define_triangle_type(name, abstracttype)
oriented = !occursin("UnOriented", string(name))
esc(quote
mutable struct $name{T <: AbstractPoint2D} <: $abstracttype
_a::T; _b::T; _c::T
_bx::Float64; _by::Float64
_cx::Float64; _cy::Float64
_px::Float64; _py::Float64
_pr2::Float64
$((oriented ? tuple() : (:(_o::Int8),))...)
function $name{T}(a::T, b::T, c::T) where T
t = new(a, b, c, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, $((oriented ? tuple() : (0,))...))
clean!(t)
t
end
end
end)
end
@_define_triangle_type(UnOrientedTriangle, AbstractTriangleUnOriented)
@_define_triangle_type(PositivelyOrientedTriangle, AbstractPositivelyOrientedTriangle)
@_define_triangle_type(NegativelyOrientedTriangle, AbstractNegativelyOrientedTriangle)
abstract type AbstractOrientation end
mutable struct PositivelyOriented <: AbstractOrientation; end
mutable struct NegativelyOriented <: AbstractOrientation; end
mutable struct UnOriented <: AbstractOrientation; end
const positivelyoriented = PositivelyOriented()
const negativelyoriented = NegativelyOriented()
const unoriented = UnOriented()
Triangle(a::T, b::T, c::T, ::PositivelyOriented) where {T<:AbstractPoint2D} = PositivelyOrientedTriangle{T}(a, b, c)
Triangle(a::T, b::T, c::T, ::NegativelyOriented) where {T<:AbstractPoint2D} = NegativelyOrientedTriangle{T}(a, b, c)
Triangle(a::T, b::T, c::T, ::UnOriented) where {T<:AbstractPoint2D} = UnOrientedTriangle{T}(a, b, c)
Triangle(a::T, b::T, c::T) where {T<:AbstractPoint2D} = Triangle(a, b, c, unoriented)
Triangle(ax::Real, ay::Real, bx::Real, by::Real, cx::Real, cy::Real, orientation::AbstractOrientation=unoriented) =
Triangle(Point(ax, ay), Point(bx, by), Point(cx, cy), orientation)
Primitive(ax::Real, ay::Real, bx::Real, by::Real, cx::Real, cy::Real, orientation::AbstractOrientation=unoriented) =
Triangle(Point(ax, ay), Point(bx, by), Point(cx, cy), orientation)
Primitive(a::T, b::T, c::T, orientation::AbstractOrientation=unoriented) where {T<:AbstractPoint2D} =
Triangle(a, b, c, orientation)
area(tr::TriangleTypes) = abs(tr._pr2)/2
centroid(tr::TriangleTypes) =
Point2D(
(getx(geta(tr)) + getx(getb(tr)) + getx(getc(tr))) / 3.0,
(gety(geta(tr)) + gety(getb(tr)) + gety(getc(tr))) / 3.0)
function circumcenter(tr::TriangleTypes)
d = -2.0 * tr._pr2
Point2D(
tr._px/d + getx(geta(tr)),
tr._py/d + gety(geta(tr))
)
end
function circumradius2(tr::TriangleTypes)
c = circumcenter(tr)
x = getx(c) - getx(geta(tr))
y = gety(c) - gety(geta(tr))
x*x + y*y
end
macro _define_tetrahedron_type(name, abstracttype)
oriented = !occursin("UnOriented", string(name))
esc(quote
mutable struct $name{T<:AbstractPoint3D} <: $abstracttype
_a::T; _b::T; _c::T; _d::T
_bx::Float64; _by::Float64; _bz::Float64
_cx::Float64; _cy::Float64; _cz::Float64
_dx::Float64; _dy::Float64; _dz::Float64
_px::Float64; _py::Float64; _pz::Float64
_pr2::Float64
$((oriented ? tuple() : (:(_o::Int8),))...)
function $name{T}(a::AbstractPoint3D, b::AbstractPoint3D, c::AbstractPoint3D, d::AbstractPoint3D) where T
t = new(a, b, c, d, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, $((oriented ? tuple() : (0,))...))
clean!(t)
t
end
end
end)
end
@_define_tetrahedron_type(UnOrientedTetrahedron, AbstractTetrahedronUnOriented)
@_define_tetrahedron_type(PositivelyOrientedTetrahedron, AbstractPositivelyOrientedTetrahedron)
@_define_tetrahedron_type(NegativelyOrientedTetrahedron, AbstractNegativelyOrientedTetrahedron)
Tetrahedron(a::T, b::T, c::T, d::T, ::PositivelyOriented) where {T<:AbstractPoint3D} = PositivelyOrientedTetrahedron{T}(a, b, c, d)
Tetrahedron(a::T, b::T, c::T, d::T, ::NegativelyOriented) where {T<:AbstractPoint3D} = NegativelyOrientedTetrahedron{T}(a, b, c, d)
Tetrahedron(a::T, b::T, c::T, d::T, ::UnOriented) where {T<:AbstractPoint3D} = UnOrientedTetrahedron{T}(a, b, c, d)
Tetrahedron(a::T, b::T, c::T, d::T) where {T<:AbstractPoint3D} = Tetrahedron(a, b, c, d, unoriented)
Tetrahedron(ax::Real, ay::Real, az::Real, bx::Real, by::Real, bz::Real,
cx::Real, cy::Real, cz::Real, dx::Real, dy::Real, dz::Real, orientation::AbstractOrientation=unoriented) =
Tetrahedron(Point(ax,ay,az), Point(bx,by,bz), Point(cx,cy,cz), Point(dx,dy,dz), orientation)
Primitive(ax::Real, ay::Real, az::Real, bx::Real, by::Real, bz::Real,
cx::Real, cy::Real, cz::Real, dx::Real, dy::Real, dz::Real, orientation::AbstractOrientation=unoriented) =
Tetrahedron(Point(ax,ay,az), Point(bx,by,bz), Point(cx,cy,cz), Point(dx,dy,dz), orientation)
Primitive(a::T, b::T, c::T, d::T, orientation::AbstractOrientation=unoriented) where {T<:AbstractPoint3D} =
Tetrahedron(a, b, c, d, orientation)
volume(tr::TetrahedronTypes) = abs(tr._pr2)/2
centroid(tr::TetrahedronTypes) =
Point3D(
(getx(geta(tr)) + getx(getb(tr)) + getx(getc(tr)) + getx(getd(tr))) / 4.0,
(gety(geta(tr)) + gety(getb(tr)) + gety(getc(tr)) + gety(getd(tr))) / 4.0,
(getz(geta(tr)) + getz(getb(tr)) + getz(getc(tr)) + getz(getd(tr))) / 4.0)
function circumcenter(tr::TetrahedronTypes)
d = -2.0 * tr._pr2
Point3D(
tr._px/d + getx(geta(tr)),
tr._py/d + gety(geta(tr)),
tr._pz/d + getz(geta(tr))
)
end
function circumradius2(tr::TetrahedronTypes)
c = circumcenter(tr)
x = getx(c) - getx(geta(tr))
y = gety(c) - gety(geta(tr))
z = getz(c) - getz(geta(tr))
x*x + y*y + z*z
end
# extract exact integer representation of float to be used in exact calculations when needed
_extract_int(n::Float64) = reinterpret(UInt64, n) & 0x000fffffffffffff
_extract_bigint(n::Float64) = BigInt(_extract_int(n))
# functions to re-validate cached pre-calculations in primitives
function _clean!(t::TriangleTypes)
t._bx = getx(getb(t))-getx(geta(t)); t._by = gety(getb(t))-gety(geta(t));
t._cx = getx(getc(t))-getx(geta(t)); t._cy = gety(getc(t))-gety(geta(t));
br2 = t._bx*t._bx+t._by*t._by
cr2 = t._cx*t._cx+t._cy*t._cy
t._px = br2*t._cy - t._by*cr2
t._py = -br2*t._cx + t._bx*cr2
t._pr2 = -t._bx *t._cy + t._by*t._cx
end
function _sz_clean!(t::AbstractTriangleUnOriented)
sz = abs(t._bx)+abs(t._by)+abs(t._cx)+abs(t._cy)
if t._pr2 < -_abs_err_orientation_2d*sz
t._o = 1
elseif t._pr2 > _abs_err_orientation_2d*sz
t._o = -1
else
t._o = _exact_sign_orientation_determinant!(
_extract_bigint(getx(geta(t))), _extract_bigint(gety(geta(t))),
_extract_bigint(getx(getb(t))), _extract_bigint(gety(getb(t))),
_extract_bigint(getx(getc(t))), _extract_bigint(gety(getc(t))))
end
end
function clean!(t::AbstractTriangleUnOriented)
_clean!(t)
if t._pr2 < -_abs_err_orientation_2d
t._o = 1
elseif t._pr2 > _abs_err_orientation_2d
t._o = -1
else
_sz_clean!(t)
end
end
function _clean!(t::TetrahedronTypes)
t._bx = getx(getb(t))-getx(geta(t)); t._by = gety(getb(t))-gety(geta(t)); t._bz = getz(getb(t))-getz(geta(t))
t._cx = getx(getc(t))-getx(geta(t)); t._cy = gety(getc(t))-gety(geta(t)); t._cz = getz(getc(t))-getz(geta(t))
t._dx = getx(getd(t))-getx(geta(t)); t._dy = gety(getd(t))-gety(geta(t)); t._dz = getz(getd(t))-getz(geta(t))
br2 = t._bx*t._bx+t._by*t._by+t._bz*t._bz
cr2 = t._cx*t._cx+t._cy*t._cy+t._cz*t._cz
dr2 = t._dx*t._dx+t._dy*t._dy+t._dz*t._dz
t._px = t._by*t._cz*dr2 - br2*t._cz*t._dy - t._by*cr2*t._dz - t._bz*t._cy*dr2 + br2*t._cy*t._dz + t._bz*cr2*t._dy
t._py = br2*t._cz*t._dx + t._bz*t._cx*dr2 - br2*t._cx*t._dz - t._bx*t._cz*dr2 - t._bz*cr2*t._dx + t._bx*cr2*t._dz
t._pz = br2*t._cx*t._dy + t._bx*t._cy*dr2 + t._by*cr2*t._dx - br2*t._cy*t._dx - t._bx*cr2*t._dy - t._by*t._cx*dr2
t._pr2 = -t._bx*t._cy*t._dz + t._bx*t._cz*t._dy + t._by*t._cx*t._dz - t._by*t._cz*t._dx - t._bz*t._cx*t._dy + t._bz*t._cy*t._dx
end
function _sz_clean!(t::AbstractTetrahedronUnOriented)
sz = abs(t._bx) + abs(t._by) + abs(t._bz) +
abs(t._cx) + abs(t._cy) + abs(t._cz) +
abs(t._dx) + abs(t._dy) + abs(t._dz)
# calculate the orientation
if t._pr2 < -_abs_err_orientation_3d*sz
t._o = 1
elseif t._pr2 > _abs_err_orientation_3d*sz
t._o = -1
else
# exact calculation is required
t._o = _exact_sign_orientation_determinant!(
_extract_bigint(getx(geta(t))), _extract_bigint(gety(geta(t))), _extract_bigint(getz(geta(t))),
_extract_bigint(getx(getb(t))), _extract_bigint(gety(getb(t))), _extract_bigint(getz(getb(t))),
_extract_bigint(getx(getc(t))), _extract_bigint(gety(getc(t))), _extract_bigint(getz(getc(t))),
_extract_bigint(getx(getd(t))), _extract_bigint(gety(getd(t))), _extract_bigint(getz(getd(t))))
end
end
function clean!(t::AbstractTetrahedronUnOriented)
_clean!(t)
# calculate the orientation
if t._pr2 < -_abs_err_orientation_3d
t._o = 1
elseif t._pr2 > _abs_err_orientation_3d
t._o = -1
else
_sz_clean!(t)
end
end
clean!(t::AbstractOrientedPrimitive) = _clean!(t)
# getting points of the primitive
geta(t::AbstractPrimitive) = t._a
getb(t::AbstractPrimitive) = t._b
getc(t::AbstractPrimitive) = t._c
getd(t::TetrahedronTypes) = t._d
# changing points in the primitive
seta(t::AbstractPrimitive, p::AbstractPoint) = (t._a=p; clean!(t))
setb(t::AbstractPrimitive, p::AbstractPoint) = (t._b=p; clean!(t))
setc(t::AbstractPrimitive, p::AbstractPoint) = (t._c=p; clean!(t))
setd(t::TetrahedronTypes, p::AbstractPoint3D) = (t._d=p; clean!(t))
setabc(t::AbstractPrimitive, pa::AbstractPoint, pb::AbstractPoint, pc::AbstractPoint) = (t._a=pa; t._b=pb; t._c=pc; clean!(t))
setab(t::AbstractPrimitive, pa::AbstractPoint, pb::AbstractPoint) = (t._a=pa; t._b=pb; clean!(t))
setbc(t::AbstractPrimitive, pb::AbstractPoint, pc::AbstractPoint) = (t._b=pb; t._c=pc; clean!(t))
setac(t::AbstractPrimitive, pa::AbstractPoint, pc::AbstractPoint) = (t._a=pa; t._c=pc; clean!(t))
setabcd(t::TetrahedronTypes, pa::AbstractPoint3D, pb::AbstractPoint3D, pc::AbstractPoint3D, pd::AbstractPoint3D) = (t._a=pa; t._b=pb; t._c=pc; t._d=pd; clean!(t))
setabc(t::TetrahedronTypes, pa::AbstractPoint3D, pb::AbstractPoint3D, pc::AbstractPoint3D) = (t._a=pa; t._b=pb; t._c=pc; clean!(t))
setabd(t::TetrahedronTypes, pa::AbstractPoint3D, pb::AbstractPoint3D, pd::AbstractPoint3D) = (t._a=pa; t._b=pb; t._d=pd; clean!(t))
setacd(t::TetrahedronTypes, pa::AbstractPoint3D, pc::AbstractPoint3D, pd::AbstractPoint3D) = (t._a=pa; t._c=pc; t._d=pd; clean!(t))
setbcd(t::TetrahedronTypes, pb::AbstractPoint3D, pc::AbstractPoint3D, pd::AbstractPoint3D) = (t._b=pb; t._c=pc; t._d=pd; clean!(t))
setad(t::TetrahedronTypes, pa::AbstractPoint3D, pd::AbstractPoint3D) = (t._a=pa; t._d=pd; clean!(t))
setbd(t::TetrahedronTypes, pb::AbstractPoint3D, pd::AbstractPoint3D) = (t._b=pb; t._d=pd; clean!(t))
setcd(t::TetrahedronTypes, pc::AbstractPoint3D, pd::AbstractPoint3D) = (t._c=pc; t._d=pd; clean!(t))
orientation(p::AbstractUnOrientedPrimitive) = p._o
orientation(p::AbstractPositivelyOrientedPrimitive) = 1
orientation(p::AbstractNegativelyOrientedPrimitive) = -1
orientation(ax::Real, ay::Real, bx::Real, by::Real, cx::Real, cy::Real) =
orientation(Triangle(ax, ay, bx, by, cx, cy))
orientation(ax::Real, ay::Real, az::Real, bx::Real, by::Real, bz::Real, cx::Real, cy::Real, cz::Real, dx::Real, dy::Real, dz::Real) =
orientation(Tetrahedron(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz))
# exact orientation for triangle
function _exact_sign_orientation_determinant!(ax::BigInt, ay::BigInt, bx::BigInt, by::BigInt, cx::BigInt, cy::BigInt)
bx -= ax; by -= ay
cx -= ax; cy -= ay
Int(sign(bx*cy - by*cx))
end
# exact orientation for tetrahedron
function _exact_sign_orientation_determinant!(ax::BigInt, ay::BigInt, az::BigInt, bx::BigInt, by::BigInt, bz::BigInt, cx::BigInt, cy::BigInt, cz::BigInt, dx::BigInt, dy::BigInt, dz::BigInt)
bx -= ax; by -= ay; bz -= az
cx -= ax; cy -= ay; cz -= az
dx -= ax; dy -= ay; dz -= az
Int(sign(+bx*cy*dz - bx*cz*dy - by*cx*dz + by*cz*dx + bz*cx*dy - bz*cy*dx))
end
# exact incircle for triangle
function _exact_sign_incircle_determinant!(ax::BigInt, ay::BigInt, bx::BigInt, by::BigInt, cx::BigInt, cy::BigInt, px::BigInt, py::BigInt)
bx -= ax; by -= ay;
cx -= ax; cy -= ay;
px -= ax; py -= ay;
br2 = bx*bx+by*by
cr2 = cx*cx+cy*cy
pr2 = px*px+py*py
Int(sign(-br2*cx*py + br2*cy*px + bx*cr2*py - bx*cy*pr2 - by*cr2*px + by*cx*pr2))
end
# exact incircle for tetrahedron
function _exact_sign_incircle_determinant!(ax::BigInt, ay::BigInt, az::BigInt, bx::BigInt, by::BigInt, bz::BigInt, cx::BigInt, cy::BigInt, cz::BigInt, dx::BigInt, dy::BigInt, dz::BigInt, px::BigInt, py::BigInt, pz::BigInt)
bx -= ax; by -= ay; bz -= az
cx -= ax; cy -= ay; cz -= az
dx -= ax; dy -= ay; dz -= az
px -= ax; py -= ay; pz -= az
br2 = bx*bx+by*by+bz*bz
cr2 = cx*cx+cy*cy+cz*cz
dr2 = dx*dx+dy*dy+dz*dz
pr2 = px*px+py*py+pz*pz
Int(sign(
+br2*cx*dy*pz - br2*cx*dz*py - br2*cy*dx*pz + br2*cy*dz*px +
br2*cz*dx*py - br2*cz*dy*px - bx*cr2*dy*pz + bx*cr2*dz*py +
bx*cy*dr2*pz - bx*cy*dz*pr2 - bx*cz*dr2*py + bx*cz*dy*pr2 +
by*cr2*dx*pz - by*cr2*dz*px - by*cx*dr2*pz + by*cx*dz*pr2 +
by*cz*dr2*px - by*cz*dx*pr2 - bz*cr2*dx*py + bz*cr2*dy*px +
bz*cx*dr2*py - bz*cx*dy*pr2 - bz*cy*dr2*px + bz*cy*dx*pr2))
end
# finer filtered incircle
function _sz_incircle(t::TriangleTypes, p::AbstractPoint2D, px::Float64, py::Float64, pr2::Float64)
if orientation(t) != 0
sz = abs(px)+abs(py)+abs(t._bx)+abs(t._by)+abs(t._cx)+abs(t._cy)
d = t._px*px + t._py*py + t._pr2*pr2
if d < -_abs_err_incircle_2d*sz
return -orientation(t)
elseif d > _abs_err_incircle_2d*sz
return orientation(t)
end
end
exact_in = _exact_sign_incircle_determinant!(
_extract_bigint(getx(geta(t))), _extract_bigint(gety(geta(t))),
_extract_bigint(getx(getb(t))), _extract_bigint(gety(getb(t))),
_extract_bigint(getx(getc(t))), _extract_bigint(gety(getc(t))),
_extract_bigint(getx(p)) , _extract_bigint(gety(p)))
if orientation(t) != 0
return orientation(t)*exact_in
elseif exact_in == 0
return 1
else
return 2
end
end
# gross filtered incircle, asumming maximal triangle size (=1.0)
function incircle(t::TriangleTypes, p::AbstractPoint2D)
px = getx(p) - getx(geta(t))
py = gety(p) - gety(geta(t))
pr2 = px*px + py*py
if orientation(t) != 0
d = t._px*px + t._py*py + t._pr2*pr2
if d < -_abs_err_incircle_2d
return -orientation(t)
elseif d > _abs_err_incircle_2d
return orientation(t)
end
end
_sz_incircle(t, p, px, py, pr2)
end
# finer filtered incircle
function _sz_incircle(t::TetrahedronTypes, p::AbstractPoint3D)
if orientation(t) != 0
px = getx(p) - getx(geta(t))
py = gety(p) - gety(geta(t))
pz = getz(p) - getz(geta(t))
sz = abs(px) + abs(py) + abs(pz) +
abs(t._bx) + abs(t._by) + abs(t._bz) +
abs(t._cx) + abs(t._cy) + abs(t._cz) +
abs(t._dx) + abs(t._dy) + abs(t._dz)
pr2 = px*px + py*py + pz*pz
d = t._px*px + t._py*py + t._pz*pz + t._pr2*pr2
if d < -_abs_err_incircle_3d*sz
return -orientation(t)
elseif d > _abs_err_incircle_3d*sz
return orientation(t)
end
end
exact_in = _exact_sign_incircle_determinant!(
_extract_bigint(getx(geta(t))), _extract_bigint(gety(geta(t))), _extract_bigint(getz(geta(t))),
_extract_bigint(getx(getb(t))), _extract_bigint(gety(getb(t))), _extract_bigint(getz(getb(t))),
_extract_bigint(getx(getc(t))), _extract_bigint(gety(getc(t))), _extract_bigint(getz(getc(t))),
_extract_bigint(getx(getd(t))), _extract_bigint(gety(getd(t))), _extract_bigint(getz(getd(t))),
_extract_bigint(getx(p)) , _extract_bigint(gety(p)) , _extract_bigint(getz(p)))
if orientation(t) != 0
return orientation(t)*exact_in
elseif exact_in == 0
return 1
else
return 2
end
end
# gross filtered incircle, asumming maximal possible tetra size (=1.0)
function incircle(t::TetrahedronTypes, p::AbstractPoint3D)
if orientation(t) != 0
px = getx(p) - getx(geta(t))
py = gety(p) - gety(geta(t))
pz = getz(p) - getz(geta(t))
pr2 = px*px + py*py + pz*pz
d = t._px*px + t._py*py + t._pz*pz + t._pr2*pr2
if d < -_abs_err_incircle_3d
return -orientation(t)
elseif d > _abs_err_incircle_3d
return orientation(t)
end
end
_sz_incircle(t, p)
end
# helper methods to use incircle directly with coordinates
incircle(ax::Real, ay::Real, bx::Real, by::Real, cx::Real, cy::Real, dx::Real, dy::Real) =
incircle(Triangle(ax, ay, bx, by, cx, cy), Point(dx, dy))
incircle(ax::Real, ay::Real, az::Real, bx::Real, by::Real, bz::Real, cx::Real, cy::Real, cz::Real, dx::Real, dy::Real, dz::Real, ex::Real, ey::Real, ez::Real) =
incircle(Tetrahedron(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz), Point(ex, ey, ez))
# exact intriangle (slow!)
function _exact_intriangle!(ax::BigInt, ay::BigInt, bx::BigInt, by::BigInt, cx::BigInt, cy::BigInt, px::BigInt, py::BigInt)
bx -= ax; by -= ay;
cx -= ax; cy -= ay;
px -= ax; py -= ay;
nb = -cx*py + cy*px
nc = bx*py - by*px
denom = bx*cy - by*cx
sdenom = Int(sign(denom))
if Int(sign(nb)) * sdenom < 0
return -2
end
if Int(sign(nc)) * sdenom < 0
return -3
end
l = nb+nc - denom
sl = Int(sign(l)) * sdenom
if sl > 0
return -1
end
if Int(sign(nb)) == 0
return 3
end
if Int(sign(nc)) == 0
return 4
end
if sl == 0
return 2
end
-sl
end
function _exact_intriangle!(ax::BigInt, ay::BigInt, az::BigInt, bx::BigInt, by::BigInt, bz::BigInt, cx::BigInt, cy::BigInt, cz::BigInt, dx::BigInt, dy::BigInt, dz::BigInt, px::BigInt, py::BigInt, pz::BigInt)
bx -= ax; by -= ay; bz -= az
cx -= ax; cy -= ay; cz -= az
dx -= ax; dy -= ay; dz -= az
px -= ax; py -= ay; pz -= az
denom = bx*cy*dz-bx*cz*dy-by*cx*dz+by*cz*dx+bz*cx*dy-bz*cy*dx
nb = cx*dy*pz-cx*dz*py-cy*dx*pz+cy*dz*px+cz*dx*py-cz*dy*px
sdenom = Int(sign(denom))
if Int(sign(nb)) * sdenom < 0
return -2
end
nc = -bx*dy*pz+bx*dz*py+by*dx*pz-by*dz*px-bz*dx*py+bz*dy*px
if Int(sign(nc)) * sdenom < 0
return -3
end
nd = bx*cy*pz-bx*cz*py-by*cx*pz+by*cz*px+bz*cx*py-bz*cy*px
if Int(sign(nd)) * sdenom < 0
return -4
end
l = (nb+nc+nd - denom) * sdenom
sl = Int(sign(l))
if sl > 0
return -1
end
if Int(sign(nb)) == 0
return 3
end
if Int(sign(nc)) == 0
return 4
end
if Int(sign(nd)) == 0
return 5
end
if sl == 0
return 2
end
-sl
end
# helper methods to use the exact intriangle directly with coordinates
_exact_intriangle(t::TriangleTypes, p::AbstractPoint2D) =
_exact_intriangle!(
_extract_bigint(getx(geta(t))), _extract_bigint(gety(geta(t))),
_extract_bigint(getx(getb(t))), _extract_bigint(gety(getb(t))),
_extract_bigint(getx(getc(t))), _extract_bigint(gety(getc(t))),
_extract_bigint(getx(p)) , _extract_bigint(gety(p)))
_exact_intriangle(t::TetrahedronTypes, p::AbstractPoint3D) =
_exact_intriangle!(
_extract_bigint(getx(geta(t))), _extract_bigint(gety(geta(t))), _extract_bigint(getz(geta(t))),
_extract_bigint(getx(getb(t))), _extract_bigint(gety(getb(t))), _extract_bigint(getz(getb(t))),
_extract_bigint(getx(getc(t))), _extract_bigint(gety(getc(t))), _extract_bigint(getz(getc(t))),
_extract_bigint(getx(getd(t))), _extract_bigint(gety(getd(t))), _extract_bigint(getz(getd(t))),
_extract_bigint(getx(p)), _extract_bigint(gety(p)), _extract_bigint(getz(p)))
# finer filter for intriangle, using triangle actual size
function _sz_intriangle(t::TriangleTypes, p::AbstractPoint2D, px::Float64, py::Float64)
sz = abs(px)+abs(py)+abs(t._bx)+abs(t._by)+abs(t._cx)+abs(t._cy)
nb = (-t._cx*py + t._cy*px) * sign(-t._pr2)
if nb < -_abs_err_intriangle_zero*sz
return -2
elseif nb < _abs_err_intriangle_zero*sz
# we need an exact calculation
return _exact_intriangle(t, p)
end
nc = (t._bx*py - t._by*px) * sign(-t._pr2)
if nc < -_abs_err_intriangle_zero*sz
return -3
elseif nc < _abs_err_intriangle_zero*sz
# we need an exact calculation
return _exact_intriangle(t, p)
end
l = nb+nc + t._pr2*sign(-t._pr2)
if l < -_abs_err_intriangle*sz
return 1
elseif l > _abs_err_intriangle*sz
return -1
end
# we need an exact calculation
_exact_intriangle(t, p)
end
# gross filter, assuming maximal possible triangle size (=1.0)
function intriangle(t::TriangleTypes, p::AbstractPoint2D)
px = getx(p) - getx(geta(t)); py = gety(p) - gety(geta(t))
nb = (-t._cx*py + t._cy*px) * sign(-t._pr2)
if nb < -_abs_err_intriangle_zero
return -2
elseif nb < _abs_err_intriangle_zero
return _sz_intriangle(t, p, px, py)
end
nc = (t._bx*py - t._by*px) * sign(-t._pr2)
if nc < -_abs_err_intriangle_zero
return -3
elseif nc < _abs_err_intriangle_zero
return _sz_intriangle(t, p, px, py)
end
l = nb+nc + t._pr2*sign(-t._pr2)
if l < -_abs_err_intriangle
return 1
elseif l > _abs_err_intriangle
return -1
end
_sz_intriangle(t, p, px, py)
end
# fine filtered intriangle
function _sz_intriangle(t::TetrahedronTypes, p::AbstractPoint3D)
px = getx(p) - getx(geta(t)); py = gety(p) - gety(geta(t)); pz = getz(p) - getz(geta(t))
sz = abs(px) + abs(py) + abs(pz) +
abs(t._bx) + abs(t._by) + abs(t._bz) +
abs(t._cx) + abs(t._cy) + abs(t._cz) +
abs(t._dx) + abs(t._dy) + abs(t._dz)
nb = (t._cx*t._dy*pz-t._cx*t._dz*py-t._cy*t._dx*pz+t._cy*t._dz*px+t._cz*t._dx*py-t._cz*t._dy*px) * sign(-t._pr2)
if nb < -_abs_err_intetra_zero*sz
return -2
elseif nb < _abs_err_intetra_zero*sz
# we need an exact calculation
return _exact_intriangle(t, p)
end
nc = (-t._bx*t._dy*pz+t._bx*t._dz*py+t._by*t._dx*pz-t._by*t._dz*px-t._bz*t._dx*py+t._bz*t._dy*px) * sign(-t._pr2)
if nc < -_abs_err_intetra_zero*sz
return -3
elseif nc < _abs_err_intetra_zero*sz
# we need an exact calculation
return _exact_intriangle(t, p)
end
nd = (t._bx*t._cy*pz-t._bx*t._cz*py-t._by*t._cx*pz+t._by*t._cz*px+t._bz*t._cx*py-t._bz*t._cy*px) * sign(-t._pr2)
if nd < -_abs_err_intetra_zero*sz
return -4
elseif nd < _abs_err_intetra_zero*sz
# we need an exact calculation
return _exact_intriangle(t, p)
end
l = nb+nc+nd + t._pr2*sign(-t._pr2)
if l < -_abs_err_intetra*sz
return 1
elseif l > _abs_err_intetra*sz
return -1
end
# we need an exact calculation
_exact_intriangle(t, p)
end
# gross filtered intriangle
function intriangle(t::TetrahedronTypes, p::AbstractPoint3D)
px = getx(p) - getx(geta(t)); py = gety(p) - gety(geta(t)); pz = getz(p) - getz(geta(t))
nb = (t._cx*t._dy*pz-t._cx*t._dz*py-t._cy*t._dx*pz+t._cy*t._dz*px+t._cz*t._dx*py-t._cz*t._dy*px) * sign(-t._pr2)
if nb < -_abs_err_intetra_zero
return -2
elseif nb < _abs_err_intetra_zero
return _sz_intriangle(t, p)
end
nc = (-t._bx*t._dy*pz+t._bx*t._dz*py+t._by*t._dx*pz-t._by*t._dz*px-t._bz*t._dx*py+t._bz*t._dy*px) * sign(-t._pr2)
if nc < -_abs_err_intetra_zero
return -3
elseif nc < _abs_err_intetra_zero
return _sz_intriangle(t, p)
end
nd = (t._bx*t._cy*pz-t._bx*t._cz*py-t._by*t._cx*pz+t._by*t._cz*px+t._bz*t._cx*py-t._bz*t._cy*px) * sign(-t._pr2)
if nd < -_abs_err_intetra_zero
return -4
elseif nd < _abs_err_intetra_zero
return _sz_intriangle(t, p)
end
l = nb+nc+nd + t._pr2*sign(-t._pr2)
if l < -_abs_err_intetra
return 1
elseif l > _abs_err_intetra
return -1
end
_sz_intriangle(t, p)
end
# helper methods to use the filtered (fast, exact) intriangle directly with raw coordinates
intriangle(ax::Real, ay::Real, bx::Real, by::Real, cx::Real, cy::Real, px::Real, py::Real) =
intriangle(Triangle(ax, ay, bx, by, cx, cy), Point(px, py))
intriangle(ax::Real, ay::Real, az::Real, bx::Real, by::Real, bz::Real, cx::Real, cy::Real, cz::Real, dx::Real, dy::Real, dz::Real, px::Real, py::Real, pz::Real) =
intriangle(Tetrahedron(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz), Point(px, py, pz))
###################################################################################
#
# Hilbert stuff
#
#
# number of bits to use per dimension in calculating the peano-key
const peano_2D_bits = 31
const peano_3D_bits = 21
# implementing 2D scale dependent Peano-Hilbert indexing
_extract_peano_bin_num(nbins::Int64, n::Float64) = trunc(Integer, (n-1)*nbins )
# calculate peano key for given point
function peanokey(p::AbstractPoint2D, bits::Int64=peano_2D_bits)
n = 1 << bits
s = n >> 1; d = 0
x = _extract_peano_bin_num(n, getx(p))
y = _extract_peano_bin_num(n, gety(p))
while true
rx = (x & s) > 0
ry = (y & s) > 0
d += s * s * xor(3 * rx, ry)
s = s >> 1
(s == 0) && break
if ry == 0
if rx == 1
x = n - 1 - x;
y = n - 1 - y;
end
x, y = y, x
end
end
d
end
# Inverse calculation. I.e. calculate the point that given given peano key
function Point2D(peanokey::Int64, bits::Int64=peano_2D_bits)
n = 1 << bits
x = 0; y = 0; s=1
while true
rx = 1 & (peanokey >> 1)
ry = 1 & xor(peanokey, rx)
if ry == 0
if rx == 1
x = s - 1 - x;
y = s - 1 - y;
end
x, y = y, x
end
x += s * rx
y += s * ry
s = s << 1
(s >= n) && break
peanokey = peanokey >> 2
end
Point2D(1+x/n, 1+y/n)
end
# implementing 3D scaleful Peano-Hilbert indexing
const quadrants_arr = [
0, 7, 1, 6, 3, 4, 2, 5,
7, 4, 6, 5, 0, 3, 1, 2,
4, 3, 5, 2, 7, 0, 6, 1,
3, 0, 2, 1, 4, 7, 5, 6,
1, 0, 6, 7, 2, 3, 5, 4,
0, 3, 7, 4, 1, 2, 6, 5,
3, 2, 4, 5, 0, 1, 7, 6,
2, 1, 5, 6, 3, 0, 4, 7,
6, 1, 7, 0, 5, 2, 4, 3,
1, 2, 0, 3, 6, 5, 7, 4,
2, 5, 3, 4, 1, 6, 0, 7,
5, 6, 4, 7, 2, 1, 3, 0,
7, 6, 0, 1, 4, 5, 3, 2,
6, 5, 1, 2, 7, 4, 0, 3,
5, 4, 2, 3, 6, 7, 1, 0,
4, 7, 3, 0, 5, 6, 2, 1,
6, 7, 5, 4, 1, 0, 2, 3,
7, 0, 4, 3, 6, 1, 5, 2,
0, 1, 3, 2, 7, 6, 4, 5,
1, 6, 2, 5, 0, 7, 3, 4,
2, 3, 1, 0, 5, 4, 6, 7,
3, 4, 0, 7, 2, 5, 1, 6,
4, 5, 7, 6, 3, 2, 0, 1,
5, 2, 6, 1, 4, 3, 7, 0]
quadrants(a::Int, b::Int, c::Int, d::Int) = (@inbounds x = quadrants_arr[1+a<<3+b<<2+c<<1+d]; x)
rotxmap_table = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 17, 18, 19, 16, 23, 20, 21, 22]
rotymap_table = [1, 2, 3, 0, 16, 17, 18, 19, 11, 8, 9, 10, 22, 23, 20, 21, 14, 15, 12, 13, 4, 5, 6, 7]
rotx_table = [3, 0, 0, 2, 2, 0, 0, 1]
roty_table = [0, 1, 1, 2, 2, 3, 3, 0]
sense_table = [-1, -1, -1, +1, +1, -1, -1, -1]
function peanokey(p::AbstractPoint3D, bits::Int64=peano_3D_bits)
n = 1 << bits
x = _extract_peano_bin_num(n, getx(p))
y = _extract_peano_bin_num(n, gety(p))
z = _extract_peano_bin_num(n, getz(p))
mask = 1 << (bits - 1)
key = 0
rotation = 0
sense = 1
for i in 1:bits
bitx = (x & mask > 0) ? 1 : 0
bity = (y & mask > 0) ? 1 : 0
bitz = (z & mask > 0) ? 1 : 0
quad = quadrants(rotation, bitx, bity, bitz)
key <<= 3
key += sense == 1 ? quad : 7-quad
@inbounds rotx = rotx_table[quad+1]
@inbounds roty = roty_table[quad+1]
@inbounds sense *= sense_table[quad+1]
while rotx > 0
@inbounds rotation = rotxmap_table[rotation+1]
rotx -= 1
end
while(roty > 0)
@inbounds rotation = rotymap_table[rotation+1]
roty -= 1
end
mask >>= 1
end
key
end
const quadrants_inverse_x = Array{Int64}(undef,24,8)
const quadrants_inverse_y = Array{Int64}(undef,24,8)
const quadrants_inverse_z = Array{Int64}(undef,24,8)
function _init_inv_peano_3d()
for rotation in 0:23
for bitx in 0:1
for bity in 0:1
for bitz in 0:1
quad = quadrants(rotation, bitx, bity, bitz)
quadrants_inverse_x[rotation+1, quad+1] = bitx;
quadrants_inverse_y[rotation+1, quad+1] = bity;