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
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <math.h>
00038 #include <string.h>
00039
00040 #if defined(_AIX)
00041 #include <strings.h>
00042 #endif
00043
00044 #include "molfile_plugin.h"
00045
00046 typedef struct {
00047 FILE *fd;
00048 int nsets;
00049 molfile_volumetric_t *vol;
00050 } biomocca_t;
00051
00052
00053 static void *open_biomocca_read(const char *filepath, const char *filetype,
00054 int *natoms) {
00055 FILE *fd;
00056 biomocca_t *biomocca;
00057 float scale;
00058 int xsize, ysize, zsize;
00059 float orig[3];
00060
00061 fd = fopen(filepath, "r");
00062 if (!fd) {
00063 printf("biomoccaplugin) Error opening file.\n");
00064 return NULL;
00065 }
00066
00067 if (fscanf(fd, "%f %f %f", orig, orig+1, orig+2) != 3) {
00068 printf("biomoccaplugin) Error reading grid origin.\n");
00069 return NULL;
00070 }
00071
00072
00073 if (fscanf(fd, "%d %d %d", &xsize, &ysize, &zsize) != 3) {
00074 printf("biomoccaplugin) Error reading grid dimensions.\n");
00075 return NULL;
00076 }
00077
00078
00079 if (fscanf(fd, "%f", &scale) != 1) {;
00080 printf("biomoccaplugin) Error reading voxel scale.\n");
00081 return NULL;
00082 }
00083
00084
00085 biomocca = new biomocca_t;
00086 biomocca->fd = fd;
00087 biomocca->vol = NULL;
00088 *natoms = MOLFILE_NUMATOMS_NONE;
00089 biomocca->nsets = 1;
00090
00091 biomocca->vol = new molfile_volumetric_t[1];
00092 strcpy(biomocca->vol[0].dataname, "BioMocca map");
00093
00094
00095 for (int i=0; i<3; i++) {
00096 biomocca->vol[0].origin[i] = orig[i];
00097 biomocca->vol[0].xaxis[i] = 0.0;
00098 biomocca->vol[0].yaxis[i] = 0.0;
00099 biomocca->vol[0].zaxis[i] = 0.0;
00100 }
00101
00102 biomocca->vol[0].xaxis[0] = scale * (xsize-1);
00103 biomocca->vol[0].yaxis[1] = scale * (ysize-1);
00104 biomocca->vol[0].zaxis[2] = scale * (zsize-1);
00105
00106 biomocca->vol[0].origin[0] -= 0.5 * biomocca->vol[0].xaxis[0];
00107 biomocca->vol[0].origin[1] -= 0.5 * biomocca->vol[0].yaxis[1];
00108 biomocca->vol[0].origin[2] -= 0.5 * biomocca->vol[0].zaxis[2];
00109
00110 biomocca->vol[0].xsize = xsize;
00111 biomocca->vol[0].ysize = ysize;
00112 biomocca->vol[0].zsize = zsize;
00113
00114 biomocca->vol[0].has_color = 0;
00115
00116 return biomocca;
00117 }
00118
00119 static int read_biomocca_metadata(void *v, int *nsets,
00120 molfile_volumetric_t **metadata) {
00121 biomocca_t *biomocca = (biomocca_t *)v;
00122 *nsets = biomocca->nsets;
00123 *metadata = biomocca->vol;
00124
00125 return MOLFILE_SUCCESS;
00126 }
00127
00128 static int read_biomocca_data(void *v, int set, float *datablock,
00129 float *colorblock) {
00130 biomocca_t *biomocca = (biomocca_t *)v;
00131 FILE *fd = biomocca->fd;
00132 int x, y, z, xsize, ysize, zsize, xysize;
00133
00134 xsize = biomocca->vol[0].xsize;
00135 ysize = biomocca->vol[0].ysize;
00136 zsize = biomocca->vol[0].zsize;
00137 xysize = xsize * ysize;
00138
00139 for (x=0; x<xsize; x++) {
00140 for (y=0; y<ysize; y++) {
00141 for (z=0; z<zsize; z++) {
00142 if (fscanf(fd, "%f", datablock + z*xysize + y*xsize + x) != 1) {
00143 printf("biomoccaplugin) Failed reading biomocca map data\n");
00144 return MOLFILE_ERROR;
00145 }
00146
00147 }
00148 }
00149 }
00150
00151 return MOLFILE_SUCCESS;
00152 }
00153
00154 static void close_biomocca_read(void *v) {
00155 biomocca_t *biomocca = (biomocca_t *)v;
00156
00157 fclose(biomocca->fd);
00158 if (biomocca->vol != NULL)
00159 delete [] biomocca->vol;
00160 delete biomocca;
00161 }
00162
00163
00164
00165
00166 static molfile_plugin_t plugin;
00167
00168 VMDPLUGIN_API int VMDPLUGIN_init(void) {
00169 memset(&plugin, 0, sizeof(molfile_plugin_t));
00170 plugin.abiversion = vmdplugin_ABIVERSION;
00171 plugin.type = MOLFILE_PLUGIN_TYPE;
00172 plugin.name = "biomocca";
00173 plugin.prettyname = "Biomocca Volumetric Map";
00174 plugin.author = "John Stone";
00175 plugin.majorv = 0;
00176 plugin.minorv = 2;
00177 plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00178 plugin.filename_extension = "bmcg";
00179 plugin.open_file_read = open_biomocca_read;
00180 plugin.read_volumetric_metadata = read_biomocca_metadata;
00181 plugin.read_volumetric_data = read_biomocca_data;
00182 plugin.close_file_read = close_biomocca_read;
00183
00184 return VMDPLUGIN_SUCCESS;
00185 }
00186
00187 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00188
00189 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00190 (*cb)(v, (vmdplugin_t *)&plugin);
00191 return VMDPLUGIN_SUCCESS;
00192 }
00193