File: main.cpp

package info (click to toggle)
opensubdiv 3.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,132 kB
  • sloc: cpp: 137,820; python: 1,069; objc: 412; javascript: 361; lisp: 216; ansic: 170; makefile: 24
file content (339 lines) | stat: -rw-r--r-- 10,675 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
//
//   Copyright 2013 Pixar
//
//   Licensed under the Apache License, Version 2.0 (the "Apache License")
//   with the following modification; you may not use this file except in
//   compliance with the Apache License and the following modification to it:
//   Section 6. Trademarks. is deleted and replaced with:
//
//   6. Trademarks. This License does not grant permission to use the trade
//      names, trademarks, service marks, or product names of the Licensor
//      and its affiliates, except as required to comply with Section 4(c) of
//      the License and to reproduce the content of the NOTICE file.
//
//   You may obtain a copy of the Apache License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the Apache License with the above modification is
//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//   KIND, either express or implied. See the Apache License for the specific
//   language governing permissions and limitations under the Apache License.
//

#include <stdio.h>

#include "../../regression/common/hbr_utils.h"

//
// Regression testing matching Hbr to a pre-generated data-set
//

// Precision is currently held at bit-wise identical
static bool g_allowWeakRegression=true,
            g_strictRegressionFailure=false,
            g_verbose=false;

#define STRICT_PRECISION 0
#define WEAK_PRECISION 1e-6

//------------------------------------------------------------------------------
// Vertex class implementation
struct xyzVV {

    xyzVV() { }

    xyzVV( int /*i*/ ) { }

    xyzVV( float x, float y, float z ) { _pos[0]=x; _pos[1]=y; _pos[2]=z; }

    xyzVV( const xyzVV & src ) { _pos[0]=src._pos[0]; _pos[1]=src._pos[1]; _pos[2]=src._pos[2]; }

   ~xyzVV( ) { }

    void     AddWithWeight(const xyzVV& src, float weight, void * =0 ) {
        _pos[0]+=weight*src._pos[0];
        _pos[1]+=weight*src._pos[1];
        _pos[2]+=weight*src._pos[2];
    }

    void     AddVaryingWithWeight(const xyzVV& , float, void * =0 ) { }

    void     Clear( void * =0 ) { _pos[0]=_pos[1]=_pos[2]=0.0f; }

    void     SetPosition(float x, float y, float z) { _pos[0]=x; _pos[1]=y; _pos[2]=z; }

    void     ApplyVertexEdit(const OpenSubdiv::HbrVertexEdit<xyzVV> & edit) {
                 const float *src = edit.GetEdit();
                 switch(edit.GetOperation()) {
                   case OpenSubdiv::HbrHierarchicalEdit<xyzVV>::Set:
                     _pos[0] = src[0];
                     _pos[1] = src[1];
                     _pos[2] = src[2];
                     break;
                   case OpenSubdiv::HbrHierarchicalEdit<xyzVV>::Add:
                     _pos[0] += src[0];
                     _pos[1] += src[1];
                     _pos[2] += src[2];
                     break;
                   case OpenSubdiv::HbrHierarchicalEdit<xyzVV>::Subtract:
                     _pos[0] -= src[0];
                     _pos[1] -= src[1];
                     _pos[2] -= src[2];
                     break;
                 }
             }

    void     ApplyMovingVertexEdit(const OpenSubdiv::HbrMovingVertexEdit<xyzVV> &) { }

    const float * GetPos() const { return _pos; }

private:
    float _pos[3];
};

//------------------------------------------------------------------------------
class xyzFV;
typedef OpenSubdiv::HbrMesh<xyzVV>           xyzmesh;
typedef OpenSubdiv::HbrFace<xyzVV>           xyzface;
typedef OpenSubdiv::HbrVertex<xyzVV>         xyzvertex;
typedef OpenSubdiv::HbrHalfedge<xyzVV>       xyzhalfedge;
typedef OpenSubdiv::HbrFaceOperator<xyzVV>   xyzFaceOperator;
typedef OpenSubdiv::HbrVertexOperator<xyzVV> xyzVertexOperator;

//------------------------------------------------------------------------------

#include "./init_shapes.h"

static Shape * readShape( char const * fname, Scheme scheme ) {

    FILE * handle = fopen( fname, "rt" );
    if (! handle) {
        printf("Could not open \"%s\" - aborting.\n", fname);
        exit(0);
    }

    fseek( handle, 0, SEEK_END );
    size_t size = ftell(handle);
    fseek( handle, 0, SEEK_SET );

    char * shapeStr = new char[size+1];

    if ( fread( shapeStr, size, 1, handle)!=1 ) {
        printf("Error reading \"%s\" - aborting.\n", fname);
        exit(0);
    }

    fclose(handle);

    shapeStr[size]='\0';

    return Shape::parseObj( shapeStr, scheme );
}

#define STR(x) x

#ifdef  HBR_BASELINE_DIR
    std::string g_baseline_path = STR(HBR_BASELINE_DIR);
#else
    std::string g_baseline_path;
#endif

//------------------------------------------------------------------------------
static void writeObj( const char * fname, xyzmesh const * mesh,
    int firstface, int lastface, int firstvert, int lastvert ) {

    FILE * handle = fopen( fname, "w" );
    if (! handle) {
        printf("Could not open \"%s\" - aborting.\n", fname);
        exit(0);
    }

    fprintf(handle, "# This file uses centimeters as units for non-parametric coordinates.\n");

    for (int i=firstvert; i<lastvert; ++i) {
        const float * pos = mesh->GetVertex(i)->GetData().GetPos();
        fprintf(handle, "v  %.*g %.*g %.*g\n", 9, pos[0], 9, pos[1], 9, pos[2]);
    }

    fprintf(handle, "s off\n");

    for (int i=firstface; i<lastface; ++i) {
        xyzface * f = mesh->GetFace(i);

        fprintf(handle, "f ");
        for (int j=0; j<f->GetNumVertices();) {

            int vert = f->GetVertex(j)->GetID()-firstvert+1;

            fprintf(handle, "%d", vert);

            if (++j<f->GetNumVertices())
                fprintf(handle, " ");
        }
        fprintf(handle, "\n");
    }

    fclose(handle);
}

