|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | + |
| 5 | +#include "drake/common/bit_cast.h" |
| 6 | +#include "drake/common/drake_assert.h" |
| 7 | +#include "drake/common/drake_copyable.h" |
| 8 | +#include "drake/common/eigen_types.h" |
| 9 | + |
| 10 | +namespace drake { |
| 11 | +namespace multibody { |
| 12 | +namespace mpm { |
| 13 | +namespace internal { |
| 14 | + |
| 15 | +/* This class is a lightweight wrapper around an integer type (`int32_t` or |
| 16 | + `int64_t`) used to differentiate between active grid node indices, inactive |
| 17 | + states, and a special flag. An IndexOrFlag can be in exactly one of the |
| 18 | + following states: |
| 19 | +
|
| 20 | + 1. Active index: A non-negative integer representing the index of a grid node. |
| 21 | + 2. Inactive state (the default state): neither an index nor a flag. |
| 22 | + 3. The `flag` state: A special state used to mark grid nodes for deferred |
| 23 | + processing. The flag state is intended as a marker that must only be set |
| 24 | + from the inactive state (not from an active index) to maintain consistency. |
| 25 | +
|
| 26 | + Transitions between states are as follows: |
| 27 | + - Any state can become inactive. |
| 28 | + - The inactive state can become flag state. |
| 29 | + - The inactive state can become active (with a non-negative index). |
| 30 | + - Active cannot directly become flag state (must go inactive first). |
| 31 | +
|
| 32 | + An IndexOrFlag object is guaranteed to have size equal to its template |
| 33 | + parameter T. In addition, the inactive state is guaranteed to be represented |
| 34 | + with the value -1. |
| 35 | +
|
| 36 | + @tparam T The integer type for the index. Must be `int32_t` or `int64_t`. */ |
| 37 | +template <typename T> |
| 38 | +class IndexOrFlag { |
| 39 | + public: |
| 40 | + DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(IndexOrFlag); |
| 41 | + |
| 42 | + static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t>, |
| 43 | + "T must be int32_t or int64_t."); |
| 44 | + |
| 45 | + /* Default constructor initializes the object to the inactive state. */ |
| 46 | + constexpr IndexOrFlag() = default; |
| 47 | + |
| 48 | + /* Constructor for an active index. |
| 49 | + @pre index >= 0 */ |
| 50 | + explicit constexpr IndexOrFlag(T index) { set_index(index); } |
| 51 | + |
| 52 | + /* Sets the index to the given value, which must be non-negative, thereby |
| 53 | + making `this` active. |
| 54 | + @pre index >= 0 */ |
| 55 | + void set_index(T index) { |
| 56 | + DRAKE_ASSERT(index >= 0); |
| 57 | + value_ = index; |
| 58 | + } |
| 59 | + |
| 60 | + /* Sets `this` to the inactive state. */ |
| 61 | + void set_inactive() { value_ = kInactive; } |
| 62 | + |
| 63 | + /* Sets `this` to the flag state. |
| 64 | + @pre !is_index() (i.e., must currently be inactive) */ |
| 65 | + void set_flag() { |
| 66 | + DRAKE_ASSERT(!is_index()); |
| 67 | + value_ = kFlag; |
| 68 | + } |
| 69 | + |
| 70 | + /* Returns true iff `this` is an active index (i.e., a non-negative integer). |
| 71 | + */ |
| 72 | + constexpr bool is_index() const { return value_ >= 0; } |
| 73 | + |
| 74 | + /* Returns true iff `this` is in the inactive state. */ |
| 75 | + constexpr bool is_inactive() const { return value_ == kInactive; } |
| 76 | + |
| 77 | + /* Returns true iff `this` is in the flag state. */ |
| 78 | + constexpr bool is_flag() const { return value_ == kFlag; } |
| 79 | + |
| 80 | + /* Returns the index value. |
| 81 | + @pre is_index() == true; */ |
| 82 | + constexpr T index() const { |
| 83 | + DRAKE_ASSERT(is_index()); |
| 84 | + return value_; |
| 85 | + } |
| 86 | + |
| 87 | + private: |
| 88 | + /* Note that the enum values are not arbitrary; kInactive must be -1 as in the |
| 89 | + class documentation. */ |
| 90 | + enum : T { kInactive = -1, kFlag = -2 }; |
| 91 | + /* We encode all information in `value_` to satisfy the size requirement laid |
| 92 | + out in the class documentation. */ |
| 93 | + T value_{kInactive}; |
| 94 | +}; |
| 95 | + |
| 96 | +/* GridData stores data at a single grid node of SparseGrid. |
| 97 | +
|
| 98 | + It contains the mass and velocity of the node along with a scratch space for |
| 99 | + temporary storage and an index to the node. |
| 100 | +
|
| 101 | + It's important to be conscious of the size of GridData since the MPM algorithm |
| 102 | + is usually memory-bound. We carefully pack GridData to be a power of 2 to work |
| 103 | + with SPGrid, which automatically packs the data to a power of 2. |
| 104 | +
|
| 105 | + @tparam T double or float. */ |
| 106 | +template <typename T> |
| 107 | +struct GridData { |
| 108 | + static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, |
| 109 | + "T must be float or double."); |
| 110 | + |
| 111 | + /* Resets `this` GridData to its default state where all floating point values |
| 112 | + are set to NAN and the index is inactive. */ |
| 113 | + void reset() { *this = {}; } |
| 114 | + |
| 115 | + /* Returns true iff `this` GridData is bit-wise equal to `other`. */ |
| 116 | + bool operator==(const GridData<T>& other) const { |
| 117 | + return std::memcmp(this, &other, sizeof(GridData<T>)) == 0; |
| 118 | + } |
| 119 | + |
| 120 | + Vector3<T> v{Vector3<T>::Constant(nan_with_all_bits_set())}; |
| 121 | + T m{nan_with_all_bits_set()}; |
| 122 | + Vector3<T> scratch{Vector3<T>::Constant(nan_with_all_bits_set())}; |
| 123 | + std::conditional_t<std::is_same_v<T, float>, IndexOrFlag<int32_t>, |
| 124 | + IndexOrFlag<int64_t>> |
| 125 | + index_or_flag{}; |
| 126 | + |
| 127 | + private: |
| 128 | + /* Returns a floating point NaN value with all bits set to one. This choice |
| 129 | + makes the reset() function more efficient. In particlar, it allows the |
| 130 | + generated machine code to memset all bits to 1 instead of handling each field |
| 131 | + individually. */ |
| 132 | + static T nan_with_all_bits_set() { |
| 133 | + using IntType = |
| 134 | + std::conditional_t<std::is_same_v<T, float>, int32_t, int64_t>; |
| 135 | + constexpr IntType kAllBitsOn = -1; |
| 136 | + return drake::internal::bit_cast<T>(kAllBitsOn); |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +/* With T = float, GridData is expected to be 32 bytes. With T = double, |
| 141 | + GridData is expected to be 64 bytes. We enforce these sizes at compile time |
| 142 | + with static_assert, so that if future changes to this code, compiler alignment, |
| 143 | + or Eigen alignment rules cause a size shift, it will be caught early. */ |
| 144 | +static_assert(sizeof(GridData<float>) == 32, |
| 145 | + "Unexpected size for GridData<float>."); |
| 146 | +static_assert(sizeof(GridData<double>) == 64, |
| 147 | + "Unexpected size for GridData<double>."); |
| 148 | + |
| 149 | +} // namespace internal |
| 150 | +} // namespace mpm |
| 151 | +} // namespace multibody |
| 152 | +} // namespace drake |
0 commit comments