File: AmiraMeshIO.cpp

package info (click to toggle)
psurface 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,016 kB
  • ctags: 1,077
  • sloc: cpp: 12,335; makefile: 111; awk: 38
file content (541 lines) | stat: -rw-r--r-- 19,696 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include "config.h"

#include <vector>

#ifdef PSURFACE_STANDALONE
#include "TargetSurface.h"
#else
#include "hxsurface/Surface.h"
#endif

#include "StaticVector.h"
#include "AmiraMeshIO.h"
#include "Domains.h"
#include "PSurface.h"
#include "PSurfaceFactory.h"

#if defined HAVE_AMIRAMESH || !defined PSURFACE_STANDALONE
#include <amiramesh/AmiraMesh.h>
#endif

using namespace psurface;

/** \brief Tiny array class with controlled length

The AmiraMesh library cannot handle vectors of vectors.  Therefore the list
of triangle indices is returned as int* rather than array<int,3>*.  For easier
access I would like to cast to array<int,3>, but that is dangerous.  In various
gcc implementations of array, its size is more than sizeof(type)*N.
What I need is a replacement for array with guaranteed size, so the cast works.
*/

template <class T, int N>
struct MiniArray
{

    T& operator[](int index) {
        return data_[index];
    }

    const T& operator[](int index) const {
        return data_[index];
    }

    T data_[N];
};



