Computer Assited Medical Intervention Tool Kit  version 5.0
PhysicalModel.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * $CAMITK_LICENCE_BEGIN$
3  *
4  * CamiTK - Computer Assisted Medical Intervention ToolKit
5  * (c) 2001-2021 Univ. Grenoble Alpes, CNRS, Grenoble INP, TIMC, 38000 Grenoble, France
6  *
7  * Visit http://camitk.imag.fr for more information
8  *
9  * This file is part of CamiTK.
10  *
11  * CamiTK is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * CamiTK is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License version 3 for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22  *
23  * $CAMITK_LICENCE_END$
24  ****************************************************************************/
25 
26 #ifndef PHYSICALMODEL_H
27 #define PHYSICALMODEL_H
28 
29 //pmlschema forward declaration
30 namespace physicalModel {
31 class PhysicalModel;
32 class Atoms;
33 class ExclusiveComponent;
34 class InformativeComponent;
35 class MultiComponent;
36 }
37 
38 #include "PhysicalModelIO.h"
39 #include <string>
40 #include <memory>
41 #include <algorithm>
42 #include "PMLAbortException.h"
43 #include "StructuralComponent.h" // so we can put the optimized getAtom method inline
44 #include "Atom.h" // so we can put the optimized getAtom method inline
45 #include "Cell.h" // so we can put the optimized getCell method inline
46 
47 
48 class MultiComponent;
49 class Component;
50 class Cell;
51 class Structure;
52 
53 
54 // Hide warning for Exception + declspec(nothrow)
55 #if defined(_WIN32) && !defined(__MINGW32__) // MSVC only
56 #pragma warning( disable : 4290 )
57 #endif // MSVC only
58 
60 using PtrToSetProgressFunction = void (*)(const float donePercentage);
61 
62 namespace std {
66 using GlobalIndexStructurePair = std::pair<unsigned int, Structure*>;
71 using GlobalIndexStructureMap = std::map <unsigned int, Structure*>;
73 using GlobalIndexStructureMapIterator = std::map <unsigned int, Structure*> ::iterator;
74 }
75 
76 
87 
88 public:
95  PhysicalModel() noexcept;
96 
104  PhysicalModel(const char* fileName, PtrToSetProgressFunction pspf = nullptr);
105 
107  virtual ~PhysicalModel();
109  static const char* VERSION;
111 
116  const std::string getName() const;
118  void setName(const std::string);
125  bool isModified();
129  void setModified();
131 
145  void xmlPrint(std::ostream& o, bool opt = false);
146 
154  void exportPatran(std::string filename);
155 
163  void exportAnsysMesh(std::string filename);
165 
169  unsigned int getNumberOfExclusiveComponents() const;
171 
173  unsigned int getNumberOfInformativeComponents() const;
174 
176  unsigned int getNumberOfAtoms() const;
177 
179  unsigned int getNumberOfCells() const;
180 
182  Component* getExclusiveComponent(const unsigned int) const;
183 
186 
189 
192 
194  StructuralComponent* getAtoms() const;
195 
197  Component* getInformativeComponent(const unsigned int) const;
198 
201 
207  void setAtoms(StructuralComponent*, bool deleteOld = true);
208 
214  bool addAtom(Atom*);
215 
228  Atom* getAtom(const unsigned int id);
229 
235 
241 
246  Cell* getCell(const unsigned int id);
247 
249  Structure* getStructureByName(const std::string n);
250 
255  Component* getComponentByName(const std::string n);
256 
261  virtual void setProgress(const float donePercentage);
262 
265  virtual void setAtomPosition(Atom* atom, const double pos[3]);
266 
271  double* getPositionPointer() const;
272 
274  double* getPositionPointer(const unsigned int index) const;
275 
277  double* getPositionPointer(const Atom* a) const;
279 
280 
281 private:
286  void xmlRead(const char* n);
287 
289  bool parseTree(std::unique_ptr<physicalModel::PhysicalModel> root, std::string defaultName);
291  bool parseAtoms(physicalModel::Atoms atomsRoot);
293  bool parseComponents(physicalModel::MultiComponent mcFather, Component* father, bool isExclusive);
294 
297 
300 
307 
314 
319 
321  void clear();
322 
325 
327  std::vector <Cell*> optimizedCellList;
328 
331 
334  void optimizeIndexes();
335 
337  void optimizeIndexes(MultiComponent*, unsigned int*);
338 
341 
343  void init();
344 
347 
349  double* positionPtr;
350 };
351 
352 
353 // ------------------ simple inline functions ------------------
355  return properties;
356 }
357 
358 inline const std::string PhysicalModel::getName() const {
359  return properties->getName();
360 }
361 
362 inline void PhysicalModel::setName(const std::string n) {
363  properties->setName(n);
364 }
365 
367  return isModifiedFlag;
368 }
369 
371  isModifiedFlag = true;
372 }
373 
375  return exclusiveComponents;
376 }
378  return informativeComponents;
379 }
381  return atoms;
382 }
383 
384 // ------------------ getAtom ------------------
385 inline Atom* PhysicalModel::getAtom(const unsigned int id) {
386 
387  // optimization: first check if the order is the structure is not the same
388  // as the atom index (which is the case very often)
389  Atom* quickAccessed = dynamic_cast<Atom*>(atoms->getStructure(id));
390 
391  if (quickAccessed && quickAccessed->getIndex() == id) {
392  return quickAccessed;
393  }
394  else {
395  // if not then check if it could be found in the map
396  std::GlobalIndexStructureMapIterator mapIt; // a representation map iterator
397  mapIt = atomMap.find(id);
398 
399  // search in the map, and return the correct result
400  return ((mapIt == atomMap.end()) ? nullptr : (Atom*) mapIt->second);
401  }
402 }
403 
404 // ------------------ getCell ------------------
405 inline Cell* PhysicalModel::getCell(const unsigned int cellIndex) {
406  if (cellIndexOptimized) {
407  return optimizedCellList[cellIndex];
408  }
409  else {
410  std::GlobalIndexStructureMapIterator mapIt; // a representation map iterator
411 
412  // check if it was find in the list
413  mapIt = cellMap.find(cellIndex);
414 
415  // search in the map, and return the correct result
416  return ((mapIt == cellMap.end()) ? nullptr : (Cell*) mapIt->second);
417  }
418 }
419 
420 // ------------------ getStructureByName ------------------
421 inline Structure* PhysicalModel::getStructureByName(const std::string n) {
422  // look for structures into the global maps
423 
424  // look for a cell with this name
425  auto mapIt = cellMap.begin();
426 
427  while (mapIt != cellMap.end() && mapIt->second->getName() != n) {
428  mapIt++;
429  }
430 
431  // if found returns it
432  if (mapIt != cellMap.end()) {
433  return mapIt->second;
434  }
435 
436  // look now in the atoms
437  mapIt = atomMap.begin();
438 
439  while (mapIt != atomMap.end() && mapIt->second->getName() != n) {
440  mapIt++;
441  }
442 
443  // if found returns it
444  if (mapIt != atomMap.end()) {
445  return mapIt->second;
446  }
447 
448  return nullptr;
449 }
450 
451 #endif
PhysicalModel::atoms
StructuralComponent * atoms
List of all the atoms : this is the basic stuff for a physicall model.
Definition: PhysicalModel.h:318
PhysicalModel::addGlobalIndexAtomPair
bool addGlobalIndexAtomPair(std::GlobalIndexStructurePair)
add or update a pair to the atom map.
Definition: PhysicalModel.cpp:605
PhysicalModel::optimizedCellList
std::vector< Cell * > optimizedCellList
optimized consecutive cell vector (in here optimizedCellList[i]->getIndex() == i )
Definition: PhysicalModel.h:327
Cell::setIndex
bool setIndex(const unsigned int) override
set the index.
Definition: Cell.cpp:139
PhysicalModel::getExclusiveComponent
Component * getExclusiveComponent(const unsigned int) const
get an exclusive component by its index in the list
Definition: PhysicalModel.cpp:726
PhysicalModel::setModified
void setModified()
tell the physical model something has changed (for example: a property was modified/added).
Definition: PhysicalModel.h:370
Cell.h
PhysicalModel::getExclusiveComponents
MultiComponent * getExclusiveComponents() const
get all the exclusive components
Definition: PhysicalModel.h:374
PhysicalModel::setExclusiveComponents
void setExclusiveComponents(MultiComponent *)
set the exclusive multi component. Becareful: the physical model takes control of this MultiComponent
Definition: PhysicalModel.cpp:676
PhysicalModel::getPositionPointer
double * getPositionPointer() const
get the pointer to the memory space allocated for the atom's position.
Definition: PhysicalModel.cpp:145
PhysicalModel::getNumberOfCells
unsigned int getNumberOfCells() const
get the total nr of cell in the physical model (exclusive as well as informative)
Definition: PhysicalModel.cpp:130
MultiComponent::getComponentByName
Component * getComponentByName(const std::string)
conveniant method to get the sub component of the name given in parameter
Definition: MultiComponent.h:137
StructureProperties::WEDGE
@ WEDGE
the structure is a wedge (like the Pink Floyd's "Dark Side Of the Moon" prism), it must be a cell and...
Definition: StructureProperties.h:160
StructureProperties::TETRAHEDRON
@ TETRAHEDRON
the structure is a tetrahedron, it must be a cell and have sub-structures that are atoms
Definition: StructureProperties.h:159
MultiComponent::xmlPrint
void xmlPrint(std::ostream &) const override
print to an output stream in "pseaudo" XML format (do nothing if there are no sub components).
Definition: MultiComponent.cpp:58
StructuralComponent::getNumberOfStructures
unsigned int getNumberOfStructures() const
get the number of structures
Definition: StructuralComponent.h:252
PhysicalModel::properties
Properties * properties
all physical model properties
Definition: PhysicalModel.h:296
Atom::getPosition
void getPosition(double pos[3]) const
get the position of the atom (array of 3 doubles)
Definition: Atom.h:121
PhysicalModel::addGlobalIndexCellPair
bool addGlobalIndexCellPair(std::GlobalIndexStructurePair)
add or update a pair to the cell map.
Definition: PhysicalModel.cpp:633
Structure::getType
StructureProperties::GeometricType getType() const
get the type of index
Definition: Structure.cpp:40
Structure::isInstanceOf
virtual bool isInstanceOf(const char *) const =0
pure virtual method, implemented in the child-class
PhysicalModel::getStructureByName
Structure * getStructureByName(const std::string n)
get a cell using its name
Definition: PhysicalModel.h:421
MultiComponent::getSubComponent
Component * getSubComponent(const unsigned int) const
get a subcomponent by its order number (index in the list of subcomponents)
Definition: MultiComponent.h:114
Atom
An atom has an unique index in the physical model object, a 3D position, and different basic properti...
Definition: Atom.h:49
Properties::xmlToFields
void xmlToFields(xsd::cxx::tree::attribute_set< char > attrs)
convert the xml node parameters to data fields
Definition: Properties.cpp:41
PhysicalModel::exportPatran
void exportPatran(std::string filename)
Save the geometry (atoms/cells) of this PhysicalModel in the Patran format.
Definition: PhysicalModel.cpp:896
std::GlobalIndexStructurePair
std::pair< unsigned int, Structure * > GlobalIndexStructurePair
definition of a couple (=STL pair) (int , Structure *) this associates a global cell/atom index to th...
Definition: PhysicalModel.h:66
PhysicalModel::getName
const std::string getName() const
Return the name of the physical model.
Definition: PhysicalModel.h:358
CellProperties.h
Properties::getField
std::string getField(unsigned int) const
get the name of field of given index
Definition: Properties.cpp:68
PhysicalModel::getInformativeComponents
MultiComponent * getInformativeComponents() const
get all the informative components
Definition: PhysicalModel.h:377
a
#define a
CellProperties::resetUniqueIndex
static void resetUniqueIndex()
Reinitialize the unique index to zero (usually that what you want to do when you start to load a new ...
Definition: CellProperties.cpp:31
PhysicalModel::getNumberOfAtoms
unsigned int getNumberOfAtoms() const
get the number of atoms
Definition: PhysicalModel.cpp:716
MultiComponent::setPhysicalModel
void setPhysicalModel(PhysicalModel *) override
set the physical model (recursively)
Definition: MultiComponent.cpp:133
PhysicalModel::setName
void setName(const std::string)
set the name of the physical model
Definition: PhysicalModel.h:362
PhysicalModel::getAtom
Atom * getAtom(const unsigned int id)
Get the atom that has the global index given in parameters.
Definition: PhysicalModel.h:385
Structure::getIndex
unsigned int getIndex() const
get the structure unique index (stored in its property)
Definition: Structure.cpp:30
Component::isInstanceOf
virtual bool isInstanceOf(const char *) const =0
pure virtual method, implemented in the child-class
MultiComponent::getNumberOfSubComponents
unsigned int getNumberOfSubComponents() const
return the number of subcomponents
Definition: MultiComponent.h:111
PhysicalModel::setProgress
virtual void setProgress(const float donePercentage)
this method is called during a long process everytime a little bit of the process is finished.
Definition: PhysicalModel.cpp:84
Component::setExclusive
void setExclusive(const bool)
set the exclusive flag
Definition: modeling/libraries/pml/Component.h:141
PhysicalModel::setProgressFunction
PtrToSetProgressFunction setProgressFunction
the progress function
Definition: PhysicalModel.h:346
PhysicalModel::getNumberOfExclusiveComponents
unsigned int getNumberOfExclusiveComponents() const
get the total number of exclusive components
Definition: PhysicalModel.cpp:696
PhysicalModel::isModifiedFlag
bool isModifiedFlag
is the current property state modified
Definition: PhysicalModel.h:299
StructuralComponent
A structural component is composed either by cell or by atoms.
Definition: StructuralComponent.h:52
Cell::makePrintData
bool makePrintData(const StructuralComponent *)
is this sc the one that will be the one that will make the cell to print out all its data or is this ...
Definition: Cell.cpp:147
PMLAbortException
Exception class to handle abortion in the xmlReading Particularly useful to handle constructor's abor...
Definition: PMLAbortException.h:39
Properties::numberOfFields
unsigned int numberOfFields() const
get the number of extra fields found in the PML
Definition: Properties.cpp:63
Structure
Pure virtual class that represent an element of the structure. This implies that every structure coul...
Definition: Structure.h:43
StructuralComponent::addStructure
void addStructure(Structure *s, bool check=true)
Add a Structure in the list (and tells the structure to add this structural component in its list).
Definition: StructuralComponent.h:212
Atom.h
Properties::getName
std::string getName() const
get the name (be careful, this method DOES NOT return a copy, so you got the direct ptr to the name!...
Definition: Properties.h:250
PhysicalModel::PhysicalModel
PhysicalModel() noexcept
Default constructor : this one just initialize everything.
Definition: PhysicalModel.cpp:54
PhysicalModel
This is the main class of this project. Following a nice concept, a physical model is able to represe...
Definition: PhysicalModel.h:86
PhysicalModel::VERSION
static const char * VERSION
Current PML library version.
Definition: PhysicalModel.h:109
PMLAbortException.h
Component::getNumberOfCells
virtual unsigned int getNumberOfCells() const =0
get the total nr of cell of the component
Cell
A cell has an unique index in the physical model object, is composed by atoms, and different basic pr...
Definition: Cell.h:46
PhysicalModel::getProperties
Properties * getProperties()
get all properties (beware of what you do with them!): please consider calling setModified().
Definition: PhysicalModel.h:354
PhysicalModel::optimizeIndexes
void optimizeIndexes()
optimize atom and cell indexes so that each order number is equal to the index
Definition: PhysicalModel.cpp:232
Component::getCell
virtual Cell * getCell(unsigned int) const =0
conveniant method to get cell by order number (not cell index)
PhysicalModel::xmlPrint
void xmlPrint(std::ostream &o, bool opt=false)
print the physical model to an output stream in a XML format (see physicalmodel.xsd for detail about ...
Definition: PhysicalModel.cpp:258
PhysicalModelIO.h
PhysicalModel::cellIndexOptimized
bool cellIndexOptimized
tell if optimizedCellList can be used
Definition: PhysicalModel.h:330
Properties::getString
std::string getString(std::string attName) const
field accessor: get the field attName as a string value, if field does not exist, empty string is ret...
Definition: Properties.h:178
StructuralComponent.h
PhysicalModel::getComponentByName
Component * getComponentByName(const std::string n)
get a structural or multi component by its name.
Definition: PhysicalModel.cpp:553
PhysicalModel::getInformativeComponent
Component * getInformativeComponent(const unsigned int) const
get an informative component by its index in the list
Definition: PhysicalModel.cpp:736
PhysicalModel::isModified
bool isModified()
check if something have changed
Definition: PhysicalModel.h:366
PhysicalModel::exportAnsysMesh
void exportAnsysMesh(std::string filename)
Save the mesh (atoms/cells) of this PhysicalModel in the Ansys format.
Definition: PhysicalModel.cpp:747
Atom::setPosition
void setPosition(const double[3])
set the position of the atom
Definition: Atom.h:125
StructuralComponent::composedBy
virtual ComposedBy composedBy()
return the type of structure composing the structural component: a structural component is either a l...
Definition: StructuralComponent.cpp:296
StructureProperties::HEXAHEDRON
@ HEXAHEDRON
the structure is a hexahedron, it must be a cell and have sub-structures that are atoms
Definition: StructureProperties.h:162
PhysicalModel::informativeComponents
MultiComponent * informativeComponents
Informative components could be overlaping with other components : they are extra components that giv...
Definition: PhysicalModel.h:313
Properties::setName
void setName(std::string)
set the name (use the string = operator)
Definition: Properties.h:254
MultiComponent::getNumberOfCells
unsigned int getNumberOfCells() const override
get the total nr of cell of the component
Definition: MultiComponent.cpp:79
PhysicalModel::clear
void clear()
Clear everything. That allows one to restart an allready instantiated object from scratch.
Definition: PhysicalModel.cpp:91
StructuralComponent::getStructure
Structure * getStructure(const unsigned int) const
get a structure by its index (fisrt structure is at index 0)
Definition: StructuralComponent.h:220
PhysicalModel::parseTree
bool parseTree(std::unique_ptr< physicalModel::PhysicalModel > root, std::string defaultName)
read the xml tree and call other parse methods to build the physicalModel.
Definition: PhysicalModel.cpp:401
StructuralComponent::xmlPrint
void xmlPrint(std::ostream &) const override
print to an output stream in "pseudo" XML format (do nothing if there are no sub structures).
Definition: StructuralComponent.cpp:154
PhysicalModel.h
Component
A component is something that composed something and could also be a part of something.
Definition: modeling/libraries/pml/Component.h:48
PhysicalModel::exclusiveComponents
MultiComponent * exclusiveComponents
Exclusive components are the non-overlaping components : they defined all the components of the physi...
Definition: PhysicalModel.h:306
PhysicalModel::getCell
Cell * getCell(const unsigned int id)
get the cell that has the global index given in parameters.
Definition: PhysicalModel.h:405
PhysicalModel::parseAtoms
bool parseAtoms(physicalModel::Atoms atomsRoot)
read the atom list in the xml tree and build them.
Definition: PhysicalModel.cpp:461
std::GlobalIndexStructureMapIterator
std::map< unsigned int, Structure * > ::iterator GlobalIndexStructureMapIterator
the iterator corresponding to GlobalIndexStructureMap
Definition: PhysicalModel.h:73
Component::setName
void setName(const std::string)
set the name of the component
Definition: modeling/libraries/pml/Component.h:150
physicalModel
Definition: Atom.h:36
PhysicalModel::getAtoms
StructuralComponent * getAtoms() const
get all the atoms
Definition: PhysicalModel.h:380
PhysicalModel::~PhysicalModel
virtual ~PhysicalModel()
destructor
Definition: PhysicalModel.cpp:67
PhysicalModel::xmlRead
void xmlRead(const char *n)
use the XML Parser/Reader to read an XML file conform to physicalmodel.dtd This method may throw a PM...
Definition: PhysicalModel.cpp:332
MultiComponent.h
PhysicalModel::addAtom
bool addAtom(Atom *)
Add a new atom to the atoms' structural component.
Definition: PhysicalModel.cpp:592
PhysicalModel::setInformativeComponents
void setInformativeComponents(MultiComponent *)
set the exclusive multi component. Becareful: the physical model takes control of this MultiComponent
Definition: PhysicalModel.cpp:686
Properties
Describes the properties common to all structures and components.
Definition: Properties.h:59
StructureProperties::QUAD
@ QUAD
the structure is a quad, i.e it must be a cell composed of 4 atoms
Definition: StructureProperties.h:158
PhysicalModel::setAtomPosition
virtual void setAtomPosition(Atom *atom, const double pos[3])
Set the new position of an atom.
Definition: PhysicalModel.cpp:671
std::GlobalIndexStructureMap
std::map< unsigned int, Structure * > GlobalIndexStructureMap
definition of the association set (=map in STL) globalIndexStructureMap.
Definition: PhysicalModel.h:71
PtrToSetProgressFunction
void(*)(const float donePercentage) PtrToSetProgressFunction
Definition of a function/method that could be called by the setProgress method.
Definition: PhysicalModel.h:60
PhysicalModel::atomMap
std::GlobalIndexStructureMap atomMap
the association couple list, which contains the direct map between the atom's global index and the at...
Definition: PhysicalModel.h:340
StructuralComponent::ATOMS
@ ATOMS
the structural component is made of atoms
Definition: StructuralComponent.h:176
MultiComponent
A multi-component stores other components, hence providing a way to have an tree representation of co...
Definition: MultiComponent.h:44
PhysicalModel::getNumberOfInformativeComponents
unsigned int getNumberOfInformativeComponents() const
get the total number of informative components
Definition: PhysicalModel.cpp:706
PhysicalModel::cellMap
std::GlobalIndexStructureMap cellMap
the association couple list, which contains the direct map between the cell's global index and the ce...
Definition: PhysicalModel.h:324
camitk::Initialize
interactor Initialize()
PhysicalModel::positionPtr
double * positionPtr
the big memory space where all the atom's position are stored (it is one big block,...
Definition: PhysicalModel.h:349
StructuralComponent::plannedNumberOfStructures
void plannedNumberOfStructures(const unsigned int)
optimize the I/O of the std:vector structures.
Definition: StructuralComponent.h:270
PhysicalModel::parseComponents
bool parseComponents(physicalModel::MultiComponent mcFather, Component *father, bool isExclusive)
read the exclusive components list in the xml tree and build them.
Definition: PhysicalModel.cpp:490
AtomProperties::resetUniqueIndex
static void resetUniqueIndex()
Reinitialize the unique index to zero (usually that what you want to do when you start to load a new ...
Definition: AtomProperties.cpp:36
PhysicalModel::setAtoms
void setAtoms(StructuralComponent *, bool deleteOld=true)
set the atom structural component.
Definition: PhysicalModel.cpp:572
PhysicalModel::init
void init()
initialization method
Definition: PhysicalModel.cpp:72