7 #include "imstkCapsule.h" 8 #include "imstkCollisionHandling.h" 9 #include "imstkGeometry.h" 10 #include "imstkMath.h" 11 #include "imstkMeshIO.h" 12 #include "imstkPbdModel.h" 13 #include "imstkPbdModelConfig.h" 14 #include "imstkPbdObject.h" 15 #include "imstkPbdObjectCollision.h" 16 #include "imstkPointSetToCapsuleCD.h" 17 #include "imstkPointwiseMap.h" 18 #include "imstkRbdConstraint.h" 19 #include "imstkScene.h" 20 #include "imstkSphere.h" 21 #include "imstkSurfaceMesh.h" 22 #include "imstkSurfaceMeshToCapsuleCD.h" 23 #include "imstkTetrahedralMesh.h" 25 #include <benchmark/benchmark.h> 27 using namespace imstk;
35 static std::shared_ptr<TetrahedralMesh>
36 makeTetGrid(
const Vec3d& size,
const Vec3i& dim,
const Vec3d& center)
38 auto prismMesh = std::make_shared<TetrahedralMesh>();
39 auto verticesPtr = std::make_shared<VecDataArray<double, 3>>(dim[0] * dim[1] * dim[2]);
42 const Vec3d dx = size.cwiseQuotient((dim - Vec3i(1, 1, 1)).cast<double>());
43 for (
int z = 0; z < dim[2]; z++)
45 for (
int y = 0; y < dim[1]; y++)
47 for (
int x = 0; x < dim[0]; x++)
49 vertices[x + dim[0] * (y + dim[1] * z)] = Vec3i(x, y, z).
cast<
double>().cwiseProduct(dx) - size * 0.5 + center;
55 auto indicesPtr = std::make_shared<VecDataArray<int, 4>>();
58 for (
int z = 0; z < dim[2] - 1; z++)
60 for (
int y = 0; y < dim[1] - 1; y++)
62 for (
int x = 0; x < dim[0] - 1; x++)
66 x + dim[0] * (y + dim[1] * z),
67 (x + 1) + dim[0] * (y + dim[1] * z),
68 (x + 1) + dim[0] * (y + dim[1] * (z + 1)),
69 x + dim[0] * (y + dim[1] * (z + 1)),
70 x + dim[0] * ((y + 1) + dim[1] * z),
71 (x + 1) + dim[0] * ((y + 1) + dim[1] * z),
72 (x + 1) + dim[0] * ((y + 1) + dim[1] * (z + 1)),
73 x + dim[0] * ((y + 1) + dim[1] * (z + 1))
77 if ((z % 2 ^ x % 2) ^ y % 2)
79 indices.
push_back(Vec4i(cubeIndices[0], cubeIndices[7], cubeIndices[5], cubeIndices[4]));
80 indices.push_back(Vec4i(cubeIndices[3], cubeIndices[7], cubeIndices[2], cubeIndices[0]));
81 indices.push_back(Vec4i(cubeIndices[2], cubeIndices[7], cubeIndices[5], cubeIndices[0]));
82 indices.push_back(Vec4i(cubeIndices[1], cubeIndices[2], cubeIndices[0], cubeIndices[5]));
83 indices.push_back(Vec4i(cubeIndices[2], cubeIndices[6], cubeIndices[7], cubeIndices[5]));
87 indices.push_back(Vec4i(cubeIndices[3], cubeIndices[7], cubeIndices[6], cubeIndices[4]));
88 indices.push_back(Vec4i(cubeIndices[1], cubeIndices[3], cubeIndices[6], cubeIndices[4]));
89 indices.push_back(Vec4i(cubeIndices[3], cubeIndices[6], cubeIndices[2], cubeIndices[1]));
90 indices.push_back(Vec4i(cubeIndices[1], cubeIndices[6], cubeIndices[5], cubeIndices[4]));
91 indices.push_back(Vec4i(cubeIndices[0], cubeIndices[3], cubeIndices[1], cubeIndices[4]));
97 auto uvCoordsPtr = std::make_shared<VecDataArray<float, 2>>(dim[0] * dim[1] * dim[2]);
100 for (
int z = 0; z < dim[2]; z++)
102 for (
int y = 0; y < dim[1]; y++)
104 for (
int x = 0; x < dim[0]; x++)
106 uvCoords[x + dim[0] * (y + dim[1] * z)] = Vec2f(static_cast<float>(x) / dim[0],
static_cast<float>(z) / dim[2]) * 3.0;
112 for (
int i = 0; i < indices.size(); i++)
114 if (tetVolume(vertices[indices[i][0]], vertices[indices[i][1]], vertices[indices[i][2]], vertices[indices[i][3]]) < 0.0)
116 std::swap(indices[i][0], indices[i][2]);
120 prismMesh->initialize(verticesPtr, indicesPtr);
121 prismMesh->setVertexTCoords(
"uvs", uvCoordsPtr);
130 BM_CopyLoop(benchmark::State& state)
132 std::shared_ptr<PointSet> parent = std::make_shared<PointSet>();
133 std::shared_ptr<PointSet> child = std::make_shared<PointSet>();
135 const int numPoints = state.range(0);
137 auto parentVerticesPtr = std::make_shared<VecDataArray<double, 3>>(numPoints);
138 auto childVerticesPtr = std::make_shared<VecDataArray<double, 3>>(numPoints);
141 for (
int i = 0; i < numPoints; i++)
143 (*parentVerticesPtr)[i] = Vec3d::Random();
145 parent->setInitialVertexPositions(parentVerticesPtr);
146 child->setInitialVertexPositions(childVerticesPtr);
150 std::vector<std::pair<int, int>> map;
151 for (
int i = 0; i < numPoints; i++)
153 map.push_back({ i, numPoints - i - 1 });
159 for (
int i = 0; i < numPoints; i++)
161 (*childVerticesPtr)[map[i].second] = (*parentVerticesPtr)[map[i].first];
166 BENCHMARK(BM_CopyLoop)
167 ->Unit(benchmark::kMicrosecond)
168 ->Name(
"Copy vertices in loop")
169 ->RangeMultiplier(2)->Range(8, 16 << 10);
172 BM_CopyParallel(benchmark::State& state)
174 std::shared_ptr<PointSet> parent = std::make_shared<PointSet>();
175 std::shared_ptr<PointSet> child = std::make_shared<PointSet>();
177 const int numPoints = state.range(0);
179 auto parentVerticesPtr = std::make_shared<VecDataArray<double, 3>>(numPoints);
180 auto childVerticesPtr = std::make_shared<VecDataArray<double, 3>>(numPoints);
183 for (
int i = 0; i < numPoints; i++)
185 (*parentVerticesPtr)[i] = Vec3d::Random();
187 parent->setInitialVertexPositions(parentVerticesPtr);
188 child->setInitialVertexPositions(childVerticesPtr);
192 std::vector<std::pair<int, int>> map;
193 for (
int i = 0; i < numPoints; i++)
195 map.push_back({ i, numPoints - i - 1 });
204 const int size = map.size();
205 ParallelUtils::parallelFor(map.size(),
206 [&](
const size_t idx)
208 const auto& mapValue = map[idx];
209 childVertices[mapValue.first] = parentVertices[mapValue.second];
214 BENCHMARK(BM_CopyParallel)
215 ->Unit(benchmark::kMicrosecond)
216 ->Name(
"Copy vertices in parallel")
217 ->RangeMultiplier(2)->Range(8, 16 << 10);
void push_back(const ValueType &val)
Append the data array to hold the new value, resizes if neccesary.
VecDataArray< U, N > cast()
Templated copy the current array with a new internal data type, does not change the number of compone...