Menu

[c54480]: / geometry.cpp  Maximize  Restore  History

Download this file

386 lines (312 with data), 8.9 kB

  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
#include "geometry.h"
#include <math.h>
#include "misc.h"
#include <QString>
#include <iostream>
using namespace std;
QRectF addBBox(QRectF r1, QRectF r2)
{
// Find smallest QRectF containing given rectangles
QRectF n;
// Set left border
if (r1.left() <= r2.left() )
n.setLeft(r1.left() );
else
n.setLeft(r2.left() );
// Set top border
if (r1.top() <= r2.top() )
n.setTop(r1.top() );
else
n.setTop(r2.top() );
// Set right border
if (r1.right() <= r2.right() )
n.setRight(r2.right() );
else
n.setRight(r1.right() );
// Set bottom
if (r1.bottom() <= r2.bottom() )
n.setBottom(r2.bottom() );
else
n.setBottom(r1.bottom() );
return n;
}
QSize addBBoxSize (QSize s1, QSize s2)
{
// Find the minimimum smallest QSize which could include 2 given sizes
QSize s=s1;
if (s1.width() <= s2.width() )
s.setWidth (s2.width() );
if (s1.height() <= s2.height() )
s.setHeight(s2.height() );
return s;
}
bool isInBox(const QPointF &p, const QRectF &box)
{
if (p.x() >= box.left() && p.x() <= box.right()
&& p.y() <= box.bottom() && p.y() >= box.top() )
return true;
return false;
}
qreal Geometry::distance (const QPointF &p, const QPointF &q)
{
return sqrt (p.x()*q.x() + p.y()*q.y());
}
Vector::Vector ():QPointF ()
{
}
Vector::Vector (const QPointF &p):QPointF (p)
{
}
Vector::Vector (qreal x, qreal y):QPointF (x,y)
{
}
Vector::~Vector ()
{
}
//! Check if length is 0
bool Vector::isNull()
{
if (x()==0 && y()==0)
return true;
return false;
}
//! Normalize vector
void Vector::normalize ()
{
if (isNull() ) return;
qreal l=sqrt ( x()*x() + y()*y() );
setX (x()/l);
setY (y()/l);
}
//! Dot product of two vectors
qreal Vector::dotProduct (const QPointF &b)
{
return x()*b.x() + y()*b.y();
}
void Vector::scale (const qreal &f)
{
setX (x()*f);
setY (y()*f);
}
void Vector::invert ()
{
setX (-x());
setY (-y());
}
QPointF Vector::toQPointF ()
{
return QPointF (x(),y());
}
/*! Calculate the projection of a polygon on an axis
and returns it as a [min, max] interval */
ConvexPolygon::ConvexPolygon ()
{
}
ConvexPolygon::ConvexPolygon (QPolygonF p):QPolygonF (p)
{
}
ConvexPolygon::~ConvexPolygon ()
{
}
void ConvexPolygon::calcCentroid()
{
// Calculate area and centroid
// http://en.wikipedia.org/wiki/Centroid
qreal cx,cy,p;
cx=cy=0;
_area=0;
append (at(0));
for (int i=0;i<size()-1;i++)
{
p=at(i).x() * at(i+1).y() - at(i+1).x() * at(i).y();
_area+=p;
cx+=(at(i).x()+at(i+1).x()) * p;
cy+=(at(i).y()+at(i+1).y()) * p;
}
pop_back();
// area is negative if vertices ordered counterclockwise
// (in mirrored graphicsview!)
_area=_area/2;
p=_area*6;
_centroid.setX (cx/p);
_centroid.setY (cy/p);
}
QPointF ConvexPolygon::centroid() const
{
return _centroid;
}
qreal ConvexPolygon::weight() const
{
return _area;
}
std::string ConvexPolygon::toStdString()
{
QString s ("(");
for (int i=0;i<size();++i)
{
s+=QString("(%1,%2)").arg(at(i).x()).arg(at(i).y());
if (i<size()-1) s+=",";
}
s+=")";
return s.toStdString();
}
Vector ConvexPolygon::at(const int &i) const
{
return Vector (QPolygonF::at(i).x(),QPolygonF::at(i).y());
}
void ConvexPolygon::translate ( const Vector & offset )
{ translate (offset.x(),offset.y());}
void ConvexPolygon::translate ( qreal dx, qreal dy )
{
QPolygonF::translate (dx,dy);
_centroid=_centroid+QPointF (dx,dy);
}
void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max)
{
// To project a point on an axis use the dot product
//qDebug() << "Projecting on "<< axis;
qreal d = axis.dotProduct(polygon.at(0));
min = d;
max = d;
for (int i = 0; i < polygon.size(); i++)
{
d= polygon.at(i).dotProduct (axis);
if (d < min)
min = d;
else
if (d> max) max = d;
// qDebug() << "p="<<polygon.at(i)<<" d="<<d<<" (min, max)=("<<min<<","<<max<<")";
}
}
// Calculate the signed distance between [minA, maxA] and [minB, maxB]
// The distance will be negative if the intervals overlap
qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB) {
if (minA < minB) {
return minB - maxA;
} else {
return minA - maxB;
}
}
/*!
Check if polygon A is going to collide with polygon B.
The last parameter is the *relative* velocity
of the polygons (i.e. velocityA - velocityB)
*/
PolygonCollisionResult polygonCollision(ConvexPolygon polygonA,
ConvexPolygon polygonB, Vector velocity)
{
PolygonCollisionResult result;
result.intersect = true;
result.willIntersect = true;
int edgeCountA = polygonA.size();
int edgeCountB = polygonB.size();
qreal minIntervalDistance = 1000000000;
QPointF translationAxis;
QPointF edge;
/*
qDebug() << "A: ";
for (int k=0; k<edgeCountA;k++)
qDebug() <<polygonA.at(k);
qDebug() << "B: ";
for (int k=0; k<edgeCountB;k++)
qDebug() <<polygonB.at(k);
qDebug() ;
*/
// Loop through all the edges of both polygons
for (int i=0;i<edgeCountA + edgeCountB;i++)
{
if (i< edgeCountA)
{
// Loop through polygon A
if (i<edgeCountA-1)
edge = QPointF (
polygonA.at(i+1).x()-polygonA.at(i).x(),
polygonA.at(i+1).y()-polygonA.at(i).y());
else
edge = QPointF (
polygonA.at(0).x()-polygonA.at(i).x(),
polygonA.at(0).y()-polygonA.at(i).y());
} else
{
// Loop through polygon B
if (i < edgeCountA +edgeCountB -1 )
edge = QPointF (
polygonB.at(i-edgeCountA+1).x() - polygonB.at(i-edgeCountA).x(),
polygonB.at(i-edgeCountA+1).y() - polygonB.at(i-edgeCountA).y());
else
edge = QPointF (
polygonB.at(0).x() - polygonB.at(i-edgeCountA).x(),
polygonB.at(0).y() - polygonB.at(i-edgeCountA).y());
}
// ===== 1. Find if the polygons are currently intersecting =====
// Find the axis perpendicular to the current edge
Vector axis (-edge.y(), edge.x());
axis.normalize();
// Find the projection of the polygon on the current axis
qreal minA = 0; qreal minB = 0; qreal maxA = 0; qreal maxB = 0;
projectPolygon(axis, polygonA, minA, maxA);
projectPolygon(axis, polygonB, minB, maxB);
// Check if the polygon projections are currentlty intersecting
qreal d = intervalDistance(minA, maxA, minB, maxB);
if (d > 0) result.intersect = false;
// ===== 2. Now find if the polygons *will* intersect =====
// Project the velocity on the current axis
qreal velocityProjection = axis.dotProduct(velocity);
// Get the projection of polygon A during the movement
if (velocityProjection < 0)
minA += velocityProjection;
else
maxA += velocityProjection;
// Do the same test as above for the new projection
// d = intervalDistance(minA, maxA, minB, maxB);
//if (d > 0) result.willIntersect = false;
/*
qDebug() <<" ";
qDebug() << "edge="<<edge<<" ";
qDebug() <<"axis="<<axis<<" ";
qDebug() <<"dA=("<<minA<<","<<maxA<<") dB=("<<minB<<","<<maxB<<")";
qDebug() <<" d="<<d<<" ";
//qDebug() <<"minD="<<minIntervalDistance<<" ";
qDebug() <<"int="<<result.intersect<<" ";
//qDebug() <<"wint="<<result.willIntersect<<" ";
//qDebug() <<"velProj="<<velocityProjection<<" ";
qDebug() ;
*/
if (result.intersect )// || result.willIntersect)
{
// Check if the current interval distance is the minimum one. If so
// store the interval distance and the current distance. This will
// be used to calculate the minimum translation vector
if (d<0) d=-d;
if (d < minIntervalDistance) {
minIntervalDistance = d;
//translationAxis = axis;
//qDebug() << "tAxix="<<translationAxis;
//QPointF t = polygonA.Center - polygonB.Center;
//QPointF t = polygonA.at(0) - polygonB.at(0);
//if (dotProduct(t,translationAxis) < 0)
// translationAxis = -translationAxis;
}
}
}
// The minimum translation vector
// can be used to push the polygons appart.
if (result.willIntersect)
result.minTranslation =
translationAxis * minIntervalDistance;
return result;
}
/* The function can be used this way:
QPointF polygonATranslation = new QPointF();
*/
/*
PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);
if (r.WillIntersect)
// Move the polygon by its velocity, then move
// the polygons appart using the Minimum Translation Vector
polygonATranslation = velocity + r.minTranslation;
else
// Just move the polygon by its velocity
polygonATranslation = velocity;
polygonA.Offset(polygonATranslation);
*/
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.