1 // Created on: 1992-04-06
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Draw_Viewer.hxx>
18 #include <Draw_View.hxx>
21 #include <gp_Pnt2d.hxx>
22 #include <Draw_Window.hxx>
23 #include <Draw_Display.hxx>
24 #include <TCollection_AsciiString.hxx>
28 #define MotionNotify 6
29 static const Standard_Real DRAWINFINITE = 1e50;
30 Standard_EXPORT Standard_Boolean Draw_Bounds = Standard_True;
31 extern Standard_Boolean Draw_Batch;
32 const Standard_Integer MAXSEGMENT = 1000;
33 Segment segm[MAXSEGMENT];
35 static Draw_View* curview = NULL;
36 static Standard_Integer curviewId = 0;
37 static char blank[2] = "";
38 static Standard_Real xmin,xmax,ymin,ymax;
39 static Standard_Boolean found = Standard_False;
40 static Standard_Integer xpick, ypick, precpick;
41 static gp_Pnt lastPickP1;
42 static gp_Pnt lastPickP2;
43 static Standard_Real lastPickParam;
44 static Draw_Color highlightcol;
45 static Draw_Color currentcolor;
46 static Standard_Boolean highlight = Standard_False;
47 static Standard_Integer ps_vx, ps_vy;
48 static Standard_Real ps_kx, ps_ky;
49 static Standard_Integer ps_px, ps_py;
50 static std::ostream* ps_stream;
51 static Standard_Integer ps_width[MAXCOLOR];
52 static Standard_Real ps_gray[MAXCOLOR];
54 enum DrawingMode {DRAW, PICK, POSTSCRIPT};
55 static DrawingMode CurrentMode = DRAW;
57 //=======================================================================
60 //=======================================================================
62 Draw_Viewer::Draw_Viewer()
64 if (Draw_Batch) return;
66 for ( i = 0; i < MAXVIEW; i++) myViews[i] = NULL;
67 for (i = 0; i < MAXCOLOR; i++) {
73 //=======================================================================
74 //function : DefineColor
76 //=======================================================================
78 Standard_Boolean Draw_Viewer::DefineColor (const Standard_Integer i, const char* colname)
80 if (Draw_Batch) return 1;
81 return Draw_Window::DefineColor(i,colname);
85 //=======================================================================
88 //=======================================================================
90 void Draw_Viewer::MakeView(const Standard_Integer id,
92 const Standard_Integer X, const Standard_Integer Y,
93 const Standard_Integer W, const Standard_Integer H)
95 if (Draw_Batch) return;
99 myViews[id] = new Draw_View(id,this,X , Y, W, H);
102 myViews[id]->SetDx(W / 2);
103 myViews[id]->SetDy(- H / 2);
105 if (!myViews[id]->Init(typ))
113 //=======================================================================
114 //function : MakeView
116 //=======================================================================
118 void Draw_Viewer::MakeView(const Standard_Integer id,
120 const Standard_Integer X, const Standard_Integer Y,
121 const Standard_Integer W, const Standard_Integer H,
122 HWND win, Standard_Boolean useBuffer)
124 if (Draw_Batch) return;
128 myViews[id] = new Draw_View(id, this, X, Y, W, H, win);
129 myViews[id]->SetUseBuffer(useBuffer);
132 myViews[id]->SetDx( W / 2);
133 myViews[id]->SetDy(-H / 2);
135 if (!myViews[id]->Init(typ))
142 //=======================================================================
143 //function : MakeView
145 //=======================================================================
147 void Draw_Viewer::MakeView (const Standard_Integer id,
151 if (Draw_Batch) return;
155 myViews[id] = new Draw_View(id,this,window);
158 myViews[id]->SetDx(myViews[id]->WidthWin() / 2);
159 myViews[id]->SetDy(-myViews[id]->HeightWin() / 2);
161 if (!myViews[id]->Init(typ))
169 //=======================================================================
170 //function : SetTitle
172 //=======================================================================
174 void Draw_Viewer::SetTitle (const Standard_Integer id, const char* name)
176 if (Draw_Batch) return;
177 if(myViews[id]) myViews[id]->SetTitle (name);
180 //=======================================================================
181 //function : ResetView
182 //purpose : reset view zoom and axes
183 //=======================================================================
185 void Draw_Viewer::ResetView(const Standard_Integer id)
187 if (Draw_Batch) return;
189 myViews[id]->Init(myViews[id]->Type());
194 //=======================================================================
197 //=======================================================================
199 void Draw_Viewer::SetZoom (const Standard_Integer id, const Standard_Real z)
204 Draw_View* aView = myViews[id];
207 Standard_Real zz = z / aView->GetZoom();
209 Standard_Integer X,Y,W,H;
210 GetPosSize(id,X,Y,W,H);
212 const Standard_Real w = 0.5 * static_cast<Standard_Real>(W);
213 const Standard_Real h = 0.5 * static_cast<Standard_Real>(H);
215 const Standard_Integer aDx = static_cast<Standard_Integer>
216 ( w - zz * (w - aView->GetDx()) );
217 const Standard_Integer aDy = static_cast<Standard_Integer>
218 ( -h + zz * (h + aView->GetDy()) );
225 //=======================================================================
226 //function : RotateView
228 //=======================================================================
230 void Draw_Viewer::RotateView (const Standard_Integer id,
232 const Standard_Real A)
234 if (Draw_Batch) return;
236 gp_Trsf T = myViews[id]->GetMatrix();
240 gp_Dir DD(D.X(),D.Y(),0);
243 RotateView(id,PP,DD,A);
247 //=======================================================================
248 //function : RotateView
250 //=======================================================================
252 void Draw_Viewer::RotateView (const Standard_Integer id,
255 const Standard_Real A)
257 if (Draw_Batch) return;
260 T.SetRotation(gp_Ax1(P,D),A);
261 myViews[id]->Transform(T);
266 //=======================================================================
267 //function : SetFocal
269 //=======================================================================
271 void Draw_Viewer::SetFocal (const Standard_Integer id, const Standard_Real F)
273 if (Draw_Batch) return;
275 myViews[id]->SetFocalDistance(F);
278 //=======================================================================
281 //=======================================================================
283 char* Draw_Viewer::GetType (const Standard_Integer id) const
285 if (Draw_Batch) return blank;
287 return const_cast<char*>(myViews[id]->Type());
292 //=======================================================================
295 //=======================================================================
297 Standard_Real Draw_Viewer::Zoom (const Standard_Integer id) const
299 if (Draw_Batch) return Standard_False;
301 return myViews[id]->GetZoom();
306 //=======================================================================
309 //=======================================================================
311 Standard_Real Draw_Viewer::Focal (const Standard_Integer id) const
313 if (Draw_Batch) return 1.;
315 return myViews[id]->GetFocalDistance();
320 //=======================================================================
323 //=======================================================================
325 void Draw_Viewer::GetTrsf (const Standard_Integer id,gp_Trsf& T) const
327 if (Draw_Batch) return;
329 T = myViews[id]->GetMatrix();
332 //=======================================================================
335 //=======================================================================
337 Standard_Boolean Draw_Viewer::Is3D (const Standard_Integer id) const
339 if (Draw_Batch) return Standard_False;
341 return !myViews[id]->Is2D();
343 return Standard_False;
346 //=======================================================================
349 //=======================================================================
351 void Draw_Viewer::SetTrsf (const Standard_Integer id,gp_Trsf& T)
353 if (Draw_Batch) return;
355 myViews[id]->SetMatrix(T);
358 //=======================================================================
359 //function : GetPosSize
361 //=======================================================================
363 void Draw_Viewer::GetPosSize(const Standard_Integer id,
364 Standard_Integer& X, Standard_Integer& Y,
365 Standard_Integer& W, Standard_Integer& H)
367 if (Draw_Batch) return;
368 if (myViews[id] != NULL) {
369 myViews[id]->GetPosition(X, Y);
370 W = myViews[id]->WidthWin();
371 H = myViews[id]->HeightWin();
375 //=======================================================================
376 //function : GetFrame
378 //=======================================================================
380 void Draw_Viewer::GetFrame(const Standard_Integer id,
381 Standard_Integer& xminf, Standard_Integer& yminf,
382 Standard_Integer& xmaxf, Standard_Integer& ymaxf)
384 if (Draw_Batch) return;
386 Standard_Integer X,Y,H,W;
387 GetPosSize(id,X,Y,W,H);
388 xminf = - myViews[id]->GetDx();
389 xmaxf = W - myViews[id]->GetDx();
390 yminf = - myViews[id]->GetDy() - H;
391 ymaxf = - myViews[id]->GetDy();
395 //=======================================================================
398 //=======================================================================
400 void Draw_Viewer::FitView(const Standard_Integer id, const Standard_Integer frame)
402 if (Draw_Batch) return;
405 // is this the only view in its category
406 Standard_Boolean is2d = myViews[id]->Is2D();
407 Standard_Integer i,nbviews = 0;
408 for (i = 1; i < MAXVIEW; i++) {
410 if (myViews[i]->Is2D() == is2d)
414 Standard_Boolean only = (nbviews == 1);
416 Standard_Integer X,Y,H,W;
417 GetPosSize(id,X,Y,W,H);
418 // compute the min max
419 Standard_Integer n = myDrawables.Length();
422 curview = myViews[id];
423 Standard_Real umin,umax,vmin,vmax;
424 Standard_Real u1,u2,v1,v2;
425 umin = vmin = DRAWINFINITE;
426 umax = vmax = -DRAWINFINITE;
428 for (i = 1; i <= n; i++) {
429 Standard_Boolean d3d = myDrawables(i)->Is3D();
430 if ((d3d && !is2d) || (!d3d && is2d)) {
431 // if this is not the only view recompute...
433 DrawOnView(id,myDrawables(i));
434 myDrawables(i)->Bounds(u1,u2,v1,v2);
435 if (u1 < umin) umin = u1;
436 if (u2 > umax) umax = u2;
437 if (v1 < vmin) vmin = v1;
438 if (v2 > vmax) vmax = v2;
442 umin = umin / curview->GetZoom();
443 vmin = vmin / curview->GetZoom();
444 umax = umax / curview->GetZoom();
445 vmax = vmax / curview->GetZoom();
446 if ((umax - umin) < 1.e-6) {
447 if ((vmax - vmin) < 1.e-6)
450 z = ((Standard_Real)(H - 2*frame)) / (vmax - vmin);
453 z = ((Standard_Real)(W - 2*frame)) /((Standard_Real) (umax - umin));
454 if ((vmax - vmin) > 1.e-6) {
455 Standard_Real z2 = ((Standard_Real)(H - 2*frame)) /(vmax - vmin);
460 curview->SetDx( static_cast<Standard_Integer>( W / 2 - 0.5 * (umin+umax) * z) );
461 curview->SetDy( static_cast<Standard_Integer>(-H / 2 - 0.5 * (vmin+vmax) * z) );
465 //=======================================================================
468 //=======================================================================
470 void Draw_Viewer::PanView(const Standard_Integer id,
471 const Standard_Integer DX, const Standard_Integer DY)
473 if (Draw_Batch) return;
475 myViews[id]->SetDx(myViews[id]->GetDx() + DX);
476 myViews[id]->SetDy(myViews[id]->GetDy() + DY);
481 //=======================================================================
484 //=======================================================================
486 void Draw_Viewer::SetPan(const Standard_Integer id,
487 const Standard_Integer DX, const Standard_Integer DY)
489 if (Draw_Batch) return;
491 myViews[id]->SetDx(DX);
492 myViews[id]->SetDy(DY);
496 //=======================================================================
499 //=======================================================================
501 void Draw_Viewer::GetPan(const Standard_Integer id,
502 Standard_Integer& DX, Standard_Integer& DY)
504 if (Draw_Batch) return;
506 DX = myViews[id]->GetDx();
507 DY = myViews[id]->GetDy();
511 //=======================================================================
514 //=======================================================================
516 Standard_Boolean Draw_Viewer::HasView(const Standard_Integer id) const
518 if (Draw_Batch) return Standard_False;
519 if ((id < 0) || id >= MAXVIEW) return Standard_False;
520 return myViews[id] != NULL;
523 //=======================================================================
524 //function : DisplayView
526 //=======================================================================
528 void Draw_Viewer::DisplayView (const Standard_Integer id) const
530 if (Draw_Batch) return;
531 if (myViews[id]) myViews[id]->DisplayWindow();
534 //=======================================================================
535 //function : HideView
537 //=======================================================================
539 void Draw_Viewer::HideView (const Standard_Integer id) const
541 if (Draw_Batch) return;
547 //=======================================================================
548 //function : ClearView
550 //=======================================================================
552 void Draw_Viewer::ClearView(const Standard_Integer id) const
554 if (Draw_Batch) return;
555 if (myViews[id]) myViews[id]->Clear();
558 //=======================================================================
559 //function : RemoveView
561 //=======================================================================
563 void Draw_Viewer::RemoveView(const Standard_Integer id)
565 if (Draw_Batch) return;
572 //=======================================================================
573 //function : RepaintView
575 //=======================================================================
576 void Draw_Viewer::RepaintView (const Standard_Integer id) const
578 if (Draw_Batch) return;
581 Standard_Integer n = myDrawables.Length();
582 for (Standard_Integer i = 1; i <= n; i++)
583 DrawOnView(id,myDrawables(i));
589 //=======================================================================
590 //function : ResizeView
591 //purpose : WNT re-drawing optimization
592 //=======================================================================
593 void Draw_Viewer::ResizeView (const Standard_Integer id) const
595 if (Draw_Batch) return;
596 if (myViews[id] && myViews[id]->GetUseBuffer()) {
597 myViews[id]->InitBuffer();
602 //=======================================================================
603 //function : UpdateView
604 //purpose : WNT re-drawing optimization
605 //=======================================================================
606 void Draw_Viewer::UpdateView (const Standard_Integer id, const Standard_Boolean forced) const
608 if (Draw_Batch) return;
610 if (!myViews[id]->GetUseBuffer() || forced) {
613 // Fast redrawing on WNT
614 if (myViews[id]->GetUseBuffer()) myViews[id]->Redraw();
619 //=======================================================================
620 //function : ConfigView
622 //=======================================================================
624 void Draw_Viewer::ConfigView (const Standard_Integer id) const
626 if (Draw_Batch) return;
629 myViews[id]->SetDx(myViews[id]->WidthWin() / 2);
630 myViews[id]->SetDy(-myViews[id]->HeightWin() / 2);
634 //=======================================================================
635 //function : PostScriptView
637 //=======================================================================
639 void Draw_Viewer::PostScriptView (const Standard_Integer id,
640 const Standard_Integer VXmin,
641 const Standard_Integer VYmin,
642 const Standard_Integer VXmax,
643 const Standard_Integer VYmax,
644 const Standard_Integer PXmin,
645 const Standard_Integer PYmin,
646 const Standard_Integer PXmax,
647 const Standard_Integer PYmax,
648 std::ostream& sortie) const
650 if (Draw_Batch) return;
656 ps_kx = ((Standard_Real) (PXmax - PXmin)) / ((Standard_Real) (VXmax - VXmin));
657 ps_ky = ((Standard_Real) (PYmax - PYmin)) / ((Standard_Real) (VYmax - VYmin));
659 Standard_Integer n = myDrawables.Length();
661 CurrentMode = POSTSCRIPT;
662 Draw_Display DF = MakeDisplay(id);
663 Standard_Boolean view2d = myViews[id]->Is2D();
664 for (Standard_Integer i = 1; i <= n; i++)
665 if (myDrawables(i)->Is3D()) {
666 if (!view2d) myDrawables(i)->DrawOn(DF);
669 if (view2d) myDrawables(i)->DrawOn(DF);
671 sortie << "stroke\n";
676 //=======================================================================
677 //function : PostColor
679 //=======================================================================
681 void Draw_Viewer::PostColor(const Standard_Integer icol,
682 const Standard_Integer width,
683 const Standard_Real gray)
685 if (Draw_Batch) return;
686 if ((icol < 0) || (icol >= MAXCOLOR)) return;
687 ps_width[icol] = width;
688 ps_gray[icol] = gray;
691 //=======================================================================
692 //function : SaveView
694 //=======================================================================
696 Standard_Boolean Draw_Viewer::SaveView(const Standard_Integer id,
697 const char* filename)
701 return Standard_False;
705 return myViews[id]->Save(filename);
709 std::cerr << "View " << id << " doesn't exists!\n";
710 return Standard_False;
714 //=======================================================================
715 //function : RepaintAll
717 //=======================================================================
719 void Draw_Viewer::RepaintAll () const
721 if (Draw_Batch) return;
722 for (Standard_Integer id = 0; id < MAXVIEW; id++)
726 //=======================================================================
727 //function : Repaint2D
729 //=======================================================================
731 void Draw_Viewer::Repaint2D () const
733 if (Draw_Batch) return;
734 for (Standard_Integer id = 0; id < MAXVIEW; id++)
736 if (myViews[id]->Is2D())
741 //=======================================================================
742 //function : Repaint3D
744 //=======================================================================
746 void Draw_Viewer::Repaint3D () const
748 if (Draw_Batch) return;
749 for (Standard_Integer id = 0; id < MAXVIEW; id++)
751 if (!myViews[id]->Is2D())
756 //=======================================================================
757 //function : DeleteView
759 //=======================================================================
761 void Draw_Viewer::DeleteView(const Standard_Integer id)
763 if (Draw_Batch) return;
764 if (myViews[id] != NULL) {
770 //=======================================================================
773 //=======================================================================
775 void Draw_Viewer::Clear()
777 if (Draw_Batch) return;
778 for (Standard_Integer i = 1; i <= myDrawables.Length(); i++)
779 myDrawables(i)->Visible(Standard_False);
781 for (Standard_Integer id = 0; id < MAXVIEW; id++)
785 //=======================================================================
788 //=======================================================================
790 void Draw_Viewer::Clear2D()
792 if (Draw_Batch) return;
793 Standard_Integer i = 1;
794 while (i <= myDrawables.Length()) {
795 if (myDrawables(i)->Is3D())
798 myDrawables(i)->Visible(Standard_False);
799 myDrawables.Remove(i);
802 for (Standard_Integer id = 0; id < MAXVIEW; id++) {
804 if (myViews[id]->Is2D())
810 //=======================================================================
813 //=======================================================================
815 void Draw_Viewer::Clear3D()
817 if (Draw_Batch) return;
818 Standard_Integer i = 1;
819 while (i <= myDrawables.Length()) {
820 if (myDrawables(i)->Is3D()) {
821 myDrawables(i)->Visible(Standard_False);
822 myDrawables.Remove(i);
827 for (Standard_Integer id = 0; id < MAXVIEW; id++) {
829 if (!myViews[id]->Is2D())
835 //=======================================================================
838 //=======================================================================
840 void Draw_Viewer::Flush()
842 if (Draw_Batch) return;
843 Draw_Window::Flush();
846 //=======================================================================
847 //function : DrawOnView
849 //=======================================================================
851 void Draw_Viewer::DrawOnView(const Standard_Integer id,
852 const Handle(Draw_Drawable3D)& D) const
854 if (Draw_Batch) return;
856 Draw_Display d = MakeDisplay(id);
857 xmin = ymin = DRAWINFINITE;
858 xmax = ymax = -DRAWINFINITE;
860 Standard_Boolean view2d = myViews[id]->Is2D();
861 myViews[id]->ResetFrame();
862 if ((D->Is3D() && !view2d) || (!D->Is3D() && view2d))
865 if (CurrentMode == DRAW)
866 D->SetBounds(xmin,xmax,ymin,ymax);
872 //=======================================================================
873 //function : HighlightOnView
875 //=======================================================================
877 void Draw_Viewer::HighlightOnView (const Standard_Integer id,
878 const Handle(Draw_Drawable3D)& D,
879 const Draw_ColorKind C) const
881 if (Draw_Batch) return;
882 highlight = Standard_True;
885 highlight = Standard_False;
888 //=======================================================================
889 //function : AddDrawable
891 //=======================================================================
893 void Draw_Viewer::AddDrawable (const Handle(Draw_Drawable3D)& D)
895 if (Draw_Batch) return;
896 if (!D.IsNull() && !D->Visible()) {
897 myDrawables.Append(D);
898 D->Visible(Standard_True);
902 //=======================================================================
903 //function : RemoveDrawable
905 //=======================================================================
907 void Draw_Viewer::RemoveDrawable (const Handle(Draw_Drawable3D)& D)
909 if (Draw_Batch) return;
910 if (!D.IsNull() && D->Visible()) {
911 Standard_Integer index;
912 for (index = 1; index <= myDrawables.Length(); index++) {
913 if (myDrawables(index) == D) {
914 D->Visible(Standard_False);
915 myDrawables.Remove(index);
922 //=======================================================================
923 //function : MakeDisplay
924 //purpose : return a display on the view
925 //=======================================================================
927 Draw_Display Draw_Viewer::MakeDisplay (const Standard_Integer id) const
929 if (Draw_Batch) {Draw_Display dis;return dis;}
931 curview = myViews[id];
933 Draw_Color initcol(Draw_blanc);
934 // to force setting the color
935 currentcolor = Draw_Color(Draw_rouge);
937 dis.SetColor(initcol);
938 dis.SetMode(0x3 /*GXcopy*/);
942 //=======================================================================
945 //=======================================================================
947 void Draw_Viewer::Select (Standard_Integer& id, Standard_Integer& X, Standard_Integer& Y,
948 Standard_Integer& Button, Standard_Boolean wait)
950 if (Draw_Batch) return;
951 id = X = Y = Button = 0;
952 Standard_Boolean hasView = Standard_False;
953 for (int aViewIter = 0; aViewIter < MAXVIEW; ++aViewIter)
955 if (myViews[aViewIter] != NULL
956 && myViews[aViewIter]->IsMapped())
958 hasView = Standard_True;
964 std::cerr << "No selection is possible with no open views\n";
968 #if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
970 if (id >=0 && id < MAXVIEW) {
971 if (myViews[id]) myViews[id]->Wait(wait);
975 for(int i=0 ; i<MAXVIEW ; i++)
976 if (myViews[i]) myViews[i]->Wait(wait);
979 Standard_Boolean again = Standard_True;
991 for (iv = 0; iv < MAXVIEW; iv++) {
993 if (myViews[iv]->win == ev.window)
997 if (wait || id == iv) {
1007 again = Standard_False;
1016 again = Standard_False;
1022 X = X - myViews[id]->GetDx();
1023 Y = -Y - myViews[id]->GetDy();
1025 if (!wait) myViews[id]->Wait(!wait);
1026 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
1027 Standard_Integer aWindowNumber;
1030 while (id >= MAXVIEW)
1032 GetNextEvent(wait, aWindowNumber, X, Y, Button);
1036 continue; // mouse clicked on window title
1039 for (Standard_Integer anIter = 0; anIter < MAXVIEW; anIter++)
1041 if (myViews[anIter] && myViews[anIter]->IsEqualWindows (aWindowNumber))
1048 X = X - myViews[id]->GetDx();
1049 Y = -Y - myViews[id]->GetDy();
1054 id = MAXVIEW; //:abv 29.05.02: cycle for working in console mode
1055 while ( id >= MAXVIEW ) {
1057 Draw_Window::SelectWait(hWnd, X, Y, Button);
1059 Draw_Window::SelectNoWait(hWnd, X, Y, Button);
1061 // Recherche du numero de la vue grace au HANDLE
1062 for(int i=0 ; i<MAXVIEW ; i++)
1063 if (myViews[i] && myViews[i]->win == hWnd ) id = i;
1065 X = X - myViews[id]->GetDx();
1066 Y = -Y - myViews[id]->GetDy();
1070 //=======================================================================
1073 //=======================================================================
1075 Standard_Integer Draw_Viewer::Pick(const Standard_Integer id,
1076 const Standard_Integer X, const Standard_Integer Y, const Standard_Integer Prec,
1077 Handle(Draw_Drawable3D)& D,
1078 const Standard_Integer first) const
1080 if (Draw_Batch) return 0;
1081 if (myViews[id] == NULL)
1084 // is this the only view in its category
1085 Standard_Boolean is2d = myViews[id]->Is2D();
1086 Standard_Integer i,nbviews = 0;
1087 for (i = 0; i < MAXVIEW; i++)
1090 if (myViews[i]->Is2D() == is2d)
1093 Standard_Boolean only = (nbviews == 1);
1099 found = Standard_False;
1100 Standard_Real x1,x2,y1,y2;
1101 for (i = first+1; i <= myDrawables.Length(); i++) {
1102 Standard_Boolean reject = Standard_False;
1103 // rejection if only view
1105 myDrawables(i)->Bounds(x1,x2,y1,y2);
1106 if ((xpick+Prec < x1) || (xpick-Prec > x2) ||
1107 (ypick+Prec < y1) || (ypick-Prec > y2))
1108 reject = Standard_True;
1111 DrawOnView(id,myDrawables(i));
1117 found = Standard_False;
1118 if (i <= myDrawables.Length())
1125 //=======================================================================
1126 //function : LastPick
1128 //=======================================================================
1130 void Draw_Viewer::LastPick(gp_Pnt& P1, gp_Pnt& P2, Standard_Real& Param)
1132 if (Draw_Batch) return;
1135 Param = lastPickParam;
1138 //=======================================================================
1139 //function : ~Draw_Viewer
1141 //=======================================================================
1143 Draw_Viewer::~Draw_Viewer()
1145 if (Draw_Batch) return;
1146 for (Standard_Integer id = 0; id < MAXVIEW; id++)
1150 //=======================================================================
1151 //function : operator<<
1153 //=======================================================================
1155 Draw_Viewer& Draw_Viewer::operator<<(const Handle(Draw_Drawable3D)& d3d)
1157 if (Draw_Batch) return *this;
1158 if (!d3d.IsNull()) {
1160 for (Standard_Integer id = 0; id < MAXVIEW; id++)
1166 //=======================================================================
1167 //function : GetDrawables
1169 //=======================================================================
1170 const Draw_SequenceOfDrawable3D& Draw_Viewer::GetDrawables()
1175 // *******************************************************************
1177 // *******************************************************************
1182 if (Draw_Batch) return;
1183 if (highlight) curview->SetColor(highlightcol.ID());
1184 curview->DrawSegments(segm,nbseg);
1188 //=======================================================================
1189 //function : SetColor
1191 //=======================================================================
1193 void Draw_Display::SetColor (const Draw_Color& col) const
1195 if (Draw_Batch) return;
1196 if (col.ID() == currentcolor.ID()) return;
1199 switch (CurrentMode) {
1203 curview->SetColor(col.ID());
1207 (*ps_stream) << "stroke\nnewpath\n";
1208 (*ps_stream) << ps_width[col.ID()]<<" setlinewidth\n";
1209 (*ps_stream) << ps_gray[col.ID()]<<" setgray\n";
1216 //=======================================================================
1217 //function : SetMode
1219 //=======================================================================
1221 void Draw_Display::SetMode (const Standard_Integer M) const
1223 if (Draw_Batch) return;
1224 switch (CurrentMode) {
1228 curview->SetMode(M);
1237 //=======================================================================
1240 //=======================================================================
1242 Standard_Real Draw_Display::Zoom() const
1244 if (Draw_Batch) return 1.;
1245 return curview->GetZoom();
1248 //=======================================================================
1251 //=======================================================================
1253 void Draw_Display::Flush () const
1255 if (Draw_Batch) return;
1259 //=======================================================================
1260 //function : DrawString
1262 //=======================================================================
1264 void Draw_Display::DrawString(const gp_Pnt2d& ppt,
1265 const Standard_CString S,
1266 const Standard_Real moveX,
1267 const Standard_Real moveY)
1269 if (Draw_Batch) return;
1270 if (ppt.X() > 1.e09 || ppt.X() < -1.e09 ) return;
1271 if (ppt.Y() > 1.e09 || ppt.Y() < -1.e09 ) return;
1273 gp_Pnt2d pt(ppt.X()*curview->GetZoom(),ppt.Y()*curview->GetZoom());
1275 if (pt.X() > 1.e09 || pt.X() < -1.e09 ) return;
1276 if (pt.Y() > 1.e09 || pt.Y() < -1.e09 ) return;
1278 switch (CurrentMode) {
1282 int X = (int) ( pt.X() + moveX + curview->GetDx());
1283 int Y = (int) (-pt.Y() + moveY - curview->GetDy());
1284 curview->DrawString(X,Y,(char *)S);
1286 if (pt.X() + moveX > xmax) xmax = pt.X();
1287 if (pt.X() + moveX < xmin) xmin = pt.X();
1288 if (-pt.Y() - moveY > ymax) ymax = -pt.Y();
1289 if (-pt.Y() - moveY < ymin) ymin = -pt.Y();
1296 Standard_Integer x = (Standard_Integer )( (pt.X() + moveX - ps_vx) * ps_kx + ps_px);
1297 Standard_Integer y = (Standard_Integer )( (pt.Y() + moveY - ps_vy) * ps_ky + ps_py);
1298 (*ps_stream) <<"stroke\n";
1299 (*ps_stream) << x << " " << y << " m\n";
1300 (*ps_stream) <<"("<<S<<") show\nnewpath\n";
1309 //=======================================================================
1310 //function : DrawString
1312 //=======================================================================
1314 void Draw_Display::DrawString(const gp_Pnt2d& ppt,
1315 const Standard_CString S)
1317 if (Draw_Batch) return;
1318 DrawString(ppt,S,0.0,0.0);
1321 //=======================================================================
1322 //function : DrawString
1324 //=======================================================================
1326 void Draw_Display::DrawString(const gp_Pnt& pt,
1327 const Standard_CString S,
1328 const Standard_Real moveX,
1329 const Standard_Real moveY)
1331 if (Draw_Batch) return;
1332 DrawString(Project(pt),S,moveX,moveY);
1335 //=======================================================================
1336 //function : DrawString
1338 //=======================================================================
1340 void Draw_Display::DrawString(const gp_Pnt& pt,
1341 const Standard_CString S)
1343 if (Draw_Batch) return;
1344 DrawString(Project(pt),S,0.0,0.0);
1348 // *******************************************************************
1349 // Drawing data static variables
1350 // *******************************************************************
1351 static gp_Pnt2d PtCur; // current 2D point
1352 static gp_Pnt PtPers; // current 3D point for Pers
1354 //=======================================================================
1355 //function : Project
1357 //=======================================================================
1359 void Draw_Display::Project(const gp_Pnt& p, gp_Pnt2d& p2d) const
1361 if (Draw_Batch) return;
1363 pt.Transform(curview->GetMatrix());
1364 Standard_Real xp,yp,zp;
1366 if (curview->IsPerspective()) {
1367 const Standard_Real aDistance = curview->GetFocalDistance();
1368 xp = xp * aDistance / (aDistance-zp);
1369 yp = yp * aDistance / (aDistance-zp);
1371 p2d.SetCoord(xp,yp);
1374 //=======================================================================
1375 //function : Draw_Display
1377 //=======================================================================
1379 Draw_Display::Draw_Display ()
1381 if (Draw_Batch) return;
1383 PtPers.SetCoord(0., 0., 0.);
1384 PtPers.Transform(curview->GetMatrix());
1385 PtCur.SetCoord(PtPers.X()*curview->GetZoom(),PtPers.Y()*curview->GetZoom());
1389 //=======================================================================
1390 //function : MoveTo 2D
1392 //=======================================================================
1394 void Draw_Display::MoveTo (const gp_Pnt2d& pp)
1396 if (Draw_Batch) return;
1397 const Standard_Real aZoom = curview->GetZoom();
1398 gp_Pnt2d pt(pp.X() * aZoom, pp.Y() * aZoom);
1399 switch (CurrentMode) {
1404 if (pt.X() > xmax) xmax = pt.X();
1405 if (pt.X() < xmin) xmin = pt.X();
1406 if (pt.Y() > ymax) ymax = pt.Y();
1407 if (pt.Y() < ymin) ymin = pt.Y();
1417 Standard_Integer x = (Standard_Integer )( (pt.X() - ps_vx) * ps_kx + ps_px);
1418 Standard_Integer y = (Standard_Integer )( (pt.Y() - ps_vy) * ps_ky + ps_py);
1419 (*ps_stream) <<"stroke\nnewpath\n"<< x << " " << y << " m\n";
1425 //=======================================================================
1426 //function : DrawTo 2D
1428 //=======================================================================
1429 inline Standard_Integer CalculRegion(const Standard_Real x,
1430 const Standard_Real y,
1431 const Standard_Real x1,
1432 const Standard_Real y1,
1433 const Standard_Real x2,
1434 const Standard_Real y2) {
1436 if(x<x1) { r=1; } else { if(x>x2) { r=2; } else { r=0; } }
1437 if(y<y1) { r|=4; } else { if(y>y2) { r|=8; } }
1441 Standard_Boolean Trim(gp_Pnt2d& P1,gp_Pnt2d& P2,
1446 Standard_Real xa=P1.X(),ya=P1.Y(),xb=P2.X(),yb=P2.Y();
1448 Standard_Integer regiona=0,regionb=0;
1449 regiona = CalculRegion(xa,ya,x0,y0,x1,y1);
1450 regionb = CalculRegion(xb,yb,x0,y0,x1,y1);
1451 if((regiona & regionb)==0) {
1452 Standard_Real dx=xb-xa;
1453 Standard_Real dy=yb-ya;
1454 Standard_Real dab=sqrt(dx*dx+dy*dy);
1455 if(dab<1e-10) return(Standard_False);
1459 Standard_Real xm,ym,mfenx,mfeny;
1460 mfenx=xm=0.5*(x0+x1);
1461 mfeny=ym=0.5*(y0+y1);
1463 Standard_Real d=sqrt(x1*x1+y1*y1) * 2;
1465 Standard_Real p=(xm-xa)*dx+(ym-ya)*dy;
1470 gp_Pnt2d MFen(mfenx,mfeny);
1471 if(MFen.SquareDistance(Pm) > d*d) return(Standard_False);
1473 Standard_Real PmDistP1 = Pm.Distance(P1);
1474 Standard_Real PmDistP2 = Pm.Distance(P2);
1476 Standard_Real amab = (xm-xa)*(xb-xa)+(ym-ya)*(yb-ya);
1478 if(amab > 0) { //-- M est compris entre A et B
1480 P1.SetCoord(xm-d*dx,ym-d*dy);
1483 P2.SetCoord(xm+d*dx,ym+d*dy);
1486 else if(PmDistP1 < PmDistP2) { //-- On a M P1 P2
1488 P2.SetCoord(xm+d*dx,ym+d*dy);
1491 else { //-- On a P1 P2 M
1493 P1.SetCoord(xm-d*dx,ym-d*dy);
1496 return(Standard_True);
1498 else return(Standard_False);
1504 void Draw_Display::DrawTo (const gp_Pnt2d& pp2)
1506 if (Draw_Batch) return;
1507 if (pp2.X() > 1.e09 || pp2.X() < -1.e09 ) return;
1508 if (pp2.Y() > 1.e09 || pp2.Y() < -1.e09 ) return;
1510 gp_Pnt2d p2(pp2.X() * curview->GetZoom(), pp2.Y() * curview->GetZoom());
1512 if (p2.X() > 1.e09 || p2.X() < -1.e09 ) return;
1513 if (p2.Y() > 1.e09 || p2.Y() < -1.e09 ) return;
1515 gp_Pnt2d p1 = PtCur;
1516 if (p1.X() > 1.e09 || p1.X() < -1.e09 ) return;
1517 if (p1.Y() > 1.e09 || p1.Y() < -1.e09 ) return;
1521 switch (CurrentMode) {
1526 Standard_Integer x0,y0,x1,y1;
1527 curview->GetFrame(x0,y0,x1,y1);
1530 //Standard_Integer qx0,qy0,qx1,qy1;
1531 //curview->viewer->GetFrame(curview->id,qx0,qy0,qx1,qy1);
1532 //if(qx0!=x0 || qx1!=x1 || qy0!=y0 || qy1!=y1) {
1533 // x0=qx0; x1=qx1; y0=qy0; y1=qy1;
1541 if(Trim(PI1,PI2,x0,y0,x1,y1))
1543 segm[nbseg].Init(static_cast<Standard_Integer>( PI1.X() + curview->GetDx()),
1544 static_cast<Standard_Integer>(-PI1.Y() - curview->GetDy()),
1545 static_cast<Standard_Integer>( PI2.X() + curview->GetDx()),
1546 static_cast<Standard_Integer>(-PI2.Y() - curview->GetDy()));
1550 segm[nbseg].Init(static_cast<Standard_Integer>( p1.X() + curview->GetDx()),
1551 static_cast<Standard_Integer>(-p1.Y() - curview->GetDy()),
1552 static_cast<Standard_Integer>( p2.X() + curview->GetDx()),
1553 static_cast<Standard_Integer>(-p2.Y() - curview->GetDy()));
1556 if (nbseg == MAXSEGMENT) {
1560 if (p2.X() > xmax) xmax = p2.X();
1561 if (p2.X() < xmin) xmin = p2.X();
1562 if (p2.Y() > ymax) ymax = p2.Y();
1563 if (p2.Y() < ymin) ymin = p2.Y();
1570 Standard_Integer x1 = (int) p1.X() ;
1571 Standard_Integer y1 = (int) p1.Y() ;
1572 Standard_Integer x2 = (int) p2.X() ;
1573 Standard_Integer y2 = (int) p2.Y() ;
1574 if ((x1 >= xpick + precpick) && (x2 >= xpick + precpick)) break;
1575 if ((x1 <= xpick - precpick) && (x2 <= xpick - precpick)) break;
1576 if ((y1 >= ypick + precpick) && (y2 >= ypick + precpick)) break;
1577 if ((y1 <= ypick - precpick) && (y2 <= ypick - precpick)) break;
1579 Standard_Boolean inside = Standard_True;
1580 if ((x1 > xpick + precpick) || (x2 > xpick + precpick)) {
1581 Standard_Real y = (Standard_Real) y1 +
1582 (Standard_Real) (y2-y1) * (Standard_Real) (xpick+precpick-x1) /
1583 (Standard_Real) (x2 - x1);
1584 if ( (y < ypick+precpick) && (y > ypick - precpick)) {
1585 found = Standard_True;
1586 lastPickParam = (Standard_Real) (xpick - x1) /
1587 (Standard_Real) (x2 - x1);
1591 inside = Standard_False;
1594 if ((x1 < xpick - precpick) || (x2 < xpick - precpick)) {
1595 Standard_Real y = (Standard_Real) y1 +
1596 (Standard_Real) (y2-y1) * (Standard_Real) (xpick-precpick-x1) /
1597 (Standard_Real) (x2 - x1);
1598 if ( (y < ypick+precpick) && (y > ypick - precpick)) {
1599 found = Standard_True;
1600 lastPickParam = (Standard_Real) (xpick - x1) /
1601 (Standard_Real) (x2 - x1);
1605 inside = Standard_False;
1609 if ((y1 > ypick + precpick) || (y2 > ypick + precpick)) {
1610 Standard_Real x = (Standard_Real) x1 +
1611 (Standard_Real) (x2-x1) * (Standard_Real) (ypick+precpick-y1) /
1612 (Standard_Real) (y2 - y1);
1613 if ( (x < xpick+precpick) && (x > xpick - precpick)) {
1614 found = Standard_True;
1615 lastPickParam = (Standard_Real) (ypick - y1) /
1616 (Standard_Real) (y2 - y1);
1620 inside = Standard_False;
1624 if ((y1 < ypick - precpick) || (y2 < ypick - precpick)) {
1625 Standard_Real x = (Standard_Real) x1 +
1626 (Standard_Real) (x2-x1) * (Standard_Real) (ypick-precpick-y1) /
1627 (Standard_Real) (y2 - y1);
1628 if ( (x < xpick+precpick) && (x > xpick - precpick)) {
1629 found = Standard_True;
1630 lastPickParam = (Standard_Real) (ypick - y1) /
1631 (Standard_Real) (y2 - y1);
1635 inside = Standard_False;
1637 found = found || inside;
1639 if (Abs(x2-x1) > Abs(y2-y1)) {
1640 if (Abs(x2-x1) < 1e-5) lastPickParam = 0;
1642 lastPickParam = (Standard_Real)(xpick - x1) /
1643 (Standard_Real)(x2 - x1);
1646 if (Abs(y2-y1) < 1e-5) lastPickParam = 0;
1648 lastPickParam = (Standard_Real)(ypick - y1) /
1649 (Standard_Real)(y2 - y1);
1657 Standard_Integer x = (Standard_Integer )( (p2.X() - ps_vx) * ps_kx + ps_px);
1658 Standard_Integer y = (Standard_Integer )( (p2.Y() - ps_vy) * ps_ky + ps_py);
1659 (*ps_stream) << x << " " << y << " l\n";
1666 //=======================================================================
1669 //=======================================================================
1671 void Draw_Display::MoveTo (const gp_Pnt& pt)
1673 if (Draw_Batch) return;
1674 if (CurrentMode == PICK) {
1675 if (!found) lastPickP1 = pt;
1679 PtPers.Transform(curview->GetMatrix());
1680 Standard_Real xp = PtPers.X();
1681 Standard_Real yp = PtPers.Y();
1682 if (curview->IsPerspective())
1684 Standard_Real ZPers = PtPers.Z();
1685 const Standard_Real aDistance = curview->GetFocalDistance();
1686 if (ZPers < aDistance * precpers)
1688 xp=xp * aDistance / (aDistance-ZPers);
1689 yp=yp * aDistance / (aDistance-ZPers);
1692 MoveTo(gp_Pnt2d(xp,yp));
1695 //=======================================================================
1698 //=======================================================================
1700 void Draw_Display::DrawTo (const gp_Pnt& pt)
1702 if (Draw_Batch) return;
1703 if ((CurrentMode == PICK) && found) return;
1705 gp_Pnt pt2 = pt.Transformed(curview->GetMatrix());
1706 Standard_Real xp2 = pt2.X();
1707 Standard_Real yp2 = pt2.Y();
1709 if (curview->IsPerspective())
1711 const Standard_Real aZoom = curview->GetZoom();
1712 const Standard_Real aDistance = curview->GetFocalDistance();
1714 Standard_Real xp1 = PtPers.X();
1715 Standard_Real yp1 = PtPers.Y();
1716 Standard_Real zp1 = PtPers.Z();
1717 Standard_Real zp2 = pt2.Z();
1719 if ((zp1 >= aDistance*precpers) && (zp2 >= aDistance*precpers) )
1721 return; // segment is not visible in perspective (behind the eye)
1723 else if (zp1 >= aDistance*precpers)
1725 xp1=xp1+(xp2-xp1)*(aDistance*precpers-zp1)/(zp2-zp1);
1726 yp1=yp1+(yp2-yp1)*(aDistance*precpers-zp1)/(zp2-zp1);
1727 zp1=aDistance*precpers;
1728 xp1=xp1*aDistance/(aDistance-zp1);
1729 yp1=yp1*aDistance/(aDistance-zp1);
1730 MoveTo( gp_Pnt2d(xp1 * aZoom, yp1 * aZoom) );
1732 else if (zp2 >= aDistance*precpers)
1734 xp2=xp2+(xp1-xp2)*(aDistance*precpers-zp2)/(zp1-zp2);
1735 yp2=yp2+(yp1-yp2)*(aDistance*precpers-zp2)/(zp1-zp2);
1736 zp2=aDistance*precpers;
1738 xp2 = xp2 * aDistance / (aDistance - zp2);
1739 yp2 = yp2 * aDistance / (aDistance - zp2);
1741 DrawTo(gp_Pnt2d(xp2,yp2));
1742 if (CurrentMode == PICK) {
1743 if (!found) lastPickP1 = pt;
1744 else lastPickP2 = pt;
1748 //=======================================================================
1751 //=======================================================================
1753 void Draw_Display::Draw (const gp_Pnt& p1, const gp_Pnt& p2)
1755 if (Draw_Batch) return;
1760 //=======================================================================
1763 //=======================================================================
1765 void Draw_Display::Draw(const gp_Pnt2d& p1, const gp_Pnt2d& p2)
1767 if (Draw_Batch) return;
1773 //=======================================================================
1776 //=======================================================================
1778 Standard_Integer Draw_Display::ViewId() const
1780 if (Draw_Batch) return 0;
1785 //=======================================================================
1786 //function : HasPicked
1788 //=======================================================================
1790 Standard_Boolean Draw_Display::HasPicked() const
1792 if (Draw_Batch) return Standard_False;