00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023
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;
00036 FILE *vfd;
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
00049
00050
00051
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
00103 while (fscanf(msms->ffd, "%d %d %d %d %d", &t, &t, &t, &t, &t) == 5)
00104 facecount++;
00105 rewind(msms->ffd);
00106
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
00113 if (facecount < 1 || vertexcount < 3)
00114 return MOLFILE_ERROR;
00115
00116
00117 float *vertex = new float[3 * vertexcount];
00118 float *normal = new float[3 * vertexcount];
00119
00120
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
00130
00131 msms->graphics = new molfile_graphics_t[2*facecount];
00132
00133
00134 for (i=0; i<facecount; i++) {
00135 int v0, v1, v2, surftype, ana;
00136
00137 msms->graphics[2*i ].type = MOLFILE_TRINORM;
00138 msms->graphics[2*i + 1].type = MOLFILE_NORMS;
00139
00140
00141 fscanf(msms->ffd, "%d %d %d %d %d", &v0, &v1, &v2, &surftype, &ana);
00142 v0--;
00143 v1--;
00144 v2--;
00145
00146
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
00158 *nelem = 2*facecount;
00159 *data = msms->graphics;
00160
00161
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
00180
00181 static molfile_plugin_t plugin = {
00182 vmdplugin_ABIVERSION,
00183 MOLFILE_PLUGIN_TYPE,
00184 "msms",
00185 "MSMS Surface Mesh",
00186 "John Stone",
00187 0,
00188 2,
00189 VMDPLUGIN_THREADSAFE,
00190 "face,vert"
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