MOAB: Mesh Oriented datABase  (version 5.2.1)
SphericalDomainTest.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2010 Sandia National Laboratories.  Developed at the
00005     University of Wisconsin--Madison under SNL contract number
00006     624796.  The U.S. Government and the University of Wisconsin
00007     retain certain rights to this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2010) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file SphericalDomainTest.cpp
00028  *  \brief Unit tests for SphericalDomain class
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "UnitUtil.hpp"
00034 #include "SphericalDomain.hpp"
00035 #include "ArrayMesh.hpp"
00036 
00037 using namespace MBMesquite;
00038 
00039 class SphericalDomainTest : public CppUnit::TestFixture
00040 {
00041     CPPUNIT_TEST_SUITE( SphericalDomainTest );
00042     CPPUNIT_TEST( test_construct );
00043     CPPUNIT_TEST( test_fit_vertices );
00044     CPPUNIT_TEST( test_snap_to );
00045     CPPUNIT_TEST( test_normal_at );
00046     CPPUNIT_TEST( test_closest_point );
00047     CPPUNIT_TEST( test_domain_DoF );
00048     CPPUNIT_TEST_SUITE_END();
00049 
00050   public:
00051     void test_construct();
00052     void test_fit_vertices();
00053     void test_snap_to();
00054     void test_normal_at();
00055     void test_closest_point();
00056     void test_domain_DoF();
00057 
00058   private:
00059     void check_closest_pt( const SphericalDomain& dom, const Vector3D& input_pt, const Vector3D& output_pt );
00060 
00061     void check_normal( const SphericalDomain& dom, const Vector3D& point, const Vector3D& normal );
00062 };
00063 
00064 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SphericalDomainTest, "SphericalDomainTest" );
00065 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SphericalDomainTest, "Unit" );
00066 
00067 void SphericalDomainTest::check_closest_pt( const SphericalDomain& dom, const Vector3D& input_pt,
00068                                             const Vector3D& output_pt )
00069 {
00070     Vector3D vo = output_pt - dom.center();
00071     Vector3D vi = input_pt - dom.center();
00072     CPPUNIT_ASSERT_DOUBLES_EQUAL( dom.radius(), vo.length(), 1e-6 );
00073     vi *= dom.radius() / vi.length();
00074     CPPUNIT_ASSERT_VECTORS_EQUAL( vo, vi, 1e-6 );
00075 }
00076 void SphericalDomainTest::check_normal( const SphericalDomain& dom, const Vector3D& point, const Vector3D& normal )
00077 {
00078     Vector3D vi = point - dom.center();
00079     CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, normal.length(), 1e-6 );
00080     vi /= vi.length();
00081     CPPUNIT_ASSERT_VECTORS_EQUAL( vi, normal, 1e-6 );
00082 }
00083 
00084 void SphericalDomainTest::test_construct()
00085 {
00086     Vector3D cen( 1, 2, -1 );
00087     double rad = 3.14159;
00088     SphericalDomain sph( cen, rad );
00089     CPPUNIT_ASSERT_VECTORS_EQUAL( cen, sph.center(), 1e-18 );
00090     CPPUNIT_ASSERT_DOUBLES_EQUAL( rad, sph.radius(), 1e-18 );
00091 
00092     cen = Vector3D( 5, 6, 1.14 );
00093     rad = 1 / rad;
00094     sph.set_sphere( cen, rad );
00095     CPPUNIT_ASSERT_VECTORS_EQUAL( cen, sph.center(), 1e-18 );
00096     CPPUNIT_ASSERT_DOUBLES_EQUAL( rad, sph.radius(), 1e-18 );
00097 }
00098 
00099 static void sphere_point( Vector3D cen, double rad, double theta, double phi, double pt[3] )
00100 {
00101     Vector3D pdir( cos( phi ) * cos( theta ), cos( phi ) * sin( theta ), sin( phi ) );
00102     Vector3D rslt = cen + rad * pdir;
00103     pt[0]         = rslt[0];
00104     pt[1]         = rslt[1];
00105     pt[2]         = rslt[2];
00106 }
00107 
00108 void SphericalDomainTest::test_fit_vertices()
00109 {
00110     Vector3D cen( 1, 2, 3 );
00111     double rad = 2.5;
00112 
00113     // point locations on a expected sphere
00114     const int num_pt = 6;
00115     double coords[3 * num_pt];
00116     sphere_point( cen, rad, 0.1, 0.2, coords );
00117     sphere_point( cen, rad, 2.0, 2.0, coords + 3 );
00118     sphere_point( cen, rad, -1.0, 0.0, coords + 6 );
00119     sphere_point( cen, rad, 3.1, -0.5, coords + 9 );
00120     sphere_point( cen, rad, -1.5, -1.0, coords + 12 );
00121     sphere_point( cen, rad, 0.2, 0.1, coords + 15 );
00122 
00123     // make sure our setup is valid
00124     for( int i = 0; i < num_pt; ++i )
00125     {
00126         Vector3D pt( coords + 3 * i );
00127         CPPUNIT_ASSERT_DOUBLES_EQUAL( rad, ( pt - cen ).length(), 1e-6 );
00128     }
00129 
00130     std::vector< int > fixed( num_pt, 0 );
00131     ArrayMesh mesh( 3, num_pt, coords, arrptr( fixed ), 0, TRIANGLE, 0 );
00132     SphericalDomain sph;
00133     MsqError err;
00134     sph.fit_vertices( &mesh, err );
00135     ASSERT_NO_ERROR( err );
00136     CPPUNIT_ASSERT_VECTORS_EQUAL( cen, sph.center(), 1e-6 );
00137     CPPUNIT_ASSERT_DOUBLES_EQUAL( rad, sph.radius(), 1e-6 );
00138 }
00139 
00140 void SphericalDomainTest::test_snap_to()
00141 {
00142     Vector3D cen( 3.14, 15, 0.91 );
00143     double rad = 6.02;
00144     SphericalDomain dom( cen, rad );
00145 
00146     const int num_pts         = 5;
00147     double points[num_pts][3] = { { 0, 0, 0 }, { 10, 11, 8 }, { 1, 2, 3 }, { -5, 1, 1 }, { -1, 0, -2 } };
00148     for( int i = 0; i < num_pts; ++i )
00149     {
00150         Vector3D v( points[i] );
00151         dom.snap_to( 0, v );
00152         check_closest_pt( dom, Vector3D( points[i] ), v );
00153     }
00154 }
00155 
00156 void SphericalDomainTest::test_normal_at()
00157 {
00158     Vector3D cen( -3.14, 0, 0.91 );
00159     double rad = 2;
00160     SphericalDomain dom( cen, rad );
00161 
00162     const int num_pts         = 5;
00163     double points[num_pts][3] = { { 0, 0, 0 }, { 10, 11, 8 }, { 1, 2, 3 }, { -5, 1, 1 }, { -1, 0, -2 } };
00164     for( int i = 0; i < num_pts; ++i )
00165     {
00166         Vector3D v( points[i] );
00167         dom.vertex_normal_at( 0, v );
00168         check_normal( dom, Vector3D( points[i] ), v );
00169 
00170         v = Vector3D( points[i] );
00171         dom.element_normal_at( 0, v );
00172         check_normal( dom, Vector3D( points[i] ), v );
00173     }
00174 }
00175 
00176 void SphericalDomainTest::test_closest_point()
00177 {
00178     Vector3D cen( -1, -1, -2 );
00179     double rad = 1.4;
00180     SphericalDomain dom( cen, rad );
00181     MsqPrintError err( std::cout );
00182 
00183     const int num_pts         = 5;
00184     double points[num_pts][3] = { { 0, 0, 0 }, { 10, 11, 8 }, { 1, 2, 3 }, { -5, 1, 1 }, { -1, 0, -2 } };
00185     for( int i = 0; i < num_pts; ++i )
00186     {
00187         Vector3D p( points[i] ), c, n;
00188         dom.closest_point( 0, p, c, n, err );
00189         ASSERT_NO_ERROR( err );
00190         check_closest_pt( dom, p, c );
00191         check_normal( dom, p, n );
00192     }
00193 }
00194 
00195 void SphericalDomainTest::test_domain_DoF()
00196 {
00197     std::vector< Mesh::VertexHandle > junk( 10 );
00198     std::vector< unsigned short > dof( junk.size() );
00199     std::vector< unsigned short > expected( dof.size(), 2 );
00200     SphericalDomain dom;
00201     MsqPrintError err( std::cout );
00202     dom.domain_DoF( arrptr( junk ), arrptr( dof ), junk.size(), err );
00203     ASSERT_NO_ERROR( err );
00204     CPPUNIT_ASSERT( expected == dof );
00205 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines