home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Educational / MolViewer / Source / MolShape.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.0 KB  |  130 lines

  1. /* MolShape.m - Copyright 1993  Steve Ludtke */
  2. /* This object does the actual molecule rendering (except for quick mode) */
  3. /* ball and stick mode is still quite primitive */
  4. /* I should probably change the flags from numbers to #defined strings ... */
  5. #import "Mtypes.h"
  6. #import "MolShape.h"
  7. #import <ri/ri.h>
  8. #import <math.h>
  9.  
  10. #define SC .05
  11. #define SCP1 SC/1.732
  12. #define SCP2 SC/.866
  13. #define SCP3 SC/2.45
  14. #define SCP4 SC/1.633-SCP3
  15.  
  16. @implementation MolShape:N3DShape
  17. - renderSelf:(RtToken)context
  18. {
  19. int i,j,n,ln;
  20. float x,y,z,r,lx,ly,lz;
  21. /* a big square for a white background */
  22. static RtPoint bsquare[4]= {{-200.0,-200.0,-40.0},{200.0,-200.0,-40.0},{200.0,200.0,-40.0},{-200.0,200.0,-40.0}};
  23. static RtColor bgCol = {1.0,1.0,1.0};
  24. static RtColor bCol = { 0,0,0 };
  25. static RtColor dCol = { 1.0,1.0,1.0 };
  26. static RtColor selCol = { 0.0,0.0,0.0 };
  27.  
  28. if (mode==4) return self;    /* quick mode, do nothing */
  29. if (mode==12) mode=11;        /* quick mode, but we want to print, so render */
  30.  
  31. RiSurface("constant",RI_NULL);
  32. if (mode>=8) {
  33.     RiColor(bgCol);
  34.     RiPolygon(4,RI_P,(RtPointer)bsquare,RI_NULL);
  35. }
  36. RiRotate(chi,0,1.0,0);
  37. RiRotate(theta,1.0,0,0.0);
  38. RiRotate(phi,0,1.0,0);
  39.  
  40. /* metal is ok too ... */
  41. RiSurface("plastic",RI_NULL);
  42. RiColor(dCol);
  43.  
  44. /* stick mode */
  45. if ((flags&3)==0) {
  46.     RiColor(bCol);
  47.     for (i=0; i<nmol; i++) {
  48.         ln=-1;
  49.         if (mol[i].numlb==0) continue;
  50.         RiPointsLines(mol[i].numlb,mol[i].nverts,mol[i].verts
  51.                 ,RI_P,mol[i].coord,RI_NULL);
  52.     }
  53. }
  54. /* ball and stick mode, ball size ratio hardcoded */
  55. else if ((flags&3)==1) {
  56.     for (i=0; i<nmol; i++) {
  57.         if ((mode&3)>=2) RiColor(dCol);
  58.         else RiColor(dCol);
  59.         ln=-1;
  60.         for (j=0; j<mol[i].numa; j++) {
  61.             x=mol[i].coord[j][0];
  62.             y=mol[i].coord[j][1];
  63.             z=mol[i].coord[j][2];
  64.             n=mol[i].atom[j].anum;
  65.             r=elinfo[n].rad/3.0;
  66.             if (n!=ln) RiColor(elinfo[n].color);
  67.             ln=n;
  68.             RiTransformBegin();
  69.             RiTranslate(x,y,z);
  70.             RiSphere(r,-r,r,360.0,RI_NULL);
  71.             RiTransformEnd();             
  72.         }
  73.         RiColor(bCol);
  74.         RiPointsLines(mol[i].numlb,mol[i].nverts,mol[i].verts
  75.                 ,RI_P,mol[i].coord,RI_NULL);
  76.     }
  77. }
  78. /* space filling mode */
  79. else if ((flags&3)==2) {
  80.     lx=ly=lz=0;
  81.     for (i=0; i<nmol; i++) {
  82.         if ((mode&3)>=2) RiColor(dCol);
  83.         else RiColor(dCol);
  84.         ln=-1;
  85.         for (j=0; j<mol[i].numa; j++) {
  86.             x=mol[i].coord[j][0];
  87.             y=mol[i].coord[j][1];
  88.             z=mol[i].coord[j][2];
  89.             n=mol[i].atom[j].anum;
  90.             r=elinfo[n].rad;
  91.             if (n!=ln) RiColor(elinfo[n].color);
  92.             ln=n;
  93.             if (mol[i].atom[j].sel) { RiColor(selCol); ln=-1; }
  94.             RiTranslate(x-lx,y-ly,z-lz);
  95.             RiSphere(r,-r,r,360.0,RI_NULL);
  96.             if (r==0.0) printf("%d %d  %f %f %f\n",j,n,x,y,z);
  97.             lx=x; ly=y; lz=z;     
  98.         }
  99.     }
  100. }
  101. return self;
  102. }
  103.  
  104. -setData:(Molecule *)Mol :(int)Nmol :(struct ELINFO *)Elinfo :(int)Mode :(int)Flags
  105. {
  106. mol=Mol;
  107. nmol=Nmol;
  108. elinfo=Elinfo;
  109. mode=Mode;
  110. flags=Flags;
  111. return self;
  112. }
  113.  
  114. -setAng:(float)Theta :(float)Chi :(float)Phi
  115. {
  116. theta=Theta/DRC;
  117. chi=Chi/DRC;
  118. phi=Phi/DRC;
  119. return self;
  120. }
  121.  
  122. - setSurfaceType:(N3DSurfaceType)st andDescendants:(BOOL)flag
  123. {
  124. [super setSurfaceType:st andDescendants:flag];
  125. return self;
  126. }
  127.  
  128. @end
  129.   
  130.