File: ExtractLevelCurves.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 (239 lines) | stat: -rw-r--r-- 6,973 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
// 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.0 (2010/01/01)

#include "ExtractLevelCurves.h"

WM5_CONSOLE_APPLICATION(ExtractLevelCurves);

// Comment this out for linear interpolation.
#define USE_BILINEAR

ImageRGB82D* ExtractLevelCurves::msColor = 0;
unsigned int ExtractLevelCurves::msSelectedColor = 0;
float ExtractLevelCurves::msMultiply = 1.0f/(float)MAGNIFY;

//----------------------------------------------------------------------------
ExtractLevelCurves::ExtractLevelCurves ()
    :
    ConsoleApplication("SampleImagics/ExtractLevelCurves")
{
}
//----------------------------------------------------------------------------
void ExtractLevelCurves::DrawPixel (int x, int y)
{
    if (0 <= x && x < msColor->GetBound(0)
    &&  0 <= y && y < msColor->GetBound(1))
    {
        (*msColor)(x, y) = msSelectedColor;
    }
}
//----------------------------------------------------------------------------
bool ExtractLevelCurves::Extract ()
{
    msColor = new0 ImageRGB82D(MAGNIFY*SIZE, MAGNIFY*SIZE);

    // Load image for level set extraction.
    std::string imageName = Environment::GetPathR("Head.im");
    ImageInt2D image(imageName.c_str());
    if (image.GetDimensions() == 0)
    {
        // Cannot load the image.
        return false;
    }

    // Get the extremes.
    int minValue = image[0], maxValue = minValue;
    for (int i = 1; i < image.GetQuantity(); ++i)
    {
        int value = image[i];
        if (value < minValue)
        {
            minValue = value;
        }
        else if (value > maxValue)
        {
            maxValue = value;
        }
    }
    int range = maxValue - minValue;

    // Generate a color subimage to superimpose level sets.  Process the
    // subimage with upper left corner (100,100) and of size 32x32.
    for (int y = 0; y < SIZE; ++y)
    {
        for (int x = 0; x < SIZE; ++x)
        {
            for (int dy = 0; dy < MAGNIFY; ++dy)
            {
                float fy = YPOS + y + msMultiply*dy;
                for (int dx = 0; dx < MAGNIFY; ++dx)
                {
                    float fx = XPOS + x + msMultiply*dx;
#ifdef USE_BILINEAR
                    float interp = Bilerp(image, fx, fy);
#else
                    float interp = Lerp(image, fx, fy);
#endif
                    interp = (interp - minValue)/(float)range;
                    unsigned char gray = (unsigned char)(255.0f*interp);
                    (*msColor)(MAGNIFY*x + dx, MAGNIFY*y + dy) =
                        GetColor24(gray, gray, gray);
                }
            }
        }
    }

    // Extract the level set.
    std::vector<Vector2f> vertices;
    std::vector<EdgeKey> edges;
#ifdef USE_BILINEAR
    ExtractCurveSquares ecs(image.GetBound(0), image.GetBound(1),
        (int*)image.GetData());
#else
    ExtractCurveTris ecs(image.GetBound(0), image.GetBound(1),
        (int*)image.GetData());
#endif
    ecs.ExtractContour(512, vertices, edges);
    ecs.MakeUnique(vertices, edges);

    // Draw the edges in the subimage.
    msSelectedColor = GetColor24(0, 255, 0);
    std::vector<EdgeKey>::iterator eiter = edges.begin();
    std::vector<EdgeKey>::iterator eend = edges.end();
    for (/**/; eiter != eend; ++eiter)
    {
        const EdgeKey& edge = *eiter;

        Vector2f v0 = vertices[edge.V[0]];
        int x0 = (int)((v0[0] - XPOS)*MAGNIFY);
        int y0 = (int)((v0[1] - YPOS)*MAGNIFY);

        Vector2f v1 = vertices[edge.V[1]];
        int x1 = (int)((v1[0] - XPOS)*MAGNIFY);
        int y1 = (int)((v1[1] - YPOS)*MAGNIFY);

        Line2D(x0, y0, x1, y1, DrawPixel);
    }

    // Draw the vertices in the subimage.
    msSelectedColor = GetColor24(255, 0, 0);
    std::vector<Vector2f>::iterator viter = vertices.begin();
    std::vector<Vector2f>::iterator vend = vertices.end();
    for (/**/; viter != vend; ++viter)
    {
        Vector2f v = *viter;
        int x = (int)((v[0] - XPOS)*MAGNIFY);
        int y = (int)((v[1] - YPOS)*MAGNIFY);
        DrawPixel(x, y);
    }

#ifdef USE_BILINEAR
    msColor->Save("BilinearZoom.im");
#else
    msColor->Save("LinearZoom.im");
#endif

    delete0(msColor);
    msColor = 0;
    return true;
}
//----------------------------------------------------------------------------
float ExtractLevelCurves::Lerp (const ImageInt2D& image, float fx, float fy)
    const
{
    int x = (int)fx;
    if (x < 0 || x >= image.GetBound(0))
    {
        return 0.0f;
    }

    int y = (int)fy;
    if (y < 0 || y >= image.GetBound(1))
    {
        return 0.0f;
    }

    float dx = fx - x, dy = fy - y;

    int xBound = image.GetBound(0);
    const int* data = (const int*)image.GetData();
    int i00 = x + xBound*y;
    int i10 = i00 + 1;
    int i01 = i00 + xBound;
    int i11 = i10 + xBound;
    float f00 = (float)data[i00];
    float f10 = (float)data[i10];
    float f01 = (float)data[i01];
    float f11 = (float)data[i11];
    float interp;

    int xParity = (x & 1), yParity = (y & 1);
    if (xParity == yParity)
    {
        if (dx + dy <= 1.0f)
        {
            interp = f00 + dx*(f10 - f00) + dy*(f01 - f00);
        }
        else
        {
            interp = f10 + f01 - f11 + dx*(f11 - f01) + dy*(f11 - f10);
        }
    }
    else
    {
        if (dy <= dx)
        {
            interp = f00 + dx*(f10 - f00) + dy*(f11 - f10);
        }
        else
        {
            interp = f00 + dx*(f11 - f01) + dy*(f01 - f00);
        }
    }

    return interp;
}
//----------------------------------------------------------------------------
float ExtractLevelCurves::Bilerp (const ImageInt2D& image, float fx, float fy)
    const
{
    int x = (int)fx;
    if (x < 0 || x >= image.GetBound(0))
    {
        return 0.0;
    }

    int y = (int)fy;
    if (y < 0 || y >= image.GetBound(1))
    {
        return 0.0;
    }

    float dx = fx - x, dy = fy - y;
    float omdx = 1.0f - dx, omdy = 1.0f - dy;

    int xBound = image.GetBound(0);
    const int* data = (const int*)image.GetData();
    int i00 = x + xBound*y;
    int i10 = i00 + 1;
    int i01 = i00 + xBound;
    int i11 = i10 + xBound;
    float f00 = (float)data[i00];
    float f10 = (float)data[i10];
    float f01 = (float)data[i01];
    float f11 = (float)data[i11];

    float interp = omdx*(omdy*f00 + dy*f01) + dx*(omdy*f10 + dy*f11);
    return interp;
}
//----------------------------------------------------------------------------
int ExtractLevelCurves::Main (int, char**)
{
    return (Extract() ? 0 : -1);
}
//----------------------------------------------------------------------------