00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "FieldObject.h"
00010 #include "PotentialfieldComposition.h"
00011
00012
00013
00014 Object::Object()
00015 {
00016 refToPfieldComposition = 0;
00017 geometry = 0;
00018 absGeometry = 0;
00019 function = 0;
00020 tangentialField = NO_TANGENTIALFIELD;
00021 }
00022
00023
00024 Object::Object(const std::string& name, ObjectType objectType)
00025 {
00026 this->geometry = 0;
00027 this->absGeometry = 0;
00028 this->name = name;
00029 this->objectType = objectType;
00030 refToPfieldComposition = 0;
00031 active = true;
00032 function = 0;
00033 tangentialField = NO_TANGENTIALFIELD;
00034 }
00035
00036
00037 Object::~Object()
00038 {
00039 if(geometry != 0)
00040 {
00041 delete geometry;
00042 }
00043 if(absGeometry != 0)
00044 {
00045 delete absGeometry;
00046 }
00047 if(function != 0)
00048 {
00049 delete function;
00050 }
00051 }
00052
00053
00054 Object* Object::createInstance(const std::string& instanceName)
00055 {
00056 Object* instance = new Object();
00057 (*instance) = (*this);
00058 instance->name = instanceName;
00059 return instance;
00060 }
00061
00062
00063 void Object::operator = (const Object& other)
00064 {
00065 name = other.name;
00066 objectType = other.objectType;
00067 active = other.active;
00068 setPose(other.pose);
00069 setActivation(other.active);
00070 if(function != 0)
00071 {
00072 delete function;
00073 function = 0;
00074 }
00075 setFunction(other.function);
00076 if(geometry != 0)
00077 {
00078 delete geometry;
00079 geometry = 0;
00080 }
00081 if(absGeometry != 0)
00082 {
00083 delete absGeometry;
00084 absGeometry = 0;
00085 }
00086 setGeometry(other.geometry);
00087 setTangentialField(other.tangentialField);
00088 setStatic(other.isStaticVar, other.dynamicPoseId);
00089 setPfieldCompositionRef(other.refToPfieldComposition);
00090 switch(other.fieldType)
00091 {
00092 case SECTOR_FIELD: setField(other.fieldType, other.sector);
00093 default: setField(other.fieldType);
00094 }
00095 }
00096
00097
00098 Object* Object::getCopy()
00099 {
00100 Object* newObject = new Object();
00101 (*newObject) = (*this);
00102 return newObject;
00103 }
00104
00105
00106 void Object::getMinimalCopyFrom(Object* other)
00107 {
00108 if(other->isActive())
00109 {
00110 setPose(other->getPose());
00111 setActivation(true);
00112 }
00113 else
00114 {
00115 setActivation(false);
00116 }
00117 }
00118
00119
00120 void Object::setGeometry(PfieldGeometricObject* geometry)
00121 {
00122 if(geometry == 0)
00123 {
00124 this->geometry = 0;
00125 absGeometry = 0;
00126 }
00127 else
00128 {
00129 this->geometry = geometry->clone();
00130 absGeometry = geometry->getAbs(pose);
00131 }
00132 }
00133
00134
00135 void Object::setPose(const PfPose& pose)
00136 {
00137 this->pose = pose;
00138 if(absGeometry != 0)
00139 {
00140 absGeometry->setAbsoluteFromOther(pose, geometry);
00141 }
00142 }
00143
00144
00145 double Object::computeChargeAt(const PfPose& otherPose)
00146 {
00147 double charge = 0.0;
00148 if(active)
00149 {
00150 if(pose.hasProbabilityDistribution)
00151 {
00152 for(unsigned int j=0; j<pose.probabilityDistribution.size(); j++)
00153 {
00154 PfPose ownProbPose(pose.probabilityDistribution[j]);
00155 if(otherPose.hasProbabilityDistribution)
00156 {
00157 for(unsigned int i=0; i<otherPose.probabilityDistribution.size(); i++)
00158 {
00159 PfPose probPose(otherPose.probabilityDistribution[i]);
00160 double probCharge(computeChargeForSinglePose(ownProbPose, probPose));
00161 probCharge *= (probPose.probability * ownProbPose.probability);
00162 charge += probCharge;
00163 }
00164 }
00165 else
00166 {
00167 charge += ownProbPose.probability *
00168 computeChargeForSinglePose(ownProbPose, otherPose);
00169 }
00170 }
00171 }
00172 else
00173 {
00174 if(otherPose.hasProbabilityDistribution)
00175 {
00176 for(unsigned int i=0; i<otherPose.probabilityDistribution.size(); i++)
00177 {
00178 PfPose probPose = otherPose.probabilityDistribution[i];
00179 double probCharge(computeChargeForSinglePose(pose, probPose));
00180 probCharge *= probPose.probability;
00181 charge += probCharge;
00182 }
00183 }
00184 else
00185 {
00186 charge = computeChargeForSinglePose(pose, otherPose);
00187 }
00188 }
00189 }
00190 return charge;
00191 }
00192
00193
00194 PfVec Object::computeAbsFieldVecAt(const PfPose& otherPose)
00195 {
00196 PfVec result(0.0,0.0);
00197 if(active)
00198 {
00199 if(pose.hasProbabilityDistribution)
00200 {
00201 for(unsigned int j=0; j<pose.probabilityDistribution.size(); j++)
00202 {
00203 PfPose ownProbPose(pose.probabilityDistribution[j]);
00204 if(otherPose.hasProbabilityDistribution)
00205 {
00206 for(unsigned int i=0; i<otherPose.probabilityDistribution.size(); i++)
00207 {
00208 PfPose probPose(otherPose.probabilityDistribution[i]);
00209 PfVec probVec(computeGradientVecForSinglePose(ownProbPose, probPose));
00210 probVec *= (probPose.probability * ownProbPose.probability);
00211 result += probVec;
00212 }
00213 }
00214 else
00215 {
00216 result += (computeGradientVecForSinglePose(ownProbPose, otherPose) *
00217 ownProbPose.probability);
00218 }
00219 }
00220 }
00221 else
00222 {
00223 if(otherPose.hasProbabilityDistribution)
00224 {
00225 for(unsigned int i=0; i<otherPose.probabilityDistribution.size(); i++)
00226 {
00227 PfPose probPose = otherPose.probabilityDistribution[i];
00228 PfVec probVec(computeGradientVecForSinglePose(pose, probPose));
00229 probVec *= otherPose.probabilityDistribution[i].probability;
00230 result += probVec;
00231 }
00232 }
00233 else
00234 {
00235 result = computeGradientVecForSinglePose(pose, otherPose);
00236 }
00237 }
00238 if(tangentialField == CLOCKWISE)
00239 {
00240 if(objectType == REPULSIVE)
00241 {
00242 result.rotate(pi_2);
00243 }
00244 else
00245 {
00246 result.rotate(-pi_2);
00247 }
00248 }
00249 else if(tangentialField == COUNTER_CLOCKWISE)
00250 {
00251 if(objectType == REPULSIVE)
00252 {
00253 result.rotate(-pi_2);
00254 }
00255 else
00256 {
00257 result.rotate(pi_2);
00258 }
00259 }
00260 }
00261 return result;
00262 }
00263
00264
00265 inline double Object::computeChargeForSinglePose(const PfPose& objectPose,
00266 const PfPose& position) const
00267 {
00268 double charge(0.0);
00269 switch(fieldType)
00270 {
00271 case POINT_FIELD: charge = computeChargeForPointfield(objectPose, position, function);
00272 break;
00273 case SHAPE_FIELD: charge = computeChargeForShapefield(objectPose, position, function, geometry);
00274 break;
00275 case SECTOR_FIELD: charge = computeChargeForSectorfield(objectPose, position, function, sector);
00276 break;
00277 }
00278 return charge;
00279 }
00280
00281
00282 inline PfVec Object::computeGradientVecForSinglePose(const PfPose& objectPose,
00283 const PfPose& position) const
00284 {
00285 PfVec gradient;
00286 switch(fieldType)
00287 {
00288 case POINT_FIELD: gradient = computeGradientForPointfield(objectPose, position, function);
00289 break;
00290 case SHAPE_FIELD: gradient = computeGradientForShapefield(objectPose, position, function,
00291 geometry, objectType);
00292 break;
00293 case SECTOR_FIELD: gradient = computeGradientForSectorfield(objectPose, position, function,
00294 sector);
00295 break;
00296 }
00297 return gradient;
00298 }
00299
00300
00301 void Object::setFunction(PotentialfieldFunction* function)
00302 {
00303 if(this->function != 0)
00304 {
00305 delete (this->function);
00306 this->function = 0;
00307 }
00308 if(function != 0)
00309 {
00310 this->function = function->clone();
00311 }
00312 }
00313
00314
00315 void Object::setField(FieldType type)
00316 {
00317 fieldType = type;
00318 }
00319
00320
00321 void Object::setField(FieldType type, const Sector& sector)
00322 {
00323 fieldType = type;
00324 this->sector = sector;
00325 this->sector.crossFunction = sector.crossFunction->clone();
00326 }
00327
00328
00329 void Object::updateData()
00330 {
00331 ObjectStateDescription desc;
00332 desc = refToPfieldComposition->getDescriptionFromId(dynamicPoseId);
00333 setPose(desc.pose);
00334 active = desc.isActive;
00335 }
00336
00337
00338 void Object::computeAbsGeometry()
00339 {
00340 absGeometry->setAbsoluteFromOther(pose, geometry);
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377