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