import.hpp 34.8 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Topology/generic/attributeHandler.h"
#include "Topology/generic/autoAttributeHandler.h"
#include "Container/fakeAttribute.h"
28
#include "Algo/Modelisation/polyhedron.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
29
30
31
32
33
34
35

namespace CGoGN
{

namespace Algo
{

36
37
38
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
39
40
41
42
43
44
namespace Import
{

template <typename PFP>
bool importMesh(typename PFP::MAP& map, MeshTablesSurface<PFP>& mts)
{
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
    VertexAutoAttribute< NoTypeNameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, "incidents");

    unsigned nbf = mts.getNbFaces();
    int index = 0;
    // buffer for tempo faces (used to remove degenerated edges)
    std::vector<unsigned int> edgesBuffer;
    edgesBuffer.reserve(16);

    DartMarkerNoUnmark m(map) ;

    FunctorInitEmb<typename PFP::MAP, VERTEX> fsetemb(map);

    // for each face of table
    for(unsigned int i = 0; i < nbf; ++i)
    {
        // store face in buffer, removing degenerated edges
        unsigned int nbe = mts.getNbEdgesFace(i);
        edgesBuffer.clear();
        unsigned int prec = EMBNULL;
        for (unsigned int j = 0; j < nbe; ++j)
        {
            unsigned int em = mts.getEmbIdx(index++);
            if (em != prec)
            {
                prec = em;
                edgesBuffer.push_back(em);
            }
        }
        // check first/last vertices
        if (edgesBuffer.front() == edgesBuffer.back())
            edgesBuffer.pop_back();

        // create only non degenerated faces
        nbe = edgesBuffer.size();
        if (nbe > 2)
        {
            Dart d = map.newFace(nbe, false);
            for (unsigned int j = 0; j < nbe; ++j)
            {
                unsigned int em = edgesBuffer[j];		// get embedding
                fsetemb.changeEmb(em) ;
                map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);

                m.mark(d) ;								// mark on the fly to unmark on second loop
                vecDartsPerVertex[em].push_back(d);		// store incident darts for fast adjacency reconstruction
                d = map.phi1(d);
            }
        }
    }

    bool needBijectiveCheck = false;

    // reconstruct neighbourhood
    unsigned int nbBoundaryEdges = 0;
    for (Dart d = map.begin(); d != map.end(); map.next(d))
    {
        if (m.isMarked(d))
        {
            // darts incident to end vertex of edge
            std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)];

            unsigned int embd = map.template getEmbedding<VERTEX>(d);
            Dart good_dart = NIL;
            bool firstOK = true;
            for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
            {
                if (map.template getEmbedding<VERTEX>(map.phi1(*it)) == embd)
                {
                    good_dart = *it;
                    if (good_dart == map.phi2(good_dart))
                    {
                        map.sewFaces(d, good_dart, false);
                        m.unmarkOrbit<EDGE>(d);
                    }
                    else
                    {
                        good_dart = NIL;
                        firstOK = false;
                    }
                }
            }

            if (!firstOK)
                needBijectiveCheck = true;

            if (good_dart == NIL)
            {
                m.unmarkOrbit<EDGE>(d);
                ++nbBoundaryEdges;
            }
        }
    }

    if (nbBoundaryEdges > 0)
    {
        unsigned int nbH = map.closeMap();
        CGoGNout << "Map closed (" << nbBoundaryEdges << " boundary edges / " << nbH << " holes)" << CGoGNendl;
    }

    if (needBijectiveCheck)
    {
        // ensure bijection between topo and embedding
        map.template bijectiveOrbitEmbedding<VERTEX>();
    }

    return true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
151
152
}

153

154
template <typename PFP>
155
156
bool importMesh(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, bool mergeCloseVertices)
{
157
    MeshTablesSurface<PFP> mts(map);
158

159
160
    if(!mts.importMesh(filename, attrNames))
        return false;
161

162
163
    if (mergeCloseVertices)
        mts.mergeCloseVertices();
164

165
    return importMesh<PFP>(map, mts);
166
167
}

