MeshKit  1.0
planeProj.cpp
Go to the documentation of this file.
00001 
00011 #include <iostream>
00012 
00013 #include "meshkit/AF2PlaneProjection.hpp"
00014 #include "meshkit/AF2Point2D.hpp"
00015 #include "meshkit/AF2Point3D.hpp"
00016 #include "meshkit/MKCore.hpp"
00017 #include "meshkit/ModelEnt.hpp"
00018 #include "meshkit/Matrix.hpp"
00019 
00020 using namespace MeshKit;
00021 
00022 #include "TestUtil.hpp"
00023 
00024 #if HAVE_OCC
00025 #define FILE_EXT "stp"
00026 double epsError = 1.e-12;
00027 #else
00028 #define FILE_EXT "facet"
00029 double epsError = 1.e-3;
00030 #endif
00031 
00032 MKCore *mk = NULL;
00033 
00034 void test_plane_projection();
00035 bool testPlaneProj();
00036 
00037 int main(int argc, char **argv)
00038 {
00039 
00040   // start up MK and load the geometry
00041   int num_fail = 0;
00042 
00043   num_fail += RUN_TEST(test_plane_projection);
00044 
00045   return num_fail;
00046 
00047 }
00048 
00049 void test_plane_projection()
00050 {
00051   CHECK(testPlaneProj());
00052 }
00053 
00054 bool testPlaneProj()
00055 {
00056   int failCount = 0;
00057 
00058   mk = new MKCore();
00059 
00060   std::string file_name = TestDir + "/pieceOfTorus01." + FILE_EXT;
00061   mk->load_geometry_mesh(file_name.c_str(), file_name.c_str());
00062 
00063   MEntVector surfs;
00064   mk->get_entities_by_dimension(2, surfs);
00065   ModelEnt *this_surf = (*surfs.begin());
00066 
00067   MeshKit::Vector<3> origin;
00068   // point on surface is at approx (2.82842712474619, 2.82842712474619, 0)
00069   // = (4*sin(pi/4), 4*sin(pi/4), 0) = (4*sqrt(2)/2, 4*sqrt(2)/2, 0)
00070   origin[0] = 2.8;
00071   origin[1] = 2.8;
00072   origin[2] = 0.0;
00073   MeshKit::Vector<3> normal;
00074   normal[0] = 0.7071067811865476; // sqrt(2)/2
00075   normal[1] = 0.7071067811865476;
00076   normal[2] = 0.0;
00077   MeshKit::Vector<3> xDir;
00078   xDir[0] = -0.7071067811865476;
00079   xDir[1] = 0.7071067811865476;
00080   xDir[2] = 0.0;
00081   AF2PlaneProjection planeProj(this_surf->igeom_instance(),
00082       this_surf->geom_handle(), origin, normal, xDir, 1.0);
00083   std::cout << "Created plane passing through (2.8, 2.8, 0) "
00084       << "normal to (1, 1, 0)." << std::endl;
00085   std::cout << "The y-coordinate in the plane is in the direction of "
00086       << "the z-axis." << std::endl;
00087 
00088 
00089   // 2.8059735404411 is (sqrt(2)/2) * (3 + cos(asin(0.25)))
00090   AF2Point3D testPnt(0, 2.8059735404411, 2.8059735404411, 0.25);
00091   bool legal=true;
00092   AF2Point2D* planePnt = planeProj.transformFromSurface(testPnt, legal);
00093   std::cout << "Transform (" << testPnt.getX() << ", " << testPnt.getY() << ", "
00094       << testPnt.getZ() << ") from surface to plane." << std::endl;
00095   std::cout << "Coordinates in plane: (" << planePnt->getX() << ", "
00096       << planePnt->getY() << ")" << std::endl;
00097   if ((fabs(planePnt->getX() - 0.0) + fabs(planePnt->getY() - 0.25)) > epsError)
00098   {
00099     std::cout << "FAIL: The coordinates in the plane should be (0, 0.25)."
00100         << std::endl;
00101     ++failCount;
00102   }
00103   delete planePnt;
00104 
00105   // 0.1539268506556547 is sin(acos(2.82 * sqrt(2) - 3))
00106   AF2Point2D planeTestPntA(0, 0.1539268506556547);
00107   AF2Point3D* surfacePnt = planeProj.transformToSurface(planeTestPntA, 1);
00108   std::cout << "Transform (" << planeTestPntA.getX() << ", "
00109       << planeTestPntA.getY() << ") from plane to surface." << std::endl;
00110   std::cout << "Coordinates on surface: (" << surfacePnt->getX() << ", "
00111       << surfacePnt->getY() << ", " << surfacePnt->getZ() << ")" << std::endl;
00112   if ((fabs(surfacePnt->getX() - 2.82) + fabs(surfacePnt->getY() - 2.82) +
00113       fabs(surfacePnt->getZ() - 0.15392685065547)) > epsError)
00114   {
00115     std::cout << "FAIL: The coordinates on the surface should be\n"
00116         << "  (2.82, 2.82, 0.15392685065547)."
00117         << std::endl;
00118     ++failCount;
00119   }
00120   delete surfacePnt;
00121 
00122   // 0.574682269122808 is sin(acos(2.7 * sqrt(2) - 3))
00123   AF2Point2D planeTestPntB(0, 0.574682269122808);
00124   surfacePnt = planeProj.transformToSurface(planeTestPntB, 2);
00125   std::cout << "Transform (" << planeTestPntB.getX() << ", "
00126       << planeTestPntB.getY() << ") from plane to surface." << std::endl;
00127   std::cout << "Coordinates on surface: (" << surfacePnt->getX() << ", "
00128       << surfacePnt->getY() << ", " << surfacePnt->getZ() << ")" << std::endl;
00129   if ((fabs(surfacePnt->getX() - 2.7) + fabs(surfacePnt->getY() - 2.7) +
00130       fabs(surfacePnt->getZ() - 0.574682269122808)) > epsError)
00131   {
00132     std::cout << "FAIL: The coordinates on the surface should be\n"
00133         << "  (2.7, 2.7, 0.574682269122808)."
00134         << std::endl;
00135     ++failCount;
00136   }
00137   delete surfacePnt;
00138 
00139 
00140   // create a plane projection (with a normal that does not agree with the
00141   // normal of any plane tangent to the surface) such that there will
00142   // be points on the surface on both sides of the plane
00143   origin[0] = 3.5;
00144   origin[1] = 0.0;
00145   origin[2] = 0.0;
00146   normal[0] = -0.7071067811865476; // -sqrt(2)/2
00147   normal[1] = 0.0;
00148   normal[2] = 0.7071067811865476;
00149   xDir[0] = 0.7071067811865476;
00150   xDir[1] = 0.0;
00151   xDir[2] = 0.7071067811865476;
00152   AF2PlaneProjection wackyProj(this_surf->igeom_instance(),
00153       this_surf->geom_handle(), origin, normal, xDir, 1.0);
00154   std::cout << "Created plane passing through (3.5, 0, 0) "
00155       << "normal to (-1, 0, 1)." << std::endl;
00156   std::cout << "The y-coordinate in the plane is in the direction of "
00157       << "the y-axis." << std::endl;
00158 
00159   // test projecting a point from this plane onto the surface
00160   // given x = 3.95, y = 0.1,
00161   // z = sqrt(-8 + 6 * sqrt (x * x + y * y) - x * x - y * y)
00162   // ~ 0.3083726968400695 is the coordinate of the point on the surface
00163   // this projects to roughly (3.87918634842, 0.1, 0.37918634842) as a
00164   // 3-dimensional pooint on the plane, computing z-coordinate there as
00165   // z + (0.45 - z)/2.  Thus 2-dimensional planar coordinates are as
00166   // follows, with the first coordinat as 3-d z-coord divided by sqrt(2)/2
00167   AF2Point2D planeTestPntC(0.53625047660234298437463798744018, 0.1);
00168   surfacePnt = wackyProj.transformToSurface(planeTestPntC, 3);
00169   std::cout << "Transform (" << planeTestPntC.getX() << ", "
00170       << planeTestPntC.getY() << ") from plane to surface." << std::endl;
00171   std::cout << "Coordinates on surface: (" << surfacePnt->getX() << ", "
00172       << surfacePnt->getY() << ", " << surfacePnt->getZ() << ")" << std::endl;
00173   // expect ~ (3.95, 0.1, 0.3083726968400695)
00174   if ((fabs(surfacePnt->getX() - 3.95) + fabs(surfacePnt->getY() - 0.1) +
00175       fabs(surfacePnt->getZ() - 0.3083726968400695)) > epsError)
00176   {
00177     std::cout << "FAIL: The coordinates on the surface should be\n"
00178         << "  (3.95, 0.1, 0.3083726968400695)."
00179         << std::endl;
00180     ++failCount;
00181   }
00182   // The line also intersects the surface near (3.30723, 0.1, 0.951148),
00183   // but that point is farther away from the plane than the expected result.
00184   delete surfacePnt;
00185 
00186 
00187   // Perform a similar test with the plane origin at x = 2.6
00188   origin[0] = 2.6;
00189   AF2PlaneProjection wackyProjShift01(this_surf->igeom_instance(),
00190       this_surf->geom_handle(), origin, normal, xDir, 1.0);
00191   std::cout << "Created plane passing through (2.6, 0, 0) "
00192       << "normal to (-1, 0, 1)." << std::endl;
00193   std::cout << "The y-coordinate in the plane is in the direction of "
00194       << "the y-axis." << std::endl;
00195 
00196   AF2Point2D planeTestPntD(1.17264657967023277, 0.1);
00197   surfacePnt = wackyProjShift01.transformToSurface(planeTestPntD, 4);
00198   std::cout << "Transform (" << planeTestPntD.getX() << ", "
00199       << planeTestPntD.getY() << ") from plane to surface." << std::endl;
00200   std::cout << "Coordinates on surface: (" << surfacePnt->getX() << ", "
00201       << surfacePnt->getY() << ", " << surfacePnt->getZ() << ")" << std::endl;
00202   if ((fabs(surfacePnt->getX() - 3.3072251285719) +
00203       fabs(surfacePnt->getY() - 0.1) +
00204       fabs(surfacePnt->getZ() - 0.9511475682682)) > epsError)
00205   {
00206     std::cout << "FAIL: The coordinates on the surface should be\n"
00207         << "  (3.30722512857195, 0.1, 0.9511475682682)."
00208         << std::endl;
00209     ++failCount;
00210   }
00211   // The line also intersects the surface near (3.95, 0.1, 0.3083726968400695)
00212   // but that point is farther away from the plane than the expected result.
00213   delete surfacePnt;
00214 
00215 
00216   // Perform a similar test with the plane origin at x = 2.1
00217   origin[0] = 2.1;
00218   AF2PlaneProjection wackyProjShift02(this_surf->igeom_instance(),
00219       this_surf->geom_handle(), origin, normal, xDir, 1.0);
00220   std::cout << "Created plane passing through (2.1, 0, 0) "
00221       << "normal to (-1, 0, 1)." << std::endl;
00222   std::cout << "The y-coordinate in the plane is in the direction of "
00223       << "the y-axis." << std::endl;
00224 
00225   AF2Point2D planeTestPntE(1.5261999702635065, 0.1);
00226   surfacePnt = wackyProjShift02.transformToSurface(planeTestPntE, 5);
00227   std::cout << "Transform (" << planeTestPntE.getX() << ", "
00228       << planeTestPntE.getY() << ") from plane to surface." << std::endl;
00229   std::cout << "Coordinates on surface: (" << surfacePnt->getX() << ", "
00230       << surfacePnt->getY() << ", " << surfacePnt->getZ() << ")" << std::endl;
00231   if ((fabs(surfacePnt->getX() - 3.3072251285719) +
00232       fabs(surfacePnt->getY() - 0.1) +
00233       fabs(surfacePnt->getZ() - 0.9511475682682)) > epsError)
00234   {
00235     std::cout << "FAIL: The coordinates on the surface should be\n"
00236         << "  (3.30722512857195, 0.1, 0.9511475682682)."
00237         << std::endl;
00238     ++failCount;
00239   }
00240   // The line also intersects the surface near (3.95, 0.1, 0.3083726968400695)
00241   // but that point is farther away from the plane than the expected result.
00242   // Both points are in the negative normal direction from the plane.
00243   delete surfacePnt;
00244 
00245   delete mk;
00246 
00247   return ( failCount == 0 );
00248 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines