summaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvmath/ProximityGrid.h
blob: a21bb3bd6893ea6aaa19088aff821f879812e37d (plain)
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
#pragma once
#ifndef NV_MATH_PROXIMITYGRID_H
#define NV_MATH_PROXIMITYGRID_H

#include "Vector.h"
#include "ftoi.h"

#include "nvcore/Array.inl"


// A simple, dynamic proximity grid based on Jon's code.
// Instead of storing pointers here I store indices.

namespace nv {

    class Box;

    struct Cell {
        Array<uint> indexArray;
    };

    struct ProximityGrid {
        ProximityGrid();

        void reset();
        void init(const Array<Vector3> & pointArray);
        void init(const Box & box, uint count);

        int index_x(float x) const;
        int index_y(float y) const;
        int index_z(float z) const;
        int index(int x, int y, int z) const;
        int index(const Vector3 & pos) const;
        
        uint32 mortonCount() const;
        int mortonIndex(uint32 code) const;

        void add(const Vector3 & pos, uint key);
        bool remove(const Vector3 & pos, uint key);

        void gather(const Vector3 & pos, float radius, Array<uint> & indices);

        Array<Cell> cellArray;

        Vector3 corner;
        Vector3 invCellSize;
        int sx, sy, sz;
    };

    // For morton traversal, do:
    // for (int code = 0; code < mortonCount(); code++) {
    //   int idx = mortonIndex(code);
    //   if (idx < 0) continue;
    // }



    inline int ProximityGrid::index_x(float x) const {
        return clamp(ftoi_floor((x - corner.x) * invCellSize.x),  0, sx-1);
    }

    inline int ProximityGrid::index_y(float y) const {
        return clamp(ftoi_floor((y - corner.y) * invCellSize.y),  0, sy-1);
    }

    inline int ProximityGrid::index_z(float z) const {
        return clamp(ftoi_floor((z - corner.z) * invCellSize.z),  0, sz-1);
    }

    inline int ProximityGrid::index(int x, int y, int z) const {
        nvDebugCheck(x >= 0 && x < sx);
        nvDebugCheck(y >= 0 && y < sy);
        nvDebugCheck(z >= 0 && z < sz);
        int idx = (z * sy + y) * sx + x;
        nvDebugCheck(idx >= 0 && uint(idx) < cellArray.count());
        return idx;
    }

    inline int ProximityGrid::index(const Vector3 & pos) const {
        int x = index_x(pos.x);
        int y = index_y(pos.y);
        int z = index_z(pos.z);
        return index(x, y, z);
    }


    inline void ProximityGrid::add(const Vector3 & pos, uint key) {
        uint idx = index(pos);
        cellArray[idx].indexArray.append(key);
    }

    inline bool ProximityGrid::remove(const Vector3 & pos, uint key) {
        uint idx = index(pos);
        return cellArray[idx].indexArray.remove(key);
    }

} // nv namespace

#endif // NV_MATH_PROXIMITYGRID_H