DART 6.12.2
Loading...
Searching...
No Matches
Geometry-impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2021, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_MATH_DETAIL_GEOMETRY_IMPL_HPP_
34#define DART_MATH_DETAIL_GEOMETRY_IMPL_HPP_
35
36#include <unordered_map>
37
38#include "dart/external/convhull_3d/convhull_3d.h"
40
41namespace dart {
42namespace math {
43
44//==============================================================================
45template <typename S, typename Index>
46std::tuple<
47 std::vector<Eigen::Matrix<S, 3, 1>>,
48 std::vector<Eigen::Matrix<Index, 3, 1>>>
50 const std::vector<Eigen::Matrix<S, 3, 1>>& vertices,
51 const std::vector<Eigen::Matrix<Index, 3, 1>>& triangles)
52{
53 auto newVertices = std::vector<Eigen::Matrix<S, 3, 1>>();
54 auto newTriangles = std::vector<Eigen::Matrix<Index, 3, 1>>();
55 newTriangles.resize(triangles.size());
56 auto indexMap = std::unordered_map<Index, Index>();
57 auto newIndex = 0;
58
59 for (auto i = 0u; i < triangles.size(); ++i)
60 {
61 const auto& triangle = triangles[i];
62 auto& newTriangle = newTriangles[i];
63
64 for (auto j = 0u; j < 3; ++j)
65 {
66 const auto result
67 = indexMap.insert(std::make_pair(triangle[j], newIndex));
68 const bool& inserted = result.second;
69 if (inserted)
70 {
71 newVertices.push_back(vertices[triangle[j]]);
72 newIndex++;
73 }
74
75 newTriangle[j] = indexMap[triangle[j]];
76 }
77 }
78
79 return std::make_tuple(newVertices, newTriangles);
80}
81
82//==============================================================================
83template <typename S, typename Index>
84std::tuple<
85 std::vector<Eigen::Matrix<S, 3, 1>>,
86 std::vector<Eigen::Matrix<Index, 3, 1>>>
88 const std::vector<Eigen::Matrix<S, 3, 1>>& inputVertices, bool optimize)
89{
90 ch_vertex* vertices = new ch_vertex[inputVertices.size()];
91
92 for (auto i = 0u; i < inputVertices.size(); ++i)
93 {
94 const Eigen::Matrix<S, 3, 1>& inputV = inputVertices[i];
95 ch_vertex& v = vertices[i];
96 v.x = inputV[0];
97 v.y = inputV[1];
98 v.z = inputV[2];
99 }
100
101 int* faces = nullptr;
102 int numFaces = 0;
103
104 convhull_3d_build(vertices, inputVertices.size(), &faces, &numFaces);
105
106 std::vector<Eigen::Matrix<Index, 3, 1>> eigenFaces;
107 eigenFaces.reserve(numFaces);
108
109 for (auto i = 0; i < numFaces; ++i)
110 {
111 const auto index1 = faces[3 * i];
112 const auto index2 = faces[3 * i + 1];
113 const auto index3 = faces[3 * i + 2];
114
115 eigenFaces.emplace_back(index1, index2, index3);
116 }
117
118 free(faces);
119 delete[] vertices;
120
121 if (optimize)
122 return discardUnusedVertices<S, Index>(inputVertices, eigenFaces);
123 else
124 return std::make_pair(inputVertices, eigenFaces);
125}
126
127} // namespace math
128} // namespace dart
129
130#endif // DART_MATH_DETAIL_GEOMETRY_IMPL_HPP_
CollisionResult * result
Collision result of DART.
Definition FCLCollisionDetector.cpp:160
std::tuple< std::vector< Eigen::Matrix< S, 3, 1 > >, std::vector< Eigen::Matrix< Index, 3, 1 > > > discardUnusedVertices(const std::vector< Eigen::Matrix< S, 3, 1 > > &vertices, const std::vector< Eigen::Matrix< Index, 3, 1 > > &triangles)
Definition Geometry-impl.hpp:49
std::tuple< std::vector< Eigen::Matrix< S, 3, 1 > >, std::vector< Eigen::Matrix< Index, 3, 1 > > > computeConvexHull3D(const std::vector< Eigen::Matrix< S, 3, 1 > > &inputVertices, bool optimize)
Generates a 3D convex hull given vertices and indices.
Definition Geometry-impl.hpp:87
Definition BulletCollisionDetector.cpp:60