| 1 | 
gezelter | 
1334 | 
#include "ZconsVisitor.hpp" | 
| 2 | 
  | 
  | 
#include <cmath> | 
| 3 | 
  | 
  | 
 | 
| 4 | 
  | 
  | 
ZConsVisitor::ZConsVisitor(SimInfo* info) : BaseVisitor(), zconsReader(NULL){ | 
| 5 | 
  | 
  | 
  GenericData* data; | 
| 6 | 
  | 
  | 
  DoubleData* tolerance;   | 
| 7 | 
  | 
  | 
  ZConsParaData* zConsParaData; | 
| 8 | 
  | 
  | 
  StringData* filename;  | 
| 9 | 
  | 
  | 
  DoubleData* sampleTime; | 
| 10 | 
  | 
  | 
  vector<ZConsParaItem>* parameters; | 
| 11 | 
  | 
  | 
 | 
| 12 | 
  | 
  | 
  this->info = info; | 
| 13 | 
  | 
  | 
  visitorName = "ZConsVisitor"; | 
| 14 | 
  | 
  | 
   | 
| 15 | 
  | 
  | 
  //retrieve tolerance for z-constraint molecuels | 
| 16 | 
  | 
  | 
  data = info->getProperty(ZCONSTOL_ID); | 
| 17 | 
  | 
  | 
 | 
| 18 | 
  | 
  | 
  if (!data){ | 
| 19 | 
  | 
  | 
    cerr << "Can not get zconstraint tolerance from SimInfo" << endl; | 
| 20 | 
  | 
  | 
    haveZcons = false;   | 
| 21 | 
  | 
  | 
    return; | 
| 22 | 
  | 
  | 
  } | 
| 23 | 
  | 
  | 
  else{ | 
| 24 | 
  | 
  | 
    tolerance = dynamic_cast<DoubleData*>(data); | 
| 25 | 
  | 
  | 
 | 
| 26 | 
  | 
  | 
    if (!tolerance){ | 
| 27 | 
  | 
  | 
      cerr << "Can not get zconstraint tolerance  from SimInfo" << endl; | 
| 28 | 
  | 
  | 
      haveZcons = false;     | 
| 29 | 
  | 
  | 
      return; | 
| 30 | 
  | 
  | 
    } | 
| 31 | 
  | 
  | 
    else{ | 
| 32 | 
  | 
  | 
      zconsTol = tolerance->getData(); | 
| 33 | 
  | 
  | 
    } | 
| 34 | 
  | 
  | 
  } | 
| 35 | 
  | 
  | 
 | 
| 36 | 
  | 
  | 
  //retrieve sample time of z-contraint  | 
| 37 | 
  | 
  | 
  data = info->getProperty(ZCONSTIME_ID); | 
| 38 | 
  | 
  | 
 | 
| 39 | 
  | 
  | 
  if (!data){ | 
| 40 | 
  | 
  | 
    cerr << "Can not get zcons time  from SimInfo" << endl; | 
| 41 | 
  | 
  | 
    haveZcons = false;     | 
| 42 | 
  | 
  | 
    return; | 
| 43 | 
  | 
  | 
  } | 
| 44 | 
  | 
  | 
  else{ | 
| 45 | 
  | 
  | 
    sampleTime = dynamic_cast<DoubleData*>(data); | 
| 46 | 
  | 
  | 
 | 
| 47 | 
  | 
  | 
    if (!sampleTime){ | 
| 48 | 
  | 
  | 
      cerr << "Can not get zcons time  from SimInfo" << endl; | 
| 49 | 
  | 
  | 
      haveZcons = false;     | 
| 50 | 
  | 
  | 
      return; | 
| 51 | 
  | 
  | 
    } | 
| 52 | 
  | 
  | 
    else{ | 
| 53 | 
  | 
  | 
     zconsTime = sampleTime->getData(); | 
| 54 | 
  | 
  | 
    } | 
| 55 | 
  | 
  | 
  } | 
| 56 | 
  | 
  | 
 | 
| 57 | 
  | 
  | 
  //retrieve index of z-constraint molecules | 
| 58 | 
  | 
  | 
  data = info->getProperty(ZCONSPARADATA_ID); | 
| 59 | 
  | 
  | 
  if (!data){ | 
| 60 | 
  | 
  | 
    cerr << "Can not get index of z-constraint molecules from SimInfo" << endl; | 
| 61 | 
  | 
  | 
    haveZcons = false; | 
| 62 | 
  | 
  | 
    return; | 
| 63 | 
  | 
  | 
  } | 
| 64 | 
  | 
  | 
  else{ | 
| 65 | 
  | 
  | 
    zConsParaData = dynamic_cast<ZConsParaData*>(data); | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
    if (!zConsParaData){ | 
| 68 | 
  | 
  | 
      cerr << "Can not get index of z-constraint molecules from SimInfo" << endl; | 
| 69 | 
  | 
  | 
      haveZcons = false; | 
| 70 | 
  | 
  | 
      return; | 
| 71 | 
  | 
  | 
    } | 
| 72 | 
  | 
  | 
    else{ | 
| 73 | 
  | 
  | 
      vector<ZConsParaItem>::iterator i; | 
| 74 | 
  | 
  | 
      Molecule* mol; | 
| 75 | 
  | 
  | 
 | 
| 76 | 
  | 
  | 
      parameters = zConsParaData->getData(); | 
| 77 | 
  | 
  | 
      for(i = parameters->begin(); i != parameters->end(); ++i){ | 
| 78 | 
  | 
  | 
 | 
| 79 | 
  | 
  | 
        mol = findZconsMol(i->zconsIndex); | 
| 80 | 
  | 
  | 
        if(mol != NULL) | 
| 81 | 
  | 
  | 
          zconsMol.push_back(mol); | 
| 82 | 
  | 
  | 
 | 
| 83 | 
  | 
  | 
      } | 
| 84 | 
  | 
  | 
 | 
| 85 | 
  | 
  | 
      if(zconsMol.size() < 1){ | 
| 86 | 
  | 
  | 
        cerr << "number of zconstraint molecules is less than one" << endl; | 
| 87 | 
  | 
  | 
        haveZcons = false; | 
| 88 | 
  | 
  | 
        return; | 
| 89 | 
  | 
  | 
      } | 
| 90 | 
  | 
  | 
       | 
| 91 | 
  | 
  | 
    }//end if (!zConsParaData) | 
| 92 | 
  | 
  | 
 | 
| 93 | 
  | 
  | 
  }//end  if (!data   | 
| 94 | 
  | 
  | 
   | 
| 95 | 
  | 
  | 
  //retrieve output filename of z force | 
| 96 | 
  | 
  | 
  data = info->getProperty(ZCONSFILENAME_ID); | 
| 97 | 
  | 
  | 
  if (!data){ | 
| 98 | 
  | 
  | 
    cerr << "Can not get filename of z-constraint output from SimInfo" << endl; | 
| 99 | 
  | 
  | 
    haveZcons = false; | 
| 100 | 
  | 
  | 
    return; | 
| 101 | 
  | 
  | 
  } | 
| 102 | 
  | 
  | 
  else{ | 
| 103 | 
  | 
  | 
    filename = dynamic_cast<StringData*>(data); | 
| 104 | 
  | 
  | 
 | 
| 105 | 
  | 
  | 
    if (!filename){ | 
| 106 | 
  | 
  | 
      cerr << "Can not get filename of z-constraint output from SimInfo" << endl; | 
| 107 | 
  | 
  | 
      haveZcons = false; | 
| 108 | 
  | 
  | 
      return; | 
| 109 | 
  | 
  | 
    } | 
| 110 | 
  | 
  | 
    else{ | 
| 111 | 
  | 
  | 
      zconsFilename = filename->getData(); | 
| 112 | 
  | 
  | 
    } | 
| 113 | 
  | 
  | 
  } | 
| 114 | 
  | 
  | 
 | 
| 115 | 
  | 
  | 
  zconsReader = new ZConsReader(info); | 
| 116 | 
  | 
  | 
 | 
| 117 | 
  | 
  | 
  if (zconsReader->hasNextFrame()) | 
| 118 | 
  | 
  | 
  zconsReader->readNextFrame(); | 
| 119 | 
  | 
  | 
   | 
| 120 | 
  | 
  | 
  haveZcons = true; | 
| 121 | 
  | 
  | 
} | 
| 122 | 
  | 
  | 
 | 
