2 //----------------------------------------------------------------------------
3 // Class definition include
4 //----------------------------------------------------------------------------
5 #include "pGraphicalFunction.h"
12 // ----------------------------------------------------------------------------
13 // WX headers inclusion.
14 // For compilers that support precompilation, includes <wx/wx.h>.
15 // ----------------------------------------------------------------------------
21 //----------------------------------------------------------------------------
22 // Class implementation
23 //----------------------------------------------------------------------------
25 IMPLEMENT_CLASS(pGraphicalFunction, pPlotterLayer)
27 pGraphicalFunction:: pGraphicalFunction(wxString name, int flags)
32 logicFunction = new pLogicalFunction ();
41 initialDrawingPoint=NULL;
42 finalDrawingPoint=NULL;
43 //type=1 means that is going to be a piecewise function
54 * Is the destructor!!!
56 pGraphicalFunction :: ~ pGraphicalFunction ()
61 //set if the function has to draw the points
62 void pGraphicalFunction::SetShowPoints(bool showPoints)
64 showPointsF = showPoints;
66 //get the paramater showPointsF
67 bool pGraphicalFunction::getShowPoints()
72 * Set Up startPoint, endPoint, maxY,maxX points
74 void pGraphicalFunction::setUp()
76 logicFunction->setUp();
77 setMaxX(logicFunction->getMaxX());
78 setMinX(logicFunction->getMinX());
79 setMaxY(logicFunction->getMaxY());
80 setMinY(logicFunction->getMinY());
81 setStartX(logicFunction->getStartX());
82 setEndX(logicFunction->getEndX());
83 setStartY(logicFunction->getStartY());
84 setEndY(logicFunction->getEndY());
89 * validate if the function has that point in a sensible area returning the index where the point was found or -1 if is in not part of the function: define the sensible area is
90 * x1-validPointRange<x<x1+validPointRange y1-validPointRange<y<y1+validPointRange
92 int pGraphicalFunction::validPointOnFunction(wxPoint realPoint)
94 return (logicFunction -> validPointOnFunction (realPoint));
97 * returns the index in the list of the point
99 int pGraphicalFunction::getIndexOf(wxPoint realpoint)
101 return logicFunction -> getIndexOf( realpoint );
105 * This metohd returns the node of the point that is the movingPointIndex position in the list of points.
106 * @param: int movingPointIndex, Is the index value of the searched node.
107 * @return: Return a pointer to the node corresponding to the index value by parameter.
109 wxNode* pGraphicalFunction::GetPointAt( int movingPointIndex )
111 return logicFunction -> GetPointAt ( movingPointIndex );
114 * Set the scales of the function in x and y
117 void pGraphicalFunction::setScales()
119 //int dx= logicFunction->getMaxX()-logicFunction->getMinX();
120 //int dy= logicFunction->getMaxY()-logicFunction->getMinY();
121 int dx= maxShowedX-minShowedX;
122 int dy= maxShowedY-minShowedY;
127 double scaleX=(((float)(screenX-100))/dx)*factorZoom;
128 double scaleY=(((float)(screenY-50))/dy)*factorZoom;
131 logicFunction->setScaleX(scaleX);
132 logicFunction->setScaleY(scaleY);
137 * Includes a point between two existing points of this function. The new point (x,y) was not defined when the function was created.
138 * @return Returns true is the point was succcesfully added to the funcion.
140 bool pGraphicalFunction:: AddNewPoint(int x,int y)
142 bool added= logicFunction -> AddNewPoint ( x, y );
147 logicFunction->setEndPoints();
148 logicFunction->setStartPoints();
154 * This tries to Add a point to the function if possible it returns true.
155 * @pre: The list of existing points is ordered.
156 * @param: int aX, The x value of the point.
157 * @param: int aY, The y value of the point.
158 * @param: return Returns TRUE if the point was succesfuly included in the realPoints list.
160 bool pGraphicalFunction::AddPoint(int aX, int aY,bool order)
163 if (logicFunction!=NULL){
164 added=logicFunction -> AddPoint( aX, aY,order );
169 logicFunction->setEndPoints();
170 logicFunction->setStartPoints();
172 } // if logicFunction
177 * deletes the point given by the user from the collection of logical points of the function
181 bool pGraphicalFunction::DeletePoint(int aX, int aY)
183 bool added= logicFunction -> DeletePoint( aX, aY );
188 logicFunction->setEndPoints();
189 logicFunction->setStartPoints();
195 * deletes a point of the functio given its index
197 bool pGraphicalFunction::deletePointAt(int index)
199 bool added=logicFunction -> deletePointAt( index );
204 logicFunction->setEndPoints();
205 logicFunction->setStartPoints();
211 * Change de coordinates of the given index point to the newCoords, if it is possible and return the result of the invoked action.
212 * @retun Return TRUE if it was possible to do the change. A valid change is when the new x value of the point is still between the previous and next point, when condition.
214 bool pGraphicalFunction::changePoint(wxPoint newCoords, int movingIndexPoint)
216 bool added= (logicFunction -> changePoint( newCoords, movingIndexPoint ));
221 logicFunction->setEndPoints();
222 logicFunction->setStartPoints();
227 * Evaluates if the given point belongs to the function.
230 bool pGraphicalFunction::hasPoint(wxPoint aPoint)
232 return logicFunction -> hasPoint( aPoint );
237 * Returns the real x values of the control points of the function. It dont includes the points calculated by interpolation.
239 double* pGraphicalFunction::getX_RealValues()
241 return (logicFunction -> getX_RealValues( ));
245 * Returns the real y values of the control points of the function. It dont includes the points calculated by interpolation.
247 double * pGraphicalFunction::getY_RealValues()
250 return (logicFunction -> getY_RealValues());
253 //----------------------------------
254 // Asking if it has a point (x,y)
255 //----------------------------------
258 * returns true if the point is along the function
261 bool pGraphicalFunction:: isInFunction(int x, int y)
265 pFunctionPoint* point= new pFunctionPoint(x,y);
267 before=getBefore(point);
269 if(before && before->GetNext())
271 next = before->GetNext();
272 //next = (before->GetNext())->GetNext();
273 inLine= isInLine((pFunctionPoint*)before->GetData(),(pFunctionPoint*)next->GetData(),point);
280 * give us the point that is in the function and is exactly before
281 * the point we are giving
283 wxNode* pGraphicalFunction:: getBefore(pFunctionPoint* point)
285 int minDiference=10000000;
286 wxNode* node=logicFunction->GetPointAt(0);
290 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
291 int beforeX=before1->getRealX();
292 int pointX=point->getRealX();
295 int minDiference1=pointX-beforeX;
296 if(minDiference1<minDiference)
298 minDiference=minDiference1;
302 node=node->GetNext();
307 * Returns true if the point is in the line
309 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
313 x1=(float)before->getRealX();
314 x2=(float)next->getRealX();
315 y1=(float)before->getRealY();
316 y2=(float)next->getRealY();
317 x=(float)point->getRealX();
318 y=(float)point->getRealY();
321 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
322 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
323 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
324 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
329 * give us the value of y in the line define by the arguments
331 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
336 //----------------------------
338 //----------------------------
340 //----------------------------
342 //----------------------------
346 * clear the spline vectors
348 void pGraphicalFunction:: clearSplineVectors()
355 Initiliaze xSpline and ySpline vectors
357 void pGraphicalFunction:: initializeSplines()
360 xKSpline = vtkKochanekSpline::New();
362 yKSpline= vtkKochanekSpline::New();
368 Add the (x,y) points of the function to the
371 void pGraphicalFunction::addSplinesPoints()
373 xKSpline->RemoveAllPoints();
374 yKSpline->RemoveAllPoints();
375 wxNode *node= logicFunction->GetPointAt(0);
380 p=(pFunctionPoint*)node->GetData();
381 xKSpline->AddPoint(i,p->getRealX());
382 yKSpline->AddPoint(i,p->getRealY());
384 node=node->GetNext();
388 This Method adds the point calculated by
389 the splines to the vectors
391 void pGraphicalFunction:: initializeSplineVectors()
393 double x=0,y=0,t,delta;
397 np = logicFunction->getSizePoints();
399 delta=( double ) ( np ) / ( double ) ( nps );
400 for( i = 0; i < nps; i++ )
402 t = delta * (double)i;
403 GetSplinePoint(t,x,y);
404 xSpline.push_back(x);
405 ySpline.push_back(y);
409 get the spline point for t, in xKSpline and yKSpline
411 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
413 if (logicFunction->getSizePoints()==0)
418 if (logicFunction->getSizePoints()>=2)
420 x = xKSpline->Evaluate(t);
421 y = yKSpline->Evaluate(t);
425 //------------------------------
427 //------------------------------
430 This method set the initial point and the final point of drawing
431 according with the width of the square around the point clicked
434 @param width: width of the square
435 Note: all the parameters are pixels
436 Pre: screenX, screenY, zoom are set! with the actual data.
439 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
441 int xS,yS,x1,x2,y1,y2;
443 xS=clickedX/_scaleX+_offsetX;
444 yS=clickedY/_scaleY+_offsetY;
446 x1=(clickedX-width)/_scaleX+_offsetX;
447 x2=(clickedX+width)/_scaleX+_offsetX;
448 y1=(clickedY-width)/_scaleY+_offsetY;
449 y2=(clickedY+width)/_scaleY+_offsetY;
451 int numberPoints=getSizePoints();
452 pFunctionPoint* point;
454 bool inSquare=false,first=false;
456 //if the user made a zoom
460 for(i=0; i<numberPoints;i++)
462 p=logicFunction->GetPointAt(i);
463 point=(pFunctionPoint*)p->GetData();
464 int rx=point->getRealX();
465 int ry=point->getRealY();
466 inSquare= rx>=x1 && rx<=x2; //&& ry>=y1 && ry<=y2;
471 initialDrawingPoint=point;
476 finalDrawingPoint=point;
482 //We extend the range of vision in x and y,20 and 10 respectively
485 _offsetX=_offsetX-20;
491 _offsetY=_offsetY-10;
499 This method sets the offsets according with the point
500 clicked on the window
501 @param offx: x-value of the point clicked
502 @param offx: y-value of the point clicked
503 Note: all the parameters are pixels
504 Pre: screenX, screenY are set! with the actual data.
505 Pos: Sets the value of minXshowed,minYshowed
507 void pGraphicalFunction::setOffsets(int offx,int offy)
513 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
514 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
522 setOffsetPixelX(offx);
523 setOffsetPixelY(offy);
527 * This method sets the minShowedX
528 * and the minShowedY, accordig to the offset in
529 * x-axis and y-axis respectively
530 * pre: _offsetX>=0,_offsetY>=0
531 * screenX and screenY are set with the actual size of the window
532 * scaleX and scaleY are set to the actual size of the window
535 void pGraphicalFunction::setMinShowed()
537 bool firstX=false,firstY=false;
539 wxNode* node=logicFunction->GetPointAt(0);
542 //float middleWX=(((float)(screenX-100))/2)/scaleX; // JPRx
543 //float middleWY=(((float)(screenY-50))/2)/scaleY; // JPRx
545 //node=node->GetNext();
548 p=(pFunctionPoint*)node->GetData();
549 int px=p->getRealX();
550 int py=p->getRealY();
551 int x=(px-_offsetX);//+middleWX;
552 int y=(py-_offsetY);//+middleWY;
563 node=node->GetNext();
567 //-----------------------
569 //-----------------------
572 Save the points of the function
574 void pGraphicalFunction::save(wxString fileName)
577 logicFunction->save(fileName);
581 Load the points of a function
583 void pGraphicalFunction::load(wxString fileName)
585 logicFunction->load(fileName);
586 logicFunction->setUp();
587 setMaxShowedX(logicFunction->getMaxX());
588 setMinShowedX(logicFunction->getMinX());
589 setMaxShowedY(logicFunction->getMaxY());
590 setMinShowedY(logicFunction->getMinY());
593 //--------------------------
594 //GETTERS AND SETTERS
595 //---------------------------
599 void pGraphicalFunction::setActual(bool act)
603 bool pGraphicalFunction:: getActual()
613 void pGraphicalFunction::setScaleWay(int typeS)
618 int pGraphicalFunction:: getScaleWay()
627 void pGraphicalFunction::setScreenX(int scrX)
631 int pGraphicalFunction::getScreenX()
633 return this->screenX;
635 void pGraphicalFunction::setScreenY(int scrY)
639 int pGraphicalFunction::getScreenY()
641 return this->screenY;
648 void pGraphicalFunction::setStartX(double aStartX) {
650 logicFunction->setStartX(aStartX);
651 this->_startX =logicFunction->getStartX();
654 double pGraphicalFunction::getStartX()
656 return logicFunction->getStartX();
659 void pGraphicalFunction::setStartY(double aStartY)
661 logicFunction->setStartY(aStartY);
662 this->_startY = aStartY;
665 double pGraphicalFunction::getStartY() {
666 return logicFunction->getStartY();
671 void pGraphicalFunction::setEndX(double aEndX)
673 logicFunction->setEndX(aEndX);
677 double pGraphicalFunction::getEndX() {
678 return logicFunction->getEndX();
681 void pGraphicalFunction::setEndY(double aEndY) {
682 logicFunction->setEndY(aEndY);
686 double pGraphicalFunction::getEndY() {
687 return logicFunction->getEndY();
692 void pGraphicalFunction::setScaleX(double aScaleX)
694 logicFunction->setScaleX(aScaleX);
695 this->_scaleX = aScaleX;
698 double pGraphicalFunction::getScaleX()
700 return this->_scaleX;
703 void pGraphicalFunction::setScaleY(double aScaleY)
705 logicFunction->setScaleY(aScaleY);
706 this->_scaleY = aScaleY;
709 double pGraphicalFunction::getScaleY() {
710 return this->_scaleY;
715 void pGraphicalFunction::setMinX(double aMinX)
717 logicFunction->setMinX(aMinX);
721 double pGraphicalFunction::getMinX()
723 return logicFunction->getMinX();
726 void pGraphicalFunction::setMinY(double aMinY)
728 logicFunction->setMinY(aMinY);
732 double pGraphicalFunction::getMinY() {
733 return logicFunction->getMinY();
738 void pGraphicalFunction::setMaxX(double aMaxX)
740 logicFunction->setMaxX(aMaxX);
744 double pGraphicalFunction::getMaxX()
746 return logicFunction->getMaxX();
749 void pGraphicalFunction::setMaxY(double aMaxY)
751 logicFunction->setMaxY(aMaxY);
755 double pGraphicalFunction::getMaxY() {
756 return logicFunction->getMaxY();
762 void pGraphicalFunction::setOffsetX(double aOffsetX)
764 logicFunction->setOffsetX(aOffsetX);
765 this->_offsetX = aOffsetX;
768 double pGraphicalFunction:: getOffsetX()
771 return this->_offsetX;
774 void pGraphicalFunction:: setOffsetY(double aOffsetY)
776 logicFunction->setOffsetY(aOffsetY);
777 this->_offsetY = aOffsetY;
780 double pGraphicalFunction:: getOffsetY() {
781 return this->_offsetY;
787 void pGraphicalFunction:: setType(int aType)
789 logicFunction->setType(aType);
793 int pGraphicalFunction :: getType() {
800 int pGraphicalFunction :: getValidPointRange()
802 return validPointRange;
805 void pGraphicalFunction :: setValidPointRange(int theRange)
807 logicFunction->setValidPointRange(theRange);
808 validPointRange = theRange;
811 * Returns the number of points that the function
814 int pGraphicalFunction :: getSizePoints()
816 return logicFunction->getSizePoints();
820 * Sets the color points of the function by teh given parameter
821 * @param colorVector Is the color points vector to set
823 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
825 f_colorPoints.clear();
827 while(i<(int)(colorVector.size()))
829 f_colorPoints.push_back(colorVector[i]);
835 * Gets the color points of the function in the given parameter
836 * @param colorVector Is the color points list to get the points
838 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
841 while(i<(int)(f_colorPoints.size()))
843 pColorPoint * originaslPoint = f_colorPoints[i];
844 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
845 colorVector.push_back(copyPoint);