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

msmsplugin.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2016 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: msmsplugin.C,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.15 $       $Date: 2017/10/27 18:00:57 $
00015  *
00016  ***************************************************************************/
00017 
00018 /* 
00019  * Reader for MSMS surface files
00020  */
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include "molfile_plugin.h"
00025 
00026 typedef struct {
00027   FILE *ffd;
00028   FILE *vfd;
00029   molfile_graphics_t *graphics;
00030 } msms_t;
00031 
00032 // Get a string from a stream, printing any errors that occur
00033 static char *msmsgets(char *s, int n, FILE *stream) {
00034   char *returnVal;
00035 
00036   if (feof(stream)) {
00037     return NULL;
00038   } else if (ferror(stream)) {
00039     return NULL;
00040   } else {
00041     returnVal = fgets(s, n, stream);
00042   }
00043 
00044   return returnVal;
00045 }
00046 
00047 
00048 static void *open_file_read(const char *filepath, const char *filetype,
00049     int *natoms) {
00050   FILE *ffd; // face file
00051   FILE *vfd; // vertex file
00052   msms_t *msms;
00053   char * facefilepath;
00054   char * vertfilepath;
00055   char * cp;
00056 
00057   int filenamelen = strlen(filepath);
00058   facefilepath = (char *) malloc(filenamelen + 10);
00059   vertfilepath = (char *) malloc(filenamelen + 10);
00060   strcpy(facefilepath, filepath);
00061   strcpy(vertfilepath, filepath);
00062 
00063   // require the MSMS output filenames to match what MSMS does
00064   // If the user selected the .face file or the .vert file, we should
00065   // be able to cope either way by assigning the right filenames to the
00066   // right strings and getting the right files opened accordingly.
00067   cp = strstr(facefilepath, ".face");
00068   if (cp == NULL) {
00069     cp = strstr(facefilepath, ".vert");
00070     if (cp != NULL) {
00071        strcpy(cp, ".face");
00072     } else {
00073       printf("msmsplugin) file names don't match expected MSMS output\n");
00074       free(facefilepath);
00075       free(vertfilepath);
00076       return NULL;
00077     } 
00078   }
00079   cp = strstr(vertfilepath, ".vert");
00080   if (cp == NULL) {
00081     cp = strstr(vertfilepath, ".face");
00082     if (cp != NULL) {
00083        strcpy(cp, ".vert");
00084     } else {
00085       printf("msmsplugin) file names don't match expected MSMS output\n");
00086       free(facefilepath);
00087       free(vertfilepath);
00088       return NULL;
00089     } 
00090   }
00091  
00092   ffd = fopen(facefilepath, "r");
00093   vfd = fopen(vertfilepath, "r");
00094   if (!ffd || !vfd) { 
00095     printf("msmsplugin) failed to open either the MSMS face or vertex file\n");
00096     if (ffd) fclose(ffd);
00097     if (vfd) fclose(vfd);
00098     free(facefilepath);
00099     free(vertfilepath);
00100     return NULL;
00101   }
00102   msms = new msms_t;
00103   msms->ffd = ffd;
00104   msms->vfd = vfd;
00105   msms->graphics = NULL;
00106   *natoms = 0;
00107   free(facefilepath);
00108   free(vertfilepath);
00109   return msms;
00110 }
00111 
00112 static int read_rawgraphics(void *v, int *nelem, 
00113     const molfile_graphics_t **data) {
00114   msms_t *msms = (msms_t *)v;
00115   #define LINESIZE 180
00116   char inbuf[LINESIZE];
00117   int i, t;
00118   float tf=0.0f;
00119   int facecount=0;
00120   int vertexcount=0;
00121 
00122   //
00123   // count number of faces
00124   //
00125   while (msmsgets(inbuf, LINESIZE, msms->ffd) != NULL) {
00126     if (sscanf(inbuf, "%d %d %d %d %d", &t, &t, &t, &t, &t) == 5) 
00127       facecount++;
00128   }
00129   rewind(msms->ffd);
00130 
00131   //
00132   // count number of vertices
00133   //
00134   while (msmsgets(inbuf, LINESIZE, msms->vfd) != NULL) {
00135     if (sscanf(inbuf, "%f %f %f %f %f %f %d %d %d", 
00136         &tf, &tf, &tf, &tf, &tf, &tf, &t, &t, &t) == 9)
00137       vertexcount++;
00138   }
00139   rewind(msms->vfd);
00140 
00141   // simple sanity check to insure we have at least one usable triangle
00142   if (facecount < 1 || vertexcount < 3) 
00143     return MOLFILE_ERROR;
00144 
00145   // allocate storage for vertex and normal data
00146   float *vertex = new float[3 * vertexcount];
00147   float *normal = new float[3 * vertexcount];
00148 
00149   //
00150   // read in the vertex data
00151   //
00152   i=0;
00153   while (msmsgets(inbuf, LINESIZE, msms->vfd) != NULL) {
00154     int addr = i * 3;
00155     int atomid, l0fa, l;
00156     
00157     if (inbuf[0] != '#') { 
00158       if (sscanf(inbuf, "%f %f %f %f %f %f %d %d %d",
00159                  &vertex[addr], &vertex[addr+1], &vertex[addr+2], 
00160                  &normal[addr], &normal[addr+1], &normal[addr+2], 
00161                  &l0fa, &atomid, &l) == 9)
00162         i++;
00163     }
00164   }
00165  
00166   // allocate the graphics objects, read in the facet data and 
00167   // copy the vertex coordinates into triangles as necessary 
00168   msms->graphics = new molfile_graphics_t[2*facecount];
00169 
00170   //
00171   // read in the facet data
00172   //
00173   i=0;
00174   while (msmsgets(inbuf, LINESIZE, msms->ffd) != NULL) {
00175     int v0, v1, v2, surftype, ana;
00176 
00177     if (inbuf[0] != '#') { 
00178       // read in the next facet
00179       if (sscanf(inbuf, "%d %d %d %d %d", &v0, &v1, &v2, &surftype, &ana) == 5) {
00180         // set the graphics object type
00181         msms->graphics[2*i    ].type = MOLFILE_TRINORM;  
00182         msms->graphics[2*i + 1].type = MOLFILE_NORMS;
00183 
00184         v0--; // convert from 1-based indexing to 0-based indexing
00185         v1--;
00186         v2--;
00187 
00188         // copy the triangle vertices
00189         float *tri = msms->graphics[2*i    ].data;
00190         float *nrm = msms->graphics[2*i + 1].data;
00191         memcpy(tri  , vertex+(3*v0), 3*sizeof(float)); 
00192         memcpy(tri+3, vertex+(3*v1), 3*sizeof(float)); 
00193         memcpy(tri+6, vertex+(3*v2), 3*sizeof(float)); 
00194         memcpy(nrm  , normal+(3*v0), 3*sizeof(float)); 
00195         memcpy(nrm+3, normal+(3*v1), 3*sizeof(float)); 
00196         memcpy(nrm+6, normal+(3*v2), 3*sizeof(float)); 
00197 
00198         i++;
00199       }
00200     }
00201   }
00202 
00203   // set the result array pointers
00204   *nelem = 2*facecount;
00205   *data = msms->graphics;
00206 
00207   // delete work area storage
00208   delete [] normal;
00209   delete [] vertex;
00210 
00211   return MOLFILE_SUCCESS;
00212 }
00213 
00214 
00215 static void close_file_read(void *v) {
00216   msms_t *msms = (msms_t *)v;
00217   fclose(msms->ffd);
00218   fclose(msms->vfd);
00219   delete [] msms->graphics;
00220   delete msms;
00221 }
00222 
00223 
00224 /*
00225  * Initialization stuff here
00226  */
00227 static molfile_plugin_t plugin;
00228 
00229 VMDPLUGIN_API int VMDPLUGIN_init(void) { 
00230   memset(&plugin, 0, sizeof(molfile_plugin_t));
00231   plugin.abiversion = vmdplugin_ABIVERSION;
00232   plugin.type = MOLFILE_PLUGIN_TYPE;
00233   plugin.name = "msms";
00234   plugin.prettyname = "MSMS Surface Mesh";
00235   plugin.author = "John Stone";
00236   plugin.majorv = 0;
00237   plugin.minorv = 6;
00238   plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00239   plugin.filename_extension = "face,vert";
00240   plugin.open_file_read = open_file_read;
00241   plugin.read_rawgraphics = read_rawgraphics;
00242   plugin.close_file_read = close_file_read;
00243   return VMDPLUGIN_SUCCESS; 
00244 }
00245 
00246 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00247   (*cb)(v, (vmdplugin_t *)&plugin);
00248   return VMDPLUGIN_SUCCESS;
00249 }
00250 
00251 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00252 
00253 

Generated on Wed Apr 24 03:07:24 2024 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002