MOAB: Mesh Oriented datABase  (version 5.4.1)
Randomize.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Sandia Corporation and Argonne National
00005     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
00006     with Sandia Corporation, the U.S. Government retains certain
00007     rights in 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     [email protected], [email protected], [email protected],
00024     [email protected], [email protected], [email protected]
00025 
00026   ***************************************************************** */
00027 /*!
00028   \file   Randomize.cpp
00029   \brief
00030 
00031   The Randomize Class is the concrete class that randomizes
00032   the vertex positions.
00033 
00034   \author Michael Brewer
00035   \date   2002-10-27
00036 */
00037 
00038 #include "Randomize.hpp"
00039 #include "MsqFreeVertexIndexIterator.hpp"
00040 #include "MsqDebug.hpp"
00041 #include <cmath>
00042 
00043 using namespace MBMesquite;
00044 
00045 Randomize::~Randomize() {}
00046 
00047 std::string Randomize::get_name() const
00048 {
00049     return "Randomize";
00050 }
00051 
00052 PatchSet* Randomize::get_patch_set()
00053 {
00054     return &patchSet;
00055 }
00056 
00057 Randomize::Randomize() : mPercent( 0.5 ), patchSet( 1, true ) {}
00058 
00059 Randomize::Randomize( double percent ) : mPercent( percent ), patchSet( 1, true ) {}
00060 
00061 void Randomize::initialize( PatchData& /*pd*/, MsqError& ) {}
00062 
00063 void Randomize::initialize_mesh_iteration( PatchData& /*pd*/, MsqError& /*err*/ )
00064 {
00065     //  cout << "- Executing Randomize::iteration_complete()\n";
00066 }
00067 
00068 /*!Function calculates a scale factor for the patch, then moves
00069   the incident vertex randomly in each of the three coordinate
00070   directions (relative to the scale factor multiplied by mPercent).
00071 */
00072 static inline void randomize_vertex( PatchData& pd, size_t free_ind, double percent, MsqError& err )
00073 {
00074     size_t i;
00075     short j;
00076     const MsqVertex* verts = pd.get_vertex_array( err );MSQ_ERRRTN( err );
00077     const size_t num_vtx = pd.num_nodes();
00078     // a scale w.r.t. the patch size
00079     double scale_factor = 0.0;
00080     // a "random" number between -1 and 1
00081     double rand_double = 0.0;
00082     // a "random" int
00083     int rand_int = 0;
00084     if( num_vtx <= 1 )
00085     {
00086         MSQ_PRINT( 1 )( "WARNING: Number of incident vertex is zero.  Returning.\n" );
00087         return;
00088     }
00089 
00090     for( i = 0; i < num_vtx; ++i )
00091     {
00092         if( i != free_ind ) scale_factor += ( verts[i] - verts[free_ind] ).length();
00093     }
00094     scale_factor /= ( (double)num_vtx - 1.0 );
00095     Vector3D delta;
00096     for( j = 0; j < 3; ++j )
00097     {
00098         rand_int = rand();
00099         // number between 0 and 1000
00100         rand_int = rand_int % 1000;
00101         // number between -1 and 1
00102         rand_double = ( ( (double)rand_int ) / 500.0 ) - 1.0;
00103         delta[j]    = scale_factor * rand_double * percent;
00104     }
00105     pd.move_vertex( delta, free_ind, err );
00106 
00107     return;
00108 }
00109 
00110 void Randomize::optimize_vertex_positions( PatchData& pd, MsqError& err )
00111 {
00112     // cout << "- Executing Randomize::optimize_vertex_position()\n";
00113 
00114     // gets the array of coordinates for the patch and print it
00115     // does the randomize smooth
00116     MsqFreeVertexIndexIterator free_iter( pd, err );MSQ_ERRRTN( err );
00117     free_iter.reset();
00118     free_iter.next();
00119     // find the free vertex.
00120     int m = free_iter.value();
00121     randomize_vertex( pd, m, mPercent, err );MSQ_ERRRTN( err );
00122     pd.snap_vertex_to_domain( m, err );MSQ_ERRRTN( err );
00123 }
00124 
00125 void Randomize::terminate_mesh_iteration( PatchData& /*pd*/, MsqError& /*err*/ )
00126 {
00127     //  cout << "- Executing Randomize::iteration_complete()\n";
00128 }
00129 
00130 void Randomize::cleanup()
00131 {
00132     //  cout << "- Executing Randomize::iteration_end()\n";
00133 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines