Debian Codegraph @

src/Mol.cpp

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