Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OBJ_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: OBJ_Reader.h,v 1.3 2008/12/06 08:43:24 mtcampbe Exp $
24 
25 #ifndef OBJ_READER_H
26 #define OBJ_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 
36 
37 // This class provides an interface for reading in a surface mesh
38 // from the ".smf" or ".obj" file.
39 class OBJ_Reader {
40 public:
41  // Constructor from an MPI communicator and a scale factor.
43 
44  // Assuming the window has been created, read in the local panes from
45  // given file and return the number of local panes.
46  int read_mesh( const char *fname, const std::string &wname) {
47  std::ifstream is( fname);
48  if ( is == NULL) {
49  std::cerr << "Error: Could not open file " << fname << std::endl;
50  exit(-1);
51  }
52 
53  // Create the window if not yet exist
54  int h=COM_get_window_handle(wname.c_str());
55  if ( h<=0) COM_new_window( wname.c_str());
56 
57  std::cout << "Reading file " << fname << std::endl;
58 
59  int nn, ne;
60  get_nextline( is, buf);
61  std::sscanf( buf, "%d %d", &nn, &ne);
62 
63  std::cout << "Reading " << nn << " nodes and "
64  << ne << " triangles " << std::endl;
65  read_pane_coors( is, wname, nn);
66  read_pane_elems( is, wname, ne);
67 
68  return 1;
69  }
70 
71 private:
72  void get_nextline( std::istream &is, char *str) {
73  str[0] = '\0';
74  if ( is.eof()) { return; }
75  is.getline( str, MAXLEN);
76  }
77 
78  // Read in coordinates of a pane into a window if local
79  void read_pane_coors( std::istream &is, const std::string &wname, int nn) {
80  COM_set_size( (wname+".nc").c_str(), 1, nn);
81  SURF::Point_3<double> *ps;
82  COM_allocate_array( (wname+".nc").c_str(), 1, &(void*&)ps);
83 
84  for ( int i=0; i<nn; ++i) {
85  get_nextline( is, buf);
86  std::sscanf( buf, "%lf %lf %lf", &ps[i][0], &ps[i][1], &ps[i][2]);
87  }
88  }
89 
90  // Read in element connectivity of a pane into a window if local
91  void read_pane_elems( std::istream &is, const std::string &wname, int ne){
92  // Note: Only supports triangular meshes now.
93  COM_set_size( (wname+".:t3:").c_str(), 1, ne);
94  SURF::Vector_3<int> *es;
95  COM_allocate_array( (wname+".:t3:").c_str(), 1, &(void*&)es);
96 
97  for (int i=0; i<ne; ++i) {
98  get_nextline( is, buf);
99  std::sscanf( buf, "%d %d %d", &es[i][0], &es[i][1], &es[i][2]);
100  }
101  }
102 
103 private:
104  enum {MAXLEN=255};
105  char buf[MAXLEN+1];
106 };
107 
108 #endif
109 
110 
111 
112 
113 
114 
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
void get_nextline(std::istream &is, char *str)
Definition: OBJ_Reader.h:72
void read_pane_coors(std::istream &is, const std::string &wname, int nn)
Definition: OBJ_Reader.h:79
Definition: Rocin.h:64
blockLoc i
Definition: read.cpp:79
void read_pane_elems(std::istream &is, const std::string &wname, int ne)
Definition: OBJ_Reader.h:91
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
char buf[MAXLEN+1]
Definition: OBJ_Reader.h:105
int COM_get_window_handle(const char *wname)
Definition: roccom_c++.h:404
int read_mesh(const char *fname, const std::string &wname)
Definition: OBJ_Reader.h:46
#define COM_EXTERN_MODULE(moduleName)
Definition: roccom_basic.h:116