| 123 | 
  | 
  | 
ZConsVisitor::~ZConsVisitor(){ | 
| 124 | 
  | 
  | 
  if(!zconsReader)  | 
| 125 | 
  | 
  | 
    delete zconsReader; | 
| 126 | 
  | 
  | 
   | 
| 127 | 
  | 
  | 
} | 
| 128 | 
  | 
  | 
 | 
| 129 | 
  | 
  | 
void ZConsVisitor::visit(Atom* atom){ | 
| 130 | 
  | 
  | 
  string prefix; | 
| 131 | 
  | 
  | 
  if(isZconstraint(atom->getIndex(), prefix)) | 
| 132 | 
  | 
  | 
    internalVisit(atom, prefix); | 
| 133 | 
  | 
  | 
} | 
| 134 | 
  | 
  | 
 | 
| 135 | 
  | 
  | 
void ZConsVisitor::visit(DirectionalAtom* datom){ | 
| 136 | 
  | 
  | 
  string prefix; | 
| 137 | 
  | 
  | 
   | 
| 138 | 
  | 
  | 
  if(isZconstraint(datom->getIndex(), prefix)) | 
| 139 | 
  | 
  | 
    internalVisit(datom, prefix); | 
| 140 | 
  | 
  | 
} | 
| 141 | 
  | 
  | 
 | 
