cgma
raySurfaceInt.cpp
Go to the documentation of this file.
00001 
00007 #include <iostream>
00008 
00009 #include "Body.hpp"
00010 #include "CubitCompat.hpp"
00011 #include "DLIList.hpp"
00012 #include "GeometryModifyTool.hpp"
00013 #include "GeometryQueryTool.hpp"
00014 #include "InitCGMA.hpp"
00015 #include "RefEntity.hpp"
00016 #include "RefFace.hpp"
00017 #include "RefVolume.hpp"
00018 #include "Surface.hpp"
00019 
00020 #ifdef HAVE_OCC
00021 #define FILE_EXT "stp" 
00022 #define GEO_TYPE "STEP"
00023 #endif
00024 
00025 #define STRINGIFY(S) XSTRINGIFY(S)
00026 #define XSTRINGIFY(S) #S
00027 
00028 bool testDirectedProjection();
00029 bool testPieceOfTorusFromFile();
00030 
00031 int main (int argc, char **argv)
00032 {
00033   CubitStatus status = InitCGMA::initialize_cgma();
00034   if (CUBIT_SUCCESS != status)
00035   {
00036     return 1;
00037   }
00038 
00039   if (!testDirectedProjection())
00040   {
00041     return 2;
00042   }
00043 
00044   std::cout << std::endl;
00045   if (!testPieceOfTorusFromFile())
00046   {
00047     return 3;
00048   }
00049 
00050   return 0;
00051 }
00052 
00053 bool testDirectedProjection()
00054 {
00055   GeometryQueryTool* gqt = GeometryQueryTool::instance();
00056   GeometryModifyTool* gmt = GeometryModifyTool::instance();
00057 
00058   // create a sphere
00059   Body* sphereBody = gmt->sphere(5.0);
00060   std::cout << "Created sphere of radius 5 centered at (0, 0, 0)."
00061       << std::endl;
00062 
00063   // extract the surface of the sphere
00064   DLIList<RefEntity*> sphereChildren;
00065   sphereBody->get_child_ref_entities(sphereChildren);
00066   RefVolume* sphereVol = dynamic_cast<RefVolume*>(sphereChildren.get());
00067   if (!sphereVol)
00068   {
00069     std::cout << "The child of the sphere body is not a RefVolume."
00070         << std::endl;
00071     return false;
00072   }
00073   sphereVol->get_child_ref_entities(sphereChildren);
00074   RefFace* sphereFace = dynamic_cast<RefFace*>(sphereChildren.get());
00075   if (!sphereFace)
00076   {
00077     std::cout << "The child of the sphere volume is not a RefFace."
00078         << std::endl;
00079     return false;
00080   }
00081   Surface* sphere = sphereFace->get_surface_ptr();
00082 
00083   // test firing ray directly down at the north pole of the sphere
00084   CubitVector raySource01(0, 0, 10);
00085   CubitVector rayDir01(0, 0, -2); // NOTE: not a unit vector
00086   CubitVector rayHit01;
00087   CubitStatus rayResult01 =
00088       sphere->closest_point_along_vector(raySource01, rayDir01, rayHit01);
00089   if (rayResult01 != CUBIT_SUCCESS)
00090   {
00091     std::cout << "FAIL: Fire from (0, 0, 10) in direction (0, 0, -1) "
00092         << "did not produce CUBIT_SUCCESS." << std::endl;
00093     return false;
00094   }
00095   std::cout << "Fire from (0, 0, 10) in direction (0, 0, -1) hits at ("
00096       << rayHit01.x() << ", " << rayHit01.y() << ", "
00097       << rayHit01.z() << ")." << std::endl;
00098   if ((fabs(rayHit01.x() - 0) + fabs(rayHit01.y() - 0) +
00099       fabs(rayHit01.z() - 5)) > 1e-12)
00100   {
00101     std::cout << "FAIL: The ray should hit at (0, 0, 5)." << std::endl;
00102     return false;
00103   }
00104 
00105   // test firing ray from above the north pole at an angle that should
00106   // hit the sphere where x = 2.5 and y = 0.
00107   CubitVector raySource02(0, 0, 10);
00108   CubitVector rayDir02(0.4034491106775367, 0, -0.9150020847481741);
00109   CubitVector rayHit02;
00110   CubitStatus rayResult02 =
00111       sphere->closest_point_along_vector(raySource02, rayDir02, rayHit02);
00112   if (rayResult02 != CUBIT_SUCCESS)
00113   {
00114     std::cout << "FAIL: Fire from (0, 0, 10)\n  in direction "
00115         << "(0.4034491106775367, 0, -0.9150020847481741)\n  "
00116         << "did not produce CUBIT_SUCCESS." << std::endl;
00117     return false;
00118   }
00119   std::cout << "Fire from (0, 0, 10)\n  in direction "
00120       << "(0.4034491106775367, 0, -0.9150020847481741)\n  hits at ("
00121       << rayHit02.x() << ", " << rayHit02.y() << ", "
00122       << rayHit02.z() << ")." << std::endl;
00123   if ((fabs(rayHit02.x() - 2.5) + fabs(rayHit02.y() - 0) +
00124       fabs(rayHit02.z() - 4.330127018922193)) > 1e-12)
00125   {
00126     std::cout << "FAIL: The ray should hit at approximately "
00127         << "(2.5, 0, 4.330127018922193)." << std::endl;
00128     return false;
00129   }
00130 
00131   // test firing ray directly down from the center of the sphere
00132   CubitVector raySource03(0, 0, 0);
00133   CubitVector rayDir03(0, 0, -1);
00134   CubitVector rayHit03;
00135   CubitStatus rayResult03 =
00136       sphere->closest_point_along_vector(raySource03, rayDir03, rayHit03);
00137   if (rayResult03 != CUBIT_SUCCESS)
00138   {
00139     std::cout << "FAIL: Fire from (0, 0, 0) in direction (0, 0, -1) "
00140         << "did not produce CUBIT_SUCCESS." << std::endl;
00141     return false;
00142   }
00143   std::cout << "Fire from (0, 0, 0) in direction (0, 0, -1) hits at ("
00144       << rayHit03.x() << ", " << rayHit03.y() << ", "
00145       << rayHit03.z() << ")." << std::endl;
00146   if ((fabs(rayHit03.x() - 0) + fabs(rayHit03.y() - 0) +
00147       fabs(rayHit03.z() + 5)) > 1e-12)
00148   {
00149     std::cout << "FAIL: The ray should hit at (0, 0, -5)." << std::endl;
00150     return false;
00151   }
00152   std::cout << std::endl;
00153 
00154   // delete the sphere body
00155   gqt->delete_Body(sphereBody);
00156 
00157 
00158   // create a torus
00159   Body* torusBody = gmt->torus(3.0, 1.0);
00160   std::cout << "Created torus of major radius 3 and minor radius 1 "
00161       << "centered at (0, 0, 0)\n  with normal along the z-axis."
00162       << std::endl;
00163 
00164   // extract the surface of the torus
00165   DLIList<RefEntity*> torusChildren;
00166   torusBody->get_child_ref_entities(torusChildren);
00167   RefVolume* torusVol = dynamic_cast<RefVolume*>(torusChildren.get());
00168   if (!torusVol)
00169   {
00170     std::cout << "The child of the torus body is not a RefVolume."
00171         << std::endl;
00172     return false;
00173   }
00174   torusVol->get_child_ref_entities(torusChildren);
00175   RefFace* torusFace = dynamic_cast<RefFace*>(torusChildren.get());
00176   if (!torusFace)
00177   {
00178     std::cout << "The child of the torus volume is not a RefFace."
00179         << std::endl;
00180     return false;
00181   }
00182   Surface* torus = torusFace->get_surface_ptr();
00183 
00184   // test firing ray directly down from (0, 3.5, 1)
00185   CubitVector raySource04(0, 3.5, 1);
00186   CubitVector rayDir04(0, 0, -1);
00187   CubitVector rayHit04;
00188   CubitStatus rayResult04 =
00189       torus->closest_point_along_vector(raySource04, rayDir04, rayHit04);
00190   if (rayResult04 != CUBIT_SUCCESS)
00191   {
00192     std::cout << "FAIL: Fire from (0, 3.5, 1) in direction (0, 0, -1) "
00193         << "did not produce CUBIT_SUCCESS." << std::endl;
00194     return false;
00195   }
00196   std::cout << "Fire from (0, 3.5, 1) in direction (0, 0, -1)\n  hits at ("
00197       << rayHit04.x() << ", " << rayHit04.y() << ", "
00198       << rayHit04.z() << ")." << std::endl;
00199   if ((fabs(rayHit04.x() - 0) + fabs(rayHit04.y() - 3.5) +
00200       fabs(rayHit04.z() - 0.8660254037844386)) > 1e-12)
00201   {
00202     std::cout << "FAIL: The ray should hit at (0, 3.5, 0.8660254037844386)."
00203         << std::endl;
00204     return false;
00205   }
00206 
00207   // test firing ray directly down from (0, 4.5, 1)
00208   CubitVector raySource05(0, 4.5, 1);
00209   CubitVector rayDir05(0, 0, -1);
00210   CubitVector rayHit05;
00211   CubitStatus rayResult05 =
00212       torus->closest_point_along_vector(raySource05, rayDir05, rayHit05);
00213   if (rayResult05 != CUBIT_FAILURE)
00214   {
00215     std::cout << "FAIL: Fire from (0, 4.5, 1) in direction (0, 0, -1) "
00216         << "did not produce CUBIT_FAILURE." << std::endl;
00217     return false;
00218   }
00219   std::cout << "Fire from (0, 4.5, 1) in direction (0, 0, -1) "
00220       << "did not intersect torus,\n  as expected." << std::endl;
00221 
00222   // test firing ray towards (-1, 1, 0) from (0, 0, 0)
00223   CubitVector raySource06(0, 0, 0);
00224   CubitVector rayDir06(-1, 1, 0);
00225   CubitVector rayHit06;
00226   CubitStatus rayResult06 =
00227       torus->closest_point_along_vector(raySource06, rayDir06, rayHit06);
00228   if (rayResult06 != CUBIT_SUCCESS)
00229   {
00230     std::cout << "FAIL: Fire from (0, 0, 0) in direction (-1, 1, 0) "
00231         << "did not produce CUBIT_SUCCESS." << std::endl;
00232     return false;
00233   }
00234   std::cout << "Fire from (0, 0, 0) in direction (-1, 1, 0)\n  hits at ("
00235       << rayHit06.x() << ", " << rayHit06.y() << ", "
00236       << rayHit06.z() << ")." << std::endl;
00237   if ((fabs(rayHit06.x() + 1.414213562373095) +
00238       fabs(rayHit06.y() - 1.414213562373095) + fabs(rayHit06.z() - 0)) > 1e-12)
00239   {
00240     std::cout << "FAIL: The ray should hit\n  "
00241         << "at (-1.414213562373095, 1.414213562373095, 0)."
00242         << std::endl;
00243     return false;
00244   }
00245 
00246   // delete the torus body
00247   gqt->delete_Body(torusBody);
00248 
00249   return true;
00250 }
00251 
00252 bool testPieceOfTorusFromFile()
00253 {
00254   GeometryQueryTool* gqt = GeometryQueryTool::instance();
00255 
00256   std::string fileName(STRINGIFY(SRCDIR) "/pieceOfTorus01.");
00257   fileName = fileName + FILE_EXT;
00258   CubitStatus readGeoResult =
00259       CubitCompat_import_solid_model(fileName.c_str(), GEO_TYPE);
00260   if (readGeoResult != CUBIT_SUCCESS)
00261   {
00262     std::cout << "FAIL: Attempt to read geometry from " << fileName
00263         << " did not produce CUBIT_SUCCESS." << std::endl;
00264     return false;
00265   }
00266   
00267   // extract the torus sheet body
00268   DLIList<Body*> bodies;
00269   gqt->bodies(bodies);
00270   Body* pieceOfTorusBody = bodies.pop();
00271   std::cout << "Extracted piece of torus body from file" << std::endl;
00272 
00273   // extract the surface of the torus
00274   DLIList<RefEntity*> torusPieceChildren;
00275   pieceOfTorusBody->get_child_ref_entities(torusPieceChildren);
00276   RefVolume* torusPieceVol =
00277       dynamic_cast<RefVolume*>(torusPieceChildren.get());
00278   if (!torusPieceVol)
00279   {
00280     std::cout << "The child of the piece of torus body is not a RefVolume."
00281         << std::endl;
00282     return false;
00283   }
00284   torusPieceVol->get_child_ref_entities(torusPieceChildren);
00285   RefFace* torusPieceFace = dynamic_cast<RefFace*>(torusPieceChildren.get());
00286   if (!torusPieceFace)
00287   {
00288     std::cout << "The child of the piece of torus volume is not a RefFace."
00289         << std::endl;
00290     return false;
00291   }
00292   Surface* torusPiece = torusPieceFace->get_surface_ptr();
00293 
00294   // test firing ray in the direction (0.707107, 0, -0.707107) from
00295   // (3.17919, 0.1, 1.07919)
00296   CubitVector raySource01(3.17919, 0.1, 1.07919);
00297   CubitVector rayDir01(0.707107, 0, -0.707107);
00298   CubitVector rayHit01;
00299   CubitStatus rayResult01 =
00300       torusPiece->closest_point_along_vector(raySource01, rayDir01, rayHit01);
00301   if (rayResult01 != CUBIT_SUCCESS)
00302   {
00303     std::cout << "FAIL: Fire from (3.17919, 0.1, 1.07919) "
00304         << "in direction (0.707107, 0, -0.707107) "
00305         << "did not produce CUBIT_SUCCESS." << std::endl;
00306     return false;
00307   }
00308   std::cout << "Fire from (3.17919, 0.1, 1.07919) "
00309       << "in direction (0.707107, 0, -0.707107)\n  hits at ("
00310       << rayHit01.x() << ", " << rayHit01.y() << ", "
00311       << rayHit01.z() << ")." << std::endl;
00312   if ((fabs(rayHit01.x() - 3.30723) +
00313       fabs(rayHit01.y() - 0.1) + fabs(rayHit01.z() - 0.951148)) > 1e-4)
00314   {
00315     std::cout << "FAIL: The ray should hit\n  "
00316         << "at (3.30723, 0.1, 0.951148)."
00317         << std::endl;
00318     return false;
00319   }
00320 
00321   return true;
00322 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines