coal  3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
geometric_shapes.hxx
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2015, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
38 #ifndef COAL_GEOMETRIC_SHAPES_HXX
39 #define COAL_GEOMETRIC_SHAPES_HXX
40 
41 #include "coal/shape/convex.h"
44 
45 #include <iostream>
46 
47 namespace coal {
48 
49 template <typename IndexType>
51  COAL_THROW_PRETTY("Uknown vertex index type for ConvexBase.",
52  std::runtime_error);
53  return NODE_COUNT;
54 }
55 
56 template <>
58  return GEOM_CONVEX16;
59 }
60 
61 template <>
63  return GEOM_CONVEX32;
64 }
65 
66 template <typename IndexType>
68  std::shared_ptr<std::vector<Vec3s>> points_, unsigned int num_points_) {
69  this->points = points_;
70  this->num_points = num_points_;
71  COAL_ASSERT(this->points->size() == this->num_points,
72  "The number of points is not consistent with the size of the "
73  "points vector",
74  std::logic_error);
75  this->num_normals_and_offsets = 0;
76  this->normals.reset();
77  this->offsets.reset();
78  this->computeCenter();
79 }
80 
81 template <typename IndexType>
82 void ConvexBaseTpl<IndexType>::set(std::shared_ptr<std::vector<Vec3s>> points_,
83  unsigned int num_points_) {
84  initialize(points_, num_points_);
85 }
86 
87 template <typename IndexType>
89  const ConvexBaseTpl& other) {
90  if (this != &other) {
91  // Copy the base
92  this->base() = other.base();
93 
94  // Shallow copy the rest of the data
95  this->points = other.points;
96  this->num_points = other.num_points;
97  this->normals = other.normals;
98  this->offsets = other.offsets;
99  this->num_normals_and_offsets = other.num_normals_and_offsets;
100  this->neighbors = other.neighbors;
101  this->nneighbors_ = other.nneighbors_;
102  this->center = other.center;
103  this->support_warm_starts = other.support_warm_starts;
104  }
105 
106  return *this;
107 }
108 
109 template <typename IndexType>
110 template <typename OtherIndexType>
113  if (source == nullptr || copy == nullptr) {
114  return;
115  }
116 
117  // Copy the base
118  copy->base() = source->base();
119 
120  // Copy the non-templated data
121  if (source->points != nullptr) {
122  copy->points.reset(new std::vector<Vec3s>(*source->points));
123  } else {
124  copy->points.reset();
125  assert(source->num_points == 0);
126  }
127  copy->num_points = source->num_points;
128 
129  if (source->normals != nullptr) {
130  copy->normals.reset(new std::vector<Vec3s>(*source->normals));
131  } else {
132  copy->normals.reset();
133  assert(source->num_normals_and_offsets == 0);
134  }
135  if (source->offsets != nullptr) {
136  copy->offsets.reset(new std::vector<Scalar>(*source->offsets));
137  } else {
138  copy->offsets.reset();
139  assert(source->num_normals_and_offsets == 0);
140  }
142 
143  copy->center = source->center;
144  copy->support_warm_starts =
145  source->support_warm_starts.template cast<OtherIndexType>();
146 
147  // Convert neighbors to new type
148  if (source->points->size() >=
149  (std::size_t)(std::numeric_limits<OtherIndexType>::max())) {
151  "The source has more points than the max of OtherIndexType.",
152  std::runtime_error);
153  }
154 
155  if (source->nneighbors_ != nullptr) {
156  const std::vector<IndexType>& source_nneighbors = *(source->nneighbors_);
157  copy->nneighbors_.reset(
158  new std::vector<OtherIndexType>(source_nneighbors.size()));
159  std::vector<OtherIndexType>& copy_nneighbors = *(copy->nneighbors_);
160  for (std::size_t i = 0; i < source_nneighbors.size(); ++i) {
161  copy_nneighbors[i] = OtherIndexType(source_nneighbors[i]);
162  }
163  } else {
164  copy->nneighbors_.reset();
165  }
166 
167  if (source->neighbors != nullptr) {
168  typedef typename ConvexBaseTpl<OtherIndexType>::Neighbors OtherNeighbors;
169  assert(source->neighbors->size() == source->points->size());
170  const std::vector<Neighbors>& source_neighbors = *(source->neighbors);
171  copy->neighbors.reset(
172  new std::vector<OtherNeighbors>(source_neighbors.size()));
173  std::vector<OtherNeighbors>& copy_neighbors = *(copy->neighbors);
174  for (std::size_t i = 0; i < source_neighbors.size(); ++i) {
175  copy_neighbors[i].count = source_neighbors[i].count;
176  copy_neighbors[i].begin_id = OtherIndexType(source_neighbors[i].begin_id);
177  }
178  } else {
179  copy->neighbors.reset();
180  }
181 }
182 
183 template <typename IndexType>
185  center.setZero();
186  const std::vector<Vec3s>& points_ = *points;
187  for (std::size_t i = 0; i < num_points; ++i)
188  center += points_[i]; // TODO(jcarpent): vectorization
189  center /= Scalar(num_points);
190 }
191 
192 // forward declaration for ConvexBase
193 template <typename BV, typename S>
194 void computeBV(const S& s, const Transform3s& tf, BV& bv);
195 
196 template <typename IndexType>
198  computeBV<AABB, ConvexBaseTpl<IndexType>>(*this, Transform3s(), aabb_local);
199  const Scalar ssr = this->getSweptSphereRadius();
200  if (ssr > 0) {
201  aabb_local.min_ -= Vec3s::Constant(ssr);
202  aabb_local.max_ += Vec3s::Constant(ssr);
203  }
204  aabb_center = aabb_local.center();
205  aabb_radius = (aabb_local.min_ - aabb_center).norm();
206 }
207 
208 // Reorders `tri` such that the dot product between the normal of triangle and
209 // the vector `triangle barycentre - convex_tri.center` is positive.
210 template <typename IndexType>
212  TriangleTpl<IndexType>& tri) {
213  Vec3s p0, p1, p2;
214  p0 = (*(convex_tri->points))[tri[0]];
215  p1 = (*(convex_tri->points))[tri[1]];
216  p2 = (*(convex_tri->points))[tri[2]];
217 
218  Vec3s barycentre_tri, center_barycenter;
219  barycentre_tri = (p0 + p1 + p2) / 3;
220  center_barycenter = barycentre_tri - convex_tri->center;
221 
222  Vec3s edge_tri1, edge_tri2, n_tri;
223  edge_tri1 = p1 - p0;
224  edge_tri2 = p2 - p1;
225  n_tri = edge_tri1.cross(edge_tri2);
226 
227  if (center_barycenter.dot(n_tri) < 0) {
228  tri.set(tri[1], tri[0], tri[2]);
229  }
230 }
231 
232 } // namespace coal
233 
234 #endif // COAL_GEOMETRIC_SHAPES_HXX
Base for convex polytope.
Definition: geometric_shapes.h:691
unsigned int num_points
Definition: geometric_shapes.h:797
std::shared_ptr< std::vector< Vec3s > > normals
An array of the normals of the polygon.
Definition: geometric_shapes.h:800
SupportWarmStartPolytope support_warm_starts
Support warm start polytopes.
Definition: geometric_shapes.h:822
std::shared_ptr< std::vector< Vec3s > > points
An array of the points of the polygon.
Definition: geometric_shapes.h:796
Vec3s center
center of the convex polytope, this is used for collision: center is guaranteed in the internal of th...
Definition: geometric_shapes.h:813
std::shared_ptr< std::vector< Neighbors > > neighbors
Neighbors of each vertex. It is an array of size num_points. For each vertex, it contains the number ...
Definition: geometric_shapes.h:809
NODE_TYPE getNodeType() const
Get node type: a convex polytope.
Definition: geometric_shapes.hxx:50
std::shared_ptr< std::vector< Scalar > > offsets
An array of the offsets to the normals of the polygon. Note: there are as many offsets as normals.
Definition: geometric_shapes.h:803
Base & base()
Cast ConvexBaseTpl to ShapeBase. This method should never be marked as virtual.
Definition: geometric_shapes.h:727
unsigned int num_normals_and_offsets
Definition: geometric_shapes.h:804
std::shared_ptr< std::vector< IndexType > > nneighbors_
Array of indices of the neighbors of each vertex. Since we don't know a priori the number of neighbor...
Definition: geometric_shapes.h:861
Convex polytope.
Definition: convex.h:50
Simple transform class used locally by InterpMotion.
Definition: transform.h:55
Triangle with 3 indices for points.
Definition: data_types.h:122
void set(IndexType p1, IndexType p2, IndexType p3)
Set the vertex indices of the triangle.
Definition: data_types.h:170
#define COAL_ASSERT(check, message, exception)
Definition: fwd.hh:82
#define COAL_THROW_PRETTY(message, exception)
Definition: fwd.hh:64
@ GEOM_CONVEX32
Definition: collision_object.h:80
@ NODE_COUNT
Definition: collision_object.h:89
@ GEOM_CONVEX16
Definition: collision_object.h:79
Main namespace.
Definition: broadphase_bruteforce.h:44
void computeBV(const S &s, const Transform3s &tf, BV &bv)
calculate a bounding volume for a shape in a specific configuration
Definition: geometric_shapes_utility.h:82
void reorderTriangle(const ConvexTpl< TriangleTpl< IndexType >> *convex_tri, TriangleTpl< IndexType > &tri)
Definition: geometric_shapes.hxx:211
NODE_TYPE
traversal node type: bounding volume (AABB, OBB, RSS, kIOS, OBBRSS, KDOP16, KDOP18,...
Definition: collision_object.h:64
Eigen::Matrix< Scalar, 3, 1 > Vec3s
Definition: data_types.h:70
double Scalar
Definition: data_types.h:68
Definition: geometric_shapes.h:638
bool initialize(MeshCollisionTraversalNode< BV, RelativeTransformationIsIdentity > &node, BVHModel< BV > &model1, Transform3s &tf1, BVHModel< BV > &model2, Transform3s &tf2, CollisionResult &result, bool use_refit=false, bool refit_bottomup=false)
Initialize traversal node for collision between two meshes, given the current transforms.
Definition: traversal_node_setup.h:467