Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
meshio.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 // $Id: meshio.C,v 1.3 2008/12/06 08:43:29 mtcampbe Exp $
24 
25 #include <cstdio>
26 #include <iostream>
27 #include <fstream>
28 #include <vector>
29 #include <algorithm>
30 #include <cstring>
31 #include <string>
32 #include <cstdlib>
33 #include <cmath>
34 #include <cassert>
35 
36 using namespace std;
37 
38 // Read in an unstructed triangular mesh in obj format
39 int read_obj( istream &is, vector<double> &coors,
40  vector<int> &elems) {
41  const int MAXLEN=255;
42  char buf[MAXLEN];
43  int face_type = 3;
44 
45  is.getline( buf, MAXLEN);
46  if ( strncmp( buf, "#OBJ", 4)==0 && !is.eof()) {
47  face_type = buf[4] - '0';
48  assert( face_type == 3 || face_type == 4 || face_type == 6);
49  do { is.getline( buf, MAXLEN); } while ( buf[0]=='#' && !is.eof());
50  }
51  int np=0, nf=0;
52  if ( strcmp( buf, "off")==0 || strcmp( buf, "OFF")==0 )
53  do { is.getline( buf, MAXLEN); } while ( buf[0]=='#' && !is.eof());
54  sscanf( buf, "%d %d", &np, &nf);
55 
56  // Allocate space for points and faces.
57  coors.reserve( np*3); elems.reserve( nf*face_type);
58 
59  // Read in the coordinates
60  for ( int i=0; i<np; ++i) {
61  do { is.getline( buf, MAXLEN); } while ( buf[0]=='#' && !is.eof());
62  double p0, p1, p2;
63  sscanf( buf, "%lf %lf %lf", &p0, &p1, &p2);
64  coors.push_back( p0); coors.push_back( p1); coors.push_back( p2);
65  }
66 
67  // Read in faces
68  vector<int> f( face_type);
69  while( is.peek() == '#' && !is.eof()) is.getline( buf, MAXLEN);
70  for ( int i=0; i<nf; ++i) {
71  for ( int j=0; j<face_type; ++j) {
72  is >> f[j]; assert( f[j] >= 1 && f[j] <= np);
73  elems.push_back( f[j]);
74  }
75  }
76 
77  return face_type;
78 }
79 
80 // Read in a nstructed mesh
81 void read_ij( istream &is, vector<double> &coors, int dims[2]) {
82  const int MAXLEN=255;
83  char buf[MAXLEN];
84 
85  do { is.getline( buf, MAXLEN); } while ( buf[0]=='#' && !is.eof());
86  sscanf( buf, "%d %d", &dims[0], &dims[1]);
87 
88  int np=dims[0]*dims[1];
89  // Allocate space for points and faces.
90 
91  // Read in the coordinates
92  for ( int i=0; i<np; ++i) {
93  do { is.getline( buf, MAXLEN); } while ( buf[0]=='#' && !is.eof());
94  double p0, p1, p2;
95  sscanf( buf, "%lf %lf %lf", &p0, &p1, &p2);
96  coors.push_back( p0); coors.push_back( p1); coors.push_back( p2);
97  }
98 }
99 
100 // Read in an unstructed triangular mesh in obj format
101 void write_ij( ostream &os, const vector<double> &coors, int dims[]) {
102  os << dims[0] << " " << dims[1] << endl;
103 
104  char buf[100];
105  // Write out the coordinates
106  for ( unsigned int i=0; i<coors.size(); i+=3) {
107  sprintf( buf, "%.10E\t%.10E\t%.10E", coors[i],
108  coors[i+1], coors[i+2]);
109  os << buf << endl;
110  }
111 }
112 
113 
114 
115 
116 
117 
NT p1
#define MAXLEN
Definition: Coupling.h:41
int read_obj(std::istream &is, vector< double > &coors, vector< int > &elems)
Definition: meshio.C:39
void write_ij(ostream &os, const vector< double > &coors, int dims[])
Definition: meshio.C:101
NT p0
blockLoc i
Definition: read.cpp:79
j indices j
Definition: Indexing.h:6
void read_ij(std::istream &is, vector< double > &coors, int dims[2])
Definition: meshio.C:81