]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pPlotterLayer.cxx
BUG MacOs
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / pPlotter / pPlotterLayer.cxx
1 //----------------------------------------------------------------------------
2 // Class definition include
3 //----------------------------------------------------------------------------
4 #include "pPlotterLayer.h"
5
6 // ----------------------------------------------------------------------------
7 // WX headers inclusion.
8 // For compilers that support precompilation, includes <wx/wx.h>.
9 // ----------------------------------------------------------------------------
10
11 #ifndef WX_PRECOMP
12 #include <wx/wx.h>
13 #endif
14
15 #include <vector>
16 #include <fstream>
17 #include <iostream>
18 //----------------------------------------------------------------------------
19 // Class implementation
20 //----------------------------------------------------------------------------
21
22 IMPLEMENT_ABSTRACT_CLASS(pPlotterLayer, mpLayer)
23 //----------------------------------------------------------------------------
24 // Methods
25 //----------------------------------------------------------------------------
26
27 /** 
28 *@param name  Label
29 *@param flags Label alignment, pass one of #mpALIGN_NE, #mpALIGN_NW, #mpALIGN_SW, #mpALIGN_SE.
30 */
31 pPlotterLayer:: pPlotterLayer(wxString name , int flags )
32 {
33         SetName(name);
34         m_flags = flags;
35         points.DeleteContents(TRUE);
36         offsetX=0;
37         offsetY=0;
38 }
39
40 /*
41 Draw the line from (x1,y1) to (x2,y2) only passing by the 
42 positive points in the line
43 */
44 void  pPlotterLayer::draw(wxDC & dc,mpWindow & w,double x1,double y1,double x2,double y2)
45 {
46         
47         //intercepts
48         float m=((float)(y2-y1))/(x2-x1);
49         float x0=-y1/m+x1;
50         float y0=-m*x1+y1;      
51
52
53         //analyzing the curve
54
55                 if(x1<=0 && x2>=0)
56                 {
57                         if(y2>=0 && y1>=0)
58                                         dc.DrawLine(0,y0, x2,y2);
59
60                         else if(y2<=0 && y1>=0)
61                         {
62                                 if(y0>=0 && x0>=0)
63                                         dc.DrawLine(0,y0,x0,0 );
64                         }
65                         else if(y2>=0 && y1<=0)
66                         {
67                                 if(y0>=0) 
68                                 dc.DrawLine(0,y0,x2,y2 );
69                         }
70         
71                 }
72
73                 if(x1>=0 && x2>=0)
74                 {
75                         if(y1>=0 && y2>=0 )
76                                 dc.DrawLine(x1,y1, x2,y2);
77                         else if(y1>=0 && y2<=0)
78                                 dc.DrawLine(x1,y1,x0,0 );
79                         else if(y1<=0 && y2>=0) 
80                                         dc.DrawLine(x0,0,x2,y2);
81                 }
82                 
83                 
84 }
85         
86 /**
87 * Draw the function with th spline points
88 *
89 */
90 void pPlotterLayer::drawSplineCurve(wxDC & dc,mpWindow & w)
91 {
92         std::vector<double> vx=getXSpline();
93         std::vector<double> vy=getYSpline();
94         wxPoint* ppoints;
95         
96         int /*counter=0,*/ minX,maxX,minY,maxY; // JPRx
97         /*
98         This is the offset of every point scale to the window
99         pixel
100         */
101         int offsetpx=getOffsetPixelsXv();
102         int offsetpy=getOffsetPixelsYv();
103         
104         // type of plotter
105         int type=getmType();
106  
107         wxPoint point;
108         /***********/
109         getMaxShowed(maxX,maxY);
110         getMinShowed(minX,minY);
111         /***********/
112
113         
114         if(type==2)
115         {
116          /*
117                 Fill the function if it represented an histogram
118           */
119                 ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+2));
120                 //initialize points
121                 point.x=-5000;
122                 point.y=-5000;
123                 ppoints[0]=point;
124                 ppoints[1]=point;
125         }
126         
127         int size=vx.size();
128         int j=2;
129
130         for(int i=0;(i+1)< size;i++)
131         {
132                 
133                 
134                 double cxi=(vx.at(i)-minX-offsetX)*scaleX+offsetpx;
135                 double cyi=(vy.at(i)-minY-offsetY)*scaleY+offsetpy;
136                 
137                 double cxj=(vx.at(i+1)-minX-offsetX)*scaleX+offsetpx;
138                 double cyj=(vy.at(i+1)-minY-offsetY)*scaleY+offsetpy;
139                 
140                 
141                 if(type!=2)     
142                         draw(dc,w,cxi,cyi,cxj,cyj);
143                 else if(type==2)
144                 {
145                         if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
146                         {
147                                         point.x=cxj;
148                                         point.y=cyj;
149                                         ppoints[j]=point;
150                                         j++;
151                 
152                         }
153                 }
154                 
155         }
156         if(type==2)
157         {
158                 //point.x=vx.at(size-1)* scaleX + offsetpx;
159                 point.x=ppoints[j-1].x;
160                 point.y=0;
161                 //ppoints[vx.size()]=point;
162                 ppoints[j]=point;
163                 /*
164                 Fill the function if it represented an histogram
165           */
166                 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
167                 //settings for fill
168                 //point.x=vx.at(0)*scaleX + offsetpx;
169                 point.x=ppoints[0].x;
170                 point.y=0;
171                 //ppoints[vx.size()+1]=point;
172                 ppoints[j+1]=point;
173
174                 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID  ));
175                 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID  ));
176                 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
177                 dc.DrawPolygon(j+2,ppoints,0,0);
178         }
179         
180         
181 }
182
183 /*
184  it define the first point of the polygon for be drawing
185  returns true if the first and second point of the polygon are set
186 */
187 bool pPlotterLayer::initializePolygon(wxPoint* points,double x1, double y1,double x2, double y2)
188 {
189         bool setted=false;
190
191                 //intercepts
192         float m=((float)(y2-y1))/(x2-x1);
193         float x0=-y1/m+x1;
194         float y0=-m*x1+y1;      
195
196         if(points[0].x<=0&& points[1].x<=0 && points[0].y<=0&& points[1].y<=0)
197         {
198         
199                 //int offsetpx=getOffsetPixelsXv(); //JPRx      
200
201                 //analyzing the curve
202
203                         if(x1<=0 && x2>=0)
204                         {
205                                 if(y2>=0 && y1>=0)
206                                 {
207                                                 //dc.DrawLine(0,y0, x2,y2);
208                                                 points[0].x=0;
209                                                 points[0].y=y0;
210                                                 points[1].x=x2;
211                                                 points[1].y=y2;
212                                                 setted=true;                                    
213                                         
214                                 }
215                                 else if(y2<=0 && y1>=0)
216                                 {
217                                         if(y0>=0 && x0>=0)
218                                         {
219                                                 //dc.DrawLine(0,y0,x0,0 );
220                                                 points[0].x=0;
221                                                 points[0].y=y0;
222                                                 points[1].x=x0;
223                                                 points[1].y=0;
224                                                 setted=true;    
225
226                                         }
227                                                 
228                                 }
229                                 else if(y2>=0 && y1<=0)
230                                 {       
231                                         if(y0>=0) 
232                                         {       
233                                                 //dc.DrawLine(0,y0,x2,y2 );
234                                                 points[0].x=0;
235                                                 points[0].y=y0;
236                                                 points[1].x=x2;
237                                                 points[1].y=y2;
238                                                 setted=true;    
239                                         }
240                                 }
241                 
242                         }
243
244                         if(x1>=0 && x2>=0)
245                         {
246                                 if(y1>=0 && y2>=0 )
247                                 {
248                                         //dc.DrawLine(x1,y1, x2,y2);
249                                         points[0].x=x1;
250                                         points[0].y=y1;
251                                         points[1].x=x2;
252                                         points[1].y=y2;
253                                         setted=true;    
254                                 }
255                                 else if(y1>=0 && y2<=0)
256                                 {
257                                         //dc.DrawLine(x1,y1,x0,0 );
258                                         points[0].x=x1;
259                                         points[0].y=y1;
260                                         points[1].x=x0;
261                                         points[1].y=0;
262                                         setted=true;    
263                                 }
264                                 else if(y1<=0 && y2>=0) 
265                                 {       
266                                         //dc.DrawLine(x0,0,x2,y2);
267                                         points[0].x=x0;
268                                         points[0].y=0;
269                                         points[1].x=x2;
270                                         points[1].y=y2;
271                                         setted=true;    
272                                 }
273                         }
274                         else
275                         return setted;
276         }
277         else
278                 return setted;
279         return setted; 
280 }
281
282 /**
283 * Draw the lines between the points of the function
284 */
285 void pPlotterLayer::drawFunction(wxDC & dc,mpWindow & w)
286 {
287
288         
289         int scrwX,scrwY,offsetpx,offsetpy,maxX,minX,maxY,minY;
290         wxPoint* ppoints=NULL;
291
292         /*
293         This is the offset of every point scale to the window
294         pixel
295         */
296         offsetpx=getOffsetPixelsXv();
297         offsetpy=getOffsetPixelsYv();   
298
299         Rewind();
300         
301         dc.GetSize(&scrwX, &scrwY);
302         
303         //Lines between the points
304         
305         GetPoints(points);
306
307         // type of plotter
308         int type=getmType();
309         /***********/
310         getMaxShowed(maxX,maxY);
311         getMinShowed(minX,minY);
312         /***********/
313
314         //traslation
315         //int xTraslation=getXTraslation(); //JPRx
316         wxNode* node= points.GetFirst();
317         // int i=1;//points.GetCount()+1; //JPRx
318         int j=2;
319         /*
320          Fill it if it is an histogram
321         */
322         wxPoint point;
323         // pFunctionPoint* pointk; //JPRx
324         if(type==2)
325         {
326                 /*
327                 Fill the function if it represented an histogram
328           */
329                 ppoints=(wxPoint*)malloc(sizeof(int)*2*(points.GetCount()+2));
330                 //initialize points
331                 point.x=-5000;
332                 point.y=-5000;
333                 ppoints[0]=point;
334                 ppoints[1]=point;
335
336         }
337         
338         
339         while (node!=NULL && node->GetNext()!=NULL)
340         { 
341                 pFunctionPoint* pointi=(pFunctionPoint*)node->GetData();
342                                 wxNode* nextNode=node->GetNext();
343                 pFunctionPoint* pointj=(pFunctionPoint*)nextNode->GetData();
344                                                 
345                 // we do the offset
346                 int pxi=(pointi->getRealX()-minX)-offsetX;
347                 int pyi=(pointi->getRealY()-minY)-offsetY;
348                 int pxj=(pointj->getRealX()-minX)-offsetX;
349                 int pyj=(pointj->getRealY()-minY)-offsetY;
350
351
352                 int cxi=pxi* scaleX + offsetpx; //+ xTraslation;
353                 int cxj=pxj* scaleX + offsetpx; //+ xTraslation;
354                 int cyi=pyi* scaleY+ offsetpy ;
355                 int cyj=pyj* scaleY+ offsetpy ;
356                 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
357                 if(type!=2)
358                         draw(dc,w,pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
359                         //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
360                 else if(type==2)
361                 {
362                         if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
363                         {
364                                         point.x=cxj;
365                                         point.y=cyj;
366                                         ppoints[j]=point;
367                                         j++;
368                 
369                         }
370                 }
371                 
372                 node=node->GetNext();
373
374         } 
375         if(type==2)
376         {
377                 //point.x=vx.at(size-1)* scaleX + offsetpx;
378                 point.x=ppoints[j-1].x;
379                 point.y=0;
380                 //ppoints[vx.size()]=point;
381                 ppoints[j]=point;
382                 /*
383                 Fill the function if it represented an histogram
384           */
385                 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
386                 //settings for fill
387                 //point.x=vx.at(0)*scaleX + offsetpx;
388                 point.x=ppoints[0].x;
389                 point.y=0;
390                 //ppoints[vx.size()+1]=point;
391                 ppoints[j+1]=point;
392
393                 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID  ));
394                 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID  ));
395                 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
396                 dc.DrawPolygon(j+2,ppoints,0,0);
397
398         }
399         
400 }
401
402 /**
403 *  Draw the points of the function
404 */
405 void pPlotterLayer::drawPoints(wxDC & dc,mpWindow & w)
406 {
407         
408         Rewind();
409         double x, y;
410         int minX,maxX,minY,maxY;
411
412         
413         /*
414         This is the offset of every point scale to the window
415         pixel
416         */
417         int offsetpx=getOffsetPixelsXv();
418         int offsetpy=getOffsetPixelsYv();
419         //traslation
420 //      int xTraslation=getXTraslation();  //EED
421         /*wxPen mypen(*wxBLACK, 5, wxSOLID);
422         dc.SetPen(mypen);*/
423         /***********/
424         getMaxShowed(maxX,maxY);
425         getMinShowed(minX,minY);
426         /***********/
427         //we have to draw the points
428
429         while (GetNextXY(x, y))
430         {
431                 //GetNextXY(x, y);
432                 //set the points of the polygons
433                 wxPoint points[4];
434                 dc.SetBrush(wxBrush( wxColour(255,0,0),wxSOLID  ));
435                 //dc.SetPen(wxPen(wxColour(0,0,0),1,wxSOLID) );
436                 points[0].x=((x-minX-offsetX)*scaleX + offsetpx-MOVE);
437                 points[0].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
438                 points[1].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
439                 points[1].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
440                 points[2].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
441                 points[2].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
442                 points[3].x=((x-minX-offsetX)*scaleX+ offsetpx-MOVE);
443                 points[3].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
444                 if((x-minX-offsetX)*scaleX >=0 && (y-minY-offsetY/*w.getMinScrY()*/)*scaleY>=0)
445                 dc.DrawPolygon(4,points,0,0);
446         }
447 }
448
449 /**
450 * Draw the line between the last point of the function
451 * and the position of the mouse
452 */
453 void pPlotterLayer::drawLineToMousePoint(wxDC & dc,mpWindow & w)
454 {
455         int x,y,sizeP,maxX,maxY,minX,minY;
456         bool direction;
457         
458         Rewind();
459         
460         getMousePoint(x,y);
461         getDirection(direction);
462         getSize(sizeP);
463         //Lines betwen the points
464         GetPoints(points);
465
466         wxNode *node=points.GetLast();
467         wxNode *node1=points.GetFirst();
468         
469         
470         /*
471         This is the offset of every point scale to the window
472         pixel
473         */
474         int offsetpx = getOffsetPixelsXv();
475         int offsetpy = getOffsetPixelsYv();
476         /***********/
477         getMaxShowed(maxX,maxY);
478         getMinShowed(minX,minY);
479         /***********/
480         //traslation
481 //      int xTraslation=getXTraslation(); //EED
482         
483         if(node)
484         {
485                 pFunctionPoint* lastPoint=(pFunctionPoint*)node->GetData();
486                 pFunctionPoint* firstPoint=(pFunctionPoint*)node1->GetData();
487
488                 //LeftDrawing
489                 if(lastPoint->getRealX()<x && direction && sizeP >=2)
490                         
491                         dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
492                 
493                 //right drawing
494                 else if(firstPoint->getRealX()>x && !direction && sizeP >=2 )
495                         
496                         dc.DrawLine((firstPoint->getRealX()-minX-offsetX)*scaleX+ offsetpx,(firstPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy,(x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
497                 
498                 //just a point
499                 else if(sizeP==1 )
500                 {
501                         dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
502                 }
503         }                       
504         
505         if( w.drawGuideLines() )  
506         {
507                 dc.SetPen(wxPen(  wxColour(255,0,0),1,wxDOT ));
508                 // Drawing the horizontal guide line    
509                 dc.DrawLine( 0, (y-minY)*scaleY + offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);                                    
510                 // Drawing the vertical guide line      
511                 dc.DrawLine( (x-minX)*scaleX + offsetpx,0, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);     
512         }
513 }
514
515
516 /** 
517 Layer plot handler.
518 This implementation will plot the locus in the visible area and
519 put a label according to the aligment specified.
520 */
521 void pPlotterLayer::Plot(wxDC & dc, mpWindow & w)
522 {
523         bool show,drawing,actual;
524         int scrwX,scrwY,maxX,maxY,minX,minY,type;
525         float factorZoom;
526         
527         ifShowPoints(show);
528         getDrawing(drawing);
529         type=vGetType();
530         
531         dc.SetPen( m_pen);
532         
533         /*
534          * Managing the scale and zoom
535          */
536         
537         dc.GetSize(&scrwX, &scrwY);
538         getIfActual(actual);
539         //getMax(maxX, maxY);
540         /***********/
541         getMaxShowed(maxX,maxY);
542         getMinShowed(minX,minY);
543         /***********/
544         getFactorZoom(factorZoom);
545         getOffsets(offsetX,offsetY);
546         /*
547         maxX=w.getMaxScrX();
548         maxY=w.getMaxScrY();
549         minX=w.getMinScrX();
550         minY=w.getMinScrY();
551         */
552         if((maxX-minX)!=0 && (maxY-minY)!=0)
553         {
554                 
555                 scaleX=(((double)scrwX-100)/(maxX-minX))*factorZoom;
556                 scaleY=(((double)scrwY-50)/(maxY-minY))*factorZoom;
557                 
558                 if(actual)
559                 {
560                         w.SetScaleX(scaleX);
561                         w.SetScaleY(scaleY);
562                         //w.setMinScrX(offsetX);
563                         //w.setMinScrY(offsetY);
564                 }
565                 
566         }
567         /*
568                 Managing the drawing
569         */
570         int orgx=70;
571
572         
573 //EED 14Mai2009
574 //      int orgy=w.GetScrY()-40;
575 //      dc.SetDeviceOrigin( orgx ,orgy);
576 //      dc.SetAxisOrientation(true,true);
577         
578         int orgy = 40;  
579         dc.SetDeviceOrigin( orgx ,orgy);
580         dc.SetAxisOrientation(true,false);
581
582         
583
584         //if the user dont want to see the points of the function and if he stops drawing
585         //we have to draw the function
586         if(!show && !drawing && type==1)
587                 drawFunction(dc,w);
588         else if(!show && !drawing && type==2)
589                 drawSplineCurve(dc,w);
590         //we just draw the point that the user just clicked
591         else if(drawing && type==1)
592         { 
593                 drawFunction(dc,w);
594                 drawLineToMousePoint(dc,w);
595                 drawPoints(dc,w);
596         }
597         else if(drawing && type==1)
598         {
599                 drawSplineCurve(dc,w);
600                 drawLineToMousePoint(dc,w);
601                 drawPoints(dc,w);
602         }
603         else if(show && type==1)
604         {
605                 drawFunction(dc,w);
606                 drawPoints(dc,w);
607         }
608         else if(show && type==2)
609         {
610                 drawSplineCurve(dc,w);
611                 drawPoints(dc,w);
612         }
613
614         // Drawing the guides according to real values entered according to the integrated interaction (IS NOT WORKING!!!)
615         if ( actual )
616         {
617                 dc.SetPen(wxPen(  wxColour(255,0,0),1,wxDOT ));
618                 int offsetpx = getOffsetPixelsXv();
619 //              int offsetpy = getOffsetPixelsYv();  //EED
620
621                 int realX_guide = w.getRealGuideX();
622                 if( realX_guide!=-1 )
623                 {
624                 dc.DrawLine( (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, 0, (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, scrwY);                 
625                 }
626                         
627                 int realY_guide = w.getRealGuideY();
628                 if( realY_guide!=-1 )
629                 {
630                         dc.DrawLine( 0,(realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx, scrwX, (realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx);
631                 }
632         }
633    
634 }
635