Pierre Kraemer's avatar
Pierre Kraemer committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
template <typename PFP>
bool importVoxellisation(typename PFP::MAP& map, Algo::Surface::Modelisation::Voxellisation& voxellisation, std::vector<std::string>& attrNames, bool mergeCloseVertices)
{
    MeshTablesSurface<PFP> mts(map);

    if(!mts.importVoxellisation(voxellisation, attrNames))
        return false;

    if (mergeCloseVertices)
        mts.mergeCloseVertices();

    return importMesh<PFP>(map, mts);
}

182
183
184
185

template <typename PFP>
bool importMeshSAsV(typename PFP::MAP& map, MeshTablesSurface<PFP>& mts)
{
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
    VertexAutoAttribute< NoTypeNameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, "incidents");

    unsigned nbf = mts.getNbFaces();
    int index = 0;
    // buffer for tempo faces (used to remove degenerated edges)
    std::vector<unsigned int> edgesBuffer;
    edgesBuffer.reserve(16);

    DartMarkerNoUnmark m(map) ;

    // for each face of table
    for(unsigned int i = 0; i < nbf; ++i)
    {
        // store face in buffer, removing degenerated edges
        unsigned int nbe = mts.getNbEdgesFace(i);
        edgesBuffer.clear();
        unsigned int prec = EMBNULL;
        for (unsigned int j = 0; j < nbe; ++j)
        {
            unsigned int em = mts.getEmbIdx(index++);
            if (em != prec)
            {
                prec = em;
                edgesBuffer.push_back(em);
            }
        }
        // check first/last vertices
        if (edgesBuffer.front() == edgesBuffer.back())
            edgesBuffer.pop_back();

        // create only non degenerated faces
        nbe = edgesBuffer.size();
        if (nbe > 2)
        {
            Dart d = map.newFace(nbe, false);
            for (unsigned int j = 0; j < nbe; ++j)
            {
                unsigned int em = edgesBuffer[j];		// get embedding
224

225
                FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, em);
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
                map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT2>(d, fsetemb);

                m.mark(d) ;								// mark on the fly to unmark on second loop
                vecDartsPerVertex[em].push_back(d);		// store incident darts for fast adjacency reconstruction
                d = map.phi1(d);
            }
        }
    }

    // reconstruct neighbourhood
    unsigned int nbBoundaryEdges = 0;
    for (Dart d = map.begin(); d != map.end(); map.next(d))
    {
        if (m.isMarked(d))
        {
            // darts incident to end vertex of edge
            std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)];

            unsigned int embd = map.template getEmbedding<VERTEX>(d);
            Dart good_dart = NIL;
            for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
            {
                if (map.template getEmbedding<VERTEX>(map.phi1(*it)) == embd)
                    good_dart = *it;
            }

            if (good_dart != NIL)
            {
                map.sewFaces(d, good_dart, false);
                m.unmarkOrbit<EDGE>(d);
            }
            else
            {
                m.unmark(d);
                ++nbBoundaryEdges;
            }
        }
    }

    unsigned int nbH = map.closeMap();
    CGoGNout << "Map closed (" << map.template getNbOrbits<FACE>() << " boundary faces / " << nbH << " holes)" << CGoGNendl;
    std::cout << "nb darts : " << map.getNbDarts() << std::endl ;
    // ensure bijection between topo and embedding
    //map.template bijectiveOrbitEmbedding<VERTEX>();

    return true ;
272
273
274
275
276
}

template <typename PFP>
bool importMeshSAsV(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames)
{
277
    MeshTablesSurface<PFP> mts(map);
278

279
280
    if(!mts.importMesh(filename, attrNames))
        return false;
281

282
    return importMeshSAsV<PFP>(map, mts);
283
284
285
}


286
287
288
289
} // namespace Import

} // namespace Surface

290
291
292
293
294
295
296
297
298
299