//------------------------------------------------------------------------------
static int checkMesh( ShapeDesc const & r, int levels ) {

    int count=0;

    float deltaAvg[3] = {0.0f, 0.0f, 0.0f},
          deltaCnt[3] = {0.0f, 0.0f, 0.0f};

    xyzmesh * mesh = simpleHbr<xyzVV>(r.data.c_str(), r.scheme, 0);

    int firstface=0, lastface=mesh->GetNumFaces(),
        firstvert=0, lastvert=mesh->GetNumVertices(), nverts;

    static char const * schemes[] = { "Bilinear", "Catmark", "Loop" };

    printf("- %-25s ( %-8s ): ", r.name.c_str(), schemes[r.scheme]);

    for (int l=0; l<levels; ++l ) {

        int errcount=0;

        std::stringstream fname;

        fname << g_baseline_path <<  r.name << "_level" << l << ".obj";


        Shape * sh = readShape( fname.str().c_str(), r.scheme );
        assert(sh);

        // subdivide up to current level
        for (int i=firstface; i<lastface; ++i) {
            xyzface * f = mesh->GetFace(i);
            f->Refine();
        }

        firstface = lastface;
        lastface = mesh->GetNumFaces();
        //nfaces = lastface - firstface;

        firstvert = lastvert;
        lastvert = mesh->GetNumVertices();
        nverts = lastvert - firstvert;

        for (int i=firstvert; i<lastvert; ++i) {
            const float * apos = mesh->GetVertex(i)->GetData().GetPos(),
                        * bpos = &sh->verts[(i-firstvert)*3];

            if ( apos[0] != bpos[0] )
                deltaCnt[0]++;
            if ( apos[1] != bpos[1] )
                deltaCnt[1]++;
            if ( apos[2] != bpos[2] )
                deltaCnt[2]++;

            float delta[3] = { apos[0] - bpos[0],
                               apos[1] - bpos[1],
                               apos[2] - bpos[2] };

            deltaAvg[0]+=delta[0];
            deltaAvg[1]+=delta[1];
            deltaAvg[2]+=delta[2];

            float dist = sqrtf( delta[0]*delta[0]+delta[1]*delta[1]+delta[2]*delta[2]);
            if ( dist > STRICT_PRECISION ) {
                if(dist < WEAK_PRECISION && g_allowWeakRegression) {
                    g_strictRegressionFailure=true;
                } else {
                    if (g_verbose) {
                        printf("\n// HbrVertex<T> %d fails : dist=%.10f "
                            "(%.10f %.10f %.10f) (%.10f %.10f %.10f)", i, dist,
                                    apos[0], apos[1], apos[2],
                                        bpos[0], bpos[1], bpos[2] );
                    }
                    ++errcount;
                }
            }
        }

        if (errcount) {

            std::stringstream errfile;
            errfile << r.name << "_level" << l << "_error.obj";

            writeObj(errfile.str().c_str(), mesh,
                firstface, lastface, firstvert, lastvert);

            printf("\n  wrote: %s\n", errfile.str().c_str());
        }

        delete sh;
        count += errcount;
    }

    if (deltaCnt[0])
        deltaAvg[0]/=deltaCnt[0];
    if (deltaCnt[1])
        deltaAvg[1]/=deltaCnt[1];
    if (deltaCnt[2])
        deltaAvg[2]/=deltaCnt[2];

    if (g_verbose) {
        printf("\n  delta ratio : (%d/%d %d/%d %d/%d)", (int)deltaCnt[0], nverts,
                                                        (int)deltaCnt[1], nverts,
                                                        (int)deltaCnt[2], nverts );
        printf("\n  average delta : (%.10f %.10f %.10f)", deltaAvg[0],
                                                          deltaAvg[1],
                                                          deltaAvg[2] );
    }

    if (count==0) {
        printf(" success !\n");
    } else
        printf(" failed !\n");

    delete mesh;

    return count;
}

//------------------------------------------------------------------------------
static void usage(char const * appname) {
    printf("Usage : %s [options]\n", appname);
    printf("    -s | -strict  : strict bitwise comparisons\n");
    printf("    -v | -verbose : verbose output\n");
}

//------------------------------------------------------------------------------
int main(int argc, char ** argv) {

    int levels=5, total=0;

    for (int i=1; i<argc; ++i) {
        if ((! strcmp(argv[i],"-s")) || (! strcmp(argv[i],"-strict"))) {
            g_allowWeakRegression=false;
        } else if ((! strcmp(argv[i],"-v")) || (! strcmp(argv[i],"-verbose"))) {
            g_verbose=true;
        } else {
            usage( argv[1] );
            return 1;
        }
    }

    initShapes();

    printf("Baseline Path : \"%s\"\n", g_baseline_path.c_str());

    for (int i=0; i<(int)g_shapes.size(); ++i)
        total+=checkMesh( g_shapes[i], levels );

    if (total==0) {
        printf("All tests passed.\n");
        if(g_strictRegressionFailure)
            printf("Some tests were not bit-wise accurate.\n"
                   "Rerun with -s for strict regression\n");
        }
    else
      printf("Total failures : %d\n", total);

}