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
00289 while(node)
00290 {
00291 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
00292 int beforeX=before1->getRealX();
00293 int pointX=point->getRealX();
00294 if(beforeX<pointX)
00295 {
00296 int minDiference1=pointX-beforeX;
00297 if(minDiference1<minDiference)
00298 {
00299 minDiference=minDiference1;
00300 before=node;
00301 }
00302 }
00303 node=node->GetNext();
00304 }
00305 return before;
00306 }
00307
00308
00309
00310 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
00311 {
00312 int x1,x2,x,y1,y2,y;
00313
00314 x1=(float)before->getRealX();
00315 x2=(float)next->getRealX();
00316 y1=(float)before->getRealY();
00317 y2=(float)next->getRealY();
00318 x=(float)point->getRealX();
00319 y=(float)point->getRealY();
00320
00321
00322 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
00323 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
00324 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
00325 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
00326 return true;
00327 return false;
00328 }
00332 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
00333 {
00334 return m*(x-x1)+y1;
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 void pGraphicalFunction:: clearSplineVectors()
00350 {
00351 xSpline.clear();
00352 ySpline.clear();
00353 }
00354
00355
00356
00357
00358 void pGraphicalFunction:: initializeSplines()
00359 {
00360 if(xKSpline==NULL)
00361 xKSpline = vtkKochanekSpline::New();
00362 if(yKSpline==NULL)
00363 yKSpline= vtkKochanekSpline::New();
00364 addSplinesPoints();
00365
00366 }
00367
00368
00369
00370
00371
00372 void pGraphicalFunction::addSplinesPoints()
00373 {
00374 xKSpline->RemoveAllPoints();
00375 yKSpline->RemoveAllPoints();
00376 wxNode *node= logicFunction->GetPointAt(0);
00377 pFunctionPoint* p;
00378 int i=0;
00379 while(node)
00380 {
00381 p=(pFunctionPoint*)node->GetData();
00382 xKSpline->AddPoint(i,p->getRealX());
00383 yKSpline->AddPoint(i,p->getRealY());
00384 i++;
00385 node=node->GetNext();
00386 }
00387 }
00388
00389
00390
00391
00392 void pGraphicalFunction:: initializeSplineVectors()
00393 {
00394 double x=0,y=0,t,delta;
00395 int i,np,nps;
00396
00397
00398 np = logicFunction->getSizePoints();
00399 nps = 200;
00400 delta=( double ) ( np ) / ( double ) ( nps );
00401 for( i = 0; i < nps; i++ )
00402 {
00403 t = delta * (double)i;
00404 GetSplinePoint(t,x,y);
00405 xSpline.push_back(x);
00406 ySpline.push_back(y);
00407 }
00408 }
00409
00410
00411
00412 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
00413 {
00414 if (logicFunction->getSizePoints()==0)
00415 {
00416 x = 0;
00417 y = 0;
00418 }
00419 if (logicFunction->getSizePoints()>=2)
00420 {
00421 x = xKSpline->Evaluate(t);
00422 y = yKSpline->Evaluate(t);
00423 }
00424 }
00425
00426
00427
00428
00429
00440 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
00441 {
00442 int xS,yS,x1,x2,y1,y2;
00443 setScales();
00444 xS=clickedX/_scaleX+_offsetX;
00445 yS=clickedY/_scaleY+_offsetY;
00446
00447 x1=(clickedX-width)/_scaleX+_offsetX;
00448 x2=(clickedX+width)/_scaleX+_offsetX;
00449 y1=(clickedY-width)/_scaleY+_offsetY;
00450 y2=(clickedY+width)/_scaleY+_offsetY;
00451
00452 int numberPoints=getSizePoints();
00453 pFunctionPoint* point;
00454 wxNode* p;
00455 bool inSquare=false,first=false;
00456
00457
00458 if(zoomIn)
00459 {
00460 int i;
00461 for(i=0; i<numberPoints;i++)
00462 {
00463 p=logicFunction->GetPointAt(i);
00464 point=(pFunctionPoint*)p->GetData();
00465 int rx=point->getRealX();
00466 int ry=point->getRealY();
00467 inSquare= rx>=x1 && rx<=x2;
00468 if(inSquare)
00469 {
00470 if(!first)
00471 {
00472 initialDrawingPoint=point;
00473 _offsetX=rx;
00474 _offsetY=ry;
00475 first=true;
00476 }
00477 finalDrawingPoint=point;
00478 }
00479 }
00480 }
00481 else
00482 {
00483
00484 if((_offsetX-20)>=0)
00485 {
00486 _offsetX=_offsetX-20;
00487 }
00488 else
00489 _offsetX=0;
00490 if((_offsetY-10)>=0)
00491 {
00492 _offsetY=_offsetY-10;
00493 }
00494 else
00495 _offsetY=0;
00496 }
00497 }
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 void pGraphicalFunction::setOffsets(int offx,int offy)
00509 {
00510 int xS,yS;
00511
00512 setScales();
00513
00514 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
00515 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
00516
00517 setOffsetX(xS);
00518 setOffsetY(yS);
00519
00520
00521
00522
00523 setOffsetPixelX(offx);
00524 setOffsetPixelY(offy);
00525
00526 }
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 void pGraphicalFunction::setMinShowed()
00537 {
00538 bool firstX=false,firstY=false;
00539
00540 wxNode* node=logicFunction->GetPointAt(0);
00541 pFunctionPoint* p;
00542
00543
00544
00545
00546
00547 while(node)
00548 {
00549 p=(pFunctionPoint*)node->GetData();
00550 int px=p->getRealX();
00551 int py=p->getRealY();
00552 int x=(px-_offsetX);
00553 int y=(py-_offsetY);
00554 if(!firstX && x>=0)
00555 {
00556 firstX=true;
00557 setMinShowedX(px);
00558 }
00559 if(!firstY && y>=0)
00560 {
00561 firstY=true;
00562 setMinShowedY(py);
00563 }
00564 node=node->GetNext();
00565 }
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575 void pGraphicalFunction::save(wxString fileName)
00576 {
00577
00578 logicFunction->save(fileName);
00579
00580 }
00581
00582
00583
00584 void pGraphicalFunction::load(wxString fileName)
00585 {
00586 logicFunction->load(fileName);
00587 logicFunction->setUp();
00588 setMaxShowedX(logicFunction->getMaxX());
00589 setMinShowedX(logicFunction->getMinX());
00590 setMaxShowedY(logicFunction->getMaxY());
00591 setMinShowedY(logicFunction->getMinY());
00592 }
00593
00594
00595
00596
00600 void pGraphicalFunction::setActual(bool act)
00601 {
00602 ifActual=act;
00603 }
00604 bool pGraphicalFunction:: getActual()
00605 {
00606 return ifActual;
00607 }
00614 void pGraphicalFunction::setScaleWay(int typeS)
00615 {
00616 scaleWay=typeS;
00617 }
00618
00619 int pGraphicalFunction:: getScaleWay()
00620 {
00621 return scaleWay;
00622 }
00623
00624
00625
00626
00627
00628 void pGraphicalFunction::setScreenX(int scrX)
00629 {
00630 this->screenX=scrX;
00631 }
00632 int pGraphicalFunction::getScreenX()
00633 {
00634 return this->screenX;
00635 }
00636 void pGraphicalFunction::setScreenY(int scrY)
00637 {
00638 this->screenY=scrY;
00639 }
00640 int pGraphicalFunction::getScreenY()
00641 {
00642 return this->screenY;
00643 }
00644
00645
00646
00647
00648
00649 void pGraphicalFunction::setStartX(double aStartX) {
00650
00651 logicFunction->setStartX(aStartX);
00652 this->_startX =logicFunction->getStartX();
00653 }
00654
00655 double pGraphicalFunction::getStartX()
00656 {
00657 return logicFunction->getStartX();
00658 }
00659
00660 void pGraphicalFunction::setStartY(double aStartY)
00661 {
00662 logicFunction->setStartY(aStartY);
00663 this->_startY = aStartY;
00664 }
00665
00666 double pGraphicalFunction::getStartY() {
00667 return logicFunction->getStartY();
00668 }
00669
00670
00671
00672 void pGraphicalFunction::setEndX(double aEndX)
00673 {
00674 logicFunction->setEndX(aEndX);
00675 this->_endX = aEndX;
00676 }
00677
00678 double pGraphicalFunction::getEndX() {
00679 return logicFunction->getEndX();
00680 }
00681
00682 void pGraphicalFunction::setEndY(double aEndY) {
00683 logicFunction->setEndY(aEndY);
00684 this->_endY = aEndY;
00685 }
00686
00687 double pGraphicalFunction::getEndY() {
00688 return logicFunction->getEndY();
00689 }
00690
00691
00692
00693 void pGraphicalFunction::setScaleX(double aScaleX)
00694 {
00695 logicFunction->setScaleX(aScaleX);
00696 this->_scaleX = aScaleX;
00697 }
00698
00699 double pGraphicalFunction::getScaleX()
00700 {
00701 return this->_scaleX;
00702 }
00703
00704 void pGraphicalFunction::setScaleY(double aScaleY)
00705 {
00706 logicFunction->setScaleY(aScaleY);
00707 this->_scaleY = aScaleY;
00708 }
00709
00710 double pGraphicalFunction::getScaleY() {
00711 return this->_scaleY;
00712 }
00713
00714
00715
00716 void pGraphicalFunction::setMinX(double aMinX)
00717 {
00718 logicFunction->setMinX(aMinX);
00719 this->_minX = aMinX;
00720 }
00721
00722 double pGraphicalFunction::getMinX()
00723 {
00724 return logicFunction->getMinX();
00725 }
00726
00727 void pGraphicalFunction::setMinY(double aMinY)
00728 {
00729 logicFunction->setMinY(aMinY);
00730 this->_minY = aMinY;
00731 }
00732
00733 double pGraphicalFunction::getMinY() {
00734 return logicFunction->getMinY();
00735 }
00736
00737
00738
00739 void pGraphicalFunction::setMaxX(double aMaxX)
00740 {
00741 logicFunction->setMaxX(aMaxX);
00742 this->_maxX = aMaxX;
00743 }
00744
00745 double pGraphicalFunction::getMaxX()
00746 {
00747 return logicFunction->getMaxX();
00748 }
00749
00750 void pGraphicalFunction::setMaxY(double aMaxY)
00751 {
00752 logicFunction->setMaxY(aMaxY);
00753 this->_maxY = aMaxY;
00754 }
00755
00756 double pGraphicalFunction::getMaxY() {
00757 return logicFunction->getMaxY();
00758 }
00759
00760
00761
00762
00763 void pGraphicalFunction::setOffsetX(double aOffsetX)
00764 {
00765 logicFunction->setOffsetX(aOffsetX);
00766 this->_offsetX = aOffsetX;
00767 }
00768
00769 double pGraphicalFunction:: getOffsetX()
00770 {
00771
00772 return this->_offsetX;
00773 }
00774
00775 void pGraphicalFunction:: setOffsetY(double aOffsetY)
00776 {
00777 logicFunction->setOffsetY(aOffsetY);
00778 this->_offsetY = aOffsetY;
00779 }
00780
00781 double pGraphicalFunction:: getOffsetY() {
00782 return this->_offsetY;
00783 }
00784
00785
00786
00787
00788 void pGraphicalFunction:: setType(int aType)
00789 {
00790 logicFunction->setType(aType);
00791 this->_type = aType;
00792 }
00793
00794 int pGraphicalFunction :: getType() {
00795 return this->_type;
00796 }
00797
00798
00799
00800
00801 int pGraphicalFunction :: getValidPointRange()
00802 {
00803 return validPointRange;
00804 }
00805
00806 void pGraphicalFunction :: setValidPointRange(int theRange)
00807 {
00808 logicFunction->setValidPointRange(theRange);
00809 validPointRange = theRange;
00810 }
00811
00812
00813
00814
00815 int pGraphicalFunction :: getSizePoints()
00816 {
00817 return logicFunction->getSizePoints();
00818 }
00819
00820
00821
00822
00823
00824 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
00825 {
00826 f_colorPoints.clear();
00827 int i = 0;
00828 while(i<(int)(colorVector.size()))
00829 {
00830 f_colorPoints.push_back(colorVector[i]);
00831 i++;
00832 }
00833 }
00834
00835
00836
00837
00838
00839 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
00840 {
00841 int i = 0;
00842 while(i<(int)(f_colorPoints.size()))
00843 {
00844 pColorPoint * originaslPoint = f_colorPoints[i];
00845 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
00846 colorVector.push_back(copyPoint);
00847 i++;
00848 }
00849 }
00850
00851