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