#if defined HAVE_AMIRAMESH || !defined PSURFACE_STANDALONE
template <class ctype>
int psurface::AmiraMeshIO<ctype>::writeAmiraMesh(PSurface<2,ctype>* par, const char* filename)
{
    AmiraMesh am;

    int i, j;
    int numVertices  = par->getNumVertices();
    int numTriangles = par->getNumTriangles();

    am.parameters.set("ContentType", "Parametrization");


    //////////////////////////////////////
    // store base grid vertices
    AmiraMesh::Location* vertices = new AmiraMesh::Location("BaseGridVertexCoords", numVertices);
    am.insert(vertices);

    std::vector<StaticVector<ctype,3> > baseGridVertexCoordsArray(numVertices);
    for (size_t i=0; i<par->getNumVertices(); i++)
        baseGridVertexCoordsArray[i] = par->vertices(i);

    AmiraMesh::Data* vertexCoords = new AmiraMesh::Data("BaseGridVertexCoords", vertices,
                                                        McPrimType::mc_float, 3,
                                                        (void*)&baseGridVertexCoordsArray[0]);
    am.insert(vertexCoords);

    /////////////////////////////////////
    // store base grid triangles
    AmiraMesh::Location* triangles = new AmiraMesh::Location("BaseGridTriangles", numTriangles);
    am.insert(triangles);

    std::vector<MiniArray<int, 3> > baseGridTriArray(numTriangles);

    for (size_t i=0; i<par->getNumTriangles(); i++)
        for (int j=0; j<3; j++)
            baseGridTriArray[i][j] = par->triangles(i).vertices[j];


    AmiraMesh::Data* triangleCoords = new AmiraMesh::Data("BaseGridTriangles", triangles,
                                                          McPrimType::mc_int32, 3,
                                                          (void*)&baseGridTriArray[0]);
    am.insert(triangleCoords);

    ////////////////////////////////////////
    // the image positions of the nodes
    AmiraMesh::Location* nodePosLoc = new AmiraMesh::Location("NodePositions", par->iPos.size());
    am.insert(nodePosLoc);

    AmiraMesh::Data* nodePosData = new AmiraMesh::Data("NodePositions", nodePosLoc,
                                                       McPrimType::mc_float, 3,
                                                       (void*)&par->iPos[0]);
    am.insert(nodePosData);

    ////////////////////////////////////////////////////////////
    // the number of nodes and parameterEdges per triangle and the
    // patch it belongs to.  Plus the NodeNumber of the three corner nodes
    //
    AmiraMesh::Location* numNodesAndEdges = new AmiraMesh::Location("NumNodesAndParameterEdgesPerTriangle", numTriangles);
    am.insert(numNodesAndEdges);

    int numNodes      = 0;
    int numParamEdges = 0;
    int numEdgePoints = 0;

    std::vector<int> numNodesAndEdgesArray(11*numTriangles);

    for (i=0; i<numTriangles; i++) {

        const DomainTriangle<ctype>& cT = par->triangles(i);

        int numIntersectionNodes;
        int numTouchingNodes;
        int numInteriorNodes;

        cT.countNodes(numIntersectionNodes, numTouchingNodes, numInteriorNodes);
        int numEdges = cT.getNumRegularEdges();

        numNodesAndEdgesArray[11*i+0] = numIntersectionNodes;
        numNodesAndEdgesArray[11*i+1] = numTouchingNodes;
        numNodesAndEdgesArray[11*i+2] = numInteriorNodes;
        numNodesAndEdgesArray[11*i+3] = numEdges;
        numNodesAndEdgesArray[11*i+4] = cT.patch;

        numNodesAndEdgesArray[11*i+5] = cT.edgePoints[0].size()-2;
        numNodesAndEdgesArray[11*i+6] = cT.edgePoints[1].size()-2;
        numNodesAndEdgesArray[11*i+7] = cT.edgePoints[2].size()-2;

        numNodesAndEdgesArray[11*i+8] = cT.nodes[cT.cornerNode(0)].getNodeNumber();
        numNodesAndEdgesArray[11*i+9] = cT.nodes[cT.cornerNode(1)].getNodeNumber();
        numNodesAndEdgesArray[11*i+10] = cT.nodes[cT.cornerNode(2)].getNodeNumber();

        numNodes += numIntersectionNodes;
        numNodes += numTouchingNodes;
        numNodes += numInteriorNodes;

        numEdgePoints += cT.edgePoints[0].size() + cT.edgePoints[1].size() + cT.edgePoints[2].size() - 6;

        numParamEdges += numEdges;

    }

    AmiraMesh::Data*  numNodesAndEdgesData = new AmiraMesh::Data("NumNodesAndParameterEdgesPerTriangle",
                                                                 numNodesAndEdges,
                                                                 McPrimType::mc_int32, 11,
                                                                 (void*)&numNodesAndEdgesArray[0]);
    am.insert(numNodesAndEdgesData);

    /////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////

    std::vector<StaticVector<ctype,2> > domainPositions(numNodes);
    std::vector<int>     nodeNumbers(numNodes);
    std::vector<MiniArray<int,2> > parameterEdgeArray(numParamEdges);
    std::vector<int>     edgePointsArray(numEdgePoints);

    int arrayIdx           = 0;
    int edgeArrayIdx       = 0;
    int edgePointsArrayIdx = 0;

    for (i=0; i<numTriangles; i++) {

        const DomainTriangle<ctype>& cT = par->triangles(i);

        std::vector<int> newIdx(cT.nodes.size());
        int localArrayIdx = 3;
        // the cornerNode are not saved, because everything about them
        // can be deduced from the base grid
        newIdx[cT.cornerNode(0)] = 0;
        newIdx[cT.cornerNode(1)] = 1;
        newIdx[cT.cornerNode(2)] = 2;

        // the three remaining types are saved separately, in order to avoid
        // explicitly saving the type for each node.
        for (size_t cN=0; cN<cT.nodes.size(); cN++) {
            if (cT.nodes[cN].isINTERSECTION_NODE()){

                domainPositions[arrayIdx] = cT.nodes[cN].domainPos();
                nodeNumbers[arrayIdx]     = cT.nodes[cN].getNodeNumber();

                newIdx[cN] = localArrayIdx;

                arrayIdx++;
                localArrayIdx++;
            }
        }

        for (size_t cN=0; cN<cT.nodes.size(); cN++) {

            if (cT.nodes[cN].isTOUCHING_NODE()){

                domainPositions[arrayIdx] = cT.nodes[cN].domainPos();
                nodeNumbers[arrayIdx]     = cT.nodes[cN].getNodeNumber();

                newIdx[cN] = localArrayIdx;

                arrayIdx++;
                localArrayIdx++;
            }
        }

        for (size_t cN=0; cN<cT.nodes.size(); cN++) {

            if (cT.nodes[cN].isINTERIOR_NODE()){

                domainPositions[arrayIdx] = cT.nodes[cN].domainPos();
                nodeNumbers[arrayIdx]     = cT.nodes[cN].getNodeNumber();

                newIdx[cN] = localArrayIdx;

                arrayIdx++;
                localArrayIdx++;
            }
        }

        // ///////////////////////////////////////
        // the parameterEdges for this triangle
        // ///////////////////////////////////////

        typename PlaneParam<ctype>::UndirectedEdgeIterator cE;
        for (cE = cT.firstUndirectedEdge(); cE.isValid(); ++cE){
            if (cE.isRegularEdge()) {
                parameterEdgeArray[edgeArrayIdx][0] = newIdx[cE.from()];
                parameterEdgeArray[edgeArrayIdx][1] = newIdx[cE.to()];
                edgeArrayIdx++;
            }
        }


        ///////////////////////////////////////
        // the edgePoints for this triangle
        ///////////////////////////////////////
        for (j=0; j<3; j++){

            for (size_t k=1; k<cT.edgePoints[j].size()-1; k++)
                edgePointsArray[edgePointsArrayIdx++] = newIdx[cT.edgePoints[j][k]];

        }
    }

    AmiraMesh::Location* nodeLoc = new AmiraMesh::Location("Nodes", numNodes);
    am.insert(nodeLoc);

    AmiraMesh::Data*  nodeData   = new AmiraMesh::Data("Nodes", nodeLoc,
                                                       McPrimType::mc_float, 2, (void*)&domainPositions[0]);
    am.insert(nodeData);

    AmiraMesh::Location* nNLoc = new AmiraMesh::Location("NodeNumbers", numNodes);
    am.insert(nNLoc);

    AmiraMesh::Data* nNData    = new AmiraMesh::Data("NodeNumbers", nNLoc,
                                                     McPrimType::mc_int32, 1, (void*)&nodeNumbers[0]);
    am.insert(nNData);


    AmiraMesh::Location* parameterEdges = new AmiraMesh::Location("ParameterEdges", numParamEdges);
    am.insert(parameterEdges);

    AmiraMesh::Data*  parameterEdgeData = new AmiraMesh::Data("ParameterEdges", parameterEdges,
                                                              McPrimType::mc_int32, 2, (void*)&parameterEdgeArray[0]);
    am.insert(parameterEdgeData);

    /////////////////////////////////////////
    // The edgePoints arrays

    AmiraMesh::Location* edgePoints = new AmiraMesh::Location("EdgePoints", edgePointsArray.size());
    am.insert(edgePoints);

    AmiraMesh::Data* edgePointsData = new AmiraMesh::Data("EdgePoints", edgePoints,
                                                          McPrimType::mc_int32, 1,
                                                          (void*)&edgePointsArray[0]);
    am.insert(edgePointsData);

    //////////////////////////////
    // actually write the file
    //////////////////////////////
    if (!am.write(filename) ) {
        printf("An error has occured writing file %s.\n", filename);
        return 0;
    }

    return 1;

}

