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
|
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2016 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK 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 version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <iomanip>
// Other includes
#include "PhysicalModel.h"
//#include "Object3D.h"
#include "Atom.h"
#include "Cell.h"
#include "CellProperties.h"
#include "MultiComponent.h"
#include "StructuralComponent.h"
#include "PhysicalModelVersion.h"
// pmlschema stuffs
#include <PhysicalModel.hxx>
#include <StructuralComponent.hxx>
#include <InformativeComponent.hxx>
#include <Atoms.hxx>
#include <MultiComponent.hxx>
// XercesC stuffs
#include <xercesc/util/PlatformUtils.hpp>
//--------------- Constructor/Destructor ------------------------------
PhysicalModel::PhysicalModel() {
init();
}
PhysicalModel::PhysicalModel ( const char * fileName, PtrToSetProgressFunction pspf ) throw ( PMLAbortException ) {
init();
setProgressFunction = pspf;
// load from the xml file
xmlRead ( fileName );
}
// --------------- destructor ---------------
PhysicalModel::~PhysicalModel() {
clear();
}
// --------------- init ---------------
void PhysicalModel::init() {
properties = new Properties ( this );
positionPtr = NULL;
exclusiveComponents = NULL;
informativeComponents = NULL;
atoms = NULL;
setProgressFunction = NULL;
cellIndexOptimized = true; //always hopeful!
isModifiedFlag = false;
}
// --------------- setProgress ---------------
void PhysicalModel::setProgress ( const float donePercentage ) {
if ( setProgressFunction != NULL ) {
setProgressFunction ( donePercentage );
}
}
// --------------- clear ---------------
void PhysicalModel::clear() {
if ( informativeComponents ) {
delete informativeComponents;
}
informativeComponents = NULL;
if ( exclusiveComponents ) {
delete exclusiveComponents;
}
exclusiveComponents = NULL;
// delete all atoms at the end (otherwise deletion of cells in informative
// or exclusive components will try to tell already deleted (unaccessible) atoms
// that some of their SC is to be removed from their list
if ( atoms ) {
delete atoms;
}
atoms = NULL;
// clean position memory
delete [] positionPtr;
// reset all the unique indexes
AtomProperties::resetUniqueIndex();
CellProperties::resetUniqueIndex();
atomMap.clear();
cellMap.clear();
delete properties;
properties = NULL;
}
// --------------- getNumberOfCells ---------------
unsigned int PhysicalModel::getNumberOfCells() const {
unsigned int nrOfCells = 0;
if ( exclusiveComponents ) {
nrOfCells += exclusiveComponents->getNumberOfCells();
}
if ( informativeComponents ) {
nrOfCells += informativeComponents->getNumberOfCells();
}
return nrOfCells;
}
// --------------- getPositionPointer ---------------
double * PhysicalModel::getPositionPointer() const {
return positionPtr;
}
double * PhysicalModel::getPositionPointer ( const Atom *a ) const {
unsigned int idInAtom = a->getIndexInAtoms();
if ( idInAtom>=0 )
// * 3 because the position are stored consequently, using a double for x, y and z (3 double then!)
{
return positionPtr + idInAtom*3;
} else {
// look for atom #index in the atoms
Atom *ai = NULL;
unsigned int i = 0;
while ( i<atoms->getNumberOfStructures() && ( ai != a ) ) {
ai = dynamic_cast<Atom *> ( atoms->getStructure ( i ) );
i++;
}
if ( ai == a )
// the memory allocated to atom #index is (i-1) * 3 (because the position are stored consequently, using a double for x, y and z
{
return positionPtr + ( i-1 ) *3;
} else {
return NULL;
}
}
}
double * PhysicalModel::getPositionPointer ( const unsigned int index ) const {
// look for atom #index in the atoms
bool found = false;
unsigned int i = 0;
while ( i<atoms->getNumberOfStructures() && !found ) {
Atom *a = dynamic_cast<Atom *> ( atoms->getStructure ( i ) );
found = ( a->getIndex() == index );
i++;
}
if ( found )
// the memory allocated to atom #index is (i-1) * 3 (because the position are stored consequently, using a double for x, y and z
{
return positionPtr + ( i-1 ) *3;
} else {
return NULL;
}
}
// --------------- optimizeIndexes ---------------
void PhysicalModel::optimizeIndexes ( MultiComponent * mc, unsigned int * index ) {
Component *c;
StructuralComponent *sc;
Cell *cell;
for ( unsigned int i = 0; i < mc->getNumberOfSubComponents(); i++ ) {
c = mc->getSubComponent ( i );
if ( c->isInstanceOf ( "MultiComponent" ) ) {
optimizeIndexes ( ( MultiComponent * ) c, index );
} else {
if ( c->isInstanceOf ( "StructuralComponent" ) ) {
sc = ( StructuralComponent * ) c;
// check all cells
for ( unsigned int j = 0; j < sc->getNumberOfStructures(); j++ ) {
if ( sc->getStructure ( j )->isInstanceOf ( "Cell" ) ) {
cell = ( Cell * ) sc->getStructure ( j );
// if this is the sc that make the cell print its data, change cell index
if ( cell->makePrintData ( sc ) ) {
cell->setIndex ( *index );
*index = ( *index ) + 1;
}
}
}
}
}
}
}
void PhysicalModel::optimizeIndexes() {
// to optimize the indexes: do as if it was a print/read operation (same order
// and change the cell index (as everyone is linked with ptrs, that should not
// change anything else
// first: the atoms
if ( atoms ) {
for ( unsigned int i = 0; i < atoms->getNumberOfStructures(); i++ ) {
( ( Atom * ) atoms->getStructure ( i ) )->setIndex ( i );
}
}
// then the cells
unsigned int newIndex = 0;
if ( exclusiveComponents ) {
optimizeIndexes ( exclusiveComponents, &newIndex );
}
if ( informativeComponents ) {
optimizeIndexes ( informativeComponents, &newIndex );
}
}
// --------------- xmlPrint ---------------
void PhysicalModel::xmlPrint ( std::ostream &o, bool opt ) {
// should we optimize the cell indexes ?
if ( !cellIndexOptimized && opt ) {
optimizeIndexes();
}
// print out the whole thing
o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
o << "<!-- physical model (PML) is a generic representation for 3D physical model." << std::endl
<< " PML supports not continous indexes and multiple non-exclusive labelling." << std::endl
<< " --> " << std::endl;
o << "<physicalModel";
if ( getName() != "" ) {
o << " name=\"" << getName().c_str() << "\"";
}
if ( atoms ) {
o << " nrOfAtoms=\"" << atoms->getNumberOfStructures() << "\"" << std::endl;
}
if ( exclusiveComponents ) {
o << " nrOfExclusiveComponents=\"" << exclusiveComponents->getNumberOfSubComponents() << "\"" << std::endl;
}
if ( informativeComponents ) {
o << " nrOfInformativeComponents=\"" << informativeComponents->getNumberOfSubComponents() << "\"" << std::endl;
}
o << " nrOfCells=\"" << getNumberOfCells() << "\"" << std::endl;
for ( unsigned int i = 0; i<properties->numberOfFields(); i++ ) {
o << " " << properties->getField ( i ) <<"=\"" << properties->getString ( properties->getField ( i ) ) << "\"" << std::endl;
}
o << ">" << std::endl;
o << "<!-- list of atoms: -->" << std::endl;
o << "<atoms>" << std::endl;
if ( atoms ) {
atoms->xmlPrint ( o );
}
o << "</atoms>" << std::endl;
o << "<!-- list of exclusive components : -->" << std::endl;
o << "<exclusiveComponents>" << std::endl;
if ( exclusiveComponents ) {
exclusiveComponents->xmlPrint ( o );
}
o << "</exclusiveComponents>" << std::endl;
if ( informativeComponents ) {
o << "<!-- list of informative components : -->" << std::endl;
o << "<informativeComponents>" << std::endl;
informativeComponents->xmlPrint ( o );
o << "</informativeComponents>" << std::endl;
}
o << "</physicalModel>" << std::endl;
// serialized/marshalled -> saved
isModifiedFlag = false;
}
// --------------- xmlRead ---------------
void PhysicalModel::xmlRead ( const char * filename ) throw ( PMLAbortException ) {
// clear all the current data
clear();
// Set the locale to C for using dot as decimal point dispite locale
// Set utf8 for output to enforce using utf8 strings.
char * statusOk = setlocale ( LC_CTYPE, "C.UTF-8" );
if ( statusOk!=NULL ) {
statusOk = setlocale ( LC_NUMERIC, "C.UTF-8" );
}
if ( statusOk!=NULL ) {
statusOk = setlocale ( LC_TIME, "C.UTF-8" );
}
// french is: "fr_FR.UTF8"
if ( !statusOk ) {
// try without UTF-8
statusOk = setlocale ( LC_CTYPE,"C" );
if ( statusOk!=NULL ) {
statusOk = setlocale ( LC_NUMERIC, "C" );
}
if ( statusOk!=NULL ) {
statusOk = setlocale ( LC_TIME, "C" );
}
if ( statusOk==NULL ) {
std::cerr << "Could not set the locale to C. This is mandatory to enforce using dot as decimal separator (platform independency)." << std::endl;
std::cerr << "This can cause a lot of trouble for XML I/O... Beware of decimal dots..." << std::endl;
}
}
// Initialize manually the XercesC++ runtime, required by using wildcards in XSD schema.
// Implies that we pass the xml_shema::flags::dont_initialize to any serialization method of the pmlschema library
// as we already have initialized the XercesC runtime manually.
xercesc::XMLPlatformUtils::Initialize ();
// Use XSD with the pmlschema library to read the loads described in the xml file.
try {
std::auto_ptr<physicalModel::PhysicalModel> root = physicalModel::physicalModel ( filename, xml_schema::flags::dont_initialize | xml_schema::flags::dont_validate );
// get the basename
std::string basename = filename;
// remove the path
unsigned lastSeparator = basename.find_last_of ( "/\\" ); // works for unix and windows
if ( lastSeparator != std::string::npos ) {
basename = basename.substr ( lastSeparator+1 );
}
// remove the extension
lastSeparator = basename.find_last_of ( "." );
if ( lastSeparator != std::string::npos ) {
basename.erase ( lastSeparator );
}
// Parse the xml content
this->parseTree ( root, basename );
} catch ( const xml_schema::exception& e ) {
std::ostringstream os;
os << "Library-pml Error: In PhysicalModel::xmlRead(..): Failed to read the xml file, reason:" << std::endl;
os << e << std::endl;
std::cerr << os.str() << std::endl;
throw PMLAbortException ( os.str () );
}
// Terminate the XercesC++ runtime manually
xercesc::XMLPlatformUtils::Terminate ();
}
// ------------------ parse tree ------------------
bool PhysicalModel::parseTree ( std::auto_ptr<physicalModel::PhysicalModel> root , std::string defaultName ) {
// Pml file have a name which can be an empty string
std::string name;
if ( root->name().present() ) {
name = root->name().get();
} else {
name = defaultName;
}
this->properties = new Properties ( this, name );
// add additionnal attributes as properties
physicalModel::PhysicalModel::any_attribute_set unknownAttrs = root->any_attribute();
this->properties->xmlToFields ( unknownAttrs );
// Parse the atoms
parseAtoms ( root->atoms() );
// allocate the big memory bunch
positionPtr = new double[3*atoms->getNumberOfStructures()];
// assign all position memory
double * currentPositionPtr = positionPtr;
for ( unsigned int i=0; i<atoms->getNumberOfStructures(); i++ ) {
Atom *a = dynamic_cast<Atom *> ( atoms->getStructure ( i ) );
a->getProperties()->setPositionPointer ( currentPositionPtr );
// next position => jump 3 double memory space
currentPositionPtr = currentPositionPtr + 3;
}
// Parse the exclusive components
physicalModel::ExclusiveComponent ecs = root->exclusiveComponents();
physicalModel::MultiComponent xmlExclusiveMC = ecs.multiComponent();
// Get the top level multicomponent node (with name + unkown attributes)
MultiComponent* exclusiveMC = new MultiComponent ( this );
// recursively build the tree of exclusive components
parseComponents ( xmlExclusiveMC, exclusiveMC, true );
// Add it to the structure
this->setExclusiveComponents ( exclusiveMC );
// Parse the informative components
if ( root->informativeComponents().present() ) {
physicalModel::InformativeComponent ics = root->informativeComponents().get();
physicalModel::MultiComponent xmlInformativeMC = ics.multiComponent();
// Get the top level multicomponent node (with name + unkown attributes)
MultiComponent* informativeMC = new MultiComponent ( this );
// recursively build the tree of informative components
parseComponents ( xmlInformativeMC, informativeMC, false );
// Add it to the structure
this->setInformativeComponents ( informativeMC );
}
return true;
}
// ------------------ parse atoms ------------------
bool PhysicalModel::parseAtoms ( physicalModel::PhysicalModel::atoms_type atomsRoot ) {
// Parse the content of the structuralComponent
physicalModel::Atoms:: structuralComponent_type xmlSC = atomsRoot.structuralComponent();
StructuralComponent* sc = new StructuralComponent ( this, xmlSC );
// Parse the number of structures
if ( xmlSC.nrOfStructures().present() ) {
sc->plannedNumberOfStructures ( xmlSC.nrOfStructures().get().value() );
}
// Parse the atoms
unsigned int atomOrderNumber = 0; // index in the atoms SC
physicalModel::StructuralComponent::atom_sequence &atoms = xmlSC.atom();
for ( physicalModel::StructuralComponent::atom_iterator atomIt ( atoms.begin() ); atomIt != atoms.end(); atomIt++ ) {
physicalModel::Atom& currentAtom = *atomIt;
Atom *newAtom = new Atom ( this, currentAtom, atomOrderNumber );
atomOrderNumber++;
sc->addStructure ( newAtom );
}
// Identify the parsed structural component as the list of atoms
this->setAtoms ( sc );
return true;
}
// ------------------ parse Components ------------------
bool PhysicalModel::parseComponents ( physicalModel::MultiComponent xmlFatherMC, Component* father, bool isExclusive ) {
// Parse the name and unkown properties attribute of the main multiComponent
MultiComponent* fatherMC = ( MultiComponent* ) father;
fatherMC->setName ( xmlFatherMC.name().get() );
fatherMC->getProperties()->xmlToFields ( xmlFatherMC.any_attribute() );
// Recursively consider multi component children
physicalModel::MultiComponent::multiComponent_sequence xmlChildren = xmlFatherMC.multiComponent();
for ( physicalModel::MultiComponent::multiComponent_iterator MCIt = xmlChildren.begin(); MCIt != xmlChildren.end(); MCIt++ ) {
physicalModel::MultiComponent xml_child = ( *MCIt );
MultiComponent* child = new MultiComponent ( this );
fatherMC->addSubComponent ( child );
this->parseComponents ( xml_child, child, isExclusive );
}
// Consider the structural component children
physicalModel::MultiComponent::structuralComponent_sequence xmlAllSC = xmlFatherMC.structuralComponent();
for ( physicalModel::MultiComponent::structuralComponent_iterator SCIt = xmlAllSC.begin(); SCIt != xmlAllSC.end(); SCIt++ ) {
physicalModel::StructuralComponent xmlSC = ( *SCIt );
StructuralComponent* sc = new StructuralComponent ( this, xmlSC );
sc->setExclusive ( isExclusive );
fatherMC->addSubComponent ( sc );
// StructuralComponent's cells
physicalModel::StructuralComponent::cell_sequence xmlAllCells = xmlSC.cell();
for ( physicalModel::StructuralComponent::cell_iterator cellIt = xmlAllCells.begin();
cellIt != xmlAllCells.end();
cellIt++ ) {
physicalModel::Cell xmlCell = * ( cellIt );
// create the cell according to its geometric type
Cell* cell = new Cell ( this, xmlCell, sc );
cell->setExclusive ( isExclusive );
if ( this->cellIndexOptimized ) {
std::GlobalIndexStructurePair pair ( cell->getIndex(), cell );
this->addGlobalIndexCellPair ( pair );
}
sc->addStructure ( cell, false );
}
// StructuralComponent's atoms references
physicalModel::StructuralComponent::atomRef_sequence xmlAllAtomRefs = xmlSC.atomRef();
for ( physicalModel::StructuralComponent::atomRef_iterator atomRefIt = xmlAllAtomRefs.begin();
atomRefIt != xmlAllAtomRefs.end();
atomRefIt++ ) {
physicalModel::AtomRef atomRef = * ( atomRefIt );
// Get the atom corresponding to the reference
Atom* a = this->getAtom ( atomRef.index() );
if ( a ) {
sc->addStructure ( a );
} else {
std::cerr << "PhysicalModel::parseComponents: cannot find atom of ref: " << atomRef.index() << std::endl;
}
}
}
return true;
}
// ------------------ getComponentByName ------------------
Component * PhysicalModel::getComponentByName ( const std::string n ) {
//-- look for the component in exclusive component first
Component * foundC;
foundC = exclusiveComponents->getComponentByName ( n );
//-- then look in the informative components
if ( !foundC && informativeComponents ) {
foundC = informativeComponents->getComponentByName ( n );
}
//-- at last, just in case, look if this is not the name of the atoms SC
if ( !foundC && getAtoms()->getName() == n) {
foundC = getAtoms();
}
return foundC;
}
// ----------------------- setAtoms ------------------
void PhysicalModel::setAtoms ( StructuralComponent *sc, bool deleteOld ) {
Atom *a;
if ( sc->composedBy() == StructuralComponent::ATOMS ) {
if ( atoms && deleteOld ) {
delete atoms;
}
atoms = sc;
// register all the atoms in the map, and tell the atoms about its new status
for ( unsigned int i = 0; i < sc->getNumberOfStructures(); i++ ) {
a = ( Atom * ) sc->getStructure ( i );
a->getProperties()->setPhysicalModel ( this );
addGlobalIndexAtomPair ( std::GlobalIndexStructurePair ( a->getIndex(), a ) );
}
}
}
// ----------------------- addAtom ------------------
bool PhysicalModel::addAtom ( Atom *newA ) {
// register the atom in the map if possible
if ( atoms && addGlobalIndexAtomPair ( std::GlobalIndexStructurePair ( newA->getIndex(), newA ) ) ) {
// add the atom in the atom structural component
atoms->addStructure ( newA );
return true;
} else {
return false; // atom does not have a unique index
}
}
// ----------------------- addGlobalIndexAtomPair ------------------
bool PhysicalModel::addGlobalIndexAtomPair ( std::GlobalIndexStructurePair p ) {
std::GlobalIndexStructureMapIterator mapIt;
// check if the atom's index is unique
mapIt = atomMap.find ( p.first );
// if the index was found, one can not add the atom
if ( mapIt != atomMap.end() ) {
return false;
}
// if the atom is present in the map then replace the pair <atomIndex, Atom*>
mapIt = atomMap.begin();
while ( mapIt != atomMap.end() && mapIt->second != p.second ) {
mapIt++;
}
// if found then remove the pair
if ( mapIt != atomMap.end() ) {
atomMap.erase ( mapIt );
}
// insert or re-insert (and return true if insertion was ok)
return atomMap.insert ( p ).second;
}
// ----------------------- addGlobalIndexCellPair ------------------
bool PhysicalModel::addGlobalIndexCellPair ( std::GlobalIndexStructurePair p ) {
std::GlobalIndexStructureMapIterator mapIt;
// check if the cell index is unique
mapIt = cellMap.find ( p.first );
// if the index was found, one can not add the cell
if ( mapIt != cellMap.end() ) {
return false;
}
// if the cell is present in the map then replace the pair <cellIndex, Cell*>
mapIt = cellMap.begin();
while ( mapIt != cellMap.end() && mapIt->second != p.second ) {
mapIt++;
}
// if found then remove the pair
if ( mapIt != cellMap.end() ) {
cellMap.erase ( mapIt );
}
// insert or re-insert
bool insertionOk = cellMap.insert ( p ).second;
// is that optimized?
cellIndexOptimized = cellIndexOptimized && ( ( Cell * ) p.second )->getIndex() == optimizedCellList.size();
if ( cellIndexOptimized ) {
optimizedCellList.push_back ( ( Cell * ) p.second );
}
// insert or re-insert (and return true if insertion was ok)
return insertionOk;
}
// ----------------------- setAtomPosition ------------------
void PhysicalModel::setAtomPosition ( Atom *atom, const double pos[3] ) {
atom->setPosition ( pos );
}
// ----------------------- setExclusiveComponents ------------------
void PhysicalModel::setExclusiveComponents ( MultiComponent *mc ) {
if ( exclusiveComponents ) {
delete exclusiveComponents;
}
exclusiveComponents = mc;
mc->setPhysicalModel ( this );
}
// ----------------------- setInformativeComponents ------------------
void PhysicalModel::setInformativeComponents ( MultiComponent *mc ) {
if ( informativeComponents ) {
delete informativeComponents;
}
informativeComponents = mc;
mc->setPhysicalModel ( this );
}
// ----------------------- getNumberOfExclusiveComponents ------------------
unsigned int PhysicalModel::getNumberOfExclusiveComponents() const {
if ( !exclusiveComponents ) {
return 0;
} else {
return exclusiveComponents->getNumberOfSubComponents();
}
}
// ----------------------- getNumberOfInformativeComponents ------------------
unsigned int PhysicalModel::getNumberOfInformativeComponents() const {
if ( !informativeComponents ) {
return 0;
} else {
return informativeComponents->getNumberOfSubComponents();
}
}
// ----------------------- getNumberOfAtoms ------------------
unsigned int PhysicalModel::getNumberOfAtoms() const {
if ( !atoms ) {
return 0;
} else {
return atoms->getNumberOfStructures();
}
}
// ----------------------- getExclusiveComponent ------------------
Component * PhysicalModel::getExclusiveComponent ( const unsigned int i ) const {
if ( !exclusiveComponents ) {
return 0;
} else {
return exclusiveComponents->getSubComponent ( i );
}
}
// ----------------------- getInformativeComponent ------------------
Component * PhysicalModel::getInformativeComponent ( const unsigned int i ) const {
if ( !informativeComponents ) {
return 0;
} else {
return informativeComponents->getSubComponent ( i );
}
}
// ----------------------- exportAnsysMesh ------------------
void PhysicalModel::exportAnsysMesh ( std::string filename ) {
//--- Writing nodes
std::ofstream nodeFile;
nodeFile.open ( ( filename + ".node" ).c_str() );
if ( !nodeFile.is_open() ) {
std::cerr << "Error in PhysicalModel::exportAnsysMesh : unable to create .node output file" << std::endl;
return;
}
for ( unsigned int i = 0; i < getNumberOfAtoms(); i++ ) {
double pos[3];
//WARNING getAtom(i) do not work if indexes do not follow each others
Atom* at= ( Atom* ) ( atoms->getStructure ( i ) );
// WARNING : indexes are in base 1 !!!!
unsigned int ansysIndex = at->getIndex() + 1;
// coordinates of this node
at->getPosition ( pos );
nodeFile << std::setw ( 8 ) << ansysIndex << " ";
for ( unsigned int j=0; j<3; j++ ) {
nodeFile << std::setprecision ( 8 ) << std::setw ( 3 ) << std::fixed << std::scientific << pos[j] << " ";
}
nodeFile << std::endl;
}
nodeFile.close();
//--- Writing elements : exlusive cells
std::ofstream elemFile;
elemFile.open ( ( filename + ".elem" ).c_str() );
if ( !elemFile.is_open() ) {
std::cerr << "Error in PhysicalModel::exportAnsysMesh : unable to create .elem output file" << std::endl;
return;
}
int MAT, TYPE;
Component *elements = this->getComponentByName ( "Elements" );
for ( unsigned int i = 0; i < elements->getNumberOfCells(); i++ ) {
// get the cell
Cell * cell = elements->getCell ( i );
unsigned int ansysIndex = cell->getIndex() + 1;
switch ( cell->getType() ) {
case StructureProperties::HEXAHEDRON:
// I,J,K,L,M,N,O,P,MAT,TYPE,REAL,SECNUM,ESYS,IEL
//
// Format hex:
// I,J,K,L,M,N,O,P = indices des noeuds
// MAT,TYPE,REAL,SECNUM et ESYS = attributes numbers
// SECNUM = beam section number
// IEL = element number
MAT = 1;
TYPE = 1;
for ( unsigned int k = 0; k < cell->getNumberOfStructures(); k++ ) {
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( k )->getIndex() + 1 ;
}
elemFile << " " << std::setw ( 5 ) << MAT << " " << std::setw ( 5 ) << TYPE << " 1 1 0 " << std::setw ( 5 ) << ansysIndex << std::endl;
break;
case StructureProperties::WEDGE:
// I,J,K,L,M,N,O,P,MAT,TYPE,REAL,SECNUM,ESYS,IEL
//
// Format prism: on repete les noeuds 3 et 7:
// I,J,K,K,M,N,O,O = indices des noeuds
// MAT,TYPE,REAL,SECNUM et ESYS = attributes numbers
// SECNUM = beam section number
// IEL = element number
MAT = 1;
TYPE = 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 0 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 1 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 2 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 2 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 3 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 4 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 5 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 5 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << MAT << " " << std::setw ( 5 ) << TYPE << " 1 1 0 " << std::setw ( 5 ) << ansysIndex << std::endl;
break;
case StructureProperties::TETRAHEDRON:
MAT = 1;
TYPE = 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 0 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 1 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 2 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 3 )->getIndex() + 1;
elemFile << " 0 0 0 0" ;
elemFile << " " << std::setw ( 5 ) << MAT << " " << std::setw ( 5 ) << TYPE << " 1 1 0 " << std::setw ( 5 ) << ansysIndex << std::endl;
break;
case StructureProperties::QUAD:
// I,J,K,L,M,N,O,P,MAT,TYPE,REAL,SECNUM,ESYS,IEL
//
// Format quad:
// I,J,K,K,L,L,L,L = indices des noeuds
// MAT,TYPE,REAL,SECNUM et ESYS = attributes numbers
// SECNUM = beam section number
// IEL = element number
MAT = 1;
TYPE = 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 0 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 1 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 2 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 2 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 3 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 3 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 3 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << cell->getStructure ( 3 )->getIndex() + 1;
elemFile << " " << std::setw ( 5 ) << MAT << " " << std::setw ( 5 ) << TYPE << " 1 1 0 " << std::setw ( 5 ) << ansysIndex << std::endl;
break;
default:
std::cerr << "PhysicalModel::exportAnsysMesh : unknown type for cell " << cell->getIndex() + 1 << ", neither HEXAHEDRON, WEDGE, THETRAHEDRON nor QUAD. Cant' export in Patran format." << std::endl;
continue;
}
}
elemFile.close();
}
// ----------------------- exportPatran ------------------
void PhysicalModel::exportPatran ( std::string filename ) {
std::ofstream outputFile;
outputFile.open ( filename.c_str() );
if ( !outputFile.is_open() ) {
std::cerr << "Error in PhysicalModel::exportPatran : unable to create output file" << std::endl;
return;
}
//--- patran header -> mostly useless info in our case...
outputFile << "25 0 0 1 0 0 0 0 0\n";
outputFile << "PATRAN File from: " << getName().c_str() << std::endl;
outputFile << "26 0 0 1 " << this->getNumberOfAtoms() << " " << this->getExclusiveComponent ( 0 )->getNumberOfCells() << " 3 4 -1\n";
outputFile << "17-Mar-00 08:00:00 3.0\n";
//--- Nodes (atoms)
for ( unsigned int i = 0; i < this->getNumberOfAtoms(); i++ ) {
double pos[3];
// first line
// WARNING : indexes are in base 1 !!!!
outputFile << " 1" << std::setw ( 8 ) << getAtom ( i )->getIndex() + 1 << " 0 2 0 0 0 0 0\n";
// coordinates of this node
// fscanf(inputFile, "%d %f %f %f", &j, &x, &y, &z);
getAtom ( i )->getPosition ( pos );
// second line : node coordinates
for ( unsigned int j=0; j<3; j++ ) {
outputFile << std::setprecision ( 8 ) << std::setw ( 16 ) << std::fixed << std::scientific << pos[j];
}
outputFile << " " << std::endl;
// third line : ??
outputFile << "1G 6 0 0 000000\n";
}
//--- Elements : exlusive cells
for ( unsigned int i = 0; i < this->getExclusiveComponent ( 0 )->getNumberOfCells(); i++ ) {
int typeElement;
// get the cell
Cell * cell = this->getExclusiveComponent ( 0 )->getCell ( i );
switch ( cell->getType() ) {
case StructureProperties::HEXAHEDRON:
typeElement = 8;
break;
case StructureProperties::WEDGE:
typeElement = 7;
break;
default:
std::cerr << "PhysicalModel::exportPatran : unknown type for cell " << cell->getIndex() + 1 << ", neither HEXAHEDRON nor WEDGE. Cant' export in Patran format." << std::endl;
continue;
}
// first element line
outputFile << " 2" << std::setw ( 8 ) << cell->getIndex() + 1 << std::setw ( 8 ) << typeElement << " 2 0 0 0 0 0\n";
// second element line
outputFile << std::setw ( 8 ) << cell->getNumberOfStructures() << " 0 1 0 0.000000000E+00 0.000000000E+00 0.000000000E+00\n";
// third element line : list of nodes
for ( unsigned int k = 0; k < cell->getNumberOfStructures(); k++ ) {
outputFile << std::setw ( 8 ) << cell->getStructure ( k )->getIndex() + 1;
}
outputFile << "\n";
}
//--- final line
outputFile << "99 0 0 1 0 0 0 0 0\n" ;
outputFile.close();
}
|