1 /* *****************************************************************
2     MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4     Copyright 2004 Sandia Corporation and Argonne National
5     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
6     with Sandia Corporation, the U.S. Government retains certain
7     rights in 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     diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
24     pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov
25 
26   ***************************************************************** */
27 /*! \file EdgeLengthRangeQualityMetric.cpp
28   \author Michael Brewer
29   \date 2002-05-14
30   Evaluates the lengths of the edges attached to the given vertex.
31   By default, the averaging method is set to SUM.
32 */
33 
34 
35 #include "EdgeLengthRangeQualityMetric.hpp"
36 #include "Vector3D.hpp"
37 #include "QualityMetric.hpp"
38 #include "MsqVertex.hpp"
39 #include "PatchData.hpp"
40 #include "MsqDebug.hpp"
41 #include "MsqError.hpp"
42 
43 #include <vector>
44 using std::vector;
45 
46 using namespace MBMesquite;
47 
EdgeLengthRangeQualityMetric(double low_a,double high_a)48 EdgeLengthRangeQualityMetric::EdgeLengthRangeQualityMetric( double low_a, double high_a )
49   : AveragingQM(SUM),
50     highVal(high_a),
51     lowVal(low_a)
52 {
53   if (lowVal > highVal)
54     std::swap( lowVal, highVal );
55 }
56 
~EdgeLengthRangeQualityMetric()57 EdgeLengthRangeQualityMetric::~EdgeLengthRangeQualityMetric()
58 {}
59 
get_name() const60 std::string EdgeLengthRangeQualityMetric::get_name() const
61   { return "Edge Length Range Metric"; }
62 
get_negate_flag() const63 int EdgeLengthRangeQualityMetric::get_negate_flag() const
64   { return 1; }
65 
66 /*!For the given vertex, vert, with connected edges of lengths l_j for
67   j=1...k, the metric value is the average (where the default average
68   type is SUM) of
69         u_j = ( | l_j - lowVal | - (l_j - lowVal) )^2 +
70               ( | highVal - l_j | - (highVal - l_j) )^2.
71 */
evaluate_common(PatchData & pd,size_t this_vert,double & fval,std::vector<size_t> & adj_verts,MsqError & err)72 bool EdgeLengthRangeQualityMetric::evaluate_common(PatchData &pd,
73                                              size_t this_vert,
74                                              double &fval,
75                                              std::vector<size_t>& adj_verts,
76                                              MsqError &err)
77 {
78   fval=0.0;
79   Vector3D edg;
80   pd.get_adjacent_vertex_indices(this_vert,adj_verts,err);  MSQ_ERRZERO(err);
81   int num_sample_points=adj_verts.size();
82   double *metric_values=new double[num_sample_points];
83   const MsqVertex* verts = pd.get_vertex_array(err);  MSQ_ERRZERO(err);
84     //store the length of the edge, and the first and second component of
85     //metric values, respectively.
86   double temp_length=0.0;
87   double temp_first=0.0;
88   double temp_second=0.0;
89     //PRINT_INFO("INSIDE ELR, vertex = %f,%f,%f\n",verts[this_vert][0],verts[this_vert][1],verts[this_vert][2]);
90     //loop while there are still more adjacent vertices.
91   for (unsigned i = 0; i < adj_verts.size(); ++i)
92   {
93     edg = verts[this_vert] - verts[adj_verts[i]];
94       //compute the edge length
95     temp_length=edg.length();
96       //get the first component
97     temp_first = temp_length - lowVal;
98     temp_first = fabs(temp_first) - (temp_first);
99     temp_first*=temp_first;
100       //get the second component
101     temp_second = highVal - temp_length;
102     temp_second = fabs(temp_second) - (temp_second);
103     temp_second*=temp_second;
104       //combine the two components
105     metric_values[i]=temp_first+temp_second;
106   }
107     //average the metric values of the edges
108   fval=average_metrics(metric_values,num_sample_points,err);
109     //clean up
110   delete[] metric_values;
111     //always return true because mesh is always valid wrt this metric.
112   return !MSQ_CHKERR(err);
113 
114 }
115 
116 
evaluate(PatchData & pd,size_t vertex,double & value,MsqError & err)117 bool EdgeLengthRangeQualityMetric::evaluate( PatchData& pd,
118                                         size_t vertex,
119                                         double& value,
120                                         MsqError& err )
121 {
122   std::vector<size_t> verts;
123   bool rval = evaluate_common( pd, vertex, value, verts, err );
124   return !MSQ_CHKERR(err) && rval;
125 }
126 
evaluate_with_indices(PatchData & pd,size_t vertex,double & value,std::vector<size_t> & indices,MsqError & err)127 bool EdgeLengthRangeQualityMetric::evaluate_with_indices( PatchData& pd,
128                                                      size_t vertex,
129                                                      double& value,
130                                                      std::vector<size_t>& indices,
131                                                      MsqError& err )
132 {
133   indices.clear();
134   bool rval = evaluate_common( pd, vertex, value, indices, err );
135 
136   std::vector<size_t>::iterator r, w;
137   for (r = w = indices.begin(); r != indices.end(); ++r) {
138     if (*r < pd.num_free_vertices()) {
139       *w = *r;
140       ++w;
141     }
142   }
143   indices.erase( w, indices.end() );
144   if (vertex < pd.num_free_vertices())
145     indices.push_back( vertex );
146 
147   return !MSQ_CHKERR(err) && rval;
148 }
149