00001
00002
00003
00004
00005 #include "pGraphicalFunction.h"
00006 #include "math.h"
00007 #include <iostream>
00008 #include <fstream>
00009 #include <string>
00010 #include <vector>
00011
00012
00013
00014
00015
00016
00017 #ifndef WX_PRECOMP
00018 #include <wx/wx.h>
00019 #endif
00020
00021
00022
00023
00024
00025 IMPLEMENT_CLASS(pGraphicalFunction, pPlotterLayer)
00026
00027 pGraphicalFunction:: pGraphicalFunction(wxString name, int flags)
00028 {
00029 SetName(name);
00030 showPointsF = false;
00031 validPointRange = 5;
00032 logicFunction = new pLogicalFunction ();
00033 fromWindow=false;
00034 factorZoom=1;
00035 drawing=false;
00036 editable=true;
00037 ifActual=false;
00038 zoomIn=false;
00039 setOffsetX(0);
00040 setOffsetY(0);
00041 initialDrawingPoint=NULL;
00042 finalDrawingPoint=NULL;
00043
00044 _type=1;
00045 xKSpline=NULL;
00046 yKSpline=NULL;
00047 offsetPixelX=0;
00048 offsetPixelY=0;
00049 xTraslation=0;
00050 mType=DEFAULT;
00051 }
00052
00056 pGraphicalFunction :: ~ pGraphicalFunction ()
00057 {
00058
00059 }
00060
00061
00062 void pGraphicalFunction::SetShowPoints(bool showPoints)
00063 {
00064 showPointsF = showPoints;
00065 }
00066
00067 bool pGraphicalFunction::getShowPoints()
00068 {
00069 return showPointsF;
00070 }
00071
00072
00073
00074 void pGraphicalFunction::setUp()
00075 {
00076 logicFunction->setUp();
00077 setMaxX(logicFunction->getMaxX());
00078 setMinX(logicFunction->getMinX());
00079 setMaxY(logicFunction->getMaxY());
00080 setMinY(logicFunction->getMinY());
00081 setStartX(logicFunction->getStartX());
00082 setEndX(logicFunction->getEndX());
00083 setStartY(logicFunction->getStartY());
00084 setEndY(logicFunction->getEndY());
00085
00086 }
00087
00088
00089
00090
00091
00092 int pGraphicalFunction::validPointOnFunction(wxPoint realPoint)
00093 {
00094 return (logicFunction -> validPointOnFunction (realPoint));
00095 }
00096
00097
00098
00099 int pGraphicalFunction::getIndexOf(wxPoint realpoint)
00100 {
00101 return logicFunction -> getIndexOf( realpoint );
00102 }
00103
00104
00105
00106
00107
00108
00109 wxNode* pGraphicalFunction::GetPointAt( int movingPointIndex )
00110 {
00111 return logicFunction -> GetPointAt ( movingPointIndex );
00112 }
00113
00114
00115
00116
00117 void pGraphicalFunction::setScales()
00118 {
00119
00120
00121 int dx= maxShowedX-minShowedX;
00122 int dy= maxShowedY-minShowedY;
00123
00124
00125 if(dx!=0 && dy!=0)
00126 {
00127 double scaleX=(((float)(screenX-100))/dx)*factorZoom;
00128 double scaleY=(((float)(screenY-50))/dy)*factorZoom;
00129 setScaleX(scaleX);
00130 setScaleY(scaleY);
00131 logicFunction->setScaleX(scaleX);
00132 logicFunction->setScaleY(scaleY);
00133
00134 }
00135 }
00136
00137
00138
00139
00140 bool pGraphicalFunction:: AddNewPoint(int x,int y)
00141 {
00142 bool added= logicFunction -> AddNewPoint ( x, y );
00143 if(!fromWindow)
00144 setUp();
00145 else
00146 {
00147 logicFunction->setEndPoints();
00148 logicFunction->setStartPoints();
00149 }
00150 return added;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160 bool pGraphicalFunction::AddPoint(int aX, int aY,bool order)
00161 {
00162 bool added=false;
00163 if (logicFunction!=NULL){
00164 added=logicFunction -> AddPoint( aX, aY,order );
00165 if(!fromWindow)
00166 setUp();
00167 else
00168 {
00169 logicFunction->setEndPoints();
00170 logicFunction->setStartPoints();
00171 }
00172 }
00173 return added;
00174 }
00175
00181 bool pGraphicalFunction::DeletePoint(int aX, int aY)
00182 {
00183 bool added= logicFunction -> DeletePoint( aX, aY );
00184 if(!fromWindow)
00185 setUp();
00186 else
00187 {
00188 logicFunction->setEndPoints();
00189 logicFunction->setStartPoints();
00190 }
00191 return added;
00192 }
00193
00197 bool pGraphicalFunction::deletePointAt(int index)
00198 {
00199 bool added=logicFunction -> deletePointAt( index );
00200 if(!fromWindow)
00201 setUp();
00202 else
00203 {
00204 logicFunction->setEndPoints();
00205 logicFunction->setStartPoints();
00206 }
00207 return added;
00208 }
00209
00214 bool pGraphicalFunction::changePoint(wxPoint newCoords, int movingIndexPoint)
00215 {
00216 bool added= (logicFunction -> changePoint( newCoords, movingIndexPoint ));
00217 if(!fromWindow)
00218 setUp();
00219 else
00220 {
00221 logicFunction->setEndPoints();
00222 logicFunction->setStartPoints();
00223 }
00224 return added;
00225 }
00229
00230
00231
00232
00233
00234
00235
00239 double* pGraphicalFunction::getX_RealValues()
00240 {
00241 return (logicFunction -> getX_RealValues( ));
00242 }
00243
00247 double * pGraphicalFunction::getY_RealValues()
00248 {
00249
00250 return (logicFunction -> getY_RealValues());
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 bool pGraphicalFunction:: isInFunction(int x, int y)
00262 {
00263 wxNode* before;
00264 wxNode* next;
00265 pFunctionPoint* point= new pFunctionPoint(x,y);
00266 bool inLine;
00267 before=getBefore(point);
00268
00269 if(before && before->GetNext())
00270 {
00271 next = before->GetNext();
00272
00273 inLine= isInLine((pFunctionPoint*)before->GetData(),(pFunctionPoint*)next->GetData(),point);
00274 }
00275 else
00276 inLine=false;
00277 return inLine;
00278 }
00279
00280
00281
00282
00283 wxNode* pGraphicalFunction:: getBefore(pFunctionPoint* point)
00284 {
00285 int minDiference=10000000;
00286 wxNode* node=logicFunction->GetPointAt(0);
00287 wxNode* before=NULL;
00288 while(node)
00289 {
00290 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
00291 int beforeX=before1->getRealX();
00292 int pointX=point->getRealX();
00293 if(beforeX<pointX)
00294 {
00295 int minDiference1=pointX-beforeX;
00296 if(minDiference1<minDiference)
00297 {
00298 minDiference=minDiference1;
00299 before=node;
00300 }
00301 }
00302 node=node->GetNext();
00303 }
00304 return before;
00305 }
00306
00307
00308
00309 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
00310 {
00311 int x1,x2,x,y1,y2,y;
00312
00313 x1=(float)before->getRealX();
00314 x2=(float)next->getRealX();
00315 y1=(float)before->getRealY();
00316 y2=(float)next->getRealY();
00317 x=(float)point->getRealX();
00318 y=(float)point->getRealY();
00319
00320
00321 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
00322 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
00323 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
00324 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
00325 return true;
00326 return false;
00327 }
00331 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
00332 {
00333 return m*(x-x1)+y1;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 void pGraphicalFunction:: clearSplineVectors()
00349 {
00350 xSpline.clear();
00351 ySpline.clear();
00352 }
00353
00354
00355
00356
00357 void pGraphicalFunction:: initializeSplines()
00358 {
00359 if(xKSpline==NULL)
00360 xKSpline = vtkKochanekSpline::New();
00361 if(yKSpline==NULL)
00362 yKSpline= vtkKochanekSpline::New();
00363 addSplinesPoints();
00364
00365 }
00366
00367
00368
00369
00370
00371 void pGraphicalFunction::addSplinesPoints()
00372 {
00373 xKSpline->RemoveAllPoints();
00374 yKSpline->RemoveAllPoints();
00375 wxNode *node= logicFunction->GetPointAt(0);
00376 pFunctionPoint* p;
00377 int i=0;
00378 while(node)
00379 {
00380 p=(pFunctionPoint*)node->GetData();
00381 xKSpline->AddPoint(i,p->getRealX());
00382 yKSpline->AddPoint(i,p->getRealY());
00383 i++;
00384 node=node->GetNext();
00385 }
00386 }
00387
00388
00389
00390
00391 void pGraphicalFunction:: initializeSplineVectors()
00392 {
00393 double x=0,y=0,t,delta;
00394 int i,np,nps;
00395
00396
00397 np = logicFunction->getSizePoints();
00398 nps = 100;
00399 delta=( double ) ( np ) / ( double ) ( nps );
00400 for( i = 0; i < nps; i++ )
00401 {
00402 t = delta * (double)i;
00403 GetSplinePoint(t,x,y);
00404 xSpline.push_back(x);
00405 ySpline.push_back(y);
00406 }
00407 }
00408
00409
00410
00411 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
00412 {
00413 if (logicFunction->getSizePoints()==0)
00414 {
00415 x = 0;
00416 y = 0;
00417 }
00418 if (logicFunction->getSizePoints()>=2)
00419 {
00420 x = xKSpline->Evaluate(t);
00421 y = yKSpline->Evaluate(t);
00422 }
00423 }
00424
00425
00426
00427
00428
00439 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
00440 {
00441 int xS,yS,x1,x2,y1,y2;
00442 setScales();
00443 xS=clickedX/_scaleX+_offsetX;
00444 yS=clickedY/_scaleY+_offsetY;
00445
00446 x1=(clickedX-width)/_scaleX+_offsetX;
00447 x2=(clickedX+width)/_scaleX+_offsetX;
00448 y1=(clickedY-width)/_scaleY+_offsetY;
00449 y2=(clickedY+width)/_scaleY+_offsetY;
00450
00451 int numberPoints=getSizePoints();
00452 pFunctionPoint* point;
00453 wxNode* p;
00454 bool inSquare=false,first=false;
00455
00456
00457 if(zoomIn)
00458 {
00459 int i;
00460 for(i=0; i<numberPoints;i++)
00461 {
00462 p=logicFunction->GetPointAt(i);
00463 point=(pFunctionPoint*)p->GetData();
00464 int rx=point->getRealX();
00465 int ry=point->getRealY();
00466 inSquare= rx>=x1 && rx<=x2;
00467 if(inSquare)
00468 {
00469 if(!first)
00470 {
00471 initialDrawingPoint=point;
00472 _offsetX=rx;
00473 _offsetY=ry;
00474 first=true;
00475 }
00476 finalDrawingPoint=point;
00477 }
00478 }
00479 }
00480 else
00481 {
00482
00483 if((_offsetX-20)>=0)
00484 {
00485 _offsetX=_offsetX-20;
00486 }
00487 else
00488 _offsetX=0;
00489 if((_offsetY-10)>=0)
00490 {
00491 _offsetY=_offsetY-10;
00492 }
00493 else
00494 _offsetY=0;
00495 }
00496 }
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507 void pGraphicalFunction::setOffsets(int offx,int offy)
00508 {
00509 int xS,yS;
00510
00511 setScales();
00512
00513 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
00514 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
00515
00516 setOffsetX(xS);
00517 setOffsetY(yS);
00518
00519
00520
00521
00522 setOffsetPixelX(offx);
00523 setOffsetPixelY(offy);
00524
00525 }
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535 void pGraphicalFunction::setMinShowed()
00536 {
00537 bool firstX=false,firstY=false;
00538
00539 wxNode* node=logicFunction->GetPointAt(0);
00540 pFunctionPoint* p;
00541
00542
00543
00544
00545
00546 while(node)
00547 {
00548 p=(pFunctionPoint*)node->GetData();
00549 int px=p->getRealX();
00550 int py=p->getRealY();
00551 int x=(px-_offsetX);
00552 int y=(py-_offsetY);
00553 if(!firstX && x>=0)
00554 {
00555 firstX=true;
00556 setMinShowedX(px);
00557 }
00558 if(!firstY && y>=0)
00559 {
00560 firstY=true;
00561 setMinShowedY(py);
00562 }
00563 node=node->GetNext();
00564 }
00565 }
00566
00567
00568
00569
00570
00571
00572
00573
00574 void pGraphicalFunction::save(wxString fileName)
00575 {
00576
00577 logicFunction->save(fileName);
00578
00579 }
00580
00581
00582
00583 void pGraphicalFunction::load(wxString fileName)
00584 {
00585 logicFunction->load(fileName);
00586 logicFunction->setUp();
00587 setMaxShowedX(logicFunction->getMaxX());
00588 setMinShowedX(logicFunction->getMinX());
00589 setMaxShowedY(logicFunction->getMaxY());
00590 setMinShowedY(logicFunction->getMinY());
00591 }
00592
00593
00594
00595
00599 void pGraphicalFunction::setActual(bool act)
00600 {
00601 ifActual=act;
00602 }
00603 bool pGraphicalFunction:: getActual()
00604 {
00605 return ifActual;
00606 }
00613 void pGraphicalFunction::setScaleWay(int typeS)
00614 {
00615 scaleWay=typeS;
00616 }
00617
00618 int pGraphicalFunction:: getScaleWay()
00619 {
00620 return scaleWay;
00621 }
00622
00623
00624
00625
00626
00627 void pGraphicalFunction::setScreenX(int scrX)
00628 {
00629 this->screenX=scrX;
00630 }
00631 int pGraphicalFunction::getScreenX()
00632 {
00633 return this->screenX;
00634 }
00635 void pGraphicalFunction::setScreenY(int scrY)
00636 {
00637 this->screenY=scrY;
00638 }
00639 int pGraphicalFunction::getScreenY()
00640 {
00641 return this->screenY;
00642 }
00643
00644
00645
00646
00647
00648 void pGraphicalFunction::setStartX(double aStartX) {
00649
00650 logicFunction->setStartX(aStartX);
00651 this->_startX =logicFunction->getStartX();
00652 }
00653
00654 double pGraphicalFunction::getStartX()
00655 {
00656 return logicFunction->getStartX();
00657 }
00658
00659 void pGraphicalFunction::setStartY(double aStartY)
00660 {
00661 logicFunction->setStartY(aStartY);
00662 this->_startY = aStartY;
00663 }
00664
00665 double pGraphicalFunction::getStartY() {
00666 return logicFunction->getStartY();
00667 }
00668
00669
00670
00671 void pGraphicalFunction::setEndX(double aEndX)
00672 {
00673 logicFunction->setEndX(aEndX);
00674 this->_endX = aEndX;
00675 }
00676
00677 double pGraphicalFunction::getEndX() {
00678 return logicFunction->getEndX();
00679 }
00680
00681 void pGraphicalFunction::setEndY(double aEndY) {
00682 logicFunction->setEndY(aEndY);
00683 this->_endY = aEndY;
00684 }
00685
00686 double pGraphicalFunction::getEndY() {
00687 return logicFunction->getEndY();
00688 }
00689
00690
00691
00692 void pGraphicalFunction::setScaleX(double aScaleX)
00693 {
00694 logicFunction->setScaleX(aScaleX);
00695 this->_scaleX = aScaleX;
00696 }
00697
00698 double pGraphicalFunction::getScaleX()
00699 {
00700 return this->_scaleX;
00701 }
00702
00703 void pGraphicalFunction::setScaleY(double aScaleY)
00704 {
00705 logicFunction->setScaleY(aScaleY);
00706 this->_scaleY = aScaleY;
00707 }
00708
00709 double pGraphicalFunction::getScaleY() {
00710 return this->_scaleY;
00711 }
00712
00713
00714
00715 void pGraphicalFunction::setMinX(double aMinX)
00716 {
00717 logicFunction->setMinX(aMinX);
00718 this->_minX = aMinX;
00719 }
00720
00721 double pGraphicalFunction::getMinX()
00722 {
00723 return logicFunction->getMinX();
00724 }
00725
00726 void pGraphicalFunction::setMinY(double aMinY)
00727 {
00728 logicFunction->setMinY(aMinY);
00729 this->_minY = aMinY;
00730 }
00731
00732 double pGraphicalFunction::getMinY() {
00733 return logicFunction->getMinY();
00734 }
00735
00736
00737
00738 void pGraphicalFunction::setMaxX(double aMaxX)
00739 {
00740 logicFunction->setMaxX(aMaxX);
00741 this->_maxX = aMaxX;
00742 }
00743
00744 double pGraphicalFunction::getMaxX()
00745 {
00746 return logicFunction->getMaxX();
00747 }
00748
00749 void pGraphicalFunction::setMaxY(double aMaxY)
00750 {
00751 logicFunction->setMaxY(aMaxY);
00752 this->_maxY = aMaxY;
00753 }
00754
00755 double pGraphicalFunction::getMaxY() {
00756 return logicFunction->getMaxY();
00757 }
00758
00759
00760
00761
00762 void pGraphicalFunction::setOffsetX(double aOffsetX)
00763 {
00764 logicFunction->setOffsetX(aOffsetX);
00765 this->_offsetX = aOffsetX;
00766 }
00767
00768 double pGraphicalFunction:: getOffsetX()
00769 {
00770
00771 return this->_offsetX;
00772 }
00773
00774 void pGraphicalFunction:: setOffsetY(double aOffsetY)
00775 {
00776 logicFunction->setOffsetY(aOffsetY);
00777 this->_offsetY = aOffsetY;
00778 }
00779
00780 double pGraphicalFunction:: getOffsetY() {
00781 return this->_offsetY;
00782 }
00783
00784
00785
00786
00787 void pGraphicalFunction:: setType(int aType)
00788 {
00789 logicFunction->setType(aType);
00790 this->_type = aType;
00791 }
00792
00793 int pGraphicalFunction :: getType() {
00794 return this->_type;
00795 }
00796
00797
00798
00799
00800 int pGraphicalFunction :: getValidPointRange()
00801 {
00802 return validPointRange;
00803 }
00804
00805 void pGraphicalFunction :: setValidPointRange(int theRange)
00806 {
00807 logicFunction->setValidPointRange(theRange);
00808 validPointRange = theRange;
00809 }
00810
00811
00812
00813
00814 int pGraphicalFunction :: getSizePoints()
00815 {
00816 return logicFunction->getSizePoints();
00817 }
00818
00819
00820
00821
00822
00823 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
00824 {
00825 f_colorPoints.clear();
00826 int i = 0;
00827 while(i<(int)(colorVector.size()))
00828 {
00829 f_colorPoints.push_back(colorVector[i]);
00830 i++;
00831 }
00832 }
00833
00834
00835
00836
00837
00838 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
00839 {
00840 int i = 0;
00841 while(i<(int)(f_colorPoints.size()))
00842 {
00843 pColorPoint * originaslPoint = f_colorPoints[i];
00844 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
00845 colorVector.push_back(copyPoint);
00846 i++;
00847 }
00848 }
00849
00850