| 142 | 
  | 
  | 
void ZConsVisitor::visit(RigidBody* rb){ | 
| 143 | 
  | 
  | 
  string prefix; | 
| 144 | 
  | 
  | 
  vector<Atom*> atoms; | 
| 145 | 
  | 
  | 
   | 
| 146 | 
  | 
  | 
  atoms = rb->getAtoms(); | 
| 147 | 
  | 
  | 
     | 
| 148 | 
  | 
  | 
  if(isZconstraint(atoms[0]->getIndex(), prefix)) | 
| 149 | 
  | 
  | 
    internalVisit(rb, prefix); | 
| 150 | 
  | 
  | 
} | 
| 151 | 
  | 
  | 
 | 
| 152 | 
  | 
  | 
Molecule* ZConsVisitor::findZconsMol(int index){ | 
| 153 | 
  | 
  | 
  Molecule* mols; | 
| 154 | 
  | 
  | 
  mols = info->molecules; | 
| 155 | 
  | 
  | 
  for(int i =0; i < info->n_mol; i++) | 
| 156 | 
  | 
  | 
    if(index == mols[i].getMyIndex()) | 
| 157 | 
  | 
  | 
      return &mols[i]; | 
| 158 | 
  | 
  | 
 | 
| 159 | 
  | 
  | 
  return NULL; | 
| 160 | 
  | 
  | 
} | 
| 161 | 
  | 
  | 
 | 
| 162 | 
  | 
  | 
