]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pPlotterLayer.cxx
- Comment out uselesss #define MAX (vtk-4.5 doesn't compile)
[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 }
280
281 /**
282 * Draw the lines between the points of the function
283 */
284 void pPlotterLayer::drawFunction(wxDC & dc,mpWindow & w)
285 {
286
287         
288         int scrwX,scrwY,offsetpx,offsetpy,maxX,minX,maxY,minY;
289         wxPoint* ppoints=NULL;
290
291         /*
292         This is the offset of every point scale to the window
293         pixel
294         */
295         offsetpx=getOffsetPixelsXv();
296         offsetpy=getOffsetPixelsYv();   
297
298         Rewind();
299         
300         dc.GetSize(&scrwX, &scrwY);
301         
302         //Lines between the points
303         
304         GetPoints(points);
305
306         // type of plotter
307         int type=getmType();
308         /***********/
309         getMaxShowed(maxX,maxY);
310         getMinShowed(minX,minY);
311         /***********/
312
313         //traslation
314         //int xTraslation=getXTraslation(); //JPRx
315         wxNode* node= points.GetFirst();
316         // int i=1;//points.GetCount()+1; //JPRx
317         int j=2;
318         /*
319          Fill it if it is an histogram
320         */
321         wxPoint point;
322         // pFunctionPoint* pointk; //JPRx
323         if(type==2)
324         {
325                 /*
326                 Fill the function if it represented an histogram
327           */
328                 ppoints=(wxPoint*)malloc(sizeof(int)*2*(points.GetCount()+2));
329                 //initialize points
330                 point.x=-5000;
331                 point.y=-5000;
332                 ppoints[0]=point;
333                 ppoints[1]=point;
334
335         }
336         
337         
338         while (node!=NULL && node->GetNext()!=NULL)
339         { 
340                 pFunctionPoint* pointi=(pFunctionPoint*)node->GetData();
341                                 wxNode* nextNode=node->GetNext();
342                 pFunctionPoint* pointj=(pFunctionPoint*)nextNode->GetData();
343                                                 
344                 // we do the offset
345                 int pxi=(pointi->getRealX()-minX)-offsetX;
346                 int pyi=(pointi->getRealY()-minY)-offsetY;
347                 int pxj=(pointj->getRealX()-minX)-offsetX;
348                 int pyj=(pointj->getRealY()-minY)-offsetY;
349
350
351                 int cxi=pxi* scaleX + offsetpx; //+ xTraslation;
352                 int cxj=pxj* scaleX + offsetpx; //+ xTraslation;
353                 int cyi=pyi* scaleY+ offsetpy ;
354                 int cyj=pyj* scaleY+ offsetpy ;
355                 //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
356                 if(type!=2)
357                         draw(dc,w,pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
358                         //dc.DrawLine(pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy );
359                 else if(type==2)
360                 {
361                         if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
362                         {
363                                         point.x=cxj;
364                                         point.y=cyj;
365                                         ppoints[j]=point;
366                                         j++;
367                 
368                         }
369                 }
370                 
371                 node=node->GetNext();
372
373         } 
374         if(type==2)
375         {
376                 //point.x=vx.at(size-1)* scaleX + offsetpx;
377                 point.x=ppoints[j-1].x;
378                 point.y=0;
379                 //ppoints[vx.size()]=point;
380                 ppoints[j]=point;
381                 /*
382                 Fill the function if it represented an histogram
383           */
384                 //ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+1));
385                 //settings for fill
386                 //point.x=vx.at(0)*scaleX + offsetpx;
387                 point.x=ppoints[0].x;
388                 point.y=0;
389                 //ppoints[vx.size()+1]=point;
390                 ppoints[j+1]=point;
391
392                 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID  ));
393                 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID  ));
394                 //dc.DrawPolygon(vx.size()+2,ppoints,0,0);
395                 dc.DrawPolygon(j+2,ppoints,0,0);
396
397         }
398         
399 }
400
401 /**
402 *  Draw the points of the function
403 */
404 void pPlotterLayer::drawPoints(wxDC & dc,mpWindow & w)
405 {
406         
407         Rewind();
408         double x, y;
409         int minX,maxX,minY,maxY;
410
411         
412         /*
413         This is the offset of every point scale to the window
414         pixel
415         */
416         int offsetpx=getOffsetPixelsXv();
417         int offsetpy=getOffsetPixelsYv();
418         //traslation
419         int xTraslation=getXTraslation();
420         /*wxPen mypen(*wxBLACK, 5, wxSOLID);
421         dc.SetPen(mypen);*/
422         /***********/
423         getMaxShowed(maxX,maxY);
424         getMinShowed(minX,minY);
425         /***********/
426         //we have to draw the points
427
428         while (GetNextXY(x, y))
429         {
430                 //GetNextXY(x, y);
431                 //set the points of the polygons
432                 wxPoint points[4];
433                 dc.SetBrush(wxBrush( wxColour(255,0,0),wxSOLID  ));
434                 //dc.SetPen(wxPen(wxColour(0,0,0),1,wxSOLID) );
435                 points[0].x=((x-minX-offsetX)*scaleX + offsetpx-MOVE);
436                 points[0].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
437                 points[1].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
438                 points[1].y=((y-minY-offsetY)*scaleY+ offsetpy-MOVE);
439                 points[2].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
440                 points[2].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
441                 points[3].x=((x-minX-offsetX)*scaleX+ offsetpx-MOVE);
442                 points[3].y=((y-minY-offsetY)*scaleY+ offsetpy+MOVE);
443                 if((x-minX-offsetX)*scaleX >=0 && (y-minY-offsetY/*w.getMinScrY()*/)*scaleY>=0)
444                 dc.DrawPolygon(4,points,0,0);
445         }
446 }
447
448 /**
449 * Draw the line between the last point of the function
450 * and the position of the mouse
451 */
452 void pPlotterLayer::drawLineToMousePoint(wxDC & dc,mpWindow & w)
453 {
454         int x,y,sizeP,maxX,maxY,minX,minY;
455         bool direction;
456         
457         Rewind();
458         
459         getMousePoint(x,y);
460         getDirection(direction);
461         getSize(sizeP);
462         //Lines betwen the points
463         GetPoints(points);
464
465         wxNode *node=points.GetLast();
466         wxNode *node1=points.GetFirst();
467         
468         
469         /*
470         This is the offset of every point scale to the window
471         pixel
472         */
473         int offsetpx = getOffsetPixelsXv();
474         int offsetpy = getOffsetPixelsYv();
475         /***********/
476         getMaxShowed(maxX,maxY);
477         getMinShowed(minX,minY);
478         /***********/
479         //traslation
480         int xTraslation=getXTraslation();
481         
482         if(node)
483         {
484                 pFunctionPoint* lastPoint=(pFunctionPoint*)node->GetData();
485                 pFunctionPoint* firstPoint=(pFunctionPoint*)node1->GetData();
486
487                 //LeftDrawing
488                 if(lastPoint->getRealX()<x && direction && sizeP >=2)
489                         
490                         dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
491                 
492                 //right drawing
493                 else if(firstPoint->getRealX()>x && !direction && sizeP >=2 )
494                         
495                         dc.DrawLine((firstPoint->getRealX()-minX-offsetX)*scaleX+ offsetpx,(firstPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy,(x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
496                 
497                 //just a point
498                 else if(sizeP==1 )
499                 {
500                         dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
501                 }
502         }                       
503         
504         if( w.drawGuideLines() )  
505         {
506                 dc.SetPen(wxPen(  wxColour(255,0,0),1,wxDOT ));
507                 // Drawing the horizontal guide line    
508                 dc.DrawLine( 0, (y-minY)*scaleY + offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);                                    
509                 // Drawing the vertical guide line      
510                 dc.DrawLine( (x-minX)*scaleX + offsetpx,0, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);     
511         }
512 }
513
514
515 /** 
516 Layer plot handler.
517 This implementation will plot the locus in the visible area and
518 put a label according to the aligment specified.
519 */
520 void pPlotterLayer::Plot(wxDC & dc, mpWindow & w)
521 {
522         bool show,drawing,actual;
523         int scrwX,scrwY,maxX,maxY,minX,minY,type;
524         float factorZoom;
525         
526         ifShowPoints(show);
527         getDrawing(drawing);
528         type=vGetType();
529         
530         dc.SetPen( m_pen);
531         
532         /*
533          * Managing the scale and zoom
534          */
535         
536         dc.GetSize(&scrwX, &scrwY);
537         getIfActual(actual);
538         //getMax(maxX, maxY);
539         /***********/
540         getMaxShowed(maxX,maxY);
541         getMinShowed(minX,minY);
542         /***********/
543         getFactorZoom(factorZoom);
544         getOffsets(offsetX,offsetY);
545         /*
546         maxX=w.getMaxScrX();
547         maxY=w.getMaxScrY();
548         minX=w.getMinScrX();
549         minY=w.getMinScrY();
550         */
551         if((maxX-minX)!=0 && (maxY-minY)!=0)
552         {
553                 
554                 scaleX=(((double)scrwX-100)/(maxX-minX))*factorZoom;
555                 scaleY=(((double)scrwY-50)/(maxY-minY))*factorZoom;
556                 
557                 if(actual)
558                 {
559                         w.SetScaleX(scaleX);
560                         w.SetScaleY(scaleY);
561                         //w.setMinScrX(offsetX);
562                         //w.setMinScrY(offsetY);
563                 }
564                 
565         }
566         /*
567                 Managing the drawing
568         */
569         int orgx=70;
570         int orgy=w.GetScrY()-40;
571         dc.SetDeviceOrigin( orgx ,orgy);
572         dc.SetAxisOrientation(true,true);
573
574
575         //if the user dont want to see the points of the function and if he stops drawing
576         //we have to draw the function
577         if(!show && !drawing && type==1)
578                 drawFunction(dc,w);
579         else if(!show && !drawing && type==2)
580                 drawSplineCurve(dc,w);
581         //we just draw the point that the user just clicked
582         else if(drawing && type==1)
583         { 
584                 drawFunction(dc,w);
585                 drawLineToMousePoint(dc,w);
586                 drawPoints(dc,w);
587         }
588         else if(drawing && type==1)
589         {
590                 drawSplineCurve(dc,w);
591                 drawLineToMousePoint(dc,w);
592                 drawPoints(dc,w);
593         }
594         else if(show && type==1)
595         {
596                 drawFunction(dc,w);
597                 drawPoints(dc,w);
598         }
599         else if(show && type==2)
600         {
601                 drawSplineCurve(dc,w);
602                 drawPoints(dc,w);
603         }
604
605         // Drawing the guides according to real values entered according to the integrated interaction (IS NOT WORKING!!!)
606         if ( actual )
607         {
608                 dc.SetPen(wxPen(  wxColour(255,0,0),1,wxDOT ));
609                 int offsetpx = getOffsetPixelsXv();
610                 int offsetpy = getOffsetPixelsYv();
611
612                 int realX_guide = w.getRealGuideX();
613                 if( realX_guide!=-1 )
614                 {
615                 dc.DrawLine( (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, 0, (realX_guide/*-w.getMinScrX()*/-offsetX)*scaleX + offsetpx, scrwY);                 
616                 }
617                         
618                 int realY_guide = w.getRealGuideY();
619                 if( realY_guide!=-1 )
620                 {
621                         dc.DrawLine( 0,(realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx, scrwX, (realY_guide/*-w.getMinScrY()*/-offsetX)*scaleX + offsetpx);
622                 }
623         }
624    
625 }
626