00001
00002
00003
00004 #include "pPlotterLayer.h"
00005
00006
00007
00008
00009
00010
00011 #ifndef WX_PRECOMP
00012 #include <wx/wx.h>
00013 #endif
00014
00015 #include <vector>
00016 #include <fstream>
00017 #include <iostream>
00018
00019
00020
00021
00022 IMPLEMENT_ABSTRACT_CLASS(pPlotterLayer, mpLayer)
00023
00024
00025
00026
00027
00031 pPlotterLayer:: pPlotterLayer(wxString name , int flags )
00032 {
00033 SetName(name);
00034 m_flags = flags;
00035 points.DeleteContents(TRUE);
00036 offsetX=0;
00037 offsetY=0;
00038 }
00039
00040
00041
00042
00043
00044 void pPlotterLayer::draw(wxDC & dc,mpWindow & w,double x1,double y1,double x2,double y2, int orgy)
00045 {
00046
00047
00048 float m=((float)(y2-y1))/(x2-x1);
00049 float x0=-y1/m+x1;
00050 float y0=-m*x1+y1;
00051
00052 double sizedc = dc.GetSize().GetHeight()-orgy;
00053
00054
00055
00056 if(x1<=0 && x2>=0)
00057 {
00058 if(y2>=0 && y1>=0)
00059 dc.DrawLine(0,GetYTranslated(sizedc,y0), x2,GetYTranslated(sizedc,y2));
00060
00061 else if(y2<=0 && y1>=0)
00062 {
00063 if(y0>=0 && x0>=0)
00064 dc.DrawLine(0,GetYTranslated(sizedc,y0),x0,GetYTranslated(sizedc,0) );
00065 }
00066 else if(y2>=0 && y1<=0)
00067 {
00068 if(y0>=0)
00069 dc.DrawLine(0,GetYTranslated(sizedc,y0),x2,GetYTranslated(sizedc,y2) );
00070 }
00071
00072 }
00073
00074 if(x1>=0 && x2>=0)
00075 {
00076 if(y1>=0 && y2>=0 )
00077 dc.DrawLine(x1,GetYTranslated(sizedc,y1), x2,GetYTranslated(sizedc,y2));
00078 else if(y1>=0 && y2<=0)
00079 dc.DrawLine(x1,GetYTranslated(sizedc,y1),x0,GetYTranslated(sizedc,0) );
00080 else if(y1<=0 && y2>=0)
00081 dc.DrawLine(x0,GetYTranslated(sizedc,0),x2,GetYTranslated(sizedc,y2));
00082 }
00083
00084
00085 }
00086
00091 void pPlotterLayer::drawSplineCurve(wxDC & dc,mpWindow & w, int orgy)
00092 {
00093 std::vector<double> vx=getXSpline();
00094 std::vector<double> vy=getYSpline();
00095 wxPoint* ppoints;
00096
00097 int minX,maxX,minY,maxY;
00098
00099
00100
00101
00102 int offsetpx=getOffsetPixelsXv();
00103 int offsetpy=getOffsetPixelsYv();
00104
00105
00106 int type=getmType();
00107
00108 wxPoint point;
00109
00110 getMaxShowed(maxX,maxY);
00111 getMinShowed(minX,minY);
00112
00113
00114
00115 if(type==2)
00116 {
00117
00118
00119
00120 ppoints=(wxPoint*)malloc(sizeof(int)*2*(vx.size()+2));
00121
00122 point.x=-5000;
00123 point.y=-5000;
00124 ppoints[0]=point;
00125 ppoints[1]=point;
00126 }
00127
00128 int size=vx.size();
00129 int j=2;
00130
00131 for(int i=0;(i+1)< size;i++)
00132 {
00133
00134
00135 double cxi=(vx.at(i)-minX-offsetX)*scaleX+offsetpx;
00136 double cyi=(vy.at(i)-minY-offsetY)*scaleY+offsetpy;
00137
00138 double cxj=(vx.at(i+1)-minX-offsetX)*scaleX+offsetpx;
00139 double cyj=(vy.at(i+1)-minY-offsetY)*scaleY+offsetpy;
00140
00141
00142 if(type!=2)
00143 draw(dc,w,cxi,cyi,cxj,cyj,orgy);
00144 else if(type==2)
00145 {
00146 if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
00147 {
00148 point.x=cxj;
00149 point.y=cyj;
00150 ppoints[j]=point;
00151 j++;
00152
00153 }
00154 }
00155
00156 }
00157 if(type==2)
00158 {
00159
00160 point.x=ppoints[j-1].x;
00161 point.y=0;
00162
00163 ppoints[j]=point;
00164
00165
00166
00167
00168
00169
00170 point.x=ppoints[0].x;
00171 point.y=0;
00172
00173 ppoints[j+1]=point;
00174
00175 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID ));
00176 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID ));
00177
00178 for(int i = 0; i <= j + 1; i++){
00179 int sizedc = dc.GetSize().GetHeight()-orgy;
00180 ppoints[i].y = GetYTranslated(sizedc, ppoints[i].y);
00181 }
00182 dc.DrawPolygon(j+2,ppoints,0,0);
00183 }
00184
00185
00186 }
00187
00188
00189
00190
00191
00192 bool pPlotterLayer::initializePolygon(wxPoint* points,double x1, double y1,double x2, double y2)
00193 {
00194 bool setted=false;
00195
00196
00197 float m=((float)(y2-y1))/(x2-x1);
00198 float x0=-y1/m+x1;
00199 float y0=-m*x1+y1;
00200
00201 if(points[0].x<=0&& points[1].x<=0 && points[0].y<=0&& points[1].y<=0)
00202 {
00203
00204
00205
00206
00207
00208 if(x1<=0 && x2>=0)
00209 {
00210 if(y2>=0 && y1>=0)
00211 {
00212
00213 points[0].x=0;
00214 points[0].y=y0;
00215 points[1].x=x2;
00216 points[1].y=y2;
00217 setted=true;
00218
00219 }
00220 else if(y2<=0 && y1>=0)
00221 {
00222 if(y0>=0 && x0>=0)
00223 {
00224
00225 points[0].x=0;
00226 points[0].y=y0;
00227 points[1].x=x0;
00228 points[1].y=0;
00229 setted=true;
00230
00231 }
00232
00233 }
00234 else if(y2>=0 && y1<=0)
00235 {
00236 if(y0>=0)
00237 {
00238
00239 points[0].x=0;
00240 points[0].y=y0;
00241 points[1].x=x2;
00242 points[1].y=y2;
00243 setted=true;
00244 }
00245 }
00246
00247 }
00248
00249 if(x1>=0 && x2>=0)
00250 {
00251 if(y1>=0 && y2>=0 )
00252 {
00253
00254 points[0].x=x1;
00255 points[0].y=y1;
00256 points[1].x=x2;
00257 points[1].y=y2;
00258 setted=true;
00259 }
00260 else if(y1>=0 && y2<=0)
00261 {
00262
00263 points[0].x=x1;
00264 points[0].y=y1;
00265 points[1].x=x0;
00266 points[1].y=0;
00267 setted=true;
00268 }
00269 else if(y1<=0 && y2>=0)
00270 {
00271
00272 points[0].x=x0;
00273 points[0].y=0;
00274 points[1].x=x2;
00275 points[1].y=y2;
00276 setted=true;
00277 }
00278 }
00279 else
00280 return setted;
00281 }
00282 else
00283 return setted;
00284 return setted;
00285 }
00286
00290 void pPlotterLayer::drawFunction(wxDC & dc,mpWindow & w, int orgy)
00291 {
00292
00293
00294 int scrwX,scrwY,offsetpx,offsetpy,maxX,minX,maxY,minY;
00295 wxPoint* ppoints=NULL;
00296
00297
00298
00299
00300
00301 offsetpx=getOffsetPixelsXv();
00302 offsetpy=getOffsetPixelsYv();
00303
00304 Rewind();
00305
00306 dc.GetSize(&scrwX, &scrwY);
00307
00308
00309
00310 int sizedc = dc.GetSize().GetHeight()-orgy;
00311
00312 GetPoints(points);
00313
00314
00315 int type=getmType();
00316
00317 getMaxShowed(maxX,maxY);
00318 getMinShowed(minX,minY);
00319
00320
00321
00322
00323 wxNode* node= points.GetFirst();
00324
00325 int j=2;
00326
00327
00328
00329 wxPoint point;
00330
00331 if(type==2)
00332 {
00333
00334
00335
00336 ppoints=(wxPoint*)malloc(sizeof(int)*2*(points.GetCount()+2));
00337
00338 point.x=-5000;
00339 point.y=GetYTranslated(sizedc,-5000);
00340 ppoints[0]=point;
00341 ppoints[1]=point;
00342
00343 }
00344
00345
00346 while (node!=NULL && node->GetNext()!=NULL)
00347 {
00348 pFunctionPoint* pointi=(pFunctionPoint*)node->GetData();
00349 wxNode* nextNode=node->GetNext();
00350 pFunctionPoint* pointj=(pFunctionPoint*)nextNode->GetData();
00351
00352
00353 int pxi=(pointi->getRealX()-minX)-offsetX;
00354 int pyi=(pointi->getRealY()-minY)-offsetY;
00355 int pxj=(pointj->getRealX()-minX)-offsetX;
00356 int pyj=(pointj->getRealY()-minY)-offsetY;
00357
00358
00359 int cxi=pxi* scaleX + offsetpx;
00360 int cxj=pxj* scaleX + offsetpx;
00361 int cyi=pyi* scaleY+ offsetpy ;
00362 int cyj=pyj* scaleY+ offsetpy ;
00363
00364 if(type!=2)
00365 draw(dc,w,pxi* scaleX + offsetpx,pyi* scaleY+ offsetpy, pxj* scaleX + offsetpx,pyj* scaleY+ offsetpy,orgy );
00366
00367 else if(type==2)
00368 {
00369 if(!initializePolygon(ppoints,cxi,cyi,cxj,cyj) && ((cxi<=0 && cxj>=0)||(cxi>=0 && cxj>=0)))
00370 {
00371 point.x=cxj;
00372 point.y=GetYTranslated(sizedc,cyj);
00373 ppoints[j]=point;
00374 j++;
00375
00376 }
00377 }
00378
00379 node=node->GetNext();
00380
00381 }
00382 if(type==2)
00383 {
00384
00385 point.x=ppoints[j-1].x;
00386 point.y=GetYTranslated(sizedc,0);
00387
00388 ppoints[j]=point;
00389
00390
00391
00392
00393
00394
00395 point.x=ppoints[0].x;
00396 point.y=GetYTranslated(sizedc,0);
00397
00398 ppoints[j+1]=point;
00399
00400 dc.SetBrush(wxBrush( wxColour(239,238,242) ,wxSOLID ));
00401 dc.SetPen(wxPen( wxColour(0,0,0) ,1,wxSOLID ));
00402
00403 dc.DrawPolygon(j+2,ppoints,0,0);
00404
00405 }
00406
00407 }
00408
00412 void pPlotterLayer::drawPoints(wxDC & dc,mpWindow & w, int orgy)
00413 {
00414
00415 Rewind();
00416 double x, y;
00417 int minX,maxX,minY,maxY;
00418
00419 double sizedc = dc.GetSize().GetHeight()-orgy;
00420
00421
00422
00423
00424
00425 int offsetpx=getOffsetPixelsXv();
00426 int offsetpy=getOffsetPixelsYv();
00427
00428
00429
00430
00431
00432 getMaxShowed(maxX,maxY);
00433 getMinShowed(minX,minY);
00434
00435
00436
00437 while (GetNextXY(x, y))
00438 {
00439
00440
00441 wxPoint points[4];
00442 dc.SetBrush(wxBrush( wxColour(255,0,0),wxSOLID ));
00443
00444 points[0].x=((x-minX-offsetX)*scaleX + offsetpx-MOVE);
00445 points[0].y=GetYTranslated(sizedc,((y-minY-offsetY)*scaleY+ offsetpy-MOVE));
00446 points[1].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
00447 points[1].y=GetYTranslated(sizedc,((y-minY-offsetY)*scaleY+ offsetpy-MOVE));
00448 points[2].x=((x-minX-offsetX)*scaleX+ offsetpx+MOVE);
00449 points[2].y=GetYTranslated(sizedc,((y-minY-offsetY)*scaleY+ offsetpy+MOVE));
00450 points[3].x=((x-minX-offsetX)*scaleX+ offsetpx-MOVE);
00451 points[3].y=GetYTranslated(sizedc,((y-minY-offsetY)*scaleY+ offsetpy+MOVE));
00452 if((x-minX-offsetX)*scaleX >=0 && (y-minY-offsetY)*scaleY>=0)
00453 dc.DrawPolygon(4,points,0,0);
00454 }
00455 }
00456
00461 void pPlotterLayer::drawLineToMousePoint(wxDC & dc,mpWindow & w, int orgy)
00462 {
00463 int x,y,sizeP,maxX,maxY,minX,minY;
00464 bool direction;
00465
00466 Rewind();
00467
00468 getMousePoint(x,y);
00469 getDirection(direction);
00470 getSize(sizeP);
00471
00472 GetPoints(points);
00473
00474 wxNode *node=points.GetLast();
00475 wxNode *node1=points.GetFirst();
00476
00477
00478
00479
00480
00481
00482 int offsetpx = getOffsetPixelsXv();
00483 int offsetpy = getOffsetPixelsYv();
00484
00485 getMaxShowed(maxX,maxY);
00486 getMinShowed(minX,minY);
00487
00488
00489
00490
00491 if(node)
00492 {
00493 pFunctionPoint* lastPoint=(pFunctionPoint*)node->GetData();
00494 pFunctionPoint* firstPoint=(pFunctionPoint*)node1->GetData();
00495
00496
00497 if(lastPoint->getRealX()<x && direction && sizeP >=2)
00498
00499 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
00500
00501
00502 else if(firstPoint->getRealX()>x && !direction && sizeP >=2 )
00503
00504 dc.DrawLine((firstPoint->getRealX()-minX-offsetX)*scaleX+ offsetpx,(firstPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy,(x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
00505
00506
00507 else if(sizeP==1 )
00508 {
00509 dc.DrawLine((lastPoint->getRealX()-minX-offsetX)*scaleX + offsetpx,(lastPoint->getRealY()-minY-offsetY)*scaleY+ offsetpy, (x-minX)*scaleX+ offsetpx, (y-minY)*scaleY+ offsetpy);
00510 }
00511 }
00512
00513 if( w.drawGuideLines() )
00514 {
00515 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
00516
00517 dc.DrawLine( 0, (y-minY)*scaleY + offsetpy, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
00518
00519 dc.DrawLine( (x-minX)*scaleX + offsetpx,0, (x-minX)*scaleX + offsetpx, (y-minY)*scaleY + offsetpy);
00520 }
00521 }
00522
00523
00529 void pPlotterLayer::Plot(wxDC & dc, mpWindow & w)
00530 {
00531 bool show,drawing,actual;
00532 int scrwX,scrwY,maxX,maxY,minX,minY,type;
00533 float factorZoom;
00534
00535 ifShowPoints(show);
00536 getDrawing(drawing);
00537 type=vGetType();
00538
00539 dc.SetPen( m_pen);
00540
00541
00542
00543
00544
00545 dc.GetSize(&scrwX, &scrwY);
00546
00547 getIfActual(actual);
00548
00549
00550 getMaxShowed(maxX,maxY);
00551 getMinShowed(minX,minY);
00552
00553 getFactorZoom(factorZoom);
00554 getOffsets(offsetX,offsetY);
00555
00556
00557
00558
00559
00560
00561 if((maxX-minX)!=0 && (maxY-minY)!=0)
00562 {
00563
00564 scaleX=(((double)scrwX-100)/(maxX-minX))*factorZoom;
00565 scaleY=(((double)scrwY-50)/(maxY-minY))*factorZoom;
00566
00567 if(actual)
00568 {
00569 w.SetScaleX(scaleX);
00570 w.SetScaleY(scaleY);
00571
00572
00573 }
00574
00575 }
00576
00577
00578
00579 int orgx=70;
00580
00581
00582
00583
00584
00585
00586
00587 int orgy = 40;
00588
00589 dc.SetDeviceOrigin( orgx ,0);
00590 int sizedc = dc.GetSize().GetHeight()-orgy;
00591
00592
00593
00594
00595
00596
00597 if(!show && !drawing && type==1)
00598 drawFunction(dc,w,orgy);
00599 else if(!show && !drawing && type==2)
00600 drawSplineCurve(dc,w,orgy);
00601
00602 else if(drawing && type==1)
00603 {
00604 drawFunction(dc,w,orgy);
00605 drawLineToMousePoint(dc,w,orgy);
00606 drawPoints(dc,w,orgy);
00607 }
00608 else if(drawing && type==1)
00609 {
00610 drawSplineCurve(dc,w,orgy);
00611 drawLineToMousePoint(dc,w,orgy);
00612 drawPoints(dc,w,orgy);
00613 }
00614 else if(show && type==1)
00615 {
00616 drawFunction(dc,w,orgy);
00617 drawPoints(dc,w,orgy);
00618 }
00619 else if(show && type==2)
00620 {
00621 drawSplineCurve(dc,w,orgy);
00622 drawPoints(dc,w,orgy);
00623 }
00624
00625
00626 if ( actual )
00627 {
00628 dc.SetPen(wxPen( wxColour(255,0,0),1,wxDOT ));
00629 int offsetpx = getOffsetPixelsXv();
00630
00631
00632 int realX_guide = w.getRealGuideX();
00633 if( realX_guide!=-1 )
00634 {
00635 dc.DrawLine( (realX_guide-offsetX)*scaleX + offsetpx,
00636 GetYTranslated(sizedc,0),
00637 (realX_guide-offsetX)*scaleX + offsetpx,
00638 GetYTranslated(sizedc,scrwY));
00639 }
00640
00641 int realY_guide = w.getRealGuideY();
00642 if( realY_guide!=-1 )
00643 {
00644 dc.DrawLine( 0,
00645 GetYTranslated(sizedc,(realY_guide-offsetX)*scaleX + offsetpx),
00646 scrwX,
00647 GetYTranslated(sizedc,(realY_guide-offsetX)*scaleX + offsetpx));
00648 }
00649 }
00650
00651 }
00652