forked from raphaelmenges/MolecularDynamicsVisualization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLoader.cpp
More file actions
116 lines (95 loc) · 3.38 KB
/
SimpleLoader.cpp
File metadata and controls
116 lines (95 loc) · 3.38 KB
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
//============================================================================
// Distributed under the MIT License. Author: Raphael Menges
//============================================================================
#include "SimpleLoader.h"
#include "Utils/Logger.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <map>
// Lookup table for radii
std::map<std::string, float> radiiLookup =
{
{"H", 1.2f}, {"N", 1.55f}, {"C", 1.7f}, {"O", 1.52f}, {"S", 1.8f}, {"P", 1.8f}
};
std::unique_ptr<GPUProtein> parseSimplePDB(std::string filepath, glm::vec3& rMinExtent, glm::vec3& rMaxExtent)
{
// Initialize min / max extent values
rMinExtent.x = std::numeric_limits<float>::max();
rMinExtent.y = std::numeric_limits<float>::max();
rMinExtent.z = std::numeric_limits<float>::max();
rMaxExtent.x = std::numeric_limits<float>::min();
rMaxExtent.y = std::numeric_limits<float>::min();
rMaxExtent.z = std::numeric_limits<float>::min();
// Read file
std::ifstream in(filepath);
// Create vector to save values
std::vector<glm::vec4> atoms;
// Check whether file was found
if (!in)
{
Logger::instance().print("Could not open file: " + filepath);
}
else
{
// Convert input file to string
std::stringstream strStream;
strStream << in.rdbuf();
std::string content = strStream.str();
// Close file
in.close();
// Add line in the end to catch all lines
content += "\n";
// Some values for iteration
std::string lineDelimiter = "\n";
size_t linePos = 0;
std::string line;
// Iterate through lines
while ((linePos = content.find(lineDelimiter)) != std::string::npos)
{
// Extract line
line = content.substr(0, linePos);
content.erase(0, linePos + lineDelimiter.length());
// Check for empty line
if(line.empty())
{
continue;
}
// Split by space
std::string valueDelimiter = " ";
size_t valuePos = 0;
std::string value;
int valueCount = 0;
// Prepare vec3 for center
glm::vec3 center;
// Find values in a line (quite hacky but works for well formatted files)
while ((valuePos = line.find(valueDelimiter)) != std::string::npos)
{
// Extract value
value = line.substr(0, valuePos);
line.erase(0, valuePos + valueDelimiter.length());
// Forget empty values
if(value.empty())
{
continue;
}
// Has to be component of center
if(valueCount < 3)
{
// Just guess that there are floats
center[valueCount] = std::stof(value);
}
valueCount++;
}
// Decide about min / max
rMinExtent = glm::min(rMinExtent, center);
rMaxExtent = glm::max(rMaxExtent, center);
// Radius
float radius = radiiLookup.at(line);
// Add found atom to vectors
atoms.push_back(glm::vec4(center, radius));
}
}
// Return protein
return std::move(std::unique_ptr<GPUProtein>(new GPUProtein(atoms)));
}