namespace Volume
{

namespace Import
{

template <typename PFP>
bool importMeshSToV(typename PFP::MAP& map, Surface::Import::MeshTablesSurface<PFP>& mts, float dist)
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
    VertexAutoAttribute< NoTypeNameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, "incidents");
    unsigned nbf = mts.getNbFaces();
    int index = 0;
    // buffer for tempo faces (used to remove degenerated edges)
    std::vector<unsigned int> edgesBuffer;
    edgesBuffer.reserve(16);

    DartMarkerNoUnmark m(map) ;

    VertexAttribute<typename PFP::VEC3> position = map.template getAttribute<typename PFP::VEC3, VERTEX>("position");
    std::vector<unsigned int > backEdgesBuffer(mts.getNbVertices(), EMBNULL);

    // for each face of table -> create a prism
    for(unsigned int i = 0; i < nbf; ++i)
    {
        // store face in buffer, removing degenerated edges
        unsigned int nbe = mts.getNbEdgesFace(i);
        edgesBuffer.clear();
        unsigned int prec = EMBNULL;
        for (unsigned int j = 0; j < nbe; ++j)
        {
            unsigned int em = mts.getEmbIdx(index++);
            if (em != prec)
            {
                prec = em;
                edgesBuffer.push_back(em);
            }
        }
        // check first/last vertices
        if (edgesBuffer.front() == edgesBuffer.back())
            edgesBuffer.pop_back();

        // create only non degenerated faces
        nbe = edgesBuffer.size();
        if (nbe > 2)
        {
            Dart d = Surface::Modelisation::createPrism<PFP>(map, nbe,false);

            //Embed the base faces
            for (unsigned int j = 0; j < nbe; ++j)
            {
                unsigned int em = edgesBuffer[j];		// get embedding

                if(backEdgesBuffer[em] == EMBNULL)
                {
                    unsigned int emn = map.template newCell<VERTEX>();
                    map.template copyCell<VERTEX>(emn, em);
                    backEdgesBuffer[em] = emn;
                    position[emn] += typename PFP::VEC3(0,0,dist);
                }

                FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, em);
                //foreach_dart_of_orbit_in_parent<typename PFP::MAP>(&map, VERTEX, d, fsetemb) ;
                map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);

                //Embed the other base face
                Dart d2 = map.phi1(map.phi1(map.phi2(d)));
                unsigned int em2 = backEdgesBuffer[em];
                FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb2(map, em2);
                //foreach_dart_of_orbit_in_parent<typename PFP::MAP>(&map, VERTEX, d2, fsetemb2) ;
                map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d2, fsetemb2);

                m.mark(d) ;								// mark on the fly to unmark on second loop
                vecDartsPerVertex[em].push_back(d);		// store incident darts for fast adjacency reconstruction
                d = map.phi_1(d);
            }

        }
    }

    // reconstruct neighbourhood
    unsigned int nbBoundaryEdges = 0;
    for (Dart d = map.begin(); d != map.end(); map.next(d))
    {
        if (m.isMarked(d))
        {
            // darts incident to end vertex of edge
            std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)];

            unsigned int embd = map.template getEmbedding<VERTEX>(d);
            Dart good_dart = NIL;
            for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
            {
                if (map.template getEmbedding<VERTEX>(map.phi1(*it)) == embd)
                    good_dart = *it;
            }

            if (good_dart != NIL)
            {
                map.sewVolumes(map.phi2(d), map.phi2(good_dart), false);
                m.unmarkOrbit<EDGE>(d);
            }
            else
            {
                m.unmark(d);
                ++nbBoundaryEdges;
            }
        }
    }

    return true ;
402
403
}

