b1535eb5efff3a384263666fb3ef61dc9ddc097f
[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.ixx>
18 #include <PrsMgr_Presentation.hxx>
19 #include <PrsMgr_ModedPresentation.hxx>
20 #include <PrsMgr_PresentationManager.hxx>
21 #include <TColStd_ListIteratorOfListOfInteger.hxx>
22 #include <TColStd_MapOfInteger.hxx>
23 #include <Graphic3d_TypeOfStructure.hxx>
24 #include <Geom_Transformation.hxx>
25
26 //=======================================================================
27 //function : PrsMgr_PresentableObject
28 //purpose  :
29 //=======================================================================
30 PrsMgr_PresentableObject::PrsMgr_PresentableObject (const PrsMgr_TypeOfPresentation3d theType)
31 : myTypeOfPresentation3d (theType),
32   myIsMutable (Standard_False)
33 {
34   myTransformPersistence.Flag = 0;
35   myTransformPersistence.Point.x = 0.0;
36   myTransformPersistence.Point.y = 0.0;
37   myTransformPersistence.Point.z = 0.0;
38 }
39
40 //=======================================================================
41 //function : Fill
42 //purpose  : 
43 //=======================================================================
44 void PrsMgr_PresentableObject::Fill (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
45                                      const Handle(PrsMgr_Presentation)&        thePrs,
46                                      const Standard_Integer                    theMode)
47 {
48   Handle(Prs3d_Presentation) aStruct3d = thePrs->Presentation();
49   Compute (thePrsMgr, aStruct3d, theMode);
50   UpdateLocation (aStruct3d);
51   aStruct3d->SetClipPlanes (myClipPlanes);
52   aStruct3d->SetTransformPersistence (GetTransformPersistenceMode(), GetTransformPersistencePoint());
53 }
54
55 //=======================================================================
56 //function : Compute
57 //purpose  :
58 //=======================================================================
59 void PrsMgr_PresentableObject::Compute (const Handle(PrsMgr_PresentationManager)& /*aPresentationManager*/,
60                                         const Handle(Prs3d_Presentation)& /*aPresentation*/,
61                                         const Standard_Integer /*aMode*/)
62 {
63   Standard_NotImplemented::Raise("cannot compute in a 3d visualizer");
64 }
65
66 //=======================================================================
67 //function : Compute
68 //purpose  : 
69 //=======================================================================
70 void PrsMgr_PresentableObject::Compute(const Handle(Prs3d_Projector)& /*aProjector*/,
71                                        const Handle(Prs3d_Presentation)& /*aPresentation*/)
72 {
73   Standard_NotImplemented::Raise("cannot compute under a specific projector");
74 }
75
76 //=======================================================================
77 //function : Compute
78 //purpose  : 
79 //=======================================================================
80 void PrsMgr_PresentableObject::Compute(const Handle(Prs3d_Projector)& /* aProjector*/,
81                                        const Handle(Geom_Transformation)& /*aTrsf*/,
82                                                                const Handle(Prs3d_Presentation)& /*aPresentation*/)
83 {
84   Standard_NotImplemented::Raise("cannot compute under a specific projector");
85 }
86
87 //=======================================================================
88 //function : Update
89 //purpose  : 
90 //=======================================================================
91 void PrsMgr_PresentableObject::Update (const Standard_Boolean AllModes) {
92   Standard_Integer l = myPresentations.Length();
93   Handle(PrsMgr_PresentationManager) PM; 
94   for (Standard_Integer i=1; i <= l; i++) {
95     PM = myPresentations(i).Presentation()->PresentationManager();
96     if(AllModes) 
97       PM->Update(this,myPresentations(i).Mode());
98     else{
99       if(PM->IsDisplayed(this,myPresentations(i).Mode()) ||
100          PM->IsHighlighted(this,myPresentations(i).Mode())){
101         PM->Update(this,myPresentations(i).Mode());
102       }
103       else
104         SetToUpdate(myPresentations(i).Mode());
105     }
106   }
107 }
108
109 //=======================================================================
110 //function : Update
111 //purpose  : 
112 //=======================================================================
113 void PrsMgr_PresentableObject::Update (const Standard_Integer aMode, const Standard_Boolean ClearOther) {
114   Standard_Integer l = myPresentations.Length();
115   for (Standard_Integer i=1; i <= l; i++) {
116     if( myPresentations(i).Mode() == aMode){
117        Handle(PrsMgr_PresentationManager) PM=
118          myPresentations(i).Presentation()->PresentationManager();
119        
120        if(PM->IsDisplayed(this,aMode) ||
121           PM->IsHighlighted(this,aMode)){
122          PM->Update(this,aMode);
123          myPresentations(i).Presentation()->SetUpdateStatus(Standard_False);
124          
125        }
126        else
127          SetToUpdate(myPresentations(i).Mode());
128      }
129     
130   }
131   if(ClearOther) {
132     PrsMgr_Presentations save;
133     save =  myPresentations; 
134     myPresentations.Clear();
135     for (Standard_Integer i=1; i <= l; i++) {
136       if( save(i).Mode() == aMode) myPresentations.Append(save(i));
137     }
138   }
139
140 }
141
142 //=======================================================================
143 //function : Presentations
144 //purpose  : 
145 //=======================================================================
146 PrsMgr_Presentations& PrsMgr_PresentableObject::Presentations() {
147   return myPresentations;
148 }
149
150 //=======================================================================
151 //function : HasLocation
152 //purpose  : 
153 //=======================================================================
154 Standard_Boolean PrsMgr_PresentableObject::HasLocation() const 
155 {
156   return !Location().IsIdentity();
157 }
158
159 //=======================================================================
160 //function : SetToUpdate
161 //purpose  : 
162 //=======================================================================
163 void PrsMgr_PresentableObject::SetToUpdate(const Standard_Integer aMode)
164 {
165   for(Standard_Integer IP =1; IP<=myPresentations.Length();IP++){
166     if(myPresentations(IP).Mode()==aMode)
167       myPresentations(IP).Presentation()->SetUpdateStatus(Standard_True);
168   }
169 }
170
171 //=======================================================================
172 //function : SetToUpdate
173 //purpose  :
174 //=======================================================================
175 void PrsMgr_PresentableObject::SetToUpdate()
176 {
177   for(Standard_Integer IP =1; IP<=myPresentations.Length();IP++){
178     myPresentations(IP).Presentation()->SetUpdateStatus(Standard_True);
179   }
180 }
181
182 //=======================================================================
183 //function : ToBeUpdated
184 //purpose  : gets the list of modes to be updated
185 //=======================================================================
186 void PrsMgr_PresentableObject::ToBeUpdated(TColStd_ListOfInteger& OutList) const
187 {
188   OutList.Clear();
189   // on dimensionne les buckets a la taille de la seq.
190   static TColStd_MapOfInteger MI(myPresentations.Length()); 
191   
192   for(Standard_Integer IP =1; IP<=myPresentations.Length();IP++){
193     const PrsMgr_ModedPresentation& MP = myPresentations(IP);
194     if(MP.Presentation()->MustBeUpdated())
195       if(!MI.Contains(MP.Mode())){
196         OutList.Append(MP.Mode());
197         MI.Add(MP.Mode());
198       }
199   }
200   MI.Clear();
201 }
202
203 //=======================================================================
204 //function : SetTypeOfPresentation
205 //purpose  :
206 //=======================================================================
207 void PrsMgr_PresentableObject::SetTypeOfPresentation (const PrsMgr_TypeOfPresentation3d theType)
208 {
209   myTypeOfPresentation3d = theType;
210   for(Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
211   {
212     const Handle(PrsMgr_Presentation)& aPrs  = myPresentations (aPrsIter).Presentation();
213     aPrs->Presentation()->SetVisual (myTypeOfPresentation3d == PrsMgr_TOP_ProjectorDependant
214                                    ? Graphic3d_TOS_COMPUTED
215                                    : Graphic3d_TOS_ALL);
216   }
217 }
218
219 //=======================================================================
220 //function : SetLocation
221 //purpose  : WARNING : use with only 3D objects...
222 //=======================================================================
223 void PrsMgr_PresentableObject::SetLocation(const TopLoc_Location& aLoc) 
224 {
225   if(aLoc.IsIdentity()) return;
226   myLocation = aLoc;
227   UpdateLocation();
228 }
229
230 //=======================================================================
231 //function : ReSetLocation
232 //purpose  : 
233 //=======================================================================
234 void PrsMgr_PresentableObject::ResetLocation() 
235 {
236   TopLoc_Location aLoc;
237   Handle(Geom_Transformation) aTrsf = new Geom_Transformation (aLoc.Transformation());
238   for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
239   {
240     myPresentations (aPrsIter).Presentation()->Transform (aTrsf);
241   }
242   myLocation = aLoc;
243 }
244
245 //=======================================================================
246 //function : UpdateLocation
247 //purpose  :
248 //=======================================================================
249 void PrsMgr_PresentableObject::UpdateLocation()
250 {
251   if (!HasLocation())
252   {
253     return;
254   }
255
256   Handle(Geom_Transformation) aTrsf = new Geom_Transformation (Location().Transformation());
257   if (aTrsf->Trsf().Form() == gp_Identity)
258   {
259     return;
260   }
261
262   for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
263   {
264     myPresentations (aPrsIter).Presentation()->Transform (aTrsf);
265   }
266 }
267
268 //=======================================================================
269 //function : UpdateLocation
270 //purpose  : 
271 //=======================================================================
272 void PrsMgr_PresentableObject::UpdateLocation(const Handle(Prs3d_Presentation)& P)
273 {
274   if(myLocation.IsIdentity()) return;
275   Handle(Geom_Transformation) G = new Geom_Transformation(Location().Transformation());
276   P->Transform(G);
277   
278 }
279
280 //=======================================================================
281 //function : SetTransformPersistence
282 //purpose  :
283 //=======================================================================
284 void PrsMgr_PresentableObject::SetTransformPersistence (const Graphic3d_TransModeFlags& theFlag,
285                                                         const gp_Pnt&                   thePoint)
286 {
287   myTransformPersistence.Flag    = theFlag;
288   myTransformPersistence.Point.x = (Standard_ShortReal )thePoint.X();
289   myTransformPersistence.Point.y = (Standard_ShortReal )thePoint.Y();
290   myTransformPersistence.Point.z = (Standard_ShortReal )thePoint.Z();
291   for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
292   {
293     const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
294     if (!aPrs3d.IsNull()
295      && !aPrs3d->Presentation().IsNull())
296     {
297       aPrs3d->Presentation()->SetTransformPersistence (theFlag, thePoint);
298       aPrs3d->Presentation()->ReCompute();
299     }
300   }
301 }
302
303 //=======================================================================
304 //function : SetTransformPersistence
305 //purpose  : 
306 //=======================================================================
307 void  PrsMgr_PresentableObject::SetTransformPersistence( 
308                                        const Graphic3d_TransModeFlags& TheFlag )
309 {
310   SetTransformPersistence( TheFlag, gp_Pnt(0,0,0) );
311 }
312
313 //=======================================================================
314 //function : GetTransformPersistence
315 //purpose  : 
316 //=======================================================================
317 Graphic3d_TransModeFlags  PrsMgr_PresentableObject::GetTransformPersistenceMode() const
318 {
319   return myTransformPersistence.Flag;
320 }
321
322 //=======================================================================
323 //function : GetTransformPersistence
324 //purpose  : 
325 //=======================================================================
326 gp_Pnt  PrsMgr_PresentableObject::GetTransformPersistencePoint() const
327 {
328   return gp_Pnt( myTransformPersistence.Point.x, myTransformPersistence.Point.y, myTransformPersistence.Point.z );
329 }
330
331 //=======================================================================
332 //function : SetZLayer
333 //purpose  :
334 //=======================================================================
335 void PrsMgr_PresentableObject::SetZLayer 
336   (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
337    const Standard_Integer theLayerId)
338 {
339   if (!thePrsMgr.IsNull())
340     thePrsMgr->SetZLayer (this, theLayerId);
341 }
342
343 //=======================================================================
344 //function : GetZLayer
345 //purpose  :
346 //=======================================================================
347 Standard_Integer PrsMgr_PresentableObject::GetZLayer 
348   (const Handle(PrsMgr_PresentationManager)& thePrsMgr) const
349 {
350   if (!thePrsMgr.IsNull())
351     return thePrsMgr->GetZLayer (this);
352
353   return -1;
354 }
355
356 // =======================================================================
357 // function : AddClipPlane
358 // purpose  :
359 // =======================================================================
360 void PrsMgr_PresentableObject::AddClipPlane (const Handle(Graphic3d_ClipPlane)& thePlane)
361 {
362   // add to collection and process changes
363   myClipPlanes.Append (thePlane);
364   UpdateClipping();
365 }
366
367 // =======================================================================
368 // function : RemoveClipPlane
369 // purpose  :
370 // =======================================================================
371 void PrsMgr_PresentableObject::RemoveClipPlane (const Handle(Graphic3d_ClipPlane)& thePlane)
372 {
373   // remove from collection and process changes
374   Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (myClipPlanes);
375   for (; aPlaneIt.More(); aPlaneIt.Next())
376   {
377     const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
378     if (aPlane != thePlane)
379       continue;
380
381     myClipPlanes.Remove (aPlaneIt);
382     UpdateClipping();
383     return;
384   }
385 }
386
387 // =======================================================================
388 // function : SetClipPlanes
389 // purpose  :
390 // =======================================================================
391 void PrsMgr_PresentableObject::SetClipPlanes (const Graphic3d_SequenceOfHClipPlane& thePlanes)
392 {
393   // change collection and process changes
394   myClipPlanes = thePlanes;
395   UpdateClipping();
396 }
397
398 // =======================================================================
399 // function : UpdateClipping
400 // purpose  :
401 // =======================================================================
402 void PrsMgr_PresentableObject::UpdateClipping()
403 {
404   // affect generated structures
405   for (Standard_Integer aPrsIt = 1; aPrsIt <= myPresentations.Length(); ++aPrsIt)
406   {
407     // pass over presentation manager 3d mechanism right to the structures -
408     // we do not interested in display mode collections.
409     const PrsMgr_ModedPresentation& aModedPrs = myPresentations (aPrsIt);
410     if (aModedPrs.Presentation().IsNull()
411      || aModedPrs.Presentation()->Presentation().IsNull())
412     {
413       continue;
414     }
415
416     aModedPrs.Presentation()->Presentation()->SetClipPlanes (myClipPlanes);
417   }
418 }
419
420 // =======================================================================
421 // function : SetMutable
422 // purpose  :
423 // =======================================================================
424 void PrsMgr_PresentableObject::SetMutable (const Standard_Boolean theIsMutable)
425 {
426   if (myIsMutable == theIsMutable)
427   {
428     return;
429   }
430
431   myIsMutable = theIsMutable;
432   for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
433   {
434     const PrsMgr_ModedPresentation& aModedPrs = myPresentations (aPrsIter);
435     if (aModedPrs.Presentation().IsNull()
436      || aModedPrs.Presentation()->Presentation().IsNull())
437     {
438       continue;
439     }
440
441     aModedPrs.Presentation()->Presentation()->SetMutable (theIsMutable);
442   }
443 }
444
445 // =======================================================================
446 // function : IsMutable
447 // purpose  :
448 // =======================================================================
449 const Standard_Boolean PrsMgr_PresentableObject::IsMutable() const
450 {
451   return myIsMutable;
452 }