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