Skip to content

Commit 6564535

Browse files
committed
Move CPU/GPU agnostic code into pbat namespace
1 parent e060c79 commit 6564535

File tree

19 files changed

+99
-126
lines changed

19 files changed

+99
-126
lines changed

source/pbat/common/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ target_sources(PhysicsBasedAnimationToolkit_PhysicsBasedAnimationToolkit
88
"Eigen.h"
99
"Hash.h"
1010
"Indexing.h"
11+
"Queue.h"
12+
"Stack.h"
1113
)
1214
target_sources(PhysicsBasedAnimationToolkit_PhysicsBasedAnimationToolkit
1315
PRIVATE
1416
"Eigen.cpp"
1517
"Indexing.cpp"
18+
"Queue.cpp"
19+
"Stack.cpp"
1620
)

source/pbat/common/Common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
#include "Eigen.h"
77
#include "Hash.h"
88
#include "Indexing.h"
9+
#include "Queue.h"
10+
#include "Stack.h"
911

1012
#endif // PBAT_COMMON_COMMON_H

source/pbat/common/Queue.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Queue.h"

source/pbat/gpu/common/Queue.cuh renamed to source/pbat/common/Queue.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#ifndef PBAT_GPU_COMMON_QUEUE_CUH
2-
#define PBAT_GPU_COMMON_QUEUE_CUH
1+
#ifndef PBAT_COMMON_QUEUE_H
2+
#define PBAT_COMMON_QUEUE_H
33

44
#include "pbat/HostDevice.h"
5-
#include "pbat/gpu/Aliases.h"
65

76
namespace pbat {
8-
namespace gpu {
97
namespace common {
108

119
template <class T, auto kCapacity = 64>
@@ -27,16 +25,15 @@ class Queue
2725
}
2826
PBAT_HOST_DEVICE bool IsFull() const { return n == kCapacity; }
2927
PBAT_HOST_DEVICE bool IsEmpty() const { return n == 0; }
30-
PBAT_HOST_DEVICE GpuIndex Size() const { return n; }
28+
PBAT_HOST_DEVICE auto Size() const { return n; }
3129
PBAT_HOST_DEVICE void Clear() { begin = end = n = 0; }
3230

3331
private:
3432
T queue[kCapacity];
35-
GpuIndex begin, end, n;
33+
int begin, end, n;
3634
};
3735

3836
} // namespace common
39-
} // namespace gpu
4037
} // namespace pbat
4138

42-
#endif // PBAT_GPU_COMMON_QUEUE_CUH
39+
#endif // PBAT_COMMON_QUEUE_H

source/pbat/common/Stack.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Stack.h"
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#ifndef PBAT_GPU_COMMON_STACK_CUH
2-
#define PBAT_GPU_COMMON_STACK_CUH
1+
#ifndef PBAT_COMMON_STACK_H
2+
#define PBAT_COMMON_STACK_H
33

44
#include "pbat/HostDevice.h"
5-
#include "pbat/gpu/Aliases.h"
65

76
namespace pbat {
8-
namespace gpu {
97
namespace common {
108

119
template <class T, auto kCapacity = 64>
@@ -16,18 +14,17 @@ class Stack
1614
PBAT_HOST_DEVICE void Push(T value) { stack[size++] = value; }
1715
PBAT_HOST_DEVICE T Pop() { return stack[--size]; }
1816
PBAT_HOST_DEVICE T const& Top() const { return stack[size - 1]; }
19-
PBAT_HOST_DEVICE GpuIndex Size() const { return size; }
17+
PBAT_HOST_DEVICE auto Size() const { return size; }
2018
PBAT_HOST_DEVICE bool IsEmpty() const { return size == 0; }
2119
PBAT_HOST_DEVICE bool IsFull() const { return size == kCapacity; }
2220
PBAT_HOST_DEVICE void Clear() { size = 0; }
2321

2422
private:
2523
T stack[kCapacity];
26-
GpuIndex size; ///< Serves as both the stack pointer and the number of elements in the stack
24+
int size; ///< Serves as both the stack pointer and the number of elements in the stack
2725
};
2826

2927
} // namespace common
30-
} // namespace gpu
3128
} // namespace pbat
3229

33-
#endif // PBAT_GPU_COMMON_STACK_CUH
30+
#endif // PBAT_COMMON_STACK_H

source/pbat/geometry/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ target_sources(PhysicsBasedAnimationToolkit_PhysicsBasedAnimationToolkit
99
"Geometry.h"
1010
"IntersectionQueries.h"
1111
"KdTree.h"
12+
"Morton.h"
1213
"OverlapQueries.h"
1314
"TetrahedralAabbHierarchy.h"
1415
"TriangleAabbHierarchy.h"
@@ -21,6 +22,7 @@ target_sources(PhysicsBasedAnimationToolkit_PhysicsBasedAnimationToolkit
2122
"DistanceQueries.cpp"
2223
"IntersectionQueries.cpp"
2324
"KdTree.cpp"
25+
"Morton.cpp"
2426
"OverlapQueries.cpp"
2527
"TetrahedralAabbHierarchy.cpp"
2628
"TriangleAabbHierarchy.cpp"

source/pbat/geometry/Geometry.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#ifndef PBAT_GEOMETRY_GEOMETRY_H
22
#define PBAT_GEOMETRY_GEOMETRY_H
33

4+
#include "AxisAlignedBoundingBox.h"
45
#include "BoundingVolumeHierarchy.h"
6+
#include "ClosestPointQueries.h"
7+
#include "DistanceQueries.h"
8+
#include "IntersectionQueries.h"
9+
#include "KdTree.h"
10+
#include "Morton.h"
11+
#include "OverlapQueries.h"
12+
#include "TetrahedralAabbHierarchy.h"
13+
#include "TriangleAabbHierarchy.h"
514

615
#endif // PBAT_GEOMETRY_GEOMETRY_H

source/pbat/geometry/Morton.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Morton.h"

source/pbat/geometry/Morton.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef PBAT_GEOMETRY_MORTON_H
2+
#define PBAT_GEOMETRY_MORTON_H
3+
4+
#include "pbat/HostDevice.h"
5+
6+
#include <array>
7+
#include <cmath>
8+
#include <cstddef>
9+
10+
namespace pbat {
11+
namespace geometry {
12+
13+
using MortonCodeType = std::uint32_t;
14+
15+
// NOTE:
16+
// We make these (otherwise non-templated) functions inline so that they
17+
// are compiled by nvcc whenever this header is included in cuda sources.
18+
19+
// Expands a 10-bit integer into 30 bits
20+
// by inserting 2 zeros after each bit.
21+
PBAT_HOST_DEVICE inline MortonCodeType ExpandBits(MortonCodeType v)
22+
{
23+
v = (v * 0x00010001u) & 0xFF0000FFu;
24+
v = (v * 0x00000101u) & 0x0F00F00Fu;
25+
v = (v * 0x00000011u) & 0xC30C30C3u;
26+
v = (v * 0x00000005u) & 0x49249249u;
27+
return v;
28+
}
29+
30+
// Calculates a 30-bit Morton code for the
31+
// given 3D point located within the unit cube [0,1].
32+
[[maybe_unused]] PBAT_HOST_DEVICE inline MortonCodeType Morton3D(std::array<float, 3> x)
33+
{
34+
using namespace std;
35+
x[0] = min(max(x[0] * 1024.0f, 0.0f), 1023.0f);
36+
x[1] = min(max(x[1] * 1024.0f, 0.0f), 1023.0f);
37+
x[2] = min(max(x[2] * 1024.0f, 0.0f), 1023.0f);
38+
MortonCodeType xx = ExpandBits(static_cast<MortonCodeType>(x[0]));
39+
MortonCodeType yy = ExpandBits(static_cast<MortonCodeType>(x[1]));
40+
MortonCodeType zz = ExpandBits(static_cast<MortonCodeType>(x[2]));
41+
return xx * 4 + yy * 2 + zz;
42+
}
43+
44+
} // namespace geometry
45+
} // namespace pbat
46+
47+
#endif // PBAT_GEOMETRY_MORTON_H

0 commit comments

Comments
 (0)