404
template <typename PFP>
405
bool importMeshSurfToVol(typename PFP::MAP& map, Surface::Import::MeshTablesSurface<PFP>& mts, float scale, unsigned int nbStage)
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
    VertexAutoAttribute< NoTypeNameAttribute< std::vector<Dart> > > vecDartsPerVertex(map);
    unsigned nbf = mts.getNbFaces();
    int index = 0;
    // buffer for tempo faces (used to remove degenerated edges)
    std::vector<unsigned int> edgesBuffer;
    edgesBuffer.reserve(16);

    DartMarkerNoUnmark m(map) ;

    unsigned int nbVertices = mts.getNbVertices();

    VertexAttribute<typename PFP::VEC3> position = map.template getAttribute<typename PFP::VEC3, VERTEX>("position");
    std::vector<unsigned int > backEdgesBuffer(nbVertices*nbStage, EMBNULL);

    // for each face of table -> create a prism
    for(unsigned int i = 0; i < nbf; ++i)
    {
        // store face in buffer, removing degenerated edges
        unsigned int nbe = mts.getNbEdgesFace(i);
        edgesBuffer.clear();
        unsigned int prec = EMBNULL;
        for (unsigned int j = 0; j < nbe; ++j)
        {
            unsigned int em = mts.getEmbIdx(index++);
            if (em != prec)
            {
                prec = em;
                edgesBuffer.push_back(em);
            }
        }
        // check first/last vertices
        if (edgesBuffer.front() == edgesBuffer.back())
            edgesBuffer.pop_back();

        // create only non degenerated faces
        nbe = edgesBuffer.size();
        if (nbe > 2)
        {
            Dart dprev = NIL;

            for(unsigned int k = 0 ; k < nbStage ; ++k)
            {
                Dart d = Surface::Modelisation::createPrism<PFP>(map, nbe,false);

                //Embed the base faces
                for (unsigned int j = 0; j < nbe; ++j)
                {
                    unsigned int em = edgesBuffer[j];		// get embedding
                    Dart d2 = map.phi1(map.phi1(map.phi2(d)));

                    if(k==0)
                    {
                        FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, em);
                        map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
                        vecDartsPerVertex[em].push_back(d);		// store incident darts for fast adjacency reconstruction
                        m.mark(d) ;								// mark on the fly to unmark on second loop
                    }
                    else
                    {
                        unsigned int emn = backEdgesBuffer[((k-1)*nbVertices) + em];
                        FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, emn);
                        map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
                        vecDartsPerVertex[emn].push_back(d);		// store incident darts for fast adjacency reconstruction
                        m.mark(d) ;								// mark on the fly to unmark on second loop
                    }

                    if(backEdgesBuffer[(k*nbVertices) + em] == EMBNULL)
                    {
                        unsigned int emn = map.template newCell<VERTEX>();
                        map.template copyCell<VERTEX>(emn, em);
                        backEdgesBuffer[(k*nbVertices) + em] = emn;
                        position[emn] += typename PFP::VEC3(0,0, (k+1) * scale);
                    }

                    unsigned int em2 = backEdgesBuffer[(k*nbVertices) + em];
                    FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, em2);
                    map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d2, fsetemb);

                    d = map.phi_1(d);
                }


                if(dprev != NIL)
                    map.sewVolumes(d, map.phi2(map.phi1(map.phi1(map.phi2(dprev)))), false);

                dprev = d;
            }
        }
    }

    // reconstruct neighbourhood
    unsigned int nbBoundaryEdges = 0;
    for (Dart d = map.begin(); d != map.end(); map.next(d))
    {
        if (m.isMarked(d))
        {
            // darts incident to end vertex of edge
            std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)];

            unsigned int embd = map.template getEmbedding<VERTEX>(d);
            Dart good_dart = NIL;
            for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
            {
                if (map.template getEmbedding<VERTEX>(map.phi1(*it)) == embd)
                    good_dart = *it;
            }

            if (good_dart != NIL)
            {
                map.sewVolumes(map.phi2(d), map.phi2(good_dart), false);
                m.unmarkOrbit<EDGE>(d);
            }
            else
            {
                m.unmark(d);
                ++nbBoundaryEdges;
            }
        }
    }

    map.closeMap();

    return true ;
530
531
}

532

Pierre Kraemer's avatar
Pierre Kraemer committed
533
534
535
template <typename PFP>
bool importMesh(typename PFP::MAP& map, MeshTablesVolume<PFP>& mtv)
{
536
    VertexAutoAttribute< NoTypeNameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, "incidents");