template <class ctype>
void* psurface::AmiraMeshIO<ctype>::readAmiraMesh(AmiraMesh* am, const char* filename)
{
    PSurface<2,ctype>* par = new PSurface<2,ctype>;

    Surface* surf = new Surface;

    if (!initFromAmiraMesh(par, am, filename, surf)) {
        delete par;
        delete surf;

        return NULL;
    }

    return par;
}

template <class ctype>
bool psurface::AmiraMeshIO<ctype>::initFromAmiraMesh(psurface::PSurface<2,ctype>* psurf, AmiraMesh* am, const char* filename, Surface* surf)
{
    // //////////////////////////////////
    //   Create PSurface factory
    // //////////////////////////////////

    PSurfaceFactory<2,ctype> factory(psurf);

    // Target surface already exists
    factory.setTargetSurface(surf);

    ///////////////////////////////////////////////
    // test for file format
    ///////////////////////////////////////////////
    AmiraMesh::Data* AMnodePos = am->findData("NodePositions", HxFLOAT, 3, "NodePositions");

    if (!AMnodePos){
        printf("You're trying to read a file in the old-style AmiraMesh format.\n");
        printf("But the format is not supported anymore :-(\n");
        return false;
    }

    //////////////////////////////////
    // load the base grid vertices
    //////////////////////////////////
    AmiraMesh::Data* AMvertices = am->findData("BaseGridVertexCoords", HxFLOAT, 3, "BaseGridVertexCoords");
    if (!AMvertices){
        printf("AmiraMesh: Field 'BaseGridVertexCoords' not found!\n");
        return false;
    }

    int numPoints = AMvertices->location()->dims()[0];

    // copy points
    StaticVector<ctype,3> newVertex;
    for (int i=0; i<numPoints; i++) {
        for (int j=0; j<3; j++)
            newVertex[j] = ((float*)AMvertices->dataPtr())[i*3+j];
        factory.insertVertex(newVertex);
    }

    // /////////////////////////////////////////////////////
    //  copy node positions
    // /////////////////////////////////////////////////////
    psurf->iPos.resize(AMnodePos->location()->dims()[0]);

    for (size_t i=0; i<psurf->iPos.size(); i++)
        for (int j=0; j<3; j++)
            psurf->iPos[i][j] = ((float(*)[3])AMnodePos->dataPtr())[i][j];

    ////////////////////////////////////////////////////////
    // copy triangles.  This takes care of the edges, too
    ////////////////////////////////////////////////////////
    AmiraMesh::Data* AMtriangles = am->findData("BaseGridTriangles", HxINT32, 3, "BaseGridTriangles");
    if (!AMtriangles){
        printf("AmiraMesh: Field 'BaseGridTriangles' not found!\n");
        return false;
    }

    const int numTriangles = AMtriangles->location()->dims()[0];

    AmiraMesh::Data* numNodesAndEdges = am->findData("NumNodesAndParameterEdgesPerTriangle", HxINT32, 11,
                                                     "NumNodesAndParameterEdgesPerTriangle");
    if (!numNodesAndEdges){
        printf("AmiraMesh: Field 'NumNodesAndParameterEdgesPerTriangle' not found!\n");
        return false;
    }
    AmiraMesh::Data* nodes            = am->findData("Nodes", HxFLOAT, 2, "Nodes");
    if (!nodes){
        printf("AmiraMesh: Field 'Nodes' not found!\n");
        return false;
    }

    AmiraMesh::Data* AMnodeNumbers    = am->findData("NodeNumbers", HxINT32, 1, "NodeNumbers");
    if (!AMnodeNumbers){
        printf("AmiraMesh: Field 'NodeNumbers' not found!\n");
        return false;
    }

    AmiraMesh::Data* AMedges            = am->findData("ParameterEdges", HxINT32, 2, "ParameterEdges");
    if (!AMedges){
        printf("AmiraMesh: Field 'ParameterEdges' not found!\n");
        return false;
    }
    AmiraMesh::Data* AMedgePoints      = am->findData("EdgePoints", HxINT32, 1, "EdgePoints");
    if (!AMedgePoints){
        printf("AmiraMesh: Field 'EdgePoints' not found!\n");
        return false;
    }

    const int* numNodesAndEdgesData = (int*)numNodesAndEdges->dataPtr();
    const MiniArray<float,2>*   nodeData       = (MiniArray<float,2>*)nodes->dataPtr();
    const int* nodeNumbers          = (int*)AMnodeNumbers->dataPtr();
    const MiniArray<int,2>* edgeData  = (MiniArray<int,2>*)AMedges->dataPtr();
    const int*     edgePointData    = (int*)AMedgePoints->dataPtr();


    int edgeCounter=0, edgePointCounter=0;
    int nodeArrayIdx = 0;

    for (int i=0; i<numTriangles; i++){

        //int newTriIdx = psurface->createSpaceForTriangle(triIdx[i][0], triIdx[i][1], triIdx[i][2]);
        std::tr1::array<unsigned int, 3> triangleVertices = {((unsigned int*)AMtriangles->dataPtr())[3*i+0],
                                                             ((unsigned int*)AMtriangles->dataPtr())[3*i+1],
                                                             ((unsigned int*)AMtriangles->dataPtr())[3*i+2]};
        int newTriIdx = factory.insertSimplex(triangleVertices);

        psurf->triangles(newTriIdx).patch = numNodesAndEdgesData[11*i+4];

        ///////////////////////////////////////////////
        // get the parametrization on this triangle
        ///////////////////////////////////////////////

        int numIntersectionNodes = numNodesAndEdgesData[11*i+0];
        int numTouchingNodes     = numNodesAndEdgesData[11*i+1];
        int numInteriorNodes     = numNodesAndEdgesData[11*i+2];

        int numParamEdges        = numNodesAndEdgesData[11*i+3];

        ////////////////////////////////////
        // first the nodes
        ////////////////////////////////////

        psurf->triangles(newTriIdx).nodes.resize(numIntersectionNodes + numTouchingNodes + numInteriorNodes + 3);

        // three corner nodes
        StaticVector<ctype,2> domainPos(1, 0);
        int nodeNumber = numNodesAndEdgesData[11*i+8];
        psurf->triangles(newTriIdx).nodes[0].setValue(domainPos, nodeNumber, Node<ctype>::CORNER_NODE);
        psurf->triangles(newTriIdx).nodes[0].makeCornerNode(0, nodeNumber);

        domainPos = StaticVector<ctype,2>(0, 1);
        nodeNumber = numNodesAndEdgesData[11*i+9];
        psurf->triangles(newTriIdx).nodes[1].setValue(domainPos, nodeNumber, Node<ctype>::CORNER_NODE);
        psurf->triangles(newTriIdx).nodes[1].makeCornerNode(1, nodeNumber);

        domainPos = StaticVector<ctype,2>(0, 0);
        nodeNumber = numNodesAndEdgesData[11*i+10];
        psurf->triangles(newTriIdx).nodes[2].setValue(domainPos, nodeNumber, Node<ctype>::CORNER_NODE);
        psurf->triangles(newTriIdx).nodes[2].makeCornerNode(2, nodeNumber);

        int nodeCounter = 3;

        // the intersection nodes
        for (int j=0; j<numIntersectionNodes; j++, nodeCounter++, nodeArrayIdx++){

            // float --> double
            StaticVector<ctype,2> domainPos;
            domainPos[0] = nodeData[nodeArrayIdx][0];
            domainPos[1] = nodeData[nodeArrayIdx][1];

            int nodeNumber    = nodeNumbers[nodeArrayIdx];

            psurf->triangles(newTriIdx).nodes[nodeCounter].setValue(domainPos, nodeNumber, Node<ctype>::INTERSECTION_NODE);
        }

        // the touching nodes
        for (int j=0; j<numTouchingNodes; j++, nodeCounter++, nodeArrayIdx++){

            // float --> double
            StaticVector<ctype,2> domainPos;
            domainPos[0] = nodeData[nodeArrayIdx][0];
            domainPos[1] = nodeData[nodeArrayIdx][1];

            int nodeNumber    = nodeNumbers[nodeArrayIdx];

            psurf->triangles(newTriIdx).nodes[nodeCounter].setValue(domainPos, nodeNumber, Node<ctype>::TOUCHING_NODE);
        }

        // the interior nodes
        for (int j=0; j<numInteriorNodes; j++, nodeCounter++, nodeArrayIdx++){

            // float --> double
            StaticVector<ctype,2> domainPos;
            domainPos[0] = nodeData[nodeArrayIdx][0];
            domainPos[1] = nodeData[nodeArrayIdx][1];

            int nodeNumber    = nodeNumbers[nodeArrayIdx];

            psurf->triangles(newTriIdx).nodes[nodeCounter].setValue(domainPos, nodeNumber, Node<ctype>::INTERIOR_NODE);
        }

        ///////////////////////////////
        // the parameterEdges
        ///////////////////////////////
        for (int j=0; j<numParamEdges; j++, edgeCounter++)
            psurf->triangles(newTriIdx).addEdge(edgeData[edgeCounter][0], edgeData[edgeCounter][1]);

        //////////////////////////////////
        // the edgePoints arrays
        //////////////////////////////////
        for (int j=0; j<3; j++){

            psurf->triangles(newTriIdx).edgePoints[j].resize(numNodesAndEdgesData[11*i+5+j] + 2);

            psurf->triangles(newTriIdx).edgePoints[j][0]     = j;
            psurf->triangles(newTriIdx).edgePoints[j].back() = (j+1)%3;

            for (int k=0; k<numNodesAndEdgesData[11*i+5+j]; k++){
                psurf->triangles(newTriIdx).edgePoints[j][k+1] = edgePointData[edgePointCounter];
                edgePointCounter++;
            }

        }

        //psurface->integrateTriangle(newTriIdx);
    }

    // sad but true ...
    psurf->hasUpToDatePointLocationStructure = false;

    psurf->setupOriginalSurface();

    return true;
}


// ////////////////////////////////////////////////////////
//   Explicit template instantiations.
//   If you need more, you can add them here.
// ////////////////////////////////////////////////////////

namespace psurface {
  template class PSURFACE_EXPORT psurface::AmiraMeshIO<float>;
  template class PSURFACE_EXPORT psurface::AmiraMeshIO<double>;
}

#endif