ProcessIsoContour.cpp 33.3 KB
Newer Older
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
////////////////////////////////////////////////////////////////////////////////
//
//  File: ProcessIsoContour.cpp
//
//  For more information, please see: http://www.nektar.info/
//
//  The MIT License
//
//  Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
//  Department of Aeronautics, Imperial College London (UK), and Scientific
//  Computing and Imaging Institute, University of Utah (USA).
//
//  License for the specific language governing rights and limitations under
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included
//  in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.
//
//  Description: Set up fields as interpolation to equispaced output
//
////////////////////////////////////////////////////////////////////////////////
#include <string>
#include <iostream>
using namespace std;

#include "ProcessIsoContour.h"

#include <LibUtilities/BasicUtils/SharedArray.hpp>
#include <LibUtilities/BasicUtils/ParseUtils.hpp>
#include <boost/math/special_functions/fpclassify.hpp>

namespace Nektar
{
Chris Cantwell's avatar
Chris Cantwell committed
47 48 49 50 51 52 53
namespace Utilities
{

ModuleKey ProcessIsoContour::className =
    GetModuleFactory().RegisterCreatorFunction(
                        ModuleKey(eProcessModule, "isocontour"),
                        ProcessIsoContour::create,
54
                        "Extract an isocontour of fieldid variable and at value fieldvalue, Optionally fieldstr can be specified for a string defiition or smooth for smoothing");
Chris Cantwell's avatar
Chris Cantwell committed
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

ProcessIsoContour::ProcessIsoContour(FieldSharedPtr f) :
    ProcessEquiSpacedOutput(f)
{

    m_config["fieldstr"]           = ConfigOption(false, "NotSet",
                                        "string of isocontour to be extracted");
    m_config["fieldname"]          = ConfigOption(false, "isocon",
                                        "name for isocontour if fieldstr "
                                        "specified, default is isocon");

    m_config["fieldid"]            = ConfigOption(false, "NotSet",
                                        "field id to extract");
    m_config["fieldvalue"]         = ConfigOption(false, "NotSet",
                                        "field value to extract");

    m_config["globalcondense"]     = ConfigOption(true, "NotSet",
                                        "Globally condense contour to unique "
                                        "values");

    m_config["smooth"]             = ConfigOption(true, "NotSet",
                                        "Smooth isocontour (implies global "
                                        "condense)");
    m_config["smoothiter"]         = ConfigOption(false, "100",
                                        "Number of smoothing cycle, default = "
                                        "100");
    m_config["smoothposdiffusion"] = ConfigOption(false,"0.5",
                                        "Postive diffusion coefficient "
                                        "(0 < lambda < 1), default = 0.5");
    m_config["smoothnegdiffusion"] = ConfigOption(false,"0.495",
                                        "Negative diffusion coefficient "
                                        "(0 < mu < 1), default = 0.495");

88 89 90 91 92
    m_config["removesmallcontour"] = ConfigOption(false,"0",
                                        "Remove contours with less than specified number of triangles."
                                                    "Only valid with GlobalCondense or Smooth options.");


Chris Cantwell's avatar
Chris Cantwell committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

ProcessIsoContour::~ProcessIsoContour(void)
{
}

void ProcessIsoContour::Process(po::variables_map &vm)
{
    vector<IsoSharedPtr> iso;

    // extract all fields to equi-spaced
    SetupEquiSpacedField();

    int     fieldid;
    NekDouble value;

    if(m_config["fieldstr"].m_beenSet) //generate field of interest
110
    {
111
        fieldid = m_f->m_fieldPts->GetNFields();
112

113
        Array<OneD, NekDouble> pts(m_f->m_fieldPts->GetNpoints());
Chris Cantwell's avatar
Chris Cantwell committed
114 115 116 117 118 119

        // evaluate new function
        LibUtilities::AnalyticExpressionEvaluator strEval;
        string varstr = "x y z";
        vector<Array<OneD, const NekDouble> > interpfields;

120
        for(int i = 0; i < m_f->m_fieldPts->GetDim(); ++i)
121
        {
122
            interpfields.push_back(m_f->m_fieldPts->GetPts(i));
123
        }
124
        for(int i = 0; i < m_f->m_fieldPts->GetNFields(); ++i)
125
        {
126 127
            varstr += " " + m_f->m_fieldPts->GetFieldName(i);
            interpfields.push_back(m_f->m_fieldPts->GetPts(i+3));
Chris Cantwell's avatar
Chris Cantwell committed
128
        }
129

Chris Cantwell's avatar
Chris Cantwell committed
130 131 132
        int ExprId  = -1;
        std::string str = m_config["fieldstr"].as<string>();
        ExprId = strEval.DefineFunction(varstr.c_str(), str);
133

134
        strEval.Evaluate(ExprId, interpfields, pts);
135

Chris Cantwell's avatar
Chris Cantwell committed
136
        // set up field name if provided otherwise called "isocon" from default
137 138 139
        string fieldName = m_config["fieldname"].as<string>();

        m_f->m_fieldPts->AddField(pts, fieldName);
Chris Cantwell's avatar
Chris Cantwell committed
140 141 142
    }
    else
    {
143
        ASSERTL0(m_config["fieldid"].as<string>() != "NotSet", "fieldid must be specified");
Chris Cantwell's avatar
Chris Cantwell committed
144 145
        fieldid = m_config["fieldid"].as<int>();
    }
146

147
    ASSERTL0(m_config["fieldvalue"].as<string>() != "NotSet", "fieldvalue must be specified");
Chris Cantwell's avatar
Chris Cantwell committed
148
    value   = m_config["fieldvalue"].as<NekDouble>();
149

Chris Cantwell's avatar
Chris Cantwell committed
150 151 152 153 154 155
    iso = ExtractContour(fieldid,value);
    bool smoothing      = m_config["smooth"].m_beenSet;
    bool globalcondense = m_config["globalcondense"].m_beenSet;
    if(smoothing||globalcondense)
    {
        vector<IsoSharedPtr> glob_iso;
156
        int nfields = m_f->m_fieldPts->GetNFields() + m_f->m_fieldPts->GetDim();
Chris Cantwell's avatar
Chris Cantwell committed
157
        IsoSharedPtr g_iso = MemoryManager<Iso>::AllocateSharedPtr(nfields-3);
158
        int mincontour = 0; 
159

Chris Cantwell's avatar
Chris Cantwell committed
160
        g_iso->globalcondense(iso);
161

Chris Cantwell's avatar
Chris Cantwell committed
162 163 164 165 166 167 168
        if(smoothing)
        {
            int  niter = m_config["smoothiter"].as<int>();
            NekDouble lambda = m_config["smoothposdiffusion"].as<NekDouble>();
            NekDouble mu     = m_config["smoothnegdiffusion"].as<NekDouble>();
            g_iso->smooth(niter,lambda,-mu);
        }
169 170


171 172 173 174 175 176 177 178
        if((mincontour = m_config["removesmallcontour"].as<int>()))
        {
            g_iso->separate_regions(glob_iso,mincontour);
        }
        else
        {
            glob_iso.push_back(g_iso);
        }
Chris Cantwell's avatar
Chris Cantwell committed
179 180 181 182 183 184 185
        ResetFieldPts(glob_iso);
    }
    else
    {
        ResetFieldPts(iso);
    }
}
186

187

Chris Cantwell's avatar
Chris Cantwell committed
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
/*********************/
/* Function TwoPairs */
/*********************/

void TwoPairs (Array<OneD, NekDouble> &cx,
               Array<OneD, NekDouble> &cy,
               Array<OneD, NekDouble> &cz,
               int &pr)
/*  The logic of the following SWITCH statement may only be
    understood with a "peusdo truth-table" */
{
    if (((cx[0]-cx[1])==0.0)&&
        ((cy[0]-cy[1])==0.0)&&
        ((cz[0]-cz[1])==0.0))
    {
        if (((cx[2]-cx[3])==0.0)&&
            ((cy[2]-cy[3])==0.0)&&
            ((cz[2]-cz[3])==0.0))
        {
            pr=4;
        } else
        {
            pr=3;
        }
    }
    else
    {
        pr=1;
    }
}
218 219


Chris Cantwell's avatar
Chris Cantwell committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
/*************************/
/* Function ThreeSimilar */
/*************************/
void ThreeSimilar (const int i, const int j, const int k,
                   int &pr, int &ps)
/*  The logic of the following SWITCH statement may be
    understood with a "peusdo truth-table" */
{
    switch (i + j + k)
    {
        case (3):
            pr = 3;
            ps = 4;
            break;
        case (4):
            pr = 2;
            ps = 4;
            break;
        case (5):
            if (j == 1)
            {
                pr = 2;
                ps = 3;
243 244 245
            }
            else
            {
Chris Cantwell's avatar
Chris Cantwell committed
246 247
                pr = 1;
                ps = 4;
248
            }
Chris Cantwell's avatar
Chris Cantwell committed
249 250 251
            break;
        case (6):
            if (i == 0)
252
            {
Chris Cantwell's avatar
Chris Cantwell committed
253 254
                pr = 1;
                ps = 3;
255
            }
Chris Cantwell's avatar
Chris Cantwell committed
256
            else
257
            {
Chris Cantwell's avatar
Chris Cantwell committed
258 259
                pr = 0;
                ps = 4;
260
            }
Chris Cantwell's avatar
Chris Cantwell committed
261 262 263 264 265 266
            break;
        case (7):
            if (i == 0)
            {
                pr = 1;
                ps = 2;
267
            }
Chris Cantwell's avatar
Chris Cantwell committed
268
            else
269
            {
Chris Cantwell's avatar
Chris Cantwell committed
270 271
                pr = 0;
                ps = 3;
272
            }
Chris Cantwell's avatar
Chris Cantwell committed
273 274 275 276 277 278 279 280 281 282
            break;
        case (8):
            pr = 0;
            ps = 2;
            break;
        case (9):
            pr = 0;
            ps = 1;
            break;
        default:
283
            ASSERTL0(false,"Error in 5-point triangulation in ThreeSimilar");
Chris Cantwell's avatar
Chris Cantwell committed
284 285 286 287 288 289 290 291 292 293 294
            break;
    }
}

vector<IsoSharedPtr> ProcessIsoContour::ExtractContour(
        const int fieldid,
        const NekDouble val)
{
    vector<IsoSharedPtr> returnval;

    int coordim = m_f->m_exp[0]->GetCoordim(0);
295
    int nfields = m_f->m_fieldPts->GetNFields() + coordim;
Chris Cantwell's avatar
Chris Cantwell committed
296 297 298 299 300

    ASSERTL0(coordim == 3,
             "This methods is currently only set up for 3D fields");
    ASSERTL1(coordim + fieldid < nfields,
             "field id is larger than number contained in FieldPts");
301 302
    Array<OneD, Array<OneD, NekDouble> > fields;
    m_f->m_fieldPts->GetPts(fields);
Chris Cantwell's avatar
Chris Cantwell committed
303 304 305 306 307 308 309 310 311 312 313 314 315

    Array<OneD, NekDouble> c = fields[coordim + fieldid];

    int i, j, k, ii, jj, kk, r, s, n, counter, boolean;
    Array<OneD, Array<OneD, NekDouble> > intfields(nfields);
    intfields[0] = Array<OneD, NekDouble>(5*nfields);
    for(i = 1; i < nfields; ++i)
    {
        intfields[i] = intfields[i-1] + 5;
    }
    Array<OneD, NekDouble> cx = intfields[0];
    Array<OneD, NekDouble> cy = intfields[1];
    Array<OneD, NekDouble> cz = intfields[2];
316

317 318 319
    vector<Array<OneD, int> > ptsConn;
    m_f->m_fieldPts->GetConnectivity(ptsConn);
    for(int zone = 0; zone < ptsConn.size(); ++zone)
Chris Cantwell's avatar
Chris Cantwell committed
320 321 322 323 324
    {
        IsoSharedPtr iso;

        iso = MemoryManager<Iso>::AllocateSharedPtr(nfields-3);

325
        int nelmt = ptsConn[zone].num_elements()
Chris Cantwell's avatar
Chris Cantwell committed
326 327
            /(coordim+1);

328
        Array<OneD, int> conn = ptsConn[zone];
Chris Cantwell's avatar
Chris Cantwell committed
329 330 331 332 333 334 335 336

        for (n = 0, i = 0; i < nelmt; ++i)
        {
            // check to see if val is between vertex values
            if(!(((c[conn[i*4]]  >val)&&(c[conn[i*4+1]]>val)&&
                  (c[conn[i*4+2]]>val)&&(c[conn[i*4+3]]>val))||
                 ((c[conn[i*4  ]]<val)&&(c[conn[i*4+1]]<val)&&
                  (c[conn[i*4+2]]<val)&&(c[conn[i*4+3]]<val))))
337
            {
Chris Cantwell's avatar
Chris Cantwell committed
338 339 340 341 342 343

                // loop over all edges and interpolate if
                // contour is between vertex values
                for (counter = 0, j=0; j<=2; j++)
                {
                    for (k=j+1; k<=3; k++)
344
                    {
Chris Cantwell's avatar
Chris Cantwell committed
345 346 347 348
                        if (((c[conn[i*4+j]]>=val)&&
                             (val>=c[conn[i*4+k]]))||
                            ((c[conn[i*4+j]]<=val)&&
                             (val<=c[conn[i*4+k]])))
349
                        {
Chris Cantwell's avatar
Chris Cantwell committed
350 351 352 353 354 355 356
                            // linear interpolation of fields
                            // (and coords).
                            NekDouble cj = c[conn[i*4+j]];
                            NekDouble ck = c[conn[i*4+k]];
                            NekDouble factor =  (val-cj)/(ck-cj);

                            if(fabs(cj-ck) > 1e-12)
357
                            {
Chris Cantwell's avatar
Chris Cantwell committed
358 359
                                // interpolate coordinates and fields
                                for(int f = 0; f < nfields; ++f)
360
                                {
Chris Cantwell's avatar
Chris Cantwell committed
361
                                    if(counter == 5)
362
                                    {
Chris Cantwell's avatar
Chris Cantwell committed
363
                                        ASSERTL0(false,"Counter is 5");
364
                                    }
Chris Cantwell's avatar
Chris Cantwell committed
365 366 367 368
                                    intfields[f][counter] =
                                        fields[f][conn[4*i+j]] +
                                        factor*(fields[f][conn[4*i+k]] -
                                                fields[f][conn[4*i+j]]);
369
                                }
Chris Cantwell's avatar
Chris Cantwell committed
370
                                ++counter;
371 372
                            }
                        }
Chris Cantwell's avatar
Chris Cantwell committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
                    }
                }

                switch(counter)
                {
                case 3:
                    n+=1;
                    iso->resize_fields(3*n);

                    for(j = 0; j < 3; ++j)
                    {
                        iso->set_fields(3*(n-1)+j,intfields,j);

                    }
                    break;
                case 4:
                    n+=2;
                    iso->resize_fields(3*n);

                    for(j = 0; j < 3; ++j)
                    {
                        iso->set_fields(3*(n-2)+j,intfields,j);
                        iso->set_fields(3*(n-1)+j,intfields,j+1);

                    }
                    break;
                case 5:
                    n+=1;
                    iso->resize_fields(3*n);

                    boolean=0;
                    for (ii=0;ii<=2;ii++)
                    {
                        for (jj=ii+1;jj<=3;jj++)
407
                        {
Chris Cantwell's avatar
Chris Cantwell committed
408
                            for (kk=jj+1;kk<=4;kk++)
409
                            {
Chris Cantwell's avatar
Chris Cantwell committed
410 411 412 413 414 415 416 417 418
                                if((((cx[ii]-cx[jj])==0.0)&&
                                    ((cy[ii]-cy[jj])==0.0)&&
                                    ((cz[ii]-cz[jj])==0.0))&&
                                   (((cx[ii]-cx[kk])==0.0)&&
                                    ((cy[ii]-cy[kk])==0.0)&&
                                    ((cz[ii]-cz[kk])==0.0)))
                                {
                                    boolean+=1;
                                    ThreeSimilar (ii,jj,kk,r,s);
419

Chris Cantwell's avatar
Chris Cantwell committed
420 421 422 423 424
                                    iso->set_fields(3*(n-1)  ,intfields,ii);
                                    iso->set_fields(3*(n-1)+1,intfields,r);
                                    iso->set_fields(3*(n-1)+2,intfields,s);
                                }
                                else
425
                                {
Chris Cantwell's avatar
Chris Cantwell committed
426
                                    boolean+=0;
427 428 429 430 431
                                }
                            }
                        }
                    }

Chris Cantwell's avatar
Chris Cantwell committed
432 433 434
                    if (boolean==0)
                    {
                        TwoPairs (cx,cy,cz,r);
435

Chris Cantwell's avatar
Chris Cantwell committed
436 437 438 439 440 441 442
                        iso->set_fields(3*(n-1)  ,intfields,0);
                        iso->set_fields(3*(n-1)+1,intfields,2);
                        iso->set_fields(3*(n-1)+2,intfields,r);
                    }
                    break;
                }
            }
443
        }
Chris Cantwell's avatar
Chris Cantwell committed
444 445 446 447 448 449
        iso->set_ntris(n);

        // condense the information in this elemental extraction.
        iso->condense();
        returnval.push_back(iso);
    }
450

Chris Cantwell's avatar
Chris Cantwell committed
451 452 453 454 455 456
    return returnval;
}

// reset m_fieldPts with values from iso;
void ProcessIsoContour::ResetFieldPts(vector<IsoSharedPtr> &iso)
{
457
    int nfields = m_f->m_fieldPts->GetNFields() + m_f->m_fieldPts->GetDim();
Chris Cantwell's avatar
Chris Cantwell committed
458 459

    // set output to triangle block.
460
    m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsTriBlock);
Chris Cantwell's avatar
Chris Cantwell committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

    Array<OneD, Array<OneD, NekDouble> > newfields(nfields);

    // count up number of points
    int npts = 0;
    for(int i =0; i < iso.size(); ++i)
    {
        npts += iso[i]->get_nvert();
    }

    // set up coordinate in new field
    newfields[0] = Array<OneD, NekDouble>(npts);
    newfields[1] = Array<OneD, NekDouble>(npts);
    newfields[2] = Array<OneD, NekDouble>(npts);

    int cnt = 0;
    for(int i =0; i < iso.size(); ++i)
    {
        for(int j = 0; j < iso[i]->get_nvert(); ++j,++cnt)
480
        {
Chris Cantwell's avatar
Chris Cantwell committed
481 482 483 484 485
            newfields[0][cnt] = iso[i]->get_x(j);
            newfields[1][cnt] = iso[i]->get_y(j);
            newfields[2][cnt] = iso[i]->get_z(j);
        }
    }
486 487


Chris Cantwell's avatar
Chris Cantwell committed
488 489 490 491
    // set up fields
    for(int f = 0; f < nfields-3; ++f)
    {
        newfields[f+3] = Array<OneD, NekDouble>(npts);
492

Chris Cantwell's avatar
Chris Cantwell committed
493 494 495 496
        cnt = 0;
        for(int i =0; i < iso.size(); ++i)
        {
            for(int j = 0; j < iso[i]->get_nvert(); ++j,++cnt)
497
            {
Chris Cantwell's avatar
Chris Cantwell committed
498
                newfields[f+3][cnt] = iso[i]->get_fields(f,j);
499
            }
Chris Cantwell's avatar
Chris Cantwell committed
500 501
        }
    }
502

503
    m_f->m_fieldPts->SetPts(newfields);
504

Chris Cantwell's avatar
Chris Cantwell committed
505
    // set up connectivity data.
506 507
    vector<Array<OneD, int> > ptsConn;
    m_f->m_fieldPts->GetConnectivity(ptsConn);
Chris Cantwell's avatar
Chris Cantwell committed
508
    cnt = 0;
509
    ptsConn.clear();
Chris Cantwell's avatar
Chris Cantwell committed
510 511 512 513
    for(int i =0; i < iso.size(); ++i)
    {
        int ntris = iso[i]->get_ntris();
        Array<OneD, int> conn(ntris*3);
514

Chris Cantwell's avatar
Chris Cantwell committed
515 516 517 518
        for(int j = 0; j < 3*ntris; ++j)
        {
            conn[j] = cnt + iso[i]->get_vid(j);
        }
519
        ptsConn.push_back(conn);
Chris Cantwell's avatar
Chris Cantwell committed
520 521
        cnt += iso[i]->get_nvert();
    }
522
    m_f->m_fieldPts->SetConnectivity(ptsConn);
Chris Cantwell's avatar
Chris Cantwell committed
523
}
524

Chris Cantwell's avatar
Chris Cantwell committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
void Iso::condense(void)
{
    register int i,j,cnt;
    IsoVertex v;
    vector<IsoVertex> vert;
    vector<IsoVertex>::iterator pt;

    if(!m_ntris) return;

    if(m_condensed) return;
    m_condensed = true;

    vert.reserve(m_ntris/6);

    m_vid = Array<OneD, int>(3*m_ntris);

    // fill first 3 points and initialise fields
    v.m_fields.resize(m_fields.size());
    for(cnt =0, i = 0; i < 3; ++i)
    {
        v.m_x = m_x[i];
        v.m_y = m_y[i];
        v.m_z = m_z[i];
        for(int f = 0; f < m_fields.size(); ++f)
        {
            v.m_fields[f] = m_fields[f][i];
551
        }
Chris Cantwell's avatar
Chris Cantwell committed
552 553 554 555 556 557 558 559 560
        v.m_id = cnt;
        vert.push_back(v);
        m_vid[i] = v.m_id;
        ++cnt;
    }

    for(i = 1; i < m_ntris; ++i)
    {
        for(j = 0; j < 3; ++j)
561
        {
Chris Cantwell's avatar
Chris Cantwell committed
562 563 564 565 566 567
            v.m_x = m_x[3*i+j];
            v.m_y = m_y[3*i+j];
            v.m_z = m_z[3*i+j];

            pt = find(vert.begin(),vert.end(),v);
            if(pt != vert.end())
568
            {
Chris Cantwell's avatar
Chris Cantwell committed
569 570 571 572 573 574
                m_vid[3*i+j] = pt[0].m_id;
            }
            else
            {
                v.m_id = cnt;

575 576
                for(int f = 0; f < m_fields.size(); ++f)
                {
Chris Cantwell's avatar
Chris Cantwell committed
577
                    v.m_fields[f] = m_fields[f][3*i+j];
578
                }
Chris Cantwell's avatar
Chris Cantwell committed
579

580
                vert.push_back(v);
Chris Cantwell's avatar
Chris Cantwell committed
581 582

                m_vid[3*i+j] = v.m_id;
583 584
                ++cnt;
            }
Chris Cantwell's avatar
Chris Cantwell committed
585 586 587 588 589 590 591 592 593 594 595 596
        }
    }

    // remove elements with multiple vertices
    for(i = 0,cnt=0; i < m_ntris;)
    {
        if((m_vid[3*i]  ==m_vid[3*i+1])||
           (m_vid[3*i]  ==m_vid[3*i+2])||
           (m_vid[3*i+1]==m_vid[3*i+2]))
        {
            cnt++;
            for(j = 3*i; j < 3*(m_ntris-1); ++j)
597
            {
Chris Cantwell's avatar
Chris Cantwell committed
598
                m_vid[j] = m_vid[j+3];
599
            }
Chris Cantwell's avatar
Chris Cantwell committed
600 601 602 603 604
            m_ntris--;
        }
        else
        {
            ++i;
605
        }
Chris Cantwell's avatar
Chris Cantwell committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    }

    m_nvert  = vert.size();

    m_x.resize(m_nvert);
    m_y.resize(m_nvert);
    m_z.resize(m_nvert);

    for(int f = 0; f < m_fields.size(); ++f)
    {
        m_fields[f].resize(m_nvert);
    }

    for(i = 0; i < m_nvert; ++i)
    {
        m_x[i] = vert[i].m_x;
        m_y[i] = vert[i].m_y;
        m_z[i] = vert[i].m_z;
        for(int f = 0; f < m_fields.size(); ++f)
        {
            m_fields[f][i] = vert[i].m_fields[f];
        }
    }
}


NekDouble SQ_PNT_TOL=1e-16;

// define == if point is within 1e-4
bool operator == (const IsoVertex& x, const IsoVertex& y)
{
    return ((x.m_x-y.m_x)*(x.m_x-y.m_x) + (x.m_y-y.m_y)*(x.m_y-y.m_y) +
            (x.m_z-y.m_z)*(x.m_z-y.m_z) < SQ_PNT_TOL)? true:false;
}

// define != if point is outside 1e-4
bool operator != (const IsoVertex& x, const IsoVertex& y)
{
    return ((x.m_x-y.m_x)*(x.m_x-y.m_x) + (x.m_y-y.m_y)*(x.m_y-y.m_y) +
            (x.m_z-y.m_z)*(x.m_z-y.m_z) < SQ_PNT_TOL)? 0:1;
}
647 648


Chris Cantwell's avatar
Chris Cantwell committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
bool same(NekDouble x1, NekDouble y1, NekDouble z1,
          NekDouble x2, NekDouble y2, NekDouble z2)
{
    if((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2) < SQ_PNT_TOL)
    {
        return true;
    }

    return false;
}

void Iso::globalcondense(vector<IsoSharedPtr> &iso)
{
    int    i,j,n;
    int    nvert,nelmt;
    int    niso=iso.size();
    int    id1,id2;
    Array<OneD, Array<OneD, int> > vidmap;

    if(m_condensed) return;
    m_condensed = true;

    vidmap = Array<OneD, Array<OneD, int> > (niso);
672

Chris Cantwell's avatar
Chris Cantwell committed
673 674 675 676
    m_ntris = 0;
    for(i = 0; i < niso; ++i)
    {
        if(iso[i]->m_ntris)
677
        {
Chris Cantwell's avatar
Chris Cantwell committed
678
            m_ntris += iso[i]->m_ntris;
679
        }
Chris Cantwell's avatar
Chris Cantwell committed
680 681 682 683 684 685 686 687
    }

    m_vid = Array<OneD, int>(3*m_ntris);

    m_nvert = 0;
    for(i = 0; i < niso; ++i)
    {
        if(iso[i]->m_ntris)
688
        {
Chris Cantwell's avatar
Chris Cantwell committed
689
            m_nvert += iso[i]->m_nvert;
690
        }
Chris Cantwell's avatar
Chris Cantwell committed
691
    }
692

Chris Cantwell's avatar
Chris Cantwell committed
693 694
    vector< vector<int> > isocon;
    isocon.resize(niso);
695

Chris Cantwell's avatar
Chris Cantwell committed
696 697 698 699 700 701 702
    // identify which iso are connected by at least one point;
    // find min x,y,z and max x,y,z and see if overlap to select
    // which zones should be connected
    {
        vector<Array<OneD, NekDouble> > sph(niso);
        Array<OneD, NekDouble> rng(6);
        for(i = 0; i < niso; ++i)
703
        {
Chris Cantwell's avatar
Chris Cantwell committed
704 705 706 707 708 709 710 711
            sph[i] = Array<OneD, NekDouble>(4);

            // find max and min of isocontour
            rng[0] = rng[3] = iso[i]->m_x[0];
            rng[1] = rng[4] = iso[i]->m_x[1];
            rng[2] = rng[5] = iso[i]->m_x[2];

            for(id1 = 1; id1 < iso[i]->m_nvert;++id1)
712
            {
Chris Cantwell's avatar
Chris Cantwell committed
713 714 715 716 717 718 719
                rng[0] = min(rng[0],iso[i]->m_x[i]);
                rng[1] = min(rng[1],iso[i]->m_y[i]);
                rng[2] = min(rng[2],iso[i]->m_z[i]);

                rng[3] = max(rng[3],iso[i]->m_x[i]);
                rng[4] = max(rng[4],iso[i]->m_y[i]);
                rng[5] = max(rng[5],iso[i]->m_z[i]);
720
            }
Chris Cantwell's avatar
Chris Cantwell committed
721 722 723 724 725 726 727 728 729 730

            // centroid
            sph[i][0] = (rng[3]+rng[0])/2.0;
            sph[i][1] = (rng[4]+rng[1])/2.0;
            sph[i][2] = (rng[5]+rng[2])/2.0;

            // radius;
            sph[i][3] = sqrt((rng[3]-rng[0])*(rng[3]-rng[0]) +
                             (rng[4]-rng[1])*(rng[4]-rng[1]) +
                             (rng[5]-rng[2])*(rng[5]-rng[2]));
731 732
        }

Chris Cantwell's avatar
Chris Cantwell committed
733
        for(i = 0; i < niso; ++i)
734
        {
Chris Cantwell's avatar
Chris Cantwell committed
735
            for(j = i+1; j < niso; ++j)
736
            {
Chris Cantwell's avatar
Chris Cantwell committed
737 738 739 740 741 742
                NekDouble diff=sqrt((sph[i][0]-sph[j][0])*(sph[i][0]-sph[j][0])+
                          (sph[i][1]-sph[j][1])*(sph[i][1]-sph[j][1])+
                          (sph[i][2]-sph[j][2])*(sph[i][2]-sph[j][2]));

                // if centroid is closer than added radii
                if(diff < sph[i][3] + sph[j][3])
743
                {
Chris Cantwell's avatar
Chris Cantwell committed
744
                    isocon[i].push_back(j);
745 746
                }
            }
Chris Cantwell's avatar
Chris Cantwell committed
747 748 749 750 751 752 753 754 755 756
        }
    }


    for(i = 0; i < niso; ++i)
    {
        vidmap[i] = Array<OneD, int>(iso[i]->m_nvert,-1);
    }
    nvert = 0;
    // identify which vertices are connected to tolerance
757
    cout << "GlobalCondense: Matching Vertices [" << endl << flush;
Chris Cantwell's avatar
Chris Cantwell committed
758
    int cnt = 0;
759
    // count up amount of checking to be done
760
    NekDouble totpts = 0; 
761 762 763 764 765
    for(i = 0; i < niso; ++i)
    {
        for(n = 0; n < isocon[i].size(); ++n)
        {
            int con = isocon[i][n];
766
            totpts += iso[i]->m_nvert*iso[con]->m_nvert;
767 768
        }
    }
769
    int totchk  = totpts/50;
770
    int cnt_out = 0; 
Chris Cantwell's avatar
Chris Cantwell committed
771 772 773 774 775
    for(i = 0; i < niso; ++i)
    {
        for(n = 0; n < isocon[i].size(); ++n)
        {
            int con = isocon[i][n];
776
            for(id1 = 0; id1 < iso[i]->m_nvert; ++id1)
777
            {
778
                for(id2 = 0; id2 < iso[con]->m_nvert; ++id2, ++cnt)
779
                {
780
                    if(cnt%totchk == 0)
781
                    {
782 783
                        cout <<cnt_out << "%" << '\r' << flush;
                        cnt_out +=2; 
784 785
                    }

786
                    if((vidmap[con][id2] == -1)||(vidmap[i][id1] == -1))
787
                    {
Chris Cantwell's avatar
Chris Cantwell committed
788 789 790
                        if(same(iso[i]->m_x[id1],  iso[i]->m_y[id1],
                                iso[i]->m_z[id1],  iso[con]->m_x[id2],
                                iso[con]->m_y[id2],iso[con]->m_z[id2]))
791
                        {
Chris Cantwell's avatar
Chris Cantwell committed
792 793
                            if((vidmap[i][id1] == -1) &&
                               (vidmap[con][id2] != -1))
794
                            {
Chris Cantwell's avatar
Chris Cantwell committed
795
                                vidmap[i][id1] = vidmap[con][id2];
796
                            }
Chris Cantwell's avatar
Chris Cantwell committed
797 798
                            else if((vidmap[con][id2] == -1) &&
                                    (vidmap[i][id1] != -1))
799
                            {
Chris Cantwell's avatar
Chris Cantwell committed
800 801 802 803 804 805
                                vidmap[con][id2] = vidmap[i][id1];
                            }
                            else if((vidmap[con][id2] == -1) &&
                                    (vidmap[i][id1] == -1))
                            {
                                vidmap[i][id1] = vidmap[con][id2] = nvert++;
806 807 808 809
                            }
                        }
                    }
                }
Chris Cantwell's avatar
Chris Cantwell committed
810 811
            }
        }
812

Chris Cantwell's avatar
Chris Cantwell committed
813 814 815 816 817
        for(id1 = 0; id1 < iso[i]->m_nvert;++id1)
        {
            if(vidmap[i][id1] == -1)
            {
                vidmap[i][id1] = nvert++;
818
            }
Chris Cantwell's avatar
Chris Cantwell committed
819 820
        }
    }
821
    cout <<endl << "]"<<endl;
Chris Cantwell's avatar
Chris Cantwell committed
822 823 824 825 826 827 828 829 830
    m_nvert = nvert;

    nelmt = 0;
    // reset m_vid;
    for(n = 0; n < niso; ++n)
    {
        for(i = 0; i < iso[n]->m_ntris; ++i,nelmt++)
        {
            for(j=0; j < 3;++j)
831
            {
Chris Cantwell's avatar
Chris Cantwell committed
832
                m_vid[3*nelmt+j] = vidmap[n][iso[n]->m_vid[3*i+j]];
833
            }
Chris Cantwell's avatar
Chris Cantwell committed
834 835 836 837 838 839 840 841
        }
    }

    m_ntris = nelmt;

    m_x.resize(m_nvert);
    m_y.resize(m_nvert);
    m_z.resize(m_nvert);
842

Chris Cantwell's avatar
Chris Cantwell committed
843 844 845 846 847
    m_fields.resize(iso[0]->m_fields.size());
    for(i = 0; i < iso[0]->m_fields.size(); ++i)
    {
        m_fields[i].resize(m_nvert);
    }
848 849


Chris Cantwell's avatar
Chris Cantwell committed
850 851 852 853 854 855 856 857 858 859
    // reset coordinate and fields.
    for(n = 0; n < niso; ++n)
    {
        for(i = 0; i < iso[n]->m_nvert; ++i)
        {
            m_x[vidmap[n][i]] = iso[n]->m_x[i];
            m_y[vidmap[n][i]] = iso[n]->m_y[i];
            m_z[vidmap[n][i]] = iso[n]->m_z[i];

            for(j = 0; j < m_fields.size(); ++j)
860
            {
Chris Cantwell's avatar
Chris Cantwell committed
861
                m_fields[j][vidmap[n][i]] = iso[n]->m_fields[j][i];
862
            }
Chris Cantwell's avatar
Chris Cantwell committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
        }
    }
}

void Iso::smooth(int n_iter, NekDouble lambda, NekDouble mu)
{
    int   iter,i,j;
    NekDouble del_v[3];
    NekDouble w;
    Array<OneD, NekDouble>  xtemp, ytemp, ztemp;
    vector< vector<int> > adj,vertcon;
    vector<int>::iterator iad;
    vector<int>::iterator ipt;

    // determine elements around each vertex
    vertcon.resize(m_nvert);
    for(i = 0; i < m_ntris; ++i)
    {
        for(j = 0; j < 3; ++j)
        {
            vertcon[m_vid[3*i+j]].push_back(i);
        }
    }
886

Chris Cantwell's avatar
Chris Cantwell committed
887 888 889 890 891 892 893 894
    // determine vertices around each vertex
    adj.resize(m_nvert);

    for(i =0; i < m_nvert; ++i)
    {
        for(ipt = vertcon[i].begin(); ipt != vertcon[i].end(); ++ipt)
        {
            for(j = 0; j < 3; ++j)
895
            {
Chris Cantwell's avatar
Chris Cantwell committed
896 897
                // make sure not adding own vertex
                if(m_vid[3*(*ipt)+j] != i)
898
                {
Chris Cantwell's avatar
Chris Cantwell committed
899 900
                    // check to see if vertex has already been added
                    for(iad = adj[i].begin(); iad != adj[i].end();++iad)
901
                    {
Chris Cantwell's avatar
Chris Cantwell committed
902
                        if(*iad == (m_vid[3*(*ipt)+j])) break;
903
                    }
904

Chris Cantwell's avatar
Chris Cantwell committed
905
                    if(iad == adj[i].end())
906
                    {
Chris Cantwell's avatar
Chris Cantwell committed
907
                        adj[i].push_back(m_vid[3*(*ipt)+j]);
908 909 910
                    }
                }
            }
911
        }
Chris Cantwell's avatar
Chris Cantwell committed
912
    }
913

Chris Cantwell's avatar
Chris Cantwell committed
914 915 916 917 918 919 920 921 922
    xtemp = Array<OneD, NekDouble>(m_nvert);
    ytemp = Array<OneD, NekDouble>(m_nvert);
    ztemp = Array<OneD, NekDouble>(m_nvert);

    // smooth each point
    for (iter=0;iter<n_iter;iter++)
    {
        // compute first weighted average
        for(i=0;i< m_nvert;++i)
923
        {
Chris Cantwell's avatar
Chris Cantwell committed
924 925 926 927 928
            w = 1.0/(NekDouble)(adj[i].size());

            del_v[0] = del_v[1] = del_v[2] = 0.0;

            for(iad = adj[i].begin(); iad != adj[i].end(); ++iad)
929
            {
Chris Cantwell's avatar
Chris Cantwell committed
930 931 932
                del_v[0] =  del_v[0] + (m_x[*iad]-m_x[i])*w;
                del_v[1] =  del_v[1] + (m_y[*iad]-m_y[i])*w;
                del_v[2] =  del_v[2] + (m_z[*iad]-m_z[i])*w;
933 934
            }

Chris Cantwell's avatar
Chris Cantwell committed
935 936 937 938
            xtemp[i] = m_x[i] + del_v[0] * lambda;
            ytemp[i] = m_y[i] + del_v[1] * lambda;
            ztemp[i] = m_z[i] + del_v[2] * lambda;
        }
939

Chris Cantwell's avatar
Chris Cantwell committed
940 941 942 943 944 945
        // compute second weighted average
        for(i=0;i< m_nvert;++i)
        {

            w = 1.0/(NekDouble)(adj[i].size());
            del_v[0] = del_v[1] = del_v[2] = 0;
946

Chris Cantwell's avatar
Chris Cantwell committed
947
            for(iad = adj[i].begin(); iad != adj[i].end(); ++iad)
948
            {
Chris Cantwell's avatar
Chris Cantwell committed
949 950 951
                del_v[0] =  del_v[0] + (m_x[*iad]-m_x[i])*w;
                del_v[1] =  del_v[1] + (m_y[*iad]-m_y[i])*w;
                del_v[2] =  del_v[2] + (m_z[*iad]-m_z[i])*w;
952
            }
Chris Cantwell's avatar
Chris Cantwell committed
953 954 955 956

            m_x[i] = xtemp[i] + del_v[0] * mu;
            m_y[i] = ytemp[i] + del_v[1] * mu;
            m_z[i] = ztemp[i] + del_v[2] * mu;
957 958 959 960
        }
    }
}

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

