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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 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 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 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 985 986 987 988 989 990
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLSDynaReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkLSDynaPartCollection.h"
#include "vtkLSDynaPart.h"
#include "LSDynaMetaData.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkFloatArray.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkStringArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnstructuredGrid.h"
#include <algorithm>
#include <vector>
#include <list>
//-----------------------------------------------------------------------------
class vtkLSDynaPartCollection::LSDynaPartStorage
{
protected:
//---------------------------------------------------------------------------
//stores the number of cells for a given part
//this struct is meant to resemble a run length encoding style of storage
//of mapping cell ids to the part that holds those cells
struct PartInfo
{
PartInfo(vtkLSDynaPart *p, const int& type, const vtkIdType& pId,
const vtkIdType& start, const vtkIdType& npts):
numCells(1), //we are inserting the first cell when we create this so start with 1
startId(start),
cellStructureSize(npts), //start with number of points in first cell
partId(pId)
{
//we store the part id our selves because we can have null parts
//if the user has disabled reading that part
this->part = p;
if(this->part)
{
this->part->SetPartType(type);
}
}
vtkIdType numCells; //number of cells in this continuous block
vtkIdType startId; //the global index to start of this block
vtkIdType cellStructureSize; //stores the size of the cell array for this section
vtkIdType partId; //id of the part this block represents, because the part can be NULL
vtkLSDynaPart *part;
};
//---------------------------------------------------------------------------
struct PartInsertion
{
PartInsertion():numCellsInserted(0){}
PartInsertion(std::vector<PartInfo> *pInfo):numCellsInserted(0)
{
this->pIt = pInfo->begin();
}
//increments the numCells, and when needed increments to the next part
void inc()
{
++numCellsInserted;
if ( (*pIt).numCells == numCellsInserted)
{
++pIt;
numCellsInserted=0;
}
}
std::vector<PartInfo>::iterator pIt;
vtkIdType numCellsInserted;
};
//---------------------------------------------------------------------------
public:
LSDynaPartStorage(const vtkIdType& numMaterials):
NumParts(numMaterials),PartIteratorLoc(0)
{
//a part represents a single material. A part type is
this->Info = new std::vector<PartInfo>[LSDynaMetaData::NUM_CELL_TYPES];
this->CellInsertionIterators = new PartInsertion[LSDynaMetaData::NUM_CELL_TYPES];
this->Parts = new vtkLSDynaPart*[numMaterials];
for(vtkIdType i=0; i<numMaterials; ++i)
{
this->Parts[i]=NULL;
}
}
~LSDynaPartStorage()
{
for(vtkIdType i=0; i < this->NumParts; ++i)
{
if(this->Parts[i])
{
this->Parts[i]->Delete();
this->Parts[i]=NULL;
}
}
delete[] this->Parts;
delete[] this->CellInsertionIterators;
delete[] this->Info;
}
//---------------------------------------------------------------------------
vtkIdType GetNumParts() const { return NumParts; }
//---------------------------------------------------------------------------
void RegisterCell(const int& partType,const vtkIdType &matId,
const vtkIdType &npts)
{
if(this->Info[partType].size() != 0)
{
PartInfo *info = &this->Info[partType].back();
if(info->partId == matId)
{
//append to this item
++info->numCells;
info->cellStructureSize += npts;
}
else
{
//add a new item
//PartInfo sets the part type!
PartInfo newInfo(this->Parts[matId],partType,matId,
(info->startId + info->numCells), npts);
this->Info[partType].push_back(newInfo);
}
}
else
{
//PartInfo sets the part type!
PartInfo newInfo(this->Parts[matId],partType,matId,0,npts);
this->Info[partType].push_back(newInfo);
}
}
//---------------------------------------------------------------------------
void ConstructPart(const vtkIdType &index,
const std::string &name,
const int &materialId,
const int &numGlobalNodes,
const int &wordSize
)
{
vtkLSDynaPart *p = vtkLSDynaPart::New();
p->InitPart(name,index,materialId,
numGlobalNodes,wordSize);
this->Parts[index] = p;
}
//---------------------------------------------------------------------------
void InitCellInsertion()
{
//we build up an array of cell insertion iterators
//that point to the first element of each part type info
for(int i=0; i < LSDynaMetaData::NUM_CELL_TYPES; ++i)
{
if(this->Info[i].size()>0)
{
PartInsertion partIt(&this->Info[i]);
this->CellInsertionIterators[i] = partIt;
}
}
}
//---------------------------------------------------------------------------
void InsertCell(const int& partType, const int& cellType,
const vtkIdType& npts, vtkIdType conn[8])
{
//get the correct iterator from the array of iterations
if(this->CellInsertionIterators[partType].pIt->part)
{
//only insert the cell if the part is turned on
this->CellInsertionIterators[partType].pIt->part->AddCell(
cellType,npts,conn);
}
this->CellInsertionIterators[partType].inc();
}
//---------------------------------------------------------------------------
bool PartExists(const vtkIdType &index) const
{
if(index<0||index>this->NumParts)
{
return false;
}
return (this->Parts[index]!=NULL && this->Parts[index]->HasCells());
}
//---------------------------------------------------------------------------
vtkLSDynaPart* GetPart(const vtkIdType &index)
{
return this->Parts[index];
}
//---------------------------------------------------------------------------
vtkUnstructuredGrid* GetPartGrid(const vtkIdType &index)
{
return this->Parts[index]->GenerateGrid();
}
//---------------------------------------------------------------------------
void InitPartIteration(const int &partType)
{
for(vtkIdType i=0; i < this->NumParts; ++i)
{
if(this->Parts[i] && this->Parts[i]->PartType() == partType)
{
PartIteratorLoc = i;
this->PartIterator = this->Parts[i];
return;
}
}
//failed to find a part that matches the type
PartIteratorLoc = -1;
this->PartIterator = NULL;
}
//---------------------------------------------------------------------------
bool GetNextPart(vtkLSDynaPart *&part)
{
if(!this->PartIterator)
{
part = NULL;
return false;
}
part=this->PartIterator;
//clear iterator before we search for the next part
vtkIdType pos = this->PartIteratorLoc + 1;
this->PartIterator = NULL;
this->PartIteratorLoc = -1;
//find the next part
for(vtkIdType i=pos; i<this->NumParts;i++)
{
if(this->Parts[i] && this->Parts[i]->PartType() == part->PartType())
{
this->PartIteratorLoc = i;
this->PartIterator = this->Parts[i];
break;
}
}
return true;
}
//---------------------------------------------------------------------------
void AllocateParts()
{
vtkIdType numCells=0,cellLength=0;
for (vtkIdType i=0; i < this->NumParts; ++i)
{
vtkLSDynaPart* part = this->Parts[i];
if(part)
{
bool canBeAllocated = this->GetInfoForPart(part, numCells,cellLength);
if(canBeAllocated)
{
part->AllocateCellMemory(numCells,cellLength);
}
else
{
//this part has no cells allocated to it, so remove it now.
part->Delete();
this->Parts[i] = NULL;
}
}
}
//Only needed when debugging
//this->DumpPartInfo();
}
//---------------------------------------------------------------------------
bool GetInfoForPart(vtkLSDynaPart *part, vtkIdType &numCells,
vtkIdType &cellArrayLength) const
{
//verify that the part is valid
numCells = 0;
cellArrayLength = 0;
bool validPart = part->hasValidType();
if(!validPart)
{
//we return early because an invalid type would
//cause the Info array to be accessed out of bounds
return validPart;
}
//give a part type and a material id
//walk the run length encoding to determe the total size
std::vector<PartInfo>::const_iterator it;
for(it = this->Info[part->PartType()].begin();
it != this->Info[part->PartType()].end(); ++it)
{
const PartInfo *info = &(*it);
if(info->partId== part->GetPartId())
{
validPart = true;
numCells += info->numCells;
cellArrayLength += info->cellStructureSize;
}
}
return validPart;
}
//---------------------------------------------------------------------------
void DumpPartInfo()
{
for(int i=0; i < LSDynaMetaData::NUM_CELL_TYPES;++i)
{
//now lets dump all the part info
std::cout << "For Info index: " << i << std::endl;
std::cout << "We have " << this->Info[i].size() << " info entries" <<std::endl;
std::vector<PartInfo>::const_iterator it;
for(it = this->Info[i].begin();
it != this->Info[i].end(); ++it)
{
const PartInfo *info = &(*it);
if(info->part != NULL)
{
std::cout << "The material id is: " << info->partId << std::endl;
std::cout << "The numCells is: " << info->numCells << std::endl;
std::cout << std::endl;
std::cout << "The Part is :" << std::endl;
info->part->PrintSelf(cout,vtkIndent().GetNextIndent());
std::cout << std::endl;
std::cout << std::endl;
}
}
}
}
//---------------------------------------------------------------------------
void InitCellIteration(const int &partType, int pos=0)
{
this->CellIteratorEnd = this->Info[partType].end();
if(this->Info[partType].size()>0)
{
this->CellIterator = this->Info[partType].begin();
}
else
{
this->CellIterator = this->Info[partType].end();
}
while(pos>0 && this->CellIterator != this->CellIteratorEnd)
{
pos -= (*this->CellIterator).numCells;
if(pos>0)
{
++this->CellIterator;
}
}
}
//---------------------------------------------------------------------------
bool GetNextCellPart(vtkIdType& startId, vtkIdType &numCells,
vtkLSDynaPart *&part)
{
if(this->CellIterator == this->CellIteratorEnd)
{
return false;
}
startId = (*this->CellIterator).startId;
numCells = (*this->CellIterator).numCells;
part = (*this->CellIterator).part;
++this->CellIterator;
return true;
}
//---------------------------------------------------------------------------
void FinalizeTopology()
{
for (vtkIdType i=0; i < this->NumParts; ++i)
{
vtkLSDynaPart* part = this->Parts[i];
if (part && part->HasCells())
{
part->BuildToplogy();
}
else if(part)
{
part->Delete();
this->Parts[i]=NULL;
}
}
}
//---------------------------------------------------------------------------
void DisableDeadCells()
{
for (vtkIdType i=0; i < this->NumParts; ++i)
{
vtkLSDynaPart* part = this->Parts[i];
if (part && part->HasCells())
{
part->DisableDeadCells();
}
}
}
//---------------------------------------------------------------------------
void PrintSelf(ostream &os, vtkIndent indent)
{
for (vtkIdType i=0; i < this->NumParts; ++i)
{
os << indent << "Part Number " << i << std::endl;
if(this->PartExists(i))
{
vtkLSDynaPart* part = this->Parts[i];
part->PrintSelf(os,indent.GetNextIndent());
}
else
{
os << indent.GetNextIndent() << "Does not exist." << std::endl;
}
}
}
protected:
vtkIdType NumParts;
//stores all the parts for this collection.
vtkLSDynaPart **Parts;
//maps cell indexes which are tracked by output type to the part
//Since cells are ordered the same between the cell connectivity data block
//and the state block in the d3plot format we only need to know which part
//the cell is part of.
//This info is constant for each time step
std::vector<PartInfo> *Info;
PartInsertion *CellInsertionIterators;
std::vector<PartInfo>::const_iterator CellIterator,CellIteratorEnd;
vtkLSDynaPart *PartIterator;
vtkIdType PartIteratorLoc;
};
vtkStandardNewMacro(vtkLSDynaPartCollection);
//-----------------------------------------------------------------------------
vtkLSDynaPartCollection::vtkLSDynaPartCollection()
{
this->MetaData = NULL;
this->Storage = NULL;
this->MinIds = NULL;
this->MaxIds = NULL;
}
//-----------------------------------------------------------------------------
vtkLSDynaPartCollection::~vtkLSDynaPartCollection()
{
delete this->Storage;
delete[] this->MinIds;
delete[] this->MaxIds;
this->MetaData = NULL;
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::PrintSelf(ostream &os, vtkIndent indent)
{
//just needs to print all public accessible ivars
this->Superclass::PrintSelf(os, indent);
//number of parts
os << indent << "Number of Parts: " << this->GetNumberOfParts() << std::endl;
//print self for each part
this->Storage->PrintSelf(os,indent.GetNextIndent());
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::InitCollection(LSDynaMetaData *metaData,
vtkIdType* mins, vtkIdType* maxs)
{
delete this->Storage;
delete[] this->MinIds;
delete[] this->MaxIds;
//reserve enough space for the grids. Each node
//will have a part allocated, since we don't know yet
//how the cells map to parts.
this->Storage = new LSDynaPartStorage(metaData->PartIds.size());
this->MinIds = new vtkIdType[LSDynaMetaData::NUM_CELL_TYPES];
this->MaxIds = new vtkIdType[LSDynaMetaData::NUM_CELL_TYPES];
//We only have to map the cell ids between min and max, so we
//skip into the proper place
for(int i=0; i < LSDynaMetaData::NUM_CELL_TYPES;++i)
{
this->MinIds[i]= (mins!=NULL) ? mins[i] : 0;
this->MaxIds[i]= (maxs!=NULL) ? maxs[i] : metaData->NumberOfCells[i];
}
if(metaData)
{
this->MetaData = metaData;
this->BuildPartInfo();
}
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::BuildPartInfo()
{
//we iterate on part materials as those are those are from 1 to num Parts.
//the part ids are the user material ids
std::vector<int>::const_iterator partMIt;
std::vector<int>::const_iterator materialIdIt = this->MetaData->PartIds.begin();
std::vector<int>::const_iterator statusIt = this->MetaData->PartStatus.begin();
std::vector<std::string>::const_iterator nameIt = this->MetaData->PartNames.begin();
for (partMIt = this->MetaData->PartMaterials.begin();
partMIt != this->MetaData->PartMaterials.end();
++partMIt,++statusIt,++nameIt,++materialIdIt)
{
if (*statusIt)
{
//make the index contain a part
this->Storage->ConstructPart((*partMIt)-1,*nameIt,*materialIdIt,
this->MetaData->NumberOfNodes,
this->MetaData->Fam.GetWordSize());
}
}
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::RegisterCellIndexToPart(const int& partType,
const vtkIdType& matId,
const vtkIdType&,
const vtkIdType& npts)
{
this->Storage->RegisterCell(partType,matId-1,npts);
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::AllocateParts( )
{
this->Storage->AllocateParts();
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::InitCellInsertion()
{
this->Storage->InitCellInsertion();
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::InsertCell(const int& partType,
const vtkIdType&,
const int& cellType,
const vtkIdType& npts,
vtkIdType conn[8])
{
this->Storage->InsertCell(partType,cellType,npts,conn);
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::SetCellDeadFlags(const int& partType,
vtkUnsignedCharArray *death,
const int& deadCellsAsGhostArray)
{
//go through and flag each part cell as deleted or not.
//this means breaking up this array into an array for each part
if (!death)
{
return;
}
//The array that passed in from the reader only contains the subset
//of the full data that we are interested in so we don't have to adjust
//any indices
this->Storage->InitCellIteration(partType);
vtkIdType numCells, startId;
vtkLSDynaPart *part;
unsigned char* dead = static_cast<unsigned char*>(death->GetVoidPointer(0));
while(this->Storage->GetNextCellPart(startId,numCells,part))
{
//perfectly valid to have a NULL part being returned
//just skip it as the user doesn't want it loaded.
if(part)
{
part->EnableDeadCells(deadCellsAsGhostArray);
part->SetCellsDeadState(dead,numCells);
}
dead += numCells;
}
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::AddProperty(
const LSDynaMetaData::LSDYNA_TYPES& type, const char* name,
const int& offset, const int& numComps)
{
vtkLSDynaPart* part = NULL;
this->Storage->InitPartIteration(type);
while(this->Storage->GetNextPart(part))
{
if(part)
{
part->AddCellProperty(name,offset,numComps);
}
}
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::FillCellProperties(float *buffer,
const LSDynaMetaData::LSDYNA_TYPES& type, const vtkIdType& startId,
const vtkIdType& numCells, const int& numPropertiesInCell)
{
this->FillCellArray(buffer,type,startId,numCells,numPropertiesInCell);
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::FillCellProperties(double *buffer,
const LSDynaMetaData::LSDYNA_TYPES& type, const vtkIdType& startId,
const vtkIdType& numCells, const int& numPropertiesInCell)
{
this->FillCellArray(buffer,type,startId,numCells,numPropertiesInCell);
}
//-----------------------------------------------------------------------------
template<typename T>
void vtkLSDynaPartCollection::FillCellArray(T *buffer,
const LSDynaMetaData::LSDYNA_TYPES& type, const vtkIdType& startId,
vtkIdType numCells, const int& numPropertiesInCell)
{
//we only need to iterate the array for the subsection we need
T* loc = buffer;
vtkIdType size, globalStartId;
vtkLSDynaPart *part;
this->Storage->InitCellIteration(type,startId);
while(this->Storage->GetNextCellPart(globalStartId,size,part))
{
vtkIdType start = std::max(globalStartId,startId);
vtkIdType end = std::min(globalStartId+size,startId+numCells);
if(end<start)
{
break;
}
vtkIdType is = end - start;
if(part)
{
part->ReadCellProperties(loc,is,numPropertiesInCell);
}
loc += is * numPropertiesInCell;
}
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::ReadCellUserIds(
const LSDynaMetaData::LSDYNA_TYPES& type, const int& status)
{
vtkIdType numCells,numSkipStart,numSkipEnd;
this->GetPartReadInfo(type,numCells,numSkipStart,numSkipEnd);
if(!status)
{
//skip this part type
this->MetaData->Fam.SkipWords(numSkipStart + numCells + numSkipEnd);
return;
}
this->MetaData->Fam.SkipWords(numSkipStart);
vtkIdType numChunks = this->MetaData->Fam.InitPartialChunkBuffering(numCells,1);
vtkIdType startId = 0;
if(this->MetaData->Fam.GetWordSize() == 8 && numCells > 0)
{
for(vtkIdType i=0; i < numChunks; ++i)
{
vtkIdType chunkSize = this->MetaData->Fam.GetNextChunk( LSDynaFamily::Float);
vtkIdType numCellsInChunk = chunkSize;
vtkIdType *buf = this->MetaData->Fam.GetBufferAs<vtkIdType>();
this->FillCellUserId(buf,type,startId,numCellsInChunk);
startId += numCellsInChunk;
}
}
else if (numCells > 0)
{
for(vtkIdType i=0; i < numChunks; ++i)
{
vtkIdType chunkSize = this->MetaData->Fam.GetNextChunk( LSDynaFamily::Float);
vtkIdType numCellsInChunk = chunkSize;
int *buf = this->MetaData->Fam.GetBufferAs<int>();
this->FillCellUserId(buf,type,startId,numCellsInChunk);
startId += numCellsInChunk;
}
}
this->MetaData->Fam.SkipWords(numSkipEnd);
//clear the buffer as it will be very large and not needed
this->MetaData->Fam.ClearBuffer();
}
//-----------------------------------------------------------------------------
template<typename T>
void vtkLSDynaPartCollection::FillCellUserIdArray(T *buffer,
const LSDynaMetaData::LSDYNA_TYPES& type, const vtkIdType& startId,
vtkIdType numCells)
{
const int numWordsPerIdType(this->MetaData->Fam.GetWordSize() / sizeof(T));
//we only need to iterate the array for the subsection we need
T* loc = buffer;
vtkIdType size,globalStartId;
vtkLSDynaPart *part;
this->Storage->InitCellIteration(type,startId);
while(this->Storage->GetNextCellPart(globalStartId,size,part))
{
vtkIdType start = std::max(globalStartId,startId);
vtkIdType end = std::min(globalStartId+size,startId+numCells);
if(end<start)
{
break;
}
vtkIdType is = (end - start)*numWordsPerIdType;
if(part)
{
part->EnableCellUserIds();
for(vtkIdType i=0; i<is; i+=numWordsPerIdType)
{
part->SetNextCellUserIds((vtkIdType)loc[i]);
}
}
//perfectly valid to have a NULL part being returned
//just skip it as the user doesn't want it loaded.
loc+=is;
}
}
//-----------------------------------------------------------------------------
bool vtkLSDynaPartCollection::IsActivePart(const int& id) const
{
return this->Storage->PartExists(id);
}
//-----------------------------------------------------------------------------
vtkUnstructuredGrid* vtkLSDynaPartCollection::GetGridForPart(
const int& index) const
{
return this->Storage->GetPartGrid(index);
}
//-----------------------------------------------------------------------------
int vtkLSDynaPartCollection::GetNumberOfParts() const
{
return static_cast<int>(this->Storage->GetNumParts());
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::DisbleDeadCells()
{
this->Storage->DisableDeadCells();
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::GetPartReadInfo(const int& partType,
vtkIdType& numberOfCells, vtkIdType& numCellsToSkipStart,
vtkIdType& numCellsToSkipEnd) const
{
vtkIdType size = this->MaxIds[partType]-this->MinIds[partType];
if(size<=0)
{
numberOfCells = 0;
//skip everything
numCellsToSkipStart = this->MetaData->NumberOfCells[partType];
numCellsToSkipEnd = 0; //no reason to skip anything else
}
else
{
numberOfCells = size;
numCellsToSkipStart = this->MinIds[partType];
numCellsToSkipEnd = this->MetaData->NumberOfCells[partType] -
(numberOfCells+numCellsToSkipStart);
}
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::FinalizeTopology()
{
this->Storage->FinalizeTopology();
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::ReadPointUserIds(const vtkIdType& numTuples,
const char* name)
{
this->SetupPointPropertyForReading(numTuples,1,name,true,true,false,false);
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::ReadPointProperty(
const vtkIdType& numTuples,
const vtkIdType& numComps,
const char* name,
const bool &isProperty,
const bool& isGeometryPoints,
const bool& isRoadPoints)
{
this->SetupPointPropertyForReading(numTuples,numComps,name,false,isProperty,
isGeometryPoints,isRoadPoints);
}
//-----------------------------------------------------------------------------
void vtkLSDynaPartCollection::SetupPointPropertyForReading(
const vtkIdType& numTuples,
const vtkIdType& numComps,
const char* name,
const bool &isIdType,
const bool &isProperty,
const bool& isGeometryPoints,
const bool& isRoadPoints)
{
if ( !isProperty && !isGeometryPoints && !isRoadPoints)
{
// don't read arrays the user didn't request, just skip them
this->MetaData->Fam.SkipWords(numTuples * numComps);
return;
}
//If this is a geometeric point property it needs to apply
//to the following
//BEAM,SHELL,THICK_SHELL,SOLID,Particles
//if it is road surface it only applies to RigidSurfaceData part types
vtkLSDynaPart* part=NULL;
vtkLSDynaPart **validParts = new vtkLSDynaPart*[this->Storage->GetNumParts()];
vtkIdType idx=0;
if(!isRoadPoints)
{
enum LSDynaMetaData::LSDYNA_TYPES validCellTypes[5] = {
LSDynaMetaData::PARTICLE,
LSDynaMetaData::BEAM,
LSDynaMetaData::SHELL,
LSDynaMetaData::THICK_SHELL,
LSDynaMetaData::SOLID
};
for(int i=0; i<5;++i)
{
this->Storage->InitPartIteration(validCellTypes[i]);
while(this->Storage->GetNextPart(part))
{
part->AddPointProperty(name,numComps,isIdType,isProperty,
isGeometryPoints);
validParts[idx++]=part;
}
}
}
else
{
//is a road point
this->Storage->InitPartIteration(LSDynaMetaData::ROAD_SURFACE);
while(this->Storage->GetNextPart(part))
{
part->AddPointProperty(name,numComps,isIdType,isProperty,
isGeometryPoints);
validParts[idx++]=part;
}
}
if(idx<=0)
{
//don't do anything as we have no valid parts
}
else if(this->MetaData->Fam.GetWordSize() == 8)
{
this->FillPointProperty<double>(numTuples,numComps,validParts, idx);
}
else
{
this->FillPointProperty<float>(numTuples,numComps,validParts, idx);
}
delete[] validParts;
}
namespace
{
//this function is used to sort a collection of parts
//based on the max and min global point ids that the part
//we use both to enforce better weak ordering
bool sortPartsOnGlobalIds(const vtkLSDynaPart *p1, const vtkLSDynaPart *p2)
{
if(p1->GetMaxGlobalPointId() < p2->GetMaxGlobalPointId())
{
return true;
}
return false;
}
}
//-----------------------------------------------------------------------------
template<typename T>
void vtkLSDynaPartCollection::FillPointProperty(const vtkIdType& numTuples,
const vtkIdType& numComps,
vtkLSDynaPart** parts,
const vtkIdType numParts)
{
LSDynaMetaData* p = this->MetaData;
//construct the sorted array of parts so we only
//have to iterate a subset that are interested in the points we have
//are reading in.
std::list<vtkLSDynaPart*> sortedParts(parts,parts+numParts);
std::list<vtkLSDynaPart*>::iterator partIt;
sortedParts.sort(sortPartsOnGlobalIds);
//find the max as the subset of points
const vtkIdType maxGlobalPoint(sortedParts.back()->GetMaxGlobalPointId());
vtkIdType minGlobalPoint = maxGlobalPoint;
for(partIt = sortedParts.begin(); partIt != sortedParts.end(); ++partIt)
{
minGlobalPoint = std::min((*partIt)->GetMinGlobalPointId(),minGlobalPoint);
}
const vtkIdType realNumberOfTuples(maxGlobalPoint-minGlobalPoint);
const vtkIdType numPointsToSkipStart(minGlobalPoint);
const vtkIdType numPointsToSkipEnd(numTuples - (realNumberOfTuples + minGlobalPoint));
vtkIdType offset = numPointsToSkipStart;
const vtkIdType numPointsToRead(1048576);
const vtkIdType loopTimes(realNumberOfTuples/numPointsToRead);
const vtkIdType leftOver(realNumberOfTuples%numPointsToRead);
const vtkIdType bufferChunkSize(numPointsToRead*numComps);
T* buf = NULL;
p->Fam.SkipWords(numPointsToSkipStart * numComps);
for(vtkIdType j=0;j<loopTimes;++j,offset+=numPointsToRead)
{
p->Fam.BufferChunk(LSDynaFamily::Float,bufferChunkSize);
buf = p->Fam.GetBufferAs<T>();
partIt = sortedParts.begin();
while(partIt!=sortedParts.end() &&
(*partIt)->GetMaxGlobalPointId() < offset)
{
//remove all parts from the list that have already been
//filled by previous loops
sortedParts.pop_front();
partIt = sortedParts.begin();
}
while(partIt!=sortedParts.end())
{
//only read the points which have a point that lies within this section
//so we stop once the min is larger than our max id
(*partIt)->ReadPointBasedProperty(buf,numPointsToRead,numComps,offset);
++partIt;
}
}
if(leftOver>0 && !sortedParts.empty())
{
p->Fam.BufferChunk(LSDynaFamily::Float, leftOver*numComps);
buf = p->Fam.GetBufferAs<T>();
for (partIt = sortedParts.begin(); partIt!=sortedParts.end();++partIt)
{
(*partIt)->ReadPointBasedProperty(buf,leftOver,numComps,offset);
}
}
p->Fam.SkipWords(numPointsToSkipEnd * numComps);
}
|