pinocchio  UNKNOWN
multibody/geometry.hxx
1 //
2 // Copyright (c) 2015-2017 CNRS
3 //
4 // This file is part of Pinocchio
5 // Pinocchio is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // Pinocchio is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // Pinocchio If not, see
16 // <http://www.gnu.org/licenses/>.
17 
18 #ifndef __se3_geometry_hxx__
19 #define __se3_geometry_hxx__
20 
21 #include <iostream>
22 #include <map>
23 #include <list>
24 #include <utility>
25 
27 
28 namespace se3
29 {
30  inline GeometryData::GeometryData(const GeometryModel & modelGeom)
31  : oMg(modelGeom.ngeoms)
32 
33 #ifdef WITH_HPP_FCL
34  , activeCollisionPairs(modelGeom.collisionPairs.size(), true)
35  , distanceRequest (true, 0, 0, fcl::GST_INDEP)
36  , distanceResults(modelGeom.collisionPairs.size())
37  , collisionRequest (1, false, false, 1, false, true, fcl::GST_INDEP)
38  , collisionResults(modelGeom.collisionPairs.size())
39  , radius()
40  , collisionPairIndex(0)
41  , innerObjects()
42  , outerObjects()
43  {
44  collisionObjects.reserve(modelGeom.geometryObjects.size());
45  BOOST_FOREACH( const GeometryObject & geom, modelGeom.geometryObjects)
46  { collisionObjects.push_back
47  (fcl::CollisionObject(geom.fcl)); }
48  fillInnerOuterObjectMaps(modelGeom);
49  }
50 #else
51  {}
52 #endif // WITH_HPP_FCL
53 
55  const Model & model,
56  const bool autofillJointParent)
57  {
58  // TODO reenable when relevant: assert( (object.parentFrame != -1) || (object.parentJoint != -1) );
59 
60  if( autofillJointParent )
61  // TODO: this might be automatically done for some default value of parentJoint (eg ==-1)
62  object.parentJoint = model.frames[object.parentFrame].parent;
63 
64  assert( //TODO: reenable when relevant (object.parentFrame == -1) ||
65  (model.frames[object.parentFrame].type == se3::BODY) );
66  assert( //TODO: reenable when relevant (object.parentFrame == -1) ||
67  (model.frames[object.parentFrame].parent == object.parentJoint) );
68 
69  GeomIndex idx = (GeomIndex) (ngeoms ++);
70  geometryObjects.push_back(object);
71  return idx;
72  }
73 
74  inline GeomIndex GeometryModel::getGeometryId(const std::string & name) const
75  {
76 
78  geometryObjects.end(),
79  boost::bind(&GeometryObject::name, _1) == name
80  );
81  return GeomIndex(it - geometryObjects.begin());
82  }
83 
84 
85 
86  inline bool GeometryModel::existGeometryName(const std::string & name) const
87  {
88  return std::find_if(geometryObjects.begin(),
89  geometryObjects.end(),
90  boost::bind(&GeometryObject::name, _1) == name) != geometryObjects.end();
91  }
92 
93 
94  inline const std::string& GeometryModel::getGeometryName(const GeomIndex index) const
95  {
96  assert( index < (GeomIndex)geometryObjects.size() );
97  return geometryObjects[index].name;
98  }
99 
100 
101  //
102  // @brief Associate a GeometryObject of type COLLISION to a joint's inner objects list
103  //
104  // @param[in] joint Index of the joint
105  // @param[in] inner_object Index of the GeometryObject that will be an inner object
106  //
107  // inline void GeometryModel::addInnerObject(const JointIndex joint_id, const GeomIndex inner_object)
108  // {
109  // if (std::find(innerObjects[joint_id].begin(),
110  // innerObjects[joint_id].end(),
111  // inner_object) == innerObjects[joint_id].end())
112  // innerObjects[joint_id].push_back(inner_object);
113  // else
114  // std::cout << "inner object already added" << std::endl;
115  // }
116 
117  //
118  // @brief Associate a GeometryObject of type COLLISION to a joint's outer objects list
119  //
120  // @param[in] joint Index of the joint
121  // @param[in] inner_object Index of the GeometryObject that will be an outer object
122  //
123  // inline void GeometryModel::addOutterObject (const JointIndex joint, const GeomIndex outer_object)
124  // {
125  // if (std::find(outerObjects[joint].begin(),
126  // outerObjects[joint].end(),
127  // outer_object) == outerObjects[joint].end())
128  // outerObjects[joint].push_back(outer_object);
129  // else
130  // std::cout << "outer object already added" << std::endl;
131  // }
132 
133 #ifdef WITH_HPP_FCL
135  {
136  innerObjects.clear();
137  outerObjects.clear();
138 
139  for( GeomIndex gid = 0; gid<geomModel.geometryObjects.size(); gid++)
140  innerObjects[geomModel.geometryObjects[gid].parentJoint].push_back(gid);
141 
142  BOOST_FOREACH( const CollisionPair & pair, geomModel.collisionPairs )
143  {
144  outerObjects[geomModel.geometryObjects[pair.first].parentJoint].push_back(pair.second);
145  }
146  }
147 #endif
148 
149  inline std::ostream & operator<< (std::ostream & os, const GeometryModel & geomModel)
150  {
151  os << "Nb geometry objects = " << geomModel.ngeoms << std::endl;
152 
153  for(GeomIndex i=0;i<(GeomIndex)(geomModel.ngeoms);++i)
154  {
155  os << geomModel.geometryObjects[i] <<std::endl;
156  }
157 
158  return os;
159  }
160 
161  inline std::ostream & operator<< (std::ostream & os, const GeometryData & geomData)
162  {
163 #ifdef WITH_HPP_FCL
164  os << "Number of collision pairs = " << geomData.activeCollisionPairs.size() << std::endl;
165 
166  for(PairIndex i=0;i<(PairIndex)(geomData.activeCollisionPairs.size());++i)
167  {
168  os << "Pairs " << i << (geomData.activeCollisionPairs[i]?" active":" inactive") << std::endl;
169  }
170 #else
171  os << "WARNING** Without fcl library, no collision checking or distance computations are possible. Only geometry placements can be computed." << std::endl;
172  os << "Number of geometry objects = " << geomData.oMg.size() << std::endl;
173 #endif
174 
175  return os;
176  }
177 
178 #ifdef WITH_HPP_FCL
179 
181  {
182  assert( (pair.first < ngeoms) && (pair.second < ngeoms) );
183  if (!existCollisionPair(pair)) { collisionPairs.push_back(pair); }
184  }
185 
187  {
189  for (GeomIndex i = 0; i < ngeoms; ++i)
190  {
191  const JointIndex& joint_i = geometryObjects[i].parentJoint;
192  for (GeomIndex j = i+1; j < ngeoms; ++j)
193  {
194  const JointIndex& joint_j = geometryObjects[j].parentJoint;
195  if (joint_i != joint_j)
197  }
198  }
199  }
200 
202  {
203  assert( (pair.first < ngeoms) && (pair.second < ngeoms) );
204 
205  CollisionPairsVector_t::iterator it = std::find(collisionPairs.begin(),
206  collisionPairs.end(),
207  pair);
208  if (it != collisionPairs.end()) { collisionPairs.erase(it); }
209  }
210 
212 
213  inline bool GeometryModel::existCollisionPair (const CollisionPair & pair) const
214  {
215  return (std::find(collisionPairs.begin(),
216  collisionPairs.end(),
217  pair) != collisionPairs.end());
218  }
219 
220  inline PairIndex GeometryModel::findCollisionPair (const CollisionPair & pair) const
221  {
222  CollisionPairsVector_t::const_iterator it = std::find(collisionPairs.begin(),
223  collisionPairs.end(),
224  pair);
225 
226  return (PairIndex) std::distance(collisionPairs.begin(), it);
227  }
228 
229  inline void GeometryData::activateCollisionPair(const PairIndex pairId,const bool flag)
230  {
231  assert( pairId < activeCollisionPairs.size() );
232  activeCollisionPairs[pairId] = flag;
233  }
234 
235  inline void GeometryData::deactivateCollisionPair(const PairIndex pairId)
236  {
237  assert( pairId < activeCollisionPairs.size() );
238  activeCollisionPairs[pairId] = false;
239  }
240 
241 #endif //WITH_HPP_FCL
242 } // namespace se3
243 
245 
246 #endif // ifndef __se3_geometry_hxx__
void activateCollisionPair(const PairIndex pairId, const bool flag=true)
container::aligned_vector< GeometryObject > geometryObjects
Vector of GeometryObjects used for collision computations.
GeomIndex getGeometryId(const std::string &name) const
Return the index of a GeometryObject given by its name.
void addCollisionPair(const CollisionPair &pair)
Add a collision pair into the vector of collision_pairs. The method check before if the given Collisi...
void removeAllCollisionPairs()
Remove all collision pairs from collisionPairs. Same as collisionPairs.clear().
std::vector< bool > activeCollisionPairs
Vector of collision pairs.
container::aligned_vector< Frame > frames
Vector of operational frames registered on the model.
Definition: model.hpp:98
void addAllCollisionPairs()
Add all possible collision pairs.
PINOCCHIO_DEPRECATED const std::string & getGeometryName(const GeomIndex index) const
Get the name of a GeometryObject given by its index.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW std::string name
Name of the geometry object.
Definition: fcl.hpp:92
std::string name(const LieGroupVariant &lg)
Visit a LieGroupVariant to get the name of it.
void fillInnerOuterObjectMaps(const GeometryModel &geomModel)
std::vector< CollisionPair > collisionPairs
Vector of collision pairs.
void deactivateCollisionPair(const PairIndex pairId)
void removeCollisionPair(const CollisionPair &pair)
Remove if exists the CollisionPair from the vector collision_pairs.
bool existCollisionPair(const CollisionPair &pair) const
Check if a collision pair exists in collisionPairs. See also findCollisitionPair(const CollisionPair ...
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Index ngeoms
The number of GeometryObjects.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW container::aligned_vector< se3::SE3 > oMg
Vector gathering the SE3 placements of the geometry objects relative to the world. See updateGeometryPlacements to update the placements.
GeomIndex addGeometryObject(GeometryObject object, const Model &model, const bool autofillJointParent=false)
Add a geometry object to a GeometryModel.
PairIndex findCollisionPair(const CollisionPair &pair) const
Return the index of a given collision pair in collisionPairs.
Specialization of an std::vector with an aligned allocator. This specialization might be used when th...
bool existGeometryName(const std::string &name) const
Check if a GeometryObject given by its name exists.