-
Notifications
You must be signed in to change notification settings - Fork 298
/
IncrSAP.h
276 lines (212 loc) · 10.6 KB
/
IncrSAP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once
#include <sofa/component/collision/detection/algorithm/config.h>
#include <sofa/type/Vec.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/core/collision/BroadPhaseDetection.h>
#include <sofa/core/collision/NarrowPhaseDetection.h>
#include <sofa/core/CollisionElement.h>
#include <sofa/core/CollisionModel.h>
#include <sofa/component/collision/detection/algorithm/EndPoint.h>
#include <sofa/component/collision/detection/algorithm/CollisionPM.h>
#include <sofa/component/collision/geometry/CubeModel.h>
#include <sofa/component/collision/geometry/TriangleModel.h>
#include <sofa/component/collision/geometry/LineModel.h>
#include <sofa/component/collision/geometry/PointModel.h>
#include <sofa/component/collision/geometry/SphereModel.h>
#include <vector>
#include <set>
#include <map>
#include <deque>
namespace sofa::component::collision::detection::algorithm
{
class EndPointID;
/**
* ISAPBox is a simple bounding box. It contains a Cube which contains only one final
* CollisionElement and pointers to min and max EndPoints along the three dimensions. min and max end points
* are respectively min and max coordinates of the cube on a coordinate axis.
* The between end points (_min, _max) and the field cube is that cube is always updated whereas
* _min and _max are stored values of the cube end points at previous time step.
*/
class SOFA_COMPONENT_COLLISION_DETECTION_ALGORITHM_API ISAPBox{
public:
ISAPBox(){}
ISAPBox(collision::geometry::Cube c) : cube(c){}
/**
* Returns true if this overlaps other along the dimension axis.
* For the two following methods, end points are not used but real positions
* of end points of the field cube.
*/
bool endPointsOverlap(const ISAPBox & other,int axis)const;
/**
* Returns true if this overlaps other along the three dimensions.
*/
bool overlaps(const ISAPBox & other,double alarmDist)const;
double squaredDistance(const ISAPBox & other)const;
inline void show()const{
msg_info("IncrSAP") <<"MIN "<<cube.minVect() ;
msg_info("IncrSAP") <<"MAX "<<cube.maxVect() ;
}
inline void showEndPoints()const{
std::stringstream tmp;
tmp<<"MIN ";
for(int i = 0 ; i < 3 ; ++i)
tmp<<min(i).value<<" ";
tmp<<msgendl;
tmp<<"MAX ";
for(int i = 0 ; i < 3 ; ++i)
tmp<<max(i).value<<" ";
msg_info("IncrSAP") << tmp.str() ;
}
/**
* Returns true if the ISAPBox is moving along the dimension axis. i.e., returns true if the value of the end point of dimension axis is different
* from the end point of the field cube (which is the real position of the ISAPBox).
*/
bool moving(int axis,double alarmDist)const;
/**
* The same than the previous one except that this one checks the three dimensions, i.e. it returns true if
* the ISAPBox is moving at least along one dimension.
*/
bool moving(double alarmDist)const;
/**
* Inits _min and _max fiels with endPts. endPts is an one dimension array of EndPointID pointers.
* After this method, the first three end points are the mins in the dimension 0, 1, 2.
* The last three end points are the maxs in the dimension 0, 1, 2.
* Values and IDs of endPts are updated after this method.
*/
void init(int boxID,EndPointID ** endPts);
void update(double alarmDist);
void updatedMin(int dim,EndPointID &end_point,double alarmDist)const;
void updatedMax(int dim,EndPointID &end_point,double alarmDist)const;
void updateMin(int dim,double alarmDist);
void updateMax(int dim,double alarmDist);
bool minMoving(int axis,double alarmDist) const;
bool maxMoving(int axis,double alarmDist) const;
const core::CollisionElementIterator finalElement()const;
EndPointID & min(int dim);
const EndPointID & min(int dim)const;
EndPointID & max(int dim);
const EndPointID & max(int dim)const;
double curMin(int dim) const;
double curMax(int dim)const;
/// Returns true if the endpoints have id ID and if min end points are min and max are max.
/// It checks only the field data.
bool endPointsAreAlright(int ID);
collision::geometry::Cube cube;
EndPointID * _min[3];
EndPointID * _max[3];
static double tolerance;
};
/**
* Implementation of incremental sweep and prune. i.e. collision primitives are stored and updated
* which should speed up the collision detection compared to the DirectSAP.
*/
class SOFA_COMPONENT_COLLISION_DETECTION_ALGORITHM_API IncrSAP :
public core::collision::BroadPhaseDetection,
public core::collision::NarrowPhaseDetection
{
public:
SOFA_CLASS2(IncrSAP, core::collision::BroadPhaseDetection, core::collision::NarrowPhaseDetection);
typedef ISAPBox SAPBox;
typedef std::vector<EndPointID*> EndPointList;
private:
/**
* Returns the dimension number for which one have the greatest variance of end points position.
*/
int greatestVarianceAxis()const;
bool added(core::CollisionModel * cm)const;
bool add(core::CollisionModel * cm);
/**
* Updates values of end points. These values are coordinates of AABB on axis that maximazes the variance for the AABBs.
*/
void updateEndPoints();
/**
* Sets the end points ID, i.e. each end point in the list after this mehod have its position (ID) updated.
*/
void setEndPointsID();
/**
* A counterpart of DirectSAP which is used when a new collision model is added to the IncrSAP. It is more efficient than
* updating every box added to the IncrSAP.
*/
void boxPrune();
/**
* When there is no added collision model, one update only the moving boxes and in the same time, the collisions.
*/
void updateMovingBoxes();
/**
* Checks that boxes whose IDs are boxID1 and boxID2 are in collision, and add it to the list of collisions.
*/
void addIfCollide(int boxID1,int boxID2);
/**
* Checks that boxes whose IDs are boxID1 and boxID2 are in collision along axes axis1 and axis2, and add it to the list of collisions.
*/
void addIfCollide(int boxID1,int boxID2,int axis1,int axis2);
void removeCollision(int a,int b);
void reinitDetection();
/**
* Used in initialisation of IncrSAP. It clears all the IncrSAP fields.
*/
void purge();
SOFA_ATTRIBUTE_DEPRECATED__REMOVE_BDRAW_DATA()
core::objectmodel::lifecycle::DeprecatedData bDraw {this, "v24.06", "v24.12", "draw", "This Data was not used"};
SOFA_ATTRIBUTE_DEPRECATED__RENAME_DATA()
Data< type::fixed_array<type::Vec3,2> > box; ///< if not empty, objects that do not intersect this bounding-box will be ignored
Data< type::fixed_array<type::Vec3,2> > d_box; ///< if not empty, objects that do not intersect this bounding-box will be ignored
collision::geometry::CubeCollisionModel::SPtr boxModel;
std::vector<ISAPBox> _boxes;
EndPointList _end_points[3];
CollidingPM _colliding_elems;
// The following methods are used when updating end points in the end point lists, it updates in the same time the collisions.
void moveMinForward(int dim,EndPointID * cur_end_point,EndPointList::iterator & it,EndPointList::iterator & next_it);
void moveMaxForward(int dim,EndPointID * cur_end_point,EndPointList::iterator & it,EndPointList::iterator & next_it);
void moveMinBackward(int dim,EndPointID * cur_end_point,EndPointList::iterator & it,EndPointList::iterator & prev_it);
void moveMaxBackward(int dim,EndPointID * cur_end_point,EndPointList::iterator & it,EndPointList::iterator & prev_it);
static bool assertion_order(EndPointList::iterator it,EndPointList::iterator begin,EndPointList::iterator end);
static bool assertion_list_order(EndPointList::iterator begin_it,const EndPointList::iterator & end_it);
static bool assertion_superior(EndPointList::iterator begin_it,const EndPointList::iterator & end_it,EndPoint* point);
static bool assertion_inferior(EndPointList::iterator begin_it,const EndPointList::iterator & end_it,EndPoint* point);
bool assertion_end_points_sorted()const;
//EndPointID & findEndPoint(int dim,int data);
int _cur_axis;
bool _nothing_added;
double _alarmDist;
double _alarmDist_d2;
std::set<core::CollisionModel*> collisionModels;
protected:
IncrSAP();
~IncrSAP() override;
public:
SOFA_ATTRIBUTE_DEPRECATED__REMOVE_BDRAW_DATA()
void setDraw(bool) {}
void init() override;
void reinit() override;
void addCollisionModel (core::CollisionModel *cm) override;
// Unuseful methods because all is done in addCollisionModel
void addCollisionPair (const std::pair<core::CollisionModel*, core::CollisionModel*>& ) override {}
void addCollisionPairs (const type::vector<std::pair<core::CollisionModel*, core::CollisionModel*> >&) override {}
void beginNarrowPhase() override;
inline bool needsDeepBoundingTree()const override {return false;}
void showEndPoints()const;
void showBoxes()const;
};
} // namespace sofa::component::collision::detection::algorithm