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 bool added=logicFunction -> AddPoint( aX, aY,order );
168 logicFunction->setEndPoints();
169 logicFunction->setStartPoints();
175 * deletes the point given by the user from the collection of logical points of the function
179 bool pGraphicalFunction::DeletePoint(int aX, int aY)
181 bool added= logicFunction -> DeletePoint( aX, aY );
186 logicFunction->setEndPoints();
187 logicFunction->setStartPoints();
193 * deletes a point of the functio given its index
195 bool pGraphicalFunction::deletePointAt(int index)
197 bool added=logicFunction -> deletePointAt( index );
202 logicFunction->setEndPoints();
203 logicFunction->setStartPoints();
209 * Change de coordinates of the given index point to the newCoords, if it is possible and return the result of the invoked action.
210 * @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.
212 bool pGraphicalFunction::changePoint(wxPoint newCoords, int movingIndexPoint)
214 bool added= (logicFunction -> changePoint( newCoords, movingIndexPoint ));
219 logicFunction->setEndPoints();
220 logicFunction->setStartPoints();
225 * Evaluates if the given point belongs to the function.
228 bool pGraphicalFunction::hasPoint(wxPoint aPoint)
230 return logicFunction -> hasPoint( aPoint );
234 * Returns the real x values of the control points of the function. It dont includes the points calculated by interpolation.
236 double* pGraphicalFunction::getX_RealValues()
238 return (logicFunction -> getX_RealValues( ));
242 * Returns the real y values of the control points of the function. It dont includes the points calculated by interpolation.
244 double * pGraphicalFunction::getY_RealValues()
247 return (logicFunction -> getY_RealValues());
250 //----------------------------------
251 // Asking if it has a point (x,y)
252 //----------------------------------
255 * returns true if the point is along the function
258 bool pGraphicalFunction:: isInFunction(int x, int y)
262 pFunctionPoint* point= new pFunctionPoint(x,y);
264 before=getBefore(point);
266 if(before && before->GetNext())
268 next = before->GetNext();
269 //next = (before->GetNext())->GetNext();
270 inLine= isInLine((pFunctionPoint*)before->GetData(),(pFunctionPoint*)next->GetData(),point);
277 * give us the point that is in the function and is exactly before
278 * the point we are giving
280 wxNode* pGraphicalFunction:: getBefore(pFunctionPoint* point)
282 int minDiference=10000000;
283 wxNode* node=logicFunction->GetPointAt(0);
287 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
288 int beforeX=before1->getRealX();
289 int pointX=point->getRealX();
292 int minDiference1=pointX-beforeX;
293 if(minDiference1<minDiference)
295 minDiference=minDiference1;
299 node=node->GetNext();
304 * Returns true if the point is in the line
306 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
310 x1=(float)before->getRealX();
311 x2=(float)next->getRealX();
312 y1=(float)before->getRealY();
313 y2=(float)next->getRealY();
314 x=(float)point->getRealX();
315 y=(float)point->getRealY();
318 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
319 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
320 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
321 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
326 * give us the value of y in the line define by the arguments
328 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
333 //----------------------------
335 //----------------------------
337 //----------------------------
339 //----------------------------
343 * clear the spline vectors
345 void pGraphicalFunction:: clearSplineVectors()
352 Initiliaze xSpline and ySpline vectors
354 void pGraphicalFunction:: initializeSplines()
357 xKSpline = vtkKochanekSpline::New();
359 yKSpline= vtkKochanekSpline::New();
365 Add the (x,y) points of the function to the
368 void pGraphicalFunction::addSplinesPoints()
370 xKSpline->RemoveAllPoints();
371 yKSpline->RemoveAllPoints();
372 wxNode *node= logicFunction->GetPointAt(0);
377 p=(pFunctionPoint*)node->GetData();
378 xKSpline->AddPoint(i,p->getRealX());
379 yKSpline->AddPoint(i,p->getRealY());
381 node=node->GetNext();
385 This Method adds the point calculated by
386 the splines to the vectors
388 void pGraphicalFunction:: initializeSplineVectors()
390 double x=0,y=0,t,delta;
394 np = logicFunction->getSizePoints();
396 delta=( double ) ( np ) / ( double ) ( nps );
397 for( i = 0; i < nps; i++ )
399 t = delta * (double)i;
400 GetSplinePoint(t,x,y);
401 xSpline.push_back(x);
402 ySpline.push_back(y);
406 get the spline point for t, in xKSpline and yKSpline
408 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
410 if (logicFunction->getSizePoints()==0)
415 if (logicFunction->getSizePoints()>=2)
417 x = xKSpline->Evaluate(t);
418 y = yKSpline->Evaluate(t);
422 //------------------------------
424 //------------------------------
427 This method set the initial point and the final point of drawing
428 according with the width of the square around the point clicked
431 @param width: width of the square
432 Note: all the parameters are pixels
433 Pre: screenX, screenY, zoom are set! with the actual data.
436 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
438 int xS,yS,x1,x2,y1,y2;
440 xS=clickedX/_scaleX+_offsetX;
441 yS=clickedY/_scaleY+_offsetY;
443 x1=(clickedX-width)/_scaleX+_offsetX;
444 x2=(clickedX+width)/_scaleX+_offsetX;
445 y1=(clickedY-width)/_scaleY+_offsetY;
446 y2=(clickedY+width)/_scaleY+_offsetY;
448 int numberPoints=getSizePoints();
449 pFunctionPoint* point;
451 bool inSquare=false,first=false;
453 //if the user made a zoom
457 for(i=0; i<numberPoints;i++)
459 p=logicFunction->GetPointAt(i);
460 point=(pFunctionPoint*)p->GetData();
461 int rx=point->getRealX();
462 int ry=point->getRealY();
463 inSquare= rx>=x1 && rx<=x2; //&& ry>=y1 && ry<=y2;
468 initialDrawingPoint=point;
473 finalDrawingPoint=point;
479 //We extend the range of vision in x and y,20 and 10 respectively
482 _offsetX=_offsetX-20;
488 _offsetY=_offsetY-10;
496 This method sets the offsets according with the point
497 clicked on the window
498 @param offx: x-value of the point clicked
499 @param offx: y-value of the point clicked
500 Note: all the parameters are pixels
501 Pre: screenX, screenY are set! with the actual data.
502 Pos: Sets the value of minXshowed,minYshowed
504 void pGraphicalFunction::setOffsets(int offx,int offy)
510 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
511 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
519 setOffsetPixelX(offx);
520 setOffsetPixelY(offy);
524 * This method sets the minShowedX
525 * and the minShowedY, accordig to the offset in
526 * x-axis and y-axis respectively
527 * pre: _offsetX>=0,_offsetY>=0
528 * screenX and screenY are set with the actual size of the window
529 * scaleX and scaleY are set to the actual size of the window
532 void pGraphicalFunction::setMinShowed()
534 bool firstX=false,firstY=false;
536 wxNode* node=logicFunction->GetPointAt(0);
539 float middleWX=(((float)(screenX-100))/2)/scaleX;
540 float middleWY=(((float)(screenY-50))/2)/scaleY;
542 //node=node->GetNext();
545 p=(pFunctionPoint*)node->GetData();
546 int px=p->getRealX();
547 int py=p->getRealY();
548 int x=(px-_offsetX);//+middleWX;
549 int y=(py-_offsetY);//+middleWY;
560 node=node->GetNext();
564 //-----------------------
566 //-----------------------
569 Save the points of the function
571 void pGraphicalFunction::save(wxString fileName)
574 logicFunction->save(fileName);
578 Load the points of a function
580 void pGraphicalFunction::load(wxString fileName)
582 logicFunction->load(fileName);
583 logicFunction->setUp();
584 setMaxShowedX(logicFunction->getMaxX());
585 setMinShowedX(logicFunction->getMinX());
586 setMaxShowedY(logicFunction->getMaxY());
587 setMinShowedY(logicFunction->getMinY());
590 //--------------------------
591 //GETTERS AND SETTERS
592 //---------------------------
596 void pGraphicalFunction::setActual(bool act)
600 bool pGraphicalFunction:: getActual()
610 void pGraphicalFunction::setScaleWay(int typeS)
615 int pGraphicalFunction:: getScaleWay()
624 void pGraphicalFunction::setScreenX(int scrX)
628 int pGraphicalFunction::getScreenX()
630 return this->screenX;
632 void pGraphicalFunction::setScreenY(int scrY)
636 int pGraphicalFunction::getScreenY()
638 return this->screenY;
645 void pGraphicalFunction::setStartX(double aStartX) {
647 logicFunction->setStartX(aStartX);
648 this->_startX =logicFunction->getStartX();
651 double pGraphicalFunction::getStartX()
653 return logicFunction->getStartX();
656 void pGraphicalFunction::setStartY(double aStartY)
658 logicFunction->setStartY(aStartY);
659 this->_startY = aStartY;
662 double pGraphicalFunction::getStartY() {
663 return logicFunction->getStartY();
668 void pGraphicalFunction::setEndX(double aEndX)
670 logicFunction->setEndX(aEndX);
674 double pGraphicalFunction::getEndX() {
675 return logicFunction->getEndX();
678 void pGraphicalFunction::setEndY(double aEndY) {
679 logicFunction->setEndY(aEndY);
683 double pGraphicalFunction::getEndY() {
684 return logicFunction->getEndY();
689 void pGraphicalFunction::setScaleX(double aScaleX)
691 logicFunction->setScaleX(aScaleX);
692 this->_scaleX = aScaleX;
695 double pGraphicalFunction::getScaleX()
697 return this->_scaleX;
700 void pGraphicalFunction::setScaleY(double aScaleY)
702 logicFunction->setScaleY(aScaleY);
703 this->_scaleY = aScaleY;
706 double pGraphicalFunction::getScaleY() {
707 return this->_scaleY;
712 void pGraphicalFunction::setMinX(double aMinX)
714 logicFunction->setMinX(aMinX);
718 double pGraphicalFunction::getMinX()
720 return logicFunction->getMinX();
723 void pGraphicalFunction::setMinY(double aMinY)
725 logicFunction->setMinY(aMinY);
729 double pGraphicalFunction::getMinY() {
730 return logicFunction->getMinY();
735 void pGraphicalFunction::setMaxX(double aMaxX)
737 logicFunction->setMaxX(aMaxX);
741 double pGraphicalFunction::getMaxX()
743 return logicFunction->getMaxX();
746 void pGraphicalFunction::setMaxY(double aMaxY)
748 logicFunction->setMaxY(aMaxY);
752 double pGraphicalFunction::getMaxY() {
753 return logicFunction->getMaxY();
759 void pGraphicalFunction::setOffsetX(double aOffsetX)
761 logicFunction->setOffsetX(aOffsetX);
762 this->_offsetX = aOffsetX;
765 double pGraphicalFunction:: getOffsetX()
768 return this->_offsetX;
771 void pGraphicalFunction:: setOffsetY(double aOffsetY)
773 logicFunction->setOffsetY(aOffsetY);
774 this->_offsetY = aOffsetY;
777 double pGraphicalFunction:: getOffsetY() {
778 return this->_offsetY;
784 void pGraphicalFunction:: setType(int aType)
786 logicFunction->setType(aType);
790 int pGraphicalFunction :: getType() {
797 int pGraphicalFunction :: getValidPointRange()
799 return validPointRange;
802 void pGraphicalFunction :: setValidPointRange(int theRange)
804 logicFunction->setValidPointRange(theRange);
805 validPointRange = theRange;
808 * Returns the number of points that the function
811 int pGraphicalFunction :: getSizePoints()
813 return logicFunction->getSizePoints();
817 * Sets the color points of the function by teh given parameter
818 * @param colorVector Is the color points vector to set
820 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
822 f_colorPoints.clear();
824 while(i<colorVector.size())
826 f_colorPoints.push_back(colorVector[i]);
832 * Gets the color points of the function in the given parameter
833 * @param colorVector Is the color points list to get the points
835 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
838 while(i<f_colorPoints.size())
840 pColorPoint * originaslPoint = f_colorPoints[i];
841 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
842 colorVector.push_back(copyPoint);