File: Wm5Bisect2.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 90,112 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 39
file content (286 lines) | stat: -rw-r--r-- 6,983 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
// 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 "Wm5Bisect2.h"
#include "Wm5Math.h"
#include "Wm5Memory.h"

namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
Bisect2<Real>::Bisect2 (Function fFunction, Function gFunction, int maxLevel,
    Real tolerance)
    :
    mFFunction(fFunction),
    mGFunction(gFunction),
    mLevel(0),
    mMaxLevel(maxLevel),
    mTolerance(tolerance)
{
}
//----------------------------------------------------------------------------
template <typename Real>
bool Bisect2<Real>::ZeroTest (Real x, Real y, Real& f, Real& g,
    Real& xRoot, Real& yRoot)
{
    f = mFFunction(x, y);
    g = mGFunction(x, y);

    if (Math<Real>::FAbs(f) <= mTolerance
    &&  Math<Real>::FAbs(g) <= mTolerance)
    {
        xRoot = x;
        yRoot = y;
        --mLevel;
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------
template <typename Real>
typename Bisect2<Real>::BisectNode* Bisect2<Real>::AddNode (Real x, Real y,
    Real f, Real g)
{
    BisectNode* node = new0 BisectNode();
    node->X = x;
    node->Y = y;
    node->F = f;
    node->G = g;
    return node;
}
//----------------------------------------------------------------------------
template <typename Real>
bool Bisect2<Real>::Bisect (Real x0, Real y0, Real x1, Real y1,
    Real& xRoot, Real& yRoot)
{
    // Test the four corner values.
    if (ZeroTest(x0, y0, mF00, mG00, xRoot, yRoot))
    {
        return true;
    }

    if (ZeroTest(x1, y0, mF10, mG10, xRoot, yRoot))
    {
        return true;
    }

    if (ZeroTest(x0, y1, mF01, mG01, xRoot, yRoot))
    {
        return true;
    }

    if (ZeroTest(x1, y1, mF11, mG11, xRoot, yRoot))
    {
        return true;
    }

    // Build the initial quad.

    // Add N00.
    mGraph = new0 BisectNode();
    mGraph->X = x0;
    mGraph->Y = y0;
    mGraph->F = mF00;
    mGraph->G = mG00;

    // Add N10.
    BisectNode* temp = AddNode(x1, y0, mF10, mG10);
    temp->XNext = 0;
    mGraph->XNext = temp;

    // Add N01.
    temp = AddNode(x0, y1, mF01, mG01);
    temp->YNext = 0;
    mGraph->YNext = temp;

    // Add N11.
    temp = AddNode(x1, y1, mF11, mG11);
    temp->XNext = 0;
    temp->YNext = 0;
    mGraph->XNext->YNext = temp;
    mGraph->YNext->XNext = temp;

    mLevel = 0;
    bool result = BisectRecurse(mGraph);
    if (result)
    {
        xRoot = mXRoot;
        yRoot = mYRoot;
    }

    // Remove the remaining quad from mGraph.
    delete0(mGraph->XNext->YNext);
    delete0(mGraph->XNext);
    delete0(mGraph->YNext);
    delete0(mGraph);

    return result;
}
//----------------------------------------------------------------------------
template <typename Real>
bool Bisect2<Real>::BisectRecurse (BisectNode* n00)
{
    if (++mLevel == mMaxLevel)
    {
        --mLevel;
        return false;
    }

    BisectNode* n10 = n00->XNext;
    BisectNode* n11 = n10->YNext;
    BisectNode* n01 = n00->YNext;

    mNetSign = (int)(
        Math<Real>::Sign(n00->F) +
        Math<Real>::Sign(n01->F) +
        Math<Real>::Sign(n10->F) +
        Math<Real>::Sign(n11->F));

    if (abs(mNetSign) == 4)
    {
        // F has the same sign at corners.
        --mLevel;
        return false;
    }

    mNetSign = (int)(
        Math<Real>::Sign(n00->G) +
        Math<Real>::Sign(n01->G) +
        Math<Real>::Sign(n10->G) +
        Math<Real>::Sign(n11->G));

    if ( abs(mNetSign) == 4 )
    {
        // G has the same sign at corners.
        --mLevel;
        return false;
    }

    // Bisect the quad.
    mX0 = n00->X;
    mY0 = n00->Y;
    mX1 = n11->X;
    mY1 = n11->Y;
    mXm = ((Real)0.5)*(mX0 + mX1);
    mYm = ((Real)0.5)*(mY0 + mY1);

    // right edge 10,11
    if (ZeroTest(mX1, mYm, mF1m, mG1m, mXRoot, mYRoot))
    {
        return true;
    }

    // bottom edge 01,11
    if (ZeroTest(mXm, mY1, mFm1, mGm1, mXRoot, mYRoot))
    {
        return true;
    }

    // top edge 00,10
    if (ZeroTest(mXm, mY0, mFm0, mGm0, mXRoot, mYRoot))
    {
        return true;
    }

    // left edge 00,01
    if (ZeroTest(mX0, mYm, mF0m, mG0m, mXRoot, mYRoot))
    {
        return true;
    }

    // center
    if (ZeroTest(mXm, mYm, mFmm, mGmm, mXRoot, mYRoot))
    {
        return true;
    }

    // right bisector
    BisectNode* temp = AddNode(mX1, mYm, mF1m, mG1m);
    temp->XNext = 0;
    temp->YNext = n11;
    n10->YNext = temp;

    // bottom bisector
    temp = AddNode(mXm, mY1, mFm1, mGm1);
    temp->XNext = n11;
    temp->YNext = 0;
    n01->XNext = temp;

    // top bisector
    temp = AddNode(mXm, mY0, mFm0, mGm0);
    temp->XNext = n10;
    n00->XNext = temp;

    // left bisector
    temp = AddNode(mX0, mYm, mF0m, mG0m);
    temp->YNext = n01;
    n00->YNext = temp;

    // middle bisector
    temp = AddNode(mXm, mYm, mFmm, mGmm);
    temp->XNext = n10->YNext;
    temp->YNext = n01->XNext;
    n00->XNext->YNext = temp;
    n00->YNext->XNext = temp;

    // Search the subquads for roots.
    bool result =
        BisectRecurse(n00) ||
        BisectRecurse(n00->XNext) ||
        BisectRecurse(n00->YNext) ||
        BisectRecurse(n00->XNext->YNext);

    // The entire subquad check failed, remove the nodes that were added.

    // center
    delete0(n00->XNext->YNext);

    // edges
    delete0(n00->XNext);
    n00->XNext = n10;
    delete0(n00->YNext);
    n00->YNext = n01;
    delete0(n01->XNext);
    n01->XNext = n11;
    delete0(n10->YNext);
    n10->YNext = n11;

    --mLevel;
    return result;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Bisect2::BisectNode
//----------------------------------------------------------------------------
template <typename Real>
Bisect2<Real>::BisectNode::BisectNode ()
    :
    X((Real)0),
    Y((Real)0),
    F((Real)0),
    G((Real)0),
    XNext(0),
    YNext(0)
{
}
//----------------------------------------------------------------------------

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

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