void ZConsVisitor::update(){ | 
| 163 | 
  | 
  | 
  Molecule* mol; | 
| 164 | 
  | 
  | 
  double com[3]; | 
| 165 | 
  | 
  | 
  ZConsState state; | 
| 166 | 
  | 
  | 
  Atom** atoms; | 
| 167 | 
  | 
  | 
  int natoms; | 
| 168 | 
  | 
  | 
 | 
| 169 | 
  | 
  | 
  getZconsPos(info->currentTime); | 
| 170 | 
  | 
  | 
   | 
| 171 | 
  | 
  | 
  for(size_t i = 0; i < zconsMol.size(); i++){ | 
| 172 | 
  | 
  | 
    zconsMol[i]->getCOM(com); | 
| 173 | 
  | 
  | 
 | 
| 174 | 
  | 
  | 
    if(fabs(com[2] - zconsPos[i]) < zconsTol) | 
| 175 | 
  | 
  | 
      state = zsFixed; | 
| 176 | 
  | 
  | 
    else | 
| 177 | 
  | 
  | 
      state = zsMoving; | 
| 178 | 
  | 
  | 
 | 
| 179 | 
  | 
  | 
    //update the state of zconstraint atom | 
| 180 | 
  | 
  | 
    natoms = zconsMol[i]->getNAtoms(); | 
| 181 | 
  | 
  | 
    atoms = zconsMol[i]->getMyAtoms();     | 
| 182 | 
  | 
  | 
    for(int j =0; j < natoms; j++) | 
| 183 | 
  | 
  | 
      zconsState[atoms[j]->getIndex()] = state; | 
| 184 | 
  | 
  | 
  } | 
| 185 | 
  | 
  | 
   | 
| 186 | 
  | 
  | 
} | 
| 187 | 
  | 
  | 
 | 
| 188 | 
  | 
  | 
void ZConsVisitor::getZconsPos(double time){ | 
| 189 | 
  | 
  | 
 | 
| 190 | 
  | 
  | 
  vector<double> tempPos; | 
| 191 | 
  | 
  | 
  vector<double> prevPos; | 
| 192 | 
  | 
  | 
  double tempTime; | 
| 193 | 
  | 
  | 
 | 
| 194 | 
  | 
  | 
  while(true){ | 
| 195 | 
  | 
  | 
    tempTime = zconsReader->getCurTime(); | 
| 196 | 
  | 
  | 
 | 
| 197 | 
  | 
  | 
    zconsPos = zconsReader->getZConsPos(); | 
| 198 | 
  | 
  | 
     | 
| 199 | 
  | 
  | 
    if(tempTime >= time) | 
| 200 | 
  | 
  | 
      return; | 
| 201 | 
  | 
  | 
     | 
| 202 | 
  | 
  | 
    zconsReader->readNextFrame(); | 
| 203 | 
  | 
  | 
 | 
| 204 | 
  | 
  | 
  }  | 
| 205 | 
  | 
  | 
 | 
| 206 | 
  | 
  | 
} | 
| 207 | 
  | 
  | 
 | 
| 208 | 
  | 
  | 
void ZConsVisitor::internalVisit(StuntDouble* sd, const string& prefix){ | 
| 209 | 
  | 
  | 
  GenericData* data; | 
| 210 | 
  | 
  | 
  AtomData* atomData; | 
| 211 | 
  | 
  | 
  AtomInfo* atomInfo; | 
| 212 | 
  | 
  | 
  vector<AtomInfo*>::iterator iter; | 
| 213 | 
  | 
  | 
 | 
| 214 | 
  | 
  | 
   | 
| 215 | 
  | 
  | 
  //if there is not atom data, just skip it | 
| 216 | 
  | 
  | 
  data = sd->getProperty("ATOMDATA"); | 
| 217 | 
  | 
  | 
  if(data != NULL){ | 
| 218 | 
  | 
  | 
    atomData = dynamic_cast<AtomData*>(data);   | 
| 219 | 
  | 
  | 
    if(atomData == NULL) | 
| 220 | 
  | 
  | 
      return; | 
| 221 | 
  | 
  | 
  } | 
| 222 | 
  | 
  | 
  else | 
| 223 | 
  | 
  | 
    return; | 
| 224 | 
  | 
  | 
 | 
| 225 | 
  | 
  | 
  for(atomInfo  = atomData->beginAtomInfo(iter); atomInfo; atomInfo = atomData->nextAtomInfo(iter)) | 
| 226 | 
  | 
  | 
    (atomInfo->AtomType).insert(0, prefix); | 
| 227 | 
  | 
  | 
} | 
| 228 | 
  | 
  | 
 | 
