0022922: Clean up warnings on uninitialized / unused variables
[occt.git] / src / Draw / Draw_Viewer.cxx
CommitLineData
7fd59977 1// File: Draw_Viewer.cxx
2// Created: Mon Apr 6 13:12:18 1992
3// Author: Remi LEQUETTE
4// <rle@sdsun1>
5// Updated by GG Tue Oct 22 16:22:10 1996
6// reason : Try to compress the pixel image
7// in PseudoColor 8 planes format
8// see : SaveView(filename)
9//
10
11#include <Draw_Viewer.hxx>
12
13#include <gp_Pnt.hxx>
14#include <gp_Pnt2d.hxx>
15#include <Draw_Window.hxx>
16#include <Draw_Display.hxx>
17
18
19#define precpers 0.95
20#define ButtonPress 4
21#define MotionNotify 6
22static const Standard_Real DRAWINFINITE = 1e50;
23Standard_EXPORT Standard_Boolean Draw_Bounds = Standard_True;
24extern Standard_Boolean Draw_Batch;
25const Standard_Integer MAXSEGMENT = 1000;
26Segment segm[MAXSEGMENT];
27static int nbseg=0;
28static Draw_View* curview = NULL;
29static Standard_Integer curviewId = 0;
30static char blank[2] = "";
31static Standard_Real xmin,xmax,ymin,ymax;
32static Standard_Boolean found = Standard_False;
33static Standard_Integer xpick, ypick, precpick;
34static gp_Pnt lastPickP1;
35static gp_Pnt lastPickP2;
36static Standard_Real lastPickParam;
37static Draw_Color highlightcol;
38static Draw_Color currentcolor;
39static Standard_Boolean highlight = Standard_False;
40static Standard_Integer ps_vx, ps_vy;
41static Standard_Real ps_kx, ps_ky;
42static Standard_Integer ps_px, ps_py;
43static ostream* ps_stream;
44static Standard_Integer ps_width[MAXCOLOR];
45static Standard_Real ps_gray[MAXCOLOR];
46
47enum DrawingMode {DRAW, PICK, POSTSCRIPT};
48static DrawingMode CurrentMode = DRAW;
49
50/* local window class */
51
52//=======================================================================
53// class View
54//=======================================================================
55
56class Draw_View : public Draw_Window {
57 public :
58 Draw_View(Standard_Integer i,
59 Draw_Viewer* v,
60 Standard_Integer X,
61 Standard_Integer Y,
62 Standard_Integer W,
63 Standard_Integer H);
64
65#ifdef WNT
66 Draw_View(Standard_Integer i,
67 Draw_Viewer* v,
68 Standard_Integer X,
69 Standard_Integer Y,
70 Standard_Integer W,
71 Standard_Integer H,
72 HWND win);
73#endif
74
75 Draw_View(Standard_Integer i,
76 Draw_Viewer* v,
77 const char *w);
78
79 void WExpose();
80
81 Standard_Boolean Init(const char* typ);
82 ~Draw_View() {Destroy();}
83
84 void ResetFrame() { Framex0=Framey0=Framex1=Framey1=0; }
85
86 void GetFrame(Standard_Integer& x0,Standard_Integer& y0,
87 Standard_Integer& x1,Standard_Integer& y1) {
88 if(Framex0==Framex1) {
89 viewer->GetFrame(id,x0,y0,x1,y1);
90 Framex0=x0; Framex1=x1; Framey0=y0; Framey1=y1;
91 }
92 else {
93 x0=Framex0; x1=Framex1;
94 y0=Framey0; y1=Framey1;
95 }
96 }
97
98 Standard_Integer id;
99 Draw_Viewer* viewer;
100 char type[5];
101 Standard_Integer FlagPers;
102 Standard_Integer Flag2d;
103 Standard_Real FocalDist;
104 Standard_Real Zoom;
105 gp_Trsf Matrix;
106 Standard_Integer dX;
107 Standard_Integer dY;
108 Standard_Integer lastX;
109 Standard_Integer lastY;
110 Standard_Integer Framex0,Framey0,Framex1,Framey1;
111};
112
113//=======================================================================
114//function : Draw_View
115//purpose :
116//=======================================================================
117Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v,
118 Standard_Integer X,
119 Standard_Integer Y,
120 Standard_Integer W,
121 Standard_Integer H) :
122 Draw_Window("Win", X, Y, W, H), id(i), viewer(v)
123{
124 Framex0=Framey0=Framex1=Framey1=0;
125}
126
127#ifdef WNT
128//=======================================================================
129//function : Draw_View
130//purpose :
131//=======================================================================
132Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v,
133 Standard_Integer X,
134 Standard_Integer Y,
135 Standard_Integer W,
136 Standard_Integer H,
137 HWND win) :
138 Draw_Window("Win", X, Y, W, H, win), id(i), viewer(v)
139{
140 Framex0=Framey0=Framex1=Framey1=0;
141}
142#endif
143
144//=======================================================================
145//function : Draw_View
146//purpose :
147//=======================================================================
148
149// Portage WNT
150
151#ifdef WNT
152Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v, const char* w)
153#else
154Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v, const char* w) :
155 Draw_Window(w), id(i), viewer(v)
156#endif
157{
158
159}
160
161//=======================================================================
162//function : Init
163//purpose :
164//=======================================================================
165
166Standard_Boolean Draw_View::Init (const char* typ)
167{
168 // default fields
169
170 Framex0=Framey0=Framex1=Framey1=0;
171 FocalDist = 500.;
172 FlagPers = 0;
173 Flag2d = !strcmp("-2D-",typ);
174 Zoom = 1;
175 gp_Trsf T;
176 gp_Pnt Pvise(0,0,0);
177 if (!strcmp("+X+Y",typ) || Flag2d)
178 {
179 Matrix = T;
180 }
181 else if (!strcmp("-Y+X",typ))
182 {
c6541a0c 183 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,0,1)),M_PI/2);
7fd59977 184 }
185 else if (!strcmp("-X-Y",typ))
186 {
c6541a0c 187 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,0,1)),M_PI);
7fd59977 188 }
189 else if (!strcmp("+Y-X",typ))
190 {
c6541a0c 191 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,0,1)),-M_PI/2);
7fd59977 192 }
193 else if (!strcmp("+Y+X",typ))
194 {
c6541a0c
D
195 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,0,1)),-M_PI/2);
196 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI);
7fd59977 197 Matrix.Multiply(T);
198 }
199 else if (!strcmp("-X+Y",typ))
200 {
c6541a0c 201 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI);
7fd59977 202 }
203 else if (!strcmp("-Y-X",typ))
204 {
c6541a0c
D
205 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,0,1)),-M_PI/2);
206 T.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI);
7fd59977 207 Matrix.Multiply(T);
208 }
209 else if (!strcmp("+X-Y",typ))
210 {
c6541a0c 211 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI);
7fd59977 212 }
213 else if (!strcmp("+X+Z",typ))
214 {
c6541a0c 215 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),-M_PI/2);
7fd59977 216 }
217 else if (!strcmp("-Z+X",typ))
218 {
c6541a0c
D
219 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),-M_PI/2);
220 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),-M_PI/2);
7fd59977 221 Matrix.Multiply(T);
222 }
223 else if (!strcmp("-X-Z",typ))
224 {
c6541a0c
D
225 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),-M_PI/2);
226 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),-M_PI);
7fd59977 227 Matrix.Multiply(T);
228 }
229 else if (!strcmp("+Z-X",typ))
230 {
c6541a0c
D
231 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),-M_PI/2);
232 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI/2);
7fd59977 233 Matrix.Multiply(T);
234 }
235 else if (!strcmp("+Z+X",typ))
236 {
c6541a0c
D
237 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI/2);
238 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI/2);
7fd59977 239 Matrix.Multiply(T);
240 }
241 else if (!strcmp("-X+Z",typ))
242 {
c6541a0c
D
243 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI/2);
244 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI);
7fd59977 245 Matrix.Multiply(T);
246 }
247 else if (!strcmp("-Z-X",typ))
248 {
c6541a0c
D
249 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI/2);
250 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),-M_PI/2);
7fd59977 251 Matrix.Multiply(T);
252 }
253 else if (!strcmp("+X-Z",typ))
254 {
c6541a0c 255 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI/2);
7fd59977 256 }
257 else if (!strcmp("+Y+Z",typ))
258 {
c6541a0c
D
259 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),-M_PI/2);
260 T.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),-M_PI/2);
7fd59977 261 Matrix.Multiply(T);
262 }
263 else if (!strcmp("-Z+Y",typ))
264 {
c6541a0c 265 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),-M_PI/2);
7fd59977 266 }
267 else if (!strcmp("-Y-Z",typ))
268 {
c6541a0c
D
269 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),-M_PI/2);
270 T.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI/2);
7fd59977 271 Matrix.Multiply(T);
272 }
273 else if (!strcmp("+Z-Y",typ))
274 {
c6541a0c
D
275 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),-M_PI/2);
276 T.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI);
7fd59977 277 Matrix.Multiply(T);
278 }
279 else if (!strcmp("+Z+Y",typ))
280 {
c6541a0c 281 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI/2);
7fd59977 282 }
283 else if (!strcmp("-Y+Z",typ))
284 {
c6541a0c
D
285 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI/2);
286 T.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),-M_PI/2);
7fd59977 287 Matrix.Multiply(T);
288 }
289 else if (!strcmp("-Z-Y",typ))
290 {
c6541a0c
D
291 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI/2);
292 T.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI);
7fd59977 293 Matrix.Multiply(T);
294 }
295 else if (!strcmp("+Y-Z",typ))
296 {
c6541a0c
D
297 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(0,1,0)),M_PI/2);
298 T.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),M_PI/2);
7fd59977 299 Matrix.Multiply(T);
300 }
301 else if (!strcmp("AXON",typ))
302 {
c6541a0c
D
303 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),-M_PI/4);
304 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,0,1)),-M_PI/4);
7fd59977 305 Matrix.Multiply(T);
306 }
307 else if (!strcmp("PERS",typ))
308 {
309 FlagPers =1;
c6541a0c
D
310 Matrix.SetRotation(gp_Ax1(Pvise,gp_Dir(1,0,0)),-M_PI/4);
311 T.SetRotation(gp_Ax1(Pvise,gp_Dir(0,0,1)),-M_PI/4);
7fd59977 312 Matrix.Multiply(T);
313 }
314 else {
315 return Standard_False;
316 }
317 strcpy(type,typ);
318 return Standard_True;
319}
320
321//=======================================================================
322//function : WExpose
323//purpose :
324//=======================================================================
325
326void Draw_View::WExpose()
327{
328 Framex0=Framey0=Framex1=Framey1=0;
329 viewer->RepaintView(id);
330}
331
332//=======================================================================
333//function : Create
334//purpose :
335//=======================================================================
336
337Draw_Viewer::Draw_Viewer()
338{
339 if (Draw_Batch) return;
340 Standard_Integer i;
341 for ( i = 0; i < MAXVIEW; i++) myViews[i] = NULL;
342 for (i = 0; i < MAXCOLOR; i++) {
343 ps_width[i] = 1;
344 ps_gray[i] = 0;
345 }
346}
347
348//=======================================================================
349//function : DefineColor
350//purpose :
351//=======================================================================
352
353Standard_Boolean Draw_Viewer::DefineColor (const Standard_Integer i, const char* colname)
354{
355 if (Draw_Batch) return 1;
356 return Draw_Window::DefineColor(i,colname);
357}
358
359
360//=======================================================================
361//function : MakeView
362//purpose :
363//=======================================================================
364
365void Draw_Viewer::MakeView(const Standard_Integer id,
366 const char* typ,
367 const Standard_Integer X, const Standard_Integer Y,
368 const Standard_Integer W, const Standard_Integer H)
369{
370 if (Draw_Batch) return;
371 if (id < MAXVIEW) {
372
373 DeleteView(id);
374 myViews[id] = new Draw_View(id,this,X , Y, W, H);
375
376 // View fields
377 myViews[id]->dX = W / 2;
378 myViews[id]->dY = - H / 2;
379
380 if (!myViews[id]->Init(typ))
381 DeleteView(id);
382
383 RepaintView(id);
384 }
385}
386
387#ifdef WNT
388//=======================================================================
389//function : MakeView
390//purpose :
391//=======================================================================
392
393void Draw_Viewer::MakeView(const Standard_Integer id,
394 const char* typ,
395 const Standard_Integer X, const Standard_Integer Y,
396 const Standard_Integer W, const Standard_Integer H,
397 HWND win, Standard_Boolean useBuffer)
398{
399 if (Draw_Batch) return;
400 if (id < MAXVIEW) {
401
402 DeleteView(id);
403 myViews[id] = new Draw_View(id, this, X, Y, W, H, win);
404 myViews[id]->SetUseBuffer(useBuffer);
405
406 // View fields
407 myViews[id]->dX = W / 2;
408 myViews[id]->dY = - H / 2;
409
410 if (!myViews[id]->Init(typ))
411 DeleteView(id);
412 RepaintView(id);
413 }
414}
415#endif
416
417//=======================================================================
418//function : MakeView
419//purpose :
420//=======================================================================
421
422void Draw_Viewer::MakeView (const Standard_Integer id,
423 const char* typ,
424 const char* window)
425{
426 if (Draw_Batch) return;
427 if (id < MAXVIEW) {
428
429 DeleteView(id);
430 myViews[id] = new Draw_View(id,this,window);
431
432
433 myViews[id]->dX = myViews[id]->WidthWin() / 2;
434 myViews[id]->dY = - myViews[id]->HeightWin() / 2;
435
436 if (!myViews[id]->Init(typ))
437 DeleteView(id);
438
439 RepaintView(id);
440 }
441}
442
443
444//=======================================================================
445//function : SetTitle
446//purpose :
447//=======================================================================
448
449void Draw_Viewer::SetTitle (const Standard_Integer id, const char* name)
450{
451 if (Draw_Batch) return;
452 if(myViews[id]) myViews[id]->SetTitle((char*)name);
453}
454
455//=======================================================================
456//function : ResetView
457//purpose : reset view zoom and axes
458//=======================================================================
459
460void Draw_Viewer::ResetView(const Standard_Integer id)
461{
462 if (Draw_Batch) return;
463 if (myViews[id]) {
464 myViews[id]->Init(myViews[id]->type);
465 ConfigView(id);
466 }
467}
468
469//=======================================================================
470//function : SetZoom
471//purpose :
472//=======================================================================
473
474void Draw_Viewer::SetZoom (const Standard_Integer id, const Standard_Real z)
475{
476 if (Draw_Batch) return;
477 if (myViews[id]) {
478 Standard_Real zz = z / myViews[id]->Zoom;
479 myViews[id]->Zoom = z;
480 Standard_Integer X,Y,W,H;
481 GetPosSize(id,X,Y,W,H);
482 Standard_Real w = W;
483 myViews[id]->dX = (Standard_Integer )( w / 2 - zz * (w/2 - myViews[id]->dX));
484 Standard_Real h = H;
485 myViews[id]->dY = (Standard_Integer )( - h / 2 + zz * (h/2 + myViews[id]->dY));
486 }
487}
488
489//=======================================================================
490//function : RotateView
491//purpose :
492//=======================================================================
493
494void Draw_Viewer::RotateView (const Standard_Integer id,
495 const gp_Dir2d& D,
496 const Standard_Real A)
497{
498 if (Draw_Batch) return;
499 if (myViews[id]) {
500 gp_Trsf T = myViews[id]->Matrix;
7fd59977 501
502 T.Invert();
503 gp_Pnt PP(0,0,0);
504 gp_Dir DD(D.X(),D.Y(),0);
505 PP.Transform(T);
506 DD.Transform(T);
507 RotateView(id,PP,DD,A);
508 }
509}
510
511//=======================================================================
512//function : RotateView
513//purpose :
514//=======================================================================
515
516void Draw_Viewer::RotateView (const Standard_Integer id,
517 const gp_Pnt& P,
518 const gp_Dir& D,
519 const Standard_Real A)
520{
521 if (Draw_Batch) return;
522 if (myViews[id]) {
523 gp_Trsf T;
524 T.SetRotation(gp_Ax1(P,D),A);
525 myViews[id]->Matrix.Multiply(T);
526 }
527}
528
529
530//=======================================================================
531//function : SetFocal
532//purpose :
533//=======================================================================
534
535void Draw_Viewer::SetFocal (const Standard_Integer id, const Standard_Real F)
536{
537 if (Draw_Batch) return;
538 if (myViews[id])
539 myViews[id]->FocalDist = F;
540}
541
542//=======================================================================
543//function : GetType
544//purpose :
545//=======================================================================
546
547char* Draw_Viewer::GetType (const Standard_Integer id) const
548{
549 if (Draw_Batch) return blank;
550 if (myViews[id])
551 return myViews[id]->type;
552 else
553 return blank;
554}
555
556//=======================================================================
557//function : Zoom
558//purpose :
559//=======================================================================
560
561Standard_Real Draw_Viewer::Zoom (const Standard_Integer id) const
562{
563 if (Draw_Batch) return Standard_False;
564 if (myViews[id])
565 return myViews[id]->Zoom;
566 else
567 return 0;
568}
569
570//=======================================================================
571//function : Focal
572//purpose :
573//=======================================================================
574
575Standard_Real Draw_Viewer::Focal (const Standard_Integer id) const
576{
577 if (Draw_Batch) return 1.;
578 if (myViews[id])
579 return myViews[id]->FocalDist;
580 else
581 return 0;
582}
583
584//=======================================================================
585//function : GetTrsf
586//purpose :
587//=======================================================================
588
589void Draw_Viewer::GetTrsf (const Standard_Integer id,gp_Trsf& T) const
590{
591 if (Draw_Batch) return;
592 if (myViews[id])
593 T = myViews[id]->Matrix;
594}
595
596//=======================================================================
597//function : Is3D
598//purpose :
599//=======================================================================
600
601Standard_Boolean Draw_Viewer::Is3D (const Standard_Integer id) const
602{
603 if (Draw_Batch) return Standard_False;
604 if (myViews[id])
605 return !myViews[id]->Flag2d;
606 else
607 return Standard_False;
608}
609
610//=======================================================================
611//function : SetTrsf
612//purpose :
613//=======================================================================
614
615void Draw_Viewer::SetTrsf (const Standard_Integer id,gp_Trsf& T)
616{
617 if (Draw_Batch) return;
618 if (myViews[id])
619 myViews[id]->Matrix = T;
620}
621
622//=======================================================================
623//function : GetPosSize
624//purpose :
625//=======================================================================
626
627void Draw_Viewer::GetPosSize(const Standard_Integer id,
628 Standard_Integer& X, Standard_Integer& Y,
629 Standard_Integer& W, Standard_Integer& H)
630{
631 if (Draw_Batch) return;
632 if (myViews[id] != NULL) {
633 myViews[id]->GetPosition(X, Y);
634 W = myViews[id]->WidthWin();
635 H = myViews[id]->HeightWin();
636 }
637}
638
639//=======================================================================
640//function : GetFrame
641//purpose :
642//=======================================================================
643
644void Draw_Viewer::GetFrame(const Standard_Integer id,
645 Standard_Integer& xminf, Standard_Integer& yminf,
646 Standard_Integer& xmaxf, Standard_Integer& ymaxf)
647{
648 if (Draw_Batch) return;
649 if (myViews[id]) {
650 Standard_Integer X,Y,H,W;
651 GetPosSize(id,X,Y,W,H);
652 xminf = - myViews[id]->dX;
653 xmaxf = W - myViews[id]->dX;
654 yminf = - myViews[id]->dY - H;
655 ymaxf = - myViews[id]->dY;
656 }
657}
658
659//=======================================================================
660//function : FitView
661//purpose :
662//=======================================================================
663
664void Draw_Viewer::FitView(const Standard_Integer id, const Standard_Integer frame)
665{
666 if (Draw_Batch) return;
667 if (myViews[id]) {
668
669 // is this the only view in its category
670 Standard_Integer is2d = myViews[id]->Flag2d;
671 Standard_Integer i,nbviews = 0;
672 for (i = 1; i < MAXVIEW; i++) {
673 if (myViews[i]) {
674 if (myViews[i]->Flag2d == is2d) nbviews++;
675 }
676 }
677 Standard_Boolean only = (nbviews == 1);
678
679 Standard_Integer X,Y,H,W;
680 GetPosSize(id,X,Y,W,H);
681 // compute the min max
682 Standard_Integer n = myDrawables.Length();
683 if (n == 0) return;
684// Draw_Display DF;
685 curview = myViews[id];
686 Standard_Real umin,umax,vmin,vmax;
687 Standard_Real u1,u2,v1,v2;
688 umin = vmin = DRAWINFINITE;
689 umax = vmax = -DRAWINFINITE;
690
691 for (i = 1; i <= n; i++) {
692 Standard_Boolean d3d = myDrawables(i)->Is3D();
693 if ((d3d && !is2d) || (!d3d && is2d)) {
694 // if this is not the only view recompute...
695 if (!only)
696 DrawOnView(id,myDrawables(i));
697 myDrawables(i)->Bounds(u1,u2,v1,v2);
698 if (u1 < umin) umin = u1;
699 if (u2 > umax) umax = u2;
700 if (v1 < vmin) vmin = v1;
701 if (v2 > vmax) vmax = v2;
702 }
703 }
704 Standard_Real z;
705 umin = umin / curview->Zoom;
706 vmin = vmin / curview->Zoom;
707 umax = umax / curview->Zoom;
708 vmax = vmax / curview->Zoom;
709 if ((umax - umin) < 1.e-6) {
710 if ((vmax - vmin) < 1.e-6)
711 return;
712 else
713 z = ((Standard_Real)(H - 2*frame)) / (vmax - vmin);
714 }
715 else {
716 z = ((Standard_Real)(W - 2*frame)) /((Standard_Real) (umax - umin));
717 if ((vmax - vmin) > 1.e-6) {
718 Standard_Real z2 = ((Standard_Real)(H - 2*frame)) /(vmax - vmin);
719 if (z2 < z) z = z2;
720 }
721 }
722 curview->Zoom = z;
723 curview->dX = (Standard_Integer )( W / 2 - ((umin+umax) / 2) * z);
724 curview->dY = (Standard_Integer )( - H / 2 - ((vmin+vmax) / 2) * z);
725 }
726}
727
728//=======================================================================
729//function : PanView
730//purpose :
731//=======================================================================
732
733void Draw_Viewer::PanView(const Standard_Integer id,
734 const Standard_Integer DX, const Standard_Integer DY)
735{
736 if (Draw_Batch) return;
737 if (myViews[id]) {
738 myViews[id]->dX += DX;
739 myViews[id]->dY += DY;
740 }
741}
742
743
744//=======================================================================
745//function : SetPan
746//purpose :
747//=======================================================================
748
749void Draw_Viewer::SetPan(const Standard_Integer id,
750 const Standard_Integer DX, const Standard_Integer DY)
751{
752 if (Draw_Batch) return;
753 if (myViews[id]) {
754 myViews[id]->dX = DX;
755 myViews[id]->dY = DY;
756 }
757}
758
759//=======================================================================
760//function : GetPan
761//purpose :
762//=======================================================================
763
764void Draw_Viewer::GetPan(const Standard_Integer id,
765 Standard_Integer& DX, Standard_Integer& DY)
766{
767 if (Draw_Batch) return;
768 if (myViews[id]) {
769 DX = myViews[id]->dX;
770 DY = myViews[id]->dY;
771 }
772}
773
774//=======================================================================
775//function : HasView
776//purpose :
777//=======================================================================
778
779Standard_Boolean Draw_Viewer::HasView(const Standard_Integer id) const
780{
781 if (Draw_Batch) return Standard_False;
782 if ((id < 0) || id >= MAXVIEW) return Standard_False;
783 return myViews[id] != NULL;
784}
785
786//=======================================================================
787//function : DisplayView
788//purpose :
789//=======================================================================
790
791void Draw_Viewer::DisplayView (const Standard_Integer id) const
792{
793 if (Draw_Batch) return;
794 if (myViews[id]) myViews[id]->DisplayWindow();
795}
796
797//=======================================================================
798//function : HideView
799//purpose :
800//=======================================================================
801
802void Draw_Viewer::HideView (const Standard_Integer id) const
803{
804 if (Draw_Batch) return;
805 if (myViews[id]) {
806 //
807 }
808}
809
810//=======================================================================
811//function : ClearView
812//purpose :
813//=======================================================================
814
815void Draw_Viewer::ClearView(const Standard_Integer id) const
816{
817 if (Draw_Batch) return;
818 if (myViews[id]) myViews[id]->Clear();
819}
820
821//=======================================================================
822//function : RemoveView
823//purpose :
824//=======================================================================
825
826void Draw_Viewer::RemoveView(const Standard_Integer id)
827{
828 if (Draw_Batch) return;
829 if (myViews[id]) {
830 delete myViews[id];
831 myViews[id] = NULL;
832 }
833}
834
835//=======================================================================
836//function : RepaintView
837//purpose :
838//=======================================================================
839void Draw_Viewer::RepaintView (const Standard_Integer id) const
840{
841 if (Draw_Batch) return;
842 if (myViews[id]) {
843 ClearView(id);
844 Standard_Integer n = myDrawables.Length();
845 for (Standard_Integer i = 1; i <= n; i++)
846 DrawOnView(id,myDrawables(i));
847 }
848}
849
850
851#ifdef WNT
852//=======================================================================
853//function : ResizeView
854//purpose : WNT re-drawing optimization
855//=======================================================================
856void Draw_Viewer::ResizeView (const Standard_Integer id) const
857{
858 if (Draw_Batch) return;
859 if (myViews[id] && myViews[id]->GetUseBuffer()) {
860 myViews[id]->InitBuffer();
861 RepaintView(id);
862 }
863}
864
865//=======================================================================
866//function : UpdateView
867//purpose : WNT re-drawing optimization
868//=======================================================================
869void Draw_Viewer::UpdateView (const Standard_Integer id, const Standard_Boolean forced) const
870{
871 if (Draw_Batch) return;
872 if (myViews[id]) {
873 if (!myViews[id]->GetUseBuffer() || forced) {
874 RepaintView(id);
875 }
876 // Fast redrawing on WNT
877 if (myViews[id]->GetUseBuffer()) myViews[id]->Redraw();
878 }
879}
880#endif
881
882//=======================================================================
883//function : ConfigView
884//purpose :
885//=======================================================================
886
887void Draw_Viewer::ConfigView (const Standard_Integer id) const
888{
889 if (Draw_Batch) return;
890 if (myViews[id]) {
891 myViews[id]->dX = myViews[id]->WidthWin() / 2;
892 myViews[id]->dY = - myViews[id]->HeightWin() / 2;
893
894 }
895}
896
897//=======================================================================
898//function : PostScriptView
899//purpose :
900//=======================================================================
901
902void Draw_Viewer::PostScriptView (const Standard_Integer id,
903 const Standard_Integer VXmin,
904 const Standard_Integer VYmin,
905 const Standard_Integer VXmax,
906 const Standard_Integer VYmax,
907 const Standard_Integer PXmin,
908 const Standard_Integer PYmin,
909 const Standard_Integer PXmax,
910 const Standard_Integer PYmax,
911 ostream& sortie) const
912{
913 if (Draw_Batch) return;
914 if (myViews[id]) {
915 ps_vx = VXmin;
916 ps_vy = VYmin;
917 ps_px = PXmin;
918 ps_py = PYmin;
919 ps_kx = ((Standard_Real) (PXmax - PXmin)) / ((Standard_Real) (VXmax - VXmin));
920 ps_ky = ((Standard_Real) (PYmax - PYmin)) / ((Standard_Real) (VYmax - VYmin));
921 ps_stream = &sortie;
922 Standard_Integer n = myDrawables.Length();
923 if (n == 0) return;
924 CurrentMode = POSTSCRIPT;
925 Draw_Display DF = MakeDisplay(id);
926 Standard_Boolean view2d = myViews[id]->Flag2d;
927 for (Standard_Integer i = 1; i <= n; i++)
928 if (myDrawables(i)->Is3D()) {
929 if (!view2d) myDrawables(i)->DrawOn(DF);
930 }
931 else {
932 if (view2d) myDrawables(i)->DrawOn(DF);
933 }
934 sortie << "stroke\n";
935 CurrentMode = DRAW;
936 }
937}
938
939//=======================================================================
940//function : PostColor
941//purpose :
942//=======================================================================
943
944void Draw_Viewer::PostColor(const Standard_Integer icol,
945 const Standard_Integer width,
946 const Standard_Real gray)
947{
948 if (Draw_Batch) return;
949 if ((icol < 0) || (icol >= MAXCOLOR)) return;
950 ps_width[icol] = width;
951 ps_gray[icol] = gray;
952}
953
954//=======================================================================
955//function : SaveView
956//purpose :
957//=======================================================================
958
959Standard_Boolean Draw_Viewer::SaveView(const Standard_Integer id,
960 const char* filename)
961{
962 if (Draw_Batch) return Standard_False;;
963 Flush();
964 if (myViews[id]) {
965 return myViews[id]->Save(filename);
966 }
967 else
968 {
969 std::cerr << "View " << id << " doesn't exists!\n";
970 return Standard_False;
971 }
972}
973
974//=======================================================================
975//function : RepaintAll
976//purpose :
977//=======================================================================
978
979void Draw_Viewer::RepaintAll () const
980{
981 if (Draw_Batch) return;
982 for (Standard_Integer id = 0; id < MAXVIEW; id++)
983 RepaintView(id);
984}
985
986//=======================================================================
987//function : Repaint2D
988//purpose :
989//=======================================================================
990
991void Draw_Viewer::Repaint2D () const
992{
993 if (Draw_Batch) return;
994 for (Standard_Integer id = 0; id < MAXVIEW; id++)
995 if (myViews[id]) {
996 if (myViews[id]->Flag2d) RepaintView(id);
997 }
998}
999
1000//=======================================================================
1001//function : Repaint3D
1002//purpose :
1003//=======================================================================
1004
1005void Draw_Viewer::Repaint3D () const
1006{
1007 if (Draw_Batch) return;
1008 for (Standard_Integer id = 0; id < MAXVIEW; id++)
1009 if (myViews[id]) {
1010 if (!myViews[id]->Flag2d) RepaintView(id);
1011 }
1012}
1013
1014//=======================================================================
1015//function : GetWindow
1016//purpose :
1017//=======================================================================
1018unsigned long Draw_Viewer::GetWindow (const Standard_Integer id) const
1019{
1020 if (Draw_Batch) return 0;
1021 if (myViews[id]) {
1022 #ifdef WNT
1023 return (unsigned long)(myViews[id]->win);
1024 #else
1025 return myViews[id]->win;
1026 #endif
1027 }
1028 return 0;
1029}
1030
1031//=======================================================================
1032//function : DeleteView
1033//purpose :
1034//=======================================================================
1035
1036void Draw_Viewer::DeleteView(const Standard_Integer id)
1037{
1038 if (Draw_Batch) return;
1039 if (myViews[id] != NULL) {
1040 delete myViews[id];
1041 myViews[id] = NULL;
1042 }
1043}
1044
1045//=======================================================================
1046//function : Clear
1047//purpose :
1048//=======================================================================
1049
1050void Draw_Viewer::Clear()
1051{
1052 if (Draw_Batch) return;
1053 for (Standard_Integer i = 1; i <= myDrawables.Length(); i++)
1054 myDrawables(i)->Visible(Standard_False);
1055 myDrawables.Clear();
1056 for (Standard_Integer id = 0; id < MAXVIEW; id++)
1057 ClearView(id);
1058}
1059
1060//=======================================================================
1061//function : Clear2D
1062//purpose :
1063//=======================================================================
1064
1065void Draw_Viewer::Clear2D()
1066{
1067 if (Draw_Batch) return;
1068 Standard_Integer i = 1;
1069 while (i <= myDrawables.Length()) {
1070 if (myDrawables(i)->Is3D())
1071 i++;
1072 else {
1073 myDrawables(i)->Visible(Standard_False);
1074 myDrawables.Remove(i);
1075 }
1076 }
1077 for (Standard_Integer id = 0; id < MAXVIEW; id++) {
1078 if (myViews[id]) {
1079 if (myViews[id]->Flag2d)
1080 ClearView(id);
1081 }
1082 }
1083}
1084
1085//=======================================================================
1086//function : Clear3D
1087//purpose :
1088//=======================================================================
1089
1090void Draw_Viewer::Clear3D()
1091{
1092 if (Draw_Batch) return;
1093 Standard_Integer i = 1;
1094 while (i <= myDrawables.Length()) {
1095 if (myDrawables(i)->Is3D()) {
1096 myDrawables(i)->Visible(Standard_False);
1097 myDrawables.Remove(i);
1098 }
1099 else
1100 i++;
1101 }
1102 for (Standard_Integer id = 0; id < MAXVIEW; id++) {
1103 if (myViews[id]) {
1104 if (!myViews[id]->Flag2d)
1105 ClearView(id);
1106 }
1107 }
1108}
1109
1110//=======================================================================
1111//function : Flush
1112//purpose :
1113//=======================================================================
1114
1115void Draw_Viewer::Flush()
1116{
1117 if (Draw_Batch) return;
1118 Draw_Window::Flush();
1119}
1120
1121//=======================================================================
1122//function : DrawOnView
1123//purpose :
1124//=======================================================================
1125
1126void Draw_Viewer::DrawOnView(const Standard_Integer id,
1127 const Handle(Draw_Drawable3D)& D) const
1128{
1129 if (Draw_Batch) return;
1130 if (myViews[id]) {
1131 Draw_Display d = MakeDisplay(id);
1132 xmin = ymin = DRAWINFINITE;
1133 xmax = ymax = -DRAWINFINITE;
1134
1135 Standard_Boolean view2d = myViews[id]->Flag2d;
1136 myViews[id]->ResetFrame();
1137 if ((D->Is3D() && !view2d) ||
1138 (!D->Is3D() && view2d)) {
1139 D->DrawOn(d);
1140
1141 if (CurrentMode == DRAW)
1142 D->SetBounds(xmin,xmax,ymin,ymax);
1143 d.Flush();
1144 }
1145 }
1146}
1147
1148//=======================================================================
1149//function : HighlightOnView
1150//purpose :
1151//=======================================================================
1152
1153void Draw_Viewer::HighlightOnView (const Standard_Integer id,
1154 const Handle(Draw_Drawable3D)& D,
1155 const Draw_ColorKind C) const
1156{
1157 if (Draw_Batch) return;
1158 highlight = Standard_True;
1159 highlightcol = C;
1160 DrawOnView(id,D);
1161 highlight = Standard_False;
1162}
1163
1164//=======================================================================
1165//function : AddDrawable
1166//purpose :
1167//=======================================================================
1168
1169void Draw_Viewer::AddDrawable (const Handle(Draw_Drawable3D)& D)
1170{
1171 if (Draw_Batch) return;
1172 if (!D.IsNull() && !D->Visible()) {
1173 myDrawables.Append(D);
1174 D->Visible(Standard_True);
1175 }
1176}
1177
1178//=======================================================================
1179//function : RemoveDrawable
1180//purpose :
1181//=======================================================================
1182
1183void Draw_Viewer::RemoveDrawable (const Handle(Draw_Drawable3D)& D)
1184{
1185 if (Draw_Batch) return;
1186 if (!D.IsNull() && D->Visible()) {
1187 Standard_Integer index;
1188 for (index = 1; index <= myDrawables.Length(); index++) {
1189 if (myDrawables(index) == D) {
1190 D->Visible(Standard_False);
1191 myDrawables.Remove(index);
1192 return;
1193 }
1194 }
1195 }
1196}
1197
1198//=======================================================================
1199//function : MakeDisplay
1200//purpose : return a display on the view
1201//=======================================================================
1202
1203Draw_Display Draw_Viewer::MakeDisplay (const Standard_Integer id) const
1204{
1205 if (Draw_Batch) {Draw_Display dis;return dis;}
1206 curviewId = id;
1207 curview = myViews[id];
1208 int GXcopy = 0x3;
1209 nbseg = 0;
1210 Draw_Color initcol(Draw_blanc);
1211 // to force setting the color
1212 currentcolor = Draw_Color(Draw_rouge);
1213 Draw_Display dis;
1214 dis.SetColor(initcol);
1215 dis.SetMode(GXcopy);
1216 return dis;
1217}
1218
1219//=======================================================================
1220//function : Select
1221//purpose :
1222//=======================================================================
1223void Draw_Viewer::Select (Standard_Integer& id, Standard_Integer& X, Standard_Integer& Y,
1224 Standard_Integer& Button, Standard_Boolean wait)
1225{
1226 if (Draw_Batch) return;
1227 Flush();
1228#ifndef WNT
1229 if (!wait) {
1230 if (id >=0 && id < MAXVIEW) {
1231 if (myViews[id]) myViews[id]->Wait(wait);
1232 }
1233 }
1234 else {
1235 for(int i=0 ; i<MAXVIEW ; i++)
1236 if (myViews[i]) myViews[i]->Wait(wait);
1237 }
1238
1239 Standard_Boolean again = Standard_True;
1240 while (again) {
1241
1242 Event ev;
1243 ev.type = 0;
1244
1245 GetNextEvent(ev);
1246
1247 switch (ev.type) {
1248
1249 case ButtonPress :
1250 Standard_Integer iv;
1251 for (iv = 0; iv < MAXVIEW; iv++) {
1252 if (myViews[iv]) {
1253 if (myViews[iv]->win == ev.window)
1254 break;
1255 }
1256 }
1257 if (wait || id == iv) {
1258 if (iv < MAXVIEW) {
1259 id = iv;
1260 X = ev.x;
1261 Y = ev.y;
1262 Button = ev.button;
1263 }
1264 else {
1265 id = -1;
1266 }
1267 again = Standard_False;
1268 }
1269 break;
1270
1271 case MotionNotify :
1272 if (wait) break;
1273 X = ev.x;
1274 Y = ev.y;
1275 Button = 0;
1276 again = Standard_False;
1277 break;
1278 }
1279 }
1280
1281 if (id != -1) {
1282 X = X - myViews[id]->dX;
1283 Y = -Y - myViews[id]->dY;
1284 }
1285 if (!wait) myViews[id]->Wait(!wait);
1286#else
1287 HANDLE hWnd;
1288
1289 id = MAXVIEW; //:abv 29.05.02: cycle for working in console mode
1290 while ( id >= MAXVIEW ) {
1291 if (wait)
1292 Draw_Window::SelectWait(hWnd, X, Y, Button);
1293 else
1294 Draw_Window::SelectNoWait(hWnd, X, Y, Button);
1295
1296 // Recherche du numero de la vue grace au HANDLE
1297 for(int i=0 ; i<MAXVIEW ; i++)
1298 if (myViews[i] && myViews[i]->win == hWnd ) id = i;
1299 }
1300 X = X - myViews[id]->dX;
1301 Y = -Y - myViews[id]->dY;
1302#endif
1303}
1304
1305//=======================================================================
1306//function : Pick
1307//purpose :
1308//=======================================================================
1309
1310Standard_Integer Draw_Viewer::Pick(const Standard_Integer id,
1311 const Standard_Integer X, const Standard_Integer Y, const Standard_Integer Prec,
1312 Handle(Draw_Drawable3D)& D,
1313 const Standard_Integer first) const
1314{
1315 if (Draw_Batch) return 0;
1316 if (myViews[id] == NULL)
1317 return 0;
1318
1319 // is this the only view in its category
1320 Standard_Integer is2d = myViews[id]->Flag2d;
1321 Standard_Integer i,nbviews = 0;
1322 for (i = 0; i < MAXVIEW; i++) {
1323 if (myViews[i]) {
1324 if (myViews[i]->Flag2d == is2d) nbviews++;
1325 }
1326 }
1327 Standard_Boolean only = (nbviews == 1);
1328
1329 CurrentMode = PICK;
1330 xpick = X;
1331 ypick = Y;
1332 precpick = Prec;
1333 found = Standard_False;
1334 Standard_Real x1,x2,y1,y2;
1335 for (i = first+1; i <= myDrawables.Length(); i++) {
1336 Standard_Boolean reject = Standard_False;
1337 // rejection if only view
1338 if (only) {
1339 myDrawables(i)->Bounds(x1,x2,y1,y2);
1340 if ((xpick+Prec < x1) || (xpick-Prec > x2) ||
1341 (ypick+Prec < y1) || (ypick-Prec > y2))
1342 reject = Standard_True;
1343 }
1344 if (!reject) {
1345 DrawOnView(id,myDrawables(i));
1346 if (found)
1347 break;
1348 }
1349 }
1350 CurrentMode = DRAW;
1351 found = Standard_False;
1352 if (i <= myDrawables.Length())
1353 D = myDrawables(i);
1354 else
1355 i = 0;
1356 return i;
1357}
1358
1359//=======================================================================
1360//function : LastPick
1361//purpose :
1362//=======================================================================
1363
1364void Draw_Viewer::LastPick(gp_Pnt& P1, gp_Pnt& P2, Standard_Real& Param)
1365{
1366 if (Draw_Batch) return;
1367 P1 = lastPickP1;
1368 P2 = lastPickP2;
1369 Param = lastPickParam;
1370}
1371
1372//=======================================================================
1373//function : ~Draw_Viewer
1374//purpose :
1375//=======================================================================
1376
1377Draw_Viewer::~Draw_Viewer()
1378{
1379 if (Draw_Batch) return;
1380 for (Standard_Integer id = 0; id < MAXVIEW; id++)
1381 DeleteView(id);
1382}
1383
1384//=======================================================================
1385//function : operator<<
1386//purpose :
1387//=======================================================================
1388
1389Draw_Viewer& Draw_Viewer::operator<<(const Handle(Draw_Drawable3D)& d3d)
1390{
1391 if (Draw_Batch) return *this;
1392 if (!d3d.IsNull()) {
1393 AddDrawable(d3d);
1394 for (Standard_Integer id = 0; id < MAXVIEW; id++)
1395 DrawOnView(id,d3d);
1396 }
1397 return *this;
1398}
1399
1400//=======================================================================
1401//function : GetDrawables
1402//purpose :
1403//=======================================================================
1404const Draw_SequenceOfDrawable3D& Draw_Viewer::GetDrawables()
1405{
1406 return myDrawables;
1407}
1408
1409// *******************************************************************
1410// DISPLAY methods
1411// *******************************************************************
1412
1413
1414void Draw_Flush()
1415{
1416 if (Draw_Batch) return;
1417 if (highlight) curview->SetColor(highlightcol.ID());
1418 curview->DrawSegments(segm,nbseg);
1419 nbseg = 0;
1420}
1421
1422//=======================================================================
1423//function : SetColor
1424//purpose :
1425//=======================================================================
1426
1427void Draw_Display::SetColor (const Draw_Color& col) const
1428{
1429 if (Draw_Batch) return;
1430 if (col.ID() == currentcolor.ID()) return;
1431
1432 currentcolor = col;
1433 switch (CurrentMode) {
1434
1435 case DRAW :
1436 Draw_Flush();
1437 curview->SetColor(col.ID());
1438 break;
1439
1440 case POSTSCRIPT :
1441 (*ps_stream) << "stroke\nnewpath\n";
1442 (*ps_stream) << ps_width[col.ID()]<<" setlinewidth\n";
1443 (*ps_stream) << ps_gray[col.ID()]<<" setgray\n";
1444
1445 case PICK :
1446 break;
1447 }
1448}
1449
1450//=======================================================================
1451//function : SetMode
1452//purpose :
1453//=======================================================================
1454
1455void Draw_Display::SetMode (const Standard_Integer M) const
1456{
1457 if (Draw_Batch) return;
1458 switch (CurrentMode) {
1459
1460 case DRAW :
1461 Draw_Flush();
1462 curview->SetMode(M);
1463 break;
1464
1465 case PICK :
1466 case POSTSCRIPT :
1467 break;
1468 }
1469}
1470
1471//=======================================================================
1472//function : Zoom
1473//purpose :
1474//=======================================================================
1475
1476Standard_Real Draw_Display::Zoom() const
1477{
1478 if (Draw_Batch) return 1.;
1479 return curview->Zoom;
1480}
1481
1482//=======================================================================
1483//function : Flush
1484//purpose :
1485//=======================================================================
1486
1487void Draw_Display::Flush () const
1488{
1489 if (Draw_Batch) return;
1490 Draw_Flush();
1491}
1492
1493//=======================================================================
1494//function : DrawString
1495//purpose :
1496//=======================================================================
1497
1498void Draw_Display::DrawString(const gp_Pnt2d& ppt,
1499 const Standard_CString S,
1500 const Standard_Real moveX,
1501 const Standard_Real moveY)
1502{
1503 if (Draw_Batch) return;
1504 if (ppt.X() > 1.e09 || ppt.X() < -1.e09 ) return;
1505 if (ppt.Y() > 1.e09 || ppt.Y() < -1.e09 ) return;
1506
1507 gp_Pnt2d pt(ppt.X()*curview->Zoom,ppt.Y()*curview->Zoom);
1508
1509 if (pt.X() > 1.e09 || pt.X() < -1.e09 ) return;
1510 if (pt.Y() > 1.e09 || pt.Y() < -1.e09 ) return;
1511
1512 switch (CurrentMode) {
1513
1514 case DRAW :
1515 {
1516 int X = (int) ( pt.X() + moveX + curview->dX);
1517 int Y = (int) (-pt.Y() + moveY - curview->dY);
1518 curview->DrawString(X,Y,(char *)S);
1519 if (Draw_Bounds) {
1520 if (pt.X() + moveX > xmax) xmax = pt.X();
1521 if (pt.X() + moveX < xmin) xmin = pt.X();
1522 if (-pt.Y() - moveY > ymax) ymax = -pt.Y();
1523 if (-pt.Y() - moveY < ymin) ymin = -pt.Y();
1524 }
1525 }
1526 break;
1527
1528 case POSTSCRIPT :
1529 {
1530 Standard_Integer x = (Standard_Integer )( (pt.X() + moveX - ps_vx) * ps_kx + ps_px);
1531 Standard_Integer y = (Standard_Integer )( (pt.Y() + moveY - ps_vy) * ps_ky + ps_py);
1532 (*ps_stream) <<"stroke\n";
1533 (*ps_stream) << x << " " << y << " m\n";
1534 (*ps_stream) <<"("<<S<<") show\nnewpath\n";
1535 }
1536 break;
1537
1538 case PICK :
1539 break;
1540 }
1541}
1542
1543//=======================================================================
1544//function : DrawString
1545//purpose :
1546//=======================================================================
1547
1548void Draw_Display::DrawString(const gp_Pnt2d& ppt,
1549 const Standard_CString S)
1550{
1551 if (Draw_Batch) return;
1552 DrawString(ppt,S,0.0,0.0);
1553}
1554
1555//=======================================================================
1556//function : DrawString
1557//purpose :
1558//=======================================================================
1559
1560void Draw_Display::DrawString(const gp_Pnt& pt,
1561 const Standard_CString S,
1562 const Standard_Real moveX,
1563 const Standard_Real moveY)
1564{
1565 if (Draw_Batch) return;
1566 DrawString(Project(pt),S,moveX,moveY);
1567}
1568
1569//=======================================================================
1570//function : DrawString
1571//purpose :
1572//=======================================================================
1573
1574void Draw_Display::DrawString(const gp_Pnt& pt,
1575 const Standard_CString S)
1576{
1577 if (Draw_Batch) return;
1578 DrawString(Project(pt),S,0.0,0.0);
1579}
1580
1581
1582// *******************************************************************
1583// Drawing data static variables
1584// *******************************************************************
1585static gp_Pnt2d PtCur; // current 2D point
1586static gp_Pnt PtPers; // current 3D point for Pers
1587
1588//=======================================================================
1589//function : Project
1590//purpose :
1591//=======================================================================
1592
1593void Draw_Display::Project(const gp_Pnt& p, gp_Pnt2d& p2d) const
1594{
1595 if (Draw_Batch) return;
1596 gp_Pnt pt = p;
1597 pt.Transform(curview->Matrix);
1598 Standard_Real xp,yp,zp;
1599 pt.Coord(xp,yp,zp);
1600 if (curview->FlagPers) {
1601 xp = xp*curview->FocalDist/(curview->FocalDist-zp);
1602 yp = yp*curview->FocalDist/(curview->FocalDist-zp);
1603 }
1604 p2d.SetCoord(xp,yp);
1605}
1606
1607//=======================================================================
1608//function : Draw_Display
1609//purpose :
1610//=======================================================================
1611
1612Draw_Display::Draw_Display ()
1613{
1614 if (Draw_Batch) return;
1615 if (curview) {
1616 PtPers.SetCoord(0,0,0);
1617 PtPers.Transform(curview->Matrix);
1618 PtCur.SetCoord(PtPers.X()*curview->Zoom,PtPers.Y()*curview->Zoom);
1619 }
1620}
1621
1622//=======================================================================
1623//function : MoveTo 2D
1624//purpose :
1625//=======================================================================
1626
1627void Draw_Display::MoveTo (const gp_Pnt2d& pp)
1628{
1629 if (Draw_Batch) return;
1630 gp_Pnt2d pt(pp.X() * curview->Zoom, pp.Y() * curview->Zoom);
1631 switch (CurrentMode) {
1632
1633 case DRAW :
1634 PtCur = pt;
1635 if (Draw_Bounds) {
1636 if (pt.X() > xmax) xmax = pt.X();
1637 if (pt.X() < xmin) xmin = pt.X();
1638 if (pt.Y() > ymax) ymax = pt.Y();
1639 if (pt.Y() < ymin) ymin = pt.Y();
1640 }
1641 break;
1642
1643 case PICK :
1644 PtCur = pt;
1645 break;
1646
1647 case POSTSCRIPT :
1648 {
1649 Standard_Integer x = (Standard_Integer )( (pt.X() - ps_vx) * ps_kx + ps_px);
1650 Standard_Integer y = (Standard_Integer )( (pt.Y() - ps_vy) * ps_ky + ps_py);
1651 (*ps_stream) <<"stroke\nnewpath\n"<< x << " " << y << " m\n";
1652 }
1653 break;
1654 }
1655}
1656
1657//=======================================================================
1658//function : DrawTo 2D
1659//purpose :
1660//=======================================================================
1661inline Standard_Integer CalculRegion(const Standard_Real x,
1662 const Standard_Real y,
1663 const Standard_Real x1,
1664 const Standard_Real y1,
1665 const Standard_Real x2,
1666 const Standard_Real y2) {
1667 Standard_Integer r;
1668 if(x<x1) { r=1; } else { if(x>x2) { r=2; } else { r=0; } }
1669 if(y<y1) { r|=4; } else { if(y>y2) { r|=8; } }
1670 return(r);
1671}
1672
1673Standard_Boolean Trim(gp_Pnt2d& P1,gp_Pnt2d& P2,
1674 Standard_Real x0,
1675 Standard_Real y0,
1676 Standard_Real x1,
1677 Standard_Real y1) {
1678 Standard_Real xa=P1.X(),ya=P1.Y(),xb=P2.X(),yb=P2.Y();
1679
1680 Standard_Integer regiona=0,regionb=0;
1681 regiona = CalculRegion(xa,ya,x0,y0,x1,y1);
1682 regionb = CalculRegion(xb,yb,x0,y0,x1,y1);
1683 if((regiona & regionb)==0) {
1684 Standard_Real dx=xb-xa;
1685 Standard_Real dy=yb-ya;
1686 Standard_Real dab=sqrt(dx*dx+dy*dy);
1687 if(dab<1e-10) return(Standard_False);
1688 dx/=dab;
1689 dy/=dab;
1690
1691 Standard_Real xm,ym,mfenx,mfeny;
1692 mfenx=xm=0.5*(x0+x1);
1693 mfeny=ym=0.5*(y0+y1);
1694 x1-=x0; y1-=y0;
1695 Standard_Real d=sqrt(x1*x1+y1*y1) * 2;
1696
1697 Standard_Real p=(xm-xa)*dx+(ym-ya)*dy;
1698 xm=xa+p*dx;
1699 ym=ya+p*dy;
1700 gp_Pnt2d Pm(xm,ym);
1701
1702 gp_Pnt2d MFen(mfenx,mfeny);
1703 if(MFen.SquareDistance(Pm) > d*d) return(Standard_False);
1704
1705 Standard_Real PmDistP1 = Pm.Distance(P1);
1706 Standard_Real PmDistP2 = Pm.Distance(P2);
1707
1708 Standard_Real amab = (xm-xa)*(xb-xa)+(ym-ya)*(yb-ya);
1709
1710 if(amab > 0) { //-- M est compris entre A et B
1711 if(PmDistP1 > d) {
1712 P1.SetCoord(xm-d*dx,ym-d*dy);
1713 }
1714 if(PmDistP2 >d) {
1715 P2.SetCoord(xm+d*dx,ym+d*dy);
1716 }
1717 }
1718 else if(PmDistP1 < PmDistP2) { //-- On a M P1 P2
1719 if(PmDistP2 > d) {
1720 P2.SetCoord(xm+d*dx,ym+d*dy);
1721 }
1722 }
1723 else { //-- On a P1 P2 M
1724 if(PmDistP1 > d) {
1725 P1.SetCoord(xm-d*dx,ym-d*dy);
1726 }
1727 }
1728 return(Standard_True);
1729 }
1730 else return(Standard_False);
1731}
1732
1733
1734
1735
1736void Draw_Display::DrawTo (const gp_Pnt2d& pp2)
1737{
1738 if (Draw_Batch) return;
1739 if (pp2.X() > 1.e09 || pp2.X() < -1.e09 ) return;
1740 if (pp2.Y() > 1.e09 || pp2.Y() < -1.e09 ) return;
1741
1742 gp_Pnt2d p2(pp2.X() * curview->Zoom, pp2.Y() * curview->Zoom);
1743
1744 if (p2.X() > 1.e09 || p2.X() < -1.e09 ) return;
1745 if (p2.Y() > 1.e09 || p2.Y() < -1.e09 ) return;
1746
1747 gp_Pnt2d p1 = PtCur;
1748 if (p1.X() > 1.e09 || p1.X() < -1.e09 ) return;
1749 if (p1.Y() > 1.e09 || p1.Y() < -1.e09 ) return;
1750
1751 PtCur = p2;
1752
1753 switch (CurrentMode) {
1754
1755 case DRAW : {
1756
1757#if 1
1758 Standard_Integer x0,y0,x1,y1;
1759 curview->GetFrame(x0,y0,x1,y1);
1760
1761
1762 //Standard_Integer qx0,qy0,qx1,qy1;
1763 //curview->viewer->GetFrame(curview->id,qx0,qy0,qx1,qy1);
1764 //if(qx0!=x0 || qx1!=x1 || qy0!=y0 || qy1!=y1) {
1765 // x0=qx0; x1=qx1; y0=qy0; y1=qy1;
1766 //}
1767
1768
1769
1770 gp_Pnt2d PI1(p1);
1771 gp_Pnt2d PI2(p2);
1772
1773 if(Trim(PI1,PI2,x0,y0,x1,y1)) {
1774 segm[nbseg].Init((Standard_Integer )( PI1.X() + curview->dX),
1775 (Standard_Integer )(-PI1.Y() - curview->dY),
1776 (Standard_Integer )( PI2.X() + curview->dX),
1777 (Standard_Integer )(-PI2.Y() - curview->dY));
1778
1779 nbseg++;
1780 }
1781#else
1782 segm[nbseg].Init(( p1.X() + curview->dX),
1783 (-p1.Y() - curview->dY),
1784 ( p2.X() + curview->dX),
1785 (-p2.Y() - curview->dY));
1786
1787 nbseg++;
1788#endif
1789 if (nbseg == MAXSEGMENT) {
1790 Draw_Flush();
1791 }
1792 if (Draw_Bounds) {
1793 if (p2.X() > xmax) xmax = p2.X();
1794 if (p2.X() < xmin) xmin = p2.X();
1795 if (p2.Y() > ymax) ymax = p2.Y();
1796 if (p2.Y() < ymin) ymin = p2.Y();
1797 }
1798 }
1799 break;
1800
1801 case PICK :
1802 if (!found) {
1803 Standard_Integer x1 = (int) p1.X() ;
1804 Standard_Integer y1 = (int) p1.Y() ;
1805 Standard_Integer x2 = (int) p2.X() ;
1806 Standard_Integer y2 = (int) p2.Y() ;
1807 if ((x1 >= xpick + precpick) && (x2 >= xpick + precpick)) break;
1808 if ((x1 <= xpick - precpick) && (x2 <= xpick - precpick)) break;
1809 if ((y1 >= ypick + precpick) && (y2 >= ypick + precpick)) break;
1810 if ((y1 <= ypick - precpick) && (y2 <= ypick - precpick)) break;
1811
1812 Standard_Boolean inside = Standard_True;
1813 if ((x1 > xpick + precpick) || (x2 > xpick + precpick)) {
1814 Standard_Real y = (Standard_Real) y1 +
1815 (Standard_Real) (y2-y1) * (Standard_Real) (xpick+precpick-x1) /
1816 (Standard_Real) (x2 - x1);
1817 if ( (y < ypick+precpick) && (y > ypick - precpick)) {
1818 found = Standard_True;
1819 lastPickParam = (Standard_Real) (xpick - x1) /
1820 (Standard_Real) (x2 - x1);
1821 break;
1822 }
1823 else
1824 inside = Standard_False;
1825 }
1826
1827 if ((x1 < xpick - precpick) || (x2 < xpick - precpick)) {
1828 Standard_Real y = (Standard_Real) y1 +
1829 (Standard_Real) (y2-y1) * (Standard_Real) (xpick-precpick-x1) /
1830 (Standard_Real) (x2 - x1);
1831 if ( (y < ypick+precpick) && (y > ypick - precpick)) {
1832 found = Standard_True;
1833 lastPickParam = (Standard_Real) (xpick - x1) /
1834 (Standard_Real) (x2 - x1);
1835 break;
1836 }
1837 else
1838 inside = Standard_False;
1839 }
1840
1841
1842 if ((y1 > ypick + precpick) || (y2 > ypick + precpick)) {
1843 Standard_Real x = (Standard_Real) x1 +
1844 (Standard_Real) (x2-x1) * (Standard_Real) (ypick+precpick-y1) /
1845 (Standard_Real) (y2 - y1);
1846 if ( (x < xpick+precpick) && (x > xpick - precpick)) {
1847 found = Standard_True;
1848 lastPickParam = (Standard_Real) (ypick - y1) /
1849 (Standard_Real) (y2 - y1);
1850 break;
1851 }
1852 else
1853 inside = Standard_False;
1854 }
1855
1856
1857 if ((y1 < ypick - precpick) || (y2 < ypick - precpick)) {
1858 Standard_Real x = (Standard_Real) x1 +
1859 (Standard_Real) (x2-x1) * (Standard_Real) (ypick-precpick-y1) /
1860 (Standard_Real) (y2 - y1);
1861 if ( (x < xpick+precpick) && (x > xpick - precpick)) {
1862 found = Standard_True;
1863 lastPickParam = (Standard_Real) (ypick - y1) /
1864 (Standard_Real) (y2 - y1);
1865 break;
1866 }
1867 else
1868 inside = Standard_False;
1869 }
1870 found = found || inside;
1871 if (found) {
1872 if (Abs(x2-x1) > Abs(y2-y1)) {
1873 if (Abs(x2-x1) < 1e-5) lastPickParam = 0;
1874 else
1875 lastPickParam = (Standard_Real)(xpick - x1) /
1876 (Standard_Real)(x2 - x1);
1877 }
1878 else {
1879 if (Abs(y2-y1) < 1e-5) lastPickParam = 0;
1880 else
1881 lastPickParam = (Standard_Real)(ypick - y1) /
1882 (Standard_Real)(y2 - y1);
1883 }
1884 }
1885 }
1886 break;
1887
1888 case POSTSCRIPT :
1889 {
1890 Standard_Integer x = (Standard_Integer )( (p2.X() - ps_vx) * ps_kx + ps_px);
1891 Standard_Integer y = (Standard_Integer )( (p2.Y() - ps_vy) * ps_ky + ps_py);
1892 (*ps_stream) << x << " " << y << " l\n";
1893 }
1894 break;
1895 }
1896
1897}
1898
1899//=======================================================================
1900//function : MoveTo
1901//purpose :
1902//=======================================================================
1903
1904void Draw_Display::MoveTo (const gp_Pnt& pt)
1905{
1906 if (Draw_Batch) return;
1907 if (CurrentMode == PICK) {
1908 if (!found) lastPickP1 = pt;
1909 else return;
1910 }
1911 PtPers = pt;
1912 PtPers.Transform(curview->Matrix);
1913 Standard_Real xp = PtPers.X();
1914 Standard_Real yp = PtPers.Y();
1915 if (curview->FlagPers) {
1916 Standard_Real ZPers = PtPers.Z();
1917 if (ZPers < curview->FocalDist*precpers) {
1918 xp=xp*curview->FocalDist/(curview->FocalDist-ZPers);
1919 yp=yp*curview->FocalDist/(curview->FocalDist-ZPers);
1920 }
1921 }
1922 MoveTo(gp_Pnt2d(xp,yp));
1923}
1924
1925//=======================================================================
1926//function : DrawTo
1927//purpose :
1928//=======================================================================
1929
1930void Draw_Display::DrawTo (const gp_Pnt& pt)
1931{
1932 if (Draw_Batch) return;
1933 if ((CurrentMode == PICK) && found) return;
1934
1935 gp_Pnt pt2 = pt.Transformed(curview->Matrix);
1936 Standard_Real xp2 = pt2.X();
1937 Standard_Real yp2 = pt2.Y();
1938
1939 if (curview->FlagPers) {
1940 Standard_Real xp1 = PtPers.X();
1941 Standard_Real yp1 = PtPers.Y();
1942 Standard_Real zp1 = PtPers.Z();
1943 Standard_Real zp2 = pt2.Z();
1944 PtPers = pt2;
1945 if ((zp1 >= curview->FocalDist*precpers) &&
1946 (zp2 >= curview->FocalDist*precpers) ) {
1947 return; // segment is not visible in perspective (behind the eye)
1948 }
1949 else if (zp1 >= curview->FocalDist*precpers) {
1950 xp1=xp1+(xp2-xp1)*(curview->FocalDist*precpers-zp1)/(zp2-zp1);
1951 yp1=yp1+(yp2-yp1)*(curview->FocalDist*precpers-zp1)/(zp2-zp1);
1952 zp1=curview->FocalDist*precpers;
1953 xp1=xp1*curview->FocalDist/(curview->FocalDist-zp1);
1954 yp1=yp1*curview->FocalDist/(curview->FocalDist-zp1);
1955 MoveTo(gp_Pnt2d(xp1*curview->Zoom,yp1*curview->Zoom));
1956 }
1957 else if (zp2 >= curview->FocalDist*precpers) {
1958 xp2=xp2+(xp1-xp2)*(curview->FocalDist*precpers-zp2)/(zp1-zp2);
1959 yp2=yp2+(yp1-yp2)*(curview->FocalDist*precpers-zp2)/(zp1-zp2);
1960 zp2=curview->FocalDist*precpers;
1961 }
1962 xp2=xp2*curview->FocalDist/(curview->FocalDist-zp2);
1963 yp2=yp2*curview->FocalDist/(curview->FocalDist-zp2);
1964 }
1965 DrawTo(gp_Pnt2d(xp2,yp2));
1966 if (CurrentMode == PICK) {
1967 if (!found) lastPickP1 = pt;
1968 else lastPickP2 = pt;
1969 }
1970}
1971
1972//=======================================================================
1973//function : Draw
1974//purpose :
1975//=======================================================================
1976
1977void Draw_Display::Draw (const gp_Pnt& p1, const gp_Pnt& p2)
1978{
1979 if (Draw_Batch) return;
1980 MoveTo(p1);
1981 DrawTo(p2);
1982}
1983
1984//=======================================================================
1985//function : Draw
1986//purpose :
1987//=======================================================================
1988
1989void Draw_Display::Draw(const gp_Pnt2d& p1, const gp_Pnt2d& p2)
1990{
1991 if (Draw_Batch) return;
1992 MoveTo(p1);
1993 DrawTo(p2);
1994}
1995
1996
1997//=======================================================================
1998//function : ViewId
1999//purpose :
2000//=======================================================================
2001
2002Standard_Integer Draw_Display::ViewId() const
2003{
2004 if (Draw_Batch) return 0;
2005 return curviewId;
2006}
2007
2008
2009//=======================================================================
2010//function : HasPicked
2011//purpose :
2012//=======================================================================
2013
2014Standard_Boolean Draw_Display::HasPicked() const
2015{
2016 if (Draw_Batch) return Standard_False;
2017 return found;
2018}
2019