Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

msmsplugin.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2006 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.7 $       $Date: 2006/02/23 19:36:45 $
00015  *
00016  ***************************************************************************/
00017 
00018 /* 
00019  * Reader for MSMS surface files
00020  */
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 //#include <math.h>
00024 #include <string.h>
00025 #include "molfile_plugin.h"
00026 
00027 typedef struct {
00028   FILE *ffd;
00029   FILE *vfd;
00030   molfile_graphics_t *graphics;
00031 } msms_t;
00032 
00033 static void *open_file_read(const char *filepath, const char *filetype,
00034     int *natoms) {
00035   FILE *ffd; // face file
00036   FILE *vfd; // vertex file
00037   msms_t *msms;
00038   char * facefilepath;
00039   char * vertfilepath;
00040   char * cp;
00041 
00042   int filenamelen = strlen(filepath);
00043   facefilepath = (char *) malloc(filenamelen + 10);
00044   vertfilepath = (char *) malloc(filenamelen + 10);
00045   strcpy(facefilepath, filepath);
00046   strcpy(vertfilepath, filepath);
00047 
00048   // require the MSMS output filenames to match what MSMS does
00049   // If the user selected the .face file or the .vert file, we should
00050   // be able to cope either way by assigning the right filenames to the
00051   // right strings and getting the right files opened accordingly.
00052   cp = strstr(facefilepath, ".face");
00053   if (cp == NULL) {
00054     cp = strstr(facefilepath, ".vert");
00055     if (cp != NULL) {
00056        strcpy(cp, ".face");
00057     } else {
00058       printf("msmsplugin) file names don't match expected MSMS output\n");
00059       free(facefilepath);
00060       free(vertfilepath);
00061       return NULL;
00062     } 
00063   }
00064   cp = strstr(vertfilepath, ".vert");
00065   if (cp == NULL) {
00066     cp = strstr(vertfilepath, ".face");
00067     if (cp != NULL) {
00068        strcpy(cp, ".vert");
00069     } else {
00070       printf("msmsplugin) file names don't match expected MSMS output\n");
00071       free(facefilepath);
00072       free(vertfilepath);
00073       return NULL;
00074     } 
00075   }
00076  
00077   ffd = fopen(facefilepath, "r");
00078   vfd = fopen(vertfilepath, "r");
00079   if (!ffd || !vfd) { 
00080     printf("msmsplugin) failed to open either the MSMS face or vertex file\n");
00081     if (ffd) fclose(ffd);
00082     if (vfd) fclose(vfd);
00083     free(facefilepath);
00084     free(vertfilepath);
00085     return NULL;
00086   }
00087   msms = new msms_t;
00088   msms->ffd = ffd;
00089   msms->vfd = vfd;
00090   msms->graphics = NULL;
00091   *natoms = 0;
00092   return msms;
00093 }
00094 
00095 static int read_rawgraphics(void *v, int *nelem, 
00096     const molfile_graphics_t **data) {
00097   msms_t *msms = (msms_t *)v;
00098   int i, t;
00099   float tf;
00100   int facecount=0;
00101   int vertexcount=0;
00102   // count number of faces
00103   while (fscanf(msms->ffd, "%d %d %d %d %d", &t, &t, &t, &t, &t) == 5) 
00104     facecount++;
00105   rewind(msms->ffd);
00106   // count number of vertices
00107   while (fscanf(msms->vfd, "%f %f %f %f %f %f %d %d %d", 
00108          &tf, &tf, &tf, &tf, &tf, &tf, &t, &t, &t) == 9)
00109     vertexcount++;
00110   rewind(msms->vfd);
00111 
00112   // simple sanity check to insure we have at least one usable triangle
00113   if (facecount < 1 || vertexcount < 3) 
00114     return MOLFILE_ERROR;
00115 
00116   // allocate storage for vertex and normal data
00117   float *vertex = new float[3 * vertexcount];
00118   float *normal = new float[3 * vertexcount];
00119 
00120   // read in the vertex data
00121   for (i=0; i<vertexcount; i++) {
00122     int addr = i * 3;
00123     int atomid, l0fa, l;
00124     fscanf(msms->vfd, "%f %f %f %f %f %f %d %d %d",
00125            &vertex[addr], &vertex[addr+1], &vertex[addr+2], 
00126            &normal[addr], &normal[addr+1], &normal[addr+2], &l0fa, &atomid, &l);
00127   }
00128  
00129   // allocate the graphics objects, read in the facet data and 
00130   // copy the vertex coordinates into triangles as necessary 
00131   msms->graphics = new molfile_graphics_t[2*facecount];
00132 
00133   // read in the facet data
00134   for (i=0; i<facecount; i++) {
00135     int v0, v1, v2, surftype, ana;
00136     // set the graphics object type
00137     msms->graphics[2*i    ].type = MOLFILE_TRINORM;  
00138     msms->graphics[2*i + 1].type = MOLFILE_NORMS;
00139 
00140     // read in the next facet
00141     fscanf(msms->ffd, "%d %d %d %d %d", &v0, &v1, &v2, &surftype, &ana);
00142     v0--; // convert from 1-based indexing to 0-based indexing
00143     v1--;
00144     v2--;
00145 
00146     // copy the triangle vertices
00147     float *tri = msms->graphics[2*i    ].data;
00148     float *nrm = msms->graphics[2*i + 1].data;
00149     memcpy(tri  , vertex+(3*v0), 3*sizeof(float)); 
00150     memcpy(tri+3, vertex+(3*v1), 3*sizeof(float)); 
00151     memcpy(tri+6, vertex+(3*v2), 3*sizeof(float)); 
00152     memcpy(nrm  , normal+(3*v0), 3*sizeof(float)); 
00153     memcpy(nrm+3, normal+(3*v1), 3*sizeof(float)); 
00154     memcpy(nrm+6, normal+(3*v2), 3*sizeof(float)); 
00155   }
00156 
00157   // set the result array pointers
00158   *nelem = 2*facecount;
00159   *data = msms->graphics;
00160 
00161   // delete work area storage
00162   delete [] normal;
00163   delete [] vertex;
00164 
00165   return MOLFILE_SUCCESS;
00166 }
00167 
00168 
00169 static void close_file_read(void *v) {
00170   msms_t *msms = (msms_t *)v;
00171   fclose(msms->ffd);
00172   fclose(msms->vfd);
00173   delete [] msms->graphics;
00174   delete msms;
00175 }
00176 
00177 
00178 /*
00179  * Initialization stuff here
00180  */
00181 static molfile_plugin_t plugin = {
00182   vmdplugin_ABIVERSION,               // ABI version
00183   MOLFILE_PLUGIN_TYPE,                // type of plugin
00184   "msms",                             // short name of plugin
00185   "MSMS Surface Mesh",                // pretty name of plugin
00186   "John Stone",                       // author
00187   0,                                  // major version
00188   2,                                  // minor version
00189   VMDPLUGIN_THREADSAFE,               // is reentrant
00190   "face,vert"                         // filename extensions 
00191 };
00192 
00193 VMDPLUGIN_API int VMDPLUGIN_init(void) { return VMDPLUGIN_SUCCESS; }
00194 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00195 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00196   plugin.open_file_read = open_file_read;
00197   plugin.read_rawgraphics = read_rawgraphics;
00198   plugin.close_file_read = close_file_read;
00199   (*cb)(v, (vmdplugin_t *)&plugin);
00200   return VMDPLUGIN_SUCCESS;
00201 }
00202 
00203 

Generated on Wed Mar 22 13:15:30 2006 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002