537
538

    unsigned int nbv = mtv.getNbVolumes();
539
    unsigned int index = 0;
540
541
542
543
544
545
546
547
548
    // buffer for tempo faces (used to remove degenerated edges)
    std::vector<unsigned int> edgesBuffer;
    edgesBuffer.reserve(16);

    DartMarkerNoUnmark m(map) ;
    FunctorInitEmb<typename PFP::MAP, VERTEX> fsetemb(map);

    //for each volume of table
    for(unsigned int i = 0 ; i < nbv ; ++i)
549
    {		
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
        // store volume in buffer, removing degenated faces
        unsigned int nbf = mtv.getNbFacesVolume(i);

        edgesBuffer.clear();
        unsigned int prec = EMBNULL;
        for (unsigned int j = 0; j < nbf; ++j)
        {
            unsigned int em = mtv.getEmbIdx(index++);
            if (em != prec)
            {
                prec = em;
                edgesBuffer.push_back(em);
            }
        }

        if(nbf == 4) //tetrahedral case
566
        {			
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
            Dart d = Surface::Modelisation::createTetrahedron<PFP>(map,false);

            // Embed three "base" vertices
            for(unsigned int j = 0 ; j < 3 ; ++j)
            {
                unsigned int em = edgesBuffer[j];		// get embedding
                fsetemb.changeEmb(em) ;
                map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);

                //store darts per vertices to optimize reconstruction
                Dart dd = d;
                do
                {
                    m.mark(dd) ;
                    vecDartsPerVertex[em].push_back(dd);
                    dd = map.phi1(map.phi2(dd));
                } while(dd != d);

                d = map.phi1(d);
            }

            //Embed the last "top" vertex
            d = map.phi_1(map.phi2(d));

            unsigned int em = edgesBuffer[3];		// get embedding
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);

            //store darts per vertices to optimize reconstruction
            Dart dd = d;
            do
            {
                m.mark(dd) ;
                vecDartsPerVertex[em].push_back(dd);
                dd = map.phi1(map.phi2(dd));
            } while(dd != d);

604
		}
605
        else if(nbf == 5) //pyramidal case
606
        {			
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
            Dart d = Surface::Modelisation::createQuadrangularPyramid<PFP>(map,false);

            // 1.
            unsigned int em = edgesBuffer[0];		// get embedding
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            Dart dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 2.
            d = map.phi1(d);
            em = edgesBuffer[1];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 3.
            d = map.phi1(d);
            em = edgesBuffer[2];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 4.
            d = map.phi1(d);
            em = edgesBuffer[3];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 5.
            d = map.phi_1(map.phi2(d));
            em = edgesBuffer[4];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);
        }
untereiner's avatar
untereiner committed
659
		else
660
		if(nbf == 6) //prism case
661
        {			
untereiner's avatar
untereiner committed
662
            Dart d = Surface::Modelisation::createTriangularPrism<PFP>(map,false);			
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722

            // 1.
            unsigned int em = edgesBuffer[0];		// get embedding
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            Dart dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 2.
            d = map.phi1(d);
            em = edgesBuffer[1];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 3.
            d = map.phi1(d);
            em = edgesBuffer[2];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 5.
            d = map.template phi<2112>(d);
            em = edgesBuffer[3];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 6.
            d = map.phi_1(d);
            em = edgesBuffer[4];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 7.
            d = map.phi_1(d);
            em = edgesBuffer[5];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

723
724
		}
		else if(nbf == 8) //hexahedral case
725
        {			
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
            Dart d = Surface::Modelisation::createHexahedron<PFP>(map,false);

            // 1.
            unsigned int em = edgesBuffer[0];		// get embedding
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            Dart dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 2.
            d = map.phi1(d);
            em = edgesBuffer[1];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 3.
            d = map.phi1(d);
            em = edgesBuffer[2];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 4.
            d = map.phi1(d);
            em = edgesBuffer[3];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 5.
            d = map.template phi<2112>(d);
            em = edgesBuffer[4];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 6.
            d = map.phi_1(d);
            em = edgesBuffer[5];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 7.
            d = map.phi_1(d);
            em = edgesBuffer[6];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);

            // 8.
            d = map.phi_1(d);
            em = edgesBuffer[7];
            fsetemb.changeEmb(em) ;
            map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
            dd = d;
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd); dd = map.phi1(map.phi2(dd));
            vecDartsPerVertex[em].push_back(dd); m.mark(dd);
