File: NodalDataManager.cc

package info (click to toggle)
madlib 1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,196 kB
  • sloc: cpp: 39,851; sh: 10,041; makefile: 473
file content (613 lines) | stat: -rw-r--r-- 19,030 bytes parent folder | download | duplicates (6)
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
604
605
606
607
608
609
610
611
612
613
// -------------------------------------------------------------------
// MAdLib - Copyright (C) 2008-2009 Universite catholique de Louvain
//
// See the Copyright.txt and License.txt files for license information. 
// You should have received a copy of these files along with MAdLib. 
// If not, see <http://www.madlib.be/license/>
//
// Please report all bugs and problems to <contrib@madlib.be>
//
// Authors: Gaetan Compere, Jean-Francois Remacle
// -------------------------------------------------------------------

#include "NodalDataManager.h"
#include "CallbackManager.h"
#include "MAdOutput.h"
#include "MeshSizeBase.h"

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
using std::vector;
using std::set;
using std::ostream;
using std::string;

namespace MAd {

  // -------------------------------------------------------------------
  void P1CBFunction (pPList before, pPList after, void *dataNames,
                     operationType type , pEntity ppp) {
  
    set<string>* knownDataNames = static_cast<set<string>*>(dataNames);

    switch (type) {
    case MAd_ESPLIT: {
      // In the edge split case, we have to interpolate the data at the new node

      set<string>::const_iterator tIter = (*knownDataNames).begin();
      set<string>::const_iterator tLast = (*knownDataNames).end();
      for (; tIter != tLast; tIter++ ) {

        pMeshDataId dataId = MD_lookupMeshDataId((*tIter).c_str());
    
        // find the old edge
        void *tmp=0;
        pEntity pE = PList_next(before,&tmp);
        double t = E_linearParams((pEdge)pE,(pVertex)ppp);
     
        // get datas at old nodes
        double data0 = 0.;
        pVertex pV0 = E_vertex((pEdge)pE, 0); 
        int gotit0 = EN_getDataDbl((pEntity)pV0, dataId,  &data0);
      
        double data1 = 0.;
        pVertex pV1 = E_vertex((pEdge)pE, 1); 
        int gotit1 = EN_getDataDbl((pEntity)pV1, dataId,  &data1);
      
        if (!gotit0 || !gotit1) {
          printf("Error: one of the nodes has no data attached to with name %s",
                 (*tIter).c_str());
          throw;
        }

        // Interpolate the data at the new node. 
        double newData = (1.-t) * data0 + t * data1;
      
        // attach it
        EN_attachDataDbl(ppp, dataId, newData);
      }

      break;
    } 
    case MAd_ECOLLAPSE: {
      // In the edge collapse case, we have to delete the datas attached to the deleted node
    
      set<string>::const_iterator tIter = (*knownDataNames).begin();
      set<string>::const_iterator tLast = (*knownDataNames).end();
      for (; tIter != tLast; tIter++ ) {

        pMeshDataId dataId = MD_lookupMeshDataId((*tIter).c_str());
        EN_deleteData(ppp, dataId);
      }
  
      break;
    }
    case MAd_ESWAP:
    case MAd_FSWAP: {
      // nothing to be done (no modification at nodes)
      break;
    }
    case MAd_RREMOVE: {
      void * temp = NULL;
      while ( pEntity pE = PList_next(before,&temp) ) {
        if ( EN_type(pE) == 0 ) {
          set<string>::const_iterator tIter = (*knownDataNames).begin();
          set<string>::const_iterator tLast = (*knownDataNames).end();
          for (; tIter != tLast; tIter++ ) {
            pMeshDataId dataId = MD_lookupMeshDataId((*tIter).c_str());
            EN_deleteData(pE, dataId);
          }
        }
      }
      break;
    }
    case MAd_UNKNOWNOPERATION:
    case MAd_VERTEXMOVE:
    case MAd_FCOLLAPSE:
    case MAd_DESPLTCLPS:
    default: {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                  "Not implemented for mesh modification %d",
                                  type);
    }
    }
  
  }

  // -------------------------------------------------------------------
  void P1VecCBFunction (pPList before, pPList after, void *dataNames,
                        operationType type , pEntity ppp) {
  
    set<string>* knownDataNames = static_cast<set<string>*>(dataNames);

    switch (type) {
    case MAd_ESPLIT: {
      // In the edge split case, we have to interpolate the data at the new node

      set<string>::const_iterator tIter = (*knownDataNames).begin();
      set<string>::const_iterator tLast = (*knownDataNames).end();
      for (; tIter != tLast; tIter++ ) {

        pMeshDataId dataId = MD_lookupMeshDataId((*tIter).c_str());
    
        // find the old edge
        void *tmp=0;
        pEntity pE = PList_next(before,&tmp);
        double t = E_linearParams((pEdge)pE,(pVertex)ppp);
     
        // get coordinates and datas at old nodes
        void * data0 = NULL;
        pVertex pV0 = E_vertex((pEdge)pE, 0);
        int gotit0 = EN_getDataPtr((pEntity)pV0, dataId,  &data0);
        vector<double> * vec0 = (vector<double>*) data0;
      
        void * data1 = NULL;
        pVertex pV1 = E_vertex((pEdge)pE, 1);
        int gotit1 = EN_getDataPtr((pEntity)pV1, dataId,  &data1);
        vector<double> * vec1 = (vector<double>*) data1;
      
        if (!gotit0 || !gotit1) {
          printf("Error: one of the nodes has no data attached to with name %s",
                 (*tIter).c_str());
          throw;
        }

        // Interpolate the data at the new node. 
        vector<double> * newData = new vector<double>;
        for (int i=0; i<3; i++)  (*newData).push_back( (1.-t) * (*vec0)[i] + t * (*vec1)[i] );
      
        // attach it
        EN_attachDataPtr(ppp, dataId, newData);
      }

      break;
    } 
    case MAd_ECOLLAPSE: {
      // In the edge collapse case, we have to delete the datas attached to the deleted node
          
      set<string>::const_iterator tIter = (*knownDataNames).begin();
      set<string>::const_iterator tLast = (*knownDataNames).end();
      for (; tIter != tLast; tIter++ ) {

        pMeshDataId dataId = MD_lookupMeshDataId((*tIter).c_str());
        void * data = NULL;
        int gotit = EN_getDataPtr((pEntity)ppp, dataId,  &data);
        vector<double> * vec = (vector<double>*) data;
        if (gotit && vec) delete vec;
        EN_deleteData(ppp, dataId);
      }
  
      break;
    }
    case MAd_ESWAP:
    case MAd_FSWAP: {
      // nothing to be done (no modification at nodes)
      break;
    }
    case MAd_RREMOVE: {
      void * temp = NULL;
      while ( pEntity pE = PList_next(before,&temp) ) {
        if ( EN_type(pE) == 0 ) {
          set<string>::const_iterator tIter = (*knownDataNames).begin();
          set<string>::const_iterator tLast = (*knownDataNames).end();
          for (; tIter != tLast; tIter++ ) {
            pMeshDataId dataId = MD_lookupMeshDataId((*tIter).c_str());
            void * data = NULL;
            int gotit = EN_getDataPtr((pEntity)ppp, dataId,  &data);
            vector<double> * vec = (vector<double>*) data;
            if (gotit && vec) delete vec;
            EN_deleteData(pE, dataId);
          }
        }
      }
      break;
    }
    case MAd_UNKNOWNOPERATION:
    case MAd_VERTEXMOVE:
    case MAd_FCOLLAPSE:
    case MAd_DESPLTCLPS:
    default: {
      printf("Error in NodalDataManager: Callback function not implemented for mesh modification %d",
             type);  
      throw;
    }
    }
  
  }

  // -------------------------------------------------------------------
  void NodalDataManager::initialize(pMesh m)
  {
    mesh = m;

    prefix    = "NodalDataManagerId_";
    prefixVec = prefix + "Vec_";

    coordNameBase = "StoredCoordinates__";

    if(knownDataNames )
      {
        knownDataNames->clear();
      }
    else
      { 
        knownDataNames= new set<string>();
      }
    if ( knownDataVecNames  )
      {
        knownDataVecNames->clear();
      }
    else
      {
        knownDataVecNames = new set<string>();
      }

    CallBackManagerSgl::instance().registerCallBack(P1CBFunction,   knownDataNames   );
    CallBackManagerSgl::instance().registerCallBack(P1VecCBFunction,knownDataVecNames);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::setMesh(pMesh m)
  {
    mesh = m;
  }

  // -------------------------------------------------------------------
  void NodalDataManager::finalize()
  {
    removeAllData();
    if (knownDataNames) 
      {
        delete knownDataNames;
        knownDataNames = NULL;
      }
    if (knownDataVecNames)
      {
        delete knownDataVecNames;
        knownDataVecNames = NULL;
      }
  }

  // -------------------------------------------------------------------
  void NodalDataManager::diagnostics (ostream& out) const
  {
    out << "\nContent of the nodal data manager:\n";

    out << "\n  - Double scalar : " << knownDataNames->size() << " fields:\n";
    set<string>::iterator iIter;
    set<string>::iterator iLast;
    iIter = (*knownDataNames).begin();
    iLast = (*knownDataNames).end();
    for (; iIter != iLast; iIter++) {
      out << "     - "<< *iIter << "\n";
    }

    out << "\n  - Double vector : " << knownDataVecNames->size() << " vector fields:\n";
    iIter = (*knownDataVecNames).begin();
    iLast = (*knownDataVecNames).end();
    for (; iIter != iLast; iIter++) {
      out << "     - "<< *iIter << "\n";
    }
  
    out << "\n" << endl;
  }

  // -------------------------------------------------------------------
  void NodalDataManager::removeAllData()
  {
    set<string>::iterator iIter;
    set<string>::iterator iLast;

    iIter = (*knownDataNames).begin();
    iLast = (*knownDataNames).end();
    for (; iIter != iLast; iIter++)  removeData(*iIter);

    iIter = (*knownDataVecNames).begin();
    iLast = (*knownDataVecNames).end();
    for (; iIter != iLast; iIter++)  removeVData(*iIter);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::registerData(string name, const vector<double> datas)
  {
    string dataName = prefix + name;

    // check that this name doesn't exists yet
    if ( nameExists(dataName,0) ) {
      cerr << "Error: this scalar data \'"<<name<<"\' is already registered in the NodalDataManager\n";
      throw;
    }

    // check that the number of datas is equal to the number of nodes in the mesh
    int nbDatas = datas.size();
    if ( nbDatas != M_numVertices(mesh) ) {
      cerr<< "Error: wrong number of datas ("<<nbDatas<<") with "<<M_numVertices(mesh)<<" nodes\n";
      throw;
    }

    // add this name to the list
    (*knownDataNames).insert(dataName);

    // attach the data at nodes
    pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
    int i = 0;
    VIter vit = M_vertexIter(mesh);
    while (pVertex pv = VIter_next(vit))
      {
        EN_attachDataDbl((pEntity)pv,id,datas[i]);
        i++;
      }
    VIter_delete(vit);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::registerVData(string name, 
                                       const vector<vector<double> > datas)
  {
    string dataName = prefixVec + name;

    // check that this name doesn't exists yet
    if ( nameExists(dataName,1) ) {
      cerr << "Error: this vectorial data \'"<<name<<"\' is already registered in the NodalDataManager\n";
      throw;
    }

    // check that the number of datas is equal to the number of nodes in the mesh
    int nbDatas = datas.size();
    if ( nbDatas != M_numVertices(mesh) ) {
      cerr<< "Error: wrong number of datas ("<<nbDatas<<") with "<<M_numVertices(mesh)<<" nodes\n";
      throw;
    }

    // add this name to the list
    (*knownDataVecNames).insert(dataName);

    // attach the data at nodes
    pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
    int i = 0;
    VIter vit = M_vertexIter(mesh);
    while (pVertex pv = VIter_next(vit))
      {
        vector<double> * copy = new vector<double>(datas[i]);
        EN_attachDataPtr((pEntity)pv,id,copy);
        i++;
      }
    VIter_delete(vit);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::getMeshData(string name, vector<double> * result) const
  {
    string dataName = prefix + name;

    // check that this name exists
    if ( !nameExists(dataName,0) ) {
      cerr << "Error: this scalar data \'"<<name<<"\' is not registered in the NodalDataManager\n";
      throw;
    }

    result->clear();

    pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
  
    int i = 0;
    VIter vit = M_vertexIter(mesh);
    while (pVertex pv = VIter_next(vit))
      {
        double theDbl;
        EN_getDataDbl((pEntity)pv,id,&theDbl);
        result->push_back(theDbl);
        i++;
      }
    VIter_delete(vit);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::getMeshVData(string name, vector<vector<double> > * result) const
  {
    string dataName = prefixVec + name;

    // check that this name exists
    if ( !nameExists(dataName,1) ) {
      cerr << "Error: this vectorial data \'"<<name<<"\' is not registered in the NodalDataManager\n";
      throw;
    }

    result->clear();

    pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
  
    int i = 0;
    VIter vit = M_vertexIter(mesh);
    while (pVertex pv = VIter_next(vit))
      {
        void * tmp = 0;
        EN_getDataPtr((pEntity)pv,id,&tmp);
        vector<double> * vec = (vector<double>*) tmp;
        result->push_back(*vec);
        i++;
      }
    VIter_delete(vit);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::getData(string name, pVertex pv, double* res) const
  {
    string dataName = prefix + name;

    // check that this name exists
    if ( !nameExists(dataName,0) ) {
      cerr << "Error: this scalar data \'"<<name<<"\' is not registered in the NodalDataManager\n";
      throw;
    }

    pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
  
    EN_getDataDbl((pEntity)pv,id,res);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::getVData(string name, pVertex pv, vector<double>& res) const
  {
    string dataName = prefixVec + name;

    // check that this name exists
    if ( !nameExists(dataName,1) ) {
      cerr << "Error: this vectorial data \'"<<name<<"\' is not registered in the NodalDataManager\n";
      throw;
    }

    pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
  
    void * tmp = 0;
    EN_getDataPtr((pEntity)pv,id,&tmp);
    res.clear();
    for (int i=0; i<3; i++) res.push_back(  (*( (vector<double>*) tmp))[i]  );

  }

  // -------------------------------------------------------------------
  void NodalDataManager::removeData(string name)
  {
    string dataName = prefix + name;

    if ( !nameExists(dataName,0) )  return;

    // remove it from the list of names
    set<string>::iterator iIter = (*knownDataNames).find(dataName);
    (*knownDataNames).erase(iIter);

    // delete the datas at nodes
    pMeshDataId id = MD_lookupMeshDataId( (dataName).c_str() );
    VIter vit = M_vertexIter(mesh);
    while (pVertex pv = VIter_next(vit))
      {
        EN_deleteData((pEntity)pv,id);
      }
    VIter_delete(vit);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::removeVData(string name)
  {
    string dataName = prefixVec + name;

    if ( !nameExists(dataName,1) )  return;

    // remove it from the list of names
    set<string>::iterator iIter = (*knownDataVecNames).find(dataName);
    (*knownDataVecNames).erase(iIter);

    // delete the datas at nodes
    pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
    VIter vit = M_vertexIter(mesh);
    while (pVertex pv = VIter_next(vit))
      {
        void * data = NULL;
        int gotit = EN_getDataPtr((pEntity)pv, id,  &data);
        vector<double> * vec = (vector<double>*) data;
        if (gotit && vec) delete vec;
        EN_deleteData((pEntity)pv, id);
      }
    VIter_delete(vit);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::writeData(string name, const char *fn)
  {
    string dataName = prefix + name;

    // check that this name exists
    if ( !nameExists(dataName,0) ) {
      cerr << "Error: this data \'"<<name<<"\' is not registered in the NodalDataManager\n";
      throw;
    }

    pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
  
    MAdAttachedNodalDataOutput(mesh, fn, id);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::writeVData(string name, const char *fn)
  {
//     string dataName = prefixVec + name;

//     // check that this name exists
//     if ( !nameExists(dataName,1) ) {
//       cerr << "Error: this data \'"<<name<<"\' is not registered in the NodalDataManager\n";
//       throw;
//     }

//     pMeshDataId id = MD_lookupMeshDataId( dataName.c_str() );
  
    MAdMsgSgl::instance().warning(__LINE__,__FILE__,
                                  "Output for vectorial attached data is not implemented");
    // it is implemented for double arrays, not vectors. 
    // This class should store double arrays too, to be done (GC).
    //    MAdAttachedNodalDataVecOutput(mesh, fn, id);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::storeCoordinates()
  {
    removeCoordinates();

    // add the coordinates name to the list
    string coordName = prefixVec + coordNameBase;
    (*knownDataVecNames).insert(coordName);

    // attach the data at nodes
    pMeshDataId id = MD_lookupMeshDataId( coordName.c_str() );
    VIter vit = M_vertexIter(mesh);
    while (pVertex pv = VIter_next(vit))
      {
        double xyz[3]; V_coord(pv,xyz);
        vector<double> * vec = new vector<double>;
        for (int j = 0; j<3; j++)  (*vec).push_back(xyz[j]);
        EN_attachDataPtr((pEntity)pv,id,vec);
      }
    VIter_delete(vit);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::getStoredCoordinates(pVertex pv, vector<double>& res)
  {
    getVData(coordNameBase,pv,res);
  }

  // -------------------------------------------------------------------
  void NodalDataManager::removeCoordinates()
  {
    removeVData(coordNameBase);
  }

  // -------------------------------------------------------------------
  bool NodalDataManager::isCoordinates()
  {
    string coordName = prefixVec + coordNameBase;
    return nameExists(coordName,1);
  }

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

  bool NodalDataManager::nameExists(string dataName, int vec) const
  {
    if ( vec == 0 ) {
      set<string>::iterator iIter = (*knownDataNames).find(dataName);
      if ( iIter == (*knownDataNames).end() ) return false;
      else return true;
    }
    if ( vec == 1 ) {
      set<string>::iterator iIter = (*knownDataVecNames).find(dataName);
      if ( iIter == (*knownDataVecNames).end() ) return false;
      else return true;
    }
    return true;
  }

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

}