Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SubShapeID.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
23{
24public:
26
28 using Type = uint32;
29
32
33 static_assert(sizeof(BiggerType) > sizeof(Type), "The calculation below assumes BiggerType is a bigger type than Type");
34
36 static constexpr uint MaxBits = 8 * sizeof(Type);
37
39 SubShapeID() = default;
40
42 Type PopID(uint inBits, SubShapeID &outRemainder) const
43 {
44 Type mask_bits = Type((BiggerType(1) << inBits) - 1);
45 Type fill_bits = Type(BiggerType(cEmpty) << (MaxBits - inBits)); // Fill left side bits with 1 so that if there's no remainder all bits will be set, note that we do this using a BiggerType since on intel 0xffffffff << 32 == 0xffffffff
46 Type v = mValue & mask_bits;
47 outRemainder = SubShapeID(Type(BiggerType(mValue) >> inBits) | fill_bits);
48 return v;
49 }
50
52 inline Type GetValue() const
53 {
54 return mValue;
55 }
56
58 inline void SetValue(Type inValue)
59 {
60 mValue = inValue;
61 }
62
65 inline bool IsEmpty() const
66 {
67 return mValue == cEmpty;
68 }
69
71 inline bool operator == (const SubShapeID &inRHS) const
72 {
73 return mValue == inRHS.mValue;
74 }
75
77 inline bool operator != (const SubShapeID &inRHS) const
78 {
79 return mValue != inRHS.mValue;
80 }
81
82private:
83 friend class SubShapeIDCreator;
84
86 static constexpr Type cEmpty = ~Type(0);
87
89 explicit SubShapeID(const Type &inValue) : mValue(inValue) { }
90
93 void PushID(Type inValue, uint inFirstBit, uint inBits)
94 {
95 // First clear the bits
96 mValue &= ~(Type((BiggerType(1) << inBits) - 1) << inFirstBit);
97
98 // Then set them to the new value
99 mValue |= inValue << inFirstBit;
100 }
101
102 Type mValue = cEmpty;
103};
104
108{
109public:
111 SubShapeIDCreator PushID(uint inValue, uint inBits) const
112 {
113 JPH_ASSERT(inValue < (SubShapeID::BiggerType(1) << inBits));
114 SubShapeIDCreator copy = *this;
115 copy.mID.PushID(inValue, mCurrentBit, inBits);
116 copy.mCurrentBit += inBits;
117 JPH_ASSERT(copy.mCurrentBit <= SubShapeID::MaxBits);
118 return copy;
119 }
120
121 // Get the resulting sub shape ID
122 const SubShapeID & GetID() const
123 {
124 return mID;
125 }
126
128 inline uint GetNumBitsWritten() const
129 {
130 return mCurrentBit;
131 }
132
133private:
134 SubShapeID mID;
135 uint mCurrentBit = 0;
136};
137
std::uint64_t uint64
Definition Core.h:457
unsigned int uint
Definition Core.h:453
#define JPH_NAMESPACE_END
Definition Core.h:379
std::uint32_t uint32
Definition Core.h:456
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
Definition SubShapeID.h:108
SubShapeIDCreator PushID(uint inValue, uint inBits) const
Add a new id to the chain of id's and return it.
Definition SubShapeID.h:111
const SubShapeID & GetID() const
Definition SubShapeID.h:122
uint GetNumBitsWritten() const
Get the number of bits that have been written to the sub shape ID so far.
Definition SubShapeID.h:128
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition SubShapeID.h:23
bool operator==(const SubShapeID &inRHS) const
Check equal.
Definition SubShapeID.h:71
bool operator!=(const SubShapeID &inRHS) const
Check not-equal.
Definition SubShapeID.h:77
void SetValue(Type inValue)
Set the value of the sub shape ID (use with care!)
Definition SubShapeID.h:58
uint32 Type
Underlying storage type.
Definition SubShapeID.h:28
bool IsEmpty() const
Definition SubShapeID.h:65
static constexpr uint MaxBits
How many bits we can store in this ID.
Definition SubShapeID.h:36
Type PopID(uint inBits, SubShapeID &outRemainder) const
Get the next id in the chain of ids (pops parents before children)
Definition SubShapeID.h:42
Type GetValue() const
Get the value of the path to the sub shape ID.
Definition SubShapeID.h:52
SubShapeID()=default
Constructor.
uint64 BiggerType
Type that is bigger than the underlying storage type for operations that would otherwise overflow.
Definition SubShapeID.h:31