    void Iso::separate_regions(vector<IsoSharedPtr> &sep_iso, int minsize)
    {
        int i,j,k,id;
        Array<OneD, vector<int> >vertcon(m_nvert);
        vector<int>::iterator ipt;
        list<int> tocheck;  
        list<int>::iterator cid;

        Array<OneD, bool> viddone(m_nvert,false);
        
        // make list of connecting tris around each vertex
        for(i = 0; i < m_ntris; ++i)
        {
            for(j = 0; j < 3; ++j)
            {
                vertcon[m_vid[3*i+j]].push_back(i);
            }
        }
        
        Array<OneD, int> vidregion(m_nvert,-1);
  
        int nregions = -1;

985
        cout << "Identifying separate regions [." << flush ;
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
        // check all points are assigned;
        for(k = 0; k < m_nvert; ++k)
        {
            if(vidregion[k] == -1)
            {
                vidregion[k] = ++nregions;
      
                // find all elmts around this.. vertex  that need to be checked
                for(ipt = vertcon[k].begin(); ipt != vertcon[k].end(); ++ipt)
                {
                    for(i = 0; i < 3; ++i)
                    {
                        if(vidregion[id = m_vid[3*(ipt[0])+i]] == -1)
                        {
                            tocheck.push_back(id);
                            vidregion[id] = nregions;
                        }
                    }
                }
                viddone[k] = 1;
                
                // check all other neighbouring vertices
                while(tocheck.size())
                {
                    cid = tocheck.begin();
                    while(cid != tocheck.end())
                    {
                        if(!viddone[*cid])
                        {
                            for(ipt = vertcon[*cid].begin(); ipt != vertcon[*cid].end(); ++ipt)
                            {
                                for(i = 0; i < 3; ++i)
                                {
                                    if(vidregion[id = m_vid[3*(ipt[0])+i]] == -1)
                                    {
                                        tocheck.push_back(id);
                                        vidregion[id] = nregions;
                                    }
                                }
                            }
                            viddone[*cid] = 1;
                            ++cid;
                            tocheck.pop_front();
                        }
                    }
                }
            }
        }
        nregions++;
1035 1036

        cout << "]" << endl <<  flush ;
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
        
        Array<OneD, int> nvert(nregions,0);
        Array<OneD, int> nelmt(nregions,0);

        // count nverts
        for(i = 0; i < m_nvert; ++i)
        {
            nvert[vidregion[i]] +=1;
        }

        // count nelmts
        for(i = 0; i < m_ntris; ++i)
        {
            nelmt[vidregion[m_vid[3*i]]] +=1;
        }

        Array<OneD, int> vidmap(m_nvert);
        // generate new list of isocontour
        for(int n = 0; n < nregions; ++n)
        {
            if(nelmt[n] > minsize)
            {
                int nfields = m_fields.size();
                IsoSharedPtr iso = MemoryManager<Iso>::AllocateSharedPtr(nfields);
                
                iso->m_ntris = nelmt[n];
                iso->m_vid = Array<OneD, int>(3*nelmt[n]);
                
                iso->m_nvert = nvert[n];
                iso->m_x.resize(nvert[n]);
                iso->m_y.resize(nvert[n]);
                iso->m_z.resize(nvert[n]);
                
                iso->m_fields.resize(nfields);
                for(i = 0; i < nfields; ++i)
                {
                    iso->m_fields[i].resize(nvert[n]);
                }


                int cnt = 0; 
                // generate vid map; 
                Vmath::Zero(m_nvert,vidmap,1);
                for(i = 0; i < m_nvert; ++i)
                {
                    if(vidregion[i] == n)
                    {
                        vidmap[i] = cnt++;
                    }
                }

                cnt = 0; 
                for(i = 0; i < m_ntris; ++i)
                {
                    if(vidregion[m_vid[3*i]] == n)
                    {
                        for(j = 0; j < 3; ++j)
                        {
                            iso->m_vid[3*cnt+j] = vidmap[m_vid[3*i+j]];
                            
                            iso->m_x[vidmap[m_vid[3*i+j]]] = m_x[m_vid[3*i+j]];
                            iso->m_y[vidmap[m_vid[3*i+j]]] = m_y[m_vid[3*i+j]];
                            iso->m_z[vidmap[m_vid[3*i+j]]] = m_z[m_vid[3*i+j]];

                            for(k = 0; k < nfields; ++k)
                            {
                                iso->m_fields[k][vidmap[m_vid[3*i+j]]] = m_fields[k][m_vid[3*i+j]];
                            }
                        }
1106
                        cnt++;
1107 1108
                    }
                }
1109

1110
                WARNINGL0(cnt == nelmt[n],"Number of elements do not match");
1111 1112 1113 1114
                sep_iso.push_back(iso);
            }
        }
    }
Chris Cantwell's avatar
Chris Cantwell committed
1115 1116 1117
}
}

1118