| 1 | 
tim | 
1232 | 
#include "ConstraintManager.hpp" | 
| 2 | 
  | 
  | 
#include "ConstraintIterator.hpp" | 
| 3 | 
  | 
  | 
#include "SimInfo.hpp" | 
| 4 | 
  | 
  | 
ConstraintManager::ConstraintManager(SimInfo* info){ | 
| 5 | 
  | 
  | 
  int nmol; | 
| 6 | 
  | 
  | 
  Molecule* mols; | 
| 7 | 
  | 
  | 
 | 
| 8 | 
  | 
  | 
  nmol = info->n_mol; | 
| 9 | 
  | 
  | 
  mols = info->molecules; | 
| 10 | 
  | 
  | 
 | 
| 11 | 
  | 
  | 
  for(int i = 0; i < nmol; i ++) | 
| 12 | 
  | 
  | 
    addConstraints(&mols[i]); | 
| 13 | 
  | 
  | 
   | 
| 14 | 
  | 
  | 
} | 
| 15 | 
  | 
  | 
 | 
| 16 | 
  | 
  | 
ConstraintManager::~ConstraintManager(){ | 
| 17 | 
  | 
  | 
  list<vector<ConstraintElement*> >::iterator listIter; | 
| 18 | 
  | 
  | 
  vector<ConstraintElement*>::iterator vectorIter; | 
| 19 | 
  | 
  | 
   | 
| 20 | 
  | 
  | 
  for(listIter = consElements.begin();listIter != consElements.end(); ){ | 
| 21 | 
  | 
  | 
     | 
| 22 | 
  | 
  | 
    for(vectorIter = listIter->begin(); vectorIter != listIter->end(); ++vectorIter) | 
| 23 | 
  | 
  | 
      delete *vectorIter; | 
| 24 | 
  | 
  | 
 | 
| 25 | 
  | 
  | 
    listIter = consElements.erase(listIter); | 
| 26 | 
  | 
  | 
  } | 
| 27 | 
  | 
  | 
   | 
| 28 | 
  | 
  | 
} | 
| 29 | 
  | 
  | 
 | 
| 30 | 
  | 
  | 
void ConstraintManager::addConstraints(Molecule* mol){ | 
| 31 | 
  | 
  | 
  MolIterInfoMap::iterator foundResult; | 
| 32 | 
  | 
  | 
  ListIteratorInfo iterInfo; | 
| 33 | 
  | 
  | 
  StuntDouble* sd; | 
| 34 | 
  | 
  | 
  ConstraintElement* ce; | 
| 35 | 
  | 
  | 
  vector<StuntDouble*> integrableObjects; | 
| 36 | 
  | 
  | 
  vector<StuntDouble*>::iterator sdIter; | 
| 37 | 
  | 
  | 
  vector<ConstraintElement*> tempConsElem; | 
| 38 | 
  | 
  | 
   | 
| 39 | 
  | 
  | 
  foundResult = iterInfoMap.find(mol); | 
| 40 | 
tim | 
1234 | 
  if (foundResult == iterInfoMap.end()){ | 
| 41 | 
tim | 
1232 | 
 | 
| 42 | 
  | 
  | 
    //to improve efficiency, we will not record the molecule without constraint | 
| 43 | 
tim | 
1234 | 
    if (mol->getNConstrains() > 0){ | 
| 44 | 
tim | 
1232 | 
 | 
| 45 | 
  | 
  | 
      //if a molecule has constraint, all of its myIntegrableOjects must be constraint elements | 
| 46 | 
  | 
  | 
 | 
| 47 | 
  | 
  | 
      integrableObjects = mol->getIntegrableObjects(); | 
| 48 | 
  | 
  | 
 | 
| 49 | 
  | 
  | 
      for(sdIter = integrableObjects.begin(); sdIter != integrableObjects.end(); sdIter++){ | 
| 50 | 
tim | 
1234 | 
        sd = *sdIter; | 
| 51 | 
tim | 
1232 | 
        if(sd->isRigidBody()) | 
| 52 | 
  | 
  | 
          ce = new ConstraintRigidBody((RigidBody*)sd, 0); | 
| 53 | 
  | 
  | 
        else | 
| 54 | 
  | 
  | 
          ce = new ConstraintAtom((Atom *) sd); | 
| 55 | 
  | 
  | 
         | 
| 56 | 
  | 
  | 
        tempConsElem.push_back(ce); | 
| 57 | 
  | 
  | 
      } | 
| 58 | 
  | 
  | 
 | 
| 59 | 
  | 
  | 
      consElements.push_back(tempConsElem);     | 
| 60 | 
  | 
  | 
      iterInfo.firstElement = --consElements.end(); | 
| 61 | 
  | 
  | 
 | 
| 62 | 
  | 
  | 
      //just copy the constraint pairs from molecule; | 
| 63 | 
  | 
  | 
 | 
| 64 | 
  | 
  | 
      consPairs.push_back(mol->getConstraintPairs()); | 
| 65 | 
  | 
  | 
      iterInfo.firstPair = --consPairs.end(); | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
      iterInfoMap[mol] = iterInfo; | 
| 68 | 
  | 
  | 
       | 
| 69 | 
  | 
  | 
    } | 
| 70 | 
  | 
  | 
  } | 
| 71 | 
  | 
  | 
} | 
| 72 | 
  | 
  | 
 | 
| 73 | 
  | 
  | 
void ConstraintManager::removeConstraints(Molecule* mol){ | 
| 74 | 
  | 
  | 
  MolIterInfoMap::iterator foundResult; | 
| 75 | 
  | 
  | 
  ListIteratorInfo iterInfo; | 
| 76 | 
  | 
  | 
   | 
| 77 | 
  | 
  | 
  foundResult = iterInfoMap.find(mol); | 
| 78 | 
  | 
  | 
 | 
| 79 | 
  | 
  | 
  if (foundResult != iterInfoMap.end()){ | 
| 80 | 
  | 
  | 
    iterInfo = foundResult->second; | 
| 81 | 
  | 
  | 
    consElements.erase(iterInfo.firstElement); | 
| 82 | 
  | 
  | 
    consPairs.erase(iterInfo.firstPair); | 
| 83 | 
  | 
  | 
    iterInfoMap.erase(foundResult); | 
| 84 | 
  | 
  | 
  } | 
| 85 | 
  | 
  | 
 | 
| 86 | 
  | 
  | 
} | 
| 87 | 
  | 
  | 
 | 
| 88 | 
tim | 
1452 | 
ConstraintElementIterator* ConstraintManager::createElementIterator(){ | 
| 89 | 
tim | 
1232 | 
    return new ConstraintElementIterator(consElements); | 
| 90 | 
  | 
  | 
} | 
| 91 | 
  | 
  | 
 | 
| 92 | 
tim | 
1452 | 
ConstraintPairIterator* ConstraintManager::createPairIterator(){ | 
| 93 | 
tim | 
1232 | 
  return new ConstraintPairIterator(consPairs); | 
| 94 | 
  | 
  | 
} | 
| 95 | 
  | 
  | 
 |