Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSH_Reader.h
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: MSH_Reader.h,v 1.3 2008/12/06 08:43:24 mtcampbe Exp $
24 
25 #ifndef MSH_READER_H
26 #define MSH_READER_H
27 
28 #include "../Rocsurf/include/surfbasic.h"
29 #include <vector>
30 #include <string>
31 #include <fstream>
32 #include <cstdlib>
33 #include <cstdio>
34 #include <sstream>
35 
37 
38 // This class provides an interface for reading in a surface mesh
39 // from the ".smf" or ".obj" file.
40 class MSH_Reader {
41 public:
42  // Constructor from an MPI communicator and a scale factor.
44 
45  // Assuming the window has been created, read in the local panes from
46  // given file and return the number of local panes.
47  int read_mesh( const char *fname, const std::string &wname) {
48  std::ifstream is( fname);
49  if ( is == NULL) {
50  std::cerr << "Error: Could not open file " << fname << std::endl;
51  exit(-1);
52  }
53 
54  // Create the window if not yet exist
55  int h=COM_get_window_handle(wname.c_str());
56  if ( h<=0) COM_new_window( wname.c_str());
57 
58  std::cout << "Reading file " << fname << std::endl;
59 
60  read_pane_coors( is, wname);
61  read_pane_elems( is, wname);
62 
63  return 1;
64  }
65 
66 private:
67  void get_nextline( std::istream &is, char *str) {
68  str[0] = '\0';
69  if ( is.eof()) { return; }
70  is.getline( str, MAXLEN);
71  }
72 
73  // Read in coordinates of a pane into a window if local
74  void read_pane_coors( std::istream &is, const std::string &wname) {
75  std::vector<SURF::Point_3<double> > coors;
76 
77  // First, look for number of vertices
78  for ( ;;) {
79  get_nextline( is, buf);
80  std::string s(buf);
81 
82  if ( s.find("Vertices",0)!=std::string::npos) break;
83  }
84 
85  int n=0;
86  is >> n; get_nextline(is, buf);
87  coors.reserve(n);
88 
89  for ( int i=0; i<n; ++i) {
90  get_nextline( is, buf);
91 
92  SURF::Point_3<double> p;
93 
94  std::sscanf( buf, "%lf %lf %lf", &p[0], &p[1], &p[2]);
95  coors.push_back( p);
96  }
97 
98  COM_assertion_msg( n>0, "Did not find any nodes");
99  std::cout << "Read in " << n << " nodes" << std::endl;
100 
101  COM_set_size( (wname+".nc").c_str(), 1, n);
102  SURF::Point_3<double> *p;
103  COM_allocate_array( (wname+".nc").c_str(), 1, &(void*&)p);
104 
105  std::copy( coors.begin(), coors.end(), p);
106  }
107 
108  // Read in element connectivity of a pane into a window if local
109  void read_pane_elems( std::istream &is, const std::string &wname){
110  std::vector<SURF::Vector_3<int> > elems;
111 
112  // First, look for number of triangles
113  for (;;) {
114  get_nextline( is, buf);
115  std::string s(buf);
116 
117  if ( s.find("Triangles",0)!=std::string::npos) break;
118  }
119 
120  int n=0;
121  is >> n; get_nextline(is, buf);
122  elems.reserve(n);
123 
124  // When entering this routine, buf[0] should start with 'f'
125  for ( int i=0; i<n; ++i) {
126  get_nextline( is, buf);
127 
128  SURF::Vector_3<int> p;
129  std::sscanf( buf, "%d %d %d", &p[0], &p[1], &p[2]);
130  elems.push_back( p);
131  }
132 
133  COM_assertion_msg( n>0, "Did not find any faces");
134  std::cout << "Read in " << n << " triangles" << std::endl;
135 
136  COM_set_size( (wname+".:t3:").c_str(), 1, n);
137  SURF::Vector_3<int> *p;
138  COM_allocate_array( (wname+".:t3:").c_str(), 1, &(void*&)p);
139 
140  std::copy( elems.begin(), elems.end(), p);
141  }
142 
143 private:
144  enum {MAXLEN=255};
145  char buf[MAXLEN+1];
146 };
147 
148 #endif
149 
150 
151 
152 
153 
154 
int read_mesh(const char *fname, const std::string &wname)
Definition: MSH_Reader.h:47
#define COM_assertion_msg(EX, msg)
double s
Definition: blastest.C:80
void COM_set_size(const char *wa_str, int pane_id, int size, int ng=0)
Set sizes of for a specific attribute.
Definition: roccom_c++.h:136
char buf[MAXLEN+1]
Definition: MSH_Reader.h:145
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to ** copy
Definition: roccomf90.h:20
Definition: Rocin.h:64
blockLoc i
Definition: read.cpp:79
const NT & n
void COM_new_window(const char *wname, MPI_Comm c=MPI_COMM_NULL)
Definition: roccom_c++.h:86
void COM_allocate_array(const char *wa_str, int pane_id=0, void **addr=NULL, int strd=0, int cap=0)
Allocate space for an attribute on a specific pane and return the address by setting addr...
Definition: roccom_c++.h:196
int COM_get_window_handle(const char *wname)
Definition: roccom_c++.h:404
void read_pane_coors(std::istream &is, const std::string &wname)
Definition: MSH_Reader.h:74
void read_pane_elems(std::istream &is, const std::string &wname)
Definition: MSH_Reader.h:109
void get_nextline(std::istream &is, char *str)
Definition: MSH_Reader.h:67
#define COM_EXTERN_MODULE(moduleName)
Definition: roccom_basic.h:116