File: NullModel.cc

package info (click to toggle)
madlib 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 6,912 kB
  • sloc: cpp: 39,850; sh: 10,088; makefile: 476
file content (281 lines) | stat: -rw-r--r-- 9,145 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
// -------------------------------------------------------------------
// 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: Jean-Francois Remacle, Gaetan Compere
// -------------------------------------------------------------------

#ifndef _HAVE_GMSH_

#include "NullModel.h"

#include "MAdMessage.h"
#include "MshTags.h"

#include <stdio.h>
#include <set>
using std::set;
#include <string.h>

namespace MAd {

  // -------------------------------------------------------------------
  NullModel::~NullModel()
  {
    clean();
  }

  // -------------------------------------------------------------------
  void NullModel::clean()
  {
    riter itR = regions.begin();
    for (; itR != regions.end(); itR++) delete (*itR);
    regions.clear();
    fiter itF = faces.begin();
    for (; itF != faces.end(); itF++) delete (*itF);
    faces.clear();
    eiter itE = edges.begin();
    for (; itE != edges.end(); itE++) delete (*itE);
    edges.clear();
    viter itV = vertices.begin();
    for (; itV != vertices.end(); itV++) delete (*itV);
    vertices.clear();
  }

  // -------------------------------------------------------------------
  MAdGEntity * NullModel::getEntityByTag(int dim, int tag)
  {
    switch(dim) {
    case 3: return getRegionByTag(tag);
    case 2: return getFaceByTag(tag);
    case 1: return getEdgeByTag(tag);
    case 0: return getVertexByTag(tag);
    }
    return NULL;
  }

  // -------------------------------------------------------------------
  MAdGRegion *NullModel::getRegionByTag(int tag)
  {
    set<MAdGRegion *,MAdGEntityLessThan>::const_iterator it = regions.begin();
    for (; it != regions.end(); it++) {
      if ( (*it)->tag() == tag ) return *it;
    }
    MAdGRegion * newRegion = new MAdGRegion(tag,this);
    regions.insert(newRegion);
    return newRegion;
  }

  // -------------------------------------------------------------------
  MAdGFace *NullModel::getFaceByTag(int tag)
  {
    set<MAdGFace *,MAdGEntityLessThan>::const_iterator it = faces.begin();
    for (; it != faces.end(); it++) {
      if ( (*it)->tag() == tag ) return *it;
    }
    MAdGFace * newFace = new MAdGFace(tag,this);
    faces.insert(newFace);
    return newFace;
  }

  // -------------------------------------------------------------------
  MAdGEdge *NullModel::getEdgeByTag(int tag)
  {
    set<MAdGEdge *,MAdGEntityLessThan>::const_iterator it = edges.begin();
    for (; it != edges.end(); it++) {
      if ( (*it)->tag() == tag ) return *it;
    }
    MAdGEdge * newEdge = new MAdGEdge(tag,this);
    edges.insert(newEdge);
    return newEdge;
  }

  // -------------------------------------------------------------------
  MAdGVertex *NullModel::getVertexByTag(int tag)
  {
    set<MAdGVertex *,MAdGEntityLessThan>::const_iterator it = vertices.begin();
    for (; it != vertices.end(); it++) {
      if ( (*it)->tag() == tag ) return *it;
    }
    MAdGVertex * newVertex = new MAdGVertex(tag,this);
    vertices.insert(newVertex);
    return newVertex;
  }

