File: ConformalMapping.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 (321 lines) | stat: -rw-r--r-- 10,729 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
// 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 "ConformalMapping.h"

WM5_WINDOW_APPLICATION(ConformalMapping);

//----------------------------------------------------------------------------
ConformalMapping::ConformalMapping ()
    :
    WindowApplication3("SampleImagics/ConformalMapping", 0, 0, 640, 480,
        Float4(0.5f, 0.0f, 1.0f, 1.0f)),
        mTextColor(1.0f, 1.0f, 1.0f, 1.0f)
{
}
//----------------------------------------------------------------------------
bool ConformalMapping::OnInitialize ()
{
    if (!WindowApplication3::OnInitialize())
    {
        return false;
    }

    // Set up the camera.
    mCamera->SetFrustum(60.0f, GetAspectRatio(), 0.1f, 100.0f);
    APoint camPosition(0.0f, 0.0f, -6.5f);
    AVector camDVector(0.0f, 0.0f, 1.0f);
    AVector camUVector(0.0f, 1.0f, 0.0f);
    AVector camRVector = camDVector.Cross(camUVector);
    mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector);

    CreateScene();

    // Initial update of objects.
    mScene->Update();

    // Initial culling of scene.
    mCuller.SetCamera(mCamera);
    mCuller.ComputeVisibleSet(mScene);

    InitializeCameraMotion(0.01f, 0.01f);
    InitializeObjectMotion(mScene);
    return true;
}
//----------------------------------------------------------------------------
void ConformalMapping::OnTerminate ()
{
    mScene = 0;
    mMeshTree = 0;
    mSphereTree = 0;
    mWireState = 0;

    WindowApplication3::OnTerminate();
}
//----------------------------------------------------------------------------
void ConformalMapping::OnIdle ()
{
    MeasureTime();

    if (MoveCamera())
    {
        mCuller.ComputeVisibleSet(mScene);
    }

    if (MoveObject())
    {
        mScene->Update();
        mCuller.ComputeVisibleSet(mScene);
    }

    if (mRenderer->PreDraw())
    {
        mRenderer->ClearBuffers();
        mRenderer->Draw(mCuller.GetVisibleSet());
        DrawFrameRate(8, GetHeight()-8, mTextColor);
        mRenderer->PostDraw();
        mRenderer->DisplayColorBuffer();
    }

    UpdateFrameCount();
}
//----------------------------------------------------------------------------
bool ConformalMapping::OnKeyDown (unsigned char key, int x, int y)
{
    switch (key)
    {
    case 'w':
        mWireState->Enabled = !mWireState->Enabled;
        return true;
    case 'm':
        mMotionObject = mMeshTree;
        return true;
    case 's':
        mMotionObject = mSphereTree;
        return true;
    case 'r':
        mMotionObject = mScene;
        return true;
    }

    return WindowApplication3::OnKeyDown(key, x, y);
}
//----------------------------------------------------------------------------
void ConformalMapping::CreateScene ()
{
    // Load the brain mesh.  The mesh must have the topology of a sphere.
    std::string brainFile = Environment::GetPathR("BrainP.wmvf");
    Visual::PrimitiveType type;
    VertexFormat* vformat0;
    VertexBuffer* vbuffer0;
    IndexBuffer* ibuffer;
    Visual::LoadWMVF(brainFile, type, vformat0, vbuffer0, ibuffer);
    VertexBufferAccessor vba0(vformat0, vbuffer0);

    // The mesh has only positions.  Reallocate the vertex buffer to include
    // vertex colors.
    VertexFormat* vformat1 = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
    int vstride1 = vformat1->GetStride();
    VertexBuffer* vbuffer1 = new0 VertexBuffer(vba0.GetNumVertices(),
        vstride1);
    VertexBufferAccessor vba1(vformat1, vbuffer1);
    for (int i = 0; i < vba0.GetNumVertices(); ++i)
    {
        vba1.Position<Float3>(i) = vba0.Position<Float3>(i);
    }

    TriMesh* mesh = new0 TriMesh(vformat1, vbuffer1, ibuffer);
    delete0(vformat0);
    delete0(vbuffer0);

    // Map the data to the cube [-10,10]^3.  This provides numerical
    // preconditioning for computing the conformal map.  The choice of 10 is
    // based on knowledge of the magnitude of the positions of vbuffer0.
    float extreme = 10.0f;
    ScaleToCube(extreme, mesh);

    // Use pseudocoloring based on mean curvature.
    PseudocolorVertices(mesh);

    // Compute the conformal map between the mesh and a sphere.
    TriMesh* sphere = DoMapping(mesh);

    // Use vertex coloring.
    VertexColor3Effect* effect = new0 VertexColor3Effect();
    mesh->SetEffectInstance(effect->CreateInstance());
    sphere->SetEffectInstance(effect->CreateInstance());

    // Create the root of the scene graph.
    mScene = new0 Node();
    mWireState = new0 WireState();
    mRenderer->SetOverrideWireState(mWireState);

    // Subtree for mesh.  This allows for a virtual trackball centered on
    // the mesh.
    mMeshTree = new0 Node();
    mMeshTree->LocalTransform.SetTranslate(APoint(2.0f, 0.0f, 0.0f));
    mMeshTree->LocalTransform.SetUniformScale(1.0f/extreme);
    mScene->AttachChild(mMeshTree);
    Node* meshParent = new0 Node();
    mMeshTree->AttachChild(meshParent);
    meshParent->AttachChild(mesh);
    meshParent->LocalTransform.SetTranslate(
        -mesh->GetModelBound().GetCenter());

    // Subtree for sphere.  This allows for a virtual trackball centered on
    // the sphere.
    mSphereTree = new0 Node();
    mSphereTree->LocalTransform.SetTranslate(APoint(-2.0f, 0.0f, 0.0f));
    mScene->AttachChild(mSphereTree);
    Node* sphereParent = new0 Node();
    mSphereTree->AttachChild(sphereParent);
    sphereParent->AttachChild(sphere);
    sphereParent->LocalTransform.SetTranslate(
        -sphere->GetModelBound().GetCenter());
}
//----------------------------------------------------------------------------
void ConformalMapping::ScaleToCube (float extreme, TriMesh* mesh)
{
    // Uniformly scale the cube to [-extreme,extreme]^3 for numerical
    // preconditioning for the conformal mapping.

    VertexBufferAccessor vba(mesh);
    float minValue = vba.Position<Float3>(0)[0], maxValue = minValue;
    int i, j;
    for (i = 0; i < vba.GetNumVertices(); ++i)
    {
        Float3 position = vba.Position<Float3>(i);
        for (j = 0; j < 3; ++j)
        {
            if (position[j] < minValue)
            {
                minValue = position[j];
            }
            else if (position[j] > maxValue)
            {
                maxValue = position[j];
            }
        }
    }
    float halfRange = 0.5f*(maxValue - minValue);
    float mult = extreme/halfRange;
    for (i = 0; i < vba.GetNumVertices(); ++i)
    {
        Float3& position = vba.Position<Float3>(i);
        for (j = 0; j < 3; ++j)
        {
            position[j] = -extreme + mult*(position[j] - minValue);
        }
    }

    mesh->UpdateModelSpace(Visual::GU_MODEL_BOUND_ONLY);
}
//----------------------------------------------------------------------------
void ConformalMapping::PseudocolorVertices (TriMesh* mesh)
{
    // Color the vertices according to mean curvature.

    VertexBufferAccessor vba(mesh);
    const int numVertices = vba.GetNumVertices();
    const int numTriangles = mesh->GetIndexBuffer()->GetNumElements()/3;
    const int* indices = (int*)mesh->GetIndexBuffer()->GetData();
    Vector3f* positions = new1<Vector3f>(numVertices);
    int i;
    for (i = 0; i < numVertices; ++i)
    {
        positions[i] = vba.Position<Vector3f>(i);
    }
    MeshCurvaturef mc(numVertices, positions, numTriangles, indices);
    delete1(positions);

    const float* minCurv = mc.GetMinCurvatures();
    const float* maxCurv = mc.GetMaxCurvatures();
    float minMeanCurvature = minCurv[0] + maxCurv[0];
    float maxMeanCurvature = minMeanCurvature;
    float* meanCurvatures = new1<float>(numVertices);
    for (i = 0; i < numVertices; ++i)
    {
        meanCurvatures[i] = minCurv[i] + maxCurv[i];
        if (meanCurvatures[i] < minMeanCurvature)
        {
            minMeanCurvature = meanCurvatures[i];
        }
        else if (meanCurvatures[i] > maxMeanCurvature)
        {
            maxMeanCurvature = meanCurvatures[i];
        }
    }

    for (i = 0; i < numVertices; ++i)
    {
        Float3& color = vba.Color<Float3>(0, i);
        if (meanCurvatures[i] > 0.0f)
        {
            color[0] = 0.5f*(1.0f + meanCurvatures[i]/maxMeanCurvature);
            color[1] = color[0];
            color[2] = 0.0f;
        }
        else if (meanCurvatures[i] < 0.0f)
        {
            color[0] = 0.0f;
            color[1] = 0.0f;
            color[2] = 0.5f*(1.0f - meanCurvatures[i]/minMeanCurvature);
        }
        else
        {
            color[0] = 0.0f;
            color[1] = 0.0f;
            color[2] = 0.0f;
        }
    }

    delete1(meanCurvatures);
}
//----------------------------------------------------------------------------
TriMesh* ConformalMapping::DoMapping (TriMesh* mesh)
{
    VertexBufferAccessor vba0(mesh);
    const int numVertices = vba0.GetNumVertices();
    IndexBuffer* ibuffer = mesh->GetIndexBuffer();
    const int numTriangles = ibuffer->GetNumElements()/3;
    const int* indices = (int*)ibuffer->GetData();
    Vector3f* positions = new1<Vector3f>(numVertices);
    int i;
    for (i = 0; i < numVertices; ++i)
    {
        positions[i] = vba0.Position<Vector3f>(i);
    }

    // Color the punctured triangle red.
    Float3 red(1.0f, 0.0f, 0.0f);
    vba0.Color<Float3>(0, indices[0]) = red;
    vba0.Color<Float3>(0, indices[1]) = red;
    vba0.Color<Float3>(0, indices[2]) = red;

    // Conformally map the mesh to plane, sphere, and cylinder.
    ConformalMapf cm(numVertices, positions, numTriangles, indices, 0);
    delete1(positions);
    const Vector3f* spherePositions = cm.GetSphereCoordinates();

    // Create a representation of the conformal sphere.
    VertexFormat* vformat = mesh->GetVertexFormat();
    int vstride = vformat->GetStride();
    VertexBuffer* vbuffer = new0 VertexBuffer(numVertices, vstride);
    VertexBufferAccessor vba1(vformat, vbuffer);
    for (i = 0; i < numVertices; ++i)
    {
        vba1.Position<Vector3f>(i) = spherePositions[i];
        vba1.Color<Float3>(0, i) = vba0.Color<Float3>(0, i);
    }

    TriMesh* sphere = new0 TriMesh(vformat, vbuffer, ibuffer);
    return sphere;
}
//----------------------------------------------------------------------------