Molecule Viewer
 All Classes Functions Variables Enumerations Pages
Loader.cpp
1 #include "Loader.hpp"
2 
3 #include <exception>
4 #include <QDebug>
5 
6 
7 Loader::Loader(): _loaded(false) {
8 
9  initParams();
10  _colorMode = 0;
11 
12 }
13 
15 
16  if (_loaded) {
17  delete _atoms;
18  }
19  glDeleteVertexArrays(1, &_vao);
20  glDeleteBuffers(4, _vbo);
21 }
22 
23 
24 void Loader::SetTextureSize(int aoTextureSize) {
25  _textureSize = aoTextureSize;
26 }
27 
28 GLuint Loader::GetVAO() const {
29  return _vao;
30 }
31 
32 
33 void Loader::GenerateVAO() {
34 
35  if (_loaded) {
36  glDeleteVertexArrays(1, &_vao);
37  glDeleteBuffers(4, _vbo);
38  }
39 
40  std::vector<glm::vec4> vertexPos = std::vector<glm::vec4>();
41  std::vector<glm::vec3> atomColors = std::vector<glm::vec3>();
42  std::vector<glm::vec3> atomChainColors = std::vector<glm::vec3>();
43  std::vector<glm::vec4> textureQuads = std::vector<glm::vec4>();
44  std::vector<GLfloat> atomRadius = std::vector<GLfloat>();
45 
46  int numberOfAtoms = _atoms->size();
47  int numPatches = numberOfAtoms;
48  double patch_size = glm::floor(_textureSize / glm::ceil(glm::sqrt(double(numPatches))));
49  double patch_min_x = 0;
50  double patch_min_y = 0;
51 
52 
53  Atom * a;
54  for (int i=0;i<numberOfAtoms;i++) {
55 
56  a = _atoms->at(i);
57  vertexPos.push_back(a->GetPosition());
58  atomColors.push_back(a->GetColor());
59  atomChainColors.push_back(a->GetChainColor());
60 
61  //Set Ambient Occlusion Texture Coords per Atom
62  //currently no bonds are considered
63  if ((patch_min_x+patch_size)>_textureSize) {
64  patch_min_x = 0;
65  patch_min_y += patch_size;
66  }
67  textureQuads.push_back(glm::vec4(patch_min_x/_textureSize, patch_min_y/_textureSize, (patch_min_x+patch_size)/_textureSize, (patch_min_y+patch_size)/_textureSize));
68  patch_min_x += patch_size;
69 
70  atomRadius.push_back(a->GetRadius());
71  }
72 
73 
74 
75  //**************Send data to GPU ***********************************************************
76 
77  glGenVertexArrays(1, &_vao);
78  glGenBuffers(4, _vbo);
79  glBindVertexArray(_vao);
80 
81 
82  //**************Vertex positions ***********************************************************
83 
84  glBindBuffer(GL_ARRAY_BUFFER, _vbo[0]);
85  glBufferData(GL_ARRAY_BUFFER, numberOfAtoms*sizeof(glm::vec4), &vertexPos[0], GL_STATIC_DRAW);
86 
87  glEnableVertexAttribArray(0);
88  glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
89 
90 
91 
92  //************** Sphere Radius ***********************************************************
93  glBindBuffer(GL_ARRAY_BUFFER, _vbo[1]);
94  glBufferData(GL_ARRAY_BUFFER, numberOfAtoms*sizeof(GLfloat), &atomRadius[0], GL_STATIC_DRAW);
95 
96  glEnableVertexAttribArray(1);
97  glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, NULL);
98 
99 
100  //************** Color ***********************************************************
101 
102  UpdateColorMode(_colorMode, true);
103  glEnableVertexAttribArray(2);
104  glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
105 
106 
107  //************** UV quad coordinates ***********************************************************
108  glBindBuffer(GL_ARRAY_BUFFER, _vbo[3]);
109  glBufferData(GL_ARRAY_BUFFER, numberOfAtoms*sizeof(glm::vec4), &textureQuads[0], GL_STATIC_DRAW);
110 
111 
112  glEnableVertexAttribArray(3);
113  glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, NULL);
114 
115 
116  glBindBuffer(GL_ARRAY_BUFFER, 0);
117  glBindVertexArray(0);
118 
119 
120 
121 
122 }
123 
124 
125 
127  return _atoms->size();
128 }
129 
130 const bool Loader::LoadedFirstTime() const {
131  return _loaded;
132 };
133 
134 void Loader::UpdateColorMode(int colorMode, bool _loadingMolecule) {
135 
136  if ((colorMode < 0) || (colorMode > 3))
137  return;
138 
139  _colorMode = colorMode;
140 
141  if (!_loaded)
142  return;
143 
144  std::vector<glm::vec3> colors = std::vector<glm::vec3>();
145 
146  if (colorMode==0) {
147  for (int i=0;i<_atoms->size();i++) {
148  colors.push_back(_atoms->at(i)->GetColor());
149  }
150  } else if (colorMode==1) {
151  for (int i=0;i<_atoms->size();i++) {
152  colors.push_back(_atoms->at(i)->GetChainColor());
153  }
154  } else {
155  for (int i=0;i<_atoms->size();i++) {
156  colors.push_back(glm::vec3(1.0f,1.0f,1.0f));
157  }
158  }
159 
160 
161  if (!_loadingMolecule)
162  glBindVertexArray(_vao);
163 
164  glBindBuffer(GL_ARRAY_BUFFER, _vbo[2]);
165  glBufferData(GL_ARRAY_BUFFER, _atoms->size()*sizeof(glm::vec3), &colors[0], GL_STATIC_DRAW);
166 
167  if (!_loadingMolecule)
168  glBindVertexArray(0);
169 
170 
171 };
172 
173 
174 bool Loader::LoadMolecule(string absolutePathToFile) {
175 
176  std::ifstream in;
177  in.open(absolutePathToFile);
178  if (!in)
179  return false;
180 
181  if (_loaded)
182  delete _atoms;
183  _atoms = new std::vector<Atom *>();
184  _numChains = 0;
185 
186  float max_x = -100;
187  float min_x = 100;
188  float max_y = -100;
189  float min_y = 100;
190  float max_z = -100;
191  float min_z = 100;
192  std::stringstream stream;
193 
194 
195  while (true)
196  {
197  std::string line;
198  std::getline(in, line);
199 
200  if (!in)
201  break;
202 
203  std::string type = line.substr(0, 6);
204 
205  if ((type.compare("ATOM ")==0) || (type.compare("HETATM")==0))
206  {
207 
208  float x, y, z;
209  stream.clear();
210  stream.str(line.substr(30, 8));
211  stream >> x;
212  if (abs(x)>1000000)
213  continue;
214  stream.clear();
215  stream.str(line.substr(38, 8));
216  stream >> y;
217  stream.clear();
218  if (abs(y)>1000000)
219  continue;
220  stream.str(line.substr(46, 8));
221  stream >> z;
222  stream.clear();
223  if (abs(z)>1000000)
224  continue;
225  stream.str(line.substr(12, 4));
226  string name;
227  stream >> name;
228  stream.clear();
229  if (name.compare("")==0)
230  continue;
231 
232  stream.str(line.substr(21, 1));
233  string chainID;
234  stream >> chainID;
235  stream.clear();
236 
237  glm::vec3 chainColor;
238 
239  if ( _chainColors.find(chainID) == _chainColors.end() ) {
240  glm::vec3 tempColor;
241  if (_numChains>=_availableChainColors.size()) {
242  tempColor = glm::vec3(rand() % 255,rand() % 255, rand() % 255);
243  } else
244  tempColor = _availableChainColors.at(_numChains);
245 
246  _chainColors[chainID] = tempColor;
247  _numChains += 1;
248  }
249 
250  chainColor = _chainColors[chainID];
251 
252  glm::vec3 color = _colors[name];
253 
254  if (color.x == 0 && color.y == 0 && color.z == 0) {
255  color = glm::vec3(255.0f,255.0,255.0f);
256  }
257  color.x = color.x/255.0f;
258  color.y = color.y/255.0f;
259  color.z = color.z/255.0f;
260  chainColor.x = chainColor.x/255.0f;
261  chainColor.y = chainColor.y/255.0f;
262  chainColor.z = chainColor.z/255.0f;
263 
264 
265  float radius = _radius[name];
266  if (radius == 0) {
267  radius = 1;
268  }
269 
270  if(x>max_x) max_x=x;
271  if(y>max_y) max_y=y;
272  if(z>max_z) max_z=z;
273  if(x<min_x) min_x=x;
274  if(y<min_y) min_y=y;
275  if(z<min_z) min_z=z;
276 
277  Atom * atom = new Atom(x,y,z,color,chainColor,radius);
278  _atoms->push_back(atom);
279 
280  }
281 
282  }
283  in.close();
284 
285 
286  if (_atoms->empty())
287  return false;
288 
289  float x_extent = (max_x + min_x)/2;
290  float y_extent = (max_y + min_y)/2;
291  float z_extent = (max_z + min_z)/2;
292 
293  for (int i=0;i<_atoms->size();i++) {
294  _atoms->at(i)->MovePosition(-x_extent,-y_extent,-z_extent);
295  }
296  x_extent = max_x - x_extent;
297  y_extent = max_y - y_extent;
298  z_extent = max_z - z_extent;
299 
300 
301  float max_radius = 2;
302  _bounding_sphere = sqrt(pow(x_extent,2) + pow(y_extent,2) + pow(z_extent,2)) + 2*max_radius;
303 
304  /*
305  _bounding_sphere = sqrt(pow(x_extent,2) + pow(y_extent,2) + pow(z_extent,2))+ max_radius;
306  Atom * atom = new Atom(0,0,0,glm::vec3(255.0f,0.0,255.0f),_bounding_sphere/2);
307  _atoms->push_back(atom);
308  */
309 
310  _loaded = true;
311 
312  GenerateVAO();
313 
314  return _loaded;
315 }
316 
317 
318 void Loader::initParams() {
319 
320  _availableChainColors.push_back(glm::vec3(0.0f,128.0f,255.0f));
321  _availableChainColors.push_back(glm::vec3(255.0f,128.0f,128.0f));
322  _availableChainColors.push_back(glm::vec3(0.0f,128.0f,0.0f));
323  _availableChainColors.push_back(glm::vec3(255.0f,255.0f,0.0f));
324  _availableChainColors.push_back(glm::vec3(128.0f,0.0f,128.0f));
325  _availableChainColors.push_back(glm::vec3(255.0f,0.0f,0.0f));
326  _availableChainColors.push_back(glm::vec3(255.0f,128.0f,0.0f));
327  _availableChainColors.push_back(glm::vec3(128.0f,255.0f,0.0f));
328  _availableChainColors.push_back(glm::vec3(0.0f,255.0f,255.0f));
329 
330  _availableChainColors.push_back(glm::vec3(0.0f,0.0f,128.0f));
331  _availableChainColors.push_back(glm::vec3(255.0f,128.0f,128.0f));
332  _availableChainColors.push_back(glm::vec3(64.0f,128.0f,255.0f));
333  _availableChainColors.push_back(glm::vec3(255.0f,128.0f,64.0f));
334  _availableChainColors.push_back(glm::vec3(64.0f,0.0f,64.0f));
335  _availableChainColors.push_back(glm::vec3(32.0f,255.0f,32.0f));
336 
337 
338  // according to http://www.imb-jena.de/ImgLibDoc/glossary/IMAGE_VDWR.html
339  //_radius["H"]= 1.20f;
340  //_radius["C"]= 1.70f;
341  //_radius["N"]= 1.55f;
342  //_radius["O"]= 1.52f;
343  _radius["F"]= 1.47f;
344  //_radius["P"]= 1.80f;
345  //_radius["S"]= 1.80f;
346  _radius["CL"]= 1.89f;
348  _radius["H"]= 1.100f;
349  _radius["C"]= 1.548f; // was changed to 1.400 for some reason 1.548 is the reported value
350  _radius["N"]= 1.400f;
351  _radius["O"]= 1.348f;
352  _radius["P"]= 1.880f;
353  _radius["S"]= 1.808f;
354  _radius["CA"]= 1.948f;
355  _radius["FE"]= 1.948f;
356  _radius["ZN"]= 1.148f;
357  _radius["CD"]= 1.748f;
358  _radius["I"]= 1.748f;
359 
360  _colors["H"] = glm::vec3(255.0f,255.0f,255.0f);
361  _colors["HE"]= glm::vec3(217.0f,255.0f,255.0f);
362  _colors["LI"]= glm::vec3(204.0f,128.0f,255.0f);
363  _colors["BE"]= glm::vec3(194.0f,255.0f, 0.0f);
364  _colors["B"] = glm::vec3(255.0f,181.0f,181.0f);
365  _colors["C"] = glm::vec3(144.0f,144.0f,144.0f);
366  _colors["N"] = glm::vec3( 48.0f, 80.0f,248.0f);
367  _colors["O"] = glm::vec3(255.0f, 13.0f, 13.0f);
368  _colors["F"] = glm::vec3(144.0f,224.0f, 80.0f);
369  _colors["NE"]= glm::vec3(179.0f,227.0f,245.0f);
370  _colors["NA"]= glm::vec3(171.0f, 92.0f,242.0f);
371  _colors["MG"]= glm::vec3(138.0f,255.0f, 0.0f);
372  _colors["AL"]= glm::vec3(191.0f,166.0f,166.0f);
373  _colors["SI"]= glm::vec3(240.0f,200.0f,160.0f);
374  _colors["P"] = glm::vec3(255.0f,128.0f, 0.0f);
375  _colors["S"] = glm::vec3(255.0f,255.0f, 48.0f);
376  _colors["CL"]= glm::vec3( 31.0f,240.0f, 31.0f);
377  _colors["AR"]= glm::vec3(128.0f,209.0f,227.0f);
378  _colors["K"] = glm::vec3(143.0f, 64.0f,212.0f);
379  _colors["CA"]= glm::vec3( 61.0f,255.0f, 0.0f);
380  _colors["SC"]= glm::vec3(230.0f,230.0f,230.0f);
381  _colors["TI"]= glm::vec3(191.0f,194.0f,199.0f);
382  _colors["V"] = glm::vec3(166.0f,166.0f,171.0f);
383  _colors["CR"]= glm::vec3(138.0f,153.0f,199.0f);
384  _colors["MN"]= glm::vec3(156.0f,122.0f,199.0f);
385  _colors["FE"]= glm::vec3(224.0f,102.0f, 51.0f);
386  _colors["CO"]= glm::vec3(240.0f,144.0f,160.0f);
387  _colors["NI"]= glm::vec3( 80.0f,208.0f, 80.0f);
388  _colors["CU"]= glm::vec3(200.0f,128.0f, 51.0f);
389  _colors["ZN"]= glm::vec3(125.0f,128.0f,176.0f);
390  _colors["GA"]= glm::vec3(194.0f,143.0f,143.0f);
391  _colors["GE"]= glm::vec3(102.0f,143.0f,143.0f);
392  _colors["AS"]= glm::vec3(189.0f,128.0f,227.0f);
393  _colors["SE"]= glm::vec3(255.0f,161.0f, 0.0f);
394  _colors["BR"]= glm::vec3(166.0f, 41.0f, 41.0f);
395  _colors["KR"]= glm::vec3( 92.0f,184.0f,209.0f);
396  _colors["RB"]= glm::vec3(112.0f, 46.0f,176.0f);
397  _colors["SR"]= glm::vec3( 0.0f,255.0f, 0.0f);
398  _colors["Y"] = glm::vec3(148.0f,255.0f,255.0f);
399  _colors["ZR"]= glm::vec3(148.0f,224.0f,224.0f);
400  _colors["NB"]= glm::vec3(115.0f,194.0f,201.0f);
401  _colors["MO"]= glm::vec3( 84.0f,181.0f,181.0f);
402  _colors["TC"]= glm::vec3( 59.0f,158.0f,158.0f);
403  _colors["RU"]= glm::vec3( 36.0f,143.0f,143.0f);
404  _colors["RH"]= glm::vec3( 10.0f,125.0f,140.0f);
405  _colors["PD"]= glm::vec3( 0.0f,105.0f,133.0f);
406  _colors["AG"]= glm::vec3(192.0f,192.0f,192.0f);
407  _colors["CD"]= glm::vec3(255.0f,217.0f,143.0f);
408  _colors["IN"]= glm::vec3(166.0f,117.0f,115.0f);
409  _colors["SN"]= glm::vec3(102.0f,128.0f,128.0f);
410  _colors["SB"]= glm::vec3(158.0f, 99.0f,181.0f);
411  _colors["TE"]= glm::vec3(212.0f,122.0f, 0.0f);
412  _colors["I"] = glm::vec3(148.0f, 0.0f,148.0f);
413  _colors["XE"]= glm::vec3( 66.0f,158.0f,176.0f);
414  _colors["CS"]= glm::vec3( 87.0f, 23.0f,143.0f);
415  _colors["BA"]= glm::vec3( 0.0f,201.0f, 0.0f);
416  _colors["LA"]= glm::vec3(112.0f,212.0f,255.0f);
417  _colors["CE"]= glm::vec3(255.0f,255.0f,199.0f);
418  _colors["PR"]= glm::vec3(217.0f,255.0f,199.0f);
419  _colors["ND"]= glm::vec3(199.0f,255.0f,199.0f);
420  _colors["PM"]= glm::vec3(163.0f,255.0f,199.0f);
421  _colors["SM"]= glm::vec3(143.0f,255.0f,199.0f);
422  _colors["EU"]= glm::vec3( 97.0f,255.0f,199.0f);
423  _colors["GD"]= glm::vec3( 69.0f,255.0f,199.0f);
424  _colors["TB"]= glm::vec3( 48.0f,255.0f,199.0f);
425  _colors["DY"]= glm::vec3( 31.0f,255.0f,199.0f);
426  _colors["HO"]= glm::vec3( 0.0f,255.0f,156.0f);
427  _colors["ER"]= glm::vec3( 0.0f,230.0f,117.0f);
428  _colors["TM"]= glm::vec3( 0.0f,212.0f, 82.0f);
429  _colors["YB"]= glm::vec3( 0.0f,191.0f, 56.0f);
430  _colors["LU"]= glm::vec3( 0.0f,171.0f, 36.0f);
431  _colors["HF"]= glm::vec3( 77.0f,194.0f,255.0f);
432  _colors["TA"]= glm::vec3( 77.0f,166.0f,255.0f);
433  _colors["W"] = glm::vec3( 33.0f,148.0f,214.0f);
434  _colors["RE"]= glm::vec3( 38.0f,125.0f,171.0f);
435  _colors["OS"]= glm::vec3( 38.0f,102.0f,150.0f);
436  _colors["IR"]= glm::vec3( 23.0f, 84.0f,135.0f);
437  _colors["PT"]= glm::vec3(208.0f,208.0f,224.0f);
438  _colors["AU"]= glm::vec3(255.0f,209.0f, 35.0f);
439  _colors["HG"]= glm::vec3(184.0f,184.0f,208.0f);
440  _colors["TL"]= glm::vec3(166.0f, 84.0f, 77.0f);
441  _colors["PB"]= glm::vec3( 87.0f, 89.0f, 97.0f);
442  _colors["BI"]= glm::vec3(158.0f, 79.0f,181.0f);
443  _colors["PO"]= glm::vec3(171.0f, 92.0f, 0.0f);
444  _colors["AT"]= glm::vec3(117.0f, 79.0f, 69.0f);
445  _colors["RN"]= glm::vec3( 66.0f,130.0f,150.0f);
446  _colors["FR"]= glm::vec3( 66.0f, 0.0f,102.0f);
447  _colors["RA"]= glm::vec3( 0.0f,125.0f, 0.0f);
448  _colors["AC"]= glm::vec3(112.0f,171.0f,250.0f);
449  _colors["TH"]= glm::vec3( 0.0f,186.0f,255.0f);
450  _colors["PA"]= glm::vec3( 0.0f,161.0f,255.0f);
451  _colors["U"] = glm::vec3( 0.0f,143.0f,255.0f);
452  _colors["NP"]= glm::vec3( 0.0f,128.0f,255.0f);
453  _colors["PU"]= glm::vec3( 0.0f,107.0f,255.0f);
454  _colors["AM"]= glm::vec3( 84.0f, 92.0f,242.0f);
455  _colors["CM"]= glm::vec3(120.0f, 92.0f,227.0f);
456  _colors["BK"]= glm::vec3(138.0f, 79.0f,227.0f);
457  _colors["CF"]= glm::vec3(161.0f, 54.0f,212.0f);
458  _colors["ES"]= glm::vec3(179.0f, 31.0f,212.0f);
459  _colors["FM"]= glm::vec3(179.0f, 31.0f,186.0f);
460  _colors["MD"]= glm::vec3(179.0f, 13.0f,166.0f);
461  _colors["NO"]= glm::vec3(189.0f, 13.0f,135.0f);
462  _colors["LR"]= glm::vec3(199.0f, 0.0f,102.0f);
463  _colors["RF"]= glm::vec3(204.0f, 0.0f, 89.0f);
464  _colors["DB"]= glm::vec3(209.0f, 0.0f, 79.0f);
465  _colors["SG"]= glm::vec3(217.0f, 0.0f, 69.0f);
466  _colors["BH"]= glm::vec3(224.0f, 0.0f, 56.0f);
467  _colors["HS"]= glm::vec3(230.0f, 0.0f, 46.0f);
468  _colors["MT"]= glm::vec3(235.0f, 0.0f, 38.0f);
469 }
470 
471 
473  return _bounding_sphere;
474 }
475 
476