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 );
235 * Returns the real x values of the control points of the function. It dont includes the points calculated by interpolation.
237 double* pGraphicalFunction::getX_RealValues()
239 return (logicFunction -> getX_RealValues( ));
243 * Returns the real y values of the control points of the function. It dont includes the points calculated by interpolation.
245 double * pGraphicalFunction::getY_RealValues()
248 return (logicFunction -> getY_RealValues());
251 //----------------------------------
252 // Asking if it has a point (x,y)
253 //----------------------------------
256 * returns true if the point is along the function
259 bool pGraphicalFunction:: isInFunction(int x, int y)
263 pFunctionPoint* point= new pFunctionPoint(x,y);
265 before=getBefore(point);
267 if(before && before->GetNext())
269 next = before->GetNext();
270 //next = (before->GetNext())->GetNext();
271 inLine= isInLine((pFunctionPoint*)before->GetData(),(pFunctionPoint*)next->GetData(),point);
278 * give us the point that is in the function and is exactly before
279 * the point we are giving
281 wxNode* pGraphicalFunction:: getBefore(pFunctionPoint* point)
283 int minDiference=10000000;
284 wxNode* node=logicFunction->GetPointAt(0);
288 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
289 int beforeX=before1->getRealX();
290 int pointX=point->getRealX();
293 int minDiference1=pointX-beforeX;
294 if(minDiference1<minDiference)
296 minDiference=minDiference1;
300 node=node->GetNext();
305 * Returns true if the point is in the line
307 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
311 x1=(float)before->getRealX();
312 x2=(float)next->getRealX();
313 y1=(float)before->getRealY();
314 y2=(float)next->getRealY();
315 x=(float)point->getRealX();
316 y=(float)point->getRealY();
319 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
320 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
321 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
322 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
327 * give us the value of y in the line define by the arguments
329 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
334 //----------------------------
336 //----------------------------
338 //----------------------------
340 //----------------------------
344 * clear the spline vectors
346 void pGraphicalFunction:: clearSplineVectors()
353 Initiliaze xSpline and ySpline vectors
355 void pGraphicalFunction:: initializeSplines()
358 xKSpline = vtkKochanekSpline::New();
360 yKSpline= vtkKochanekSpline::New();
366 Add the (x,y) points of the function to the
369 void pGraphicalFunction::addSplinesPoints()
371 xKSpline->RemoveAllPoints();
372 yKSpline->RemoveAllPoints();
373 wxNode *node= logicFunction->GetPointAt(0);
378 p=(pFunctionPoint*)node->GetData();
379 xKSpline->AddPoint(i,p->getRealX());
380 yKSpline->AddPoint(i,p->getRealY());
382 node=node->GetNext();
386 This Method adds the point calculated by
387 the splines to the vectors
389 void pGraphicalFunction:: initializeSplineVectors()
391 double x=0,y=0,t,delta;
395 np = logicFunction->getSizePoints();
397 delta=( double ) ( np ) / ( double ) ( nps );
398 for( i = 0; i < nps; i++ )
400 t = delta * (double)i;
401 GetSplinePoint(t,x,y);
402 xSpline.push_back(x);
403 ySpline.push_back(y);
407 get the spline point for t, in xKSpline and yKSpline
409 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
411 if (logicFunction->getSizePoints()==0)
416 if (logicFunction->getSizePoints()>=2)
418 x = xKSpline->Evaluate(t);
419 y = yKSpline->Evaluate(t);
423 //------------------------------
425 //------------------------------
428 This method set the initial point and the final point of drawing
429 according with the width of the square around the point clicked
432 @param width: width of the square
433 Note: all the parameters are pixels
434 Pre: screenX, screenY, zoom are set! with the actual data.
437 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
439 int xS,yS,x1,x2,y1,y2;
441 xS=clickedX/_scaleX+_offsetX;
442 yS=clickedY/_scaleY+_offsetY;
444 x1=(clickedX-width)/_scaleX+_offsetX;
445 x2=(clickedX+width)/_scaleX+_offsetX;
446 y1=(clickedY-width)/_scaleY+_offsetY;
447 y2=(clickedY+width)/_scaleY+_offsetY;
449 int numberPoints=getSizePoints();
450 pFunctionPoint* point;
452 bool inSquare=false,first=false;
454 //if the user made a zoom
458 for(i=0; i<numberPoints;i++)
460 p=logicFunction->GetPointAt(i);
461 point=(pFunctionPoint*)p->GetData();
462 int rx=point->getRealX();
463 int ry=point->getRealY();
464 inSquare= rx>=x1 && rx<=x2; //&& ry>=y1 && ry<=y2;
469 initialDrawingPoint=point;
474 finalDrawingPoint=point;
480 //We extend the range of vision in x and y,20 and 10 respectively
483 _offsetX=_offsetX-20;
489 _offsetY=_offsetY-10;
497 This method sets the offsets according with the point
498 clicked on the window
499 @param offx: x-value of the point clicked
500 @param offx: y-value of the point clicked
501 Note: all the parameters are pixels
502 Pre: screenX, screenY are set! with the actual data.
503 Pos: Sets the value of minXshowed,minYshowed
505 void pGraphicalFunction::setOffsets(int offx,int offy)
511 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
512 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
520 setOffsetPixelX(offx);
521 setOffsetPixelY(offy);
525 * This method sets the minShowedX
526 * and the minShowedY, accordig to the offset in
527 * x-axis and y-axis respectively
528 * pre: _offsetX>=0,_offsetY>=0
529 * screenX and screenY are set with the actual size of the window
530 * scaleX and scaleY are set to the actual size of the window
533 void pGraphicalFunction::setMinShowed()
535 bool firstX=false,firstY=false;
537 wxNode* node=logicFunction->GetPointAt(0);
540 //float middleWX=(((float)(screenX-100))/2)/scaleX; // JPRx
541 //float middleWY=(((float)(screenY-50))/2)/scaleY; // JPRx
543 //node=node->GetNext();
546 p=(pFunctionPoint*)node->GetData();
547 int px=p->getRealX();
548 int py=p->getRealY();
549 int x=(px-_offsetX);//+middleWX;
550 int y=(py-_offsetY);//+middleWY;
561 node=node->GetNext();
565 //-----------------------
567 //-----------------------
570 Save the points of the function
572 void pGraphicalFunction::save(wxString fileName)
575 logicFunction->save(fileName);
579 Load the points of a function
581 void pGraphicalFunction::load(wxString fileName)
583 logicFunction->load(fileName);
584 logicFunction->setUp();
585 setMaxShowedX(logicFunction->getMaxX());
586 setMinShowedX(logicFunction->getMinX());
587 setMaxShowedY(logicFunction->getMaxY());
588 setMinShowedY(logicFunction->getMinY());
591 //--------------------------
592 //GETTERS AND SETTERS
593 //---------------------------
597 void pGraphicalFunction::setActual(bool act)
601 bool pGraphicalFunction:: getActual()
611 void pGraphicalFunction::setScaleWay(int typeS)
616 int pGraphicalFunction:: getScaleWay()
625 void pGraphicalFunction::setScreenX(int scrX)
629 int pGraphicalFunction::getScreenX()
631 return this->screenX;
633 void pGraphicalFunction::setScreenY(int scrY)
637 int pGraphicalFunction::getScreenY()
639 return this->screenY;
646 void pGraphicalFunction::setStartX(double aStartX) {
648 logicFunction->setStartX(aStartX);
649 this->_startX =logicFunction->getStartX();
652 double pGraphicalFunction::getStartX()
654 return logicFunction->getStartX();
657 void pGraphicalFunction::setStartY(double aStartY)
659 logicFunction->setStartY(aStartY);
660 this->_startY = aStartY;
663 double pGraphicalFunction::getStartY() {
664 return logicFunction->getStartY();
669 void pGraphicalFunction::setEndX(double aEndX)
671 logicFunction->setEndX(aEndX);
675 double pGraphicalFunction::getEndX() {
676 return logicFunction->getEndX();
679 void pGraphicalFunction::setEndY(double aEndY) {
680 logicFunction->setEndY(aEndY);
684 double pGraphicalFunction::getEndY() {
685 return logicFunction->getEndY();
690 void pGraphicalFunction::setScaleX(double aScaleX)
692 logicFunction->setScaleX(aScaleX);
693 this->_scaleX = aScaleX;
696 double pGraphicalFunction::getScaleX()
698 return this->_scaleX;
701 void pGraphicalFunction::setScaleY(double aScaleY)
703 logicFunction->setScaleY(aScaleY);
704 this->_scaleY = aScaleY;
707 double pGraphicalFunction::getScaleY() {
708 return this->_scaleY;
713 void pGraphicalFunction::setMinX(double aMinX)
715 logicFunction->setMinX(aMinX);
719 double pGraphicalFunction::getMinX()
721 return logicFunction->getMinX();
724 void pGraphicalFunction::setMinY(double aMinY)
726 logicFunction->setMinY(aMinY);
730 double pGraphicalFunction::getMinY() {
731 return logicFunction->getMinY();
736 void pGraphicalFunction::setMaxX(double aMaxX)
738 logicFunction->setMaxX(aMaxX);
742 double pGraphicalFunction::getMaxX()
744 return logicFunction->getMaxX();
747 void pGraphicalFunction::setMaxY(double aMaxY)
749 logicFunction->setMaxY(aMaxY);
753 double pGraphicalFunction::getMaxY() {
754 return logicFunction->getMaxY();
760 void pGraphicalFunction::setOffsetX(double aOffsetX)
762 logicFunction->setOffsetX(aOffsetX);
763 this->_offsetX = aOffsetX;
766 double pGraphicalFunction:: getOffsetX()
769 return this->_offsetX;
772 void pGraphicalFunction:: setOffsetY(double aOffsetY)
774 logicFunction->setOffsetY(aOffsetY);
775 this->_offsetY = aOffsetY;
778 double pGraphicalFunction:: getOffsetY() {
779 return this->_offsetY;
785 void pGraphicalFunction:: setType(int aType)
787 logicFunction->setType(aType);
791 int pGraphicalFunction :: getType() {
798 int pGraphicalFunction :: getValidPointRange()
800 return validPointRange;
803 void pGraphicalFunction :: setValidPointRange(int theRange)
805 logicFunction->setValidPointRange(theRange);
806 validPointRange = theRange;
809 * Returns the number of points that the function
812 int pGraphicalFunction :: getSizePoints()
814 return logicFunction->getSizePoints();
818 * Sets the color points of the function by teh given parameter
819 * @param colorVector Is the color points vector to set
821 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
823 f_colorPoints.clear();
825 while(i<colorVector.size())
827 f_colorPoints.push_back(colorVector[i]);
833 * Gets the color points of the function in the given parameter
834 * @param colorVector Is the color points list to get the points
836 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
839 while(i<f_colorPoints.size())
841 pColorPoint * originaslPoint = f_colorPoints[i];
842 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
843 colorVector.push_back(copyPoint);