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());
115 * 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
116 * x1-validPointRange<x<x1+validPointRange y1-validPointRange<y<y1+validPointRange
118 int pGraphicalFunction::validPointOnFunction(wxPoint realPoint)
120 return (logicFunction -> validPointOnFunction (realPoint));
123 * returns the index in the list of the point
125 int pGraphicalFunction::getIndexOf(wxPoint realpoint)
127 return logicFunction -> getIndexOf( realpoint );
131 * This metohd returns the node of the point that is the movingPointIndex position in the list of points.
132 * @param: int movingPointIndex, Is the index value of the searched node.
133 * @return: Return a pointer to the node corresponding to the index value by parameter.
135 wxNode* pGraphicalFunction::GetPointAt( int movingPointIndex )
137 return logicFunction -> GetPointAt ( movingPointIndex );
140 * Set the scales of the function in x and y
143 void pGraphicalFunction::setScales()
145 //int dx= logicFunction->getMaxX()-logicFunction->getMinX();
146 //int dy= logicFunction->getMaxY()-logicFunction->getMinY();
147 int dx= maxShowedX-minShowedX;
148 int dy= maxShowedY-minShowedY;
153 double scaleX=(((float)(screenX-100))/dx)*factorZoom;
154 double scaleY=(((float)(screenY-50))/dy)*factorZoom;
157 logicFunction->setScaleX(scaleX);
158 logicFunction->setScaleY(scaleY);
163 * Includes a point between two existing points of this function. The new point (x,y) was not defined when the function was created.
164 * @return Returns true is the point was succcesfully added to the funcion.
166 bool pGraphicalFunction:: AddNewPoint(int x,int y)
168 bool added= logicFunction -> AddNewPoint ( x, y );
173 logicFunction->setEndPoints();
174 logicFunction->setStartPoints();
180 * This tries to Add a point to the function if possible it returns true.
181 * @pre: The list of existing points is ordered.
182 * @param: int aX, The x value of the point.
183 * @param: int aY, The y value of the point.
184 * @param: return Returns TRUE if the point was succesfuly included in the realPoints list.
186 bool pGraphicalFunction::AddPoint(int aX, int aY,bool order)
189 if (logicFunction!=NULL){
190 added=logicFunction -> AddPoint( aX, aY,order );
195 logicFunction->setEndPoints();
196 logicFunction->setStartPoints();
198 } // if logicFunction
203 * deletes the point given by the user from the collection of logical points of the function
207 bool pGraphicalFunction::DeletePoint(int aX, int aY)
209 bool added= logicFunction -> DeletePoint( aX, aY );
214 logicFunction->setEndPoints();
215 logicFunction->setStartPoints();
221 * deletes a point of the functio given its index
223 bool pGraphicalFunction::deletePointAt(int index)
225 bool added=logicFunction -> deletePointAt( index );
230 logicFunction->setEndPoints();
231 logicFunction->setStartPoints();
237 * Change de coordinates of the given index point to the newCoords, if it is possible and return the result of the invoked action.
238 * @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.
240 bool pGraphicalFunction::changePoint(wxPoint newCoords, int movingIndexPoint)
242 bool added= (logicFunction -> changePoint( newCoords, movingIndexPoint ));
247 logicFunction->setEndPoints();
248 logicFunction->setStartPoints();
253 * Evaluates if the given point belongs to the function.
256 bool pGraphicalFunction::hasPoint(wxPoint aPoint)
258 return logicFunction -> hasPoint( aPoint );
263 * Returns the real x values of the control points of the function. It dont includes the points calculated by interpolation.
265 double* pGraphicalFunction::getX_RealValues()
267 return (logicFunction -> getX_RealValues( ));
271 * Returns the real y values of the control points of the function. It dont includes the points calculated by interpolation.
273 double * pGraphicalFunction::getY_RealValues()
276 return (logicFunction -> getY_RealValues());
279 //----------------------------------
280 // Asking if it has a point (x,y)
281 //----------------------------------
284 * returns true if the point is along the function
287 bool pGraphicalFunction:: isInFunction(int x, int y)
291 pFunctionPoint* point= new pFunctionPoint(x,y);
293 before=getBefore(point);
295 if(before && before->GetNext())
297 next = before->GetNext();
298 //next = (before->GetNext())->GetNext();
299 inLine= isInLine((pFunctionPoint*)before->GetData(),(pFunctionPoint*)next->GetData(),point);
306 * give us the point that is in the function and is exactly before
307 * the point we are giving
309 wxNode* pGraphicalFunction:: getBefore(pFunctionPoint* point)
311 int minDiference=10000000;
312 wxNode* node=logicFunction->GetPointAt(0);
317 pFunctionPoint* before1=(pFunctionPoint*)node->GetData();
318 int beforeX=before1->getRealX();
319 int pointX=point->getRealX();
322 int minDiference1=pointX-beforeX;
323 if(minDiference1<minDiference)
325 minDiference=minDiference1;
329 node=node->GetNext();
334 * Returns true if the point is in the line
336 bool pGraphicalFunction::isInLine(pFunctionPoint* before,pFunctionPoint* next, pFunctionPoint* point)
340 x1=(float)before->getRealX();
341 x2=(float)next->getRealX();
342 y1=(float)before->getRealY();
343 y2=(float)next->getRealY();
344 x=(float)point->getRealX();
345 y=(float)point->getRealY();
348 float d1= (float)sqrt(pow(float(x1-x),2)+ pow(float(y1-y),2));
349 float d2= (float)sqrt(pow(float(x2-x),2)+ pow(float(y2-y),2));
350 float d=(float)sqrt(pow(float(x2-x1),2)+ pow(float(y2-y1),2));
351 if(d1+d2<=(d*1.1) && d1<=d && d2<=d)
356 * give us the value of y in the line define by the arguments
358 double pGraphicalFunction::interpolateY(double m, int x1,int y1,int x)
363 //----------------------------
365 //----------------------------
367 //----------------------------
369 //----------------------------
373 * clear the spline vectors
375 void pGraphicalFunction:: clearSplineVectors()
382 Initiliaze xSpline and ySpline vectors
384 void pGraphicalFunction:: initializeSplines()
387 xKSpline = vtkKochanekSpline::New();
389 yKSpline= vtkKochanekSpline::New();
395 Add the (x,y) points of the function to the
398 void pGraphicalFunction::addSplinesPoints()
400 xKSpline->RemoveAllPoints();
401 yKSpline->RemoveAllPoints();
402 wxNode *node= logicFunction->GetPointAt(0);
407 p=(pFunctionPoint*)node->GetData();
408 xKSpline->AddPoint(i,p->getRealX());
409 yKSpline->AddPoint(i,p->getRealY());
411 node=node->GetNext();
415 This Method adds the point calculated by
416 the splines to the vectors
418 void pGraphicalFunction:: initializeSplineVectors()
420 double x=0,y=0,t,delta;
424 np = logicFunction->getSizePoints();
426 delta=( double ) ( np ) / ( double ) ( nps );
427 for( i = 0; i < nps; i++ )
429 t = delta * (double)i;
430 GetSplinePoint(t,x,y);
431 xSpline.push_back(x);
432 ySpline.push_back(y);
436 get the spline point for t, in xKSpline and yKSpline
438 void pGraphicalFunction::GetSplinePoint(double t, double &x, double &y)
440 if (logicFunction->getSizePoints()==0)
445 if (logicFunction->getSizePoints()>=2)
447 x = xKSpline->Evaluate(t);
448 y = yKSpline->Evaluate(t);
452 //------------------------------
454 //------------------------------
457 This method set the initial point and the final point of drawing
458 according with the width of the square around the point clicked
461 @param width: width of the square
462 Note: all the parameters are pixels
463 Pre: screenX, screenY, zoom are set! with the actual data.
466 void pGraphicalFunction::zooming(int clickedX,int clickedY,int width)
468 int xS,yS,x1,x2,y1,y2;
470 xS=clickedX/_scaleX+_offsetX;
471 yS=clickedY/_scaleY+_offsetY;
473 x1=(clickedX-width)/_scaleX+_offsetX;
474 x2=(clickedX+width)/_scaleX+_offsetX;
475 y1=(clickedY-width)/_scaleY+_offsetY;
476 y2=(clickedY+width)/_scaleY+_offsetY;
478 int numberPoints=getSizePoints();
479 pFunctionPoint* point;
481 bool inSquare=false,first=false;
483 //if the user made a zoom
487 for(i=0; i<numberPoints;i++)
489 p=logicFunction->GetPointAt(i);
490 point=(pFunctionPoint*)p->GetData();
491 int rx=point->getRealX();
492 int ry=point->getRealY();
493 inSquare= rx>=x1 && rx<=x2; //&& ry>=y1 && ry<=y2;
498 initialDrawingPoint=point;
503 finalDrawingPoint=point;
509 //We extend the range of vision in x and y,20 and 10 respectively
512 _offsetX=_offsetX-20;
518 _offsetY=_offsetY-10;
526 This method sets the offsets according with the point
527 clicked on the window
528 @param offx: x-value of the point clicked
529 @param offx: y-value of the point clicked
530 Note: all the parameters are pixels
531 Pre: screenX, screenY are set! with the actual data.
532 Pos: Sets the value of minXshowed,minYshowed
534 void pGraphicalFunction::setOffsets(int offx,int offy)
540 xS=(offx-offsetPixelX)/_scaleX+_offsetX + minShowedX;
541 yS=(offy-offsetPixelY)/_scaleY+_offsetY+ minShowedY;
549 setOffsetPixelX(offx);
550 setOffsetPixelY(offy);
554 * This method sets the minShowedX
555 * and the minShowedY, accordig to the offset in
556 * x-axis and y-axis respectively
557 * pre: _offsetX>=0,_offsetY>=0
558 * screenX and screenY are set with the actual size of the window
559 * scaleX and scaleY are set to the actual size of the window
562 void pGraphicalFunction::setMinShowed()
564 bool firstX=false,firstY=false;
566 wxNode* node=logicFunction->GetPointAt(0);
569 //float middleWX=(((float)(screenX-100))/2)/scaleX; // JPRx
570 //float middleWY=(((float)(screenY-50))/2)/scaleY; // JPRx
572 //node=node->GetNext();
575 p=(pFunctionPoint*)node->GetData();
576 int px=p->getRealX();
577 int py=p->getRealY();
578 int x=(px-_offsetX);//+middleWX;
579 int y=(py-_offsetY);//+middleWY;
590 node=node->GetNext();
594 //-----------------------
596 //-----------------------
599 Save the points of the function
601 void pGraphicalFunction::save(wxString fileName)
604 logicFunction->save(fileName);
608 Load the points of a function
610 void pGraphicalFunction::load(wxString fileName)
612 logicFunction->load(fileName);
613 logicFunction->setUp();
614 setMaxShowedX(logicFunction->getMaxX());
615 setMinShowedX(logicFunction->getMinX());
616 setMaxShowedY(logicFunction->getMaxY());
617 setMinShowedY(logicFunction->getMinY());
620 //--------------------------
621 //GETTERS AND SETTERS
622 //---------------------------
626 void pGraphicalFunction::setActual(bool act)
630 bool pGraphicalFunction:: getActual()
640 void pGraphicalFunction::setScaleWay(int typeS)
645 int pGraphicalFunction:: getScaleWay()
654 void pGraphicalFunction::setScreenX(int scrX)
658 int pGraphicalFunction::getScreenX()
660 return this->screenX;
662 void pGraphicalFunction::setScreenY(int scrY)
666 int pGraphicalFunction::getScreenY()
668 return this->screenY;
675 void pGraphicalFunction::setStartX(double aStartX) {
677 logicFunction->setStartX(aStartX);
678 this->_startX =logicFunction->getStartX();
681 double pGraphicalFunction::getStartX()
683 return logicFunction->getStartX();
686 void pGraphicalFunction::setStartY(double aStartY)
688 logicFunction->setStartY(aStartY);
689 this->_startY = aStartY;
692 double pGraphicalFunction::getStartY() {
693 return logicFunction->getStartY();
698 void pGraphicalFunction::setEndX(double aEndX)
700 logicFunction->setEndX(aEndX);
704 double pGraphicalFunction::getEndX() {
705 return logicFunction->getEndX();
708 void pGraphicalFunction::setEndY(double aEndY) {
709 logicFunction->setEndY(aEndY);
713 double pGraphicalFunction::getEndY() {
714 return logicFunction->getEndY();
719 void pGraphicalFunction::setScaleX(double aScaleX)
721 logicFunction->setScaleX(aScaleX);
722 this->_scaleX = aScaleX;
725 double pGraphicalFunction::getScaleX()
727 return this->_scaleX;
730 void pGraphicalFunction::setScaleY(double aScaleY)
732 logicFunction->setScaleY(aScaleY);
733 this->_scaleY = aScaleY;
736 double pGraphicalFunction::getScaleY() {
737 return this->_scaleY;
742 void pGraphicalFunction::setMinX(double aMinX)
744 logicFunction->setMinX(aMinX);
748 double pGraphicalFunction::getMinX()
750 return logicFunction->getMinX();
753 void pGraphicalFunction::setMinY(double aMinY)
755 logicFunction->setMinY(aMinY);
759 double pGraphicalFunction::getMinY() {
760 return logicFunction->getMinY();
765 void pGraphicalFunction::setMaxX(double aMaxX)
767 logicFunction->setMaxX(aMaxX);
771 double pGraphicalFunction::getMaxX()
773 return logicFunction->getMaxX();
776 void pGraphicalFunction::setMaxY(double aMaxY)
778 logicFunction->setMaxY(aMaxY);
782 double pGraphicalFunction::getMaxY() {
783 return logicFunction->getMaxY();
789 void pGraphicalFunction::setOffsetX(double aOffsetX)
791 logicFunction->setOffsetX(aOffsetX);
792 this->_offsetX = aOffsetX;
795 double pGraphicalFunction:: getOffsetX()
798 return this->_offsetX;
801 void pGraphicalFunction:: setOffsetY(double aOffsetY)
803 logicFunction->setOffsetY(aOffsetY);
804 this->_offsetY = aOffsetY;
807 double pGraphicalFunction:: getOffsetY() {
808 return this->_offsetY;
814 void pGraphicalFunction:: setType(int aType)
816 logicFunction->setType(aType);
820 int pGraphicalFunction :: getType() {
827 int pGraphicalFunction :: getValidPointRange()
829 return validPointRange;
832 void pGraphicalFunction :: setValidPointRange(int theRange)
834 logicFunction->setValidPointRange(theRange);
835 validPointRange = theRange;
838 * Returns the number of points that the function
841 int pGraphicalFunction :: getSizePoints()
843 return logicFunction->getSizePoints();
847 * Sets the color points of the function by teh given parameter
848 * @param colorVector Is the color points vector to set
850 void pGraphicalFunction :: setColorPoints(std::vector<pColorPoint *> &colorVector)
852 f_colorPoints.clear();
854 while(i<(int)(colorVector.size()))
856 f_colorPoints.push_back(colorVector[i]);
862 * Gets the color points of the function in the given parameter
863 * @param colorVector Is the color points list to get the points
865 void pGraphicalFunction :: getColorPoints(std::vector<pColorPoint *> &colorVector)
868 while(i<(int)(f_colorPoints.size()))
870 pColorPoint * originaslPoint = f_colorPoints[i];
871 pColorPoint * copyPoint = new pColorPoint(originaslPoint->getRealX(), originaslPoint->getColor(), originaslPoint->isTemporalColor());
872 colorVector.push_back(copyPoint);