0025695: Visualization, AIS_InteractiveContext - define default HilightMode
[occt.git] / src / PrsMgr / PrsMgr_PresentableObject.cxx
1 // Created on: 1997-12-16
2 // Created by: Jean Louis Frenkel
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <PrsMgr_PresentableObject.hxx>
18
19 #include <Prs3d_Drawer.hxx>
20 #include <Prs3d_Presentation.hxx>
21 #include <Prs3d_Projector.hxx>
22 #include <PrsMgr_ModedPresentation.hxx>
23 #include <Standard_NotImplemented.hxx>
24 #include <TColStd_ListIteratorOfListOfInteger.hxx>
25 #include <TColStd_MapOfInteger.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(PrsMgr_PresentableObject, Standard_Transient)
28
29 namespace
30 {
31   static const gp_Trsf THE_IDENTITY_TRSF;
32 }
33
34 //=======================================================================
35 //function : getIdentityTrsf
36 //purpose  :
37 //=======================================================================
38 const gp_Trsf& PrsMgr_PresentableObject::getIdentityTrsf()
39 {
40   return THE_IDENTITY_TRSF;
41 }
42
43 //=======================================================================
44 //function : PrsMgr_PresentableObject
45 //purpose  :
46 //=======================================================================
47 PrsMgr_PresentableObject::PrsMgr_PresentableObject (const PrsMgr_TypeOfPresentation3d theType)
48 : myDrawer (new Prs3d_Drawer()),
49   myTypeOfPresentation3d (theType),
50   myIsMutable (Standard_False),
51   myHasOwnPresentations (Standard_True),
52   myParent (NULL)
53 {
54   myDrawer->SetDisplayMode (-1);
55 }
56
57 //=======================================================================
58 //function : ~PrsMgr_PresentableObject
59 //purpose  : destructor
60 //=======================================================================
61 PrsMgr_PresentableObject::~PrsMgr_PresentableObject()
62 {
63   for (PrsMgr_ListOfPresentableObjectsIter anIter (myChildren); anIter.More(); anIter.Next())
64   {
65     anIter.Value()->SetCombinedParentTransform (Handle(Geom_Transformation)());
66     anIter.Value()->myParent = NULL;
67   }
68 }
69
70 //=======================================================================
71 //function : Fill
72 //purpose  : 
73 //=======================================================================
74 void PrsMgr_PresentableObject::Fill (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
75                                      const Handle(PrsMgr_Presentation)&        thePrs,
76                                      const Standard_Integer                    theMode)
77 {
78   Handle(Prs3d_Presentation) aStruct3d = thePrs->Presentation();
79   Compute (thePrsMgr, aStruct3d, theMode);
80   UpdateTransformation (aStruct3d);
81   aStruct3d->SetClipPlanes (myClipPlanes);
82   aStruct3d->SetTransformPersistence (TransformPersistence());
83 }
84
85 //=======================================================================
86 //function : Compute
87 //purpose  :
88 //=======================================================================
89 void PrsMgr_PresentableObject::Compute (const Handle(PrsMgr_PresentationManager)& /*aPresentationManager*/,
90                                         const Handle(Prs3d_Presentation)& /*aPresentation*/,
91                                         const Standard_Integer /*aMode*/)
92 {
93   Standard_NotImplemented::Raise("cannot compute in a 3d visualizer");
94 }
95
96 //=======================================================================
97 //function : Compute
98 //purpose  : 
99 //=======================================================================
100 void PrsMgr_PresentableObject::Compute(const Handle(Prs3d_Projector)& /*aProjector*/,
101                                        const Handle(Prs3d_Presentation)& /*aPresentation*/)
102 {
103   Standard_NotImplemented::Raise("cannot compute under a specific projector");
104 }
105
106 //=======================================================================
107 //function : Compute
108 //purpose  : 
109 //=======================================================================
110 void PrsMgr_PresentableObject::Compute(const Handle(Prs3d_Projector)& /* aProjector*/,
111                                        const Handle(Geom_Transformation)& /*aTrsf*/,
112                                                                const Handle(Prs3d_Presentation)& /*aPresentation*/)
113 {
114   Standard_NotImplemented::Raise("cannot compute under a specific projector");
115 }
116
117 //=======================================================================
118 //function : Update
119 //purpose  : 
120 //=======================================================================
121 void PrsMgr_PresentableObject::Update (const Standard_Boolean AllModes) {
122   Standard_Integer l = myPresentations.Length();
123   Handle(PrsMgr_PresentationManager) PM; 
124   for (Standard_Integer i=1; i <= l; i++) {
125     PM = myPresentations(i).Presentation()->PresentationManager();
126     if(AllModes) 
127       PM->Update(this,myPresentations(i).Mode());
128     else{
129       if(PM->IsDisplayed(this,myPresentations(i).Mode()) ||
130          PM->IsHighlighted(this,myPresentations(i).Mode())){
131         PM->Update(this,myPresentations(i).Mode());
132       }
133       else
134         SetToUpdate(myPresentations(i).Mode());
135     }
136   }
137 }
138
139 //=======================================================================
140 //function : Update
141 //purpose  : 
142 //=======================================================================
143 void PrsMgr_PresentableObject::Update (const Standard_Integer aMode, const Standard_Boolean ClearOther) {
144   Standard_Integer l = myPresentations.Length();
145   for (Standard_Integer i=1; i <= l; i++) {
146     if( myPresentations(i).Mode() == aMode){
147        Handle(PrsMgr_PresentationManager) PM=
148          myPresentations(i).Presentation()->PresentationManager();
149        
150        if(PM->IsDisplayed(this,aMode) ||
151           PM->IsHighlighted(this,aMode)){
152          PM->Update(this,aMode);
153          myPresentations(i).Presentation()->SetUpdateStatus(Standard_False);
154          
155        }
156        else
157          SetToUpdate(myPresentations(i).Mode());
158      }
159     
160   }
161   if(ClearOther) {
162     PrsMgr_Presentations save;
163     save =  myPresentations; 
164     myPresentations.Clear();
165     for (Standard_Integer i=1; i <= l; i++) {
166       if( save(i).Mode() == aMode) myPresentations.Append(save(i));
167     }
168   }
169
170 }
171
172 //=======================================================================
173 //function : SetToUpdate
174 //purpose  : 
175 //=======================================================================
176 void PrsMgr_PresentableObject::SetToUpdate(const Standard_Integer aMode)
177 {
178   for(Standard_Integer IP =1; IP<=myPresentations.Length();IP++){
179     if(myPresentations(IP).Mode()==aMode)
180       myPresentations(IP).Presentation()->SetUpdateStatus(Standard_True);
181   }
182 }
183
184 //=======================================================================
185 //function : SetToUpdate
186 //purpose  :
187 //=======================================================================
188 void PrsMgr_PresentableObject::SetToUpdate()
189 {
190   for(Standard_Integer IP =1; IP<=myPresentations.Length();IP++){
191     myPresentations(IP).Presentation()->SetUpdateStatus(Standard_True);
192   }
193 }
194
195 //=======================================================================
196 //function : ToBeUpdated
197 //purpose  : gets the list of modes to be updated
198 //=======================================================================
199 void PrsMgr_PresentableObject::ToBeUpdated(TColStd_ListOfInteger& OutList) const
200 {
201   OutList.Clear();
202   // on dimensionne les buckets a la taille de la seq.
203   TColStd_MapOfInteger MI(myPresentations.Length()); 
204   
205   for(Standard_Integer IP =1; IP<=myPresentations.Length();IP++){
206     const PrsMgr_ModedPresentation& MP = myPresentations(IP);
207     if(MP.Presentation()->MustBeUpdated())
208       if(!MI.Contains(MP.Mode())){
209         OutList.Append(MP.Mode());
210         MI.Add(MP.Mode());
211       }
212   }
213 }
214
215 //=======================================================================
216 //function : SetTypeOfPresentation
217 //purpose  :
218 //=======================================================================
219 void PrsMgr_PresentableObject::SetTypeOfPresentation (const PrsMgr_TypeOfPresentation3d theType)
220 {
221   myTypeOfPresentation3d = theType;
222   for(Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
223   {
224     const Handle(PrsMgr_Presentation)& aPrs  = myPresentations (aPrsIter).Presentation();
225     aPrs->Presentation()->SetVisual (myTypeOfPresentation3d == PrsMgr_TOP_ProjectorDependant
226                                    ? Graphic3d_TOS_COMPUTED
227                                    : Graphic3d_TOS_ALL);
228   }
229 }
230
231 //=======================================================================
232 //function : setLocalTransformation
233 //purpose  :
234 //=======================================================================
235 void PrsMgr_PresentableObject::setLocalTransformation (const Handle(Geom_Transformation)& theTransformation)
236 {
237   myLocalTransformation = theTransformation;
238   UpdateTransformation();
239 }
240
241 //=======================================================================
242 //function : ResetTransformation
243 //purpose  : 
244 //=======================================================================
245 void PrsMgr_PresentableObject::ResetTransformation() 
246 {
247   setLocalTransformation (Handle(Geom_Transformation)());
248 }
249
250 //=======================================================================
251 //function : SetCombinedParentTransform
252 //purpose  : 
253 //=======================================================================
254 void PrsMgr_PresentableObject::SetCombinedParentTransform (const Handle(Geom_Transformation)& theTrsf)
255 {
256   myCombinedParentTransform = theTrsf;
257   UpdateTransformation();
258 }
259
260 //=======================================================================
261 //function : UpdateTransformation
262 //purpose  :
263 //=======================================================================
264 void PrsMgr_PresentableObject::UpdateTransformation()
265 {
266   myTransformation.Nullify();
267   myInvTransformation = gp_Trsf();
268   if (!myCombinedParentTransform.IsNull() && myCombinedParentTransform->Form() != gp_Identity)
269   {
270     if (!myLocalTransformation.IsNull() && myLocalTransformation->Form() != gp_Identity)
271     {
272       const gp_Trsf aTrsf = myCombinedParentTransform->Trsf() * myLocalTransformation->Trsf();
273       myTransformation    = new Geom_Transformation (aTrsf);
274       myInvTransformation = aTrsf.Inverted();
275     }
276     else
277     {
278       myTransformation    = myCombinedParentTransform;
279       myInvTransformation = myCombinedParentTransform->Trsf().Inverted();
280     }
281   }
282   else if (!myLocalTransformation.IsNull() && myLocalTransformation->Form() != gp_Identity)
283   {
284     myTransformation    = myLocalTransformation;
285     myInvTransformation = myLocalTransformation->Trsf().Inverted();
286   }
287
288   for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
289   {
290     myPresentations (aPrsIter).Presentation()->SetTransformation (myTransformation);
291   }
292
293   for (PrsMgr_ListOfPresentableObjectsIter aChildIter (myChildren); aChildIter.More(); aChildIter.Next())
294   {
295     aChildIter.Value()->SetCombinedParentTransform (myTransformation);
296   }
297 }
298
299 //=======================================================================
300 //function : UpdateTransformation
301 //purpose  : 
302 //=======================================================================
303 void PrsMgr_PresentableObject::UpdateTransformation(const Handle(Prs3d_Presentation)& P)
304 {
305   P->SetTransformation (myTransformation);
306 }
307
308 //=======================================================================
309 //function : SetTransformPersistence
310 //purpose  :
311 //=======================================================================
312 void PrsMgr_PresentableObject::SetTransformPersistence (const Handle(Graphic3d_TransformPers)& theTrsfPers)
313 {
314   myTransformPersistence = theTrsfPers;
315   for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
316   {
317     const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
318     if (!aPrs3d.IsNull()
319      && !aPrs3d->Presentation().IsNull())
320     {
321       aPrs3d->Presentation()->SetTransformPersistence (myTransformPersistence);
322       aPrs3d->Presentation()->ReCompute();
323     }
324   }
325 }
326
327 //=======================================================================
328 //function : GetTransformPersistence
329 //purpose  :
330 //=======================================================================
331 gp_Pnt PrsMgr_PresentableObject::GetTransformPersistencePoint() const
332 {
333   if (myTransformPersistence.IsNull())
334   {
335     return gp_Pnt();
336   }
337   else if (myTransformPersistence->IsZoomOrRotate())
338   {
339     return myTransformPersistence->AnchorPoint();
340   }
341   else if (!myTransformPersistence->IsTrihedronOr2d())
342   {
343     return gp_Pnt();
344   }
345
346   Standard_Real anX = 0.0;
347   if ((myTransformPersistence->Corner2d() & Aspect_TOTP_RIGHT) != 0)
348   {
349     anX = 1.0;
350   }
351   else if ((myTransformPersistence->Corner2d() & Aspect_TOTP_LEFT) != 0)
352   {
353     anX = -1.0;
354   }
355
356   Standard_Real anY = 0.0;
357   if ((myTransformPersistence->Corner2d() & Aspect_TOTP_TOP) != 0)
358   {
359     anY = 1.0;
360   }
361   else if ((myTransformPersistence->Corner2d() & Aspect_TOTP_BOTTOM) != 0)
362   {
363     anY = -1.0;
364   }
365
366   return gp_Pnt (anX, anY, myTransformPersistence->Offset2d().x());
367 }
368
369 //=======================================================================
370 //function : AddChild
371 //purpose  : 
372 //=======================================================================
373 void PrsMgr_PresentableObject::AddChild (const Handle(PrsMgr_PresentableObject)& theObject)
374 {
375   Handle(PrsMgr_PresentableObject) aHandleGuard = theObject;
376   if (theObject->myParent != NULL)
377   {
378     theObject->myParent->RemoveChild (aHandleGuard);
379   }
380
381   myChildren.Append (theObject);  
382   theObject->myParent = this;
383   theObject->SetCombinedParentTransform (myTransformation);
384 }
385
386 //=======================================================================
387 //function : RemoveChild
388 //purpose  : 
389 //=======================================================================
390 void PrsMgr_PresentableObject::RemoveChild (const Handle(PrsMgr_PresentableObject)& theObject)
391 {
392   PrsMgr_ListOfPresentableObjectsIter anIter (myChildren);
393   for (; anIter.More(); anIter.Next())
394   {
395     if (anIter.Value() == theObject)
396     {
397       theObject->myParent = NULL;
398       theObject->SetCombinedParentTransform (Handle(Geom_Transformation)());
399       myChildren.Remove (anIter);
400       break;
401     }
402   }
403 }
404
405 //=======================================================================
406 //function : SetZLayer
407 //purpose  :
408 //=======================================================================
409 void PrsMgr_PresentableObject::SetZLayer (const Graphic3d_ZLayerId theLayerId)
410 {
411   if (myDrawer->ZLayer() == theLayerId)
412   {
413     return;
414   }
415
416   myDrawer->SetZLayer (theLayerId);
417   for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
418   {
419     const PrsMgr_ModedPresentation& aModedPrs = myPresentations (aPrsIter);
420     if (aModedPrs.Presentation().IsNull()
421      || aModedPrs.Presentation()->Presentation().IsNull())
422     {
423       continue;
424     }
425
426     aModedPrs.Presentation()->Presentation()->SetZLayer (theLayerId);
427   }
428 }
429
430 //=======================================================================
431 //function : ZLayer
432 //purpose  :
433 //=======================================================================
434 Graphic3d_ZLayerId PrsMgr_PresentableObject::ZLayer() const
435 {
436   return myDrawer->ZLayer();
437 }
438
439 // =======================================================================
440 // function : AddClipPlane
441 // purpose  :
442 // =======================================================================
443 void PrsMgr_PresentableObject::AddClipPlane (const Handle(Graphic3d_ClipPlane)& thePlane)
444 {
445   // add to collection and process changes
446   if (myClipPlanes.IsNull())
447   {
448     myClipPlanes = new Graphic3d_SequenceOfHClipPlane();
449   }
450
451   myClipPlanes->Append (thePlane);
452   UpdateClipping();
453 }
454
455 // =======================================================================
456 // function : RemoveClipPlane
457 // purpose  :
458 // =======================================================================
459 void PrsMgr_PresentableObject::RemoveClipPlane (const Handle(Graphic3d_ClipPlane)& thePlane)
460 {
461   if (myClipPlanes.IsNull())
462   {
463     return;
464   }
465
466   // remove from collection and process changes
467   for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (*myClipPlanes); aPlaneIt.More(); aPlaneIt.Next())
468   {
469     const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
470     if (aPlane != thePlane)
471       continue;
472
473     myClipPlanes->Remove (aPlaneIt);
474     UpdateClipping();
475     return;
476   }
477 }
478
479 // =======================================================================
480 // function : SetClipPlanes
481 // purpose  :
482 // =======================================================================
483 void PrsMgr_PresentableObject::SetClipPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes)
484 {
485   // change collection and process changes
486   myClipPlanes = thePlanes;
487   UpdateClipping();
488 }
489
490 // =======================================================================
491 // function : UpdateClipping
492 // purpose  :
493 // =======================================================================
494 void PrsMgr_PresentableObject::UpdateClipping()
495 {
496   // affect generated structures
497   for (Standard_Integer aPrsIt = 1; aPrsIt <= myPresentations.Length(); ++aPrsIt)
498   {
499     // pass over presentation manager 3d mechanism right to the structures -
500     // we do not interested in display mode collections.
501     const PrsMgr_ModedPresentation& aModedPrs = myPresentations (aPrsIt);
502     if (aModedPrs.Presentation().IsNull()
503      || aModedPrs.Presentation()->Presentation().IsNull())
504     {
505       continue;
506     }
507
508     aModedPrs.Presentation()->Presentation()->SetClipPlanes (myClipPlanes);
509   }
510 }
511
512 // =======================================================================
513 // function : SetMutable
514 // purpose  :
515 // =======================================================================
516 void PrsMgr_PresentableObject::SetMutable (const Standard_Boolean theIsMutable)
517 {
518   if (myIsMutable == theIsMutable)
519   {
520     return;
521   }
522
523   myIsMutable = theIsMutable;
524   for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
525   {
526     const PrsMgr_ModedPresentation& aModedPrs = myPresentations (aPrsIter);
527     if (aModedPrs.Presentation().IsNull()
528      || aModedPrs.Presentation()->Presentation().IsNull())
529     {
530       continue;
531     }
532
533     aModedPrs.Presentation()->Presentation()->SetMutable (theIsMutable);
534   }
535 }
536
537 // =======================================================================
538 // function : SetAttributes
539 // purpose  :
540 // =======================================================================
541 void PrsMgr_PresentableObject::SetAttributes (const Handle(Prs3d_Drawer)& theDrawer)
542 {
543   myDrawer = theDrawer;
544 }
545
546 // =======================================================================
547 // function : UnsetAttributes
548 // purpose  :
549 // =======================================================================
550 void PrsMgr_PresentableObject::UnsetAttributes()
551 {
552   Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer();
553   if (myDrawer->HasLink())
554   {
555     aDrawer->Link(myDrawer->Link());
556   }
557   myDrawer = aDrawer;
558 }