2 //----------------------------------------------------------------------------
3 // Class definition include
4 //----------------------------------------------------------------------------
5 #include "pLogicalFunction.h"
11 // ----------------------------------------------------------------------------
12 // WX headers inclusion.
13 // For compilers that support precompilation, includes <wx/wx.h>.
14 // ----------------------------------------------------------------------------
20 //----------------------------------------------------------------------------
21 // Class implementation
22 //----------------------------------------------------------------------------
24 IMPLEMENT_CLASS(pLogicalFunction, wxObject)
26 //----------------------------------------------------------------------------
28 //----------------------------------------------------------------------------
29 pLogicalFunction:: pLogicalFunction( )
32 realPoints.DeleteContents(TRUE);
34 leftToRigthDef = true;
41 * Is the destructor!!!
43 pLogicalFunction:: ~pLogicalFunction ()
45 realPoints.DeleteContents(TRUE);
48 * 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
49 * x1-validPointRange<x<x1+validPointRange y1-validPointRange<y<y1+validPointRange
51 int pLogicalFunction::validPointOnFunction(wxPoint realPoint)
54 wxNode* node= realPoints.GetFirst();
55 while(node && pointIndex==-1)
57 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
58 int x = point->getRealX();
59 int y = point->getRealY();
60 double vx=SENSIBLE_REGION/_scaleX;
61 double vy=SENSIBLE_REGION/_scaleY;
63 bool isInXRange= realPoint.x <= x + vx + SENSIBLE_REGION && realPoint.x >= x - vx-SENSIBLE_REGION;
64 bool isInYRange= realPoint.y <= y + vy + SENSIBLE_REGION && realPoint.y >= y - vy-SENSIBLE_REGION;
65 if(isInXRange && isInYRange)
66 pointIndex = realPoints.IndexOf(point);
68 node = node->GetNext();
76 * returns the index in the list of the point
78 int pLogicalFunction::getIndexOf(wxPoint realpoint)
80 wxNode* node= realPoints.GetFirst();
83 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
84 if(point->getRealX()==realpoint.x && point->getRealY()==realpoint.y )
86 return realPoints.IndexOf(point);
89 node= node->GetNext();
96 * This metohd returns the node of the point that is the movingPointIndex position in the list of points.
97 * @param: int movingPointIndex, Is the index value of the searched node.
98 * @return: Return a pointer to the node corresponding to the index value by parameter.
100 wxNode* pLogicalFunction::GetPointAt( int movingPointIndex )
102 if(!realPoints.IsEmpty())
104 wxNode* node= realPoints.Item(movingPointIndex);
112 * Includes a point between two existing points of this function. The new point (x,y) was not defined when the function was created.
113 * @return Returns true is the point was succcesfully added to the funcion.
115 bool pLogicalFunction:: AddNewPoint(int x,int y)
117 pFunctionPoint * newPoint = new pFunctionPoint ( x, y);
118 bool includedPoint = realPoints.Append(newPoint)!=NULL;
121 bool order=orderPoints();
124 return includedPoint;
127 * This method orders the list of points taking into account that the last appended node in the list (realPoint) is the only one disordered.
128 * @return Returns true if the last point included has a valid value for x and was ordered, according to the definition of function.
130 bool pLogicalFunction::orderPoints()
132 bool lastOrdered = false;
133 bool validToContinue = false;
135 wxNode* lastNodeIncluded = realPoints.GetLast();
137 wxNode* node = realPoints.GetFirst();
138 pFunctionPoint* lastPointIncluded = (pFunctionPoint*)lastNodeIncluded -> GetData();
139 int xToOrder = lastPointIncluded -> getRealX();
143 // Used for validating 'leftToRigthDef'
144 pFunctionPoint* ordFirstPoint = (pFunctionPoint*)realPoints.GetFirst()-> GetData();
145 pFunctionPoint* ordLastPoint = (pFunctionPoint*)(lastNodeIncluded->GetPrevious())-> GetData();
151 validToContinue = ordFirstPoint->getRealX() < xToOrder;
155 //validToContinue =ordFirstPoint->getRealX() > xToOrder;
156 validToContinue =ordLastPoint->getRealX() > xToOrder;
159 if ( validToContinue)
162 while( node && !lastOrdered )
164 pFunctionPoint* prevPoint =(pFunctionPoint*)node->GetData();
165 actualX = prevPoint ->getRealX();
166 if( actualX == xToOrder)
168 realPoints.DeleteNode(lastNodeIncluded);
171 else if ( actualX < xToOrder )
174 wxNode* nextNode = node->GetNext();
175 pFunctionPoint* nextPoint;
176 if( nextNode != lastNodeIncluded )
178 nextPoint = (pFunctionPoint*)nextNode -> GetData();
179 nextX = nextPoint->getRealX();
180 int nextIndex = realPoints.IndexOf(nextPoint);
181 if( nextX > xToOrder )
183 //we need to change the direction in memory of our node
184 pFunctionPoint* pointToInsert=new pFunctionPoint(lastPointIncluded->getRealX(),lastPointIncluded->getRealY());
185 // The last inserted point is ordered like: prevPoint - lastPointIncluded - nextPoint
186 lastOrdered = (realPoints.Insert(nextIndex, pointToInsert))!= NULL;
187 bool lastNodeDeleted=realPoints.DeleteNode(lastNodeIncluded);
188 return lastOrdered && lastNodeDeleted;
201 wxNode* prevNode = node->GetPrevious();
202 int insertIndex=realPoints.IndexOf(prevPoint);
203 pFunctionPoint* prPoint;
206 prPoint = (pFunctionPoint*)prevNode -> GetData();
207 int beforeX =prPoint->getRealX();
208 if( beforeX < xToOrder)
210 //we need to change the direction in memory of our node
211 pFunctionPoint* pointToInsert=new pFunctionPoint(lastPointIncluded->getRealX(),lastPointIncluded->getRealY());
212 // The last inserted point is ordered like: prevPoint - lastPointIncluded - nextPoint
213 lastOrdered = (realPoints.Insert(insertIndex, pointToInsert))!= NULL;
214 bool lastNodeDeleted=realPoints.DeleteNode(lastNodeIncluded);
215 return lastOrdered && lastNodeDeleted;
218 //is just the second point
221 //we need to change the direction in memory of our node
222 pFunctionPoint* pointToInsert=new pFunctionPoint(lastPointIncluded->getRealX(),lastPointIncluded->getRealY());
223 // The last inserted point is ordered like: prevPoint - lastPointIncluded - nextPoint
224 lastOrdered = (realPoints.Insert(insertIndex, pointToInsert))!= NULL;
225 bool lastNodeDeleted=realPoints.DeleteNode(lastNodeIncluded);
226 return lastOrdered && lastNodeDeleted;
230 node = node->GetNext();
236 bool lastNodeDeleted = realPoints.DeleteNode(lastNodeIncluded);
237 lastOrdered = lastOrdered && lastNodeDeleted;
242 * This tries to Add a point to the function if possible it returns true.
243 * @pre: The list of existing points is ordered.
244 * @param: int aX, The x value of the point.
245 * @param: int aY, The y value of the point.
246 * @param: return Returns TRUE if the point was succesfuly included in the realPoints list.
248 bool pLogicalFunction::AddPoint(int aX, int aY,bool order)
250 pFunctionPoint * newPoint = new pFunctionPoint (aX, aY);
251 wxNode* lastNode = realPoints.GetLast();
252 bool addedPoint = false;
255 pFunctionPoint* lastPoint = (pFunctionPoint*)lastNode->GetData();
256 if( lastPoint->getRealX() != aX )
258 addedPoint = realPoints.Append(newPoint)!=NULL;
259 //In case that the definition of points is being done backwards.
260 if( (aX < (lastPoint->getRealX()) ) && addedPoint )
262 if( realPoints.GetCount() == 2 )
264 leftToRigthDef = false;
267 addedPoint = orderPoints();
272 if( realPoints.GetCount() == 2 )
274 leftToRigthDef = true;
281 addedPoint = realPoints.Append(newPoint)!=NULL;
288 * Set Up startPoint, endPoint, maxY,maxX points
290 void pLogicalFunction::setUp()
292 //sets the start point of the function
294 //set the Last point of the function
296 //set the min value in x and y between all the points of the function
298 //set the max value in x and y between all the points of the function
304 * sets the start point of the function
306 void pLogicalFunction:: setStartPoints()
308 wxNode* first=realPoints.GetFirst();
311 pFunctionPoint* startPoint=(pFunctionPoint*)first->GetData();
312 setStartX(startPoint->getRealX());
313 setStartY(startPoint->getRealY());
322 * sets the end point of the function
324 void pLogicalFunction:: setEndPoints()
326 wxNode* last=realPoints.GetLast();
329 pFunctionPoint* lastPoint=(pFunctionPoint*)last->GetData();
330 setEndX(lastPoint->getRealX());
331 setEndY(lastPoint->getRealY());
342 * set the min value in x and y between all the points of the function
344 void pLogicalFunction::setMinPoints()
347 wxNode* node=realPoints.GetFirst();
350 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
351 minX=point->getRealX();
352 minY=point->getRealY();
353 wxNode* nextNode=node->GetNext();
356 point=(pFunctionPoint*)nextNode->GetData();
357 int x=point->getRealX();
358 int y=point->getRealY();
363 nextNode=nextNode->GetNext();
376 * set the max value in x and y between all the points of the function
378 void pLogicalFunction::setMaxPoints()
381 wxNode* node=realPoints.GetFirst();
384 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
385 maxX=point->getRealX();
386 maxY=point->getRealY();
387 wxNode* nextNode=node->GetNext();
390 point=(pFunctionPoint*)nextNode->GetData();
391 int x=point->getRealX();
392 int y=point->getRealY();
397 nextNode=nextNode->GetNext();
410 * deletes the point given by the user from the collection of logical points of the function
414 bool pLogicalFunction::DeletePoint(int aX, int aY)
416 wxNode* node= realPoints.GetFirst();
419 pFunctionPoint* point=(pFunctionPoint*)node->GetData();
420 if(point->getRealX()==aX && point->getRealY()==aY)
422 realPoints.Erase(node);
425 node= node->GetNext();
432 * deletes a point of the functio given its index
434 bool pLogicalFunction::deletePointAt(int index)
438 wxNode* node=GetPointAt(index);
440 if(node)//point!=NULL)
442 //pFunctionPoint* point=(pFunctionPoint*)node->GetData(); // JPRx
443 bool deleted=realPoints.DeleteNode(node);
448 realPoints=realPoints.;
457 * Change de coordinates of the given index point to the newCoords, if it is possible and return the result of the invoked action.
458 * @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.
460 bool pLogicalFunction::changePoint(wxPoint newCoords, int movingIndexPoint)
462 wxNode* changingNode = GetPointAt(movingIndexPoint);
463 bool validChange = false;
464 int newX = newCoords.x;
465 int newY = newCoords.y;
466 pFunctionPoint* changingPoint = (pFunctionPoint*)changingNode -> GetData();
467 bool hasPrevious = movingIndexPoint>0;
468 bool hasNext = movingIndexPoint < realPoints.size()-1;
470 wxNode* prevNode = hasPrevious ? changingNode ->GetPrevious() : NULL;
471 wxNode* nextNode = hasNext ? changingNode -> GetNext() : NULL;
475 pFunctionPoint* prevPoint = (pFunctionPoint*)prevNode -> GetData();
478 pFunctionPoint* nextPoint = (pFunctionPoint*) nextNode -> GetData();
479 validChange = ((prevPoint->getRealX()) < newX) && ((nextPoint->getRealX()) > newX);
480 if ( (prevPoint->getRealX()) > newX )
482 newX = prevPoint->getRealX()+1;
485 else if ( (nextPoint->getRealX()) < newX )
487 newX = nextPoint->getRealX()-1;
493 // Is the last point of the function.
494 if ( (prevPoint->getRealX()) < newX )
500 newX = prevPoint->getRealX();
509 // Is the first point of the function.
510 pFunctionPoint* nextPoint = (pFunctionPoint*) nextNode -> GetData();
511 if ((nextPoint->getRealX()) > newX )
517 newX = nextPoint->getRealX();
524 changingPoint -> setRealX( newX );
525 changingPoint -> setRealY( newY );
530 * Evaluates if the given point belongs to the function.
533 bool pLogicalFunction::hasPoint(wxPoint aPoint)
535 bool existPoint = false;
536 wxNode *nextNode = realPoints.GetFirst();
538 while (nextNode && !existPoint)
540 pFunctionPoint* nextPoint = (pFunctionPoint*)nextNode->GetData();
541 if( nextPoint -> getRealX() == aPoint.x && nextPoint-> getRealY() == aPoint.y)
545 nextNode = nextNode->GetNext();
551 * Returns the real x values of the control points of the function. It dont includes the points calculated by interpolation.
553 double* pLogicalFunction::getX_RealValues()
555 wxNode *nextNode = realPoints.GetFirst();
556 int size = realPoints.GetCount();
560 x_Values= new double[size];
564 pFunctionPoint* nextPoint = (pFunctionPoint*)nextNode->GetData();
565 x_Values[i] = nextPoint-> getRealX();
566 nextNode = nextNode->GetNext();
573 * Returns the real y values of the control points of the function. It dont includes the points calculated by interpolation.
575 double * pLogicalFunction::getY_RealValues()
577 wxNode *nextNode = realPoints.GetFirst();
579 int size = realPoints.GetCount();
582 y_Values= new double[size];
586 pFunctionPoint* nextPoint = (pFunctionPoint*)nextNode->GetData();
587 y_Values[i] = nextPoint-> getRealY();
588 nextNode = nextNode->GetNext();
594 //-----------------------
596 //-----------------------
599 Save the points of the function
603 for all (x,y) in the function
605 void pLogicalFunction::save(wxString fileName)
609 file.open( (const char*)(fileName.mb_str()) );
612 file << getSizePoints()<< std::endl;
613 wxNode* node= realPoints.GetFirst();
617 p=(pFunctionPoint*)node->GetData();
618 file <<p->getRealX()<<"\t"<<p->getRealY()<<std::endl;
619 node=node->GetNext();
625 Load the points of a function
627 void pLogicalFunction::load(wxString fileName)
631 file.open( (const char*)(fileName.mb_str()) );
635 std::getline(file,line);
636 int nPoints=atoi(line.c_str());
638 while(!file.eof() && i<nPoints)
640 std::getline(file,line);
641 int pos=line.find("\t");
642 int size=line.size();
643 std::string x=line.substr(0,pos);
644 std::string y=line.substr(pos+1,size);
645 int x0=atoi(x.c_str());
646 int y0=atoi(y.c_str());
655 //---------------------
656 //Getters and Setters
657 //---------------------
662 void pLogicalFunction::setStartX(double aStartX) {
663 this->_startX = aStartX;
666 double pLogicalFunction::getStartX() {
667 return this->_startX;
670 void pLogicalFunction::setStartY(double aStartY) {
671 this->_startY = aStartY;
674 double pLogicalFunction::getStartY() {
675 return this->_startY;
681 void pLogicalFunction::setEndX(double aEndX) {
685 double pLogicalFunction::getEndX() {
689 void pLogicalFunction::setEndY(double aEndY) {
693 double pLogicalFunction::getEndY() {
699 void pLogicalFunction::setScaleX(double aScaleX) {
700 this->_scaleX = aScaleX;
703 double pLogicalFunction::getScaleX() {
704 return this->_scaleX;
707 void pLogicalFunction::setScaleY(double aScaleY) {
708 this->_scaleY = aScaleY;
711 double pLogicalFunction::getScaleY() {
712 return this->_scaleY;
717 void pLogicalFunction::setMinX(double aMinX) {
721 double pLogicalFunction::getMinX() {
725 void pLogicalFunction::setMinY(double aMinY) {
729 double pLogicalFunction::getMinY() {
735 void pLogicalFunction::setMaxX(double aMaxX) {
739 double pLogicalFunction::getMaxX() {
743 void pLogicalFunction::setMaxY(double aMaxY) {
747 double pLogicalFunction::getMaxY() {
754 void pLogicalFunction::setOffsetX(double aOffsetX) {
755 this->_offsetX = aOffsetX;
758 double pLogicalFunction:: getOffsetX() {
759 return this->_offsetX;
762 void pLogicalFunction:: setOffsetY(double aOffsetY) {
763 this->_offsetY = aOffsetY;
766 double pLogicalFunction:: getOffsetY() {
767 return this->_offsetY;
773 void pLogicalFunction:: setType(int aType) {
777 int pLogicalFunction :: getType() {
784 int pLogicalFunction :: getValidPointRange()
786 return validPointRange;
789 void pLogicalFunction :: setValidPointRange(int theRange)
791 validPointRange = theRange;
794 * Returns the number of points that the function
797 int pLogicalFunction::getSizePoints()
799 return realPoints.GetCount();
801 void pLogicalFunction::getDirection(bool &dir)
803 dir = leftToRigthDef;