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