File: Wm5ContMinBox2.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,124 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 40
file content (358 lines) | stat: -rw-r--r-- 11,118 bytes parent folder | download | duplicates (3)
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
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.1 (2010/10/01)

#include "Wm5MathematicsPCH.h"
#include "Wm5ContMinBox2.h"
#include "Wm5ConvexHull2.h"
#include "Wm5Memory.h"

namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
MinBox2<Real>::MinBox2 (int numPoints, const Vector2<Real>* points,
    Real epsilon, Query::Type queryType, bool isConvexPolygon)
{
    // Get the convex hull of the points.
    Vector2<Real>* hullPoints = 0;
    if (isConvexPolygon)
    {
        hullPoints = (Vector2<Real>*)points;
    }
    else
    {
        ConvexHull2<Real> hull(numPoints, (Vector2<Real>*)points, epsilon,
            false, queryType);
        int hullDim = hull.GetDimension();
        int hullNumSimplices = hull.GetNumSimplices();
        const int* hullIndices = hull.GetIndices();

        if (hullDim == 0)
        {
            mMinBox.Center = points[0];
            mMinBox.Axis[0] = Vector2<Real>::UNIT_X;
            mMinBox.Axis[1] = Vector2<Real>::UNIT_Y;
            mMinBox.Extent[0] = (Real)0;
            mMinBox.Extent[1] = (Real)0;
            return;
        }

        if (hullDim == 1)
        {
            ConvexHull1<Real>* hull1 = hull.GetConvexHull1();
            hullIndices = hull1->GetIndices();

            mMinBox.Center = ((Real)0.5)*(points[hullIndices[0]] +
                points[hullIndices[1]]);
            Vector2<Real> diff =
                points[hullIndices[1]] - points[hullIndices[0]];
            mMinBox.Extent[0] = ((Real)0.5)*diff.Normalize();
            mMinBox.Extent[1] = (Real)0.0;
            mMinBox.Axis[0] = diff;
            mMinBox.Axis[1] = -mMinBox.Axis[0].Perp();

            delete0(hull1);
            return;
        }

        numPoints = hullNumSimplices;
        hullPoints = new1<Vector2<Real> >(numPoints);
        for (int i = 0; i < numPoints; ++i)
        {
            hullPoints[i] = points[hullIndices[i]];
        }
    }
    
    // The input points are V[0] through V[N-1] and are assumed to be the
    // vertices of a convex polygon that are counterclockwise ordered.  The
    // input points must not contain three consecutive collinear points.

    // Unit-length edge directions of convex polygon.  These could be
    // precomputed and passed to this routine if the application requires it.
    int numPointsM1 = numPoints -1;
    Vector2<Real>* edges = new1<Vector2<Real> >(numPoints);
    bool* visited = new1<bool>(numPoints);
    int i;
    for (i = 0; i < numPointsM1; ++i)
    {
        edges[i] = hullPoints[i + 1] - hullPoints[i];
        edges[i].Normalize();
        visited[i] = false;
    }
    edges[numPointsM1] = hullPoints[0] - hullPoints[numPointsM1];
    edges[numPointsM1].Normalize();
    visited[numPointsM1] = false;

    // Find the smallest axis-aligned box containing the points.  Keep track
    // of the extremum indices, L (left), R (right), B (bottom), and T (top)
    // so that the following constraints are met:
    //   V[L].X() <= V[i].X() for all i and V[(L+1)%N].X() > V[L].X()
    //   V[R].X() >= V[i].X() for all i and V[(R+1)%N].X() < V[R].X()
    //   V[B].Y() <= V[i].Y() for all i and V[(B+1)%N].Y() > V[B].Y()
    //   V[T].Y() >= V[i].Y() for all i and V[(T+1)%N].Y() < V[T].Y()
    Real xmin = hullPoints[0].X(), xmax = xmin;
    Real ymin = hullPoints[0].Y(), ymax = ymin;
    int LIndex = 0, RIndex = 0, BIndex = 0, TIndex = 0;
    for (i = 1; i < numPoints; ++i)
    {
        if (hullPoints[i].X() <= xmin)
        {
            xmin = hullPoints[i].X();
            LIndex = i;
        }
        if (hullPoints[i].X() >= xmax)
        {
            xmax = hullPoints[i].X();
            RIndex = i;
        }

        if (hullPoints[i].Y() <= ymin)
        {
            ymin = hullPoints[i].Y();
            BIndex = i;
        }
        if (hullPoints[i].Y() >= ymax)
        {
            ymax = hullPoints[i].Y();
            TIndex = i;
        }
    }

    // Apply wrap-around tests to ensure the constraints mentioned above are
    // satisfied.
    if (LIndex == numPointsM1)
    {
        if (hullPoints[0].X() <= xmin)
        {
            xmin = hullPoints[0].X();
            LIndex = 0;
        }
    }

    if (RIndex == numPointsM1)
    {
        if (hullPoints[0].X() >= xmax)
        {
            xmax = hullPoints[0].X();
            RIndex = 0;
        }
    }

    if (BIndex == numPointsM1)
    {
        if (hullPoints[0].Y() <= ymin)
        {
            ymin = hullPoints[0].Y();
            BIndex = 0;
        }
    }

    if (TIndex == numPointsM1)
    {
        if (hullPoints[0].Y() >= ymax)
        {
            ymax = hullPoints[0].Y();
            TIndex = 0;
        }
    }

    // The dimensions of the axis-aligned box.  The extents store width and
    // height for now.
    mMinBox.Center.X() = ((Real)0.5)*(xmin + xmax);
    mMinBox.Center.Y() = ((Real)0.5)*(ymin + ymax);
    mMinBox.Axis[0] = Vector2<Real>::UNIT_X;
    mMinBox.Axis[1] = Vector2<Real>::UNIT_Y;
    mMinBox.Extent[0] = ((Real)0.5)*(xmax - xmin);
    mMinBox.Extent[1] = ((Real)0.5)*(ymax - ymin);
    Real minAreaDiv4 = mMinBox.Extent[0]*mMinBox.Extent[1];

    // The rotating calipers algorithm.
    Vector2<Real> U = Vector2<Real>::UNIT_X;
    Vector2<Real> V = Vector2<Real>::UNIT_Y;

    bool done = false;
    while (!done)
    {
        // Determine the edge that forms the smallest angle with the current
        // box edges.
        int flag = F_NONE;
        Real maxDot = (Real)0;

        Real dot = U.Dot(edges[BIndex]);
        if (dot > maxDot)
        {
            maxDot = dot;
            flag = F_BOTTOM;
        }

        dot = V.Dot(edges[RIndex]);
        if (dot > maxDot)
        {
            maxDot = dot;
            flag = F_RIGHT;
        }

        dot = -U.Dot(edges[TIndex]);
        if (dot > maxDot)
        {
            maxDot = dot;
            flag = F_TOP;
        }

        dot = -V.Dot(edges[LIndex]);
        if (dot > maxDot)
        {
            maxDot = dot;
            flag = F_LEFT;
        }

        switch (flag)
        {
        case F_BOTTOM:
            if (visited[BIndex])
            {
                done = true;
            }
            else
            {
                // Compute box axes with E[B] as an edge.
                U = edges[BIndex];
                V = -U.Perp();
                UpdateBox(hullPoints[LIndex], hullPoints[RIndex],
                    hullPoints[BIndex], hullPoints[TIndex], U, V,
                    minAreaDiv4);

                // Mark edge visited and rotate the calipers.
                visited[BIndex] = true;
                if (++BIndex == numPoints)
                {
                    BIndex = 0;
                }
            }
            break;
        case F_RIGHT:
            if (visited[RIndex])
            {
                done = true;
            }
            else
            {
                // Compute box axes with E[R] as an edge.
                V = edges[RIndex];
                U = V.Perp();
                UpdateBox(hullPoints[LIndex], hullPoints[RIndex],
                    hullPoints[BIndex], hullPoints[TIndex], U, V,
                    minAreaDiv4);

                // Mark edge visited and rotate the calipers.
                visited[RIndex] = true;
                if (++RIndex == numPoints)
                {
                    RIndex = 0;
                }
            }
            break;
        case F_TOP:
            if (visited[TIndex])
            {
                done = true;
            }
            else
            {
                // Compute box axes with E[T] as an edge.
                U = -edges[TIndex];
                V = -U.Perp();
                UpdateBox(hullPoints[LIndex], hullPoints[RIndex],
                    hullPoints[BIndex], hullPoints[TIndex], U, V,
                    minAreaDiv4);

                // Mark edge visited and rotate the calipers.
                visited[TIndex] = true;
                if (++TIndex == numPoints)
                {
                    TIndex = 0;
                }
            }
            break;
        case F_LEFT:
            if (visited[LIndex])
            {
                done = true;
            }
            else
            {
                // Compute box axes with E[L] as an edge.
                V = -edges[LIndex];
                U = V.Perp();
                UpdateBox(hullPoints[LIndex], hullPoints[RIndex],
                    hullPoints[BIndex], hullPoints[TIndex], U, V,
                    minAreaDiv4);

                // Mark edge visited and rotate the calipers.
                visited[LIndex] = true;
                if (++LIndex == numPoints)
                {
                    LIndex = 0;
                }
            }
            break;
        case F_NONE:
            // The polygon is a rectangle.
            done = true;
            break;
        }
    }

    delete1(visited);
    delete1(edges);
    if (!isConvexPolygon)
    {
        delete1(hullPoints);
    }
}
//----------------------------------------------------------------------------
template <typename Real>
MinBox2<Real>::operator Box2<Real> () const
{
    return mMinBox;
}
//----------------------------------------------------------------------------
template <typename Real>
void MinBox2<Real>::UpdateBox (const Vector2<Real>& LPoint,
    const Vector2<Real>& RPoint, const Vector2<Real>& BPoint,
    const Vector2<Real>& TPoint, const Vector2<Real>& U,
    const Vector2<Real>& V, Real& minAreaDiv4)
{
    Vector2<Real> RLDiff = RPoint - LPoint;
    Vector2<Real> TBDiff = TPoint - BPoint;
    Real extent0 = ((Real)0.5)*(U.Dot(RLDiff));
    Real extent1 = ((Real)0.5)*(V.Dot(TBDiff));
    Real areaDiv4 = extent0*extent1;
    if (areaDiv4 < minAreaDiv4)
    {
        minAreaDiv4 = areaDiv4;
        mMinBox.Axis[0] = U;
        mMinBox.Axis[1] = V;
        mMinBox.Extent[0] = extent0;
        mMinBox.Extent[1] = extent1;
        Vector2<Real> LBDiff = LPoint - BPoint;
        mMinBox.Center = LPoint + U*extent0 + V*(extent1 - V.Dot(LBDiff));
    }
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
class MinBox2<float>;

template WM5_MATHEMATICS_ITEM
class MinBox2<double>;
//----------------------------------------------------------------------------
}