$search
00001 /* 00002 James R. Diebel 00003 Stanford University 00004 00005 Started: 30 August 2004 00006 Last revised: 31 Sugust 2004 00007 00008 genmesh.cc - program to generate synthetic meshes 00009 00010 Depends on: 00011 - Mesh class (mesh.hh) 00012 -- Vec3d class (vec3d.hh) 00013 -- Vert class (vert.hh) 00014 -- Edge class (edge.hh) 00015 -- Face class (face.hh) 00016 - Timer class (timer.hh) 00017 */ 00018 00019 #include <iostream> 00020 #include <fstream> 00021 #include <stdlib.h> 00022 #include <time.h> 00023 #include "bmtk/mesh.hh" 00024 #include "bmtk/timer.hh" 00025 #include "bmtk/uspline.hh" 00026 00027 using namespace std; 00028 using namespace bmtk; 00029 00030 // prototypes 00031 float grand(float mean, float std); 00032 Mesh* genPlane(int n, float s2); 00033 Mesh* genCorner(int n, float s2); 00034 00035 int main (int argc, char *argv[]) { 00036 // variable declarations 00037 int type; 00038 int paramCount[] = {2,2}; 00039 float* params; 00040 float temp; 00041 Mesh* m; 00042 00043 // if bad command line args, give usage instructions and exit 00044 if (argc < 3) { 00045 cout << endl 00046 << "Usage: genmesh <parameter file> <output.msh>" << endl; 00047 exit(1); 00048 } 00049 00050 // seed random number generator 00051 srand(int(clock())); 00052 00053 // read parameter file 00054 ifstream fin(argv[1]); 00055 if (!fin) exit(1); 00056 fin >> type; 00057 params = new float[paramCount[type]]; 00058 for (int i=0;i<paramCount[type];i++) {fin >> temp; params[i] = temp;} 00059 00060 // generate mesh by calling appropriate function 00061 switch(type) { 00062 case 0: m = genPlane(int(params[0]),params[1]); break; 00063 case 1: m = genCorner(int(params[0]),params[1]); break; 00064 default: 00065 cout << "Mesh type " << type << " is not defined, exiting..." << endl; 00066 exit(1); 00067 } 00068 00069 delete params; 00070 00071 // save mesh to output file 00072 m->writeMesh(argv[2]); 00073 } 00074 00075 float grand(float mean, float std) { 00076 const double norm = 1.0 / (RAND_MAX + 1.0); 00077 double u = 1.0 - rand() * norm; /* can't let u == 0 */ 00078 double v = rand() * norm; 00079 double z = sqrt(-2.0 * log(u)) * cos(2.0 * M_PI * v); 00080 return mean + std * z; 00081 } 00082 00083 Mesh* genPlane(int n, float s2) { 00084 cout << "Generating planar square with " << n 00085 << " vertices along each edge" << endl; 00086 Mesh* m = new Mesh(); 00087 00088 // allocate memory for vertex and face lists 00089 float* v = new float[3*n*n]; 00090 int* f = new int[6*(n-1)*(n-1)]; 00091 00092 // create vertex and triangle lists 00093 for (int i=0,k=0,l=0;i<n;i++) { 00094 for (int j=0;j<n;j++,k++) { 00095 // create array of vertices on x-y plane 00096 v[3*k] = float(i); 00097 v[3*k+1] = float(j); 00098 v[3*k+2] = grand(0,s2); 00099 00100 // create faces for current square 00101 if (i<(n-1) && j<(n-1)) { 00102 f[3*l] = k; 00103 f[3*l+1] = k+n; 00104 f[3*l+2] = k+1; 00105 l++; 00106 f[3*l] = k+1; 00107 f[3*l+1] = k+n; 00108 f[3*l+2] = k+n+1; 00109 l++; 00110 } 00111 00112 if (i==n-1 && j==n-1) cout << k << " " << l << endl; 00113 } 00114 } 00115 00116 // generate mesh from those lists 00117 m->buildFrom(v,n*n,f,2*(n-1)*(n-1)); 00118 00119 delete [] v; 00120 delete [] f; 00121 return m; 00122 } 00123 00124 Mesh* genCorner(int n, float s2) { 00125 cout << "Generating simple corner with " << n 00126 << " vertices along each edge" << endl; 00127 Mesh* m = new Mesh(); 00128 00129 // allocate memory for vertex and face lists 00130 int nv = (2*n-1)*n + (n-1)*(n-1); 00131 int nf = 2*(2*n-2)*(n-1) + 2*(n-1)*(n-1); 00132 float* v = new float[3*nv]; 00133 int* f = new int[3*nf]; 00134 00135 // create vertex and triangle lists 00136 int k=0,l=0; 00137 for (int i=0;i<(2*n-1);i++) { 00138 // create walls 00139 for (int j=0;j<n;j++,k++) { 00140 // create array of vertices on x-y plane 00141 if (i<n) { 00142 v[3*k] = float(i); 00143 v[3*k+1] = float(j); 00144 v[3*k+2] = grand(0,s2); 00145 } else { 00146 v[3*k] = float(n-1) + grand(0,s2); 00147 v[3*k+1] = float(j); 00148 v[3*k+2] = float(i-n+1); 00149 } 00150 00151 // create faces for current square 00152 if (i<(2*n-2) && j<(n-1)) { 00153 f[3*l] = k; 00154 f[3*l+1] = k+n; 00155 f[3*l+2] = k+1; 00156 l++; 00157 f[3*l] = k+1; 00158 f[3*l+1] = k+n; 00159 f[3*l+2] = k+n+1; 00160 l++; 00161 } 00162 } 00163 } 00164 00165 // create floor 00166 for (int i=0;i<(n-1);i++) { 00167 for (int j=1;j<n;j++,k++) { 00168 v[3*k] = float(i); 00169 v[3*k+1] = grand(0,s2); 00170 v[3*k+2] = float(j); 00171 00172 if (j==1 && i<(n-2)) { 00173 f[3*l] = k; 00174 f[3*l+1] = k+n-1; 00175 f[3*l+2] = i*n; 00176 l++; 00177 f[3*l] = k+n-1; 00178 f[3*l+1] = (i+1)*n; 00179 f[3*l+2] = i*n; 00180 l++; 00181 } 00182 if (j==1 && i==(n-2)){ 00183 f[3*l] = k; 00184 f[3*l+1] = n*n; 00185 f[3*l+2] = (n-2)*n; 00186 l++; 00187 f[3*l] = n*n; 00188 f[3*l+1] = (n-1)*n; 00189 f[3*l+2] = (n-2)*n; 00190 l++; 00191 } 00192 if (j>1 && i==(n-2)) { 00193 f[3*l] = k; 00194 f[3*l+1] = (n+j-1)*n; 00195 f[3*l+2] = k-1; 00196 l++; 00197 f[3*l] = (n+j-1)*n; 00198 f[3*l+1] = (n+j-2)*n; 00199 f[3*l+2] = k-1; 00200 l++; 00201 } 00202 if (j>1 && i<(n-2)) { 00203 f[3*l] = k; 00204 f[3*l+1] = k+(n-1); 00205 f[3*l+2] = k-1; 00206 l++; 00207 f[3*l] = k+(n-1); 00208 f[3*l+1] = k+(n-1)-1; 00209 f[3*l+2] = k-1; 00210 l++; 00211 } 00212 /*if (i==n-2 && j==n-1) 00213 cout <<"("<<i<<","<<j<<") -> "<<k<<"/"<< nv<<", "<<l<<"/"<<nf<<endl;*/ 00214 } 00215 } 00216 00217 // generate mesh from those lists 00218 m->buildFrom(v,nv,f,nf); 00219 00220 // fix covariances to agree with noise 00221 float big = 100; 00222 k=0,l=0; 00223 for (int i=0;i<(2*n-1);i++) { 00224 for (int j=0;j<n;j++,k++) { 00225 if (i<n) { 00226 Vec3d temp(big,big,0.1); 00227 m->v[k].iCov = Mat3x3(temp); 00228 } else if (i==n) { 00229 Vec3d temp(1,big,1); 00230 m->v[k].iCov = Mat3x3(temp); 00231 } else { 00232 Vec3d temp(0.1,big,big); 00233 m->v[k].iCov = Mat3x3(temp); 00234 } 00235 } 00236 } 00237 00238 for (int i=0;i<(n-1);i++) { 00239 for (int j=1;j<n;j++,k++) { 00240 Vec3d temp(big,0.1,big); 00241 m->v[k].iCov = Mat3x3(temp); 00242 } 00243 } 00244 00245 delete [] v; 00246 delete [] f; 00247 return m; 00248 }