  // -------------------------------------------------------------------
  int NullModel::readGEO(const std::string &filename)
  {
    MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                "Reading a model from a .geo file not implemented in NullModel");
  }

  // -------------------------------------------------------------------
  void SwapBytes(char *array, int size, int n)
  {
    char *x = new char[size];
    for(int i = 0; i < n; i++) {
      char *a = &array[i * size];
      memcpy(x, a, size);
      for(int c = 0; c < size; c++)
        a[size - 1 - c] = x[c];
    }
    delete [] x;
  }

  // -------------------------------------------------------------------
  int NullModel::readMSH(const std::string &filename)
  {
    FILE *fp = fopen(filename.c_str(), "rb");
    if(!fp){
      MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                  "Unable to open file '%s'", filename.c_str());
      return 0;
    }

    char str[256] = "XXX";
    double version = 1.0;
    bool binary = false, swap = false;
 
    while(1) {

      while(str[0] != '$'){
        if(!fgets(str, sizeof(str), fp) || feof(fp))
          break;
      }
    
      if(feof(fp))
        break;

      if(!strncmp(&str[1], "MeshFormat", 10)) {

        if(!fgets(str, sizeof(str), fp)) return 0;
        int format, size;
        if(sscanf(str, "%lf %d %d", &version, &format, &size) != 3) return 0;
        if(format){
          binary = true;
          MAdMsgSgl::instance().info(__LINE__,__FILE__,
                                     "Mesh is in binary format");
          int one;
          if(fread(&one, sizeof(int), 1, fp) != 1) return 0;
          if(one != 1){
            swap = true;
            MAdMsgSgl::instance().info(__LINE__,__FILE__,
                                       "Swapping bytes from binary file");
          }
        }

      }
      else if(!strncmp(&str[1], "PhysicalNames", 13)) {
      }
      else if(!strncmp(&str[1], "NO", 2) || !strncmp(&str[1], "Nodes", 5) ||
              !strncmp(&str[1], "ParametricNodes", 15)) {
      }
      else if(!strncmp(&str[1], "ELM", 3) || !strncmp(&str[1], "Elements", 8)) {

        if(!fgets(str, sizeof(str), fp)) return 0;
        int numElements;
        sscanf(str, "%d", &numElements);
        MAdMsgSgl::instance().info(__LINE__,__FILE__,
                                   "%d elements", numElements);
        if(!binary){
          for(int i = 0; i < numElements; i++) {
            int num, type, physical = -1, elementary = -1, partition = 0, numVertices;
            if(version <= 1.0){
              fscanf(fp, "%d %d %d %d %d", &num, &type, &physical, &elementary, &numVertices);
            }
            else{
              int numTags;
              fscanf(fp, "%d %d %d", &num, &type, &numTags);
              for(int j = 0; j < numTags; j++){
                int tag;
                fscanf(fp, "%d", &tag);       
                if(j == 0)      physical = tag;
                else if(j == 1) elementary = tag;
                else if(j == 2) partition = tag;
                // ignore any other tags for now
                numVertices = getNumVerticesForElementTypeMSH(type);
              }
            }
            int tmp;
            for(int j = 0; j < numVertices; j++) fscanf(fp, "%d", &tmp);

            int gTag = elementary;
            if ( physical != -1 ) gTag = physical;
            int gDim = getDimForElementTypeMSH(type);
            getEntityByTag(gDim,gTag);
          }
        }
        else{
          int numElementsPartial = 0;
          while(numElementsPartial < numElements){
            int header[3];
            if(fread(header, sizeof(int), 3, fp) != 3) return 0;
            if(swap) SwapBytes((char*)header, sizeof(int), 3);
            int type = header[0];
            int numElms = header[1];
            int numTags = header[2];
            int numVertices = getNumVerticesForElementTypeMSH(type);
            unsigned int n = 1 + numTags + numVertices;
            int *data = new int[n];
            for(int i = 0; i < numElms; i++) {
              if(fread(data, sizeof(int), n, fp) != n) return 0;
              if(swap) SwapBytes((char*)data, sizeof(int), n);
              int num = data[0];
              int physical = (numTags > 0) ? data[4 - numTags] : -1;
              int elementary = (numTags > 1) ? data[4 - numTags + 1] : -1;
              int partition = (numTags > 2) ? data[4 - numTags + 2] : 0;
              int *indices = &data[numTags + 1];
              int gTag = elementary;
              if ( physical != -1 ) gTag = physical;
              int gDim = getDimForElementTypeMSH(type);
              getEntityByTag(gDim,gTag);
            }
            delete [] data;
            numElementsPartial += numElms;
          }
        }

      }

      do {
        if(!fgets(str, sizeof(str), fp) || feof(fp))
          break;
      } while(str[0] != '$');
    }

    fclose(fp);
    return 1;
  }

  // -------------------------------------------------------------------
  int NullModel::readSTEP(const std::string &filename)
  {
    MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                "Null model cannot read STEP format");
  }

  // -------------------------------------------------------------------
  int NullModel::readBREP(const std::string &filename)
  {
    MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                "Null model cannot read BREP format");
  }

  // -------------------------------------------------------------------
  int NullModel::readIGES(const std::string &filename)
  {
    MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                "Null model cannot read IGES format");
  }

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

}

#endif