1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2006 Sandia National Laboratories.  Developed at the
    University of Wisconsin--Madison under SNL contract number
    624796.  The U.S. Government and the University of Wisconsin
    retain certain rights to this software.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    (lgpl.txt) along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    (2006) [email protected]

  ***************************************************************** */

/** \file TargetWriter.cpp
 *  \brief
 *  \author Jason Kraftcheck
 */

#include "Mesquite.hpp"
#include "TargetWriter.hpp"
#include "TargetCalculator.hpp"
#include "WeightCalculator.hpp"
#include "MeshInterface.hpp"
#include "MsqMatrix.hpp"
#include "PatchData.hpp"
#include "PatchSet.hpp"
#include "MsqError.hpp"
#include "ElementPatches.hpp"
#include "ElemSampleQM.hpp"
#include <sstream>

namespace MBMesquite
{

TargetWriter::TargetWriter( TargetCalculator* tc,
                            WeightCalculator* wc,
                            std::string target_base_name,
                            std::string weight_base_name )
    : targetCalc( tc ), weightCalc( wc ), targetName( target_base_name ), weightName( weight_base_name )
{
}

TargetWriter::~TargetWriter() {}

std::string TargetWriter::get_name() const
{
    return "TargetWriter";
}

double TargetWriter::loop_over_mesh( MeshDomainAssoc* mesh_and_domain, const Settings* settings, MsqError& err )
{
    Mesh* mesh         = mesh_and_domain->get_mesh();
    MeshDomain* domain = mesh_and_domain->get_domain();

    PatchData patch;
    patch.set_mesh( mesh );
    patch.set_domain( domain );
    if( settings ) patch.attach_settings( settings );

    ElementPatches patch_set;
    patch_set.set_mesh( mesh );
    std::vector< PatchSet::PatchHandle > patches;
    std::vector< PatchSet::PatchHandle >::iterator p;
    std::vector< Mesh::VertexHandle > patch_verts;
    std::vector< Mesh::ElementHandle > patch_elems;

    patch_set.get_patch_handles( patches, err );
    MSQ_ERRZERO( err );

    std::vector< MsqMatrix< 3, 3 > > targets3d;
    std::vector< MsqMatrix< 3, 2 > > targets2dorient;
    std::vector< MsqMatrix< 2, 2 > > targets2d;
    std::vector< double > weights;
    std::vector< Sample > samples;
    for( p = patches.begin(); p != patches.end(); ++p )
    {
        patch_verts.clear();
        patch_elems.clear();
        patch_set.get_patch( *p, patch_elems, patch_verts, err );
        MSQ_ERRZERO( err );
        patch.set_mesh_entities( patch_elems, patch_verts, err );
        MSQ_ERRZERO( err );
        assert( patch.num_elements() == 1 );

        MsqMeshEntity& elem = patch.element_by_index( 0 );
        EntityTopology type = elem.get_element_type();
        patch.get_samples( 0, samples, err );
        MSQ_ERRZERO( err );
        if( samples.empty() ) continue;

        if( targetCalc )
        {
            const unsigned dim = TopologyInfo::dimension( type );
            if( dim == 3 )
            {
                targets3d.resize( samples.size() );
                for( unsigned i = 0; i < samples.size(); ++i )
                {
                    targetCalc->get_3D_target( patch, 0, samples[i], targets3d[i], err );
                    MSQ_ERRZERO( err );

                    if( DBL_EPSILON > det( targets3d[i] ) )
                    {
                        MSQ_SETERR( err )( "Inverted 3D target", MsqError::INVALID_ARG );
                        return 0.0;
                    }
                }

                TagHandle tag = get_target_tag( 3, samples.size(), mesh, err );
                MSQ_ERRZERO( err );
                mesh->tag_set_element_data( tag, 1, patch.get_element_handles_array(), arrptr( targets3d ), err );
                MSQ_ERRZERO( err );
            }
            else if( targetCalc->have_surface_orient() )
            {
                targets2dorient.resize( samples.size() );
                for( unsigned i = 0; i < samples.size(); ++i )
                {
                    targetCalc->get_surface_target( patch, 0, samples[i], targets2dorient[i], err );
                    MSQ_ERRZERO( err );

                    MsqMatrix< 3, 1 > cross = targets2dorient[i].column( 0 ) * targets2dorient[i].column( 1 );
                    if( DBL_EPSILON > ( cross % cross ) )
                    {
                        MSQ_SETERR( err )( "Degenerate 2D target", MsqError::INVALID_ARG );
                        return 0.0;
                    }
                }

                TagHandle tag = get_target_tag( 2, samples.size(), mesh, err );
                MSQ_ERRZERO( err );
                mesh->tag_set_element_data( tag, 1, patch.get_element_handles_array(), arrptr( targets2dorient ), err );
                MSQ_ERRZERO( err );
            }
            else
            {
                targets2d.resize( samples.size() );
                for( unsigned i = 0; i < samples.size(); ++i )
                {
                    targetCalc->get_2D_target( patch, 0, samples[i], targets2d[i], err );
                    MSQ_ERRZERO( err );

                    if( DBL_EPSILON > det( targets2d[i] ) )
                    {
                        MSQ_SETERR( err )( "Degenerate/Inverted 2D target", MsqError::INVALID_ARG );
                        return 0.0;
                    }
                }

                TagHandle tag = get_target_tag( 2, samples.size(), mesh, err );
                MSQ_ERRZERO( err );
                mesh->tag_set_element_data( tag, 1, patch.get_element_handles_array(), arrptr( targets2d ), err );
                MSQ_ERRZERO( err );
            }
        }

        if( weightCalc )
        {
            weights.resize( samples.size() );
            for( unsigned i = 0; i < samples.size(); ++i )
            {
                weights[i] = weightCalc->get_weight( patch, 0, samples[i], err );
                MSQ_ERRZERO( err );
            }
            TagHandle tag = get_weight_tag( samples.size(), mesh, err );
            MSQ_ERRZERO( err );
            mesh->tag_set_element_data( tag, 1, patch.get_element_handles_array(), arrptr( weights ), err );
            MSQ_ERRZERO( err );
        }
    }

    return 0.0;
}

TagHandle TargetWriter::get_target_tag( unsigned dim, unsigned count, Mesh* mesh, MsqError& err )
{
    if( dim == 2 && !targetCalc->have_surface_orient() )
        count *= 4;
    else
        count *= 3 * dim;  // num doubles
    if( targetTags.size() <= count ) targetTags.resize( count + 1, 0 );
    if( !targetTags[count] )
    {
        targetTags[count] = get_tag_handle( targetName, count, mesh, err );
        if( MSQ_CHKERR( err ) )
        {
            targetTags[count] = 0;
            return 0;
        }
    }
    return targetTags[count];
}

TagHandle TargetWriter::get_weight_tag( unsigned count, Mesh* mesh, MsqError& err )
{
    if( weightTags.size() <= count ) weightTags.resize( count + 1, 0 );
    if( !weightTags[count] )
    {
        weightTags[count] = get_tag_handle( weightName, count, mesh, err );
        if( MSQ_CHKERR( err ) )
        {
            weightTags[count] = 0;
            return 0;
        }
    }
    return weightTags[count];
}

TagHandle TargetWriter::get_tag_handle( const std::string& base_name, unsigned num_dbl, Mesh* mesh, MsqError& err )
{
    std::ostringstream sstr;
    sstr << base_name << num_dbl;

    TagHandle handle = mesh->tag_get( sstr.str().c_str(), err );
    if( !MSQ_CHKERR( err ) )
    {
        std::string temp_name;
        Mesh::TagType temp_type;
        unsigned temp_length;
        mesh->tag_properties( handle, temp_name, temp_type, temp_length, err );
        MSQ_ERRZERO( err );

        if( temp_type != Mesh::DOUBLE || temp_length != num_dbl )
        {
            MSQ_SETERR( err )
            ( MsqError::TAG_ALREADY_EXISTS, "Mismatched type or length for existing tag \"%s\"", sstr.str().c_str() );
        }
    }
    else if( err.error_code() == MsqError::TAG_NOT_FOUND )
    {
        err.clear();
        handle = mesh->tag_create( sstr.str().c_str(), Mesh::DOUBLE, num_dbl, 0, err );
        MSQ_ERRZERO( err );
    }
    return handle;
}

void TargetWriter::initialize_queue( MeshDomainAssoc*, const Settings*, MsqError& ) {}
}  // namespace MBMesquite