806

807
		}  //end of hexa
untereiner's avatar
untereiner committed
808

809
810
    }

811
812
	std	::cout << " elements created " << std::endl;

813
814
    //reconstruct neighbourhood
    unsigned int nbBoundaryFaces = 0 ;
untereiner's avatar
untereiner committed
815
816
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
817
818
819
820
821
822
823
824
        if (m.isMarked(d))
        {
            std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)];

            Dart good_dart = NIL;
            for(typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
            {
                if(map.template getEmbedding<VERTEX>(map.phi1(*it)) == map.template getEmbedding<VERTEX>(d) &&
untereiner's avatar
untereiner committed
825
				   map.template getEmbedding<VERTEX>(map.phi_1(*it)) == map.template getEmbedding<VERTEX>(map.phi1(map.phi1(d))))
826
827
828
829
830
831
832
                {
                    good_dart = *it ;
                }
            }

            if (good_dart != NIL)
            {
833
834
835
                unsigned int degD = map.faceDegree(d);
                unsigned int degGD = map.faceDegree(good_dart);

836
837
838
//				std::cout << "degD = " << degD << std::endl;
//				std::cout << "degGD = " << degGD << std::endl << std::endl;

839
840
                if(degD < degGD)
                {
841
842
843
844
845
846
					Dart dt = map.phi1(good_dart);
					map.PFP::MAP::ParentMap::splitFace(dt,map.phi_1(good_dart));

					map.template initDartEmbedding<VERTEX>(map.phi1(good_dart), map.template getEmbedding<VERTEX>(dt)) ;
					map.template initDartEmbedding<VERTEX>(map.phi_1(dt), map.template getEmbedding<VERTEX>(map.phi_1(good_dart))) ;

untereiner's avatar
untereiner committed
847
					m.mark(map.phi1(good_dart));
848
					m.mark(map.phi2(map.phi1(good_dart)));
849

untereiner's avatar
untereiner committed
850
					unsigned int emb2 = map.template getEmbedding<VERTEX>(map.phi1(map.phi1(d)));
851
					vecDartsPerVertex[emb2].push_back(map.phi2(map.phi1(good_dart)));
852

untereiner's avatar
untereiner committed
853
854
855
856
857
					unsigned int emb1 = map.template getEmbedding<VERTEX>(d);
					vecDartsPerVertex[emb1].push_back(map.phi1(good_dart));

					//m.unmarkOrbit<PFP::MAP::FACE_OF_PARENT>(d);
					//m.unmarkOrbit<PFP::MAP::FACE_OF_PARENT>(good_dart);
858
859
                }
                else if(degD > degGD)
860
861
862
863
864
865
                {					
					Dart dt = map.phi1(map.phi1(d));
					map.PFP::MAP::ParentMap::splitFace(d,dt);

					map.template initDartEmbedding<VERTEX>(map.phi_1(dt), map.template getEmbedding<VERTEX>(d)) ;
					map.template initDartEmbedding<VERTEX>(map.phi_1(d), map.template getEmbedding<VERTEX>(dt)) ;
866

untereiner's avatar
untereiner committed
867
					m.mark(map.phi_1(d));
868
					m.mark(map.phi2(map.phi_1(d)));
869

870
					//ne change rien sur l'exemple test
untereiner's avatar
untereiner committed
871
					unsigned int emb1 = map.template getEmbedding<VERTEX>(map.phi1(map.phi1(good_dart)));
872
					vecDartsPerVertex[emb1].push_back(map.phi2(map.phi_1(d)));
untereiner's avatar
untereiner committed
873
874
875
876
877
878

					unsigned int emb2 = map.template getEmbedding<VERTEX>(good_dart);
					vecDartsPerVertex[emb2].push_back(map.phi_1(d));

					//m.unmarkOrbit<PFP::MAP::FACE_OF_PARENT>(d);
					//m.unmarkOrbit<PFP::MAP::FACE_OF_PARENT>(good_dart);
879
                }
untereiner's avatar
untereiner committed
880
				else if(degD == degGD)
881
				{
untereiner's avatar
untereiner committed
882
883
884
885
886
887
888
889
890
891
					map.sewVolumes(d, good_dart, false);
					m.unmarkOrbit<FACE>(d);
				}
//				else if(degD > 3 && degGD > 3)
//				{
//					if(map.template getEmbedding<VERTEX>(map.phi1(map.phi1(good_dart))) != map.template getEmbedding<VERTEX>(map.phi_1(d)))
//					{
//						std::cout << "2 faces quad" << std::endl;
//						Dart dtgd = map.phi1(good_dart);
//						map.PFP::MAP::ParentMap::splitFace(dtgd,map.phi_1(good_dart));
892

untereiner's avatar
untereiner committed
893
894
//						map.template initDartEmbedding<VERTEX>(map.phi1(good_dart), map.template getEmbedding<VERTEX>(dtgd)) ;
//						map.template initDartEmbedding<VERTEX>(map.phi_1(dtgd), map.template getEmbedding<VERTEX>(map.phi_1(good_dart))) ;
895
896


untereiner's avatar
untereiner committed
897
898
//						Dart dt = map.phi1(map.phi1(d));
//						map.PFP::MAP::ParentMap::splitFace(d,dt);
899

untereiner's avatar
untereiner committed
900
901
902
903
//						map.template initDartEmbedding<VERTEX>(map.phi_1(dt), map.template getEmbedding<VERTEX>(d)) ;
//						map.template initDartEmbedding<VERTEX>(map.phi_1(d), map.template getEmbedding<VERTEX>(dt)) ;
//					}
//				}
904

untereiner's avatar
untereiner committed
905
906
//				map.sewVolumes(d, good_dart, false);
//				m.unmarkOrbit<FACE>(d);
907
908
909
910
911
912
913
            }
            else
            {
                m.unmarkOrbit<PFP::MAP::FACE_OF_PARENT>(d);
                ++nbBoundaryFaces;
            }
        }
untereiner's avatar
untereiner committed
914
915
916
917
918
919
920
921
922
	}

	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (m.isMarked(d))
		{
			std::cout << "marked " << std::endl;
		}
	}
923
924
925

    if (nbBoundaryFaces > 0)
    {
926
		unsigned int nbH =  map.closeMap();
927
928
929
930
        CGoGNout << "Map closed (" << nbBoundaryFaces << " boundary faces / " << nbH << " holes)" << CGoGNendl;
    }

    return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
931
932
933
}


934
template <typename PFP>
935
bool importMesh(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, bool mergeCloseVertices)
936
{
937
938
939
940
    MeshTablesVolume<PFP> mtv(map);

    if(!mtv.importMesh(filename, attrNames))
        return false;
Sylvain Thery's avatar
Sylvain Thery committed
941

942
    //sif(mergeCloseVertices)
943
    //mtv.mergeCloseVertices();
Sylvain Thery's avatar
Sylvain Thery committed
944

945
    return importMesh<PFP>(map, mtv);
Pierre Kraemer's avatar
Pierre Kraemer committed
946
947
}

948
template <typename PFP>
949
950
bool importMeshToExtrude(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, float scale, unsigned int nbStage)
{
951
    Surface::Import::MeshTablesSurface<PFP> mts(map);
952

953
954
    if(!mts.importMesh(filename, attrNames))
        return false;
955

956
    return importMeshSurfToVol<PFP>(map, mts, scale, nbStage);
957
958
}

959
960


961
} // namespace Import
962
963

} // namespace Volume
964

Pierre Kraemer's avatar
Pierre Kraemer committed
965
966
967
968

} // namespace Algo

} // namespace CGoGN