File: MeshEnvelope.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 (166 lines) | stat: -rw-r--r-- 4,838 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
// 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 "MeshEnvelope.h"
#include "CreateEnvelope.h"

WM5_WINDOW_APPLICATION(MeshEnvelope);

//----------------------------------------------------------------------------
MeshEnvelope::MeshEnvelope ()
    :
    WindowApplication2("SampleMathematics/MeshEnvelope", 0, 0, 512, 512,
        Float4(1.0f, 1.0f, 1.0f, 1.0f))
{
    mVertices = 0;
    mIndices = 0;
    mEnvelopeVertices = 0;
    mSize = GetWidth();
}
//----------------------------------------------------------------------------
bool MeshEnvelope::OnInitialize ()
{
    if (!WindowApplication2::OnInitialize())
    {
        return false;
    }

    // Load the 3D face mesh.
#ifdef WM5_LITTLE_ENDIAN
    std::string path = Environment::GetPathR("FacePN.wmof");
#else
    std::string path = Environment::GetPathR("FacePN.be.wmof");
#endif
    InStream inStream;
    inStream.Load(path);
    TriMeshPtr mesh = StaticCast<TriMesh>(inStream.GetObjectAt(0));
    VertexBufferAccessor vba(mesh);
    mNumVertices = vba.GetNumVertices();

#if 1
    // Rotate the mesh just to see how the algorithm behaves.
    Matrix3f rotate(Vector3f::UNIT_X, Mathf::HALF_PI);
    for (int j = 0; j < mNumVertices; ++j)
    {
        vba.Position<Vector3f>(j) = rotate*vba.Position<Vector3f>(j);
    }
#endif

    // Project the mesh onto the xy-plane.
    mVertices = new1<Vector2f>(mNumVertices);
    float x = vba.Position<Vector3f>(0).X();
    float y = vba.Position<Vector3f>(0).Y();
    mVertices[0].X() = x;
    mVertices[0].Y() = y;
    float xmin = x, xmax = x;
    float ymin = y, ymax = y;
    int i;
    for (i = 1; i < mNumVertices; ++i)
    {
        x = vba.Position<Vector3f>(i).X();
        y = vba.Position<Vector3f>(i).Y();
        mVertices[i][0] = x;
        mVertices[i][1] = y;

        if (x < xmin)
        {
            xmin = x;
        }
        else if (x > xmax)
        {
            xmax = x;
        }

        if (y < ymin)
        {
            ymin = y;
        }
        else if (y > ymax)
        {
            ymax = y;
        }
    }

    // Transform the mesh into window coordinates.
    float xOffset = 0.125f*mSize;
    float yOffset = 0.125f*mSize;
    float xMult = (0.75f*mSize)/(xmax - xmin);
    float yMult = (0.75f*mSize)/(ymax - ymin);
    for (i = 0; i < mNumVertices; ++i)
    {
        mVertices[i].X() = xOffset + xMult*(mVertices[i].X() - xmin);
        mVertices[i].Y() = yOffset + yMult*(mVertices[i].Y() - ymin);
    }

    // Copy the mesh connectivity information.
    IndexBuffer* ibuffer = mesh->GetIndexBuffer();
    mNumIndices = ibuffer->GetNumElements();
    mIndices = new1<int>(mNumIndices);
    size_t numBytes = mNumIndices*sizeof(int);
    memcpy(mIndices, ibuffer->GetData(), numBytes);

    // Create the envelope of the mesh.
    CreateEnvelope(mNumVertices, mVertices, mNumIndices, mIndices,
        mNumEnvelopeVertices, mEnvelopeVertices);

    DoFlip(true);
    OnDisplay();
    return true;
}
//----------------------------------------------------------------------------
void MeshEnvelope::OnTerminate ()
{
    delete1(mVertices);
    delete1(mIndices);
    delete1(mEnvelopeVertices);

    WindowApplication2::OnTerminate();
}
//----------------------------------------------------------------------------
void MeshEnvelope::OnDisplay ()
{
    ClearScreen();

    // Draw the mesh triangles.
    ColorRGB black(0, 0, 0);
    int i, v0, v1, v2, x0, y0, x1, y1, x2, y2;
    for (i = 0; i < mNumIndices; /**/)
    {
        v0 = mIndices[i++];
        v1 = mIndices[i++];
        v2 = mIndices[i++];
        x0 = (int)mVertices[v0].X();
        y0 = (int)mVertices[v0].Y();
        x1 = (int)mVertices[v1].X();
        y1 = (int)mVertices[v1].Y();
        x2 = (int)mVertices[v2].X();
        y2 = (int)mVertices[v2].Y();
        DrawLine(x0, y0, x1, y1, black);
        DrawLine(x1, y1, x2, y2, black);
        DrawLine(x2, y2, x0, y0, black);
    }

    // Draw the mesh envelope.
    ColorRGB red(255, 0, 0);
    x0 = (int)mEnvelopeVertices[0].X();
    y0 = (int)mEnvelopeVertices[0].Y();
    x1 = x0;
    y1 = y0;
    for (i = 1; i < mNumEnvelopeVertices; ++i)
    {
        x2 = (int)mEnvelopeVertices[i].X();
        y2 = (int)mEnvelopeVertices[i].Y();
        DrawLine(x1, y1, x2, y2, red);
        x1 = x2;
        y1 = y2;
    }
    DrawLine(x1, y1, x0, y0, red);

    WindowApplication2::OnDisplay();
}
//----------------------------------------------------------------------------