Debian Codegraph @

src/Mol.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 #include <GL/glew.h>
2
3 typedef unsigned char Byte;
4
5 #include <vector>
6 #include <string>
7 #include <algorithm>
8
9 #include <vcg/space/point3.h>
10 #include <vcg/space/color4.h>
11
12 using namespace vcg;
13 using namespace std;
14 #include "CubeMapSamp.h"
15 #include "OctaMapSamp.h"
16 #include "Mol.h"
17 #include "HardSettings.h"
18
19 #include "Progress.h"
20 #include "AtomColor.h"
21
22 #include "MyCanvas.h"
23
24 #include "CgUtil.h"
25
26 float stick_radius;
27
28 static int lx=0,ly=0;
29 extern int BSIZE;
30 extern int CSIZE; // number of texels for a patch of an aotm
31
32 int BSIZEx, BSIZEy; // number of texels for a pach of a bond
33 int SubPatchX, SubPatchY;
34 // an atom size is subdivided in this many subpatches for bonds
35
36
37
38 #define TOO_BIG 0
39 #define TOO_SMALL 1
40 #define SIZE_OK 2
41
42
43 Mol::Mol()
44 {
45 vboNorm=vboShadow=vboAO=666; // not ready
46 vboNormSt=vboShadowSt=vboAOSt=666; // not ready
47 filename[0]=0;
48 AOdoneLvl=0;
49 ready=false;
50 natm = nhetatm = 0;
51 //DL_bonds = DL_atoms = 666;
52 colMode=1.0;
53 }
54
55 // how many texels per patch?
56 int SetCsize(int textsize, int natoms){
57
58 // initial guess:
59 int i=(int)ceil(sqrt((double)natoms));
60 BSIZE=CSIZE= textsize / i;
61
62 if (CSIZE>250) {
63 BSIZE=CSIZE=250;
64 return TOO_BIG;
65 }
66
67 if (CSIZE<6) {
68 return TOO_SMALL;
69 }
70
71 return SIZE_OK;
72 }
73
74 bool QAtom::AssignNextTextPos(int texsize){
75
76
77 if (texsize<0) { lx=ly=0; return true;};
78
79 tx=lx;
80 ty=ly;
81
82 if ((lx+s.TotTexSizeX()>texsize) || (ly+s.TotTexSizeY()>texsize)) return false;
83
84 lx+=s.TotTexSizeX();
85 if (lx+s.TotTexSizeX()>texsize) {
86 ly+=s.TotTexSizeY();
87 lx=0;
88 }
89
90 return true;
91 }
92
93 bool Bond::AssignNextTextPos(int texsize){
94 if (texsize<0) { lx=ly=0; return true;};
95
96 tx=lx;
97 ty=ly;
98
99 if ((lx+BSIZE>texsize) || (ly+CSIZE>texsize)) return false;
100
101 lx+=BSIZE;
102 if (lx+BSIZE>texsize) {
103 ly+=CSIZE;
104 lx=0;
105 }
106 return true;
107 }
108
109 bool Mol::ReassignTexture(int textsize) {
110 // reset texture
111 atom[0].AssignNextTextPos( -1 );
112
113
114 // assign texture positions
115 for (int i=0; i<atom.size(); i++) {
116 if (!atom[i].AssignNextTextPos( textsize )) return false;
117 }
118
119 if (sticks)
120 for (int i=0; i<bond.size(); i++) {
121 if (!bond[i].AssignNextTextPos( textsize )) return false;
122 }
123
124 return true;
125 /*for (int i=0; i<atom.size(); i++)
126 atom[i].FillTexture(texture, textsize);*/
127 //printf("Texture Filled!\n");
128 }
129
130 extern CgUtil cgSettings;
131
132 void Mol::ReassignTextureAutosize() {
133 if (textureAssigned) return;
134
135 /*
136 // JUST A TEST:
137 if (sticks) {
138 BSIZE=CSIZE=5;
139 }
140 else {
141 BSIZE=CSIZE=10;
142 }
143 CubeMapSamp::SetSize(CSIZE);
144 OctaMapSamp::SetSize(CSIZE);
145 moltextureCanvas.SetRes(1024);
146 ReassignTexture(1024);
147
148 textureAssigned=true;
149 return;
150 */
151
152 int nEl = atom.size();
153 if (sticks) nEl+= bond.size();
154
155 int guess = hardSettings.TSIZE;
156
157 int lastThatWorked = guess;
158
159 bool enlarge=false, shrink=false, forced=false;
160
161 do {
162
163 if (enlarge && shrink) forced=true;
164
165 moltextureCanvas.SetRes(guess);
166
167 /*
168 if (guess != hardSettings.TSIZE)
169 if (!forced)
170 if (!moltextureCanvas.Test()) {
171 // use this res!
172 guess = lastThatWorked ;
173 forced=true;
174 continue;
175 }
176 */
177
178 lastThatWorked=guess;
179
180 int res= SetCsize(guess, nEl);
181
182 if (!forced) {
183
184 if ((res==TOO_BIG) && (guess/2>=16)) {
185 shrink=true;
186 guess/=2;
187 continue;
188 }
189
190 if ((res==TOO_SMALL)&& (guess*2<=hardSettings.MAX_TSIZE)) {
191 enlarge=true;
192 guess*=2;
193 continue;
194 }
195 }
196
197 CubeMapSamp::SetSize(CSIZE);
198 OctaMapSamp::SetSize(CSIZE);
199
200 if (!ReassignTexture(guess)) {
201 // should never happen
202 }
203
204 break;
205 } while (1);
206
207
208 }
209
210 Bond::Bond(const QAtom &_a,const QAtom &_b):
211 atmA(&_a), atmB(&_b)
212 {
213
214
215 a=_a.P();
216 b=_b.P();
217
218 dir=(a-b).Normalize();
219
220 float rada= _a.covalentr;
221 float radb= _b.covalentr;
222
223 /*a-=dir*(rada*0.60);
224 b+=dir*(radb*0.60);*/
225 lenght=(a-b).Norm();
226 startp=dir^Point3f(0,0,1);
227 if (startp.SquaredNorm()<0.1) startp=dir^Point3f(0,1,0);
228 startp.Normalize();
229
230 hetatomFlag=_a.hetatomFlag || _b.hetatomFlag;
231
232 UpdateColors();
233
234 }
235
236
237 void Mol::AddBond(int i, int j)
238 {
239 bond.push_back( Bond( atom[i], atom[j] ) );
240 }
241
242 void Mol::FindBonds(){
243 /*
244 // (DRAMATICALLY) QUADRATIC VERSION
245 for (int i=0; i<atom.size(); i++)
246 for (int j=0; j<i; j++) {
247
248 const float mind=0.4;
249 float maxd=atom[i].covalentr+atom[j].covalentr+0.56;
250 float dist=(atom[i].P()-atom[j].P()).SquaredNorm();
251 if ( (dist>=mind*mind) && (dist<=maxd*maxd) ) {
252 bond.push_back( Bond( atom[i].P(), atom[j].P(),
253 atom[i].covalentr, atom[j].covalentr,
254 Point3f(atom[i].cr,atom[i].cg,atom[i].cb),
255 Point3f(atom[j].cr,atom[j].cg,atom[j].cb)
256 ) );
257 }
258 }
259 */
260
261 // (HOPEFULLY) PSEUDO-LINEAR VERSION
262
263 // sort, then find bonds
264 const float MIND=0.4;
265 const float MAXD=MAX_COVALENT_RADIUS*2+0.56;
266
267 bool showprogress=atom.size()>2000;
268
269 if (showprogress) StartProgress("Locating Bonds",atom.size() );
270
271 // Sort atoms per x coordinate
272 std::sort( atom.begin(), atom.end() );
273
274
275 int st=0; // start interval
276 for (int i=0; i<atom.size(); i++) {
277 if (showprogress) if (i%100) if (!UpdateProgress(i)) {
278 bond.clear();
279 //SetSpaceFill();
280 return;
281 };
282 float px=atom[i].px;
283 for (int j=st; j<atom.size(); j++) {
284 if (atom[j].px < px-MAXD) st=j; // move interval forward
285 else if (atom[j].px > px ) break; // no more atoms
286 else {
287 float maxd=atom[i].covalentr+atom[j].covalentr+0.56;
288 float dist=(atom[i].P()-atom[j].P()).SquaredNorm();
289 if ( (dist>=MIND*MIND) && (dist<=maxd*maxd) )
290 if ( // one way check
291 /*(atom[j].px != px ) ||
292 (atom[j].py < atom[i].py ) ||
293 ( (atom[j].py == atom[i].py ) && (atom[j].pz < atom[i].pz ) ) */
294 i>j
295 )
296 AddBond(i,j);
297 }
298 }
299 }
300
301
302 if (showprogress) EndProgress();
303 printf("%d bonds detected!\n",bond.size() );
304 }
305
306 void Mol::SetSpaceFill(){
307 if (sticks) textureAssigned=false;
308 sticks=false;
309
310 ReassignTextureAutosize();
311
312 for (int i=0; i<atom.size(); i++) atom[i].r=atom[i].spacefillr;
313 }
314
315 void Mol::SetBallAndSticks(float radius){
316 if (bond.size()==0) FindBonds();
317
318 if (!sticks) {
319 sticks=true;
320 textureAssigned=false;
321 UpdateColors(); // updata colori sticks
322 }
323
324 ReassignTextureAutosize();
325
326 for (int i=0; i<atom.size(); i++) {
327 atom[i].r=atom[i].covalentr;
328 if (atom[i].r<radius) atom[i].r=radius;
329 }
330 stick_radius=radius;
331 }
332
333 void Mol::SetLicorice(float radius){
334 if (bond.size()==0) FindBonds();
335
336 if (!sticks) {
337 sticks=true;
338 textureAssigned=false;
339 UpdateColors(); // updata colori sticks
340 }
341
342 ReassignTextureAutosize();
343
344 for (int i=0; i<atom.size(); i++) atom[i].r=radius;
345 stick_radius=radius;
346 }
347
348
349 void Mol::Transform(const Point3f &ax,const Point3f &ay,const Point3f &az){
350 atom[0].Transform(ax,ay,az);
351 tx0=atom[0].trp[0]-atom[0].r;
352 tx1=atom[0].trp[0]+atom[0].r;
353 ty0=atom[0].trp[1]-atom[0].r;
354 ty1=atom[0].trp[1]+atom[0].r;
355 for (int i=1; i<atom.size(); i++) {
356 atom[i].Transform(ax,ay,az);
357 // update bounding box
358 if (atom[i].trp[0]-atom[i].r<tx0) tx0=atom[i].trp[0]-atom[i].r;
359 if (atom[i].trp[1]-atom[i].r<ty0) ty0=atom[i].trp[1]-atom[i].r;
360 if (atom[i].trp[0]+atom[i].r>tx1) tx1=atom[i].trp[0]+atom[i].r;
361 if (atom[i].trp[1]+atom[i].r>ty1) ty1=atom[i].trp[1]+atom[i].r;
362 }
363 sort(atom.begin(), atom.end() );
364 }
365
366 void Mol::DuplicateTexels(vector<Byte> &t, int texsize){
367 for (int i=0; i<atom.size(); i++) {
368 atom[i].s.DuplicateTexels(t, texsize, atom[i].tx, atom[i].ty);
369 }
370 }
371
372
373 // color from atom name
374 void QAtom::getCol(const char* st){
375 atomColor=getAtomColor(st);
376 }
377
378 /*
379 void QAtom::getCol(const char* st){
380 int tmp=st[0]*4123+st[1]*1242+st[2]*4561+3141*st[3];
381 tmp%=155;
382 double hue=tmp/155.0;
383 if (hue<0.5) {cg=hue*2; cr=1.0-cg; cb=0;}
384 else { cr=0; cb=(hue-0.5)*2; cg=1.0-cb; }
385 const float W=0.70;
386 cr=cr*(1-W)+W;
387 cg=cg*(1-W)+W;
388 cb=cb*(1-W)+W;
389 }
390 */
391
392 // Helper function:
393 // Parse a line of the PDB file with the CONECT info
394 // Obviously atom id are 1 based indexes.
395 static void AddTmpBonds(string st, vector<int> &v)
396 {
397 int startAtom=atoi( st.substr(6,5).c_str());
398 // assert(startAtom >0 && (startAtom-1)<atom.size()); // this assert cannot be used anymore from a static method
399 int i=1;
400 while(st.size()>6+i*5+4 && isdigit(st[6+i*5+4]))
401 {
402 // printf("'%s' -> '%s' (%c)\n",st.substr(6,5).c_str(),st.substr(6+i*5,5).c_str(),st[6+i*5+4]);
403 int secondAtom=atoi( st.substr(6+i*5,5).c_str());
404 //printf("(%i) CONECT %i -> %i\n",i,startAtom-1,secondAtom-1);
405 ++i;
406 v.push_back(startAtom-1);
407 v.push_back(secondAtom-1);
408 //AddBond(startAtom-1,secondAtom-1);
409 }
410 // printf("Found %i conect\n",i);
411 }
412
413 bool Mol::ReadPdb(const char *_filename){
414
415 ready=false;
416
417 sprintf(filename,"%s",_filename);
418
419 FILE *f=fopen(filename,"r");
420
421 if (!f) {
422 sprintf(QAtom::lastReadError,"Error reding %s:\ncould not open file\n",filename);
423 return false;
424 }
425
426 long size=0;
427 if (fseek(f, 0, SEEK_END)==0) size=ftell (f);
428 fseek (f, 0, SEEK_SET);
429 bool showprogress=(size==0) || (size>1000000);
430 if (showprogress) StartProgress("Reading file", size);
431
432 char buf[82];
433 buf[81]=0;
434
435 atom.clear();
436 bond.clear();
437 textureAssigned=false;
438
439 QAtom::readError=false;
440 int lineN=0;
441
442 vector<int> tmpBondVec;
443
444 while (1){
445 if(! fgets(buf,81,f)) break;
446 lineN++;
447 if ((lineN % 1000) == 0) if (size!=0) if (showprogress) {
448 if (!UpdateProgress((int)ftell(f))) return false;
449 }
450 string st(buf);
451 if ( (strcmp( st.substr(0,6).c_str(), "ATOM ") == 0 ) ||
452 (strcmp( st.substr(0,6).c_str(), "HETATM") == 0 ) ){
453 atom.push_back(QAtom(st));
454 if (QAtom::readError) {
455 sprintf(QAtom::lastReadError,"Error reading %s!\nLine %d: %s",filename,lineN,&(st[0]));
456 if (showprogress) EndProgress();
457 return false;
458 }
459 }
460 if(strcmp( st.substr(0,6).c_str(), "CONECT") == 0 )
461 AddTmpBonds(st, tmpBondVec);
462
463 }
464 //printf("Found %d atoms!\n", atom.size() );
465
466 nhetatm=0; natm=0;
467 for (int i=0; i<atom.size(); i++){
468 if (atom[i].hetatomFlag) nhetatm++; else natm++;
469 }
470
471 ComputeSize();
472
473 UpdateColors();
474
475 for (int i=0; i<tmpBondVec.size(); i+=2) {
476 AddBond(tmpBondVec[i],tmpBondVec[i+1]);
477 }
478
479
480
481 if (showprogress) EndProgress();
482 //if (sticks) FindBonds();
483
484 //ReassignTextureAutosize();
485
486 //SetCsize(hardSettings.TSIZE, atom.size() *21 / 10 ); // approx 2.1 bonds per atom
487
488 ready=true;
489 ResetAO();
490 return true;
491 }
492
493 char* Mol::GetFilenameTex(){
494 static char res[1024];
495 sprintf(res,"%s.ppm",filename);
496 return res;
497 }
498
499 char* Mol::GetFilenameSnap(){
500 static char res[2048];
501 sprintf(res,"%s",filename);
502 // REMOVE PATH: quick and dirty: find last '/'
503 int i=0, pos=-1;
504 while (res[i]) {
505 if ((res[i]=='\\') || (res[i]=='/')) pos=i;
506 i++;
507 }
508 return res+pos+1;
509 return filename;
510 }
511
512 char* Mol::GetMolName(){
513 static char res[2048];
514 sprintf(res,"%s",filename);
515 // REMOVE PATH: quick and dirty: find last '/'
516 int i=0, pos=-1;
517 while (res[i]) {
518 if ((res[i]=='\\') || (res[i]=='/')) pos=i;
519 i++;
520 }
521 return res+pos+1;
522 return filename;
523 }
524
525 void Mol::ComputeSize(){
526 // bounding box!
527 float x0,y0,z0,x1,y1,z1;
528 if (atom.size()>0)
529 x0=x1=atom[0].px;
530 y0=y1=atom[0].py;
531 z0=z1=atom[0].pz;
532 for (int i=1; i<atom.size(); i++) {
533 if (x1>atom[i].px) x1=atom[i].px;
534 if (y1>atom[i].py) y1=atom[i].py;
535 if (z1>atom[i].pz) z1=atom[i].pz;
536 if (x0<atom[i].px) x0=atom[i].px;
537 if (y0<atom[i].py) y0=atom[i].py;
538 if (z0<atom[i].pz) z0=atom[i].pz;
539 }
540 px=(x0+x1)/2;
541 py=(y0+y1)/2;
542 pz=(z0+z1)/2;
543 int k=-4;
544 r=sqrt( (x1-x0+k)*(x1-x0+k) + (y1-y0+k)*(y1-y0+k) + (z1-z0+k)*(z1-z0+k) );
545 r*=0.5;
546
547 //printf("pos=(%f %f %f) size=%f\n",px,py,pz,r);
548
549
550 }
551
552 void mysscanf(const char* st, const char* format, float *f){
553 if (!sscanf( st, "%f", f)) {
554 if (sscanf( st, " - %f", f))
555 *f=-*f;
556 else *f=1.0;
557 }
558 }
559
560 void Mol::FillTexture(vector<Byte> &texture, const vector<int> &sumtable,
561 int texsize, float div )
562 {
563 /*
564 for (int i=0; i<atom.size(); i++)
565 atom[i].s.FillTexture(
566 texture, sumtable,
567 texsize, div , atom[i].tx, atom[i].ty );
568 */
569 }
570
571
572
573
574 void Bond::UpdateColors(){
575 col1=Point3f(atmA->cr,atmA->cg,atmA->cb);
576 col2=Point3f(atmB->cr,atmB->cg,atmB->cb);
577 }
578
579 void QAtom::UpdateColors(float mode){
580 Color4b ca,cc;
581
582 *((int *)&ca )= atomColor;
583
584 *((int *)&cc )= getChainColor( chainIndex );
585
586
587 cb=(float(ca[0])/255.0f)*mode+ (float(cc[0])/255.0f) * (1.0f-mode);
588 cg=(float(ca[1])/255.0f)*mode+ (float(cc[1])/255.0f) * (1.0f-mode);
589 cr=(float(ca[2])/255.0f)*mode+ (float(cc[2])/255.0f) * (1.0f-mode);
590
591 }
592
593 void Mol::UpdateColors(){
594 for (int i=0; i<atom.size(); i++){
595 atom[i].UpdateColors(colMode);
596 }
597
598 if (sticks)
599 for (int i=0; i<bond.size(); i++) {
600 bond[i].UpdateColors();
601 }
602 }
603
604 void Mol::SetColMode(float newColMode){
605 if (colMode!=newColMode) {
606 colMode=newColMode;
607 UpdateColors();
608 }
609 else colMode=newColMode;
610 }
611
612 bool QAtom::readError=false;
613 char QAtom::lastReadError[1024];
614
615
616
617 QAtom::QAtom(string st){
618 string elem = st.substr( 12, 2);
619 if (elem[0]>='a' && elem[0]<='z') elem[0]=elem[0]-'a'+'A';
620 if (elem[1]>='a' && elem[1]<='z') elem[1]=elem[1]-'a'+'A';
621 if (strcmp( st.substr(0,6).c_str(), "ATOM ") == 0 )
622 {
623 hetatomFlag=false;
624
625 getCol( elem.c_str() );
626 mysscanf(st.substr( 30, 8).c_str(),"%f", &px);
627 mysscanf(st.substr( 38, 8).c_str(),"%f", &py);
628 mysscanf(st.substr( 46, 8).c_str(),"%f", &pz);
629
630 chainIndex = (int) st[21];
631
632 r=spacefillr=getAtomRadius(elem.c_str()); // Van der Waals radii
633
634 covalentr=getAtomCovalentRadius(elem.c_str());
635 }
636 else if (strcmp( st.substr(0,6).c_str(), "HETATM") == 0 )
637 {
638 hetatomFlag=true;
639
640 getCol( elem.c_str() );
641 mysscanf(st.substr( 30, 8).c_str(),"%f", &px);
642 mysscanf(st.substr( 38, 8).c_str(),"%f", &py);
643 mysscanf(st.substr( 46, 8).c_str(),"%f", &pz);
644
645 chainIndex = (int) st[22];
646
647 r=spacefillr=getAtomRadius(elem.c_str()); // Van der Waals radii
648
649 covalentr=getAtomCovalentRadius(elem.c_str());
650 }
651 covalentr*=0.85; // mystical value!!!
652
653 }
654