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);
291 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
292 int beforeX=before1->getRealX();
293 int pointX=point->getRealX();
296 int minDiference1=pointX-beforeX;
297 if(minDiference1<minDiference)
299 minDiference=minDiference1;
303 node=node->GetNext();
308 * Returns true if the point is in the line
310 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
314 x1=(float)before->getRealX();
315 x2=(float)next->getRealX();
316 y1=(float)before->getRealY();
317 y2=(float)next->getRealY();
318 x=(float)point->getRealX();
319 y=(float)point->getRealY();
322 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
323 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
324 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
325 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
330 * give us the value of y in the line define by the arguments
332 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
337 //----------------------------
339 //----------------------------
341 //----------------------------
343 //----------------------------
347 * clear the spline vectors
349 void pGraphicalFunction:: clearSplineVectors()
356 Initiliaze xSpline and ySpline vectors
358 void pGraphicalFunction:: initializeSplines()
361 xKSpline = vtkKochanekSpline::New();
363 yKSpline= vtkKochanekSpline::New();
369 Add the (x,y) points of the function to the
372 void pGraphicalFunction::addSplinesPoints()
374 xKSpline->RemoveAllPoints();
375 yKSpline->RemoveAllPoints();
376 wxNode *node= logicFunction->GetPointAt(0);
381 p=(pFunctionPoint*)node->GetData();
382 xKSpline->AddPoint(i,p->getRealX());
383 yKSpline->AddPoint(i,p->getRealY());
385 node=node->GetNext();
389 This Method adds the point calculated by
390 the splines to the vectors
392 void pGraphicalFunction:: initializeSplineVectors()
394 double x=0,y=0,t,delta;
398 np = logicFunction->getSizePoints();
400 delta=( double ) ( np ) / ( double ) ( nps );
401 for( i = 0; i < nps; i++ )
403 t = delta * (double)i;
404 GetSplinePoint(t,x,y);
405 xSpline.push_back(x);
406 ySpline.push_back(y);
410 get the spline point for t, in xKSpline and yKSpline
412 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
414 if (logicFunction->getSizePoints()==0)
419 if (logicFunction->getSizePoints()>=2)
421 x = xKSpline->Evaluate(t);
422 y = yKSpline->Evaluate(t);
426 //------------------------------
428 //------------------------------
431 This method set the initial point and the final point of drawing
432 according with the width of the square around the point clicked
435 @param width: width of the square
436 Note: all the parameters are pixels
437 Pre: screenX, screenY, zoom are set! with the actual data.
440 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
442 int xS,yS,x1,x2,y1,y2;
444 xS=clickedX/_scaleX+_offsetX;
445 yS=clickedY/_scaleY+_offsetY;
447 x1=(clickedX-width)/_scaleX+_offsetX;
448 x2=(clickedX+width)/_scaleX+_offsetX;
449 y1=(clickedY-width)/_scaleY+_offsetY;
450 y2=(clickedY+width)/_scaleY+_offsetY;
452 int numberPoints=getSizePoints();
453 pFunctionPoint* point;
455 bool inSquare=false,first=false;
457 //if the user made a zoom
461 for(i=0; i<numberPoints;i++)
463 p=logicFunction->GetPointAt(i);
464 point=(pFunctionPoint*)p->GetData();
465 int rx=point->getRealX();
466 int ry=point->getRealY();
467 inSquare= rx>=x1 && rx<=x2; //&& ry>=y1 && ry<=y2;
472 initialDrawingPoint=point;
477 finalDrawingPoint=point;
483 //We extend the range of vision in x and y,20 and 10 respectively
486 _offsetX=_offsetX-20;
492 _offsetY=_offsetY-10;
500 This method sets the offsets according with the point
501 clicked on the window
502 @param offx: x-value of the point clicked
503 @param offx: y-value of the point clicked
504 Note: all the parameters are pixels
505 Pre: screenX, screenY are set! with the actual data.
506 Pos: Sets the value of minXshowed,minYshowed
508 void pGraphicalFunction::setOffsets(int offx,int offy)
514 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
515 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
523 setOffsetPixelX(offx);
524 setOffsetPixelY(offy);
528 * This method sets the minShowedX
529 * and the minShowedY, accordig to the offset in
530 * x-axis and y-axis respectively
531 * pre: _offsetX>=0,_offsetY>=0
532 * screenX and screenY are set with the actual size of the window
533 * scaleX and scaleY are set to the actual size of the window
536 void pGraphicalFunction::setMinShowed()
538 bool firstX=false,firstY=false;
540 wxNode* node=logicFunction->GetPointAt(0);
543 //float middleWX=(((float)(screenX-100))/2)/scaleX; // JPRx
544 //float middleWY=(((float)(screenY-50))/2)/scaleY; // JPRx
546 //node=node->GetNext();
549 p=(pFunctionPoint*)node->GetData();
550 int px=p->getRealX();
551 int py=p->getRealY();
552 int x=(px-_offsetX);//+middleWX;
553 int y=(py-_offsetY);//+middleWY;
564 node=node->GetNext();
568 //-----------------------
570 //-----------------------
573 Save the points of the function
575 void pGraphicalFunction::save(wxString fileName)
578 logicFunction->save(fileName);
582 Load the points of a function
584 void pGraphicalFunction::load(wxString fileName)
586 logicFunction->load(fileName);
587 logicFunction->setUp();
588 setMaxShowedX(logicFunction->getMaxX());
589 setMinShowedX(logicFunction->getMinX());
590 setMaxShowedY(logicFunction->getMaxY());
591 setMinShowedY(logicFunction->getMinY());
594 //--------------------------
595 //GETTERS AND SETTERS
596 //---------------------------
600 void pGraphicalFunction::setActual(bool act)
604 bool pGraphicalFunction:: getActual()
614 void pGraphicalFunction::setScaleWay(int typeS)
619 int pGraphicalFunction:: getScaleWay()
628 void pGraphicalFunction::setScreenX(int scrX)
632 int pGraphicalFunction::getScreenX()
634 return this->screenX;
636 void pGraphicalFunction::setScreenY(int scrY)
640 int pGraphicalFunction::getScreenY()
642 return this->screenY;
649 void pGraphicalFunction::setStartX(double aStartX) {
651 logicFunction->setStartX(aStartX);
652 this->_startX =logicFunction->getStartX();
655 double pGraphicalFunction::getStartX()
657 return logicFunction->getStartX();
660 void pGraphicalFunction::setStartY(double aStartY)
662 logicFunction->setStartY(aStartY);
663 this->_startY = aStartY;
666 double pGraphicalFunction::getStartY() {
667 return logicFunction->getStartY();
672 void pGraphicalFunction::setEndX(double aEndX)
674 logicFunction->setEndX(aEndX);
678 double pGraphicalFunction::getEndX() {
679 return logicFunction->getEndX();
682 void pGraphicalFunction::setEndY(double aEndY) {
683 logicFunction->setEndY(aEndY);
687 double pGraphicalFunction::getEndY() {
688 return logicFunction->getEndY();
693 void pGraphicalFunction::setScaleX(double aScaleX)
695 logicFunction->setScaleX(aScaleX);
696 this->_scaleX = aScaleX;
699 double pGraphicalFunction::getScaleX()
701 return this->_scaleX;
704 void pGraphicalFunction::setScaleY(double aScaleY)
706 logicFunction->setScaleY(aScaleY);
707 this->_scaleY = aScaleY;
710 double pGraphicalFunction::getScaleY() {
711 return this->_scaleY;
716 void pGraphicalFunction::setMinX(double aMinX)
718 logicFunction->setMinX(aMinX);
722 double pGraphicalFunction::getMinX()
724 return logicFunction->getMinX();
727 void pGraphicalFunction::setMinY(double aMinY)
729 logicFunction->setMinY(aMinY);
733 double pGraphicalFunction::getMinY() {
734 return logicFunction->getMinY();
739 void pGraphicalFunction::setMaxX(double aMaxX)
741 logicFunction->setMaxX(aMaxX);
745 double pGraphicalFunction::getMaxX()
747 return logicFunction->getMaxX();
750 void pGraphicalFunction::setMaxY(double aMaxY)
752 logicFunction->setMaxY(aMaxY);
756 double pGraphicalFunction::getMaxY() {
757 return logicFunction->getMaxY();
763 void pGraphicalFunction::setOffsetX(double aOffsetX)
765 logicFunction->setOffsetX(aOffsetX);
766 this->_offsetX = aOffsetX;
769 double pGraphicalFunction:: getOffsetX()
772 return this->_offsetX;
775 void pGraphicalFunction:: setOffsetY(double aOffsetY)
777 logicFunction->setOffsetY(aOffsetY);
778 this->_offsetY = aOffsetY;
781 double pGraphicalFunction:: getOffsetY() {
782 return this->_offsetY;
788 void pGraphicalFunction:: setType(int aType)
790 logicFunction->setType(aType);
794 int pGraphicalFunction :: getType() {
801 int pGraphicalFunction :: getValidPointRange()
803 return validPointRange;
806 void pGraphicalFunction :: setValidPointRange(int theRange)
808 logicFunction->setValidPointRange(theRange);
809 validPointRange = theRange;
812 * Returns the number of points that the function
815 int pGraphicalFunction :: getSizePoints()
817 return logicFunction->getSizePoints();
821 * Sets the color points of the function by teh given parameter
822 * @param colorVector Is the color points vector to set
824 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
826 f_colorPoints.clear();
828 while(i<(int)(colorVector.size()))
830 f_colorPoints.push_back(colorVector[i]);
836 * Gets the color points of the function in the given parameter
837 * @param colorVector Is the color points list to get the points
839 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
842 while(i<(int)(f_colorPoints.size()))
844 pColorPoint * originaslPoint = f_colorPoints[i];
845 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
846 colorVector.push_back(copyPoint);