moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
contact_checker_common.cpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD-2-Clause)
3 *
4 * Copyright (c) 2021, Southwest Research Institute
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *********************************************************************/
31
32/* Author: Jorge Nicho
33 * Author: Levi Armstrong
34 */
35
37
38#include <rclcpp/logger.hpp>
39#include <rclcpp/logging.hpp>
41
43{
45 const std::pair<std::string, std::string>& key, bool found)
46{
47 // add deepest penetration / smallest distance to result
48 if (cdata.req.distance)
49 {
50 if (contact.depth < cdata.res.distance)
51 {
52 cdata.res.distance = contact.depth;
53 }
54 }
55
56 RCLCPP_DEBUG_STREAM(getLogger(), "Contact btw " << key.first << " and " << key.second << " dist: " << contact.depth);
57 // case if pair hasn't a contact yet
58 if (!found)
59 {
60 if (contact.depth <= 0)
61 {
62 cdata.res.collision = true;
63 }
64
65 std::vector<collision_detection::Contact> data;
66
67 // if we don't want contacts we are done here
68 if (!cdata.req.contacts)
69 {
70 if (!cdata.req.distance)
71 {
72 cdata.done = true;
73 }
74 return nullptr;
75 }
76 else
77 {
78 data.reserve(cdata.req.max_contacts_per_pair);
79 data.emplace_back(contact);
80 cdata.res.contact_count++;
81 }
82
83 if (cdata.res.contact_count >= cdata.req.max_contacts)
84 {
85 if (!cdata.req.distance)
86 {
87 cdata.done = true;
88 }
89 }
90
91 if (cdata.req.max_contacts_per_pair == 1u)
92 {
93 cdata.pair_done = true;
94 }
95
96 return &(cdata.res.contacts.insert(std::make_pair(key, data)).first->second.back());
97 }
98 else
99 {
100 std::vector<collision_detection::Contact>& dr = cdata.res.contacts[key];
101 dr.emplace_back(contact);
102 cdata.res.contact_count++;
103
104 if (dr.size() >= cdata.req.max_contacts_per_pair)
105 {
106 cdata.pair_done = true;
107 }
108
109 if (cdata.res.contact_count >= cdata.req.max_contacts)
110 {
111 if (!cdata.req.distance)
112 {
113 cdata.done = true;
114 }
115 }
116
117 return &(dr.back());
118 }
119
120 return nullptr;
121}
122
123int createConvexHull(AlignedVector<Eigen::Vector3d>& vertices, std::vector<int>& faces,
124 const AlignedVector<Eigen::Vector3d>& input, double shrink, double shrinkClamp)
125{
126 vertices.clear();
127 faces.clear();
128
129 btConvexHullComputer conv;
130 btAlignedObjectArray<btVector3> points;
131 points.reserve(static_cast<int>(input.size()));
132 for (const Eigen::Vector3d& v : input)
133 {
134 points.push_back(btVector3(static_cast<btScalar>(v[0]), static_cast<btScalar>(v[1]), static_cast<btScalar>(v[2])));
135 }
136
137 btScalar val = conv.compute(&points[0].getX(), sizeof(btVector3), points.size(), static_cast<btScalar>(shrink),
138 static_cast<btScalar>(shrinkClamp));
139 if (val < 0)
140 {
141 RCLCPP_ERROR(getLogger(), "Failed to create convex hull");
142 return -1;
143 }
144
145 int num_verts = conv.vertices.size();
146 vertices.reserve(static_cast<size_t>(num_verts));
147 for (int i = 0; i < num_verts; ++i)
148 {
149 btVector3& v = conv.vertices[i];
150 vertices.push_back(Eigen::Vector3d(v.getX(), v.getY(), v.getZ()));
151 }
152
153 int num_faces = conv.faces.size();
154 faces.reserve(static_cast<size_t>(3 * num_faces));
155 for (int i = 0; i < num_faces; ++i)
156 {
157 std::vector<int> face;
158 face.reserve(3);
159
160 const btConvexHullComputer::Edge* source_edge = &(conv.edges[conv.faces[i]]);
161 int a = source_edge->getSourceVertex();
162 face.push_back(a);
163
164 int b = source_edge->getTargetVertex();
165 face.push_back(b);
166
167 const btConvexHullComputer::Edge* edge = source_edge->getNextEdgeOfFace();
168 int c = edge->getTargetVertex();
169 face.push_back(c);
170
171 edge = edge->getNextEdgeOfFace();
172 c = edge->getTargetVertex();
173 while (c != a)
174 {
175 face.push_back(c);
176
177 edge = edge->getNextEdgeOfFace();
178 c = edge->getTargetVertex();
179 }
180 faces.push_back(static_cast<int>(face.size()));
181 faces.insert(faces.end(), face.begin(), face.end());
182 }
183
184 return num_faces;
185}
186
187} // namespace collision_detection_bullet
collision_detection::Contact * processResult(ContactTestData &cdata, collision_detection::Contact &contact, const std::pair< std::string, std::string > &key, bool found)
Stores a single contact result in the requested way.
std::vector< T, Eigen::aligned_allocator< T > > AlignedVector
Definition basic_types.h:37
int createConvexHull(AlignedVector< Eigen::Vector3d > &vertices, std::vector< int > &faces, const AlignedVector< Eigen::Vector3d > &input, double shrink=-1, double shrinkClamp=-1)
Create a convex hull from vertices using Bullet Convex Hull Computer.
KeyboardReader input
bool contacts
If true, compute contacts. Otherwise only a binary collision yes/no is reported.
std::size_t max_contacts
Overall maximum number of contacts to compute.
std::size_t max_contacts_per_pair
Maximum number of contacts to compute per pair of bodies (multiple bodies may be in contact at differ...
bool distance
If true, compute proximity distance.
double distance
Closest distance between two bodies.
bool collision
True if collision was found, false otherwise.
std::size_t contact_count
Number of contacts returned.
Definition of a contact point.
double depth
depth (penetration between bodies)
Bundles the data for a collision query.
Definition basic_types.h:59
const collision_detection::CollisionRequest & req
Definition basic_types.h:74
bool done
Indicates if search is finished.
Definition basic_types.h:77
collision_detection::CollisionResult & res
Definition basic_types.h:73
bool pair_done
Indicates if search between a single pair is finished.
Definition basic_types.h:80