Debian Codegraph @

src/Mol.cpp

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