DART 6.11.1
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
37
38#include <unordered_map>
39
40#include "dart/external/convhull_3d/convhull_3d.h"
41
42namespace dart {
43namespace math {
44
45//==============================================================================
46template <typename S, typename Index>
47std::tuple<
48 std::vector<Eigen::Matrix<S, 3, 1>>,
49 std::vector<Eigen::Matrix<Index, 3, 1>>>
51 const std::vector<Eigen::Matrix<S, 3, 1>>& vertices,
52 const std::vector<Eigen::Matrix<Index, 3, 1>>& triangles)
53{
54 auto newVertices = std::vector<Eigen::Matrix<S, 3, 1>>();
55 auto newTriangles = std::vector<Eigen::Matrix<Index, 3, 1>>();
56 newTriangles.resize(triangles.size());
57 auto indexMap = std::unordered_map<Index, Index>();
58 auto newIndex = 0;
59
60 for (auto i = 0u; i < triangles.size(); ++i)
61 {
62 const auto& triangle = triangles[i];
63 auto& newTriangle = newTriangles[i];
64
65 for (auto j = 0u; j < 3; ++j)
66 {
67 const auto result
68 = indexMap.insert(std::make_pair(triangle[j], newIndex));
69 const bool& inserted = result.second;
70 if (inserted)
71 {
72 newVertices.push_back(vertices[triangle[j]]);
73 newIndex++;
74 }
75
76 newTriangle[j] = indexMap[triangle[j]];
77 }
78 }
79
80 return std::make_tuple(newVertices, newTriangles);
81}
82
83//==============================================================================
84template <typename S, typename Index>
85std::tuple<
86 std::vector<Eigen::Matrix<S, 3, 1>>,
87 std::vector<Eigen::Matrix<Index, 3, 1>>>
89 const std::vector<Eigen::Matrix<S, 3, 1>>& inputVertices, bool optimize)
90{
91 ch_vertex* vertices = new ch_vertex[inputVertices.size()];
92
93 for (auto i = 0u; i < inputVertices.size(); ++i)
94 {
95 const Eigen::Matrix<S, 3, 1>& inputV = inputVertices[i];
96 ch_vertex& v = vertices[i];
97 v.x = inputV[0];
98 v.y = inputV[1];
99 v.z = inputV[2];
100 }
101
102 int* faces = nullptr;
103 int numFaces = 0;
104
105 convhull_3d_build(vertices, inputVertices.size(), &faces, &numFaces);
106
107 std::vector<Eigen::Matrix<Index, 3, 1>> eigenFaces;
108 eigenFaces.reserve(numFaces);
109
110 for (auto i = 0; i < numFaces; ++i)
111 {
112 const auto index1 = faces[3 * i];
113 const auto index2 = faces[3 * i + 1];
114 const auto index3 = faces[3 * i + 2];
115
116 eigenFaces.emplace_back(index1, index2, index3);
117 }
118
119 free(faces);
120 delete[] vertices;
121
122 if (optimize)
123 return discardUnusedVertices<S, Index>(inputVertices, eigenFaces);
124 else
125 return std::make_pair(inputVertices, eigenFaces);
126}
127
128} // namespace math
129} // namespace dart
130
131#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:50
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:88
Definition BulletCollisionDetector.cpp:65