HG-MD  1
vrml.h
Go to the documentation of this file.
00001 /* Author: Vitaliy Ogarko, vogarko@gmail.com, 2009 */
00002 
00003 void CylinderTransformer(Vec3D pointA, Vec3D pointB, Mdouble* length, Mdouble* angle, Vec3D* translation, Vec3D* axis)
00004 {
00005 
00006         *length = GetDistance(pointA, pointB);
00007         
00008         // the translation (center of the cylinder) needed is
00009         *translation = (pointA + pointB) / 2.0f;
00010 
00011         //the initial orientation of the bond is in the y axis
00012         Vec3D init = Vec3D(0.0f, 1.0f, 0.0f);
00013         
00014         //the vector needed is the same as that from a to b
00015         Vec3D needed = Vec3D(pointB.X - pointA.X, pointB.Y - pointA.Y, pointB.Z - pointA.Z);
00016         
00017 
00018         Vec3D needed_n, init_n; // normilized
00019 
00020         needed_n        = needed / needed.GetLength();
00021         init_n          = init / init.GetLength();
00022 
00023         //so the angle to rotate the bond by is:
00024         *angle = acos(Dot(needed_n, init_n));
00025 
00026         
00027         //and the axis to rotate by is the cross product of the initial and
00028         //needed vectors - ie the vector orthogonal to them both
00029         Mdouble vx = init.Y * needed.Z - init.Z * needed.Y;
00030         Mdouble vy = init.Z * needed.X - init.X * needed.Z;
00031         Mdouble vz = init.X * needed.Y - init.Y * needed.X;
00032 
00033         *axis = Vec3D(vx, vy, vz);
00034 
00035 }
00036 
00037 // Export packing configuration to the VRML format.
00038 void Export_to_VRML_format2(string fname)
00039 {
00040         ofstream grout(fname.c_str());  
00041 
00042         grout << "\
00043 #VRML V2.0 utf8 \n\n\
00044 Group {\n\
00045   children [\n\
00046     WorldInfo {\n\
00047       title \"Granular Media\"\n\
00048       info [\n\
00049         \"Packing configuration.\"\n\
00050         \"Author: Vitaliy Ogarko; vogarko@gmail.com\"\n\
00051       ]\n\
00052     }\n\
00053     NavigationInfo {\n\
00054       type [ \"EXAMINE\", \"ANY\" ]\n\
00055     }\n\
00056     Background {\n\
00057       skyColor [ 1 1 1 ]\n\
00058     }\n\
00059   ]\n\
00060 }\n\n";
00061 
00062 /*
00063         grout << "\
00064 Shape {\n\
00065     appearance Appearance {\n\
00066         material Material {\n\
00067             emissiveColor 0 0 0\n\
00068         }\n\
00069     }\n\
00070     geometry IndexedLineSet {\n\
00071         coord Coordinate {\n\
00072             point [\n\
00073             # Coordinates around the top of the cube\n\
00074                    0  1.0  1.0,\n\
00075                  1.0  1.0  1.0,\n\
00076                  1.0  1.0    0,\n\
00077                    0  1.0    0,\n\
00078             # Coordinates around the bottom of the cube\n\
00079                    0    0  1.0,\n\
00080                  1.0    0  1.0,\n\
00081                  1.0    0    0,\n\
00082                    0    0    0\n\
00083             ]\n\
00084         }\n\
00085         coordIndex [\n\
00086         # top\n\
00087             0, 1, 2, 3, 0, -1,\n\
00088         # bottom\n\
00089             4, 5, 6, 7, 4, -1,\n\
00090         # vertical edges\n\
00091             0, 4, -1,\n\
00092             1, 5, -1,\n\
00093             2, 6, -1,\n\
00094             3, 7\n\
00095         ]\n\
00096     }\n\
00097 }\n\n";
00098 */
00099 
00100 
00102 
00103         Vec3D translation, axis;
00104         Mdouble length, angle;
00105 
00106         Vec3D pointsA[12];
00107         Vec3D pointsB[12];
00108 
00109         pointsA[0] = Vec3D(0,0,0); pointsB[0] = Vec3D(1,0,0);
00110         pointsA[1] = Vec3D(0,0,0); pointsB[1] = Vec3D(0,1,0);
00111         pointsA[2] = Vec3D(0,0,0); pointsB[2] = Vec3D(0,0,1);
00112 
00113         pointsA[3] = Vec3D(1,0,0); pointsB[3] = Vec3D(1,0,1);
00114         pointsA[4] = Vec3D(1,0,0); pointsB[4] = Vec3D(1,1,0);
00115 
00116         pointsA[5] = Vec3D(0,1,0); pointsB[5] = Vec3D(0,1,1);
00117         pointsA[6] = Vec3D(0,1,0); pointsB[6] = Vec3D(1,1,0);
00118 
00119         pointsA[7] = Vec3D(0,0,1); pointsB[7] = Vec3D(0,1,1);
00120         pointsA[8] = Vec3D(0,0,1); pointsB[8] = Vec3D(1,0,1);
00121 
00122         pointsA[9]  = Vec3D(1,1,1); pointsB[9]  = Vec3D(0,1,1);
00123         pointsA[10] = Vec3D(1,1,1); pointsB[10] = Vec3D(1,1,0);
00124         pointsA[11] = Vec3D(1,1,1); pointsB[11] = Vec3D(1,0,1);
00125 
00126 
00127         for(int i=0; i<12; i++)
00128         {
00129 
00130                 CylinderTransformer(pointsA[i], pointsB[i], &length, &angle, &translation, &axis);
00131 
00132                 if (i == 11) { angle = 0.; } // without this a stick becomes black colored
00133 
00134                 grout << "\
00135 Transform{\n\
00136     translation " << translation.X << " " << translation.Y << " " << translation.Z << "\n\
00137     rotation " << axis.X << " " << axis.Y << " " << axis.Z << " " << angle << "\n\
00138         children  Shape {\n\
00139         appearance Appearance {\n\
00140             material Material { diffuseColor 0.8 0.8 0.8 }\n\
00141         }\n\
00142         geometry Cylinder {\n\
00143             radius 0.005\n\
00144             height " << length << "\n\
00145         }}}\n\n";
00146 
00147 
00148         } // for
00149 
00150 
00151         grout << "\
00152 Transform {\n\
00153         translation 0 0 0\n\
00154         children Shape {\n\
00155                 geometry Sphere {\n\
00156                 radius 0.005\n\
00157                 }\n\
00158                 appearance Appearance {\n\
00159                         material Material { diffuseColor 0.8 0.8 0.8\n\
00160 }}}}\n\n";
00161 
00162         grout << "\
00163 Transform {\n\
00164         translation 1 0 0\n\
00165         children Shape {\n\
00166                 geometry Sphere {\n\
00167                 radius 0.005\n\
00168                 }\n\
00169                 appearance Appearance {\n\
00170                         material Material { diffuseColor 0.8 0.8 0.8\n\
00171 }}}}\n\n";
00172 
00173         grout << "\
00174 Transform {\n\
00175         translation 0 1 0\n\
00176         children Shape {\n\
00177                 geometry Sphere {\n\
00178                 radius 0.005\n\
00179                 }\n\
00180                 appearance Appearance {\n\
00181                         material Material { diffuseColor 0.8 0.8 0.8\n\
00182 }}}}\n\n";
00183 
00184         grout << "\
00185 Transform {\n\
00186         translation 0 0 1\n\
00187         children Shape {\n\
00188                 geometry Sphere {\n\
00189                 radius 0.005\n\
00190                 }\n\
00191                 appearance Appearance {\n\
00192                         material Material { diffuseColor 0.8 0.8 0.8\n\
00193 }}}}\n\n";
00194 
00195         grout << "\
00196 Transform {\n\
00197         translation 1 1 0\n\
00198         children Shape {\n\
00199                 geometry Sphere {\n\
00200                 radius 0.005\n\
00201                 }\n\
00202                 appearance Appearance {\n\
00203                         material Material { diffuseColor 0.8 0.8 0.8\n\
00204 }}}}\n\n";
00205 
00206         grout << "\
00207 Transform {\n\
00208         translation 1 0 1\n\
00209         children Shape {\n\
00210                 geometry Sphere {\n\
00211                 radius 0.005\n\
00212                 }\n\
00213                 appearance Appearance {\n\
00214                         material Material { diffuseColor 0.8 0.8 0.8\n\
00215 }}}}\n\n";
00216 
00217         grout << "\
00218 Transform {\n\
00219         translation 0 1 1\n\
00220         children Shape {\n\
00221                 geometry Sphere {\n\
00222                 radius 0.005\n\
00223                 }\n\
00224                 appearance Appearance {\n\
00225                         material Material { diffuseColor 0.8 0.8 0.8\n\
00226 }}}}\n\n";
00227 
00228         grout << "\
00229 Transform {\n\
00230         translation 1 1 1\n\
00231         children Shape {\n\
00232                 geometry Sphere {\n\
00233                 radius 0.005\n\
00234                 }\n\
00235                 appearance Appearance {\n\
00236                         material Material { diffuseColor 0.8 0.8 0.8\n\
00237 }}}}\n\n";
00238 
00240 
00241 
00242 
00243 // http://vrmlworks.crispen.org/orient.html >> calculate oriantation for a viewpoint
00244 
00245         grout << "\
00246 Viewpoint {\n\
00247          position    -0.9 1.6 3\n\
00248          orientation -0.572612659264449 -0.806095467134217 -0.14941499361097152 0.6261167634082665\n\
00249          fieldOfView 0.785398\n\
00250          description \"Viewpoint 1\"\n\
00251 }\n\n";
00252 
00253 
00254         Mdouble r, c1, c2, c3;
00255 
00256         for(int i=0; i<Nmax; i++)
00257         {
00258         
00259                 grout << "Transform {" << "\n";
00260                 grout << "translation ";
00261                 
00262                 //grout << Particles[i]->Position << "\n";
00263                 grout << Px[i] << ' ' << Py[i] << ' ' << Pz[i] << "\n";
00264 
00265                 grout << "children Shape {" << "\n";
00266                 grout << "geometry Sphere {" << "\n";
00267                 grout << "radius ";
00268 
00269                 //grout << Particles[i]->Radius << "\n";
00270                 grout << Pr[i] << "\n";
00271 
00272                 grout << "} \n";
00273 
00274 
00275                 grout << "appearance Appearance {" << "\n";
00276 
00277                 //grout << "material Material { diffuseColor 0.915064 0 0.0849365 }" << "\n";
00278                 grout << "material Material { diffuseColor ";
00279                 
00280                 
00281                 //Mdouble r = (Particles[i]->Radius - MinRadius) / (MaxRadius - MinRadius);
00282 
00283                 Mdouble transparency = 0.2;
00284 
00285                 if (PolyType == 1)
00286                 {
00287                         c1 = 0.5;
00288                         c2 = 0.9;
00289                         c3 = 0.9;
00290                 } else
00291                 if (PolyType == 2)
00292                 {
00293                         // small particle
00294                         if (Pr[i] == MinRadius)
00295                         {
00296                                 c1 = 0.5;
00297                                 c2 = 0.9;
00298                                 c3 = 0.9;
00299                         } else
00300                         {
00301                                 c1 = 0.9;
00302                                 c2 = 0.1;
00303                                 c3 = 0.9;
00304                         }
00305                 } else
00306                 {
00307                 
00308                         r = (Pr[i] - MinRadius) / (MaxRadius - MinRadius);
00309 
00310                         c3 = r;
00311                         c2 = 4*r*(1-r);
00312                         c1 = 1-r;
00313 
00314 
00315                         // !!! (Big - blue, small - yellow)
00316                         c3 = r;
00317                         c1 = 1-r;
00318                         c2 = 1-r;
00319 
00320                         transparency = 0;
00321 
00322                 }
00323                 
00324 
00325                 /*
00326                 Mdouble r = pow(GetDistance(Particles[i]->Position, Vec3D(0.5, 0.5, 0.5)), 2./3.) / sqrt(0.75);
00327                 
00328                 Mdouble c3 = r;
00329                 Mdouble c2 = 4*r*(1-r);
00330                 Mdouble c1 = 1-r;
00331                 */
00332 
00333                 //grout << c1 << " " << c2 << " " << c3 << " }\n";
00334                 grout << c1 << " " << c2 << " " << c3 << "\n";
00335 
00336                 grout << "      transparency " << transparency << "\n}\n";
00337 
00338                 grout << "} \n";
00339                 grout << "} \n";
00340                 grout << "} \n";
00341                 grout << "\n";
00342 
00343         } 
00344 
00345 
00346         grout.close();
00347 
00348 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines