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