MOAB: Mesh Oriented datABase
(version 5.4.1)
|
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, 00073 size_t p_handle, 00074 double& value, 00075 std::vector< size_t >& indices, 00076 std::vector< Vector3D >& gradient, 00077 MsqError& err ) 00078 { 00079 MsqMeshEntity& e = pd.element_by_index( elem( p_handle ) ); 00080 const unsigned* vert_nums; 00081 vert_nums = TopologyInfo::edge_vertices( e.get_element_type(), edge( p_handle ), err ); 00082 MSQ_ERRZERO( err ); 00083 size_t svi = e.get_vertex_index( vert_nums[0] ); 00084 size_t evi = e.get_vertex_index( vert_nums[1] ); 00085 Vector3D diff = pd.vertex_by_index( svi ) - pd.vertex_by_index( evi ); 00086 double val_sqr = diff % diff - mGamma; 00087 if( val_sqr <= 0.0 ) 00088 { 00089 value = 0.0; 00090 return false; 00091 } 00092 value = sqrt( val_sqr ); 00093 00094 diff *= 1.0 / value; 00095 indices.clear(); 00096 gradient.clear(); 00097 if( svi < pd.num_free_vertices() ) 00098 { 00099 indices.push_back( svi ); 00100 gradient.push_back( diff ); 00101 } 00102 if( evi < pd.num_free_vertices() ) 00103 { 00104 indices.push_back( evi ); 00105 gradient.push_back( -diff ); 00106 } 00107 00108 return true; 00109 } 00110 00111 } // namespace MBMesquite