pLogicalFunction.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005 #include "pLogicalFunction.h"
00006 #include <iostream>
00007 #include <fstream>
00008 #include <string>
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef WX_PRECOMP
00017 #include <wx/wx.h>
00018 #endif
00019
00020
00021
00022
00023
00024 IMPLEMENT_CLASS(pLogicalFunction, wxObject)
00025
00026
00027
00028
00029 pLogicalFunction:: pLogicalFunction( )
00030 {
00031
00032 realPoints.DeleteContents(TRUE);
00033 validPointRange = 5;
00034 leftToRigthDef = true;
00035 _maxX=0;
00036 _maxY=0;
00037 _minX=0;
00038 _minY=0;
00039 }
00043 pLogicalFunction:: ~pLogicalFunction ()
00044 {
00045 realPoints.DeleteContents(TRUE);
00046 }
00047
00048
00049
00050
00051 int pLogicalFunction::validPointOnFunction(wxPoint realPoint)
00052 {
00053 int pointIndex = -1;
00054 wxNode* node= realPoints.GetFirst();
00055 while(node && pointIndex==-1)
00056 {
00057 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
00058 int x = point->getRealX();
00059 int y = point->getRealY();
00060 double vx=SENSIBLE_REGION/_scaleX;
00061 double vy=SENSIBLE_REGION/_scaleY;
00062
00063 bool isInXRange= realPoint.x <= x + vx + SENSIBLE_REGION && realPoint.x >= x - vx-SENSIBLE_REGION;
00064 bool isInYRange= realPoint.y <= y + vy + SENSIBLE_REGION && realPoint.y >= y - vy-SENSIBLE_REGION;
00065 if(isInXRange && isInYRange)
00066 pointIndex = realPoints.IndexOf(point);
00067
00068 node = node->GetNext();
00069 }
00070 return pointIndex;
00071 }
00072
00073
00074
00075
00076
00077
00078 int pLogicalFunction::getIndexOf(wxPoint realpoint)
00079 {
00080 wxNode* node= realPoints.GetFirst();
00081 while(node)
00082 {
00083 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
00084 if(point->getRealX()==realpoint.x && point->getRealY()==realpoint.y )
00085 {
00086 return realPoints.IndexOf(point);
00087 }
00088
00089 node= node->GetNext();
00090 }
00091 return -1;
00092
00093 }
00094
00095
00096
00097
00098
00099
00100 wxNode* pLogicalFunction::GetPointAt( int movingPointIndex )
00101 {
00102 if(!realPoints.IsEmpty())
00103 {
00104 wxNode* node= realPoints.Item(movingPointIndex);
00105 return node;
00106 }
00107 else
00108 return NULL;
00109 }
00110
00111
00112
00113
00114
00115 bool pLogicalFunction:: AddNewPoint(int x,int y)
00116 {
00117 pFunctionPoint * newPoint = new pFunctionPoint ( x, y);
00118 bool includedPoint = realPoints.Append(newPoint)!=NULL;
00119 if(includedPoint)
00120 {
00121 bool order=true;
00122 if(realPoints.size()>1){
00123 order=orderPoints();
00124 }
00125
00126 return order;
00127 }
00128 return includedPoint;
00129 }
00130
00131
00132
00133
00134 bool pLogicalFunction::orderPoints()
00135 {
00136 bool lastOrdered = false;
00137 bool validToContinue = false;
00138
00139 wxNode* lastNodeIncluded = realPoints.GetLast();
00140
00141 wxNode* node = realPoints.GetFirst();
00142 pFunctionPoint* lastPointIncluded = (pFunctionPoint*)lastNodeIncluded -> GetData();
00143 int xToOrder = lastPointIncluded -> getRealX();
00144 int actualX;
00145 int nextX;
00146
00147
00148 pFunctionPoint* ordFirstPoint = NULL;
00149 pFunctionPoint* ordLastPoint = NULL;
00150 if(node!=NULL){
00151 ordFirstPoint = (pFunctionPoint*)realPoints.GetFirst()-> GetData();
00152 }
00153 if(lastNodeIncluded->GetPrevious()!=NULL){
00154 ordLastPoint = (pFunctionPoint*)(lastNodeIncluded->GetPrevious())-> GetData();
00155 }
00156
00157
00158
00159 if (leftToRigthDef)
00160 {
00161 validToContinue = ordFirstPoint->getRealX() < xToOrder;
00162 }
00163 else
00164 {
00165
00166 validToContinue =ordLastPoint->getRealX() > xToOrder;
00167 }
00168
00169 if ( validToContinue)
00170 {
00171
00172 while( node && !lastOrdered )
00173 {
00174 pFunctionPoint* prevPoint =(pFunctionPoint*)node->GetData();
00175 actualX = prevPoint ->getRealX();
00176 if( actualX == xToOrder)
00177 {
00178 realPoints.DeleteNode(lastNodeIncluded);
00179 lastOrdered = false;
00180 }
00181 else if ( actualX < xToOrder )
00182 {
00183
00184 wxNode* nextNode = node->GetNext();
00185 pFunctionPoint* nextPoint;
00186 if( nextNode != lastNodeIncluded )
00187 {
00188 nextPoint = (pFunctionPoint*)nextNode -> GetData();
00189 nextX = nextPoint->getRealX();
00190 int nextIndex = realPoints.IndexOf(nextPoint);
00191 if( nextX > xToOrder )
00192 {
00193
00194 pFunctionPoint* pointToInsert=new pFunctionPoint(lastPointIncluded->getRealX(),lastPointIncluded->getRealY());
00195
00196 lastOrdered = (realPoints.Insert(nextIndex, pointToInsert))!= NULL;
00197 bool lastNodeDeleted=realPoints.DeleteNode(lastNodeIncluded);
00198 return lastOrdered && lastNodeDeleted;
00199
00200 }
00201
00202 }
00203 else
00204 {
00205 lastOrdered = true;
00206 }
00207 }
00208 else
00209 {
00210
00211 wxNode* prevNode = node->GetPrevious();
00212 int insertIndex=realPoints.IndexOf(prevPoint);
00213 pFunctionPoint* prPoint;
00214 if(prevNode)
00215 {
00216 prPoint = (pFunctionPoint*)prevNode -> GetData();
00217 int beforeX =prPoint->getRealX();
00218 if( beforeX < xToOrder)
00219 {
00220
00221 pFunctionPoint* pointToInsert=new pFunctionPoint(lastPointIncluded->getRealX(),lastPointIncluded->getRealY());
00222
00223 lastOrdered = (realPoints.Insert(insertIndex, pointToInsert))!= NULL;
00224 bool lastNodeDeleted=realPoints.DeleteNode(lastNodeIncluded);
00225 return lastOrdered && lastNodeDeleted;
00226 }
00227 }
00228
00229 else
00230 {
00231
00232 pFunctionPoint* pointToInsert=new pFunctionPoint(lastPointIncluded->getRealX(),lastPointIncluded->getRealY());
00233
00234 lastOrdered = (realPoints.Insert(insertIndex, pointToInsert))!= NULL;
00235 bool lastNodeDeleted=realPoints.DeleteNode(lastNodeIncluded);
00236 return lastOrdered && lastNodeDeleted;
00237 }
00238 }
00239
00240 node = node->GetNext();
00241 }
00242
00243 }
00244 else
00245 {
00246 bool lastNodeDeleted = realPoints.DeleteNode(lastNodeIncluded);
00247 lastOrdered = lastOrdered && lastNodeDeleted;
00248 }
00249 return lastOrdered;
00250 }
00251
00252
00253
00254
00255
00256
00257
00258 bool pLogicalFunction::AddPoint(int aX, int aY,bool order)
00259 {
00260 pFunctionPoint * newPoint = new pFunctionPoint (aX, aY);
00261 wxNode* lastNode = realPoints.GetLast();
00262 bool addedPoint = false;
00263 if (lastNode)
00264 {
00265 pFunctionPoint* lastPoint = (pFunctionPoint*)lastNode->GetData();
00266 if( lastPoint->getRealX() != aX )
00267 {
00268 addedPoint = realPoints.Append(newPoint)!=NULL;
00269
00270 if( (aX < (lastPoint->getRealX()) ) && addedPoint )
00271 {
00272 if( realPoints.GetCount() == 2 )
00273 {
00274 leftToRigthDef = false;
00275 }
00276 if(order)
00277 addedPoint = orderPoints();
00278
00279 }
00280 else
00281 {
00282 if( realPoints.GetCount() == 2 )
00283 {
00284 leftToRigthDef = true;
00285 }
00286 }
00287 }
00288 }
00289 else
00290 {
00291 addedPoint = realPoints.Append(newPoint)!=NULL;
00292 }
00293
00294 return addedPoint;
00295 }
00296
00297
00298
00299
00300 void pLogicalFunction::setUp()
00301 {
00302
00303 setStartPoints();
00304
00305 setEndPoints();
00306
00307 setMinPoints();
00308
00309 setMaxPoints();
00310 }
00311
00312
00313
00314
00315
00316 void pLogicalFunction:: setStartPoints()
00317 {
00318 wxNode* first=realPoints.GetFirst();
00319 if(first)
00320 {
00321 pFunctionPoint* startPoint=(pFunctionPoint*)first->GetData();
00322 setStartX(startPoint->getRealX());
00323 setStartY(startPoint->getRealY());
00324 }
00325 else
00326 {
00327 setStartX(-1);
00328 setStartY(-1);
00329 }
00330 }
00331
00332
00333
00334 void pLogicalFunction:: setEndPoints()
00335 {
00336 wxNode* last=realPoints.GetLast();
00337 if(last)
00338 {
00339 pFunctionPoint* lastPoint=(pFunctionPoint*)last->GetData();
00340 setEndX(lastPoint->getRealX());
00341 setEndY(lastPoint->getRealY());
00342 }
00343 else
00344 {
00345 setEndX(-1);
00346 setEndY(-1);
00347
00348 }
00349 }
00350
00351
00352
00353
00354 void pLogicalFunction::setMinPoints()
00355 {
00356 int minX,minY;
00357 wxNode* node=realPoints.GetFirst();
00358 if(node)
00359 {
00360 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
00361 minX=point->getRealX();
00362 minY=point->getRealY();
00363 wxNode* nextNode=node->GetNext();
00364 while(nextNode)
00365 {
00366 point=(pFunctionPoint*)nextNode->GetData();
00367 int x=point->getRealX();
00368 int y=point->getRealY();
00369 if(x<minX)
00370 minX=x;
00371 if(y<minY)
00372 minY=y;
00373 nextNode=nextNode->GetNext();
00374 }
00375 setMinX(minX);
00376 setMinY(minY);
00377 }
00378 else
00379 {
00380 setMinX(0);
00381 setMinY(0);
00382 }
00383
00384 }
00385
00386
00387
00388 void pLogicalFunction::setMaxPoints()
00389 {
00390 int maxX,maxY;
00391 wxNode* node=realPoints.GetFirst();
00392 if(node)
00393 {
00394 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
00395 maxX=point->getRealX();
00396 maxY=point->getRealY();
00397 wxNode* nextNode=node->GetNext();
00398 while(nextNode)
00399 {
00400 point=(pFunctionPoint*)nextNode->GetData();
00401 int x=point->getRealX();
00402 int y=point->getRealY();
00403 if(x>maxX)
00404 maxX=x;
00405 if(y>maxY)
00406 maxY=y;
00407 nextNode=nextNode->GetNext();
00408 }
00409 setMaxX(maxX);
00410 setMaxY(maxY);
00411 }
00412 else
00413 {
00414 setMaxX(0);
00415 setMaxY(0);
00416 }
00417 }
00418
00424 bool pLogicalFunction::DeletePoint(int aX, int aY)
00425 {
00426 wxNode* node= realPoints.GetFirst();
00427 while(node)
00428 {
00429 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
00430 if(point->getRealX()==aX && point->getRealY()==aY)
00431 {
00432 realPoints.Erase(node);
00433 return true;
00434 }
00435 node= node->GetNext();
00436 }
00437
00438 return false;
00439 }
00440
00444 bool pLogicalFunction::deletePointAt(int index)
00445 {
00446 if(index!=-1)
00447 {
00448 wxNode* node=GetPointAt(index);
00449
00450 if(node)
00451 {
00452
00453 bool deleted=realPoints.DeleteNode(node);
00454
00455
00456
00457
00458
00459
00460 return deleted;
00461 }
00462 }
00463 return false;
00464 }
00465
00470 bool pLogicalFunction::changePoint(wxPoint newCoords, int movingIndexPoint)
00471 {
00472 wxNode* changingNode = GetPointAt(movingIndexPoint);
00473 bool validChange = false;
00474 int newX = newCoords.x;
00475 int newY = newCoords.y;
00476 pFunctionPoint* changingPoint = (pFunctionPoint*)changingNode -> GetData();
00477 bool hasPrevious = movingIndexPoint>0;
00478 bool hasNext = movingIndexPoint < realPoints.size()-1;
00479
00480 wxNode* prevNode = hasPrevious ? changingNode ->GetPrevious() : NULL;
00481 wxNode* nextNode = hasNext ? changingNode -> GetNext() : NULL;
00482
00483 if( hasPrevious )
00484 {
00485 pFunctionPoint* prevPoint = (pFunctionPoint*)prevNode -> GetData();
00486 if ( hasNext )
00487 {
00488 pFunctionPoint* nextPoint = (pFunctionPoint*) nextNode -> GetData();
00489 validChange = ((prevPoint->getRealX()) < newX) && ((nextPoint->getRealX()) > newX);
00490 if ( (prevPoint->getRealX()) > newX )
00491 {
00492 newX = prevPoint->getRealX()+1;
00493 validChange = true;
00494 }
00495 else if ( (nextPoint->getRealX()) < newX )
00496 {
00497 newX = nextPoint->getRealX()-1;
00498 validChange = true;
00499 }
00500 }
00501 else
00502 {
00503
00504 if ( (prevPoint->getRealX()) < newX )
00505 {
00506 validChange = true;
00507 }
00508 else
00509 {
00510 newX = prevPoint->getRealX();
00511 validChange = true;
00512 }
00513 }
00514 }
00515 else
00516 {
00517 if ( hasNext )
00518 {
00519
00520 pFunctionPoint* nextPoint = (pFunctionPoint*) nextNode -> GetData();
00521 if ((nextPoint->getRealX()) > newX )
00522 {
00523 validChange = true;
00524 }
00525 else
00526 {
00527 newX = nextPoint->getRealX();
00528 validChange = true;
00529 }
00530 }
00531 }
00532 if( validChange )
00533 {
00534 changingPoint -> setRealX( newX );
00535 changingPoint -> setRealY( newY );
00536 }
00537 return validChange;
00538 }
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00563 double* pLogicalFunction::getX_RealValues()
00564 {
00565 wxNode *nextNode = realPoints.GetFirst();
00566 int size = realPoints.GetCount();
00567 double * x_Values;
00568 int i=0;
00569
00570 x_Values= new double[size];
00571
00572 while (nextNode)
00573 {
00574 pFunctionPoint* nextPoint = (pFunctionPoint*)nextNode->GetData();
00575 x_Values[i] = nextPoint-> getRealX();
00576 nextNode = nextNode->GetNext();
00577 i++;
00578 }
00579 return x_Values;
00580 }
00581
00585 double * pLogicalFunction::getY_RealValues()
00586 {
00587 wxNode *nextNode = realPoints.GetFirst();
00588 int i =0;
00589 int size = realPoints.GetCount();
00590 double * y_Values;
00591
00592 y_Values= new double[size];
00593
00594 while (nextNode)
00595 {
00596 pFunctionPoint* nextPoint = (pFunctionPoint*)nextNode->GetData();
00597 y_Values[i] = nextPoint-> getRealY();
00598 nextNode = nextNode->GetNext();
00599 i++;
00600 }
00601 return y_Values;
00602 }
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615 void pLogicalFunction::save(wxString fileName)
00616 {
00617
00618 std::ofstream file;
00619 file.open( (const char*)(fileName.mb_str()) );
00620 if(file.is_open())
00621 {
00622 file << getSizePoints()<< std::endl;
00623 wxNode* node= realPoints.GetFirst();
00624 pFunctionPoint* p;
00625 while(node)
00626 {
00627 p=(pFunctionPoint*)node->GetData();
00628 file <<p->getRealX()<<"\t"<<p->getRealY()<<std::endl;
00629 node=node->GetNext();
00630 }
00631 }
00632 file.close();
00633 }
00634
00635
00636
00637 void pLogicalFunction::load(wxString fileName)
00638 {
00639 std::string line;
00640 std::ifstream file;
00641 file.open( (const char*)(fileName.mb_str()) );
00642
00643 if(file.is_open())
00644 {
00645 std::getline(file,line);
00646 int nPoints=atoi(line.c_str());
00647 int i=0;
00648 while(!file.eof() && i<nPoints)
00649 {
00650 std::getline(file,line);
00651 int pos=line.find("\t");
00652 int size=line.size();
00653 std::string x=line.substr(0,pos);
00654 std::string y=line.substr(pos+1,size);
00655 int x0=atoi(x.c_str());
00656 int y0=atoi(y.c_str());
00657 AddPoint(x0,y0);
00658 i++;
00659
00660 }
00661 }
00662 }
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 void pLogicalFunction::setStartX(double aStartX) {
00673 this->_startX = aStartX;
00674 }
00675
00676 double pLogicalFunction::getStartX() {
00677 return this->_startX;
00678 }
00679
00680 void pLogicalFunction::setStartY(double aStartY) {
00681 this->_startY = aStartY;
00682 }
00683
00684 double pLogicalFunction::getStartY() {
00685 return this->_startY;
00686 }
00687
00688
00689
00690
00691 void pLogicalFunction::setEndX(double aEndX) {
00692 this->_endX = aEndX;
00693 }
00694
00695 double pLogicalFunction::getEndX() {
00696 return this->_endX;
00697 }
00698
00699 void pLogicalFunction::setEndY(double aEndY) {
00700 this->_endY = aEndY;
00701 }
00702
00703 double pLogicalFunction::getEndY() {
00704 return this->_endY;
00705 }
00706
00707
00708
00709 void pLogicalFunction::setScaleX(double aScaleX) {
00710 this->_scaleX = aScaleX;
00711 }
00712
00713 double pLogicalFunction::getScaleX() {
00714 return this->_scaleX;
00715 }
00716
00717 void pLogicalFunction::setScaleY(double aScaleY) {
00718 this->_scaleY = aScaleY;
00719 }
00720
00721 double pLogicalFunction::getScaleY() {
00722 return this->_scaleY;
00723 }
00724
00725
00726
00727 void pLogicalFunction::setMinX(double aMinX) {
00728 this->_minX = aMinX;
00729 }
00730
00731 double pLogicalFunction::getMinX() {
00732 return this->_minX;
00733 }
00734
00735 void pLogicalFunction::setMinY(double aMinY) {
00736 this->_minY = aMinY;
00737 }
00738
00739 double pLogicalFunction::getMinY() {
00740 return this->_minY;
00741 }
00742
00743
00744
00745 void pLogicalFunction::setMaxX(double aMaxX) {
00746 this->_maxX = aMaxX;
00747 }
00748
00749 double pLogicalFunction::getMaxX() {
00750 return this->_maxX;
00751 }
00752
00753 void pLogicalFunction::setMaxY(double aMaxY) {
00754 this->_maxY = aMaxY;
00755 }
00756
00757 double pLogicalFunction::getMaxY() {
00758 return this->_maxY;
00759 }
00760
00761
00762
00763
00764 void pLogicalFunction::setOffsetX(double aOffsetX) {
00765 this->_offsetX = aOffsetX;
00766 }
00767
00768 double pLogicalFunction:: getOffsetX() {
00769 return this->_offsetX;
00770 }
00771
00772 void pLogicalFunction:: setOffsetY(double aOffsetY) {
00773 this->_offsetY = aOffsetY;
00774 }
00775
00776 double pLogicalFunction:: getOffsetY() {
00777 return this->_offsetY;
00778 }
00779
00780
00781
00782
00783 void pLogicalFunction:: setType(int aType) {
00784 this->_type = aType;
00785 }
00786
00787 int pLogicalFunction :: getType() {
00788 return this->_type;
00789 }
00790
00791
00792
00793
00794 int pLogicalFunction :: getValidPointRange()
00795 {
00796 return validPointRange;
00797 }
00798
00799 void pLogicalFunction :: setValidPointRange(int theRange)
00800 {
00801 validPointRange = theRange;
00802 }
00803
00804
00805
00806
00807 int pLogicalFunction::getSizePoints()
00808 {
00809 return realPoints.GetCount();
00810 }
00811 void pLogicalFunction::getDirection(bool &dir)
00812 {
00813 dir = leftToRigthDef;
00814 }
00815
00816