DART 6.13.2
Loading...
Searching...
No Matches
CollisionGroup.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, 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_COLLISION_COLLISIONGROUP_HPP_
34#define DART_COLLISION_COLLISIONGROUP_HPP_
35
36#include <unordered_map>
37#include <unordered_set>
38#include <vector>
39
49
50namespace dart {
51namespace collision {
52
54{
55public:
57 CollisionGroup(const CollisionDetectorPtr& collisionDetector);
58 // CollisionGroup also can be created from CollisionDetector::create()
59
61 virtual ~CollisionGroup() = default;
62
65
69
71 void addShapeFrame(const dynamics::ShapeFrame* shapeFrame);
72
74 void addShapeFrames(
75 const std::vector<const dynamics::ShapeFrame*>& shapeFrames);
76
85 template <typename... Others>
87 const dynamics::ShapeFrame* shapeFrame, const Others*... others);
88
90 template <typename... Others>
92 const std::vector<const dynamics::ShapeFrame*>& shapeFrames,
93 const Others*... others);
94
97 template <typename... Others>
99 const CollisionGroup* otherGroup, const Others*... others);
100
103 template <typename... Others>
104 void addShapeFramesOf(
105 const dynamics::BodyNode* bodyNode, const Others*... others);
106
109 template <typename... Others>
110 void addShapeFramesOf(
111 const dynamics::MetaSkeleton* skeleton, const Others*... others);
112
115 void addShapeFramesOf();
116
122 template <typename... Others>
123 void subscribeTo(
124 const dynamics::ConstBodyNodePtr& bodyNode, const Others&... others);
125
131 //
132 // TODO(MXG): Figure out how to determine a version number for MetaSkeletons
133 // so that this function can accept a ConstMetaSkeletonPtr instead of only a
134 // ConstSkeletonPtr.
135 template <typename... Others>
136 void subscribeTo(
137 const dynamics::ConstSkeletonPtr& skeleton, const Others&... others);
138
141 void subscribeTo();
142
147 void removeShapeFrame(const dynamics::ShapeFrame* shapeFrame);
148
151 const std::vector<const dynamics::ShapeFrame*>& shapeFrames);
152
161 template <typename... Others>
163 const dynamics::ShapeFrame* shapeFrame, const Others*... others);
164
166 template <typename... Others>
168 const std::vector<const dynamics::ShapeFrame*>& shapeFrames,
169 const Others*... others);
170
176 template <typename... Others>
178 const CollisionGroup* otherGroup, const Others*... others);
179
185 template <typename... Others>
187 const dynamics::BodyNode* bodyNode, const Others*... others);
188
194 template <typename... Others>
196 const dynamics::MetaSkeleton* skeleton, const Others*... others);
197
200 void removeShapeFramesOf();
201
204
207 template <typename... Others>
208 void unsubscribeFrom(
209 const dynamics::BodyNode* bodyNode, const Others*... others);
210
213 template <typename... Others>
214 void unsubscribeFrom(
215 const dynamics::Skeleton* skeleton, const Others*... others);
216
219 void unsubscribeFrom();
220
222 template <typename... Others>
223 bool isSubscribedTo(
224 const dynamics::BodyNode* bodyNode, const Others*... others);
225
227 template <typename... Others>
228 bool isSubscribedTo(
229 const dynamics::Skeleton* skeleton, const Others*... others);
230
233 bool isSubscribedTo();
234
236 bool hasShapeFrame(const dynamics::ShapeFrame* shapeFrame) const;
237
239 std::size_t getNumShapeFrames() const;
240
242 const dynamics::ShapeFrame* getShapeFrame(std::size_t index) const;
243
245 bool collide(
246 const CollisionOption& option = CollisionOption(false, 1u, nullptr),
247 CollisionResult* result = nullptr);
248
253 bool collide(
254 CollisionGroup* otherGroup,
255 const CollisionOption& option = CollisionOption(false, 1u, nullptr),
256 CollisionResult* result = nullptr);
257
266 double distance(
267 const DistanceOption& option = DistanceOption(false, 0.0, nullptr),
268 DistanceResult* result = nullptr);
269
282 double distance(
283 CollisionGroup* otherGroup,
284 const DistanceOption& option = DistanceOption(false, 0.0, nullptr),
285 DistanceResult* result = nullptr);
286
294 bool raycast(
295 const Eigen::Vector3d& from,
296 const Eigen::Vector3d& to,
298 RaycastResult* result = nullptr);
299
301 void setAutomaticUpdate(bool automatic = true);
302
304 bool getAutomaticUpdate() const;
305
315 void update();
316
323
324protected:
327 void updateEngineData();
328
332 virtual void initializeEngineData() = 0;
333
336
339 const std::vector<CollisionObject*>& collObjects)
340 = 0;
341
344
347
351
352protected:
355 // CollisionGroup shares the ownership of CollisionDetector with other
356 // CollisionGroups created from the same CollisionDetector so that the
357 // CollisionDetector doesn't get destroyed as long as at least one
358 // CollisionGroup is alive.
359
364 struct ObjectInfo final
365 {
368
371
373 std::size_t mLastKnownShapeID;
374
377 std::size_t mLastKnownVersion;
378
385 std::unordered_set<const void*> mSources;
386 };
387
388 using ObjectInfoList = std::vector<std::unique_ptr<ObjectInfo>>;
389
393 // CollisionGroup also shares the ownership of CollisionObjects across other
394 // CollisionGroups for the same reason with above.
395 //
396 // Dev note: This was supposed to be std::map rather than std::vector for
397 // better search performance. The reason we use std::vector is to get
398 // deterministic contact results regardless of the order of CollisionObjects
399 // in this container for FCLCollisionDetector.
400 //
401 // fcl's collision result is dependent on the order of objects in the broad
402 // phase classes. If we use std::map, the orders of element between the
403 // original and copy are not guranteed to be the same as we copy std::map
404 // (e.g., by world cloning).
405
406private:
418 {
419 public:
421 void addShapeFrame(const dynamics::ShapeFrame* shapeFrame);
422
424 void removeShapeFrame(const dynamics::ShapeFrame* shapeFrame);
425
428
432 std::unordered_set<const dynamics::ShapeFrame*> mDeletedFrames;
433
434 protected:
436 void handleDestructionNotification(const common::Subject* subject) override;
437
438 private:
442 std::unordered_map<const common::Subject*, const dynamics::ShapeFrame*>
444 };
445
450 const dynamics::ShapeFrame* shapeFrame, const void* source);
451
455 const dynamics::ShapeFrame* shapeFrame, const void* source);
456
460
464 template <typename Source, typename Child = void>
466 {
468 Source mSource;
469
471 std::size_t mLastKnownVersion;
472
474 std::unordered_map<const dynamics::ShapeFrame*, ObjectInfo*> mObjects;
475
479 {
481 std::size_t mLastKnownVersion;
482
484 std::unordered_set<const dynamics::ShapeFrame*> mFrames;
485
488 explicit ChildInfo(const std::size_t version) : mLastKnownVersion(version)
489 {
490 // Do nothing
491 }
492 };
493
496 std::unordered_map<const Child*, ChildInfo> mChildren;
497
499 CollisionSource(const Source& source, std::size_t lastKnownVersion)
500 : mSource(source), mLastKnownVersion(lastKnownVersion)
501 {
502 // Do nothing
503 }
504 };
505
506 // Convenient typedefs for Skeleton sources
510 = std::unordered_map<const dynamics::MetaSkeleton*, SkeletonSource>;
511
512 // Convenient typedefs for BodyNode sources
515 = std::unordered_map<const dynamics::BodyNode*, BodyNodeSource>;
516
519 bool updateSkeletonSource(SkeletonSources::value_type& entry);
520
523 bool updateBodyNodeSource(BodyNodeSources::value_type& entry);
524
527 bool updateShapeFrame(ObjectInfo* object);
528
531
534
537};
538
539} // namespace collision
540} // namespace dart
541
543
544#endif // DART_COLLISION_COLLISIONGROUP_HPP_
const CollisionOption & option
Collision option of DART.
Definition FCLCollisionDetector.cpp:157
CollisionResult * result
Collision result of DART.
Definition FCLCollisionDetector.cpp:160
std::size_t index
Definition SkelParser.cpp:1673
This class watches when ShapeFrames get deleted so that they can be safely removes from the Collision...
Definition CollisionGroup.hpp:418
void removeShapeFrame(const dynamics::ShapeFrame *shapeFrame)
Remove a shape frame from this observer.
Definition CollisionGroup.cpp:338
void handleDestructionNotification(const common::Subject *subject) override
This will be called each time an observed shape frame is deleted.
Definition CollisionGroup.cpp:352
std::unordered_set< const dynamics::ShapeFrame * > mDeletedFrames
Whenever an observed shape frame gets deleted, its pointer will be added to this set.
Definition CollisionGroup.hpp:432
void addShapeFrame(const dynamics::ShapeFrame *shapeFrame)
Add a shape frame to this observer.
Definition CollisionGroup.cpp:329
std::unordered_map< const common::Subject *, const dynamics::ShapeFrame * > mMap
A map from a subject pointer to its corresponding ShapeFrame pointer.
Definition CollisionGroup.hpp:443
void removeAllShapeFrames()
Remove all shape frames from this observer.
Definition CollisionGroup.cpp:346
Definition CollisionGroup.hpp:54
ShapeFrameObserver mObserver
The object that observes the Shape Frames that this group cares about.
Definition CollisionGroup.hpp:536
virtual void addCollisionObjectToEngine(CollisionObject *object)=0
Add CollisionObject to the collision detection engine.
bool isSubscribedTo()
Return true.
Definition CollisionGroup.cpp:152
void addShapeFramesOf()
Do nothing.
Definition CollisionGroup.cpp:79
bool collide(const CollisionOption &option=CollisionOption(false, 1u, nullptr), CollisionResult *result=nullptr)
Perform collision check within this CollisionGroup.
Definition CollisionGroup.cpp:196
bool mUpdateAutomatically
Set this to true to have this CollisionGroup check for updates automatically.
Definition CollisionGroup.hpp:459
void subscribeTo()
Do nothing.
Definition CollisionGroup.cpp:85
bool updateSkeletonSource(SkeletonSources::value_type &entry)
Internal function called to update a Skeleton source.
Definition CollisionGroup.cpp:431
bool updateBodyNodeSource(BodyNodeSources::value_type &entry)
Internal function called to update a BodyNode source.
Definition CollisionGroup.cpp:556
CollisionDetectorPtr mCollisionDetector
Collision detector.
Definition CollisionGroup.hpp:354
void removeShapeFrames(const std::vector< const dynamics::ShapeFrame * > &shapeFrames)
Remove ShapeFrames from this CollisionGroup.
Definition CollisionGroup.cpp:132
virtual void initializeEngineData()=0
Initialize the collision detection engine data such as broadphase algorithm.
void addShapeFrames(const std::vector< const dynamics::ShapeFrame * > &shapeFrames)
Add ShapeFrames to this CollisionGroup.
Definition CollisionGroup.cpp:71
double distance(const DistanceOption &option=DistanceOption(false, 0.0, nullptr), DistanceResult *result=nullptr)
Get the minimum signed distance between the Shape pairs in this CollisionGroup.
Definition CollisionGroup.cpp:218
SkeletonSources mSkeletonSources
Skeleton sources that this group is subscribed to.
Definition CollisionGroup.hpp:530
void unsubscribeFrom()
Do nothing.
Definition CollisionGroup.cpp:146
bool updateShapeFrame(ObjectInfo *object)
Internal function called to update a ShapeFrame.
Definition CollisionGroup.cpp:619
std::unordered_map< const dynamics::BodyNode *, BodyNodeSource > BodyNodeSources
Definition CollisionGroup.hpp:515
virtual void removeAllCollisionObjectsFromEngine()=0
Remove all the CollisionObjects from the collision detection engine.
void removeDeletedShapeFrames()
Remove any ShapeFrames that have been deleted.
Definition CollisionGroup.cpp:277
ObjectInfo * addShapeFrameImpl(const dynamics::ShapeFrame *shapeFrame, const void *source)
Implementation of addShapeFrame.
Definition CollisionGroup.cpp:360
void removeAllShapeFrames()
Remove all the ShapeFrames in this CollisionGroup.
Definition CollisionGroup.cpp:158
bool raycast(const Eigen::Vector3d &from, const Eigen::Vector3d &to, const RaycastOption &option=RaycastOption(), RaycastResult *result=nullptr)
Performs raycast to this collision group.
Definition CollisionGroup.cpp:240
void addShapeFrame(const dynamics::ShapeFrame *shapeFrame)
Add a ShapeFrame to this CollisionGroup.
Definition CollisionGroup.cpp:65
virtual void updateCollisionGroupEngineData()=0
Update the collision detection engine data such as broadphase algorithm.
void removeShapeFrame(const dynamics::ShapeFrame *shapeFrame)
Remove a ShapeFrame from this CollisionGroup.
Definition CollisionGroup.cpp:91
virtual void addCollisionObjectsToEngine(const std::vector< CollisionObject * > &collObjects)=0
Add CollisionObjects to the collision detection engine.
void updateEngineData()
Update engine data.
Definition CollisionGroup.cpp:320
virtual void removeCollisionObjectFromEngine(CollisionObject *object)=0
Remove CollisionObject from the collision detection engine.
void update()
Check whether this CollisionGroup's subscriptions or any of its objects need an update,...
Definition CollisionGroup.cpp:265
bool getAutomaticUpdate() const
Get whether this CollisionGroup is set to automatically check for updates.
Definition CollisionGroup.cpp:259
virtual ~CollisionGroup()=default
Destructor.
bool hasShapeFrame(const dynamics::ShapeFrame *shapeFrame) const
Return true if this CollisionGroup contains shapeFrame.
Definition CollisionGroup.cpp:167
void setAutomaticUpdate(bool automatic=true)
Set whether this CollisionGroup will automatically check for updates.
Definition CollisionGroup.cpp:253
std::size_t getNumShapeFrames() const
Return number of ShapeFrames added to this CollisionGroup.
Definition CollisionGroup.cpp:179
void removeShapeFramesOf()
Do nothing.
Definition CollisionGroup.cpp:140
std::vector< std::unique_ptr< ObjectInfo > > ObjectInfoList
Definition CollisionGroup.hpp:388
void removeShapeFrameInternal(const dynamics::ShapeFrame *shapeFrame, const void *source)
Internal version of removeShapeFrame.
Definition CollisionGroup.cpp:403
CollisionDetectorPtr getCollisionDetector()
Return collision detection engine associated with this CollisionGroup.
Definition CollisionGroup.cpp:53
ObjectInfoList mObjectInfoList
Information about ShapeFrames and CollisionObjects that have been added to this CollisionGroup.
Definition CollisionGroup.hpp:392
const dynamics::ShapeFrame * getShapeFrame(std::size_t index) const
Get the ShapeFrame corresponding to the given index.
Definition CollisionGroup.cpp:185
BodyNodeSources mBodyNodeSources
BodyNode sources that this group is susbscribed to.
Definition CollisionGroup.hpp:533
std::unordered_map< const dynamics::MetaSkeleton *, SkeletonSource > SkeletonSources
Definition CollisionGroup.hpp:510
Definition CollisionObject.hpp:45
Definition CollisionResult.hpp:53
The Observer class should be inherited by any class that wants to respond in a customized way to the ...
Definition Observer.hpp:52
The Subject class is a base class for any object that wants to report when it gets destroyed.
Definition Subject.hpp:58
BodyNode class represents a single node of the skeleton.
Definition BodyNode.hpp:80
MetaSkeleton is a pure abstract base class that provides a common interface for obtaining data (such ...
Definition MetaSkeleton.hpp:61
Definition ShapeFrame.hpp:192
class Skeleton
Definition Skeleton.hpp:60
TemplateBodyNodePtr is a templated class that enables users to create a reference-counting BodyNodePt...
Definition BodyNodePtr.hpp:110
std::shared_ptr< const CollisionDetector > ConstCollisionDetectorPtr
Definition SmartPointer.hpp:41
std::shared_ptr< CollisionDetector > CollisionDetectorPtr
Definition SmartPointer.hpp:41
std::shared_ptr< CollisionObject > CollisionObjectPtr
Definition SmartPointer.hpp:45
std::shared_ptr< const Skeleton > ConstSkeletonPtr
Definition SmartPointer.hpp:60
Definition BulletCollisionDetector.cpp:60
This is information pertaining to a child of the source.
Definition CollisionGroup.hpp:479
ChildInfo(const std::size_t version)
A constructor that simply accepts a last known version number.
Definition CollisionGroup.hpp:488
std::size_t mLastKnownVersion
Last known version of this child.
Definition CollisionGroup.hpp:481
std::unordered_set< const dynamics::ShapeFrame * > mFrames
Last known set of frames attached to this child.
Definition CollisionGroup.hpp:484
This struct is used to store sources of ShapeFrames that the CollisionGroup is subscribed to,...
Definition CollisionGroup.hpp:466
std::unordered_map< const Child *, ChildInfo > mChildren
The last known versions of the children related to this source.
Definition CollisionGroup.hpp:496
std::unordered_map< const dynamics::ShapeFrame *, ObjectInfo * > mObjects
The set of objects that pertain to this source.
Definition CollisionGroup.hpp:474
Source mSource
The source of ShapeFrames.
Definition CollisionGroup.hpp:468
CollisionSource(const Source &source, std::size_t lastKnownVersion)
Constructor.
Definition CollisionGroup.hpp:499
std::size_t mLastKnownVersion
The last known version of that source.
Definition CollisionGroup.hpp:471
Information on a collision object belonging to this group.
Definition CollisionGroup.hpp:365
std::size_t mLastKnownVersion
The last known version of the last known shape that was held by the shape frame.
Definition CollisionGroup.hpp:377
std::unordered_set< const void * > mSources
The set of all sources that indicate that this object should be in this group.
Definition CollisionGroup.hpp:385
CollisionObjectPtr mObject
The CollisionObject that was generated by the CollisionDetector.
Definition CollisionGroup.hpp:370
const dynamics::ShapeFrame * mFrame
The ShapeFrame for this object.
Definition CollisionGroup.hpp:367
std::size_t mLastKnownShapeID
The ID of that last known shape that was held by the shape frame.
Definition CollisionGroup.hpp:373
Definition CollisionOption.hpp:45
Definition DistanceOption.hpp:45
Definition DistanceResult.hpp:47
Definition RaycastOption.hpp:43
Definition RaycastResult.hpp:64