00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "thirdparty/CogMapObjects.h"
00022
00023 namespace cmlabs
00024 {
00025 CMMeasurement::CMMeasurement() {
00026 value = uncertainty = CMMEASUREMENT_NOVALUE;
00027 values = NULL;
00028 uncertainties = NULL;
00029 len = 1;
00030 }
00031
00032 CMMeasurement::CMMeasurement(const JString& name, unsigned int dim) {
00033 value = uncertainty = CMMEASUREMENT_NOVALUE;
00034 if (dim <= 1) {
00035 values = NULL;
00036 uncertainties = NULL;
00037 len = 1;
00038 }
00039 else {
00040 len = dim;
00041 values = new double[len];
00042 uncertainties = new double[len];
00043 }
00044 }
00045
00046 CMMeasurement::~CMMeasurement() {
00047 value = uncertainty = CMMEASUREMENT_NOVALUE;
00048 if (values != NULL)
00049 delete(values);
00050 if (uncertainties != NULL)
00051 delete(uncertainties);
00052 len = 1;
00053 values = NULL;
00054 uncertainties = NULL;
00055 }
00056
00057 CMMeasurement::CMMeasurement(const JString& xml) {
00058 value = uncertainty = CMMEASUREMENT_NOVALUE;
00059 values = NULL;
00060 uncertainties = NULL;
00061 len = 1;
00062 Object::fromXML(xml);
00063 }
00064
00065 CMMeasurement::CMMeasurement(XMLNode* node) {
00066 value = uncertainty = CMMEASUREMENT_NOVALUE;
00067 values = NULL;
00068 uncertainties = NULL;
00069 len = 1;
00070 fromXML(node);
00071 }
00072
00073 Object* CMMeasurement::clone() const {
00074 CMMeasurement* m = new CMMeasurement(name, len);
00075 m->value = value;
00076 m->uncertainties = uncertainties;
00077 if (values != NULL)
00078 memcpy(m->values, values, len*sizeof(double));
00079 if (uncertainties != NULL)
00080 memcpy(m->uncertainties, uncertainties, len*sizeof(double));
00081 return m;
00082 }
00083
00084 bool CMMeasurement::reset() {
00085 value = uncertainty = CMMEASUREMENT_NOVALUE;
00086 if (values != NULL)
00087 delete(values);
00088 if (uncertainties != NULL)
00089 delete(uncertainties);
00090 len = 1;
00091 values = NULL;
00092 uncertainties = NULL;
00093 return true;
00094 }
00095
00096 bool CMMeasurement::equals(const Object* o) const {
00097 if (o == NULL) return false;
00098 if (!this->isSameClass(o))
00099 return false;
00100 return this->equals(*(CMMeasurement*)o);
00101 }
00102
00103 bool CMMeasurement::equals(const CMMeasurement& m) const {
00104 return equals(&m);
00105 }
00106
00107 bool CMMeasurement::equals(const CMMeasurement* m) const {
00108 if (len == 1)
00109 return ( (value == m->value) && (uncertainty == m->uncertainty) );
00110 else {
00111 if (len != m->len)
00112 return false;
00113 for (unsigned int n=0; n<len; n++) {
00114 if (values[n] != m->values[n])
00115 return false;
00116 if (uncertainties[n] != m->uncertainties[n])
00117 return false;
00118 }
00119 return true;
00120 }
00121 }
00122
00123 bool CMMeasurement::hasChangedSince(const CMMeasurement& m) {
00124 return hasChangedSince(&m);
00125 }
00126
00127 bool CMMeasurement::hasChangedSince(const CMMeasurement* m) {
00128 if (len == 1) {
00129 if (value != CMMEASUREMENT_NOVALUE) {
00130 if (value != m->value)
00131 return true;
00132 }
00133 if (uncertainty != CMMEASUREMENT_NOVALUE) {
00134 if (uncertainty != m->uncertainty)
00135 return true;
00136 }
00137 return false;
00138 }
00139 else {
00140 if (len != m->len)
00141 return true;
00142 for (unsigned int n=0; n<len; n++) {
00143 if (values[n] != m->values[n])
00144 return true;
00145 if (uncertainties[n] != m->uncertainties[n])
00146 return true;
00147 }
00148 return false;
00149 }
00150 }
00151
00152 bool CMMeasurement::fromXML(XMLNode* node) {
00153 if (node == NULL)
00154 return false;
00155 reset();
00156 name = node->getAttribute("name");
00157
00158 if (node->hasAttribute("value")) {
00159 value = node->getAttribute("value").toDouble();
00160 if (node->hasAttribute("uncertainty"))
00161 uncertainty = node->getAttribute("uncertainty").toDouble();
00162 len = 1;
00163 }
00164 else {
00165 len = node->getAttribute("size").toInt();
00166 values = new double[len];
00167 uncertainties = new double[len];
00168 XMLNode* xmlNode;
00169 ObjectCollection* col = node->getChildTags();
00170 if (col != NULL) {
00171 for (int n=0; n<col->getCount(); n++) {
00172 if ( (xmlNode = (XMLNode*) col->get(n)) != NULL ) {
00173 values[n] = xmlNode->getAttribute("value").toDouble();
00174 uncertainties[n] = xmlNode->getAttribute("uncertainty").toDouble();
00175 }
00176 if (n == (len-1)) break;
00177 }
00178 }
00179 delete(col);
00180 return true;
00181 }
00182
00183 return true;
00184 }
00185
00186 JString CMMeasurement::toXML() {
00187 if (len <= 1) {
00188 if (value == CMMEASUREMENT_NOVALUE) {
00189 return "";
00190 }
00191 else {
00192 if (uncertainty == CMMEASUREMENT_NOVALUE)
00193 return JString::format("<cmmeasurement name=\"%s\" value=\"%f\" />", (char*) name.xmlStringEncode(), value);
00194 else
00195 return JString::format("<cmmeasurement name=\"%s\" value=\"%f\" uncertainty=\"%f\" />", (char*) name.xmlStringEncode(), value, uncertainty);
00196 }
00197 }
00198 else {
00199 JString xml;
00200 for (unsigned int n=0; n<len; n++)
00201 xml += JString::format("<entry value=\"%f\" uncertainty=\"%f\">\n", values[n], uncertainties[n]);
00202 return JString::format("<cmmeasurement name=\"%s\" size=\"%d\">\n%s</cmmeasurement>", (char*) name.xmlStringEncode(), len, (char*) xml.indentXML());
00203 }
00204 }
00205
00206 bool CMMeasurement::fromXMLParam(const JString& name, XMLNode* node) {
00207 JString uncertaintyName = JString::format("%s_uncertainty", (char*) name);
00208 if (!node->hasAttribute(name))
00209 return true;
00210 value = node->getAttribute(name).toDouble();
00211 if (node->hasAttribute(uncertaintyName))
00212 uncertainty = node->getAttribute(uncertaintyName).toDouble();
00213 return true;
00214 }
00215
00216 JString CMMeasurement::toXMLParam(const JString& name) {
00217 if (value == CMMEASUREMENT_NOVALUE) {
00218 return "";
00219 }
00220 else {
00221 if (uncertainty == CMMEASUREMENT_NOVALUE)
00222 return JString::format("%s=\"%f\"", (char*) name.xmlStringEncode(), value);
00223 else
00224 return JString::format("%s=\"%f\" %s_uncertainty=\"%f\"", (char*) name.xmlStringEncode(), value, (char*) name.xmlStringEncode(), uncertainty);
00225 }
00226 }
00227
00228 JString CMMeasurement::print() const {
00229 return JString::format("%s=%f (+-%f)", (char*) name, value, uncertainty);
00230 }
00231
00232 JString CMMeasurement::toHTML() {
00233 return print().toHTML();
00234 }
00235
00236
00237
00238 double CMMeasurement::operator[](int n) const {
00239 return getValue(n);
00240 }
00241
00242 double CMMeasurement::getValue(int n) const {
00243 if ((len == 1) && (n == 1)) {
00244 return value;
00245 }
00246 else {
00247 if ((values == NULL) || (n >= (int)len))
00248 return 0;
00249 return values[n];
00250 }
00251 }
00252
00253 double CMMeasurement::getUncertainty(int n) const {
00254 if ((len == 1) && (n == 1)) {
00255 return uncertainty;
00256 }
00257 else {
00258 if ((uncertainties == NULL) || (n >= (int)len))
00259 return 0;
00260 return uncertainties[n];
00261 }
00262 }
00263
00264 bool CMMeasurement::setValue(int n, double value) {
00265 if ((len == 1) && (n == 1)) {
00266 this->value = value;
00267 return true;
00268 }
00269 else {
00270 if ((values == NULL) || (n >= (int)len))
00271 return false;
00272 values[n] = value;
00273 return true;
00274 }
00275 }
00276
00277 bool CMMeasurement::setUncertainty(int n, double value) {
00278 if ((len == 1) && (n == 1)) {
00279 this->uncertainty = value;
00280 return true;
00281 }
00282 else {
00283 if ((uncertainties == NULL) || (n >= (int)len))
00284 return 0;
00285 uncertainties[n] = value;
00286 return true;
00287 }
00288 }
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 CMPoint::CMPoint() {
00307 x = y = z = w = CMMEASUREMENT_NOVALUE;
00308 xUncertainty = yUncertainty = zUncertainty = wUncertainty = CMMEASUREMENT_NOVALUE;
00309 }
00310
00311 CMPoint::~CMPoint() {
00312 x = y = z = w = CMMEASUREMENT_NOVALUE;
00313 xUncertainty = yUncertainty = zUncertainty = wUncertainty = CMMEASUREMENT_NOVALUE;
00314 }
00315
00316 CMPoint::CMPoint(const JString& xml) {
00317 x = y = z = w = CMMEASUREMENT_NOVALUE;
00318 xUncertainty = yUncertainty = zUncertainty = wUncertainty = CMMEASUREMENT_NOVALUE;
00319 Object::fromXML(xml);
00320 }
00321
00322 CMPoint::CMPoint(XMLNode* node) {
00323 x = y = z = w = CMMEASUREMENT_NOVALUE;
00324 xUncertainty = yUncertainty = zUncertainty = wUncertainty = CMMEASUREMENT_NOVALUE;
00325 fromXML(node);
00326 }
00327
00328 Object* CMPoint::clone() const {
00329 CMPoint* p = new CMPoint();
00330 *p = *this;
00331 return p;
00332 }
00333
00334 bool CMPoint::reset() {
00335 x = y = z = w = CMMEASUREMENT_NOVALUE;
00336 xUncertainty = yUncertainty = zUncertainty = wUncertainty = CMMEASUREMENT_NOVALUE;
00337 return true;
00338 }
00339
00340 bool CMPoint::equals(const Object* o) const {
00341 if (o == NULL) return false;
00342 if (!this->isSameClass(o))
00343 return false;
00344 return this->equals(*(CMPoint*)o);
00345 }
00346
00347 bool CMPoint::equals(const CMPoint& p) const {
00348 return equals(&p);
00349 }
00350
00351 bool CMPoint::equals(const CMPoint* p) const {
00352 if ( (x != p->x) || (y != p->y) || (z != p->z) || (w != p->w) )
00353 return false;
00354 if ( (xUncertainty != p->xUncertainty) || (yUncertainty != p->yUncertainty) ||
00355 (zUncertainty != p->zUncertainty) || (wUncertainty != p->wUncertainty) )
00356 return false;
00357 return true;
00358 }
00359
00360 bool CMPoint::hasChangedSince(const CMPoint& p) {
00361 return hasChangedSince(&p);
00362 }
00363
00364 bool CMPoint::hasChangedSince(const CMPoint* p) {
00365 if (x != CMMEASUREMENT_NOVALUE) {
00366 if (x != p->x)
00367 return true;
00368 }
00369 if (y != CMMEASUREMENT_NOVALUE) {
00370 if (y != p->y)
00371 return true;
00372 }
00373 if (z != CMMEASUREMENT_NOVALUE) {
00374 if (z != p->z)
00375 return true;
00376 }
00377 if (w != CMMEASUREMENT_NOVALUE) {
00378 if (w != p->w)
00379 return true;
00380 }
00381 if (xUncertainty != CMMEASUREMENT_NOVALUE) {
00382 if (xUncertainty != p->xUncertainty)
00383 return true;
00384 }
00385 if (yUncertainty != CMMEASUREMENT_NOVALUE) {
00386 if (yUncertainty != p->yUncertainty)
00387 return true;
00388 }
00389 if (zUncertainty != CMMEASUREMENT_NOVALUE) {
00390 if (zUncertainty != p->zUncertainty)
00391 return true;
00392 }
00393 if (wUncertainty != CMMEASUREMENT_NOVALUE) {
00394 if (wUncertainty != p->wUncertainty)
00395 return true;
00396 }
00397 if (!units.equals(p->units))
00398 return true;
00399 if (!coordinateSystem.equals(p->coordinateSystem))
00400 return true;
00401 return false;
00402 }
00403
00404 bool CMPoint::fromXML(XMLNode* node) {
00405 if (node == NULL)
00406 return false;
00407 reset();
00408 if (node->hasAttribute("units"))
00409 units = node->getAttribute("units");
00410 if (node->hasAttribute("coord"))
00411 coordinateSystem = node->getAttribute("coord");
00412
00413 if (node->hasAttribute("x"))
00414 x = node->getAttribute("x").toDouble();
00415 if (node->hasAttribute("y"))
00416 y = node->getAttribute("y").toDouble();
00417 if (node->hasAttribute("z"))
00418 z = node->getAttribute("z").toDouble();
00419 if (node->hasAttribute("w"))
00420 w = node->getAttribute("w").toDouble();
00421
00422 if (node->hasAttribute("xuc"))
00423 xUncertainty = node->getAttribute("xuc").toDouble();
00424 if (node->hasAttribute("yuc"))
00425 yUncertainty = node->getAttribute("yuc").toDouble();
00426 if (node->hasAttribute("zuc"))
00427 zUncertainty = node->getAttribute("zuc").toDouble();
00428 if (node->hasAttribute("wuc"))
00429 wUncertainty = node->getAttribute("wuc").toDouble();
00430
00431 return true;
00432 }
00433
00434 JString CMPoint::toXML() {
00435
00436 JString xml;
00437
00438 if (x != CMMEASUREMENT_NOVALUE)
00439 xml += JString::format("x=\"%f\" ", x);
00440 if (y != CMMEASUREMENT_NOVALUE)
00441 xml += JString::format("y=\"%f\" ", y);
00442 if (z != CMMEASUREMENT_NOVALUE)
00443 xml += JString::format("z=\"%f\" ", z);
00444 if (w != CMMEASUREMENT_NOVALUE)
00445 xml += JString::format("w=\"%f\" ", w);
00446 if (xUncertainty != CMMEASUREMENT_NOVALUE)
00447 xml += JString::format("xuc=\"%f\" ", xUncertainty);
00448 if (yUncertainty != CMMEASUREMENT_NOVALUE)
00449 xml += JString::format("yuc=\"%f\" ", yUncertainty);
00450 if (zUncertainty != CMMEASUREMENT_NOVALUE)
00451 xml += JString::format("zuc=\"%f\" ", zUncertainty);
00452 if (wUncertainty != CMMEASUREMENT_NOVALUE)
00453 xml += JString::format("wuc=\"%f\" ", wUncertainty);
00454
00455 if (units.length() > 0)
00456 xml += JString::format("units=\"%s\" ", (char*) units);
00457 if (coordinateSystem.length() > 0)
00458 xml += JString::format("coord=\"%s\" ", (char*) coordinateSystem);
00459
00460 if (xml.length() > 0)
00461 return JString::format("<cmpoint %s />", (char*) xml);
00462 else
00463 return "";
00464 }
00465
00466 JString CMPoint::print() const {
00467 return "";
00468 }
00469
00470 JString CMPoint::toHTML() {
00471 return print().toHTML();
00472 }
00473
00474
00475
00476 double CMPoint::operator[](int n) const {
00477 return get(n);
00478 }
00479
00480 double CMPoint::get(int n) const {
00481 switch (n) {
00482 case 0:
00483 return x;
00484 case 1:
00485 return y;
00486 case 2:
00487 return z;
00488 case 3:
00489 return w;
00490 default:
00491 return 0;
00492 }
00493 return 0;
00494 }
00495
00496 double CMPoint::getUncertainty(int n) const {
00497 switch (n) {
00498 case 0:
00499 return xUncertainty;
00500 case 1:
00501 return yUncertainty;
00502 case 2:
00503 return zUncertainty;
00504 case 3:
00505 return wUncertainty;
00506 default:
00507 return 0;
00508 }
00509 return 0;
00510 }
00511
00512
00513 bool CMPoint::set(int n, double value) {
00514 switch (n) {
00515 case 0:
00516 x = value;
00517 case 1:
00518 y = value;
00519 case 2:
00520 z = value;
00521 case 3:
00522 w = value;
00523 default:
00524 return false;
00525 }
00526 return true;
00527 }
00528
00529 bool CMPoint::set(int n, double value, double uncertainty) {
00530 switch (n) {
00531 case 0:
00532 xUncertainty = value;
00533 case 1:
00534 yUncertainty = value;
00535 case 2:
00536 zUncertainty = value;
00537 case 3:
00538 wUncertainty = value;
00539 default:
00540 return false;
00541 }
00542 return true;
00543 }
00544
00545 bool CMPoint::addDifference(const CMPoint& p) {
00546 return addDifference(&p);
00547 }
00548
00549 bool CMPoint::addDifference(const CMPoint* p) {
00550 if (p->x != CMMEASUREMENT_NOVALUE) {
00551 x = p->x;
00552 }
00553 if (p->y != CMMEASUREMENT_NOVALUE) {
00554 y = p->y;
00555 }
00556 if (p->z != CMMEASUREMENT_NOVALUE) {
00557 z = p->z;
00558 }
00559 if (p->w != CMMEASUREMENT_NOVALUE) {
00560 w = p->w;
00561 }
00562 if (p->xUncertainty != CMMEASUREMENT_NOVALUE) {
00563 xUncertainty = p->xUncertainty;
00564 }
00565 if (p->yUncertainty != CMMEASUREMENT_NOVALUE) {
00566 yUncertainty = p->yUncertainty;
00567 }
00568 if (p->zUncertainty != CMMEASUREMENT_NOVALUE) {
00569 zUncertainty = p->zUncertainty;
00570 }
00571 if (p->wUncertainty != CMMEASUREMENT_NOVALUE) {
00572 wUncertainty = p->wUncertainty;
00573 }
00574 if (p->units.length() > 0)
00575 units = p->units;
00576 if (p->coordinateSystem.length() > 0)
00577 coordinateSystem = p->coordinateSystem;
00578 return true;
00579 }
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598 CMObject::CMObject() {
00599 id = this->createUniqueID();
00600 parent = NULL;
00601 }
00602
00603 CMObject::CMObject(const JString& xml) {
00604 id = createUniqueID();
00605 parent = NULL;
00606 fromXML(xml);
00607 }
00608
00609 CMObject::CMObject(XMLNode* node) {
00610 id = createUniqueID();
00611 parent = NULL;
00612 fromXML(node);
00613 }
00614
00615 CMObject::~CMObject() {
00616 }
00617
00618 Object* CMObject::clone() const {
00619 CMObject* obj = new CMObject();
00620
00621 obj->id = id;
00622 obj->type = type;
00623 obj->parentID = parentID;
00624 obj->properties.copyAll(properties);
00625 obj->propertyTypes.copyAll(propertyTypes);
00626
00627 obj->pos = pos;
00628 obj->oriX = oriX;
00629 obj->oriY = oriY;
00630 obj->oriZ = oriZ;
00631 obj->oriW = oriW;
00632 obj->velX = velX;
00633 obj->velY = velY;
00634 obj->velZ = velZ;
00635 obj->boxW = boxW;
00636 obj->boxH = boxH;
00637 obj->boxD = boxD;
00638 obj->createdTime = createdTime;
00639
00640 obj->parent = NULL;
00641 return obj;
00642 }
00643
00644 bool CMObject::equals(const Object* o) const {
00645 if (o == NULL) return false;
00646 if (!this->isSameClass(o))
00647 return false;
00648 return this->equals(*(CMObject*)o);
00649 }
00650
00651 bool CMObject::equals(const CMObject& o) const {
00652 return equals(&o);
00653 }
00654
00655 bool CMObject::equals(const CMObject* obj) const {
00656
00657 if (!obj->id.equals(id))
00658 return false;
00659 if (!obj->createdTime.equals(&createdTime))
00660 return false;
00661 if (!obj->type.equals(type))
00662 return false;
00663 if (!obj->parentID.equals(parentID))
00664 return false;
00665
00666 if (!obj->pos.equals(pos))
00667 return false;
00668
00669 if (!obj->oriX.equals(oriX))
00670 return false;
00671 if (!obj->oriY.equals(oriY))
00672 return false;
00673 if (!obj->oriZ.equals(oriZ))
00674 return false;
00675 if (!obj->oriW.equals(oriW))
00676 return false;
00677
00678 if (!obj->velX.equals(velX))
00679 return false;
00680 if (!obj->velY.equals(velY))
00681 return false;
00682 if (!obj->velZ.equals(velZ))
00683 return false;
00684
00685 if (!obj->boxW.equals(boxW))
00686 return false;
00687 if (!obj->boxH.equals(boxH))
00688 return false;
00689 if (!obj->boxD.equals(boxD))
00690 return false;
00691
00692 if (!obj->propertyTypes.equals(&propertyTypes))
00693 return false;
00694 if (!obj->properties.equals(&properties))
00695 return false;
00696
00697 return true;
00698 }
00699
00700
00701 bool CMObject::fromXML(const JString& xml) {
00702
00703 if (xml.length() == 0)
00704 return false;
00705
00706 XMLParser* xmlParser = new XMLParser();
00707 xmlParser->parse(xml);
00708 bool ret = fromXML(xmlParser->getRootNode());
00709 delete(xmlParser);
00710 return ret;
00711 }
00712
00713 bool CMObject::fromXML(XMLNode* node) {
00714
00715 if ( (node == NULL) || (!node->getTag().equalsIgnoreCase("cmobject")) )
00716 return false;
00717
00718 id = node->getAttribute("id");
00719 type = node->getAttribute("type");
00720 parentID = node->getAttribute("parent");
00721
00722 pos.fromXML(node->getChildNode("cmpoint"));
00723
00724 oriX.fromXMLParam("oriX", node);
00725 oriY.fromXMLParam("oriY", node);
00726 oriZ.fromXMLParam("oriZ", node);
00727 oriW.fromXMLParam("oriW", node);
00728
00729 velX.fromXMLParam("velX", node);
00730 velY.fromXMLParam("velY", node);
00731 velZ.fromXMLParam("velZ", node);
00732
00733 boxW.fromXMLParam("boxW", node);
00734 boxH.fromXMLParam("boxH", node);
00735 boxD.fromXMLParam("boxD", node);
00736
00737 XMLNode* xmlNode = node->getChildNode("createdtime");
00738 if (xmlNode != NULL)
00739 createdTime.fromXML(xmlNode);
00740
00741 xmlNode = node->getChildNode("dictionary");
00742 if (xmlNode != NULL)
00743 propertyTypes.fromXML(xmlNode);
00744
00745 xmlNode = node->getChildNode("objectdictionary");
00746 if (xmlNode != NULL)
00747 properties.fromXML(xmlNode);
00748
00749 return true;
00750 }
00751
00752 JString CMObject::toXML() {
00753 JString spatial = JString::format("%s %s %s %s ",
00754 (char*) oriX.toXMLParam("oriX"), (char*) oriY.toXMLParam("oriY"), (char*) oriZ.toXMLParam("oriZ"), (char*) oriW.toXMLParam("oriW"));
00755
00756 spatial += JString::format("%s %s %s ",
00757 (char*) velX.toXMLParam("velX"), (char*) velY.toXMLParam("velY"), (char*) velZ.toXMLParam("velZ"));
00758
00759 spatial += JString::format("%s %s %s ",
00760 (char*) boxW.toXMLParam("boxW"), (char*) boxH.toXMLParam("boxH"), (char*) boxW.toXMLParam("boxW"));
00761
00762 JString xml = createdTime.toXML("createdtime") + "\n";
00763 if (propertyTypes.getCount() > 0)
00764 xml += propertyTypes.toXML() + "\n";
00765 if (properties.getCount() > 0)
00766 xml += properties.toXML() + "\n";
00767 xml += pos.toXML() + "\n";
00768
00769 if ( (xml.trim().length() == 0) && (spatial.trim().length() == 0) )
00770 return "";
00771
00772 return JString::format("<cmobject id=\"%s\" type=\"%s\" parent=\"%s\" %s>\n%s</cmobject>",
00773 (char*) id, (char*) type, (char*) parentID, (char*) spatial, (char*) xml.indentXML());
00774 }
00775
00776 JString CMObject::toHTML() {
00777 return print();
00778 }
00779
00780 JString CMObject::print() {
00781 return JString::format("CMObject %s [%s]\n", (char*) id, (char*) type);
00782 }
00783
00784 bool CMObject::hasProperty(const JString& name) {
00785 return properties.contains(name);
00786 }
00787
00788 JString CMObject::getPropertyType(const JString& name) {
00789 return propertyTypes.get(name);
00790 }
00791
00792 bool CMObject::removeProperty(const JString& name) {
00793 propertyTypes.remove(name);
00794 return properties.remove(name);
00795 }
00796
00797
00798 JString CMObject::getPropertyString(const JString& name) {
00799 JString* obj = (JString*) properties.get(name);
00800 if (obj != NULL)
00801 return *obj;
00802 return "";
00803 }
00804
00805 int CMObject::getPropertyInteger(const JString& name) {
00806 JString* obj = (JString*) properties.get(name);
00807 if (obj != NULL)
00808 return obj->toInt();
00809 return 0;
00810 }
00811
00812 double CMObject::getPropertyDouble(const JString& name) {
00813 JString* obj = (JString*) properties.get(name);
00814 if (obj != NULL)
00815 return obj->toDouble();
00816 return 0;
00817 }
00818
00819 Object* CMObject::getPropertyObject(const JString& name) {
00820 return properties.get(name);
00821 }
00822
00823
00824 bool CMObject::setPropertyString(const JString& name, const JString& value) {
00825 propertyTypes.put(name, "String");
00826 return properties.put(name, value.clone());
00827 }
00828
00829 bool CMObject::setPropertyInteger(const JString& name, int value) {
00830 propertyTypes.put(name, "Integer");
00831 return properties.put(name, new JString(value));
00832 }
00833
00834 bool CMObject::setPropertyDouble(const JString& name, double value) {
00835 propertyTypes.put(name, "Double");
00836 return properties.put(name, new JString(value));
00837 }
00838
00839 bool CMObject::setPropertyObject(const JString& name, Object* value, const JString& type) {
00840 propertyTypes.put(name, type);
00841 return properties.put(name, value);
00842 }
00843
00844
00845 CMObject* CMObject::operator-(CMObject* otherObject) {
00846 return getDifferenceSince(otherObject);
00847 }
00848
00849 const CMObject& CMObject::operator+=(CMObject* otherObject) {
00850 addDifference(otherObject);
00851 return *this;
00852 }
00853
00854 CMObject* CMObject::getDifferenceSince(CMObject* otherObject) {
00855 CMObject* obj = getDifferenceSinceIfAny(otherObject);
00856 if (obj == NULL) {
00857 obj = new CMObject();
00858 obj->id = id;
00859 obj->createdTime = createdTime;
00860 }
00861 return obj;
00862 }
00863
00864 CMObject* CMObject::getDifferenceSinceIfAny(CMObject* otherObject) {
00865 if (otherObject == NULL)
00866 return (CMObject*) this->clone();
00867
00868 bool didAnythingChange = false;
00869 CMObject* obj = new CMObject();
00870
00871 if ((type.length() > 0) && (!type.equals(otherObject->type))) {
00872 didAnythingChange = true;
00873 obj->type = type;
00874 }
00875 if ((parentID.length() > 0) && (!parentID.equals(otherObject->parentID))) {
00876 didAnythingChange = true;
00877 obj->parentID = parentID;
00878 }
00879
00880 if (pos.hasChangedSince(otherObject->pos)) {
00881 didAnythingChange = true;
00882 obj->pos.addDifference(pos);
00883 }
00884
00885 if (oriX.hasChangedSince(otherObject->oriX)) {
00886 didAnythingChange = true;
00887 obj->oriX = oriX;
00888 }
00889 if (oriY.hasChangedSince(otherObject->oriY)) {
00890 didAnythingChange = true;
00891 obj->oriY = oriY;
00892 }
00893 if (oriZ.hasChangedSince(otherObject->oriZ)) {
00894 didAnythingChange = true;
00895 obj->oriZ = oriZ;
00896 }
00897 if (oriW.hasChangedSince(otherObject->oriW)) {
00898 didAnythingChange = true;
00899 obj->oriW = oriW;
00900 }
00901
00902 if (velX.hasChangedSince(otherObject->velX)) {
00903 didAnythingChange = true;
00904 obj->velX = velX;
00905 }
00906 if (velY.hasChangedSince(otherObject->velY)) {
00907 didAnythingChange = true;
00908 obj->velY = velY;
00909 }
00910 if (velZ.hasChangedSince(otherObject->velZ)) {
00911 didAnythingChange = true;
00912 obj->velZ = velZ;
00913 }
00914
00915 if (boxW.hasChangedSince(otherObject->boxW)) {
00916 didAnythingChange = true;
00917 obj->boxW = boxW;
00918 }
00919 if (boxH.hasChangedSince(otherObject->boxH)) {
00920 didAnythingChange = true;
00921 obj->boxH = boxH;
00922 }
00923 if (boxD.hasChangedSince(otherObject->boxD)) {
00924 didAnythingChange = true;
00925 obj->boxD = boxD;
00926 }
00927
00928 int n;
00929 JString value, key;
00930 for (n=0; n<propertyTypes.getCount(); n++) {
00931 key = propertyTypes.getKey(n);
00932 value = propertyTypes.get(n);
00933 if ( (key.length() > 0) && (!otherObject->propertyTypes.get(key).equals(value)) ) {
00934 didAnythingChange = true;
00935 obj->propertyTypes.put(key, value);
00936 }
00937 }
00938
00939 Object* object, *object2;
00940 for (n=0; n<properties.getCount(); n++) {
00941 key = properties.getKey(n);
00942 object = properties.get(n);
00943 if ( (key.length() > 0) && (object != NULL) ) {
00944 object2 = otherObject->properties.get(key);
00945 if ( (object2 == NULL) || (!object->equals(object2)) ) {
00946 didAnythingChange = true;
00947 properties.removeNoDelete(key);
00948 obj->properties.put(key, object);
00949 }
00950 }
00951 }
00952
00953 if (didAnythingChange) {
00954 obj->id = id;
00955 obj->createdTime = createdTime;
00956 }
00957 else {
00958 delete(obj);
00959 obj = NULL;
00960 }
00961 return obj;
00962 }
00963
00964 bool CMObject::addDifference(CMObject* otherObject) {
00965 if (otherObject == NULL)
00966 return false;
00967
00968 id = otherObject->id;
00969 createdTime = otherObject->createdTime;
00970
00971 if (otherObject->type.length() > 0)
00972 type = otherObject->type;
00973 if (otherObject->parentID.length() > 0)
00974 parentID = otherObject->parentID;
00975
00976 if (otherObject->pos.hasChangedSince(pos))
00977 pos.addDifference(otherObject->pos);
00978
00979 if (otherObject->oriX.hasChangedSince(oriX))
00980 oriX = otherObject->oriX;
00981 if (otherObject->oriY.hasChangedSince(oriY))
00982 oriY = otherObject->oriY;
00983 if (otherObject->oriZ.hasChangedSince(oriZ))
00984 oriZ = otherObject->oriZ;
00985 if (otherObject->oriW.hasChangedSince(oriW))
00986 oriW = otherObject->oriW;
00987
00988 if (otherObject->velX.hasChangedSince(velX))
00989 velX = otherObject->velX;
00990 if (otherObject->velY.hasChangedSince(velY))
00991 velY = otherObject->velY;
00992 if (otherObject->velZ.hasChangedSince(velZ))
00993 velZ = otherObject->velZ;
00994
00995 if (otherObject->boxW.hasChangedSince(boxW))
00996 boxW = otherObject->boxW;
00997 if (otherObject->boxH.hasChangedSince(boxH))
00998 boxH = otherObject->boxH;
00999 if (otherObject->boxD.hasChangedSince(boxD))
01000 boxD = otherObject->boxD;
01001
01002 propertyTypes.addAll(otherObject->propertyTypes);
01003 properties.takeAll(otherObject->properties);
01004 return true;
01005 }
01006
01007 JString CMObject::get(const JString& prop) {
01008 if (prop.equalsIgnoreCase("posx")) {
01009 return JString(pos.x);
01010 }
01011 else if (prop.equalsIgnoreCase("posy")) {
01012 return JString(pos.y);
01013 }
01014 else if (prop.equalsIgnoreCase("posz")) {
01015 return JString(pos.z);
01016 }
01017 else if (prop.equalsIgnoreCase("pos")) {
01018 if ( (pos.x != CMMEASUREMENT_NOVALUE) && (pos.y != CMMEASUREMENT_NOVALUE) && (pos.z != CMMEASUREMENT_NOVALUE) ) {
01019 double val = (pos.x*pos.x) + (pos.y*pos.y) + (pos.z*pos.z);
01020 if (val > 0)
01021 val = sqrt(val);
01022 return JString(val);
01023 }
01024 }
01025 else if (prop.equalsIgnoreCase("velx")) {
01026 return JString(velX.value);
01027 }
01028 else if (prop.equalsIgnoreCase("vely")) {
01029 return JString(velY.value);
01030 }
01031 else if (prop.equalsIgnoreCase("velz")) {
01032 return JString(velZ.value);
01033 }
01034 else if (prop.equalsIgnoreCase("vel")) {
01035 if ( (velX.value != CMMEASUREMENT_NOVALUE) && (velY.value != CMMEASUREMENT_NOVALUE) && (velZ.value != CMMEASUREMENT_NOVALUE) ) {
01036 double val = (velX.value*velX.value) + (velY.value*velY.value) + (velZ.value*velZ.value);
01037 if (val > 0)
01038 val = sqrt(val);
01039 return JString(val);
01040 }
01041 }
01042 else if (prop.equalsIgnoreCase("orix")) {
01043 return JString(oriX.value);
01044 }
01045 else if (prop.equalsIgnoreCase("oriy")) {
01046 return JString(oriY.value);
01047 }
01048 else if (prop.equalsIgnoreCase("oriz")) {
01049 return JString(oriZ.value);
01050 }
01051 else if (prop.equalsIgnoreCase("oriw")) {
01052 return JString(oriW.value);
01053 }
01054 else if (prop.equalsIgnoreCase("id")) {
01055 return id;
01056 }
01057 else if (prop.equalsIgnoreCase("type")) {
01058 return type;
01059 }
01060
01061 Object* obj = properties.get(prop);
01062 if (obj != NULL)
01063 return obj->print();
01064
01065 return "";
01066 }
01067
01068 bool CMObject::unitTest() {
01069
01070 CMObject* obj = new CMObject();
01071 JString xml = obj->toXML();
01072
01073 obj->pos.x = 10;
01074 xml = obj->toXML();
01075
01076 obj->pos.y = 11;
01077 obj->pos.z = 12;
01078 obj->pos.w = 13;
01079
01080 xml = obj->toXML();
01081 CMObject* objC = new CMObject(xml);
01082
01083 if (!objC->equals(obj)) {
01084 this->addUnitTestLog("Object copy via XML faulty...\n");
01085 return false;
01086 }
01087 delete(objC);
01088
01089 objC = (CMObject*) obj->clone();
01090 if (!objC->equals(obj)) {
01091 this->addUnitTestLog("Object cloning faulty...\n");
01092 return false;
01093 }
01094 delete(objC);
01095
01096 CMObject* obj2 = new CMObject();
01097 obj2->pos.x = 111;
01098
01099 CMObject* obj3 = obj2->getDifferenceSince(obj);
01100 xml = obj3->toXML();
01101
01102 obj->addDifference(obj3);
01103 delete(obj3);
01104
01105 obj3 = obj2->getDifferenceSince(obj);
01106 xml = obj3->toXML();
01107
01108 delete(obj);
01109 delete(obj2);
01110 delete(obj3);
01111
01112 return true;
01113 }
01114
01115
01116
01117
01118
01119
01120
01121 long CMObject::getBinarySize(int chunk) {
01122 return properties.getBinarySize(chunk);
01123 }
01124
01125
01126 int CMObject::getBinaryChunkCount() {
01127 return properties.getBinaryChunkCount();
01128 }
01129
01130
01131 long CMObject::toBinaryBuffer(int chunk, char* buffer, int maxlen) {
01132 return properties.toBinaryBuffer(chunk, buffer, maxlen);
01133 }
01134
01135
01136 bool CMObject::fromBinaryBuffer(int chunk, char* buffer, long len) {
01137 return properties.fromBinaryBuffer(chunk, buffer, len);
01138 }
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149 CMPose::CMPose() {
01150 id = createUniqueID();
01151 parent = NULL;
01152 }
01153
01154 CMPose::CMPose(const JString& xml) {
01155 id = createUniqueID();
01156 parent = NULL;
01157 fromXML(xml);
01158 }
01159
01160 CMPose::CMPose(XMLNode* node) {
01161 id = createUniqueID();
01162 parent = NULL;
01163 fromXML(node);
01164 }
01165
01166 CMPose::~CMPose() {
01167 }
01168
01169 Object* CMPose::clone() const {
01170 CMPose* obj = new CMPose();
01171 *obj = *this;
01172 obj->parent = NULL;
01173 return obj;
01174 }
01175
01176 bool CMPose::fromXML(const JString& xml) {
01177
01178 if (xml.length() == 0)
01179 return false;
01180
01181 XMLParser* xmlParser = new XMLParser();
01182 xmlParser->parse(xml);
01183 bool ret = fromXML(xmlParser->getRootNode());
01184 delete(xmlParser);
01185 return ret;
01186 }
01187
01188 bool CMPose::fromXML(XMLNode* node) {
01189
01190 if ( (node == NULL) || (!node->getTag().equalsIgnoreCase("cmpose")) )
01191 return false;
01192
01193 id = node->findAttr("id");
01194 type = node->findAttr("type");
01195 return true;
01196 }
01197
01198 JString CMPose::toXML() {
01199 return JString::format("<cmpose id=\"%s\" type=\"%s\" />", (char*) id, (char*) type);
01200 }
01201
01202 JString CMPose::toHTML() {
01203 return print();
01204 }
01205
01206 JString CMPose::print() {
01207 return JString::format("CMPose %s [%s]\n", (char*) id, (char*) type);
01208 }
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218 CMManipulator::CMManipulator() {
01219 id = createUniqueID();
01220 parent = NULL;
01221 }
01222
01223 CMManipulator::CMManipulator(const JString& xml) {
01224 id = createUniqueID();
01225 parent = NULL;
01226 fromXML(xml);
01227 }
01228
01229 CMManipulator::CMManipulator(XMLNode* node) {
01230 id = createUniqueID();
01231 parent = NULL;
01232 fromXML(node);
01233 }
01234
01235 CMManipulator::~CMManipulator() {
01236 }
01237
01238 Object* CMManipulator::clone() const {
01239 CMManipulator* obj = new CMManipulator();
01240 *obj = *this;
01241 obj->parent = NULL;
01242 return obj;
01243 }
01244
01245 bool CMManipulator::fromXML(const JString& xml) {
01246
01247 if (xml.length() == 0)
01248 return false;
01249
01250 XMLParser* xmlParser = new XMLParser();
01251 xmlParser->parse(xml);
01252 bool ret = fromXML(xmlParser->getRootNode());
01253 delete(xmlParser);
01254 return ret;
01255 }
01256
01257 bool CMManipulator::fromXML(XMLNode* node) {
01258
01259 if ( (node == NULL) || (!node->getTag().equalsIgnoreCase("cmmanipulator")) )
01260 return false;
01261
01262 id = node->findAttr("id");
01263 type = node->findAttr("type");
01264 return true;
01265 }
01266
01267 JString CMManipulator::toXML() {
01268 return JString::format("<cmmanipulator id=\"%s\" type=\"%s\" />", (char*) id, (char*) type);
01269 }
01270
01271 JString CMManipulator::toHTML() {
01272 return print();
01273 }
01274
01275 JString CMManipulator::print() {
01276 return JString::format("CMManipulator %s [%s]\n", (char*) id, (char*) type);
01277 }
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287 CMMap::CMMap() {
01288 id = createUniqueID();
01289 parent = NULL;
01290 }
01291
01292 CMMap::CMMap(const JString& xml) {
01293 id = createUniqueID();
01294 parent = NULL;
01295 fromXML(xml);
01296 }
01297
01298 CMMap::CMMap(XMLNode* node) {
01299 id = createUniqueID();
01300 parent = NULL;
01301 fromXML(node);
01302 }
01303
01304 CMMap::~CMMap() {
01305 }
01306
01307 Object* CMMap::clone() const {
01308 CMMap* obj = new CMMap();
01309 *obj = *this;
01310 obj->parent = NULL;
01311 return obj;
01312 }
01313
01314 bool CMMap::fromXML(const JString& xml) {
01315
01316 if (xml.length() == 0)
01317 return false;
01318
01319 XMLParser* xmlParser = new XMLParser();
01320 xmlParser->parse(xml);
01321 bool ret = fromXML(xmlParser->getRootNode());
01322 delete(xmlParser);
01323 return ret;
01324 }
01325
01326 bool CMMap::fromXML(XMLNode* node) {
01327
01328 if ( (node == NULL) || (!node->getTag().equalsIgnoreCase("cmmap")) )
01329 return false;
01330
01331 id = node->findAttr("id");
01332 type = node->findAttr("type");
01333 return true;
01334 }
01335
01336 JString CMMap::toXML() {
01337 return JString::format("<cmmap id=\"%s\" type=\"%s\" />", (char*) id, (char*) type);
01338 }
01339
01340 JString CMMap::toHTML() {
01341 return print();
01342 }
01343
01344 JString CMMap::print() {
01345 return JString::format("CMMap %s [%s]\n", (char*) id, (char*) type);
01346 }
01347
01348
01349 CMObject* CMObjectFromObject(Object* obj) {
01350 if (obj == NULL)
01351 return NULL;
01352 CMObject* cmObject = NULL;
01353 if (obj->getClass().equalsIgnoreCase("UninstantiatedXMLObject") ) {
01354 cmObject = new CMObject();
01355 if (cmObject->fromXML(((UninstantiatedXMLObject*) obj)->getXML()))
01356 return cmObject;
01357 delete(cmObject);
01358 return NULL;
01359 }
01360 else
01361 return (CMObject*) obj->clone();
01362 }
01363
01364 CMMap* CMMapFromObject(Object* obj) {
01365 if (obj == NULL)
01366 return NULL;
01367 CMMap* cmMap = NULL;
01368 if (obj->getClass().equalsIgnoreCase("UninstantiatedXMLObject") ) {
01369 cmMap = new CMMap();
01370 if (cmMap->fromXML(((UninstantiatedXMLObject*) obj)->getXML()))
01371 return cmMap;
01372 delete(cmMap);
01373 return NULL;
01374 }
01375 else
01376 return (CMMap*) obj->clone();
01377 }
01378
01379
01380 };
01381
01382