MOAB: Mesh Oriented datABase  (version 5.4.1)
XYRectangleTest.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2007 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     (2007) [email protected]
00024 
00025   ***************************************************************** */
00026 
00027 /** \file XYRectangleTest.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "XYRectangle.hpp"
00034 #include "ArrayMesh.hpp"
00035 #include "MsqVertex.hpp"
00036 #include "UnitUtil.hpp"
00037 #include "MsqError.hpp"
00038 
00039 using namespace MBMesquite;
00040 using namespace std;
00041 
00042 // const double WIDTH = 2, HEIGHT = 3, XMIN = -2, YMIN = 1;
00043 const double WIDTH = 4, HEIGHT = 2, XMIN = 0, YMIN = 0;
00044 
00045 class XYRectangleTest : public CppUnit::TestFixture
00046 {
00047   private:
00048     CPPUNIT_TEST_SUITE( XYRectangleTest );
00049     CPPUNIT_TEST( test_snap_to );
00050     CPPUNIT_TEST( test_normal_at );
00051     CPPUNIT_TEST( test_closest_point );
00052     CPPUNIT_TEST( test_domain_DoF );
00053     CPPUNIT_TEST_SUITE_END();
00054 
00055     vector< double > vertCoords, invertCoords;
00056     vector< int > fixedFlags;
00057     vector< unsigned long > triConn, invertConn;
00058 
00059     ArrayMesh myMesh;
00060     XYRectangle myDomain;
00061     std::vector< double > mCoords;
00062     std::vector< int > mFlags;
00063 
00064   public:
00065     void setUp();
00066     void tearDown();
00067 
00068   public:
00069     XYRectangleTest() : myDomain( WIDTH, HEIGHT, XMIN, YMIN ) {}
00070 
00071     void test_snap_to();
00072     void test_normal_at();
00073     void test_closest_point();
00074     void test_domain_DoF();
00075 };
00076 
00077 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XYRectangleTest, "Unit" );
00078 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XYRectangleTest, "XYRectangleTest" );
00079 
00080 /*   (7)-------(8)-------(9)
00081  *    |\       / \       /|
00082  *    | \  8  /   \  9  / |
00083  *    |  \   /  6  \   /  |
00084  *    | 5 \ /       \ / 7 |
00085  *   (3)--(4)-------(5)--(6)
00086  *    | 2 / \       / \ 4 |
00087  *    |  /   \  3  /   \  |
00088  *    | /  0  \   /  1  \ |
00089  *    |/       \ /       \|
00090  *   (0)-------(1)-------(2)
00091  */
00092 double TEST_MESH_COORDS[]            = { XMIN,
00093                               YMIN,
00094                               0,
00095                               XMIN + 0.5 * WIDTH,
00096                               YMIN,
00097                               0,
00098                               XMIN + WIDTH,
00099                               YMIN,
00100                               0,
00101                               XMIN,
00102                               YMIN + 0.5 * HEIGHT,
00103                               0,
00104                               XMIN + .25 * WIDTH,
00105                               YMIN + 0.5 * HEIGHT,
00106                               0,
00107                               XMIN + .75 * WIDTH,
00108                               YMIN + 0.5 * HEIGHT,
00109                               0,
00110                               XMIN + WIDTH,
00111                               YMIN + 0.5 * HEIGHT,
00112                               0,
00113                               XMIN,
00114                               YMIN + HEIGHT,
00115                               0,
00116                               XMIN + 0.5 * WIDTH,
00117                               YMIN + HEIGHT,
00118                               0,
00119                               XMIN + WIDTH,
00120                               YMIN + HEIGHT,
00121                               0 };
00122 const int NUM_TEST_MESH_VERTS        = sizeof( TEST_MESH_COORDS ) / sizeof( double ) / 3;
00123 const unsigned long TEST_MESH_CONN[] = { 0, 1, 4, 1, 2, 5, 0, 4, 3, 1, 5, 4, 2, 6, 5,
00124                                          3, 4, 7, 4, 5, 8, 5, 6, 9, 4, 8, 7, 5, 9, 8 };
00125 const int NUM_TEST_MESH_TRIS         = sizeof( TEST_MESH_CONN ) / sizeof( TEST_MESH_CONN[0] ) / 3;
00126 
00127 void XYRectangleTest::setUp()
00128 {
00129     MsqError err;
00130     mCoords.resize( 3 * NUM_TEST_MESH_VERTS );
00131     std::copy( TEST_MESH_COORDS, TEST_MESH_COORDS + 3 * NUM_TEST_MESH_VERTS, mCoords.begin() );
00132     mFlags.clear();
00133     mFlags.resize( NUM_TEST_MESH_VERTS, 0 );
00134     myMesh.set_mesh( 3, NUM_TEST_MESH_VERTS, arrptr( mCoords ), arrptr( mFlags ), NUM_TEST_MESH_TRIS, TRIANGLE,
00135                      TEST_MESH_CONN );
00136     myDomain.setup( &myMesh, err );
00137     ASSERT_NO_ERROR( err );
00138 }
00139 
00140 void XYRectangleTest::tearDown() {}
00141 
00142 Vector3D snap_to( const Vector3D& vertex, const Vector3D& point )
00143 {
00144     Vector3D result;
00145     result[2] = 0.0;
00146 
00147     if( fabs( vertex[0] - XMIN ) < 1e-6 )
00148         result[0] = XMIN;
00149     else if( fabs( vertex[0] - XMIN - WIDTH ) < 1e-6 )
00150         result[0] = XMIN + WIDTH;
00151     else
00152         result[0] = point[0];
00153 
00154     if( fabs( vertex[1] - YMIN ) < 1e-6 )
00155         result[1] = YMIN;
00156     else if( fabs( vertex[1] - YMIN - HEIGHT ) < 1e-6 )
00157         result[1] = YMIN + HEIGHT;
00158     else
00159         result[1] = point[1];
00160 
00161     return result;
00162 }
00163 
00164 void XYRectangleTest::test_snap_to()
00165 {
00166     MsqError err;
00167     Vector3D off, exp, act;
00168 
00169     const Vector3D d1( 0.1, 0.1, 0.0 );
00170     const Vector3D d2( 0.0, 0.2, 0.0 );
00171     const Vector3D d3( 0.3, 0.0, 0.0 );
00172     const Vector3D d4( 0.0, 0.0, 5.0 );
00173     const Vector3D d5( 0.5, 0.5, 0.5 );
00174 
00175     std::vector< Mesh::VertexHandle > verts;
00176     myMesh.get_all_vertices( verts, err );
00177     ASSERT_NO_ERROR( err );
00178     CPPUNIT_ASSERT( !verts.empty() );
00179     std::vector< MsqVertex > coords( verts.size() );
00180     myMesh.vertices_get_coordinates( arrptr( verts ), arrptr( coords ), verts.size(), err );
00181     ASSERT_NO_ERROR( err );
00182 
00183     for( size_t i = 0; i < coords.size(); ++i )
00184     {
00185 
00186         off = coords[i] + d1;
00187         exp = snap_to( coords[i], off );
00188         act = off;
00189         myDomain.snap_to( verts[i], act );
00190         CPPUNIT_ASSERT_VECTORS_EQUAL( exp, act, 1e-6 );
00191 
00192         off = coords[i] + d2;
00193         exp = snap_to( coords[i], off );
00194         act = off;
00195         myDomain.snap_to( verts[i], act );
00196         CPPUNIT_ASSERT_VECTORS_EQUAL( exp, act, 1e-6 );
00197 
00198         off = coords[i] + d3;
00199         exp = snap_to( coords[i], off );
00200         act = off;
00201         myDomain.snap_to( verts[i], act );
00202         CPPUNIT_ASSERT_VECTORS_EQUAL( exp, act, 1e-6 );
00203 
00204         off = coords[i] + d4;
00205         exp = snap_to( coords[i], off );
00206         act = off;
00207         myDomain.snap_to( verts[i], act );
00208         CPPUNIT_ASSERT_VECTORS_EQUAL( exp, act, 1e-6 );
00209 
00210         off = coords[i] + d5;
00211         exp = snap_to( coords[i], off );
00212         act = off;
00213         myDomain.snap_to( verts[i], act );
00214         CPPUNIT_ASSERT_VECTORS_EQUAL( exp, act, 1e-6 );
00215     }
00216 }
00217 
00218 void XYRectangleTest::test_normal_at()
00219 {
00220     MsqError err;
00221     std::vector< Mesh::VertexHandle > vertices;
00222     myMesh.get_all_vertices( vertices, err );
00223     ASSERT_NO_ERROR( err );
00224 
00225     std::vector< MsqVertex > coords( vertices.size() );
00226     myMesh.vertices_get_coordinates( arrptr( vertices ), arrptr( coords ), vertices.size(), err );
00227     ASSERT_NO_ERROR( err );
00228 
00229     std::vector< Vector3D > normals( vertices.size() );
00230     std::copy( coords.begin(), coords.end(), normals.begin() );
00231     myDomain.vertex_normal_at( arrptr( vertices ), arrptr( normals ), vertices.size(), err );
00232     ASSERT_NO_ERROR( err );
00233 
00234     for( size_t i = 0; i < normals.size(); ++i )
00235     {
00236         CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( 0, 0, 1 ), normals[i], 1e-6 );
00237     }
00238 }
00239 
00240 void XYRectangleTest::test_closest_point()
00241 {
00242     MsqError err;
00243     std::vector< Mesh::ElementHandle > elems;
00244     myMesh.get_all_elements( elems, err );
00245     ASSERT_NO_ERROR( err );
00246 
00247     for( size_t i = 0; i < elems.size(); ++i )
00248     {
00249         std::vector< Mesh::VertexHandle > verts;
00250         std::vector< size_t > junk;
00251         MsqVertex coords;
00252         myMesh.elements_get_attached_vertices( &elems[i], 1, verts, junk, err );
00253         ASSERT_NO_ERROR( err );
00254         myMesh.vertices_get_coordinates( arrptr( verts ), &coords, 1, err );
00255         ASSERT_NO_ERROR( err );
00256 
00257         Vector3D offset( coords + Vector3D( 0, 0, 3 ) ), closest, norm;
00258         myDomain.closest_point( elems[i], offset, closest, norm, err );
00259         ASSERT_NO_ERROR( err );
00260         CPPUNIT_ASSERT_VECTORS_EQUAL( coords, closest, 1e-6 );
00261         CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( 0, 0, 1 ), norm, 1e-6 );
00262     }
00263 }
00264 
00265 unsigned short dof( const Vector3D& point )
00266 {
00267     unsigned short result = 2;
00268     if( ( fabs( point[0] - XMIN ) < 1e-6 ) || ( fabs( point[0] - XMIN - WIDTH ) < 1e-6 ) ) --result;
00269     if( ( fabs( point[1] - YMIN ) < 1e-6 ) || ( fabs( point[1] - YMIN - HEIGHT ) < 1e-6 ) ) --result;
00270     return result;
00271 }
00272 
00273 void XYRectangleTest::test_domain_DoF()
00274 {
00275     MsqError err;
00276 
00277     std::vector< Mesh::VertexHandle > verts;
00278     myMesh.get_all_vertices( verts, err );
00279     ASSERT_NO_ERROR( err );
00280     std::vector< MsqVertex > coords( verts.size() );
00281     myMesh.vertices_get_coordinates( arrptr( verts ), arrptr( coords ), verts.size(), err );
00282     ASSERT_NO_ERROR( err );
00283 
00284     for( size_t i = 0; i < coords.size(); ++i )
00285     {
00286         unsigned short exp = dof( coords[i] );
00287         unsigned short act = 100;
00288         myDomain.domain_DoF( &verts[i], &act, 1, err );
00289         ASSERT_NO_ERROR( err );
00290         CPPUNIT_ASSERT_EQUAL( exp, act );
00291     }
00292 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines