00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "MathClasses.h"
00026
00027
00028 namespace cmlabs {
00029
00030
00031
00032
00033
00034 Size::Size() { w=0; h=0; d=0; }
00035 Size::Size(JString xml) {
00036 if (!Object::fromXML(xml)) {
00037 w=0; h=0; d=0;
00038 }
00039 }
00040 Size::Size(XMLNode* node) {
00041 if (!fromXML(node)) {
00042 w=0; h=0; d=0;
00043 }
00044 }
00045 Size::Size(double width, double height, double depth) { w=width; h=height; d=depth; }
00046 Size::~Size() {}
00047
00048 Object* Size::clone() const {
00049 Size* s = new Size(w, h, d);
00050 return s;
00051 };
00052
00053
00054 bool Size::equals(const Size &s) const {
00055 return (
00056 (this->d == s.d) &&
00057 (this->w == s.w) &&
00058 (this->h == s.h) );
00059 }
00060
00061 bool Size::operator==(const Size &s) const {
00062 return equals(s);
00063 }
00064
00065
00066 double Size::getHeight() const { return h; }
00067 double Size::getWidth() const { return w; }
00068 double Size::getDepth() const { return d; }
00069
00070 bool Size::setHeight(double height) { h = height; return true; }
00071 bool Size::setWidth(double width) { w = width; return true; }
00072 bool Size::setDepth(double depth) { d = depth; return true; }
00073
00074 bool Size::isNonZero() const {
00075 return ( (w != 0) || (h != 0) || (d != 0) );
00076 }
00077
00078 double Size::getArea() const {
00079 return ( getWidth() * getHeight() );
00080 }
00081
00082 double Size::getDiagonalLenth() const {
00083 return sqrt( pow(getWidth(), 2) + pow(getHeight(), 2) );
00084 }
00085
00086 JString Size::print() {
00087 return JString::format("(w: %.3f, h: %.3f, d: %.3f)", w, h, d);
00088 }
00089
00090 JString Size::toXML() {
00091 return JString::format("<size width=\"%f\" height=\"%f\" depth=\"%f\" />", w, h, d);
00092 }
00093 bool Size::fromXML(XMLNode* node) {
00094 if (node == NULL) {
00095 return false;
00096 }
00097 if (!node->getTag().equalsIgnoreCase("size"))
00098 return false;
00099 if ( (!node->hasAttribute("width")) || (!node->hasAttribute("height")))
00100 return false;
00101
00102 w = node->getAttribute("width").toDouble();
00103 h = node->getAttribute("height").toDouble();
00104 d = node->getAttribute("depth").toDouble();
00105 return true;
00106 }
00107
00108
00109
00110
00111
00112
00113 Point::Point() { x=0; y=0; z=0; }
00114 Point::Point(int px, int py, int pz, Size psize) { x=px; y=py; z=pz; size=psize; }
00115 Point::Point(JString xml) {
00116 if (!Object::fromXML(xml)) {
00117 x=0; y=0; z=0;
00118 }
00119 }
00120 Point::Point(XMLNode* node) {
00121 if (!fromXML(node)) {
00122 x=0; y=0; z=0;
00123 }
00124 }
00125 Point::~Point() {}
00126
00127 Object* Point::clone() const {
00128 Point* p = new Point(x, y, z, size);
00129 return p;
00130 };
00131
00132 Point::operator PointFloat() const {
00133 PointFloat pf;
00134 pf.setSize(this->size);
00135 pf.setX(this->x);
00136 pf.setY(this->y);
00137 pf.setZ(this->z);
00138 return pf;
00139 }
00140
00141 int Point::operator[](int n) const {
00142 switch(n) {
00143 case 0:
00144 return x;
00145 case 1:
00146 return y;
00147 case 2:
00148 return z;
00149 }
00150 return 0;
00151 }
00152
00153 bool Point::operator==(const Point &p) const {
00154 return (
00155 (this->x == p.x) &&
00156 (this->y == p.y) &&
00157 (this->z == p.z) &&
00158 (this->size == p.size) );
00159 }
00160
00161 bool Point::operator==(const PointFloat &p) const {
00162 return (
00163 (this->x == (int)p.x) &&
00164 (this->y == (int)p.y) &&
00165 (this->z == (int)p.z) &&
00166 (this->size == p.size) );
00167 }
00168
00169 Point Point::operator-(const Point &p) const {
00170 Point np = *this;
00171 np.x -= p.x;
00172 np.y -= p.y;
00173 np.z -= p.z;
00174 return np;
00175 }
00176 PointFloat Point::operator-(const PointFloat &p) const {
00177 PointFloat np = *this;
00178 np.x -= p.x;
00179 np.y -= p.y;
00180 np.z -= p.z;
00181 return np;
00182 }
00183 Point Point::operator+(const Point &p) const {
00184 Point np = *this;
00185 np.x += p.x;
00186 np.y += p.y;
00187 np.z += p.z;
00188 return np;
00189 }
00190 PointFloat Point::operator+(const PointFloat &p) const {
00191 PointFloat np = *this;
00192 np.x += p.x;
00193 np.y += p.y;
00194 np.z += p.z;
00195 return np;
00196 }
00197 Point Point::operator*(const Point &p) const {
00198 Point np = *this;
00199 np.x *= p.x;
00200 np.y *= p.y;
00201 np.z *= p.z;
00202 return np;
00203 }
00204 PointFloat Point::operator*(const PointFloat &p) const {
00205 PointFloat np = *this;
00206 np.x *= p.x;
00207 np.y *= p.y;
00208 np.z *= p.z;
00209 return np;
00210 }
00211 Point Point::operator-(double a) const {
00212 Point np = *this;
00213 np.x = (int)(np.x - a);
00214 np.y = (int)(np.y - a);
00215 np.z = (int)(np.z - a);
00216 return np;
00217 }
00218 Point Point::operator+(double a) const {
00219 Point np = *this;
00220 np.x = (int)(np.x + a);
00221 np.y = (int)(np.y + a);
00222 np.z = (int)(np.z + a);
00223 return np;
00224 }
00225 Point Point::operator*(double a) const {
00226 Point np = *this;
00227 np.x = (int)(np.x * a);
00228 np.y = (int)(np.y * a);
00229 np.z = (int)(np.z * a);
00230 return np;
00231 }
00232
00233
00234 int Point::getX() const { return x; }
00235 int Point::getY() const { return y; }
00236 int Point::getZ() const { return z; }
00237 Size Point::getSize() const { return size; }
00238
00239 bool Point::set(int xx, int yy, int zz) { x = xx; y = yy; z = zz; return true; }
00240 bool Point::setX(int n) { x = n; return true; }
00241 bool Point::setY(int n) { y = n; return true; }
00242 bool Point::setZ(int n) { z = n; return true; }
00243 bool Point::setSize(Size s) { size = s; return true; }
00244
00245 double Point::getDistanceTo(Point &p) const {
00246 return sqrt( pow((double)(getX()-p.getX()), 2) + pow((double)(getX()-p.getX()), 2) );
00247 }
00248 double Point::getDistanceTo(PointFloat &p) const {
00249 return sqrt( pow((double)(getX()-p.getX()), 2) + pow((double)(getX()-p.getX()), 2) );
00250 }
00251
00252 JString Point::print() {
00253 if (size.isNonZero())
00254 return JString::format("(%d,%d,%d x %s)", x, y, z, (char*) size.print());
00255 else
00256 return JString::format("(%d,%d,%d)", x, y, z);
00257 }
00258
00259 JString Point::toXML() {
00260 return JString::format("<point x=\"%d\" y=\"%d\" z=\"%d\" >\n%s</point>", x, y, z, (char*) size.toXML().indentXML());
00261 }
00262
00263 bool Point::fromXML(XMLNode* node) {
00264 if (node == NULL) {
00265 return false;
00266 }
00267 if (!node->getTag().equalsIgnoreCase("point"))
00268 return false;
00269 if ( (!node->hasAttribute("x")) || (!node->hasAttribute("y")))
00270 return false;
00271
00272 x = node->getAttribute("x").toInt();
00273 y = node->getAttribute("y").toInt();
00274 z = node->getAttribute("z").toInt();
00275
00276 XMLNode* xmlNode = node->getChildNode("size");
00277 if (xmlNode != NULL) {
00278 size.fromXML(xmlNode);
00279 }
00280
00281 return true;
00282 }
00283
00284
00285
00286
00287
00288
00289 PointFloat::PointFloat() { x=0; y=0; z=0; }
00290 PointFloat::PointFloat(double px, double py, double pz, Size psize) { x=px; y=py; z=pz; size=psize; }
00291 PointFloat::PointFloat(JString xml) {
00292 if (!Object::fromXML(xml)) {
00293 x=0; y=0; z=0;
00294 }
00295 }
00296 PointFloat::PointFloat(XMLNode* node) {
00297 if (!fromXML(node)) {
00298 x=0; y=0; z=0;
00299 }
00300 }
00301 PointFloat::~PointFloat() {}
00302
00303 Object* PointFloat::clone() const {
00304 PointFloat* p = new PointFloat(x, y, z, size);
00305 return p;
00306 };
00307
00308 PointFloat::operator Point() const {
00309 Point p;
00310 p.setSize(this->size);
00311 p.setX((int)this->x);
00312 p.setY((int)this->y);
00313 p.setZ((int)this->z);
00314 return p;
00315 }
00316
00317 double PointFloat::operator[](int n) const {
00318 switch(n) {
00319 case 0:
00320 return x;
00321 case 1:
00322 return y;
00323 case 2:
00324 return z;
00325 }
00326 return 0;
00327 }
00328
00329 bool PointFloat::operator==(const Point &p) const {
00330 return (
00331 (this->x == p.x) &&
00332 (this->y == p.y) &&
00333 (this->z == p.z) &&
00334 (this->size == p.size) );
00335 }
00336
00337 bool PointFloat::operator==(const PointFloat &p) const {
00338 return (
00339 (this->x == p.x) &&
00340 (this->y == p.y) &&
00341 (this->z == p.z) &&
00342 (this->size == p.size) );
00343 }
00344
00345 PointFloat PointFloat::operator-(const Point &p) const {
00346 PointFloat np = *this;
00347 np.x -= p.x;
00348 np.y -= p.y;
00349 np.z -= p.z;
00350 return np;
00351 }
00352 PointFloat PointFloat::operator-(const PointFloat &p) const {
00353 PointFloat np = *this;
00354 np.x -= p.x;
00355 np.y -= p.y;
00356 np.z -= p.z;
00357 return np;
00358 }
00359 PointFloat PointFloat::operator+(const Point &p) const {
00360 PointFloat np = *this;
00361 np.x += p.x;
00362 np.y += p.y;
00363 np.z += p.z;
00364 return np;
00365 }
00366 PointFloat PointFloat::operator+(const PointFloat &p) const {
00367 PointFloat np = *this;
00368 np.x += p.x;
00369 np.y += p.y;
00370 np.z += p.z;
00371 return np;
00372 }
00373 PointFloat PointFloat::operator*(const Point &p) const {
00374 PointFloat np = *this;
00375 np.x *= p.x;
00376 np.y *= p.y;
00377 np.z *= p.z;
00378 return np;
00379 }
00380 PointFloat PointFloat::operator*(const PointFloat &p) const {
00381 PointFloat np = *this;
00382 np.x *= p.x;
00383 np.y *= p.y;
00384 np.z *= p.z;
00385 return np;
00386 }
00387 PointFloat PointFloat::operator-(double a) const {
00388 PointFloat np = *this;
00389 np.x -= a;
00390 np.y -= a;
00391 np.z -= a;
00392 return np;
00393 }
00394 PointFloat PointFloat::operator+(double a) const {
00395 PointFloat np = *this;
00396 np.x += a;
00397 np.y += a;
00398 np.z += a;
00399 return np;
00400 }
00401 PointFloat PointFloat::operator*(double a) const {
00402 PointFloat np = *this;
00403 np.x *= a;
00404 np.y *= a;
00405 np.z *= a;
00406 return np;
00407 }
00408
00409 double PointFloat::getX() const { return x; }
00410 double PointFloat::getY() const { return y; }
00411 double PointFloat::getZ() const { return z; }
00412 Size PointFloat::getSize() const { return size; }
00413
00414 bool PointFloat::set(double xx, double yy, double zz) { x = xx; y = yy; z = zz; return true; }
00415 bool PointFloat::setX(double n) { x = n; return true; }
00416 bool PointFloat::setY(double n) { y = n; return true; }
00417 bool PointFloat::setZ(double n) { z = n; return true; }
00418 bool PointFloat::setSize(Size s) { size = s; return true; }
00419
00420 double PointFloat::getDistanceTo(const Point &p) const {
00421 return sqrt( pow((getX()-p.getX()), 2) + pow((getX()-p.getX()), 2) );
00422 }
00423 double PointFloat::getDistanceTo(const PointFloat &p) const {
00424 return sqrt( pow((getX()-p.getX()), 2) + pow((getX()-p.getX()), 2) );
00425 }
00426
00427 JString PointFloat::print() {
00428 if (size.isNonZero())
00429 return JString::format("(%.3f,%.3f,%.3f x %s)", x, y, z, (char*) size.print());
00430 else
00431 return JString::format("(%.3f,%.3f,%.3f)", x, y, z);
00432 }
00433
00434 JString PointFloat::toXML() {
00435 return JString::format("<pointfloat x=\"%f\" y=\"%f\" z=\"%f\" >\n%s</pointfloat>", x, y, z, (char*) size.toXML().indentXML());
00436 }
00437 bool PointFloat::fromXML(XMLNode* node) {
00438 if (node == NULL) {
00439 return false;
00440 }
00441 if (!node->getTag().equalsIgnoreCase("pointfloat"))
00442 return false;
00443 if ( (!node->hasAttribute("x")) || (!node->hasAttribute("y")))
00444 return false;
00445
00446 x = node->getAttribute("x").toDouble();
00447 y = node->getAttribute("y").toDouble();
00448 z = node->getAttribute("z").toDouble();
00449
00450 XMLNode* xmlNode = node->getChildNode("size");
00451 if (xmlNode != NULL) {
00452 size.fromXML(xmlNode);
00453 }
00454
00455 return true;
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 Line::Line() {
00469 lineWidth = 0;
00470 }
00471
00472 Line::Line(JString xml) {
00473 if (!Object::fromXML(xml)) {
00474 lineWidth=0;
00475 }
00476 }
00477
00478 Line::Line(XMLNode* node) {
00479 if (!fromXML(node)) {
00480 lineWidth=0;
00481 }
00482 }
00483
00484 Line::Line(PointFloat startpoint, PointFloat endpoint, double width) {
00485 startPoint = startpoint;
00486 endPoint = endpoint;
00487 lineWidth = width;
00488 }
00489
00490 Line::~Line() {
00491 }
00492
00493
00494 Object* Line::clone() const {
00495 Line* l = new Line();
00496 *l = *this;
00497 return l;
00498 }
00499
00500 PointFloat Line::getStartPoint() const {
00501 return startPoint;
00502 }
00503 PointFloat Line::getEndPoint() const {
00504 return endPoint;
00505 }
00506 double Line::getLineWidth() const {
00507 return lineWidth;
00508 }
00509
00510 bool Line::setStartPoint(PointFloat point) {
00511 startPoint = point;
00512 return true;
00513 }
00514 bool Line::setEndPoint(PointFloat point) {
00515 endPoint = point;
00516 return true;
00517 }
00518 bool Line::setLineWidth(double width) {
00519 lineWidth = width;
00520 return true;
00521 }
00522
00523 JString Line::print() {
00524 if (lineWidth != 0)
00525 return JString::format("%s-%sx(W: %.3f)", (char*) startPoint.print(), (char*) endPoint.print(), lineWidth);
00526 else
00527 return JString::format("%s-%s", (char*) startPoint.print(), (char*) endPoint.print());
00528 }
00529 JString Line::toXML() {
00530 return JString::format("<line width=\"%f\" >\n%s%s</line>", lineWidth, (char*) startPoint.toXML().indentXML(), (char*) endPoint.toXML().indentXML());
00531 }
00532 bool Line::fromXML(XMLNode* node) {
00533
00534 if (node == NULL) {
00535 return false;
00536 }
00537 if (!node->getTag().equalsIgnoreCase("line"))
00538 return false;
00539 lineWidth = node->getAttribute("width").toDouble();
00540
00541 ObjectCollection* coll = node->getChildTags();
00542 if ((coll == NULL) || (coll->getCount() < 1))
00543 return true;
00544
00545 XMLNode* xmlNode = (XMLNode*) coll->getFirst();
00546 if (!xmlNode->getTag().equalsIgnoreCase("PointFloat"))
00547 return false;
00548 startPoint = PointFloat(xmlNode);
00549 xmlNode = (XMLNode*) coll->get(1);
00550 if (!xmlNode->getTag().equalsIgnoreCase("PointFloat"))
00551 return false;
00552 endPoint = PointFloat(xmlNode);
00553
00554 return true;
00555 }
00556
00557
00558
00559
00560
00561
00562 PolyLine::PolyLine() {
00563 }
00564
00565 PolyLine::PolyLine(JString xml) {
00566 if (!Object::fromXML(xml)) {
00567 }
00568 }
00569 PolyLine::PolyLine(XMLNode* node) {
00570 if (!fromXML(node)) {
00571 }
00572 }
00573
00574 PolyLine::~PolyLine() {}
00575
00576 Object* PolyLine::clone() const {
00577 PolyLine* pl = new PolyLine();
00578 *pl = *this;
00579 return pl;
00580 }
00581
00582 int PolyLine::getLineCount() const {
00583 return lines.getCount();
00584 }
00585
00586 Line PolyLine::getLine(int pos) const {
00587 return *(Line*)lines.get(pos);
00588 }
00589 bool PolyLine::addLine(Line line) {
00590 return lines.add(line.clone());
00591 }
00592 bool PolyLine::replaceLine(int pos, Line newline) {
00593 return lines.replace(pos, newline.clone());
00594 }
00595 bool PolyLine::removeLine(int pos) {
00596 return lines.remove(pos);
00597 }
00598
00599 JString PolyLine::print() {
00600 if (lines.getCount() == 0)
00601 return "PolyLine: (empty)";
00602 JString str = "PolyLine: ";
00603 Line* line;
00604 int n;
00605 for (n=0; n<lines.getCount()-1; n++) {
00606 line = (Line*) lines.get(n);
00607 if (line != NULL)
00608 str += JString::format("%s,", (char*) line->print());
00609 }
00610 line = (Line*) lines.get(n);
00611 if (line != NULL)
00612 str += JString::format("%s", (char*) line->print());
00613 return str;
00614 }
00615 JString PolyLine::toXML() {
00616 return JString::format("<polyline count=\"%d\">\n%s</vector>", lines.getCount(), (char*) lines.toXML().indentXML());
00617 }
00618 bool PolyLine::fromXML(XMLNode* node) {
00619
00620 if (node == NULL) {
00621 return false;
00622 }
00623 if (!node->getTag().equalsIgnoreCase("polyline"))
00624 return false;
00625
00626 XMLNode* xmlNode = node->getChildNode("collection");
00627
00628 if (xmlNode == NULL) {
00629 return false;
00630 }
00631
00632 lines = ObjectCollection(xmlNode);
00633
00634 return true;
00635 }
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648 VectorOfInts::VectorOfInts() {}
00649 VectorOfInts::VectorOfInts(JString xml) {
00650 if (!Object::fromXML(xml)) {
00651 }
00652 }
00653 VectorOfInts::VectorOfInts(XMLNode* node) {
00654 if (!fromXML(node)) {
00655 }
00656 }
00657 VectorOfInts::~VectorOfInts() {}
00658
00659 Object* VectorOfInts::clone() const {
00660 VectorOfInts* v = new VectorOfInts();
00661 v->coll = coll;
00662 return v;
00663 }
00664
00665 int VectorOfInts::operator[](int n) const {
00666 return coll.get(n).toInt();
00667 }
00668
00669 int VectorOfInts::get(int pos) const {
00670 return coll.get(pos).toInt();
00671 }
00672 bool VectorOfInts::set(int pos, int val) {
00673 if (coll.getCount() <= pos) return false;
00674 return coll.replace(pos, JString(val));
00675 }
00676 bool VectorOfInts::add(int val) {
00677 return coll.add(JString(val));
00678 }
00679 bool VectorOfInts::remove(int pos) {
00680 return coll.remove(pos);
00681 }
00682
00683 int VectorOfInts::getSize() const { return coll.getCount(); }
00684
00685 JString VectorOfInts::print() {
00686 if (coll.getCount() == 0)
00687 return "Vector: (empty)";
00688 JString str = "Vector: ";
00689 int n;
00690 for (n=0; n<coll.getCount()-1; n++) {
00691 str += JString::format("%s,", (char*) coll.get(n));
00692 }
00693 str += JString::format("%s", (char*) coll.get(n));
00694 return str;
00695 }
00696
00697 JString VectorOfInts::toXML() {
00698 return JString::format("<vector type=\"int\">\n%s</vector>", (char*) coll.toXML().indentXML());
00699 }
00700
00701 bool VectorOfInts::fromXML(XMLNode* node) {
00702
00703 if (node == NULL) {
00704 return false;
00705 }
00706 if (!node->getTag().equalsIgnoreCase("vector"))
00707 return false;
00708 if (!node->getAttribute("type").equals("int"))
00709 return false;
00710
00711 XMLNode* xmlNode = node->getChildNode("collection");
00712
00713 if (xmlNode == NULL) {
00714 return false;
00715 }
00716
00717 coll = Collection(xmlNode);
00718
00719 return true;
00720 }
00721
00722
00723
00724
00725
00726
00727 VectorOfDoubles::VectorOfDoubles() {}
00728 VectorOfDoubles::VectorOfDoubles(JString xml) {
00729 if (!Object::fromXML(xml)) {
00730 }
00731 }
00732 VectorOfDoubles::VectorOfDoubles(XMLNode* node) {
00733 if (!fromXML(node)) {
00734 }
00735 }
00736 VectorOfDoubles::~VectorOfDoubles() {}
00737
00738 Object* VectorOfDoubles::clone() const {
00739 VectorOfDoubles* v = new VectorOfDoubles();
00740 v->coll = coll;
00741 return v;
00742 }
00743
00744 double VectorOfDoubles::operator[](int n) const {
00745 return coll.get(n).toDouble();
00746 }
00747
00748 double VectorOfDoubles::get(int pos) const {
00749 return coll.get(pos).toDouble();
00750 }
00751 bool VectorOfDoubles::set(int pos, double val) {
00752 if (coll.getCount() <= pos) return false;
00753 return coll.replace(pos, JString(val));
00754 }
00755 bool VectorOfDoubles::add(double val) {
00756 return coll.add(JString(val));
00757 }
00758 bool VectorOfDoubles::remove(int pos) {
00759 return coll.remove(pos);
00760 }
00761
00762 int VectorOfDoubles::getSize() const { return coll.getCount(); }
00763
00764 JString VectorOfDoubles::print() {
00765 if (coll.getCount() == 0)
00766 return "Vector: (empty)";
00767 JString str = "Vector: ";
00768 int n;
00769 for (n=0; n<coll.getCount()-1; n++) {
00770 str += JString::format("%s,", (char*) coll.get(n));
00771 }
00772 str += JString::format("%s", (char*) coll.get(n));
00773 return str;
00774 }
00775
00776 JString VectorOfDoubles::toXML() {
00777 return JString::format("<vector type=\"double\">\n%s</vector>", (char*) coll.toXML().indentXML());
00778 }
00779
00780 bool VectorOfDoubles::fromXML(XMLNode* node) {
00781
00782 if (node == NULL) {
00783 return false;
00784 }
00785 if (!node->getTag().equalsIgnoreCase("vector"))
00786 return false;
00787 if (!node->getAttribute("type").equals("double"))
00788 return false;
00789
00790 XMLNode* xmlNode = node->getChildNode("collection");
00791
00792 if (xmlNode == NULL) {
00793 return false;
00794 }
00795
00796 coll = Collection(xmlNode);
00797
00798 return true;
00799 }
00800
00801
00802
00803
00804
00805
00806 VectorOfPoints::VectorOfPoints() {}
00807 VectorOfPoints::VectorOfPoints(JString xml) {
00808 if (!Object::fromXML(xml)) {
00809 }
00810 }
00811 VectorOfPoints::VectorOfPoints(XMLNode* node) {
00812 if (!fromXML(node)) {
00813 }
00814 }
00815 VectorOfPoints::~VectorOfPoints() {}
00816
00817 Object* VectorOfPoints::clone() const {
00818 VectorOfPoints* v = new VectorOfPoints();
00819 v->coll.copyAll(coll);
00820 return v;
00821 }
00822
00823 Point VectorOfPoints::operator[](int n) const {
00824 return *(Point*) coll.get(n);
00825 }
00826
00827 Point VectorOfPoints::get(int pos) const {
00828 return *(Point*) coll.get(pos);
00829 }
00830 bool VectorOfPoints::set(int pos, Point p) {
00831 if (coll.getCount() <= pos) return false;
00832 Point* newp = new Point();
00833 *newp = p;
00834 return coll.replace(pos, newp);
00835 }
00836 bool VectorOfPoints::add(Point p) {
00837 Point* newp = new Point();
00838 *newp = p;
00839 return coll.add(newp);
00840 }
00841 bool VectorOfPoints::remove(int pos) {
00842 return coll.remove(pos);
00843 }
00844
00845 int VectorOfPoints::getSize() const { return coll.getCount(); }
00846
00847 JString VectorOfPoints::print() {
00848 if (coll.getCount() == 0)
00849 return "Vector: (empty)";
00850 JString str = "Vector: ";
00851 int n;
00852 for (n=0; n<coll.getCount()-1; n++) {
00853 str += JString::format("%s,", (char*) coll.get(n)->print());
00854 }
00855 str += JString::format("%s", (char*) coll.get(n)->print());
00856 return str;
00857 }
00858
00859 JString VectorOfPoints::toXML() {
00860 return JString::format("<vector type=\"Point\">\n%s</vector>", (char*) coll.toXML().indentXML());
00861 }
00862
00863 bool VectorOfPoints::fromXML(XMLNode* node) {
00864
00865 if (node == NULL) {
00866 return false;
00867 }
00868 if (!node->getTag().equalsIgnoreCase("vector"))
00869 return false;
00870 if (!node->getAttribute("type").equals("Point"))
00871 return false;
00872
00873 XMLNode* xmlNode = node->getChildNode("collection");
00874
00875 if (xmlNode == NULL) {
00876 return false;
00877 }
00878
00879 coll = ObjectCollection(xmlNode);
00880
00881 return true;
00882 }
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907 Box::Box() {
00908 lineWidth = 0;
00909 orientation = 0;
00910 }
00911
00912 Box::Box(JString xml) {
00913 if (!Object::fromXML(xml)) {
00914 lineWidth=0;
00915 orientation = 0;
00916 }
00917 }
00918
00919 Box::Box(XMLNode* node) {
00920 if (!fromXML(node)) {
00921 lineWidth=0;
00922 orientation = 0;
00923 }
00924 }
00925
00926 Box::Box(double x, double y, double w, double h, double linewidth) {
00927 upperLeft = PointFloat(x, y);
00928 size = Size(w, h);
00929
00930 if (size.getWidth() < 0) {
00931 size.setWidth(-1*size.getWidth());
00932 upperLeft.setX(upperLeft.getX()-size.getWidth());
00933 }
00934
00935 if (size.getHeight() < 0) {
00936 size.setHeight(-1*size.getHeight());
00937 upperLeft.setY(upperLeft.getY()-size.getHeight());
00938 }
00939
00940 lineWidth = linewidth;
00941 orientation = 0;
00942 }
00943
00944 Box::Box(PointFloat upperleft, Size boxsize, double linewidth) {
00945 upperLeft = upperleft;
00946 size = boxsize;
00947
00948 if (size.getWidth() < 0) {
00949 size.setWidth(-1*size.getWidth());
00950 upperLeft.setX(upperLeft.getX()-size.getWidth());
00951 }
00952
00953 if (size.getHeight() < 0) {
00954 size.setHeight(-1*size.getHeight());
00955 upperLeft.setY(upperLeft.getY()-size.getHeight());
00956 }
00957
00958 lineWidth = linewidth;
00959 orientation = 0;
00960 }
00961
00962 Box::Box(PointFloat upperleft, PointFloat lowerright, double linewidth) {
00963 upperLeft = upperleft;
00964 size = Size(lowerright.getX() - upperleft.getX(), lowerright.getY() - upperleft.getY());
00965
00966 if (size.getWidth() < 0) {
00967 size.setWidth(-1*size.getWidth());
00968 upperLeft.setX(upperLeft.getX()-size.getWidth());
00969 }
00970
00971 if (size.getHeight() < 0) {
00972 size.setHeight(-1*size.getHeight());
00973 upperLeft.setY(upperLeft.getY()-size.getHeight());
00974 }
00975
00976 lineWidth = linewidth;
00977 orientation = 0;
00978 }
00979
00980 Box::~Box() {
00981 }
00982
00983
00984 Object* Box::clone() const {
00985 Box* l = new Box();
00986 *l = *this;
00987 return l;
00988 }
00989
00990
00991 double Box::getCMX() const {
00992 return getCM().getX();
00993 }
00994
00995 double Box::getCMY() const {
00996 return getCM().getY();
00997 }
00998
00999 PointFloat Box::getCM() const {
01000 return PointFloat(upperLeft.getX()+(size.getWidth()/2), upperLeft.getY()+(size.getHeight()/2));
01001 }
01002
01003 PointFloat Box::getUpperLeft() const {
01004 return upperLeft;
01005 }
01006
01007 PointFloat Box::getUpperRight() const {
01008 return PointFloat(upperLeft.getX()+size.getWidth(), upperLeft.getY());
01009 }
01010
01011 PointFloat Box::getLowerLeft() const {
01012 return PointFloat(upperLeft.getX(), upperLeft.getY()+size.getHeight());
01013 }
01014
01015 PointFloat Box::getLowerRight() const {
01016 return PointFloat(upperLeft.getX()+size.getWidth(), upperLeft.getY()+size.getHeight());
01017 }
01018
01019 double Box::getUpperY() const {
01020 return upperLeft.getY();
01021 }
01022
01023 double Box::getLowerY() const {
01024 return upperLeft.getY() + size.getHeight();
01025 }
01026
01027 double Box::getLeftX() const {
01028 return upperLeft.getX();
01029 }
01030
01031 double Box::getRightX() const {
01032 return upperLeft.getX() + size.getWidth();
01033 }
01034
01035
01036 double Box::getLineWidth() const {
01037 return lineWidth;
01038 }
01039
01040 Size Box::getSize() const {
01041 return size;
01042 }
01043
01044 double Box::getArea() const {
01045 return ( size.w * size.h );
01046 }
01047
01048 double Box::getWidth() const {
01049 return size.getWidth();
01050 }
01051
01052 double Box::getHeight() const {
01053 return size.getHeight();
01054 }
01055
01056
01057 bool Box::move(double dx, double dy) {
01058 return set(getLeftX() + dx, getUpperY() + dy, getWidth(), getHeight());
01059 }
01060
01061 bool Box::moveTo(double x, double y) {
01062 return set(x, y, getWidth(), getHeight());
01063 }
01064
01065 bool Box::grow(double dw, double dh) {
01066 double w = getWidth() + dw;
01067 double h = getHeight() + dh;
01068 return setSize(Size(w, h));
01069 }
01070
01071
01072
01073 Box Box::getConstrainedCopy(const Box& box) {
01074 Box newbox = *this;
01075 newbox.constrainTo(box);
01076 return newbox;
01077 }
01078
01079 Box Box::getConstrainedCopy(double x, double y, double w, double h) {
01080 Box newbox = *this;
01081 newbox.constrainTo(x, y, w, h);
01082 return newbox;
01083 }
01084
01085 bool Box::constrainTo(const Box& box) {
01086 return constrainTo(box.upperLeft.x, box.upperLeft.y, box.size.w, box.size.h);
01087 }
01088
01089 bool Box::constrainTo(double x, double y, double w, double h) {
01090 if (upperLeft.x < x)
01091 upperLeft.x = 0;
01092 if (upperLeft.y < y)
01093 upperLeft.y = 0;
01094 if (upperLeft.x + size.w > w)
01095 size.w = w - upperLeft.x;
01096 if (upperLeft.y + size.h > h)
01097 size.h = h - upperLeft.y;
01098 return true;
01099 }
01100
01101 Box Box::getDoubleSizeSameCenter() {
01102 Box box;
01103 box.upperLeft.x = upperLeft.x - (size.w / 2.0);
01104 box.upperLeft.y = upperLeft.y - (size.h / 2.0);
01105 box.size.w = size.w * 2.0;
01106 box.size.h = size.h * 2.0;
01107 return box;
01108 }
01109
01110 bool Box::set(double x, double y, double w, double h, double linewidth) {
01111 upperLeft = PointFloat(x, y);
01112 size = Size(w, h);
01113
01114 if (size.getWidth() < 0) {
01115 size.setWidth(-1*size.getWidth());
01116 upperLeft.setX(upperLeft.getX()-size.getWidth());
01117 }
01118
01119 if (size.getHeight() < 0) {
01120 size.setHeight(-1*size.getHeight());
01121 upperLeft.setY(upperLeft.getY()-size.getHeight());
01122 }
01123
01124 lineWidth = linewidth;
01125 return true;
01126 }
01127
01128
01129 bool Box::setUpperLeft(const PointFloat& point) {
01130 upperLeft = point;
01131 return true;
01132 }
01133
01134 bool Box::setSize(const Size& boxsize) {
01135 size = boxsize;
01136 return true;
01137 }
01138
01139 bool Box::setLineWidth(double width) {
01140 lineWidth = width;
01141 return true;
01142 }
01143
01144
01145 bool Box::isPointWithin(int x, int y) const {
01146 if ( x > this->getRightX() )
01147 return false;
01148 else if ( x < this->getLeftX() )
01149 return false;
01150 else if ( y < this->getUpperY() )
01151 return false;
01152 else if ( y > this->getLowerY() )
01153 return false;
01154 return true;
01155 }
01156
01157 bool Box::isPointWithin(const PointFloat& point) const {
01158 if ( point.getX() > this->getRightX() )
01159 return false;
01160 else if ( point.getX() < this->getLeftX() )
01161 return false;
01162 else if ( point.getY() < this->getUpperY() )
01163 return false;
01164 else if ( point.getY() > this->getLowerY() )
01165 return false;
01166 return true;
01167 }
01168
01169 PointFloat Box::getCentreMass() const {
01170 return PointFloat(upperLeft.getX()+(size.getWidth()/2), upperLeft.getY()+(size.getHeight()/2));
01171 }
01172
01173 bool Box::hasZeroSize() const {
01174 return (!size.isNonZero());
01175 }
01176
01177 bool Box::equals(const Box &otherbox) const {
01178 return (
01179 (this->size == otherbox.size) &&
01180 (this->upperLeft == otherbox.upperLeft) &&
01181 (this->orientation == otherbox.orientation) );
01182 }
01183
01184 bool Box::equals(const Box &otherbox, double maxerror) const {
01185 if (maxerror == 0.0)
01186 maxerror = -0.001;
01187 return (percentOverlap(otherbox) <= maxerror);
01188 }
01189
01190 bool Box::growToBoundingBox(const Box &otherbox) {
01191 Box bb = getBoundingBox(otherbox);
01192 if (bb.hasZeroSize())
01193 return false;
01194 this->upperLeft = bb.upperLeft;
01195 this->size = bb.size;
01196 return true;
01197 }
01198
01199 bool Box::growToIncludePoint(const Point& point, int padX, int padY) {
01200 double dif;
01201 if ( ( dif = upperLeft.x - (point.x - padX) ) > 0 ) {
01202 upperLeft.x -= dif;
01203 size.w += dif;
01204 }
01205 if ( ( dif = upperLeft.y - (point.y - padY) ) > 0 ) {
01206 upperLeft.y -= dif;
01207 size.h += dif;
01208 }
01209
01210 if ( ( dif = point.x + padX - (upperLeft.x + size.w) ) > 0 ) {
01211 size.w += dif;
01212 }
01213 if ( ( dif = point.y + padY - (upperLeft.y + size.h) ) > 0 ) {
01214 size.h += dif;
01215 }
01216 return true;
01217 }
01218
01219 Box Box::getBoundingBox(const Box &otherbox) const {
01220
01221 double left, right, top, bottom;
01222
01223 if (this->getLeftX() <= otherbox.getLeftX())
01224 left = this->getLeftX();
01225 else
01226 left = otherbox.getLeftX();
01227
01228 if (this->getRightX() <= otherbox.getRightX())
01229 right = otherbox.getRightX();
01230 else
01231 right = this->getRightX();
01232
01233 if (this->getUpperY() <= otherbox.getUpperY())
01234 top = this->getUpperY();
01235 else
01236 top = otherbox.getUpperY();
01237
01238 if (this->getLowerY() <= otherbox.getLowerY())
01239 bottom = otherbox.getLowerY();
01240 else
01241 bottom = this->getLowerY();
01242
01243 return Box(PointFloat(left, top), PointFloat(right, bottom));
01244 }
01245
01246 Box Box::getOverlapBox(const Box &otherbox) const {
01247
01248 Box zeroOverlap = Box();
01249
01250
01251 if ( otherbox.getLeftX() > this->getRightX() )
01252 return zeroOverlap;
01253 else if ( otherbox.getRightX() < this->getLeftX() )
01254 return zeroOverlap;
01255 else if ( otherbox.getLowerY() < this->getUpperY() )
01256 return zeroOverlap;
01257 else if ( otherbox.getUpperY() > this->getLowerY() )
01258 return zeroOverlap;
01259
01260
01261
01262 double left, right, top, bottom;
01263
01264 if (this->getLeftX() <= otherbox.getLeftX())
01265 left = otherbox.getLeftX();
01266 else
01267 left = this->getLeftX();
01268
01269 if (this->getRightX() <= otherbox.getRightX())
01270 right = this->getRightX();
01271 else
01272 right = otherbox.getRightX();
01273
01274 if (this->getUpperY() <= otherbox.getUpperY())
01275 top = otherbox.getUpperY();
01276 else
01277 top = this->getUpperY();
01278
01279 if (this->getLowerY() <= otherbox.getLowerY())
01280 bottom = this->getLowerY();
01281 else
01282 bottom = otherbox.getLowerY();
01283
01284 return Box(PointFloat(left, top), PointFloat(right, bottom));
01285 }
01286
01287 double Box::percentOverlap(const Box &otherbox) const {
01288
01289 Box overlap = getOverlapBox(otherbox);
01290 if (!overlap.getSize().isNonZero())
01291 return 0.0;
01292
01293 double myArea = size.getArea();
01294 double overlapArea = overlap.getSize().getArea();
01295
01296
01297 if ( (myArea > overlapArea) && (myArea != 0.0) )
01298 return ( overlapArea / myArea );
01299 else if (overlapArea != 0.0)
01300 return ( myArea / overlapArea );
01301 else
01302 return 0;
01303 }
01304
01305
01306
01307
01308
01309
01310 JString Box::print() {
01311 if (lineWidth != 0)
01312 return JString::format("%s-%sx(W: %.3f)", (char*) getUpperLeft().print(), (char*) getLowerRight().print(), lineWidth);
01313 else
01314 return JString::format("%s-%s", (char*) getUpperLeft().print(), (char*) getLowerRight().print());
01315 }
01316
01317 JString Box::toXML() {
01318 if (lineWidth > 0)
01319 return JString::format("<box name=\"%s\" linewidth=\"%f\" x=\"%f\" y=\"%f\" w=\"%f\" h=\"%f\"/>",
01320 (char*) name, lineWidth, getLeftX(), getUpperY(), getWidth(), getHeight());
01321 else
01322 return JString::format("<box name=\"%s\" x=\"%f\" y=\"%f\" w=\"%f\" h=\"%f\"/>",
01323 (char*) name, getLeftX(), getUpperY(), getWidth(), getHeight());
01324 }
01325
01326 bool Box::fromXML(XMLNode* node) {
01327
01328 if (node == NULL) {
01329 return false;
01330 }
01331 if (!node->getTag().equalsIgnoreCase("box"))
01332 return false;
01333
01334 name = node->getAttribute("name");
01335
01336 if ( (node->hasAttribute("cx")) && (node->hasAttribute("cy")) ) {
01337 double cx = node->getAttribute("cx").toDouble();
01338 double cy = node->getAttribute("cy").toDouble();
01339 double w = node->getAttribute("w").toDouble();
01340 double h = node->getAttribute("h").toDouble();
01341 this->set(cx - (int)(w/2), cy - (int)(h/2), w, h,
01342 node->getAttribute("linewidth").toDouble());
01343 }
01344 else if ( (node->hasAttribute("xc")) && (node->hasAttribute("yc")) ) {
01345 double xc = node->getAttribute("xc").toDouble();
01346 double yc = node->getAttribute("yc").toDouble();
01347 double w = node->getAttribute("w").toDouble();
01348 double h = node->getAttribute("h").toDouble();
01349 this->set(xc - (int)(w/2), yc - (int)(h/2), w, h,
01350 node->getAttribute("linewidth").toDouble());
01351 }
01352 else {
01353 this->set(node->getAttribute("x").toDouble(),
01354 node->getAttribute("y").toDouble(),
01355 node->getAttribute("w").toDouble(),
01356 node->getAttribute("h").toDouble(),
01357 node->getAttribute("linewidth").toDouble());
01358 }
01359
01360 XMLNode* xmlNode;
01361 ObjectCollection* coll = node->getChildTags();
01362
01363 if (coll != NULL) {
01364 for (int n=0; n<coll->getCount(); n++) {
01365 xmlNode = (XMLNode*) coll->get(n);
01366 if (xmlNode != NULL) {
01367 if (xmlNode->getTag().equalsIgnoreCase("PointFloat"))
01368 upperLeft = PointFloat(xmlNode);
01369 if (xmlNode->getTag().equalsIgnoreCase("Size"))
01370 size = Size(xmlNode);
01371 }
01372 }
01373 }
01374
01375 return true;
01376 }
01377
01378 bool Box::fromXML(XMLNode* node, const Box &parentBox) {
01379
01380 if (node == NULL) {
01381 return false;
01382 }
01383 if (!node->getTag().equalsIgnoreCase("box"))
01384 return false;
01385
01386 name = node->getAttribute("name");
01387
01388 if ( (node->hasAttribute("cx")) && (node->hasAttribute("cy")) ) {
01389 double cx = node->getAttribute("cx").toDouble();
01390 double cy = node->getAttribute("cy").toDouble();
01391 double w = node->getAttribute("w").toDouble();
01392 double h = node->getAttribute("h").toDouble();
01393 this->set(cx - (int)(w/2), cy - (int)(h/2), w, h,
01394 node->getAttribute("linewidth").toDouble());
01395 }
01396 else if ( (node->hasAttribute("xc")) && (node->hasAttribute("yc")) ) {
01397 double xc = node->getAttribute("xc").toDouble();
01398 double yc = node->getAttribute("yc").toDouble();
01399 double w = node->getAttribute("w").toDouble();
01400 double h = node->getAttribute("h").toDouble();
01401 this->set(xc - (int)(w/2), yc - (int)(h/2), w, h,
01402 node->getAttribute("linewidth").toDouble());
01403 }
01404 else {
01405 double x, y, w, h;
01406 JString xstr = node->getAttribute("x");
01407 JString ystr = node->getAttribute("y");
01408 JString wstr = node->getAttribute("w");
01409 JString hstr = node->getAttribute("h");
01410 if (xstr.contains("%"))
01411 x = (xstr.toDouble()/100.0) * parentBox.upperLeft.x;
01412 else
01413 x = xstr.toDouble();
01414 if (ystr.contains("%"))
01415 y = (ystr.toDouble()/100.0) * parentBox.upperLeft.y;
01416 else
01417 y = ystr.toDouble();
01418 if (wstr.contains("%"))
01419 w = (wstr.toDouble()/100.0) * parentBox.size.w;
01420 else
01421 w = wstr.toDouble();
01422 if (hstr.contains("%"))
01423 h = (hstr.toDouble()/100.0) * parentBox.size.h;
01424 else
01425 h = hstr.toDouble();
01426
01427 this->set(x, y, w, h, node->getAttribute("linewidth").toDouble());
01428 }
01429
01430 XMLNode* xmlNode;
01431 ObjectCollection* coll = node->getChildTags();
01432
01433 if (coll != NULL) {
01434 for (int n=0; n<coll->getCount(); n++) {
01435 xmlNode = (XMLNode*) coll->get(n);
01436 if (xmlNode != NULL) {
01437 if (xmlNode->getTag().equalsIgnoreCase("PointFloat"))
01438 upperLeft = PointFloat(xmlNode);
01439 if (xmlNode->getTag().equalsIgnoreCase("Size"))
01440 size = Size(xmlNode);
01441 }
01442 }
01443 }
01444
01445 return true;
01446 }
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461 bool StatSample::add(double val) {
01462 return values.add(JString(val, 20, 20));
01463 }
01464
01465 int StatSample::getCount() const {
01466 return values.getCount();
01467 }
01468
01469 double StatSample::getMin() const {
01470
01471 if (getCount() <= 0) return 0;
01472
01473 double minValue = values.get(0).toDouble();
01474
01475 double m;
01476 for (int n=1; n<getCount(); n++) {
01477 m = values.get(n).toDouble();
01478 if (m < minValue)
01479 minValue = m;
01480 }
01481 return minValue;
01482 }
01483
01484 double StatSample::getMax() const {
01485
01486 if (getCount() <= 0) return 0;
01487
01488 double maxValue = values.get(0).toDouble();
01489
01490 double m;
01491 for (int n=1; n<getCount(); n++) {
01492 m = values.get(n).toDouble();
01493 if (m > maxValue)
01494 maxValue = m;
01495 }
01496 return maxValue;
01497 }
01498
01499 double StatSample::getMean() const {
01500
01501 if (getCount() <= 0) return 0;
01502
01503 double total = 0;
01504
01505 for (int n=0; n<getCount(); n++) {
01506 total += values.get(n).toDouble();
01507 }
01508 return total/getCount();
01509 }
01510
01511 double StatSample::getRobustMean() const {
01512
01513 if (getCount() <= 0) return 0;
01514
01515 double total = 0;
01516
01517 Collection col = values;
01518 col.sortValues();
01519 int m = (int)((col.getCount()+1.0)/2.0);
01520 return col.get(m).toDouble();
01521 }
01522
01523 double StatSample::getVariance() const {
01524
01525 if (getCount() <= 1) return 0;
01526
01527 double total = 0;
01528 double mean = getMean();
01529
01530 for (int n=0; n<getCount(); n++) {
01531 total += pow((values.get(n).toDouble() - mean), 2);
01532 }
01533 return total/(getCount()-1);
01534
01535 }
01536
01537 double StatSample::getStandardDeviation() const {
01538 double var = getVariance();
01539 if (var <= 0) return 0;
01540 return sqrt(var);
01541 }
01542
01543 double StatSample::getRobustStandardDeviation() const {
01544
01545 if (getCount() <= 1) return 0;
01546
01547 double robustMean = getRobustMean();
01548
01549 Collection col;
01550 for (int n=0; n<getCount(); n++) {
01551 col.add(values.get(n).toDouble() - robustMean);
01552 }
01553 col.sortValues();
01554
01555 int m = (int)((col.getCount()+1.0)/2.0);
01556 return 1.4826 * col.get(m).toDouble();
01557 }
01558
01559 JString StatSample::print() {
01560 return JString::format("Mean: %f Var: %f SD: %f", getMean(), getVariance(), getStandardDeviation());
01561 }
01562
01563 JString StatSample::printList() {
01564 return values.printListLine(",");
01565 }
01566
01567 bool StatSample::removeAll() {
01568 return values.removeAll();
01569 }
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585 Vector2D::Vector2D() { x=0; y=0; }
01586 Vector2D::Vector2D(double x, double y) { this->x=x; this->y=y; }
01587 Vector2D::Vector2D(JString xml) {
01588 if (!Object::fromXML(xml)) {
01589 x=0; y=0;
01590 }
01591 }
01592 Vector2D::Vector2D(XMLNode* node) {
01593 if (!fromXML(node)) {
01594 x=0; y=0;
01595 }
01596 }
01597 Vector2D::~Vector2D() {}
01598
01599 Object* Vector2D::clone() const {
01600 Vector2D* v = new Vector2D(x, y);
01601 return v;
01602 };
01603
01604 double Vector2D::operator[](int n) const {
01605 switch(n) {
01606 case 0:
01607 return x;
01608 case 1:
01609 return y;
01610 }
01611 return 0;
01612 }
01613
01614 bool Vector2D::operator==(const Vector2D &v) const {
01615 return (
01616 (this->x == v.x) &&
01617 (this->y == v.y) );
01618 }
01619
01620 Vector2D Vector2D::operator-(const Vector2D &v) const {
01621 Vector2D np = *this;
01622 np.x -= v.x;
01623 np.y -= v.y;
01624 return np;
01625 }
01626
01627 Vector2D Vector2D::operator+(const Vector2D &v) const {
01628 Vector2D np = *this;
01629 np.x += v.x;
01630 np.y += v.y;
01631 return np;
01632 }
01633
01634 double Vector2D::operator*(const Vector2D &v) const {
01635 return (x*v.x) + (y*v.y);
01636 }
01637
01638 Vector2D Vector2D::operator-(double a) const {
01639 Vector2D np = *this;
01640 np.x -= a;
01641 np.y -= a;
01642 return np;
01643 }
01644
01645 Vector2D Vector2D::operator+(double a) const {
01646 Vector2D np = *this;
01647 np.x += a;
01648 np.y += a;
01649 return np;
01650 }
01651
01652 Vector2D Vector2D::operator*(double a) const {
01653 Vector2D np = *this;
01654 np.x *= a;
01655 np.y *= a;
01656 return np;
01657 }
01658
01659 const Vector2D& Vector2D::operator-=(const Vector2D &v) {
01660 this->x -= v.x;
01661 this->y -= v.y;
01662 return *this;
01663 }
01664
01665 const Vector2D& Vector2D::operator+=(const Vector2D &v) {
01666 this->x += v.x;
01667 this->y += v.y;
01668 return *this;
01669 }
01670
01671 const Vector2D& Vector2D::operator-=(double a) {
01672 this->x -= a;
01673 this->y -= a;
01674 return *this;
01675 }
01676
01677 const Vector2D& Vector2D::operator+=(double a) {
01678 this->x += a;
01679 this->y += a;
01680 return *this;
01681 }
01682
01683 const Vector2D& Vector2D::operator*=(double a) {
01684 this->x *= a;
01685 this->y *= a;
01686 return *this;
01687 }
01688
01689
01690
01691 double Vector2D::getX() const { return x; }
01692 double Vector2D::getY() const { return y; }
01693
01694 bool Vector2D::set(const PointFloat& p1, const PointFloat& p2) {
01695 return set(p1.x, p1.y, p2.x, p2.y);
01696 }
01697
01698 bool Vector2D::set(double x1, double y1, double x2, double y2) {
01699 return set(x2-x1, y2-y1);
01700 }
01701
01702 bool Vector2D::set(double xx, double yy) { x = xx; y = yy; return true; }
01703 bool Vector2D::setX(double n) { x = n; return true; }
01704 bool Vector2D::setY(double n) { y = n; return true; }
01705
01706 JString Vector2D::print() {
01707 return JString::format("(%.3f,%.3f)", x, y);
01708 }
01709
01710 JString Vector2D::toXML() {
01711 return JString::format("<vector2d x=\"%f\" y=\"%f\" />", x, y);
01712 }
01713
01714 bool Vector2D::fromXML(XMLNode* node) {
01715 if (node == NULL) {
01716 return false;
01717 }
01718 if ( (!node->hasAttribute("x")) || (!node->hasAttribute("y")))
01719 return false;
01720
01721 x = node->getAttribute("x").toDouble();
01722 y = node->getAttribute("y").toDouble();
01723 return true;
01724 }
01725
01726 double Vector2D::length() const {
01727 return sqrt((x*x) + (y*y));
01728 }
01729
01730 double Vector2D::det(const Vector2D &v) const {
01731 return (x*v.y) - (y*v.x);
01732 }
01733
01734 bool Vector2D::isOrthogonalWith(const Vector2D &v) const {
01735 return (*this*v == 0);
01736 }
01737
01738 bool Vector2D::isParallelWith(const Vector2D &v) const {
01739 return (det(v) == 0);
01740 }
01741
01742 Vector2D Vector2D::getProjectionOn(const Vector2D &v) const {
01743 Vector2D unit = v.getUnitVector();
01744 return unit*(*this*unit);
01745 }
01746
01747 Vector2D Vector2D::getUnitVector() const {
01748 return *this*(1.0/length());
01749 }
01750
01751 Vector2D Vector2D::getOrthogonalVector() const {
01752 return Vector2D(0-y, x);
01753 }
01754
01755 double Vector2D::getAngle(const Vector2D &v) const {
01756 double lengths = this->length() * v.length();
01757 if (lengths == 0)
01758 return 0;
01759 return acos((*this*v)/lengths);
01760 }
01761
01762 double Vector2D::getArea(const Vector2D &v) const {
01763 return fabs(det(v));
01764 }
01765
01766
01767
01768
01769
01770
01771
01772
01773 Vector3D::Vector3D() { x=0; y=0; z=0; }
01774 Vector3D::Vector3D(double x, double y, double z) { this->x=x; this->y=y; this->z=z; }
01775 Vector3D::Vector3D(JString xml) {
01776 if (!Object::fromXML(xml)) {
01777 x=0; y=0; z=0;
01778 }
01779 }
01780 Vector3D::Vector3D(XMLNode* node) {
01781 if (!fromXML(node)) {
01782 x=0; y=0; z=0;
01783 }
01784 }
01785 Vector3D::~Vector3D() {}
01786
01787 Object* Vector3D::clone() const {
01788 Vector3D* v = new Vector3D(x, y, z);
01789 return v;
01790 };
01791
01792 double Vector3D::operator[](int n) const {
01793 switch(n) {
01794 case 0:
01795 return x;
01796 case 1:
01797 return y;
01798 case 2:
01799 return z;
01800 }
01801 return 0;
01802 }
01803
01804 bool Vector3D::operator==(const Vector3D &v) const {
01805 return (
01806 (this->x == v.x) &&
01807 (this->y == v.y) &&
01808 (this->z == v.z) );
01809 }
01810
01811 Vector3D Vector3D::operator-(const Vector3D &v) const {
01812 Vector3D np = *this;
01813 np.x -= v.x;
01814 np.y -= v.y;
01815 np.z -= v.z;
01816 return np;
01817 }
01818
01819 Vector3D Vector3D::operator+(const Vector3D &v) const {
01820 Vector3D np = *this;
01821 np.x += v.x;
01822 np.y += v.y;
01823 np.z += v.z;
01824 return np;
01825 }
01826
01827 double Vector3D::operator*(const Vector3D &v) const {
01828 return (x*v.x) + (y*v.y) + (z*v.z);
01829 }
01830
01831 Vector3D Vector3D::operator-(double a) const {
01832 Vector3D np = *this;
01833 np.x -= a;
01834 np.y -= a;
01835 np.z -= a;
01836 return np;
01837 }
01838
01839 Vector3D Vector3D::operator+(double a) const {
01840 Vector3D np = *this;
01841 np.x += a;
01842 np.y += a;
01843 np.z += a;
01844 return np;
01845 }
01846
01847 Vector3D Vector3D::operator*(double a) const {
01848 Vector3D np = *this;
01849 np.x *= a;
01850 np.y *= a;
01851 np.z *= a;
01852 return np;
01853 }
01854
01855 const Vector3D& Vector3D::operator-=(const Vector3D &v) {
01856 this->x -= v.x;
01857 this->y -= v.y;
01858 this->z -= v.z;
01859 return *this;
01860 }
01861
01862 const Vector3D& Vector3D::operator+=(const Vector3D &v) {
01863 this->x += v.x;
01864 this->y += v.y;
01865 this->z += v.z;
01866 return *this;
01867 }
01868
01869 const Vector3D& Vector3D::operator-=(double a) {
01870 this->x -= a;
01871 this->y -= a;
01872 this->z -= a;
01873 return *this;
01874 }
01875
01876 const Vector3D& Vector3D::operator+=(double a) {
01877 this->x += a;
01878 this->y += a;
01879 this->z += a;
01880 return *this;
01881 }
01882
01883 const Vector3D& Vector3D::operator*=(double a) {
01884 this->x *= a;
01885 this->y *= a;
01886 this->z *= a;
01887 return *this;
01888 }
01889
01890
01891
01892 double Vector3D::getX() const { return x; }
01893 double Vector3D::getY() const { return y; }
01894 double Vector3D::getZ() const { return z; }
01895
01896 bool Vector3D::set(const PointFloat& p1, const PointFloat& p2) {
01897 return set(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z);
01898 }
01899
01900 bool Vector3D::set(double x1, double y1, double z1, double x2, double y2, double z2) {
01901 return set(x2-x1, y2-y1, z2-z1);
01902 }
01903
01904 bool Vector3D::set(double xx, double yy, double zz) { x = xx; y = yy; z = zz; return true; }
01905 bool Vector3D::setX(double n) { x = n; return true; }
01906 bool Vector3D::setY(double n) { y = n; return true; }
01907 bool Vector3D::setZ(double n) { z = n; return true; }
01908
01909 JString Vector3D::print() {
01910 return JString::format("(%.3f,%.3f,%.3f)", x, y, z);
01911 }
01912
01913 JString Vector3D::toXML() {
01914 return JString::format("<vector2d x=\"%f\" y=\"%f\" z=\"%f\" />", x, y, z);
01915 }
01916
01917 bool Vector3D::fromXML(XMLNode* node) {
01918 if (node == NULL) {
01919 return false;
01920 }
01921 if ( (!node->hasAttribute("x")) ||
01922 (!node->hasAttribute("y")) ||
01923 (!node->hasAttribute("z")))
01924 return false;
01925
01926 x = node->getAttribute("x").toDouble();
01927 y = node->getAttribute("y").toDouble();
01928 z = node->getAttribute("z").toDouble();
01929 return true;
01930 }
01931
01932 double Vector3D::length() const {
01933 return sqrt((x*x) + (y*y) + (z*z));
01934 }
01935
01936 Vector3D Vector3D::product(const Vector3D &v) const {
01937 return Vector3D(
01938 this->y*v.z - this->z*v.y,
01939 this->z*v.x - this->x*v.z,
01940 this->x*v.y - this->y*v.x);
01941 }
01942
01943 bool Vector3D::isOrthogonalWith(const Vector3D &v) const {
01944 return (*this*v == 0);
01945 }
01946
01947 bool Vector3D::isParallelWith(const Vector3D &v) const {
01948 return (product(v).length() == 0);
01949 }
01950
01951 Vector3D Vector3D::getProjectionOn(const Vector3D &v) const {
01952 Vector3D unit = v.getUnitVector();
01953 return unit*(*this*unit);
01954 }
01955
01956 Vector3D Vector3D::getUnitVector() const {
01957 return *this*(1.0/length());
01958 }
01959
01960 double Vector3D::getAngle(const Vector3D &v) const {
01961 double lengths = this->length() * v.length();
01962 if (lengths == 0)
01963 return 0;
01964 return acos((*this*v)/lengths);
01965 }
01966
01967 double Vector3D::getArea(const Vector3D &v) const {
01968 return fabs(product(v).length());
01969 }
01970
01971
01972 }