00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <math.h>
00026 #include "Surf.h"
00027 #include "Inform.h"
00028 #include "utilities.h"
00029
00030
00031 #define VMD_FILENAME_MAX 1024
00032
00033 Surf::Surf() {}
00034
00035 int Surf::compute(float probe_r, int num, float *r,
00036 float *x, float *y, float *z) {
00037 FILE *outfile;
00038 FILE *infile;
00039 char *surfbin;
00040 char *surfcmd;
00041 static int surf_firsttime = 1;
00042
00043 if ((surfbin = getenv("SURF_BIN")) == NULL) {
00044 msgErr << "No SURF binary found; set the SURF_BIN environment variable"
00045 << sendmsg;
00046 msgErr << "to the location of your SURF executable." << sendmsg;
00047 return 0;
00048 }
00049
00050
00051
00052
00053
00054 char *dirname = vmd_tempfile("");
00055 char *ofilename = new char[VMD_FILENAME_MAX];
00056 char *ifilename = new char[VMD_FILENAME_MAX];
00057 int rndnum = (vmd_random() % 999);
00058 sprintf(ofilename, "%svmdsurf.u%d.%d.in", dirname, vmd_getuid(), rndnum);
00059 sprintf(ifilename, "%svmdsurf.u%d.%d.in.tri", dirname, vmd_getuid(), rndnum);
00060 delete [] dirname;
00061 vmd_delete_file(ofilename);
00062 vmd_delete_file(ifilename);
00063
00064
00065
00066
00067 if ((outfile=fopen(ofilename, "wt")) == NULL) {
00068 msgErr << "Failed to create Surf atom radii input file" << sendmsg;
00069 if (ofilename) delete [] ofilename;
00070 if (ifilename) delete [] ifilename;
00071 return 0;
00072 }
00073 for (int i=0; i<num; i++) {
00074 fprintf(outfile, "%d %f %f %f %f\n", i, r[i], x[i], y[i], z[i]);
00075 }
00076 fclose(outfile);
00077
00078
00079
00080
00081 if ((surfcmd = new char[strlen(ofilename) + strlen(surfbin) + 82])) {
00082 sprintf(surfcmd, "\"%s\" -W 1 -R %f %s", surfbin, probe_r, ofilename);
00083 vmd_system(surfcmd);
00084 delete [] surfcmd;
00085
00086
00087 if (surf_firsttime == 1) {
00088 surf_firsttime = 0;
00089 msgInfo << "This surface is made with SURF from UNC-Chapel Hill."
00090 << " The reference is:" << sendmsg;
00091 msgInfo << "A. Varshney, F. P. Brooks, W. V. Wright, "
00092 << "Linearly Scalable Computation" << sendmsg;
00093 msgInfo << "of Smooth Molecular Surfaces, "
00094 << "IEEE Comp. Graphics and Applications, " << sendmsg;
00095 msgInfo << "v. 14 (1994) pp. 19-25." << sendmsg;
00096 }
00097 }
00098
00099
00100
00101
00102 if ((infile = fopen(ifilename, "r")) == NULL) {
00103 msgErr << "Cannot read SURF output file: " << ifilename << sendmsg;
00104
00105
00106 vmd_delete_file(ofilename);
00107 vmd_delete_file(ifilename);
00108 if (ofilename) delete [] ofilename;
00109 if (ifilename) delete [] ifilename;
00110 return 0;
00111 }
00112
00113 msgInfo << "Reading Surf geometry output file..." << sendmsg;
00114 numtriangles = 0;
00115 int vertnum = 0;
00116 int atmindex;
00117 float vn[18];
00118
00119
00120
00121 while (fscanf(infile,
00122 "%d%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f",
00123 &atmindex,
00124 vn, vn+1, vn+2, vn+3, vn+4, vn+5,
00125 vn+6, vn+7, vn+8, vn+9,vn+10,vn+11,
00126 vn+12,vn+13,vn+14,vn+15,vn+16,vn+17) == 19) {
00127 if (!tri_degenerate(&vn[0], &vn[3], &vn[6])) {
00128 ind.append(atmindex);
00129
00130 f.append(vertnum);
00131 vertnum++;
00132 f.append(vertnum);
00133 vertnum++;
00134 f.append(vertnum);
00135 vertnum++;
00136 numtriangles++;
00137
00138
00139 v.append3x3(&vn[0], &vn[6], &vn[12]);
00140 n.append3x3(&vn[3], &vn[9], &vn[15]);
00141 }
00142 }
00143 fclose(infile);
00144
00145
00146 vmd_delete_file(ofilename);
00147 vmd_delete_file(ifilename);
00148 if (ifilename) delete [] ifilename;
00149 if (ofilename) delete [] ofilename;
00150
00151 msgInfo << "Read Surf output file, processing geometry..." << sendmsg;
00152
00153 return 1;
00154 }
00155
00156 void Surf::clear() {
00157 numtriangles=0;
00158 v.clear();
00159 n.clear();
00160 f.clear();
00161 ind.clear();
00162 }
00163