Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

DrawMolItemSurface.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2011 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: DrawMolItemSurface.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.41 $       $Date: 2011/06/08 18:49:35 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  * This contains the surface code for DrawMolItem
00019  *
00020  * The surfaces are made with SURF, an external program.  It was
00021  * written by Amitabh Varshney when he was at UNC.  The code is
00022  * available from ftp.cs.unc.edu .
00023  ***************************************************************************/
00024 
00025 
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include "DrawMolecule.h"
00030 #include "DrawMolItem.h"
00031 #include "utilities.h"
00032 #include "Surf.h"
00033 #include "Inform.h"
00034 #include "Scene.h"
00035 
00036 // In general, the method is
00037 //    write the file for surf input
00038 //    call surf
00039 //    read the triangles
00040 //    write them to the draw list
00041 
00042 void DrawMolItem::draw_surface(float *framepos, int draw_wireframe, float radius) {
00043   // early-exit if nothing selected
00044   if (atomSel->selected == 0)
00045     return;
00046 
00047   // mapping from order printed out (selected) to atom id
00048   int *map = new int[atomSel->selected];
00049 
00050   int i;
00051   int count = 0; // count is number of atoms selected
00052   for (i=atomSel->firstsel; i <= atomSel->lastsel; i++) {
00053     if (atomSel->on[i]) {
00054       map[count++] = i;
00055     }
00056   }
00057 
00058   int surfs_up = 1;
00059 
00060   // regenerate sphere coordinates if necessary 
00061   if ( needRegenerate & MOL_REGEN ||
00062        needRegenerate & SEL_REGEN ||
00063        needRegenerate & REP_REGEN) {
00064 
00065     // then we need to recalculate the SURF
00066     surf.clear();
00067     float *x = new float[atomSel->selected];
00068     float *y = new float[atomSel->selected];
00069     float *z = new float[atomSel->selected];
00070     float *r = new float[atomSel->selected];
00071     const float *aradius = mol->radius();
00072 
00073     // We add all displayed atoms to the sphere array
00074     int j;
00075     for (int i=0; i<atomSel->selected; i++) {
00076       j = map[i];
00077       r[i] = aradius[j];
00078       x[i] = framepos[3*j+0];
00079       y[i] = framepos[3*j+1];
00080       z[i] = framepos[3*j+2];
00081     }
00082 
00083     // make the new surface -- returns 0 on failure
00084     surfs_up = surf.compute(radius, atomSel->selected, r, x, y, z);
00085       
00086     delete [] r;
00087     delete [] x;
00088     delete [] y;
00089     delete [] z;
00090   }
00091 
00092   // and display everything
00093   if (surfs_up && surf.numtriangles > 0) {
00094     int i, ind, vnum, vsize;  
00095     float *c;        
00096 
00097     append(DMATERIALON);
00098 
00099      vnum = surf.numtriangles * 3; // 3 vertices per triangle
00100     vsize = vnum * 3;              // 3 floats per vertex
00101 
00102     c = new float[vsize];
00103 
00104     for (i=0; i<surf.numtriangles; i++) {
00105       int col = atomColor->color[map[surf.ind[i]]];
00106       const float *fp = scene->color_value(col);
00107 
00108       ind = i * 9;
00109       c[ind    ] = fp[0]; // Red
00110       c[ind + 1] = fp[1]; // Green
00111       c[ind + 2] = fp[2]; // Blue
00112 
00113       ind+=3;
00114       c[ind    ] = fp[0]; // Red
00115       c[ind + 1] = fp[1]; // Green
00116       c[ind + 2] = fp[2]; // Blue
00117 
00118       ind+=3;
00119       c[ind    ] = fp[0]; // Red
00120       c[ind + 1] = fp[1]; // Green
00121       c[ind + 2] = fp[2]; // Blue
00122     }                                                           
00123 
00124     if (draw_wireframe) {
00125       int *l = new int[surf.numtriangles * 6];
00126       int i;
00127       for (i=0; i<surf.numtriangles; i++) {
00128         int li = i * 6; 
00129         int ll = i * 3;
00130         l[li    ] = ll + 0; 
00131         l[li + 1] = ll + 1; 
00132         l[li + 2] = ll + 1;
00133         l[li + 3] = ll + 2;
00134         l[li + 4] = ll + 2;
00135         l[li + 5] = ll + 0;
00136       }
00137 
00138       // Create a wire mesh
00139       cmdLineType.putdata(SOLIDLINE, cmdList); // set line drawing parameters
00140       cmdLineWidth.putdata(1, cmdList);
00141       cmdWireMesh.putdata(&surf.v[0], &surf.n[0], c, vnum, 
00142                           l, surf.numtriangles * 3, cmdList);
00143       delete [] l;
00144     } else {
00145       // Create a triangle mesh, but don't try to stripify it since
00146       // Surf doesn't generate connected geometry.
00147       cmdTriMesh.putdata(&surf.v[0], &surf.n[0], c, vnum, 
00148                          &surf.f[0], surf.numtriangles, 
00149                          0, cmdList);
00150     }
00151 
00152     delete [] c;
00153     delete [] map;
00154   }
00155 
00156   msgInfo << "Done." << sendmsg;
00157 }
00158 

Generated on Sat May 26 01:47:57 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002