1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
27 //----------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------
30 #include "pGraphicalFunction.h"
37 // ----------------------------------------------------------------------------
38 // WX headers inclusion.
39 // For compilers that support precompilation, includes <wx/wx.h>.
40 // ----------------------------------------------------------------------------
46 //----------------------------------------------------------------------------
47 // Class implementation
48 //----------------------------------------------------------------------------
50 IMPLEMENT_CLASS(pGraphicalFunction, pPlotterLayer)
52 pGraphicalFunction:: pGraphicalFunction(wxString name, int flags)
57 logicFunction = new pLogicalFunction ();
66 initialDrawingPoint = NULL;
67 finalDrawingPoint = NULL;
68 //type=1 means that is going to be a piecewise function
80 * Is the destructor!!!
82 pGraphicalFunction :: ~ pGraphicalFunction ()
87 //set if the function has to draw the points
88 void pGraphicalFunction::SetShowPoints(bool showPoints)
90 showPointsF = showPoints;
92 //get the paramater showPointsF
93 bool pGraphicalFunction::getShowPoints()
98 * Set Up startPoint, endPoint, maxY,maxX points
100 void pGraphicalFunction::setUp()
102 logicFunction->setUp();
103 setMaxX(logicFunction->getMaxX());
104 setMinX(logicFunction->getMinX());
105 setMaxY(logicFunction->getMaxY());
106 setMinY(logicFunction->getMinY());
107 setStartX(logicFunction->getStartX());
108 setEndX(logicFunction->getEndX());
109 setStartY(logicFunction->getStartY());
110 setEndY(logicFunction->getEndY());
114 * 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
115 * x1-validPointRange<x<x1+validPointRange y1-validPointRange<y<y1+validPointRange
117 int pGraphicalFunction::validPointOnFunction(wxPoint realPoint)
119 return (logicFunction -> validPointOnFunction (realPoint));
122 * returns the index in the list of the point
124 int pGraphicalFunction::getIndexOf(wxPoint realpoint)
126 return logicFunction -> getIndexOf( realpoint );
130 * This metohd returns the node of the point that is the movingPointIndex position in the list of points.
131 * @param: int movingPointIndex, Is the index value of the searched node.
132 * @return: Return a pointer to the node corresponding to the index value by parameter.
134 wxNode* pGraphicalFunction::GetPointAt( int movingPointIndex )
136 return logicFunction -> GetPointAt ( movingPointIndex );
139 * Set the scales of the function in x and y
142 void pGraphicalFunction::setScales()
144 //int dx= logicFunction->getMaxX()-logicFunction->getMinX();
145 //int dy= logicFunction->getMaxY()-logicFunction->getMinY();
146 int dx= maxShowedX-minShowedX;
147 int dy= maxShowedY-minShowedY;
150 double scaleX=(((float)(screenX-100))/dx)*factorZoom;
151 double scaleY=(((float)(screenY-50))/dy)*factorZoom;
154 logicFunction->setScaleX(scaleX);
155 logicFunction->setScaleY(scaleY);
159 * Includes a point between two existing points of this function. The new point (x,y) was not defined when the function was created.
160 * @return Returns true is the point was succcesfully added to the funcion.
162 bool pGraphicalFunction:: AddNewPoint(int x,int y)
164 bool added= logicFunction -> AddNewPoint ( x, y );
169 logicFunction->setEndPoints();
170 logicFunction->setStartPoints();
176 * This tries to Add a point to the function if possible it returns true.
177 * @pre: The list of existing points is ordered.
178 * @param: int aX, The x value of the point.
179 * @param: int aY, The y value of the point.
180 * @param: return Returns TRUE if the point was succesfuly included in the realPoints list.
182 bool pGraphicalFunction::AddPoint(int aX, int aY,bool order)
185 if (logicFunction!=NULL){
186 added=logicFunction -> AddPoint( aX, aY,order );
191 logicFunction->setEndPoints();
192 logicFunction->setStartPoints();
194 } // if logicFunction
199 * deletes the point given by the user from the collection of logical points of the function
203 bool pGraphicalFunction::DeletePoint(int aX, int aY)
205 bool added= logicFunction -> DeletePoint( aX, aY );
210 logicFunction->setEndPoints();
211 logicFunction->setStartPoints();
217 * deletes a point of the functio given its index
219 bool pGraphicalFunction::deletePointAt(int index)
221 bool added=logicFunction -> deletePointAt( index );
226 logicFunction->setEndPoints();
227 logicFunction->setStartPoints();
233 * Change de coordinates of the given index point to the newCoords, if it is possible and return the result of the invoked action.
234 * @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.
236 bool pGraphicalFunction::changePoint(wxPoint newCoords, int movingIndexPoint)
238 bool added= (logicFunction -> changePoint( newCoords, movingIndexPoint ));
243 logicFunction->setEndPoints();
244 logicFunction->setStartPoints();
249 * Evaluates if the given point belongs to the function.
252 bool pGraphicalFunction::hasPoint(wxPoint aPoint)
254 return logicFunction -> hasPoint( aPoint );
259 * Returns the real x values of the control points of the function. It dont includes the points calculated by interpolation.
261 double* pGraphicalFunction::getX_RealValues()
263 return (logicFunction -> getX_RealValues( ));
267 * Returns the real y values of the control points of the function. It dont includes the points calculated by interpolation.
269 double * pGraphicalFunction::getY_RealValues()
272 return (logicFunction -> getY_RealValues());
275 //----------------------------------
276 // Asking if it has a point (x,y)
277 //----------------------------------
280 * returns true if the point is along the function
283 bool pGraphicalFunction:: isInFunction(int x, int y)
287 pFunctionPoint* point= new pFunctionPoint(x,y);
289 before=getBefore(point);
290 if(before && before->GetNext())
292 next = before->GetNext();
293 //next = (before->GetNext())->GetNext();
294 inLine= isInLine((pFunctionPoint*)before->GetData(),(pFunctionPoint*)next->GetData(),point);
301 * give us the point that is in the function and is exactly before
302 * the point we are giving
304 wxNode* pGraphicalFunction:: getBefore(pFunctionPoint* point)
306 int minDiference=10000000;
307 wxNode* node=logicFunction->GetPointAt(0);
312 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
313 int beforeX=before1->getRealX();
314 int pointX=point->getRealX();
317 int minDiference1=pointX-beforeX;
318 if(minDiference1<minDiference)
320 minDiference=minDiference1;
324 node=node->GetNext();
329 * Returns true if the point is in the line
331 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
335 x1=(float)before->getRealX();
336 x2=(float)next->getRealX();
337 y1=(float)before->getRealY();
338 y2=(float)next->getRealY();
339 x=(float)point->getRealX();
340 y=(float)point->getRealY();
342 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
343 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
344 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
345 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
350 * give us the value of y in the line define by the arguments
352 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
357 //----------------------------
359 //----------------------------
361 //----------------------------
363 //----------------------------
367 * clear the spline vectors
369 void pGraphicalFunction:: clearSplineVectors()
376 Initiliaze xSpline and ySpline vectors
378 void pGraphicalFunction:: initializeSplines()
381 xKSpline = vtkKochanekSpline::New();
383 yKSpline= vtkKochanekSpline::New();
389 Add the (x,y) points of the function to the
392 void pGraphicalFunction::addSplinesPoints()
394 xKSpline->RemoveAllPoints();
395 yKSpline->RemoveAllPoints();
396 wxNode *node= logicFunction->GetPointAt(0);
401 p=(pFunctionPoint*)node->GetData();
402 xKSpline->AddPoint(i,p->getRealX());
403 yKSpline->AddPoint(i,p->getRealY());
405 node=node->GetNext();
409 This Method adds the point calculated by
410 the splines to the vectors
412 void pGraphicalFunction:: initializeSplineVectors()
414 double x=0,y=0,t,delta;
418 np = logicFunction->getSizePoints();
420 delta=( double ) ( np ) / ( double ) ( nps );
421 for( i = 0; i < nps; i++ )
423 t = delta * (double)i;
424 GetSplinePoint(t,x,y);
425 xSpline.push_back(x);
426 ySpline.push_back(y);
430 get the spline point for t, in xKSpline and yKSpline
432 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
434 if (logicFunction->getSizePoints()==0)
439 if (logicFunction->getSizePoints()>=2)
441 x = xKSpline->Evaluate(t);
442 y = yKSpline->Evaluate(t);
446 //------------------------------
448 //------------------------------
451 This method set the initial point and the final point of drawing
452 according with the width of the square around the point clicked
455 @param width: width of the square
456 Note: all the parameters are pixels
457 Pre: screenX, screenY, zoom are set! with the actual data.
460 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
462 int xS,yS,x1,x2,y1,y2;
464 xS = clickedX/_scaleX+_offsetX;
465 yS = clickedY/_scaleY+_offsetY;
467 x1 = (clickedX-width)/_scaleX+_offsetX;
468 x2 = (clickedX+width)/_scaleX+_offsetX;
469 y1 = (clickedY-width)/_scaleY+_offsetY;
470 y2 = (clickedY+width)/_scaleY+_offsetY;
472 int numberPoints = getSizePoints();
473 pFunctionPoint* point;
475 bool inSquare=false,first=false;
477 //if the user made a zoom
481 for(i=0; i<numberPoints;i++)
483 p=logicFunction->GetPointAt(i);
484 point=(pFunctionPoint*)p->GetData();
485 int rx=point->getRealX();
486 int ry=point->getRealY();
487 inSquare= rx>=x1 && rx<=x2; //&& ry>=y1 && ry<=y2;
492 initialDrawingPoint=point;
497 finalDrawingPoint=point;
501 //We extend the range of vision in x and y,20 and 10 respectively
504 _offsetX=_offsetX-20;
510 _offsetY=_offsetY-10;
518 This method sets the offsets according with the point
519 clicked on the window
520 @param offx: x-value of the point clicked
521 @param offx: y-value of the point clicked
522 Note: all the parameters are pixels
523 Pre: screenX, screenY are set! with the actual data.
524 Pos: Sets the value of minXshowed,minYshowed
526 void pGraphicalFunction::setOffsets(int offx,int offy)
532 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
533 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
541 setOffsetPixelX(offx);
542 setOffsetPixelY(offy);
546 * This method sets the minShowedX
547 * and the minShowedY, accordig to the offset in
548 * x-axis and y-axis respectively
549 * pre: _offsetX>=0,_offsetY>=0
550 * screenX and screenY are set with the actual size of the window
551 * scaleX and scaleY are set to the actual size of the window
554 void pGraphicalFunction::setMinShowed()
556 bool firstX=false,firstY=false;
558 wxNode* node=logicFunction->GetPointAt(0);
561 //float middleWX=(((float)(screenX-100))/2)/scaleX; // JPRx
562 //float middleWY=(((float)(screenY-50))/2)/scaleY; // JPRx
564 //node=node->GetNext();
567 p=(pFunctionPoint*)node->GetData();
568 int px=p->getRealX();
569 int py=p->getRealY();
570 int x=(px-_offsetX);//+middleWX;
571 int y=(py-_offsetY);//+middleWY;
582 node=node->GetNext();
586 //-----------------------
588 //-----------------------
591 Save the points of the function
593 void pGraphicalFunction::save(wxString fileName)
596 logicFunction->save(fileName);
600 Load the points of a function
602 void pGraphicalFunction::load(wxString fileName)
604 logicFunction->load(fileName);
605 logicFunction->setUp();
606 setMaxShowedX(logicFunction->getMaxX());
607 setMinShowedX(logicFunction->getMinX());
608 setMaxShowedY(logicFunction->getMaxY());
609 setMinShowedY(logicFunction->getMinY());
612 //--------------------------
613 //GETTERS AND SETTERS
614 //---------------------------
618 void pGraphicalFunction::setActual(bool act)
622 bool pGraphicalFunction:: getActual()
632 void pGraphicalFunction::setScaleWay(int typeS)
637 int pGraphicalFunction:: getScaleWay()
646 void pGraphicalFunction::setScreenX(int scrX)
650 int pGraphicalFunction::getScreenX()
652 return this->screenX;
654 void pGraphicalFunction::setScreenY(int scrY)
658 int pGraphicalFunction::getScreenY()
660 return this->screenY;
667 void pGraphicalFunction::setStartX(double aStartX) {
669 logicFunction->setStartX(aStartX);
670 this->_startX =logicFunction->getStartX();
673 double pGraphicalFunction::getStartX()
675 return logicFunction->getStartX();
678 void pGraphicalFunction::setStartY(double aStartY)
680 logicFunction->setStartY(aStartY);
681 this->_startY = aStartY;
684 double pGraphicalFunction::getStartY() {
685 return logicFunction->getStartY();
690 void pGraphicalFunction::setEndX(double aEndX)
692 logicFunction->setEndX(aEndX);
696 double pGraphicalFunction::getEndX() {
697 return logicFunction->getEndX();
700 void pGraphicalFunction::setEndY(double aEndY) {
701 logicFunction->setEndY(aEndY);
705 double pGraphicalFunction::getEndY() {
706 return logicFunction->getEndY();
711 void pGraphicalFunction::setScaleX(double aScaleX)
713 logicFunction->setScaleX(aScaleX);
714 this->_scaleX = aScaleX;
717 double pGraphicalFunction::getScaleX()
719 return this->_scaleX;
722 void pGraphicalFunction::setScaleY(double aScaleY)
724 logicFunction->setScaleY(aScaleY);
725 this->_scaleY = aScaleY;
728 double pGraphicalFunction::getScaleY() {
729 return this->_scaleY;
734 void pGraphicalFunction::setMinX(double aMinX)
736 logicFunction->setMinX(aMinX);
740 double pGraphicalFunction::getMinX()
742 return logicFunction->getMinX();
745 void pGraphicalFunction::setMinY(double aMinY)
747 logicFunction->setMinY(aMinY);
751 double pGraphicalFunction::getMinY() {
752 return logicFunction->getMinY();
757 void pGraphicalFunction::setMaxX(double aMaxX)
759 logicFunction->setMaxX(aMaxX);
763 double pGraphicalFunction::getMaxX()
765 return logicFunction->getMaxX();
768 void pGraphicalFunction::setMaxY(double aMaxY)
770 logicFunction->setMaxY(aMaxY);
774 double pGraphicalFunction::getMaxY() {
775 return logicFunction->getMaxY();
781 void pGraphicalFunction::setOffsetX(double aOffsetX)
783 logicFunction->setOffsetX(aOffsetX);
784 this->_offsetX = aOffsetX;
787 double pGraphicalFunction:: getOffsetX()
790 return this->_offsetX;
793 void pGraphicalFunction:: setOffsetY(double aOffsetY)
795 logicFunction->setOffsetY(aOffsetY);
796 this->_offsetY = aOffsetY;
799 double pGraphicalFunction:: getOffsetY() {
800 return this->_offsetY;
806 void pGraphicalFunction:: setType(int aType)
808 logicFunction->setType(aType);
812 int pGraphicalFunction :: getType() {
819 int pGraphicalFunction :: getValidPointRange()
821 return validPointRange;
824 void pGraphicalFunction :: setValidPointRange(int theRange)
826 logicFunction->setValidPointRange(theRange);
827 validPointRange = theRange;
830 * Returns the number of points that the function
833 int pGraphicalFunction :: getSizePoints()
835 return logicFunction->getSizePoints();
839 * Sets the color points of the function by teh given parameter
840 * @param colorVector Is the color points vector to set
842 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
844 f_colorPoints.clear();
846 while(i<(int)(colorVector.size()))
848 f_colorPoints.push_back(colorVector[i]);
854 * Gets the color points of the function in the given parameter
855 * @param colorVector Is the color points list to get the points
857 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
860 while(i<(int)(f_colorPoints.size()))
862 pColorPoint * originaslPoint = f_colorPoints[i];
863 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
864 colorVector.push_back(copyPoint);