00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <ctype.h>
00030 #include <math.h>
00031 #include <string.h>
00032
00033 #include "molfile_plugin.h"
00034
00035 #define LINESIZE 85
00036
00037 typedef struct {
00038 FILE *fd;
00039 int nsets;
00040 molfile_volumetric_t *vol;
00041 } gridmap_t;
00042
00043
00044
00045 static char *mapgets(char *s, int n, FILE *stream) {
00046 char *returnVal;
00047
00048 if (feof(stream)) {
00049 fprintf(stderr, "mapplugin) Unexpected end-of-file.\n");
00050 returnVal = NULL;
00051 }
00052 else if (ferror(stream)) {
00053 fprintf(stderr, "mapplugin) Error reading file.\n");
00054 return NULL;
00055 }
00056 else {
00057 returnVal = fgets(s, n, stream);
00058 if (returnVal == NULL) {
00059 fprintf(stderr, "mapplugin) Error reading line.\n");
00060 }
00061 }
00062
00063 return returnVal;
00064 }
00065
00066
00067 static void *open_map_read(const char *filepath, const char *filetype,
00068 int *natoms) {
00069 FILE *fd;
00070 gridmap_t *map;
00071 char inbuf[LINESIZE];
00072
00073 float spacing, midX, midY, midZ;
00074 int xsize, ysize, zsize;
00075
00076 fd = fopen(filepath, "rb");
00077 if (!fd) {
00078 fprintf(stderr, "mapplugin) Error opening file.\n");
00079 return NULL;
00080 }
00081
00082
00083 if (mapgets(inbuf, LINESIZE, fd) == NULL)
00084 return NULL;
00085 if (mapgets(inbuf, LINESIZE, fd) == NULL)
00086 return NULL;
00087 if (mapgets(inbuf, LINESIZE, fd) == NULL)
00088 return NULL;
00089
00090
00091 if (mapgets(inbuf, LINESIZE, fd) == NULL)
00092 return NULL;
00093 if (sscanf(inbuf, "SPACING %f", &spacing) != 1)
00094 return NULL;
00095
00096
00097 if (mapgets(inbuf, LINESIZE, fd) == NULL)
00098 return NULL;
00099 if (sscanf(inbuf, "NELEMENTS %d %d %d", &xsize, &ysize, &zsize) != 3) {
00100 fprintf(stderr, "mapplugin) Cannot read NELEMENTS.\n");
00101 return NULL;
00102 }
00103
00104
00105 xsize++;
00106 ysize++;
00107 zsize++;
00108
00109
00110 if (mapgets(inbuf, LINESIZE, fd) == NULL)
00111 return NULL;
00112 if (sscanf(inbuf, "CENTER %f %f %f", &midX, &midY, &midZ) != 3)
00113 return NULL;
00114
00115
00116 map = new gridmap_t;
00117 map->fd = fd;
00118 map->vol = NULL;
00119 *natoms = MOLFILE_NUMATOMS_NONE;
00120 map->nsets = 1;
00121
00122 map->vol = new molfile_volumetric_t[1];
00123 strcpy(map->vol[0].dataname, "Grid Map File");
00124
00125
00126 map->vol[0].origin[0] = -0.5*(xsize+1.0)* spacing + midX;
00127 map->vol[0].origin[1] = -0.5*(ysize+1.0)* spacing + midY;
00128 map->vol[0].origin[2] = -0.5*(zsize+1.0)* spacing + midZ;
00129
00130 map->vol[0].xaxis[0] = xsize * spacing;
00131 map->vol[0].xaxis[1] = 0;
00132 map->vol[0].xaxis[2] = 0;
00133
00134 map->vol[0].yaxis[0] = 0;
00135 map->vol[0].yaxis[1] = ysize * spacing;
00136 map->vol[0].yaxis[2] = 0;
00137
00138 map->vol[0].zaxis[0] = 0;
00139 map->vol[0].zaxis[1] = 0;
00140 map->vol[0].zaxis[2] = zsize * spacing;
00141
00142 map->vol[0].xsize = xsize;
00143 map->vol[0].ysize = ysize;
00144 map->vol[0].zsize = zsize;
00145
00146 map->vol[0].has_color = 0;
00147
00148 return map;
00149 }
00150
00151 static int read_map_metadata(void *v, int *nsets,
00152 molfile_volumetric_t **metadata) {
00153 gridmap_t *map = (gridmap_t *)v;
00154 *nsets = map->nsets;
00155 *metadata = map->vol;
00156
00157 return MOLFILE_SUCCESS;
00158 }
00159
00160 static int read_map_data(void *v, int set, float *datablock,
00161 float *colorblock) {
00162 gridmap_t *map = (gridmap_t *)v;
00163 FILE *fd = map->fd;
00164 float *cellIndex;
00165 char inbuf[LINESIZE];
00166 int count, ndata;
00167
00168 cellIndex = datablock;
00169 count = 0;
00170 ndata = map->vol[0].xsize * map->vol[0].ysize * map->vol[0].zsize;
00171
00172
00173 while (count < ndata) {
00174 if (mapgets(inbuf, LINESIZE, fd) == NULL) {
00175 return MOLFILE_ERROR;
00176 }
00177
00178 *cellIndex = atof(inbuf);
00179
00180 cellIndex++;
00181 count++;
00182 }
00183
00184 return MOLFILE_SUCCESS;
00185 }
00186
00187 static void close_map_read(void *v) {
00188 gridmap_t *map = (gridmap_t *)v;
00189
00190 fclose(map->fd);
00191 if (map->vol != NULL)
00192 delete [] map->vol;
00193 delete map;
00194 }
00195
00196
00197
00198
00199 static molfile_plugin_t plugin;
00200
00201 VMDPLUGIN_API int VMDPLUGIN_init(void) {
00202 memset(&plugin, 0, sizeof(molfile_plugin_t));
00203 plugin.abiversion = vmdplugin_ABIVERSION;
00204 plugin.type = MOLFILE_PLUGIN_TYPE;
00205 plugin.name = "map";
00206 plugin.prettyname = "Autodock Grid Map";
00207 plugin.author = "Eamon Caddigan";
00208 plugin.majorv = 0;
00209 plugin.minorv = 6;
00210 plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00211 plugin.filename_extension = "map";
00212 plugin.open_file_read = open_map_read;
00213 plugin.read_volumetric_metadata = read_map_metadata;
00214 plugin.read_volumetric_data = read_map_data;
00215 plugin.close_file_read = close_map_read;
00216 return VMDPLUGIN_SUCCESS;
00217 }
00218
00219 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00220 (*cb)(v, (vmdplugin_t *)&plugin);
00221 return VMDPLUGIN_SUCCESS;
00222 }
00223
00224 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00225