1. Matthew Turk
  2. yt-3.0

Source

yt-3.0 / yt / geometry / selection_routines.pyx

  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
""" 
Geometry selection routines.

Author: Matthew Turk <matthewturk@gmail.com>
Affiliation: Columbia University
Homepage: http://yt.enzotools.org/
License:
  Copyright (C) 2011 Matthew Turk.  All Rights Reserved.

  This file is part of yt.

  yt is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import numpy as np
cimport numpy as np
cimport cython
from stdlib cimport malloc, free
from fp_utils cimport fclip, iclip
from cython.parallel import prange, parallel, threadid
from selection_routines cimport SelectorObject
from oct_container cimport OctreeContainer, OctAllocationContainer, Oct
#from geometry_utils cimport point_to_hilbert
from yt.utilities.lib.grid_traversal cimport \
    VolumeContainer, sample_function, walk_volume

cdef extern from "math.h":
    double exp(double x) nogil
    float expf(float x) nogil
    long double expl(long double x) nogil
    double floor(double x) nogil
    double ceil(double x) nogil
    double fmod(double x, double y) nogil
    double log2(double x) nogil
    long int lrint(double x) nogil
    double fabs(double x) nogil

# These routines are separated into a couple different categories:
#
#   * Routines for identifying intersections of an object with a bounding box
#   * Routines for identifying cells/points inside a bounding box that
#     intersect with an object
#   * Routines that speed up some type of geometric calculation

# First, bounding box / object intersection routines.
# These all respect the interface "dobj" and a set of left_edges, right_edges,
# sometimes also accepting level and mask information.

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def convert_mask_to_indices(np.ndarray[np.uint8_t, ndim=3, cast=True] mask,
            int count, int transpose = 0):
    cdef int i, j, k, cpos
    cdef np.ndarray[np.int32_t, ndim=2] indices 
    indices = np.zeros((count, 3), dtype='int32')
    cpos = 0
    for i in range(mask.shape[0]):
        for j in range(mask.shape[1]):
            for k in range(mask.shape[2]):
                if mask[i,j,k] == 1:
                    if transpose == 1:
                        indices[cpos, 0] = k
                        indices[cpos, 1] = j
                        indices[cpos, 2] = i
                    else:
                        indices[cpos, 0] = i
                        indices[cpos, 1] = j
                        indices[cpos, 2] = k
                    cpos += 1
    return indices

# Inclined Box

cdef class SelectorObject:

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def select_grids(self,
                     np.ndarray[np.float64_t, ndim=2] left_edges,
                     np.ndarray[np.float64_t, ndim=2] right_edges):
        cdef int i, n
        cdef int ng = left_edges.shape[0]
        cdef np.ndarray[np.uint8_t, ndim=1] gridi = np.zeros(ng, dtype='uint8')
        cdef np.float64_t LE[3], RE[3]
        with nogil:
            for n in range(ng):
                # Call our selector function
                # Check if the sphere is inside the grid
                for i in range(3):
                    LE[i] = left_edges[n, i]
                    RE[i] = right_edges[n, i]
                gridi[n] = self.select_grid(LE, RE)
        return gridi.astype("bool")

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def select_octs(self, OctreeContainer octree):
        cdef int i, j, k, n
        cdef np.ndarray[np.uint8_t, ndim=2] mask = np.zeros((octree.nocts, 8), dtype='uint8')
        cdef np.float64_t pos[3], dds[3]
        # This dds is the oct-width
        for i in range(3):
            dds[i] = (octree.DRE[i] - octree.DLE[i]) / octree.nn[i]
        # Pos is the center of the octs
        pos[0] = octree.DLE[0] + dds[0]/2.0
        for i in range(octree.nn[0]):
            pos[1] = octree.DLE[1] + dds[1]/2.0
            for j in range(octree.nn[1]):
                pos[2] = octree.DLE[2] + dds[2]/2.0
                for k in range(octree.nn[2]):
                    if octree.root_mesh[i][j][k] == NULL: continue
                    self.recursively_select_octs(
                        octree.root_mesh[i][j][k],
                        pos, dds, mask)
                    pos[2] += dds[2]
                pos[1] += dds[2]
            pos[0] += dds[2]
        return mask.astype("bool")

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef void recursively_select_octs(self, Oct *root,
                        np.float64_t pos[3], np.float64_t dds[3],
                        np.ndarray[np.uint8_t, ndim=2] mask,
                        int level = 0):
        cdef np.float64_t LE[3], RE[3], sdds[3], spos[3]
        cdef int i, j, k, res, ii
        cdef Oct *ch
        # Remember that pos is the *center* of the oct, and dds is the oct
        # width.  So to get to the edges, we add/subtract half of dds.
        for i in range(3):
            # sdds is the cell width
            sdds[i] = dds[i]/2.0
            LE[i] = pos[i] - dds[i]/2.0
            RE[i] = pos[i] + dds[i]/2.0
        #print LE[0], RE[0], LE[1], RE[1], LE[2], RE[2]
        res = self.select_grid(LE, RE)
        cdef int eterm[3] 
        eterm[0] = eterm[1] = eterm[2] = 0
        if res == 0:
            for i in range(8):
                mask[root.local_ind,i] = 0
            return
        # Now we visit all our children.  We subtract off sdds for the first
        # pass because we center it on the first cell.
        spos[0] = pos[0] - sdds[0]/2.0
        for i in range(2):
            spos[1] = pos[1] - sdds[1]/2.0
            for j in range(2):
                spos[2] = pos[2] - sdds[2]/2.0
                for k in range(2):
                    ii = ((k*2)+j)*2+i
                    ch = root.children[i][j][k]
                    if ch != NULL:
                        mask[root.local_ind, ii] = 0
                        self.recursively_select_octs(
                            ch, spos, sdds, mask, level + 1)
                    else:
                        mask[root.local_ind, ii] = \
                            self.select_cell(spos, sdds, eterm)
                    spos[2] += sdds[2]
                spos[1] += sdds[1]
            spos[0] += sdds[0]

    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        return 0
    
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        return 0

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def count_cells(self, gobj):
        cdef np.ndarray[np.float64_t, ndim=1] odds = gobj.dds
        cdef np.ndarray[np.float64_t, ndim=1] left_edge = gobj.LeftEdge
        cdef np.ndarray[np.float64_t, ndim=1] right_edge = gobj.RightEdge
        cdef np.ndarray[np.uint8_t, ndim=3, cast=True] child_mask
        cdef np.float64_t dds[3], pos[3]
        cdef int i, j, k, ind[3][2]
        child_mask = gobj.child_mask
        for i in range(3):
            ind[i][0] = 0
            ind[i][1] = gobj.ActiveDimensions[i]
            dds[i] = odds[i]
        cdef int count = 0
        cdef int check = 1
        cdef int eterm[3]
        self.set_bounds(<np.float64_t *> left_edge.data,
                        <np.float64_t *> right_edge.data,
                        dds, ind, &check)
        with nogil:
            if check == 1:
                pos[0] = left_edge[0] + dds[0] * 0.5
                for i in range(ind[0][0], ind[0][1]):
                    eterm[0] = 0
                    pos[1] = left_edge[1] + dds[1] * 0.5
                    for j in range(ind[1][0], ind[1][1]):
                        eterm[1] = 0
                        pos[2] = left_edge[2] + dds[2] * 0.5
                        for k in range(ind[2][0], ind[2][1]):
                            eterm[2] = 0
                            if child_mask[i,j,k] == 1:
                                count += self.select_cell(pos, dds, eterm)
                            if eterm[2] == 1: break
                            pos[2] += dds[1]
                        if eterm[1] == 1: break
                        pos[1] += dds[1]
                    if eterm[0] == 1: break
                    pos[0] += dds[0]
            else:
                for i in range(ind[0][0], ind[0][1]):
                    for j in range(ind[1][0], ind[1][1]):
                        for k in range(ind[2][0], ind[2][1]):
                            count += child_mask[i,j,k]
        return count

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def fill_mask(self, gobj):
        cdef np.ndarray[np.uint8_t, ndim=3, cast=True] child_mask
        child_mask = gobj.child_mask
        cdef np.ndarray[np.uint8_t, ndim=3] mask 
        cdef int ind[3][2]
        cdef np.ndarray[np.float64_t, ndim=1] odds = gobj.dds
        cdef np.ndarray[np.float64_t, ndim=1] left_edge = gobj.LeftEdge
        cdef np.ndarray[np.float64_t, ndim=1] right_edge = gobj.RightEdge
        cdef int i, j, k
        cdef np.float64_t dds[3], pos[3]
        for i in range(3):
            dds[i] = odds[i]
            ind[i][0] = 0
            ind[i][1] = gobj.ActiveDimensions[i]
        mask = np.zeros(gobj.ActiveDimensions, dtype='uint8')
        cdef int check = 1
        cdef int total = 0
        cdef int eterm[3]
        self.set_bounds(<np.float64_t *> left_edge.data,
                        <np.float64_t *> right_edge.data,
                        dds, ind, &check)
        cdef int temp
        with nogil:
            if check == 1:
                pos[0] = left_edge[0] + dds[0] * 0.5
                for i in range(ind[0][0], ind[0][1]):
                    eterm[0] = 0
                    pos[1] = left_edge[1] + dds[1] * 0.5
                    for j in range(ind[1][0], ind[1][1]):
                        eterm[1] = 0
                        pos[2] = left_edge[2] + dds[2] * 0.5
                        for k in range(ind[2][0], ind[2][1]):
                            eterm[2] = 0
                            if child_mask[i,j,k] == 1:
                                mask[i,j,k] = self.select_cell(pos, dds, eterm)
                                total += mask[i,j,k]
                            if eterm[2] == 1: break
                            pos[2] += dds[1]
                        if eterm[1] == 1: break
                        pos[1] += dds[1]
                    if eterm[0] == 1: break
                    pos[0] += dds[0]
            else:
                for i in range(ind[0][0], ind[0][1]):
                    for j in range(ind[1][0], ind[1][1]):
                        for k in range(ind[2][0], ind[2][1]):
                            mask[i,j,k] = child_mask[i,j,k]
                            total += mask[i,j,k]
        if total == 0: return None
        return mask.astype("bool")

    cdef void set_bounds(self,
                         np.float64_t left_edge[3], np.float64_t right_edge[3],
                         np.float64_t dds[3], int ind[3][2], int *check):
        return

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def count_points(self, np.ndarray[np.float64_t, ndim=1] x,
                           np.ndarray[np.float64_t, ndim=1] y,
                           np.ndarray[np.float64_t, ndim=1] z,
                           np.float64_t radius = 0.0):
        cdef int count = 0
        cdef int i
        cdef np.float64_t dds[3], pos[3]
        dds[0] = dds[1] = dds[2] = radius
        cdef int eterm[3]
        with nogil:
            for i in range(x.shape[0]):
                pos[0] = x[i]
                pos[1] = y[i]
                pos[2] = z[i]
                count += self.select_cell(pos, dds, eterm)
        return count

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def select_points(self, np.ndarray[np.float64_t, ndim=1] x,
                            np.ndarray[np.float64_t, ndim=1] y,
                            np.ndarray[np.float64_t, ndim=1] z,
                            np.float64_t radius = 0.0):
        cdef int count = 0
        cdef int i
        cdef np.float64_t dds[3], pos[3]
        dds[0] = dds[1] = dds[2] = radius
        cdef int eterm[3]
        cdef np.ndarray[np.uint8_t, ndim=1] mask 
        mask = np.zeros(x.shape[0], dtype='uint8')
        with nogil:
            for i in range(x.shape[0]):
                pos[0] = x[i]
                pos[1] = y[i]
                pos[2] = z[i]
                mask[i] = self.select_cell(pos, dds, eterm)
                count += mask[i]
        if count == 0: return None
        return mask.astype("bool")

cdef class SphereSelector(SelectorObject):
    cdef np.float64_t radius2
    cdef np.float64_t center[3]

    def __init__(self, dobj):
        for i in range(3):
            self.center[i] = dobj.center[i]
        self.radius2 = dobj.radius * dobj.radius

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        cdef np.float64_t box_center, relcenter, closest, dist, edge
        return 1
        cdef int id
        if (left_edge[0] <= self.center[0] <= right_edge[0] and
            left_edge[1] <= self.center[1] <= right_edge[1] and
            left_edge[2] <= self.center[2] <= right_edge[2]):
            return 1
        # http://www.gamedev.net/topic/335465-is-this-the-simplest-sphere-aabb-collision-test/
        dist = 0
        for i in range(3):
            box_center = (right_edge[i] + left_edge[i])/2.0
            relcenter = self.center[i] - box_center
            edge = right_edge[i] - left_edge[i]
            closest = relcenter - fclip(relcenter, -edge/2.0, edge/2.0)
            dist += closest * closest
        if dist < self.radius2: return 1
        return 0

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        cdef np.float64_t dist2, temp
        cdef int i
        dist2 = 0
        for i in range(3):
            temp = (pos[i] - self.center[i])
            dist2 += temp * temp
        if dist2 <= self.radius2: return 1
        return 0

sphere_selector = SphereSelector

cdef class RegionSelector(SelectorObject):
    cdef np.float64_t left_edge[3]
    cdef np.float64_t right_edge[3]
    cdef np.float64_t dx_pad

    def __init__(self, dobj):
        cdef int i
        self.dx_pad =dobj._dx_pad
        for i in range(3):
            self.left_edge[i] = dobj.left_edge[i]
            self.right_edge[i] = dobj.right_edge[i]

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        for i in range(3):
            if left_edge[i] >= self.right_edge[i]: return 0
            if right_edge[i] <= self.left_edge[i]: return 0
        return 1

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        cdef np.float64_t dxp
        for i in range(3):
            dxp = self.dx_pad * dds[i]
            if pos[i] - dxp >= self.right_edge[i]:
                eterm[i] = 1
                return 0
            if pos[i] + dxp <= self.left_edge[i]: return 0
        return 1

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef void set_bounds(self,
                         np.float64_t left_edge[3], np.float64_t right_edge[3],
                         np.float64_t dds[3], int ind[3][2], int *check):
        cdef int temp, i, all_inside
        # Left pos is left_edge + 0.5 * dds
        all_inside = 1
        for i in range(3):
            if self.left_edge[i] > left_edge[i]:
                temp = <int> ((self.left_edge[i] - left_edge[i])/dds[i]) - 1
                ind[i][0] = iclip(temp, ind[i][0], ind[i][1] - 1)
                all_inside = 0
            if self.right_edge[i] < right_edge[i]:
                temp = <int> ((self.right_edge[i] - right_edge[i])/dds[i]) + 1
                ind[i][1] = iclip(temp, ind[i][0] + 1, ind[i][1])
                all_inside = 0
        check[0] = all_inside


region_selector = RegionSelector

cdef class DiskSelector(SelectorObject):
    cdef np.float64_t norm_vec[3]
    cdef np.float64_t center[3]
    cdef np.float64_t d
    cdef np.float64_t radius2
    cdef np.float64_t height

    def __init__(self, dobj):
        cdef int i
        for i in range(3):
            self.norm_vec[i] = dobj._norm_vec[i]
            self.center[i] = dobj.center[i]
        self.d = dobj._d
        self.radius2 = dobj._radius * dobj._radius
        self.height = dobj._height

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        cdef np.float64_t *arr[2]
        cdef np.float64_t pos[3], H, D, R2, temp
        cdef int i, j, k, n
        arr[0] = left_edge
        arr[1] = right_edge
        cdef int cond[2]
        cond[0] = cond[1] = 0
        for i in range(2):
            pos[0] = arr[i][0]
            for j in range(2):
                pos[1] = arr[j][1]
                for k in range(2):
                    pos[2] = arr[k][2]
                    H = D = 0
                    for n in range(3):
                        H += (pos[n] * self.norm_vec[n])
                        temp = (pos[n] - self.center[n])
                        D += temp*temp
                    H += self.d
                    R2 = (D - H*H)
                    if fabs(H) < self.height: cond[0] = 1
                    if R2 < self.radius2: cond[1] = 1
        # A moment of explanation:
        #    We want our height to be less than the height AND our radius2 to be
        #    less than radius2, so we set cond[0] equal to 1 if any corners
        #    match that criteria.
        # Note that we OVERSELECT grids, as we are selecting anything within
        # the height and within the radius, which is kind of a funny thing.
        # Cell selection takes care of the rest.
        if cond[0] == 1 and cond[1] == 1:
            return 1
        return 0

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        cdef np.float64_t h, d, r2, temp
        cdef int i
        h = d = 0
        for i in range(3):
            h += pos[i] * self.norm_vec[i]
            temp = pos[i] - self.center[i]
            d += temp*temp
        h += self.d
        r2 = (d - h*h)
        if fabs(h) <= self.height and r2 <= self.radius2: return 1
        return 0

disk_selector = DiskSelector

cdef class CuttingPlaneSelector(SelectorObject):
    cdef np.float64_t norm_vec[3]
    cdef np.float64_t d

    def __init__(self, dobj):
        cdef int i
        for i in range(3):
            self.norm_vec[i] = dobj._norm_vec[i]
        self.d = dobj._d

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        cdef int i, j, k, n
        cdef np.float64_t *arr[2]
        cdef np.float64_t pos[3]
        cdef np.float64_t gd
        arr[0] = left_edge
        arr[1] = right_edge
        all_under = 1
        all_over = 1
        # Check each corner
        for i in range(2):
            pos[0] = arr[i][0]
            for j in range(2):
                pos[1] = arr[j][1]
                for k in range(2):
                    pos[2] = arr[k][2]
                    gd = self.d
                    for n in range(3):
                        gd += pos[n] * self.norm_vec[n]
                    if gd < 0: all_over = 0
                    if gd > 0: all_under = 0
        if all_over == 1 or all_under == 1:
            return 0
        return 1

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        cdef np.float64_t diag2, height
        cdef int i
        height = self.d
        diag2 = 0
        for i in range(3):
            height += pos[i] * self.norm_vec[i]
            diag2 += dds[i] * dds[i] * 0.25
        if height * height <= diag2: return 1
        return 0

cutting_selector = CuttingPlaneSelector

cdef class SliceSelector(SelectorObject):
    cdef int axis
    cdef np.float64_t coord

    def __init__(self, dobj):
        self.axis = dobj.axis
        self.coord = dobj.coord

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef void set_bounds(self,
                         np.float64_t left_edge[3], np.float64_t right_edge[3],
                         np.float64_t dds[3], int ind[3][2], int *check):
        cdef int i
        for i in range(3):
            if self.axis == i:
                ind[i][0] = <int> ((self.coord - left_edge[i])/dds[i])
                ind[i][1] = ind[i][0] + 1
        check[0] = 0

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        if right_edge[self.axis] > self.coord \
           and left_edge[self.axis] <= self.coord:
            return 1
        return 0
    
    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        if pos[self.axis] + 0.5*dds[self.axis] > self.coord \
           and pos[self.axis] - 0.5*dds[self.axis] <= self.coord:
            return 1
        return 0

slice_selector = SliceSelector

cdef class OrthoRaySelector(SelectorObject):

    cdef np.uint8_t px_ax
    cdef np.uint8_t py_ax
    cdef np.float64_t px
    cdef np.float64_t py
    cdef int axis

    def __init__(self, dobj):
        self.axis = dobj.axis
        self.px_ax = dobj.px_ax
        self.py_ax = dobj.py_ax
        self.px = dobj.px
        self.py = dobj.py

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        if (    (self.px >= left_edge[self.px_ax])
            and (self.px < right_edge[self.px_ax])
            and (self.py >= left_edge[self.py_ax])
            and (self.py < right_edge[self.py_ax])):
            return 1
        return 0
    
    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        if (    (self.px >= pos[self.px_ax] - 0.5*dds[self.px_ax])
            and (self.px <  pos[self.px_ax] + 0.5*dds[self.px_ax])
            and (self.py >= pos[self.py_ax] - 0.5*dds[self.py_ax])
            and (self.py <  pos[self.py_ax] + 0.5*dds[self.py_ax])):
            return 1
        return 0


    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef void set_bounds(self,
                         np.float64_t left_edge[3], np.float64_t right_edge[3],
                         np.float64_t dds[3], int ind[3][2], int *check):
        cdef int i
        for i in range(3):
            if self.px_ax == i:
                ind[i][0] = <int> ((self.px - left_edge[i])/dds[i])
                ind[i][1] = ind[i][0] + 1
            elif self.py_ax == i:
                ind[i][0] = <int> ((self.py - left_edge[i])/dds[i])
                ind[i][1] = ind[i][0] + 1
        check[0] = 0

ortho_ray_selector = OrthoRaySelector

cdef struct IntegrationAccumulator:
    np.float64_t *t
    np.float64_t *dt
    np.uint8_t *child_mask
    int hits

cdef void dt_sampler(
             VolumeContainer *vc,
             np.float64_t v_pos[3],
             np.float64_t v_dir[3],
             np.float64_t enter_t,
             np.float64_t exit_t,
             int index[3],
             void *data) nogil:
    cdef IntegrationAccumulator *am = <IntegrationAccumulator *> data
    cdef int di = (index[0]*vc.dims[1]+index[1])*vc.dims[2]+index[2] 
    if am.child_mask[di] == 0 or enter_t == exit_t:
        return
    am.hits += 1
    am.t[di] = enter_t
    am.dt[di] = (exit_t - enter_t)

cdef class RaySelector(SelectorObject):

    cdef np.float64_t p1[3]
    cdef np.float64_t p2[3]
    cdef np.float64_t vec[3]

    def __init__(self, dobj):
        cdef int i
        for i in range(3):
            self.vec[i] = dobj.vec[i]
            self.p1[i] = dobj.start_point[i]
            self.p2[i] = dobj.end_point[i]

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        cdef int i, ax
        cdef int i1, i2
        cdef np.float64_t vs[3], t, v[3]
        for ax in range(3):
            i1 = (ax+1) % 3
            i2 = (ax+2) % 3
            t = (left_edge[ax] - self.p1[ax])/self.vec[ax]
            for i in range(3):
                vs[i] = t * self.vec[i] + self.p1[i]
            if left_edge[i1] <= vs[i1] and \
               right_edge[i1] >= vs[i1] and \
               left_edge[i2] <= vs[i2] and \
               right_edge[i2] >= vs[i2] and \
               0.0 <= t <= 1.0:
                return 1
            t = (right_edge[ax] - self.p1[ax])/self.vec[ax]
            for i in range(3):
                vs[i] = t * self.vec[i] + self.p1[i]
            if left_edge[i1] <= vs[i1] and \
               right_edge[i1] >= vs[i1] and \
               left_edge[i2] <= vs[i2] and \
               right_edge[i2] >= vs[i2] and\
               0.0 <= t <= 1.0:
                return 1
        # if the point is fully enclosed, we count the grid
        if left_edge[0] <= self.p1[0] and \
           right_edge[0] >= self.p1[0] and \
           left_edge[1] <= self.p1[1] and \
           right_edge[1] >= self.p1[1] and \
           left_edge[2] <= self.p1[2] and \
           right_edge[2] >= self.p1[2]:
            return 1
        if left_edge[0] <= self.p2[0] and \
           right_edge[0] >= self.p2[0] and \
           left_edge[1] <= self.p2[1] and \
           right_edge[1] >= self.p2[1] and \
           left_edge[2] <= self.p2[2] and \
           right_edge[2] >= self.p2[2]:
            return 1
        return 0

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def fill_mask(self, gobj):
        cdef np.ndarray[np.float64_t, ndim=3] t, dt
        cdef np.ndarray[np.uint8_t, ndim=3, cast=True] child_mask
        cdef int i
        cdef IntegrationAccumulator ia
        cdef VolumeContainer vc
        mask = np.zeros(gobj.ActiveDimensions, dtype='uint8')
        t = np.zeros(gobj.ActiveDimensions, dtype="float64")
        dt = np.zeros(gobj.ActiveDimensions, dtype="float64") - 1
        child_mask = gobj.child_mask
        ia.t = <np.float64_t *> t.data
        ia.dt = <np.float64_t *> dt.data
        ia.child_mask = <np.uint8_t *> child_mask.data
        ia.hits = 0
        for i in range(3):
            vc.left_edge[i] = gobj.LeftEdge[i]
            vc.right_edge[i] = gobj.RightEdge[i]
            vc.dds[i] = gobj.dds[i]
            vc.idds[i] = 1.0/gobj.dds[i]
            vc.dims[i] = dt.shape[i]
        walk_volume(&vc, self.p1, self.vec, dt_sampler, <void*> &ia)
        for i in range(dt.shape[0]):
            for j in range(dt.shape[1]):
                for k in range(dt.shape[2]):
                    if dt[i,j,k] >= 0:
                        mask[i,j,k] = 1
        return mask.astype("bool")

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def count_cells(self, gobj):
        return self.fill_mask(gobj).sum()
    
    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def get_dt(self, gobj):
        cdef np.ndarray[np.float64_t, ndim=3] t, dt
        cdef np.ndarray[np.float64_t, ndim=1] tr, dtr
        cdef np.ndarray[np.uint8_t, ndim=3, cast=True] child_mask
        cdef int i, j, k, ni
        cdef IntegrationAccumulator ia
        cdef VolumeContainer vc
        t = np.zeros(gobj.ActiveDimensions, dtype="float64")
        dt = np.zeros(gobj.ActiveDimensions, dtype="float64") - 1
        child_mask = gobj.child_mask
        ia.t = <np.float64_t *> t.data
        ia.dt = <np.float64_t *> dt.data
        ia.child_mask = <np.uint8_t *> child_mask.data
        ia.hits = 0
        for i in range(3):
            vc.left_edge[i] = gobj.LeftEdge[i]
            vc.right_edge[i] = gobj.RightEdge[i]
            vc.dds[i] = gobj.dds[i]
            vc.idds[i] = 1.0/gobj.dds[i]
            vc.dims[i] = dt.shape[i]
        walk_volume(&vc, self.p1, self.vec, dt_sampler, <void*> &ia)
        tr = np.zeros(ia.hits, dtype="float64")
        dtr = np.zeros(ia.hits, dtype="float64")
        ni = 0
        for i in range(dt.shape[0]):
            for j in range(dt.shape[1]):
                for k in range(dt.shape[2]):
                    if dt[i,j,k] >= 0:
                        tr[ni] = t[i,j,k]
                        dtr[ni] = dt[i,j,k]
                        ni += 1
        if not (ni == ia.hits):
            print ni, ia.hits
        return dtr, tr
    
ray_selector = RaySelector

cdef class DataCollectionSelector(SelectorObject):
    cdef object obj_ids
    cdef np.int64_t nids

    def __init__(self, dobj):
        self.obj_ids = dobj._obj_ids
        self.nids = self.obj_ids.shape[0]

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef void set_bounds(self,
                         np.float64_t left_edge[3], np.float64_t right_edge[3],
                         np.float64_t dds[3], int ind[3][2], int *check):
        check[0] = 0
        return

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def select_grids(self,
                     np.ndarray[np.float64_t, ndim=2] left_edges,
                     np.ndarray[np.float64_t, ndim=2] right_edges):
        cdef int i, n
        cdef int ng = left_edges.shape[0]
        cdef np.ndarray[np.uint8_t, ndim=1] gridi = np.zeros(ng, dtype='uint8')
        cdef np.ndarray[np.int64_t, ndim=1] oids = self.obj_ids
        with nogil:
            for n in range(self.nids):
                # Call our selector function
                # Check if the sphere is inside the grid
                gridi[oids[n]] = 1
        return gridi.astype("bool")

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def count_cells(self, gobj):
        return gobj.ActiveDimensions.prod()
    
    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def fill_mask(self, gobj):
        cdef np.ndarray[np.uint8_t, ndim=3] mask 
        mask = np.ones(gobj.ActiveDimensions, dtype='uint8')
        return mask.astype("bool")

data_collection_selector = DataCollectionSelector

cdef class EllipsoidSelector(SelectorObject):
    cdef np.float64_t vec[3][3]
    cdef np.float64_t mag[3]
    cdef np.float64_t center[3]

    def __init__(self, dobj):
        cdef int i
        for i in range(3):
            self.center[i] = dobj.center[i]
            self.vec[0][i] = dobj._e0[i]
            self.vec[1][i] = dobj._e1[i]
            self.vec[2][i] = dobj._e2[i]
        self.mag[0] = dobj._A
        self.mag[1] = dobj._B
        self.mag[2] = dobj._C

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_grid(self, np.float64_t left_edge[3],
                               np.float64_t right_edge[3]) nogil:
        # This is the sphere selection
        cdef np.float64_t radius2, box_center, relcenter, closest, dist, edge
        return 1
        radius2 = self.mag[0] * self.mag[0]
        cdef int id
        if (left_edge[0] <= self.center[0] <= right_edge[0] and
            left_edge[1] <= self.center[1] <= right_edge[1] and
            left_edge[2] <= self.center[2] <= right_edge[2]):
            return 1
        # http://www.gamedev.net/topic/335465-is-this-the-simplest-sphere-aabb-collision-test/
        dist = 0
        for i in range(3):
            box_center = (right_edge[i] + left_edge[i])/2.0
            relcenter = self.center[i] - box_center
            edge = right_edge[i] - left_edge[i]
            closest = relcenter - fclip(relcenter, -edge/2.0, edge/2.0)
            dist += closest * closest
        if dist < radius2: return 1
        return 0

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        cdef np.float64_t dot_evec[3]
        cdef np.float64_t dist
        cdef int i, j
        dot_evec[0] = dot_evec[1] = dot_evec[2] = 0
        # Calculate the rotated dot product
        for i in range(3): # axis
            dist = pos[i] - self.center[i]
            for j in range(3):
                dot_evec[j] += dist * self.vec[j][i]
        dist = 0.0
        for i in range(3):
            dist += (dot_evec[i] * dot_evec[i])/(self.mag[i] * self.mag[i])
        if dist <= 1.0: return 1
        return 0

ellipsoid_selector = EllipsoidSelector

cdef class GridSelector(SelectorObject):
    cdef object ind

    def __init__(self, dobj):
        self.ind = dobj.id - dobj._id_offset

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef void set_bounds(self,
                         np.float64_t left_edge[3], np.float64_t right_edge[3],
                         np.float64_t dds[3], int ind[3][2], int *check):
        check[0] = 0
        return

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def select_grids(self,
                     np.ndarray[np.float64_t, ndim=2] left_edges,
                     np.ndarray[np.float64_t, ndim=2] right_edges):
        cdef int ng = left_edges.shape[0]
        cdef np.ndarray[np.uint8_t, ndim=1] gridi = np.zeros(ng, dtype='uint8')
        gridi[self.ind] = 1
        return gridi.astype("bool")

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def count_cells(self, gobj):
        return gobj.ActiveDimensions.prod()
    
    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    def fill_mask(self, gobj):
        return None

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    cdef int select_cell(self, np.float64_t pos[3], np.float64_t dds[3],
                         int eterm[3]) nogil:
        return 1


grid_selector = GridSelector