MOAB: Mesh Oriented datABase  (version 5.2.1)
EdgeLengthMetric.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2009 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     (2009) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file EdgeLengthMetric.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "EdgeLengthMetric.hpp"
00033 #include "PatchData.hpp"
00034 #include "MsqError.hpp"
00035 #include "Matrix3D.hpp"
00036 
00037 namespace MBMesquite
00038 {
00039 
00040 EdgeLengthMetric::EdgeLengthMetric( double gamma ) : mGamma( gamma ) {}
00041 
00042 std::string EdgeLengthMetric::get_name() const
00043 {
00044     return "EdgeLength";
00045 }
00046 
00047 //! 1 if metric should be minimized, -1 if metric should be maximized.
00048 int EdgeLengthMetric::get_negate_flag() const
00049 {
00050     return 1;
00051 }
00052 
00053 bool EdgeLengthMetric::evaluate( PatchData& pd, size_t p_handle, double& value, MsqError& err )
00054 {
00055     MsqMeshEntity& e = pd.element_by_index( elem( p_handle ) );
00056     const unsigned* vert_nums;
00057     vert_nums = TopologyInfo::edge_vertices( e.get_element_type(), edge( p_handle ), err );
00058     MSQ_ERRZERO( err );
00059     size_t svi     = e.get_vertex_index( vert_nums[0] );
00060     size_t evi     = e.get_vertex_index( vert_nums[1] );
00061     Vector3D diff  = pd.vertex_by_index( svi ) - pd.vertex_by_index( evi );
00062     double len_sqr = diff % diff - mGamma;
00063     if( len_sqr <= 0.0 )
00064     {
00065         value = 0.0;
00066         return false;
00067     }
00068     value = sqrt( len_sqr );
00069     return true;
00070 }
00071 
00072 bool EdgeLengthMetric::evaluate_with_gradient( PatchData& pd, size_t p_handle, double& value,
00073                                                std::vector< size_t >& indices, std::vector< Vector3D >& gradient,
00074                                                MsqError& err )
00075 {
00076     MsqMeshEntity& e = pd.element_by_index( elem( p_handle ) );
00077     const unsigned* vert_nums;
00078     vert_nums = TopologyInfo::edge_vertices( e.get_element_type(), edge( p_handle ), err );
00079     MSQ_ERRZERO( err );
00080     size_t svi     = e.get_vertex_index( vert_nums[0] );
00081     size_t evi     = e.get_vertex_index( vert_nums[1] );
00082     Vector3D diff  = pd.vertex_by_index( svi ) - pd.vertex_by_index( evi );
00083     double val_sqr = diff % diff - mGamma;
00084     if( val_sqr <= 0.0 )
00085     {
00086         value = 0.0;
00087         return false;
00088     }
00089     value = sqrt( val_sqr );
00090 
00091     diff *= 1.0 / value;
00092     indices.clear();
00093     gradient.clear();
00094     if( svi < pd.num_free_vertices() )
00095     {
00096         indices.push_back( svi );
00097         gradient.push_back( diff );
00098     }
00099     if( evi < pd.num_free_vertices() )
00100     {
00101         indices.push_back( evi );
00102         gradient.push_back( -diff );
00103     }
00104 
00105     return true;
00106 }
00107 
00108 }  // namespace MBMesquite
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines