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
55 * Is the destructor!!!
57 pGraphicalFunction :: ~ pGraphicalFunction ()
62 //set if the function has to draw the points
63 void pGraphicalFunction::SetShowPoints(bool showPoints)
65 showPointsF = showPoints;
67 //get the paramater showPointsF
68 bool pGraphicalFunction::getShowPoints()
73 * Set Up startPoint, endPoint, maxY,maxX points
75 void pGraphicalFunction::setUp()
77 logicFunction->setUp();
78 setMaxX(logicFunction->getMaxX());
79 setMinX(logicFunction->getMinX());
80 setMaxY(logicFunction->getMaxY());
81 setMinY(logicFunction->getMinY());
82 setStartX(logicFunction->getStartX());
83 setEndX(logicFunction->getEndX());
84 setStartY(logicFunction->getStartY());
85 setEndY(logicFunction->getEndY());
90 * 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
91 * x1-validPointRange<x<x1+validPointRange y1-validPointRange<y<y1+validPointRange
93 int pGraphicalFunction::validPointOnFunction(wxPoint realPoint)
95 return (logicFunction -> validPointOnFunction (realPoint));
98 * returns the index in the list of the point
100 int pGraphicalFunction::getIndexOf(wxPoint realpoint)
102 return logicFunction -> getIndexOf( realpoint );
106 * This metohd returns the node of the point that is the movingPointIndex position in the list of points.
107 * @param: int movingPointIndex, Is the index value of the searched node.
108 * @return: Return a pointer to the node corresponding to the index value by parameter.
110 wxNode* pGraphicalFunction::GetPointAt( int movingPointIndex )
112 return logicFunction -> GetPointAt ( movingPointIndex );
115 * Set the scales of the function in x and y
118 void pGraphicalFunction::setScales()
120 //int dx= logicFunction->getMaxX()-logicFunction->getMinX();
121 //int dy= logicFunction->getMaxY()-logicFunction->getMinY();
122 int dx= maxShowedX-minShowedX;
123 int dy= maxShowedY-minShowedY;
128 double scaleX=(((float)(screenX-100))/dx)*factorZoom;
129 double scaleY=(((float)(screenY-50))/dy)*factorZoom;
132 logicFunction->setScaleX(scaleX);
133 logicFunction->setScaleY(scaleY);
138 * Includes a point between two existing points of this function. The new point (x,y) was not defined when the function was created.
139 * @return Returns true is the point was succcesfully added to the funcion.
141 bool pGraphicalFunction:: AddNewPoint(int x,int y)
143 bool added= logicFunction -> AddNewPoint ( x, y );
148 logicFunction->setEndPoints();
149 logicFunction->setStartPoints();
155 * This tries to Add a point to the function if possible it returns true.
156 * @pre: The list of existing points is ordered.
157 * @param: int aX, The x value of the point.
158 * @param: int aY, The y value of the point.
159 * @param: return Returns TRUE if the point was succesfuly included in the realPoints list.
161 bool pGraphicalFunction::AddPoint(int aX, int aY,bool order)
164 if (logicFunction!=NULL){
165 added=logicFunction -> AddPoint( aX, aY,order );
170 logicFunction->setEndPoints();
171 logicFunction->setStartPoints();
173 } // if logicFunction
178 * deletes the point given by the user from the collection of logical points of the function
182 bool pGraphicalFunction::DeletePoint(int aX, int aY)
184 bool added= logicFunction -> DeletePoint( aX, aY );
189 logicFunction->setEndPoints();
190 logicFunction->setStartPoints();
196 * deletes a point of the functio given its index
198 bool pGraphicalFunction::deletePointAt(int index)
200 bool added=logicFunction -> deletePointAt( index );
205 logicFunction->setEndPoints();
206 logicFunction->setStartPoints();
212 * Change de coordinates of the given index point to the newCoords, if it is possible and return the result of the invoked action.
213 * @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.
215 bool pGraphicalFunction::changePoint(wxPoint newCoords, int movingIndexPoint)
217 bool added= (logicFunction -> changePoint( newCoords, movingIndexPoint ));
222 logicFunction->setEndPoints();
223 logicFunction->setStartPoints();
228 * Evaluates if the given point belongs to the function.
231 bool pGraphicalFunction::hasPoint(wxPoint aPoint)
233 return logicFunction -> hasPoint( aPoint );
238 * Returns the real x values of the control points of the function. It dont includes the points calculated by interpolation.
240 double* pGraphicalFunction::getX_RealValues()
242 return (logicFunction -> getX_RealValues( ));
246 * Returns the real y values of the control points of the function. It dont includes the points calculated by interpolation.
248 double * pGraphicalFunction::getY_RealValues()
251 return (logicFunction -> getY_RealValues());
254 //----------------------------------
255 // Asking if it has a point (x,y)
256 //----------------------------------
259 * returns true if the point is along the function
262 bool pGraphicalFunction:: isInFunction(int x, int y)
266 pFunctionPoint* point= new pFunctionPoint(x,y);
268 before=getBefore(point);
270 if(before && before->GetNext())
272 next = before->GetNext();
273 //next = (before->GetNext())->GetNext();
274 inLine= isInLine((pFunctionPoint*)before->GetData(),(pFunctionPoint*)next->GetData(),point);
281 * give us the point that is in the function and is exactly before
282 * the point we are giving
284 wxNode* pGraphicalFunction:: getBefore(pFunctionPoint* point)
286 int minDiference=10000000;
287 wxNode* node=logicFunction->GetPointAt(0);
292 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
293 int beforeX=before1->getRealX();
294 int pointX=point->getRealX();
297 int minDiference1=pointX-beforeX;
298 if(minDiference1<minDiference)
300 minDiference=minDiference1;
304 node=node->GetNext();
309 * Returns true if the point is in the line
311 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
315 x1=(float)before->getRealX();
316 x2=(float)next->getRealX();
317 y1=(float)before->getRealY();
318 y2=(float)next->getRealY();
319 x=(float)point->getRealX();
320 y=(float)point->getRealY();
323 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
324 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
325 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
326 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
331 * give us the value of y in the line define by the arguments
333 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
338 //----------------------------
340 //----------------------------
342 //----------------------------
344 //----------------------------
348 * clear the spline vectors
350 void pGraphicalFunction:: clearSplineVectors()
357 Initiliaze xSpline and ySpline vectors
359 void pGraphicalFunction:: initializeSplines()
362 xKSpline = vtkKochanekSpline::New();
364 yKSpline= vtkKochanekSpline::New();
370 Add the (x,y) points of the function to the
373 void pGraphicalFunction::addSplinesPoints()
375 xKSpline->RemoveAllPoints();
376 yKSpline->RemoveAllPoints();
377 wxNode *node= logicFunction->GetPointAt(0);
382 p=(pFunctionPoint*)node->GetData();
383 xKSpline->AddPoint(i,p->getRealX());
384 yKSpline->AddPoint(i,p->getRealY());
386 node=node->GetNext();
390 This Method adds the point calculated by
391 the splines to the vectors
393 void pGraphicalFunction:: initializeSplineVectors()
395 double x=0,y=0,t,delta;
399 np = logicFunction->getSizePoints();
401 delta=( double ) ( np ) / ( double ) ( nps );
402 for( i = 0; i < nps; i++ )
404 t = delta * (double)i;
405 GetSplinePoint(t,x,y);
406 xSpline.push_back(x);
407 ySpline.push_back(y);
411 get the spline point for t, in xKSpline and yKSpline
413 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
415 if (logicFunction->getSizePoints()==0)
420 if (logicFunction->getSizePoints()>=2)
422 x = xKSpline->Evaluate(t);
423 y = yKSpline->Evaluate(t);
427 //------------------------------
429 //------------------------------
432 This method set the initial point and the final point of drawing
433 according with the width of the square around the point clicked
436 @param width: width of the square
437 Note: all the parameters are pixels
438 Pre: screenX, screenY, zoom are set! with the actual data.
441 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
443 int xS,yS,x1,x2,y1,y2;
445 xS=clickedX/_scaleX+_offsetX;
446 yS=clickedY/_scaleY+_offsetY;
448 x1=(clickedX-width)/_scaleX+_offsetX;
449 x2=(clickedX+width)/_scaleX+_offsetX;
450 y1=(clickedY-width)/_scaleY+_offsetY;
451 y2=(clickedY+width)/_scaleY+_offsetY;
453 int numberPoints=getSizePoints();
454 pFunctionPoint* point;
456 bool inSquare=false,first=false;
458 //if the user made a zoom
462 for(i=0; i<numberPoints;i++)
464 p=logicFunction->GetPointAt(i);
465 point=(pFunctionPoint*)p->GetData();
466 int rx=point->getRealX();
467 int ry=point->getRealY();
468 inSquare= rx>=x1 && rx<=x2; //&& ry>=y1 && ry<=y2;
473 initialDrawingPoint=point;
478 finalDrawingPoint=point;
484 //We extend the range of vision in x and y,20 and 10 respectively
487 _offsetX=_offsetX-20;
493 _offsetY=_offsetY-10;
501 This method sets the offsets according with the point
502 clicked on the window
503 @param offx: x-value of the point clicked
504 @param offx: y-value of the point clicked
505 Note: all the parameters are pixels
506 Pre: screenX, screenY are set! with the actual data.
507 Pos: Sets the value of minXshowed,minYshowed
509 void pGraphicalFunction::setOffsets(int offx,int offy)
515 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
516 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
524 setOffsetPixelX(offx);
525 setOffsetPixelY(offy);
529 * This method sets the minShowedX
530 * and the minShowedY, accordig to the offset in
531 * x-axis and y-axis respectively
532 * pre: _offsetX>=0,_offsetY>=0
533 * screenX and screenY are set with the actual size of the window
534 * scaleX and scaleY are set to the actual size of the window
537 void pGraphicalFunction::setMinShowed()
539 bool firstX=false,firstY=false;
541 wxNode* node=logicFunction->GetPointAt(0);
544 //float middleWX=(((float)(screenX-100))/2)/scaleX; // JPRx
545 //float middleWY=(((float)(screenY-50))/2)/scaleY; // JPRx
547 //node=node->GetNext();
550 p=(pFunctionPoint*)node->GetData();
551 int px=p->getRealX();
552 int py=p->getRealY();
553 int x=(px-_offsetX);//+middleWX;
554 int y=(py-_offsetY);//+middleWY;
565 node=node->GetNext();
569 //-----------------------
571 //-----------------------
574 Save the points of the function
576 void pGraphicalFunction::save(wxString fileName)
579 logicFunction->save(fileName);
583 Load the points of a function
585 void pGraphicalFunction::load(wxString fileName)
587 logicFunction->load(fileName);
588 logicFunction->setUp();
589 setMaxShowedX(logicFunction->getMaxX());
590 setMinShowedX(logicFunction->getMinX());
591 setMaxShowedY(logicFunction->getMaxY());
592 setMinShowedY(logicFunction->getMinY());
595 //--------------------------
596 //GETTERS AND SETTERS
597 //---------------------------
601 void pGraphicalFunction::setActual(bool act)
605 bool pGraphicalFunction:: getActual()
615 void pGraphicalFunction::setScaleWay(int typeS)
620 int pGraphicalFunction:: getScaleWay()
629 void pGraphicalFunction::setScreenX(int scrX)
633 int pGraphicalFunction::getScreenX()
635 return this->screenX;
637 void pGraphicalFunction::setScreenY(int scrY)
641 int pGraphicalFunction::getScreenY()
643 return this->screenY;
650 void pGraphicalFunction::setStartX(double aStartX) {
652 logicFunction->setStartX(aStartX);
653 this->_startX =logicFunction->getStartX();
656 double pGraphicalFunction::getStartX()
658 return logicFunction->getStartX();
661 void pGraphicalFunction::setStartY(double aStartY)
663 logicFunction->setStartY(aStartY);
664 this->_startY = aStartY;
667 double pGraphicalFunction::getStartY() {
668 return logicFunction->getStartY();
673 void pGraphicalFunction::setEndX(double aEndX)
675 logicFunction->setEndX(aEndX);
679 double pGraphicalFunction::getEndX() {
680 return logicFunction->getEndX();
683 void pGraphicalFunction::setEndY(double aEndY) {
684 logicFunction->setEndY(aEndY);
688 double pGraphicalFunction::getEndY() {
689 return logicFunction->getEndY();
694 void pGraphicalFunction::setScaleX(double aScaleX)
696 logicFunction->setScaleX(aScaleX);
697 this->_scaleX = aScaleX;
700 double pGraphicalFunction::getScaleX()
702 return this->_scaleX;
705 void pGraphicalFunction::setScaleY(double aScaleY)
707 logicFunction->setScaleY(aScaleY);
708 this->_scaleY = aScaleY;
711 double pGraphicalFunction::getScaleY() {
712 return this->_scaleY;
717 void pGraphicalFunction::setMinX(double aMinX)
719 logicFunction->setMinX(aMinX);
723 double pGraphicalFunction::getMinX()
725 return logicFunction->getMinX();
728 void pGraphicalFunction::setMinY(double aMinY)
730 logicFunction->setMinY(aMinY);
734 double pGraphicalFunction::getMinY() {
735 return logicFunction->getMinY();
740 void pGraphicalFunction::setMaxX(double aMaxX)
742 logicFunction->setMaxX(aMaxX);
746 double pGraphicalFunction::getMaxX()
748 return logicFunction->getMaxX();
751 void pGraphicalFunction::setMaxY(double aMaxY)
753 logicFunction->setMaxY(aMaxY);
757 double pGraphicalFunction::getMaxY() {
758 return logicFunction->getMaxY();
764 void pGraphicalFunction::setOffsetX(double aOffsetX)
766 logicFunction->setOffsetX(aOffsetX);
767 this->_offsetX = aOffsetX;
770 double pGraphicalFunction:: getOffsetX()
773 return this->_offsetX;
776 void pGraphicalFunction:: setOffsetY(double aOffsetY)
778 logicFunction->setOffsetY(aOffsetY);
779 this->_offsetY = aOffsetY;
782 double pGraphicalFunction:: getOffsetY() {
783 return this->_offsetY;
789 void pGraphicalFunction:: setType(int aType)
791 logicFunction->setType(aType);
795 int pGraphicalFunction :: getType() {
802 int pGraphicalFunction :: getValidPointRange()
804 return validPointRange;
807 void pGraphicalFunction :: setValidPointRange(int theRange)
809 logicFunction->setValidPointRange(theRange);
810 validPointRange = theRange;
813 * Returns the number of points that the function
816 int pGraphicalFunction :: getSizePoints()
818 return logicFunction->getSizePoints();
822 * Sets the color points of the function by teh given parameter
823 * @param colorVector Is the color points vector to set
825 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
827 f_colorPoints.clear();
829 while(i<(int)(colorVector.size()))
831 f_colorPoints.push_back(colorVector[i]);
837 * Gets the color points of the function in the given parameter
838 * @param colorVector Is the color points list to get the points
840 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
843 while(i<(int)(f_colorPoints.size()))
845 pColorPoint * originaslPoint = f_colorPoints[i];
846 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
847 colorVector.push_back(copyPoint);