Branch data Line data Source code
1 : : /* *****************************************************************
2 : : MESQUITE -- The Mesh Quality Improvement Toolkit
3 : :
4 : : Copyright 2009 Sandia National Laboratories. Developed at the
5 : : University of Wisconsin--Madison under SNL contract number
6 : : 624796. The U.S. Government and the University of Wisconsin
7 : : retain certain rights to this software.
8 : :
9 : : This library is free software; you can redistribute it and/or
10 : : modify it under the terms of the GNU Lesser General Public
11 : : License as published by the Free Software Foundation; either
12 : : version 2.1 of the License, or (at your option) any later version.
13 : :
14 : : This library is distributed in the hope that it will be useful,
15 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : Lesser General Public License for more details.
18 : :
19 : : You should have received a copy of the GNU Lesser General Public License
20 : : (lgpl.txt) along with this library; if not, write to the Free Software
21 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 : :
23 : : (2009) [email protected]
24 : :
25 : : ***************************************************************** */
26 : :
27 : : /** \file EdgeLengthMetric.cpp
28 : : * \brief
29 : : * \author Jason Kraftcheck
30 : : */
31 : :
32 : : #include "EdgeLengthMetric.hpp"
33 : : #include "PatchData.hpp"
34 : : #include "MsqError.hpp"
35 : : #include "Matrix3D.hpp"
36 : :
37 : : namespace MBMesquite
38 : : {
39 : :
40 : 12 : EdgeLengthMetric::EdgeLengthMetric( double gamma ) : mGamma( gamma ) {}
41 : :
42 : 6 : std::string EdgeLengthMetric::get_name() const
43 : : {
44 [ + - ]: 6 : return "EdgeLength";
45 : : }
46 : :
47 : : //! 1 if metric should be minimized, -1 if metric should be maximized.
48 : 0 : int EdgeLengthMetric::get_negate_flag() const
49 : : {
50 : 0 : return 1;
51 : : }
52 : :
53 : 146472 : bool EdgeLengthMetric::evaluate( PatchData& pd, size_t p_handle, double& value, MsqError& err )
54 : : {
55 [ + - ][ + - ]: 146472 : MsqMeshEntity& e = pd.element_by_index( elem( p_handle ) );
56 : : const unsigned* vert_nums;
57 [ + - ][ + - ]: 146472 : vert_nums = TopologyInfo::edge_vertices( e.get_element_type(), edge( p_handle ), err );
[ + - ]
58 [ + - ][ - + ]: 146472 : MSQ_ERRZERO( err );
[ # # ][ # # ]
[ - + ]
59 [ + - ]: 146472 : size_t svi = e.get_vertex_index( vert_nums[0] );
60 [ + - ]: 146472 : size_t evi = e.get_vertex_index( vert_nums[1] );
61 [ + - ][ + - ]: 146472 : Vector3D diff = pd.vertex_by_index( svi ) - pd.vertex_by_index( evi );
[ + - ]
62 [ + - ]: 146472 : double len_sqr = diff % diff - mGamma;
63 [ - + ]: 146472 : if( len_sqr <= 0.0 )
64 : : {
65 : 0 : value = 0.0;
66 : 0 : return false;
67 : : }
68 : 146472 : value = sqrt( len_sqr );
69 : 146472 : return true;
70 : : }
71 : :
72 : 0 : bool EdgeLengthMetric::evaluate_with_gradient( PatchData& pd, size_t p_handle, double& value,
73 : : std::vector< size_t >& indices, std::vector< Vector3D >& gradient,
74 : : MsqError& err )
75 : : {
76 [ # # ][ # # ]: 0 : MsqMeshEntity& e = pd.element_by_index( elem( p_handle ) );
77 : : const unsigned* vert_nums;
78 [ # # ][ # # ]: 0 : vert_nums = TopologyInfo::edge_vertices( e.get_element_type(), edge( p_handle ), err );
[ # # ]
79 [ # # ][ # # ]: 0 : MSQ_ERRZERO( err );
[ # # ][ # # ]
[ # # ]
80 [ # # ]: 0 : size_t svi = e.get_vertex_index( vert_nums[0] );
81 [ # # ]: 0 : size_t evi = e.get_vertex_index( vert_nums[1] );
82 [ # # ][ # # ]: 0 : Vector3D diff = pd.vertex_by_index( svi ) - pd.vertex_by_index( evi );
[ # # ]
83 [ # # ]: 0 : double val_sqr = diff % diff - mGamma;
84 [ # # ]: 0 : if( val_sqr <= 0.0 )
85 : : {
86 : 0 : value = 0.0;
87 : 0 : return false;
88 : : }
89 : 0 : value = sqrt( val_sqr );
90 : :
91 [ # # ]: 0 : diff *= 1.0 / value;
92 : 0 : indices.clear();
93 : 0 : gradient.clear();
94 [ # # ][ # # ]: 0 : if( svi < pd.num_free_vertices() )
95 : : {
96 [ # # ]: 0 : indices.push_back( svi );
97 [ # # ]: 0 : gradient.push_back( diff );
98 : : }
99 [ # # ][ # # ]: 0 : if( evi < pd.num_free_vertices() )
100 : : {
101 [ # # ]: 0 : indices.push_back( evi );
102 [ # # ][ # # ]: 0 : gradient.push_back( -diff );
103 : : }
104 : :
105 : 0 : return true;
106 : : }
107 : :
108 [ + - ][ + - ]: 8 : } // namespace MBMesquite
|