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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
// Copyright 2006-2007 Nanorex, Inc. See LICENSE file for details.
/*
Name: collisiondetector.h
Author: Oleksandr Shevchenko
Description: class for collisiondetector representation
*/
#if !defined(COLLISIONDETECTOR_INCLUDED)
#define COLLISIONDETECTOR_INCLUDED
#include "hierarchy.h"
#include "intersection.h"
class CollisionDetector
{
public:
//------------------------------------------------------------------------
// Constructor
inline CollisionDetector();
//------------------------------------------------------------------------
// Destructor
virtual ~CollisionDetector();
//------------------------------------------------------------------------
// AllContacts()
//
// set flag for all contacts
//
inline void AllContacts();
//------------------------------------------------------------------------
// CalculatePoints()
//
// calculate intersection points
//
void CalculatePoints();
//------------------------------------------------------------------------
// CheckCollision()
//
// check for collision between two trees
//
void CheckCollision(
Hierarchy * h0,
Hierarchy * h1);
//------------------------------------------------------------------------
// Clear()
//
// clear collision elements
//
inline void Clear();
//------------------------------------------------------------------------
// CollisionCount()
//
// get number of collision
//
inline int CollisionCount() const;
//------------------------------------------------------------------------
// EntityCount()
//
// get number of entities
//
inline int EntityCount() const;
//------------------------------------------------------------------------
// FirstContact()
//
// set flag for first contact
//
inline void FirstContact();
//------------------------------------------------------------------------
// GetEntity()
//
// return reference to entity
//
inline int GetEntity(
int i) const;
//------------------------------------------------------------------------
// GetPoints()
//
// pointer to collision triple
//
inline Triple * GetPoints() const;
//------------------------------------------------------------------------
// PairCount()
//
// get number of pair
//
inline int PairCount() const;
//------------------------------------------------------------------------
// Select()
//
// select collision elements
//
void Select(int c) const;
//------------------------------------------------------------------------
// VolumeCount()
//
// get number of volumes
//
inline int VolumeCount() const;
private:
//------------------------------------------------------------------------
// mH0
Hierarchy * mH0; // pointer to hierarchy
//------------------------------------------------------------------------
// mH1
Hierarchy * mH1; // pointer to hierarchy
//------------------------------------------------------------------------
// mEntities
Container<int> mEntities; // container for entities in the box
//------------------------------------------------------------------------
// mPoints
Triple * mPoints; // array for intersection points
//------------------------------------------------------------------------
// mRm
RotationMatrix mRm; // rotation matrix
//------------------------------------------------------------------------
// mFrm
RotationMatrix mFrm; // rotation matrix (absolute value)
//------------------------------------------------------------------------
// mTv
Triple mTv; // translation vector
//------------------------------------------------------------------------
// mDelta
Triple mDelta; // for boxes adjustment
//------------------------------------------------------------------------
// mVolume
int mVolume; // number of cheked boxes
//------------------------------------------------------------------------
// mEntity
int mEntity; // number of cheked trias
//------------------------------------------------------------------------
// mCollision
int mCollision; // number of collision entities
//------------------------------------------------------------------------
// mPair
int mPair; // number of pair
//------------------------------------------------------------------------
// mFirst
int mFirst; // flag for first contact
//------------------------------------------------------------------------
// CalculateTransformation()
//
// calculate rotation matrix and absolute value
// and translation vector
//
inline void CalculateTransformation();
//------------------------------------------------------------------------
// CheckCollision()
//
// recursive check for collision
//
void CheckCollision(
BoxTree * bt0,
BoxTree * bt1);
//------------------------------------------------------------------------
// Collision()
//
// intersect trias in boxes
//
int Collision(
BoxTree * bt0,
BoxTree * bt1);
//------------------------------------------------------------------------
// Collision()
//
// intersection detection
//
inline int Collision(
int ni,
Triple * a,
int nj,
Triple * b,
Triple * p);
//------------------------------------------------------------------------
// IsOverlap()
//
// check for boxes overlap
//
inline int IsOverlap(
const Triple & a,
const Triple & b,
const Triple & ac,
const Triple & bc);
//------------------------------------------------------------------------
// SplitQuad()
//
// divide quad onto 2 trias
//
inline void SplitQuad(
Triple * a,
Triple * a1,
Triple * a2);
};
//----------------------------------------------------------------------------
// Constructor
inline CollisionDetector::CollisionDetector():
mPoints( // array for intersection points
0),
mVolume( // number of cheked boxes
0),
mEntity( // number of cheked trias
0),
mCollision( // number of collision entities
0),
mPair( // number of pair
0),
mFirst( // flag for first contact
0)
{
}
//----------------------------------------------------------------------------
// AllContacts()
inline void CollisionDetector::AllContacts()
{
mFirst = 0;
}
//----------------------------------------------------------------------------
// CalculateTransformation()
inline void CollisionDetector::CalculateTransformation()
{
RotationMatrix * m0 = mH0->Matrix();
RotationMatrix * m1 = mH1->Matrix();
Triple * v0 = mH0->Vector();
Triple * v1 = mH1->Vector();
double EPS = 0.00000001;
if (m0 && m1)
{
mRm.Px() = Triple(m1->Px()%m0->Px(),m1->Py()%m0->Px(),m1->Pz()%m0->Px());
mRm.Py() = Triple(m1->Px()%m0->Py(),m1->Py()%m0->Py(),m1->Pz()%m0->Py());
mRm.Pz() = Triple(m1->Px()%m0->Pz(),m1->Py()%m0->Pz(),m1->Pz()%m0->Pz());
}
else if (m0)
{
mRm.Px() = Triple(m0->Xx(),m0->Xy(),m0->Xz());
mRm.Py() = Triple(m0->Yx(),m0->Yy(),m0->Yz());
mRm.Pz() = Triple(m0->Zx(),m0->Zy(),m0->Zz());
}
else if (m1)
{
mRm.Px() = Triple(m1->Xx(),m1->Yx(),m1->Zx());
mRm.Py() = Triple(m1->Xy(),m1->Yy(),m1->Zy());
mRm.Pz() = Triple(m1->Xz(),m1->Yz(),m1->Zz());
}
if (m0 || m1)
{
mFrm.Px() = Triple(fabs(mRm.Xx())+EPS,fabs(mRm.Xy())+EPS,fabs(mRm.Xz())+EPS);
mFrm.Py() = Triple(fabs(mRm.Yx())+EPS,fabs(mRm.Yy())+EPS,fabs(mRm.Yz())+EPS);
mFrm.Pz() = Triple(fabs(mRm.Zx())+EPS,fabs(mRm.Zy())+EPS,fabs(mRm.Zz())+EPS);
}
if (m0)
{
mTv = (*m0) * (*v1 - *v0);
}
else
{
mTv = *v1 - *v0;
}
}
//----------------------------------------------------------------------------
// Clear()
inline void CollisionDetector::Clear()
{
if (mPoints) delete [] mPoints;
mPoints = 0;
mEntities.Empty();
mVolume = 0;
mEntity = 0;
mCollision = 0;
mPair = 0;
}
//----------------------------------------------------------------------------
// Collision()
inline int CollisionDetector::Collision(
int ni,
Triple * a,
int nj,
Triple * b,
Triple * p)
{
Intersection inter;
int count = 0;
int i,i1,i2,i3,i4;
Triple a1[3],a2[3];
Triple b1[3],b2[3];
switch (ni)
{
case 3:
switch (nj)
{
case 3:
i = inter.Collision(a,b,p);
return (i);
case 4:
SplitQuad(b,b1,b2);
i3 = inter.Collision(a,b1,p);
if (p) count += i3;
i4 = inter.Collision(a,b2,p+2*count);
return i3+i4;
default:
return 0;
}
case 4:
switch (nj)
{
case 3:
SplitQuad(a,a1,a2);
i1 = inter.Collision(a1,b,p);
if (p) count += i1;
i2 = inter.Collision(a2,b,p+2*count);
return i1+i2;
case 4:
SplitQuad(a,a1,a2);
SplitQuad(b,b1,b2);
i1 = inter.Collision(a1,b1,p);
if (p) count += i1;
i2 = inter.Collision(a1,b2,p+2*count);
if (p) count += i2;
i3 = inter.Collision(a2,b1,p+2*count);
if (p) count += i3;
i4 = inter.Collision(a2,b2,p+2*count);
return i1+i2+i3+i4;
default:
return 0;
}
default:
return 0;
}
}
//----------------------------------------------------------------------------
// CollisionCount()
inline int CollisionDetector::CollisionCount() const
{
return (mCollision);
}
//----------------------------------------------------------------------------
// EntityCount()
inline int CollisionDetector::EntityCount() const
{
return (mEntity);
}
//----------------------------------------------------------------------------
// FirstContact()
inline void CollisionDetector::FirstContact()
{
mFirst = 1;
}
//----------------------------------------------------------------------------
// GetEntity()
inline int CollisionDetector::GetEntity(
int i) const
{
return (mEntities[i]);
}
//----------------------------------------------------------------------------
// GetPoints()
inline Triple * CollisionDetector::GetPoints() const
{
return (mPoints);
}
//----------------------------------------------------------------------------
// IsOverlap()
inline int CollisionDetector::IsOverlap(
const Triple & a,
const Triple & b,
const Triple & ac,
const Triple & bc)
{
mVolume++;
RotationMatrix * m0 = mH0->Matrix();
RotationMatrix * m1 = mH1->Matrix();
Triple * v0 = mH0->Vector();
Triple * v1 = mH1->Vector();
if (!m0 || !m1)
{
// static case
if (fabs(bc.X() + mTv.X() - ac.X()) > b.X() + a.X()) return (0);
if (fabs(bc.Y() + mTv.Y() - ac.Y()) > b.Y() + a.Y()) return (0);
if (fabs(bc.Z() + mTv.Z() - ac.Z()) > b.Z() + a.Z()) return (0);
}
else
{
// dynamic case
// test for overlap (separating axis theorem)
double m,s;
// Ax
double rax = bc.X()*mRm.Xx() + bc.Y()*mRm.Xy() + bc.Z()*mRm.Xz() + mTv.X() - ac.X();
m = fabs(rax);
s = b.X()*mFrm.Xx() + b.Y()*mFrm.Xy() + b.Z()*mFrm.Xz();
if (m > s + a.X()) return (0);
// Ay
double ray = bc.X()*mRm.Yx() + bc.Y()*mRm.Yy() + bc.Z()*mRm.Yz() + mTv.Y() - ac.Y();
m = fabs(ray);
s = b.X()*mFrm.Yx() + b.Y()*mFrm.Yy() + b.Z()*mFrm.Yz();
if (m > s +a.Y()) return (0);
// Az
double raz = bc.X()*mRm.Zx() + bc.Y()*mRm.Zy() + bc.Z()*mRm.Zz() + mTv.Z() - ac.Z();
m = fabs(raz);
s = b.X()*mFrm.Zx() + b.Y()*mFrm.Zy() + b.Z()*mFrm.Zz();
if (m > s + a.Z()) return (0);
// Bx
m = fabs(rax*mRm.Xx() + ray*mRm.Yx() + raz*mRm.Zx());
s = a.X()*mFrm.Xx() + a.Y()*mFrm.Yx() + a.Z()*mFrm.Zx();
if (m > s + b.X()) return (0);
// By
m = fabs(rax*mRm.Xy() + ray*mRm.Yy() + raz*mRm.Zy());
s = a.X()*mFrm.Xy() + a.Y()*mFrm.Yy() + a.Z()*mFrm.Zy();
if (m > s + b.Y()) return (0);
// Bz
m = fabs(rax*mRm.Xz() + ray*mRm.Yz() + raz*mRm.Zz());
s = a.X()*mFrm.Xz() + a.Y()*mFrm.Yz() + a.Z()*mFrm.Zz();
if (m > s + b.Z()) return (0);
// Ax * Bx
m = fabs(raz*mRm.Yx() - ray*mRm.Zx());
s = a.Y()*mFrm.Zx() + a.Z()*mFrm.Yx() + b.Y()*mFrm.Xz() + b.Z()*mFrm.Xy();
if (m > s) return (0);
// Ax * By
m = fabs(raz*mRm.Yy() - ray*mRm.Zy());
s = a.Y()*mFrm.Zy() + a.Z()*mFrm.Yy() + b.X()*mFrm.Xz() + b.Z()*mFrm.Xx();
if (m > s) return (0);
// Ax * Bz
m = fabs(raz*mRm.Yz() - ray*mRm.Zz());
s = a.Y()*mFrm.Zz() + a.Z()*mFrm.Yz() + b.X()*mFrm.Xy() + b.Y()*mFrm.Xx();
if (m > s) return (0);
// Ay * Bx
m = fabs(rax*mRm.Zx() - raz*mRm.Xx());
s = a.X()*mFrm.Zx() + a.Z()*mFrm.Xx() + b.Y()*mFrm.Yz() + b.Z()*mFrm.Yy();
if (m > s) return (0);
// Ay * By
m = fabs(rax*mRm.Zy() - raz*mRm.Xy());
s = a.X()*mFrm.Zy() + a.Z()*mFrm.Xy() + b.X()*mFrm.Yz() + b.Z()*mFrm.Yx();
if (m > s) return (0);
// Ay * Bz
m = fabs(rax*mRm.Zz() - raz*mRm.Xz());
s = a.X()*mFrm.Zz() + a.Z()*mFrm.Xz() + b.X()*mFrm.Yy() + b.Y()*mFrm.Yx();
if (m > s) return (0);
// Az * Bx
m = fabs(ray*mRm.Xx() - rax*mRm.Yx());
s = a.X()*mFrm.Yx() + a.Y()*mFrm.Xx() + b.Y()*mFrm.Zz() + b.Z()*mFrm.Zy();
if (m > s) return (0);
// Az * By
m = fabs(ray*mRm.Xy() - rax*mRm.Yy());
s = a.X()*mFrm.Yy() + a.Y()*mFrm.Xy() + b.X()*mFrm.Zz() + b.Z()*mFrm.Zx();
if (m > s) return (0);
// Az * Bz
m = fabs(ray*mRm.Xz() - rax*mRm.Yz());
s = a.X()*mFrm.Yz() + a.Y()*mFrm.Xz() + b.X()*mFrm.Zy() + b.Y()*mFrm.Zx();
if (m > s) return (0);
}
return (1);
}
//----------------------------------------------------------------------------
// PairCount()
inline int CollisionDetector::PairCount() const
{
return (mPair);
}
//----------------------------------------------------------------------------
// SplitQuad()
inline void CollisionDetector::SplitQuad(
Triple * a,
Triple * a1,
Triple * a2)
{
double l1 = (a[0]-a[2]).Len2();
double l2 = (a[1]-a[3]).Len2();
if (l1<l2)
{
a1[0] = a[0];
a1[1] = a[1];
a1[2] = a[2];
a2[0] = a[0];
a2[1] = a[2];
a2[2] = a[3];
}
else
{
a1[0] = a[0];
a1[1] = a[1];
a1[2] = a[3];
a2[0] = a[1];
a2[1] = a[2];
a2[2] = a[3];
}
}
//----------------------------------------------------------------------------
// VolumeCount()
inline int CollisionDetector::VolumeCount() const
{
return (mVolume);
}
#endif // COLLISIONDETECTOR_INCLUDED
|