DART 6.11.1
Loading...
Searching...
No Matches
TriMesh-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_TRIMESH_IMPL_HPP_
34#define DART_MATH_DETAIL_TRIMESH_IMPL_HPP_
35
36#include "dart/math/TriMesh.hpp"
37
38#include <Eigen/Geometry>
39
41
42namespace dart {
43namespace math {
44
45//==============================================================================
46template <typename S>
48{
49 // Do nothing
50}
51
52//==============================================================================
53template <typename S>
55 const Vertices& vertices, const Triangles& triangles)
56{
57 clear();
58
59 this->mVertices = vertices;
60 mTriangles = triangles;
61}
62
63//==============================================================================
64template <typename S>
66{
67 computeTriangleNormals();
68
69 this->mVertexNormals.clear();
70 this->mVertexNormals.resize(this->mVertices.size(), Vector3::Zero());
71
72 for (auto i = 0u; i < mTriangles.size(); ++i)
73 {
74 auto& triangle = mTriangles[i];
75 this->mVertexNormals[triangle[0]] += mTriangleNormals[i];
76 this->mVertexNormals[triangle[1]] += mTriangleNormals[i];
77 this->mVertexNormals[triangle[2]] += mTriangleNormals[i];
78 }
79
80 this->normalizeVertexNormals();
81}
82
83//==============================================================================
84template <typename S>
86{
87 return !mTriangles.empty();
88}
89
90//==============================================================================
91template <typename S>
93{
94 return hasTriangles() && mTriangles.size() == mTriangleNormals.size();
95}
96
97//==============================================================================
98template <typename S>
100{
101 return mTriangles;
102}
103
104//==============================================================================
105template <typename S>
107{
108 return mTriangleNormals;
109}
110
111//==============================================================================
112template <typename S>
114{
115 mTriangles.clear();
116 mTriangleNormals.clear();
117 Base::clear();
118}
119
120//==============================================================================
121template <typename S>
123{
124 return (TriMesh(*this) += other);
125}
126
127//==============================================================================
128template <typename S>
130{
131 if (other.isEmpty())
132 return *this;
133
134 const auto oldNumVertices = this->mVertices.size();
135 const auto oldNumTriangles = mTriangles.size();
136
137 Base::operator+=(other);
138
139 // Insert triangle normals if both meshes have normals. Otherwise, clean the
140 // triangle normals.
141 if ((!hasTriangles() || hasTriangleNormals()) && other.hasTriangleNormals())
142 {
143 mTriangleNormals.insert(
144 mTriangleNormals.end(),
145 other.mTriangleNormals.begin(),
146 other.mTriangleNormals.end());
147 }
148 else
149 {
150 mTriangleNormals.clear();
151 }
152
153 const Triangle offset = Triangle::Constant(oldNumVertices);
154 mTriangles.resize(mTriangles.size() + other.mTriangles.size());
155 for (auto i = 0u; i < other.mTriangles.size(); ++i)
156 {
157 mTriangles[i + oldNumTriangles] = other.mTriangles[i] + offset;
158 }
159
160 return *this;
161}
162
163//==============================================================================
164template <typename S>
165std::shared_ptr<TriMesh<S>> TriMesh<S>::generateConvexHull(bool optimize) const
166{
167 auto triangles = Triangles();
168 auto vertices = Vertices();
169 std::tie(vertices, triangles)
170 = computeConvexHull3D<S, Index>(this->mVertices, optimize);
171
172 auto mesh = std::make_shared<TriMesh<S>>();
173 mesh->setTriangles(vertices, triangles);
174
175 return mesh;
176}
177
178//==============================================================================
179template <typename S>
181{
182 mTriangleNormals.resize(mTriangles.size());
183
184 for (auto i = 0u; i < mTriangles.size(); ++i)
185 {
186 auto& triangle = mTriangles[i];
187 const Vector3 v01
188 = this->mVertices[triangle[1]] - this->mVertices[triangle[0]];
189 const Vector3 v02
190 = this->mVertices[triangle[2]] - this->mVertices[triangle[0]];
191 mTriangleNormals[i] = v01.cross(v02);
192 }
193
194 normalizeTriangleNormals();
195}
196
197//==============================================================================
198template <typename S>
200{
201 for (auto& normal : mTriangleNormals)
202 {
203 normal.normalize();
204 }
205}
206
207} // namespace math
208} // namespace dart
209
210#endif // DART_MATH_DETAIL_TRIMESH_IMPL_HPP_
bool isEmpty() const
Returns true if the mesh has no vertices.
Definition Mesh-impl.hpp:86
This class represents triangle meshes.
Definition TriMesh.hpp:46
std::vector< Triangle > Triangles
Definition TriMesh.hpp:56
TriMesh & operator+=(const TriMesh &other)
Addition assignment operator.
Definition TriMesh-impl.hpp:129
void clear() override
Clears all the data in the trimesh.
Definition TriMesh-impl.hpp:113
const Triangles & getTriangles() const
Returns the triangles of the mesh.
Definition TriMesh-impl.hpp:99
TriMesh operator+(const TriMesh &other) const
Addition operator.
Definition TriMesh-impl.hpp:122
void computeTriangleNormals()
Computes triangle normals.
Definition TriMesh-impl.hpp:180
void normalizeTriangleNormals()
Normalizes triangle normals.
Definition TriMesh-impl.hpp:199
Triangles mTriangles
Triangle indices of the mesh.
Definition TriMesh.hpp:105
void setTriangles(const Vertices &vertices, const Triangles &triangles)
Sets vertices and triangles.
Definition TriMesh-impl.hpp:54
const Normals & getTriangleNormals() const
Returns the triangle normals of the mesh.
Definition TriMesh-impl.hpp:106
typename Base::Vertices Vertices
Definition TriMesh.hpp:54
bool hasTriangles() const
Returns true if the mesh contains triangles.
Definition TriMesh-impl.hpp:85
std::shared_ptr< TriMesh< S > > generateConvexHull(bool optimize=true) const
Generates a convex hull that encloses the trimesh.
Definition TriMesh-impl.hpp:165
typename Base::Normals Normals
Definition TriMesh.hpp:55
TriMesh()
Default constructor.
Definition TriMesh-impl.hpp:47
Normals mTriangleNormals
Triangle normals of the mesh.
Definition TriMesh.hpp:108
bool hasTriangleNormals() const
Returns true if the mesh contains triangle normals.
Definition TriMesh-impl.hpp:92
void computeVertexNormals()
Computes vertex normals.
Definition TriMesh-impl.hpp:65
typename Base::Vector3 Vector3
Definition TriMesh.hpp:52
Eigen::Matrix< Index, 3, 1 > Triangle
Definition TriMesh.hpp:53
Definition BulletCollisionDetector.cpp:65