OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
MSMSFormat.hpp
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "hydrodynamics/Mesh.hpp"
6
7namespace OpenMD {
8
9#define MSMSLINESIZE 180
10
11 class MSMSFormat {
12 public:
13 MSMSFormat(const std::string& msmsRoot);
15 Mesh* ReadShape();
16 std::string title_;
17
18 private:
19 Mesh* mesh_;
20 std::istream* faceFile_;
21 std::istream* vertFile_;
22 std::vector<Vector3d> vertices_;
23 std::vector<Vector3d> normals_;
24 };
25
26 MSMSFormat::MSMSFormat(const std::string& msmsRoot) {
27 std::string prefix = getPrefix(msmsRoot); // just in case the user
28 // gave us one of the
29 // MSMS .face or .vert
30 // file names
31
32 std::string faceFileName = prefix + ".face";
33 std::string vertFileName = prefix + ".vert";
34
35 faceFile_ = new std::ifstream(faceFileName.c_str(),
36 ifstream::in | ifstream::binary);
37
38 if (faceFile_->fail()) {
39 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
40 "MSMSFormat: Cannot open facet file: %s\n",
41 faceFileName.c_str());
42 painCave.isFatal = 1;
43 simError();
44 }
45
46 vertFile_ = new std::ifstream(vertFileName.c_str(),
47 ifstream::in | ifstream::binary);
48
49 if (vertFile_->fail()) {
50 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
51 "MSMSFormat: Cannot open vertex file: %s\n",
52 vertFileName.c_str());
53 painCave.isFatal = 1;
54 simError();
55 }
56 mesh_ = new Mesh();
57
58 return;
59 }
60
61 MSMSFormat::~MSMSFormat() {
62 delete faceFile_;
63 delete vertFile_;
64 return;
65 }
66
67 Mesh* MSMSFormat::ReadShape() {
68 char inbuf[MSMSLINESIZE];
69 std::string line;
70
71 vertFile_->clear();
72 vertFile_->seekg(0);
73
74 while (vertFile_->getline(inbuf, MSMSLINESIZE)) {
75 // ignore comment lines:
76 if (inbuf[0] != '#') {
77 line = inbuf;
78 StringTokenizer tokenizer(line);
79 // do we have at least 3 tokens on this vertex line?
80 if (tokenizer.countTokens() >= 3) {
81 Vector3d vertex;
82 // int l0fa, atomid, l;
83 vertex[0] = tokenizer.nextTokenAsDouble();
84 vertex[1] = tokenizer.nextTokenAsDouble();
85 vertex[2] = tokenizer.nextTokenAsDouble();
86 vertices_.push_back(vertex);
87 }
88 if (tokenizer.countTokens() >= 6) {
89 Vector3d normal;
90 normal[0] = tokenizer.nextTokenAsDouble();
91 normal[1] = tokenizer.nextTokenAsDouble();
92 normal[2] = tokenizer.nextTokenAsDouble();
93 normals_.push_back(normal);
94 }
95 // l0fa = tokenizer.nextTokenAsInt();
96 // atomid = tokenizer.nextTokenAsInt();
97 // l = tokenizer.nextTokenAsInt();
98 }
99 }
100
101 faceFile_->clear();
102 faceFile_->seekg(0);
103 while (faceFile_->getline(inbuf, MSMSLINESIZE)) {
104 // ignore comment lines:
105 if (inbuf[0] != '#') {
106 line = inbuf;
107 StringTokenizer tokenizer(line);
108 // do we have at least 3 tokens on this vertex line?
109 if (tokenizer.countTokens() >= 3) {
110 int v0, v1, v2;
111 // int surftype, ana;
112 v0 = tokenizer.nextTokenAsInt();
113 v1 = tokenizer.nextTokenAsInt();
114 v2 = tokenizer.nextTokenAsInt();
115 // surftype = tokenizer.nextTokenAsInt();
116 // ana = tokenizer.nextTokenAsInt();
117 v0--; // convert from 1-based indexing to 0-based indexing
118 v1--;
119 v2--;
120 mesh_->add(vertices_[v0], vertices_[v1], vertices_[v2]);
121 }
122 }
123 }
124 std::cerr << "MSMS files have " << vertices_.size() << " vertices and ";
125 std::cerr << mesh_->size() << " facets\n";
126 return mesh_;
127 }
128} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string getPrefix(const std::string &str)