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

situsplugin.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: situsplugin.C,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.4 $       $Date: 2006/02/23 19:36:45 $
00015  *
00016  ***************************************************************************/
00017 
00018 /* 
00019  * Situs EM map file reader
00020  *   map is a cubic lattice
00021  *   all data is stored in plain ASCII for convenience
00022  *
00023  * Format of the file is:
00024  *   voxel size in Angstroms
00025  *   coordinates of first voxel
00026  *   integer X/Y/Z counts
00027  *   voxels follow in X fastest, Y next fastest, Z slowest
00028  */
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <math.h>
00033 #include <string.h>
00034 
00035 #if defined(_AIX)
00036 #include <strings.h>
00037 #endif
00038 
00039 #include "molfile_plugin.h"
00040 
00041 typedef struct {
00042   FILE *fd;
00043   int nsets;
00044   molfile_volumetric_t *vol;
00045 } situs_t;
00046 
00047 
00048 static void *open_situs_read(const char *filepath, const char *filetype,
00049     int *natoms) {
00050   FILE *fd;
00051   situs_t *situs;
00052   float scale;
00053   int xsize, ysize, zsize;
00054   float orig[3], xdelta[3], ydelta[3], zdelta[3];
00055   
00056   fd = fopen(filepath, "r");
00057   if (!fd) {
00058     fprintf(stderr, "Error opening file.\n");
00059     return NULL;
00060   }
00061 
00062   /* get the scale */
00063   if (fscanf(fd, "%f", &scale) != 1) {;
00064     fprintf(stderr, "Error reading voxel scale.\n");
00065     return NULL;
00066   }
00067 
00068   if (fscanf(fd, "%f %f %f", orig, orig+1, orig+2) != 3) {
00069     fprintf(stderr, "Error reading grid origin.\n");
00070     return NULL;
00071   }
00072 
00073   /* get the number of grid points */
00074   if (fscanf(fd, "%d %d %d", &xsize, &ysize, &zsize) != 3) {
00075     fprintf(stderr, "Error reading grid dimensions.\n");
00076     return NULL;
00077   }
00078 
00079   /* allocate and initialize the situs structure */
00080   situs = new situs_t;
00081   situs->fd = fd;
00082   situs->vol = NULL;
00083   *natoms = MOLFILE_NUMATOMS_NONE;
00084   situs->nsets = 1; /* this file contains only one data set */
00085 
00086   situs->vol = new molfile_volumetric_t[1];
00087   strcpy(situs->vol[0].dataname, "Situs map");
00088 
00089   /* Set the unit cell origin and basis vectors */
00090   for (int i=0; i<3; i++) {
00091     situs->vol[0].origin[i] = orig[i];
00092     situs->vol[0].xaxis[i] = 0.0;
00093     situs->vol[0].yaxis[i] = 0.0;
00094     situs->vol[0].zaxis[i] = 0.0;
00095   }
00096   situs->vol[0].xaxis[0] = scale * (xsize-1);
00097   situs->vol[0].yaxis[1] = scale * (ysize-1);
00098   situs->vol[0].zaxis[2] = scale * (zsize-1);
00099 
00100   situs->vol[0].xsize = xsize;
00101   situs->vol[0].ysize = ysize;
00102   situs->vol[0].zsize = zsize;
00103 
00104   situs->vol[0].has_color = 0; /* Situs maps contain no color information. */
00105 
00106   return situs;
00107 }
00108 
00109 static int read_situs_metadata(void *v, int *nsets, 
00110   molfile_volumetric_t **metadata) {
00111   situs_t *situs = (situs_t *)v;
00112   *nsets = situs->nsets; 
00113   *metadata = situs->vol;  
00114 
00115   return MOLFILE_SUCCESS;
00116 }
00117 
00118 static int read_situs_data(void *v, int set, float *datablock,
00119                          float *colorblock) {
00120   situs_t *situs = (situs_t *)v;
00121   FILE *fd = situs->fd;
00122   float grid[3];
00123   int x, y, z, xsize, ysize, zsize, xysize, count, total, i;
00124 
00125   xsize = situs->vol[0].xsize;
00126   ysize = situs->vol[0].ysize;
00127   zsize = situs->vol[0].zsize;
00128   xysize = xsize * ysize;
00129   total = xysize * zsize;
00130 
00131   /* Read the values from the file */
00132   for (count=0; count<total; count++) {
00133     if (fscanf(fd, "%f", datablock + count) != 1) {
00134       printf("Failed reading situs map data\n");
00135       return MOLFILE_ERROR;
00136     }
00137   }   
00138 
00139   return MOLFILE_SUCCESS;
00140 }
00141 
00142 static void close_situs_read(void *v) {
00143   situs_t *situs = (situs_t *)v;
00144   
00145   fclose(situs->fd);
00146   if (situs->vol != NULL)
00147     delete [] situs->vol; 
00148   delete situs;
00149 }
00150 
00151 /*
00152  * Initialization stuff here
00153  */
00154 static molfile_plugin_t plugin = {
00155   vmdplugin_ABIVERSION,   /* ABI version */
00156   MOLFILE_PLUGIN_TYPE,    /* plugin type */
00157   "situs",                /* file format description */
00158   "Situs Density Map",    /* file format description */
00159   "John Stone",           /* author(s) */
00160   0,                      /* major version */
00161   1,                      /* minor version */
00162   VMDPLUGIN_THREADSAFE,   /* is reentrant */
00163   "situs"                 /* filename extension */
00164 };
00165 
00166 VMDPLUGIN_API int VMDPLUGIN_init(void) { return VMDPLUGIN_SUCCESS; }
00167 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00168 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00169   plugin.open_file_read = open_situs_read;
00170   plugin.read_volumetric_metadata = read_situs_metadata;
00171   plugin.read_volumetric_data = read_situs_data;
00172   plugin.close_file_read = close_situs_read;
00173   (*cb)(v, (vmdplugin_t *)&plugin);
00174   return VMDPLUGIN_SUCCESS;
00175 }
00176 

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