| 229 | 
  | 
  | 
 | 
| 230 | 
  | 
  | 
bool ZConsVisitor::isZconstraint(int index, string& prefix){ | 
| 231 | 
  | 
  | 
  map<int, ZConsState>::iterator i; | 
| 232 | 
  | 
  | 
  string prefixString[] = {"ZF", "ZM"}; | 
| 233 | 
  | 
  | 
   | 
| 234 | 
  | 
  | 
  i = zconsState.find(index); | 
| 235 | 
  | 
  | 
  if(i !=zconsState.end()){ | 
| 236 | 
  | 
  | 
    prefix = prefixString[(*i).second]; | 
| 237 | 
  | 
  | 
    return true; | 
| 238 | 
  | 
  | 
  } | 
| 239 | 
  | 
  | 
  else{ | 
| 240 | 
  | 
  | 
    prefix = ""; | 
| 241 | 
  | 
  | 
    return false; | 
| 242 | 
  | 
  | 
  } | 
| 243 | 
  | 
  | 
} | 
| 244 | 
  | 
  | 
 | 
| 245 | 
  | 
  | 
const string ZConsVisitor::toString(){ | 
| 246 | 
  | 
  | 
  char buffer[65535]; | 
| 247 | 
  | 
  | 
  string result; | 
| 248 | 
  | 
  | 
   | 
| 249 | 
  | 
  | 
  sprintf(buffer ,"------------------------------------------------------------------\n"); | 
| 250 | 
  | 
  | 
  result += buffer; | 
| 251 | 
  | 
  | 
 | 
| 252 | 
  | 
  | 
  sprintf(buffer ,"Visitor name: %s\n", visitorName.c_str()); | 
| 253 | 
  | 
  | 
  result += buffer; | 
| 254 | 
  | 
  | 
   | 
| 255 | 
  | 
  | 
  sprintf(buffer , "number of zconstraint molecule: %d\n", zconsMol.size()); | 
| 256 | 
  | 
  | 
  result += buffer; | 
| 257 | 
  | 
  | 
 | 
| 258 | 
  | 
  | 
  sprintf(buffer , "zconstraint tolerance = %lf\n", zconsTol); | 
| 259 | 
  | 
  | 
  result += buffer; | 
| 260 | 
  | 
  | 
 | 
| 261 | 
  | 
  | 
  sprintf(buffer , "zconstraint sample time = %lf\n", zconsTime); | 
| 262 | 
  | 
  | 
  result += buffer; | 
| 263 | 
  | 
  | 
 | 
| 264 | 
  | 
  | 
  sprintf(buffer , "zconstraint output filename = %s\n", zconsFilename.c_str()); | 
| 265 | 
  | 
  | 
  result += buffer; | 
| 266 | 
  | 
  | 
   | 
| 267 | 
  | 
  | 
  for(size_t i = 0; i < zconsMol.size(); ++i){ | 
| 268 | 
  | 
  | 
    sprintf(buffer ,"zconstraint molecule[%d] = %d\n", i, zconsMol[i]->getMyIndex()); | 
| 269 | 
  | 
  | 
    result += buffer; | 
| 270 | 
  | 
  | 
 | 
| 271 | 
  | 
  | 
  } | 
| 272 | 
  | 
  | 
 | 
| 273 | 
  | 
  | 
   | 
| 274 | 
  | 
  | 
  sprintf(buffer ,"------------------------------------------------------------------\n"); | 
| 275 | 
  | 
  | 
  result += buffer; | 
| 276 | 
  | 
  | 
 | 
| 277 | 
  | 
  | 
  return result; | 
| 278 | 
  | 
  | 
} |