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