0027818: Visualization - provide an interface to define highlight presentation properties
[occt.git] / src / Graphic3d / Graphic3d_CView.cxx
CommitLineData
c357e426 1// Copyright (c) 2015 OPEN CASCADE SAS
2//
3// This file is part of Open CASCADE Technology software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
13
14#include <Graphic3d_CView.hxx>
15#include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
16#include <Graphic3d_StructureManager.hxx>
17
92efcf78 18IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_CView,Graphic3d_DataStructureManager)
19
50d06d8f 20namespace
21{
22 static const int THE_DEFAULT_LAYERS[] = { Graphic3d_ZLayerId_Top,
23 Graphic3d_ZLayerId_Topmost,
24 Graphic3d_ZLayerId_BotOSD,
25 Graphic3d_ZLayerId_TopOSD };
26
27 static const int THE_NB_DEFAULT_LAYERS = sizeof(THE_DEFAULT_LAYERS) / sizeof(*THE_DEFAULT_LAYERS);
28
29 void combineBox (Bnd_Box& aCombined, const Graphic3d_BndBox4f& theBox)
30 {
31 if (theBox.IsValid())
32 {
33 aCombined.Add (gp_Pnt (theBox.CornerMin().x(),
34 theBox.CornerMin().y(),
35 theBox.CornerMin().z()));
36 aCombined.Add (gp_Pnt (theBox.CornerMax().x(),
37 theBox.CornerMax().y(),
38 theBox.CornerMax().z()));
39 }
40 }
41}
42
c357e426 43//=======================================================================
44//function : Constructor
45//purpose :
46//=======================================================================
47Graphic3d_CView::Graphic3d_CView (const Handle(Graphic3d_StructureManager)& theMgr)
48: myStructureManager (theMgr),
49 myHiddenObjects (new Graphic3d_NMapOfTransient()),
50 myIsInComputedMode (Standard_False),
51 myIsActive (Standard_False),
52 myIsRemoved (Standard_False),
53 myVisualization (Graphic3d_TOV_WIREFRAME)
54{
55 myId = myStructureManager->Identification (this);
56}
57
58//=======================================================================
59//function : Destructor
60//purpose :
61//=======================================================================
62Graphic3d_CView::~Graphic3d_CView()
63{
64 if (!IsRemoved())
65 {
66 myStructureManager->UnIdentification (this);
67 }
68}
69
70// =======================================================================
71// function : Activate
72// purpose :
73// =======================================================================
74void Graphic3d_CView::Activate()
75{
76 if (!IsActive())
77 {
78 myIsActive = Standard_True;
79
80 // Activation of a new view =>
81 // Display structures that can be displayed in this new view.
82 // All structures with status
83 // Displayed in ViewManager are returned and displayed in
84 // the view directly, if the structure is not already
85 // displayed and if the view accepts it in its context.
86 Graphic3d_MapOfStructure aDisplayedStructs;
87 myStructureManager->DisplayedStructures (aDisplayedStructs);
88 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aDisplayedStructs); aStructIter.More(); aStructIter.Next())
89 {
90 const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
91 if (IsDisplayed (aStruct))
92 {
93 continue;
94 }
95
96 // If the structure can be displayed in the new context of the view, it is displayed.
97 const Graphic3d_TypeOfAnswer anAnswer = acceptDisplay (aStruct->Visual());
98 if (anAnswer == Graphic3d_TOA_YES
99 || anAnswer == Graphic3d_TOA_COMPUTE)
100 {
101 Display (aStruct, Aspect_TOU_WAIT);
102 }
103 }
104 }
105
106 Update (myStructureManager->UpdateMode());
107}
108
109// =======================================================================
110// function : Deactivate
111// purpose :
112// =======================================================================
113void Graphic3d_CView::Deactivate()
114{
115 if (IsActive())
116 {
117 // Deactivation of a view =>
118 // Removal of structures displayed in this view.
119 // All structures with status
120 // Displayed in ViewManager are returned and removed from
121 // the view directly, if the structure is not already
122 // displayed and if the view accepts it in its context.
123 Graphic3d_MapOfStructure aDisplayedStructs;
124 myStructureManager->DisplayedStructures (aDisplayedStructs);
125 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aDisplayedStructs); aStructIter.More(); aStructIter.Next())
126 {
127 const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
128 if (!IsDisplayed (aStruct))
129 {
130 continue;
131 }
132
133 const Graphic3d_TypeOfAnswer anAnswer = acceptDisplay (aStruct->Visual());
134 if (anAnswer == Graphic3d_TOA_YES
135 || anAnswer == Graphic3d_TOA_COMPUTE)
136 {
137 Erase (aStruct, Aspect_TOU_WAIT);
138 }
139 }
140
141 Update (myStructureManager->UpdateMode());
142 myIsActive = Standard_False;
143 }
144}
145
146// ========================================================================
147// function : Remove
148// purpose :
149// ========================================================================
150void Graphic3d_CView::Remove()
151{
152 if (IsRemoved())
153 {
154 return;
155 }
156
157 Graphic3d_MapOfStructure aDisplayedStructs (myStructsDisplayed);
158
159 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aDisplayedStructs); aStructIter.More(); aStructIter.Next())
160 {
161 Erase (aStructIter.Value(), Aspect_TOU_WAIT);
162 }
163
164 myStructsToCompute.Clear();
165 myStructsComputed .Clear();
166 myStructsDisplayed.Clear();
167
168 if (!myStructureManager.IsNull())
169 {
170 myStructureManager->UnIdentification (this);
171 }
172
173 myIsActive = Standard_False;
174 myIsRemoved = Standard_True;
175}
176
177// ========================================================================
178// function : SetComputedMode
179// purpose :
180// ========================================================================
181void Graphic3d_CView::SetComputedMode (const Standard_Boolean theMode)
182{
183 if (( theMode && myIsInComputedMode)
184 || (!theMode && !myIsInComputedMode))
185 {
186 return;
187 }
188
189 myIsInComputedMode = theMode;
190 if (!myIsInComputedMode)
191 {
192 for (Graphic3d_MapOfStructure::Iterator aStructIter (myStructsDisplayed); aStructIter.More(); aStructIter.Next())
193 {
194 const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
195 const Graphic3d_TypeOfAnswer anAnswer = acceptDisplay (aStruct->Visual());
196 if (anAnswer != Graphic3d_TOA_COMPUTE)
197 {
198 continue;
199 }
200
201 const Standard_Integer anIndex = IsComputed (aStruct);
202 if (anIndex != 0)
203 {
204 const Handle(Graphic3d_Structure)& aStructComp = myStructsComputed.Value (anIndex);
205 eraseStructure (aStructComp->CStructure());
206 displayStructure (aStruct->CStructure(), aStruct->DisplayPriority());
207 }
208 }
209 return;
210 }
211
212 for (Graphic3d_MapOfStructure::Iterator aDispStructIter (myStructsDisplayed); aDispStructIter.More(); aDispStructIter.Next())
213 {
214 Handle(Graphic3d_Structure) aStruct = aDispStructIter.Key();
215 const Graphic3d_TypeOfAnswer anAnswer = acceptDisplay (aStruct->Visual());
216 if (anAnswer != Graphic3d_TOA_COMPUTE)
217 {
218 continue;
219 }
220
221 const Standard_Integer anIndex = IsComputed (aStruct);
222 if (anIndex != 0)
223 {
224 eraseStructure (aStruct->CStructure());
225 displayStructure (myStructsComputed.Value (anIndex)->CStructure(), aStruct->DisplayPriority());
226
227 Display (aStruct, Aspect_TOU_WAIT);
228 if (aStruct->IsHighlighted())
229 {
230 const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.Value (anIndex);
231 if (!aCompStruct->IsHighlighted())
232 {
8e5fb5ea 233 aCompStruct->Highlight (aStruct->HighlightStyle(), Standard_False);
c357e426 234 }
235 }
236 }
237 else
238 {
1f7f5a90 239 Handle(Graphic3d_Structure) aCompStruct = aStruct->IsTransformed() ? aStruct->Compute (this, aStruct->Transformation()) : aStruct->Compute (this);
c357e426 240 aCompStruct->SetHLRValidation (Standard_True);
241
242 const Standard_Boolean toComputeWireframe = myVisualization == Graphic3d_TOV_WIREFRAME
243 && aStruct->ComputeVisual() != Graphic3d_TOS_SHADING;
244 const Standard_Boolean toComputeShading = myVisualization == Graphic3d_TOV_SHADING
245 && aStruct->ComputeVisual() != Graphic3d_TOS_WIREFRAME;
246 if (toComputeWireframe) aCompStruct->SetVisual (Graphic3d_TOS_WIREFRAME);
247 if (toComputeShading ) aCompStruct->SetVisual (Graphic3d_TOS_SHADING);
248
249 if (aStruct->IsHighlighted())
250 {
8e5fb5ea 251 aCompStruct->Highlight (aStruct->HighlightStyle(), Standard_False);
c357e426 252 }
253
254 Standard_Boolean hasResult = Standard_False;
255 const Standard_Integer aNbToCompute = myStructsToCompute.Length();
256 const Standard_Integer aStructId = aStruct->Identification();
257 for (Standard_Integer aToCompStructIter = 1; aToCompStructIter <= aNbToCompute; ++aToCompStructIter)
258 {
259 if (myStructsToCompute.Value (aToCompStructIter)->Identification() == aStructId)
260 {
261 hasResult = Standard_True;
262 myStructsComputed.ChangeValue (aToCompStructIter) = aCompStruct;
263 break;
264 }
265 }
266
267 if (!hasResult)
268 {
269 myStructsToCompute.Append (aStruct);
270 myStructsComputed .Append (aCompStruct);
271 }
272
273 eraseStructure (aStruct->CStructure());
274 displayStructure (aCompStruct->CStructure(), aStruct->DisplayPriority());
275 }
276 }
277 Update (myStructureManager->UpdateMode());
278}
279
280// =======================================================================
281// function : ReCompute
282// purpose :
283// =======================================================================
284void Graphic3d_CView::ReCompute (const Handle(Graphic3d_Structure)& theStruct)
285{
286 theStruct->CalculateBoundBox();
287 if (!theStruct->IsMutable()
288 && !theStruct->CStructure()->IsForHighlight
289 && !theStruct->CStructure()->IsInfinite)
290 {
150ed3d5 291 const Graphic3d_ZLayerId aLayerId = theStruct->GetZLayer();
c357e426 292 InvalidateBVHData (aLayerId);
293 }
294
295 if (!ComputedMode()
296 || !IsActive()
297 || !theStruct->IsDisplayed())
298 {
299 return;
300 }
301
302 const Graphic3d_TypeOfAnswer anAnswer = acceptDisplay (theStruct->Visual());
303 if (anAnswer != Graphic3d_TOA_COMPUTE)
304 {
305 return;
306 }
307
308 const Standard_Integer anIndex = IsComputed (theStruct);
309 if (anIndex == 0)
310 {
311 return;
312 }
313
314 // compute + validation
c357e426 315 Handle(Graphic3d_Structure) aCompStructOld = myStructsComputed.ChangeValue (anIndex);
316 Handle(Graphic3d_Structure) aCompStruct = aCompStructOld;
1f7f5a90 317 aCompStruct->SetTransformation (Handle(Geom_Transformation)());
318 theStruct->IsTransformed() ? theStruct->Compute (this, theStruct->Transformation(), aCompStruct)
319 : theStruct->Compute (this, aCompStruct);
c357e426 320 aCompStruct->SetHLRValidation (Standard_True);
321
322 // of which type will be the computed?
323 const Standard_Boolean toComputeWireframe = myVisualization == Graphic3d_TOV_WIREFRAME
324 && theStruct->ComputeVisual() != Graphic3d_TOS_SHADING;
325 const Standard_Boolean toComputeShading = myVisualization == Graphic3d_TOV_SHADING
326 && theStruct->ComputeVisual() != Graphic3d_TOS_WIREFRAME;
327 if (toComputeWireframe)
328 {
329 aCompStruct->SetVisual (Graphic3d_TOS_WIREFRAME);
330 }
331 else if (toComputeShading)
332 {
333 aCompStruct->SetVisual (Graphic3d_TOS_SHADING);
334 }
335
336 if (theStruct->IsHighlighted())
337 {
8e5fb5ea 338 aCompStruct->Highlight (theStruct->HighlightStyle(), Standard_False);
c357e426 339 }
340
341 // The previous calculation is removed and the new one is displayed
342 eraseStructure (aCompStructOld->CStructure());
343 displayStructure (aCompStruct->CStructure(), theStruct->DisplayPriority());
344
345 // why not just replace existing items?
346 //myStructsToCompute.ChangeValue (anIndex) = theStruct;
347 //myStructsComputed .ChangeValue (anIndex) = aCompStruct;
348
349 // hlhsr and the new associated compute are added
350 myStructsToCompute.Append (theStruct);
351 myStructsComputed .Append (aCompStruct);
352
353 // hlhsr and the new associated compute are removed
354 myStructsToCompute.Remove (anIndex);
355 myStructsComputed .Remove (anIndex);
356}
357
358// =======================================================================
359// function : Update
360// purpose :
361// =======================================================================
50d06d8f 362void Graphic3d_CView::Update (const Aspect_TypeOfUpdate theUpdateMode,
363 const Graphic3d_ZLayerId theLayerId)
c357e426 364{
50d06d8f 365 InvalidateZLayerBoundingBox (theLayerId);
366
c357e426 367 if (theUpdateMode == Aspect_TOU_ASAP)
368 {
369 Compute();
370 Redraw();
371 }
372}
373
374// =======================================================================
375// function : ContainsFacet
376// purpose :
377// =======================================================================
378Standard_Boolean Graphic3d_CView::ContainsFacet() const
379{
380 for (Graphic3d_MapOfStructure::Iterator aStructIter (myStructsDisplayed); aStructIter.More(); aStructIter.Next())
381 {
382 if (aStructIter.Key()->ContainsFacet())
383 {
384 return Standard_True;
385 }
386 }
387 return Standard_False;
388}
389
390// =======================================================================
391// function : ContainsFacet
392// purpose :
393// =======================================================================
394Standard_Boolean Graphic3d_CView::ContainsFacet (const Graphic3d_MapOfStructure& theSet) const
395{
396 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (theSet); aStructIter.More(); aStructIter.Next())
397 {
398 if (aStructIter.Key()->ContainsFacet())
399 {
400 return Standard_True;
401 }
402 }
403 return Standard_False;
404}
405
406// =======================================================================
407// function : DisplayedStructures
408// purpose :
409// =======================================================================
410void Graphic3d_CView::DisplayedStructures (Graphic3d_MapOfStructure& theStructures) const
411{
412 for (Graphic3d_MapOfStructure::Iterator aStructIter (myStructsDisplayed); aStructIter.More(); aStructIter.Next())
413 {
414 theStructures.Add (aStructIter.Key());
415 }
416}
417
c357e426 418// =======================================================================
419// function : MinMaxValues
420// purpose :
421// =======================================================================
3fe9ce0e 422Bnd_Box Graphic3d_CView::MinMaxValues (const Standard_Boolean theToIncludeAuxiliary) const
c357e426 423{
50d06d8f 424 Bnd_Box aResult;
425
426 if (!IsDefined())
427 {
428 return aResult;
429 }
430
431 Handle(Graphic3d_Camera) aCamera = Camera();
432 Standard_Integer aWinWidth = 0;
433 Standard_Integer aWinHeight = 0;
434
435 Window()->Size (aWinWidth, aWinHeight);
436
437 for (Standard_Integer aLayer = 0; aLayer < THE_NB_DEFAULT_LAYERS; ++aLayer)
438 {
439 Graphic3d_BndBox4f aBox = ZLayerBoundingBox (THE_DEFAULT_LAYERS[aLayer],
440 aCamera,
441 aWinWidth,
442 aWinHeight,
3fe9ce0e 443 theToIncludeAuxiliary);
50d06d8f 444 combineBox (aResult, aBox);
445 }
446
447 Standard_Integer aMaxZLayer = ZLayerMax();
448 for (Standard_Integer aLayerId = Graphic3d_ZLayerId_Default; aLayerId <= aMaxZLayer; ++aLayerId)
449 {
3fe9ce0e 450 Graphic3d_BndBox4f aBox = ZLayerBoundingBox (aLayerId,
451 aCamera,
452 aWinWidth,
453 aWinHeight,
454 theToIncludeAuxiliary);
50d06d8f 455 combineBox (aResult, aBox);
456 }
457
458 return aResult;
459}
460
461// =======================================================================
462// function : ConsiderZoomPersistenceObjects
463// purpose :
464// =======================================================================
465Standard_Real Graphic3d_CView::ConsiderZoomPersistenceObjects()
466{
467 if (!IsDefined())
468 {
469 return 1.0;
470 }
471
472 Handle(Graphic3d_Camera) aCamera = Camera();
473 Standard_Integer aWinWidth = 0;
474 Standard_Integer aWinHeight = 0;
475
476 Window()->Size (aWinWidth, aWinHeight);
477
478 Standard_Real aMaxCoef = 1.0;
479 for (Standard_Integer aLayer = 0; aLayer < THE_NB_DEFAULT_LAYERS; ++aLayer)
480 {
3fe9ce0e 481 aMaxCoef = Max (aMaxCoef, considerZoomPersistenceObjects (THE_DEFAULT_LAYERS[aLayer], aCamera, aWinWidth, aWinHeight));
50d06d8f 482 }
483
484 for (Standard_Integer aLayer = Graphic3d_ZLayerId_Default; aLayer <= ZLayerMax(); ++aLayer)
c357e426 485 {
3fe9ce0e 486 aMaxCoef = Max (aMaxCoef, considerZoomPersistenceObjects (aLayer, aCamera, aWinWidth, aWinHeight));
c357e426 487 }
488
50d06d8f 489 return aMaxCoef;
c357e426 490}
491
492// =======================================================================
493// function : MinMaxValues
494// purpose :
495// =======================================================================
496Bnd_Box Graphic3d_CView::MinMaxValues (const Graphic3d_MapOfStructure& theSet,
497 const Standard_Boolean theToIgnoreInfiniteFlag) const
498{
499 Bnd_Box aResult;
500 const Standard_Integer aViewId = Identification();
97f937cc 501
502 Handle(Graphic3d_Camera) aCamera = Camera();
503 Standard_Integer aWinWidth = 0;
504 Standard_Integer aWinHeight = 0;
505 if (IsDefined())
506 {
507 Window()->Size (aWinWidth, aWinHeight);
508 }
509
c357e426 510 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (theSet); aStructIter.More(); aStructIter.Next())
511 {
512 const Handle(Graphic3d_Structure)& aStructure = aStructIter.Key();
3fe9ce0e 513 if (aStructure->IsEmpty()
514 || !aStructure->CStructure()->IsVisible (aViewId))
97f937cc 515 {
516 continue;
517 }
518
519 // "FitAll" operation ignores object with transform persistence parameter
778cd667 520 if (!aStructure->TransformPersistence().IsNull())
97f937cc 521 {
522 // Panning and 2d persistence apply changes to projection or/and its translation components.
523 // It makes them incompatible with z-fitting algorithm. Ignored by now.
778cd667 524 if (!theToIgnoreInfiniteFlag
525 || aStructure->TransformPersistence()->IsTrihedronOr2d())
97f937cc 526 {
527 continue;
528 }
529 }
530
531 Bnd_Box aBox = aStructure->MinMaxValues (theToIgnoreInfiniteFlag);
532
533 if (aBox.IsWhole() || aBox.IsVoid())
c357e426 534 {
535 continue;
536 }
97f937cc 537
778cd667 538 if (!aStructure->TransformPersistence().IsNull())
97f937cc 539 {
540 const Graphic3d_Mat4d& aProjectionMat = aCamera->ProjectionMatrix();
541 const Graphic3d_Mat4d& aWorldViewMat = aCamera->OrientationMatrix();
778cd667 542 aStructure->TransformPersistence()->Apply (aCamera, aProjectionMat, aWorldViewMat, aWinWidth, aWinHeight, aBox);
97f937cc 543 }
544
545 // To prevent float overflow at camera parameters calculation and further
546 // rendering, bounding boxes with at least one vertex coordinate out of
547 // float range are skipped by view fit algorithms
548 if (Abs (aBox.CornerMax().X()) >= ShortRealLast() ||
549 Abs (aBox.CornerMax().Y()) >= ShortRealLast() ||
550 Abs (aBox.CornerMax().Z()) >= ShortRealLast() ||
551 Abs (aBox.CornerMin().X()) >= ShortRealLast() ||
552 Abs (aBox.CornerMin().Y()) >= ShortRealLast() ||
553 Abs (aBox.CornerMin().Z()) >= ShortRealLast())
c357e426 554 {
555 continue;
556 }
557
97f937cc 558 aResult.Add (aBox);
c357e426 559 }
560 return aResult;
561}
562
563// =======================================================================
564// function : acceptDisplay
565// purpose :
566// =======================================================================
567Graphic3d_TypeOfAnswer Graphic3d_CView::acceptDisplay (const Graphic3d_TypeOfStructure theStructType) const
568{
569 switch (theStructType)
570 {
571 case Graphic3d_TOS_ALL:
572 {
573 return Graphic3d_TOA_YES; // The structure accepts any type of view
574 }
575 case Graphic3d_TOS_SHADING:
576 {
577 return myVisualization == Graphic3d_TOV_SHADING
578 ? Graphic3d_TOA_YES
579 : Graphic3d_TOA_NO;
580 }
581 case Graphic3d_TOS_WIREFRAME:
582 {
583 return myVisualization == Graphic3d_TOV_WIREFRAME
584 ? Graphic3d_TOA_YES
585 : Graphic3d_TOA_NO;
586 }
587 case Graphic3d_TOS_COMPUTED:
588 {
589 return (myVisualization == Graphic3d_TOV_SHADING || myVisualization == Graphic3d_TOV_WIREFRAME)
590 ? Graphic3d_TOA_COMPUTE
591 : Graphic3d_TOA_NO;
592 }
593 }
594 return Graphic3d_TOA_NO;
595}
596
597// =======================================================================
598// function : Compute
599// purpose :
600// =======================================================================
601void Graphic3d_CView::Compute()
602{
603 // force HLRValidation to False on all structures calculated in the view
604 const Standard_Integer aNbCompStructs = myStructsComputed.Length();
605 for (Standard_Integer aStructIter = 1; aStructIter <= aNbCompStructs; ++aStructIter)
606 {
607 myStructsComputed.Value (aStructIter)->SetHLRValidation (Standard_False);
608 }
609
610 if (!ComputedMode())
611 {
612 return;
613 }
614
615 // Change of orientation or of projection type =>
616 // Remove structures that were calculated for the previous orientation.
617 // Recalculation of new structures.
618 NCollection_Sequence<Handle(Graphic3d_Structure)> aStructsSeq;
619 for (Graphic3d_MapOfStructure::Iterator aStructIter (myStructsDisplayed); aStructIter.More(); aStructIter.Next())
620 {
621 const Graphic3d_TypeOfAnswer anAnswer = acceptDisplay (aStructIter.Key()->Visual());
622 if (anAnswer == Graphic3d_TOA_COMPUTE)
623 {
624 aStructsSeq.Append (aStructIter.Key()); // if the structure was calculated, it is recalculated
625 }
626 }
627
628 for (NCollection_Sequence<Handle(Graphic3d_Structure)>::Iterator aStructIter (aStructsSeq); aStructIter.More(); aStructIter.Next())
629 {
630 Display (aStructIter.ChangeValue(), Aspect_TOU_WAIT);
631 }
632}
633
634// =======================================================================
635// function : Clear
636// purpose :
637// =======================================================================
638void Graphic3d_CView::Clear (const Handle(Graphic3d_Structure)& theStructure,
639 const Standard_Boolean theWithDestruction)
640{
641 const Standard_Integer anIndex = IsComputed (theStructure);
642 if (anIndex != 0)
643 {
644 const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.Value (anIndex);
645 aCompStruct->GraphicClear (theWithDestruction);
646 aCompStruct->SetHLRValidation (Standard_False);
647 }
648}
649
650// =======================================================================
651// function : Connect
652// purpose :
653// =======================================================================
654void Graphic3d_CView::Connect (const Handle(Graphic3d_Structure)& theMother,
655 const Handle(Graphic3d_Structure)& theDaughter)
656{
657 Standard_Integer anIndexM = IsComputed (theMother);
658 Standard_Integer anIndexD = IsComputed (theDaughter);
659 if (anIndexM != 0
660 && anIndexD != 0)
661 {
662 const Handle(Graphic3d_Structure)& aStructM = myStructsComputed.Value (anIndexM);
663 const Handle(Graphic3d_Structure)& aStructD = myStructsComputed.Value (anIndexD);
664 aStructM->GraphicConnect (aStructD);
665 }
666}
667
668// =======================================================================
669// function : Disconnect
670// purpose :
671// =======================================================================
672void Graphic3d_CView::Disconnect (const Handle(Graphic3d_Structure)& theMother,
673 const Handle(Graphic3d_Structure)& theDaughter)
674{
675 Standard_Integer anIndexM = IsComputed (theMother);
676 Standard_Integer anIndexD = IsComputed (theDaughter);
677 if (anIndexM != 0
678 && anIndexD != 0)
679 {
680 const Handle(Graphic3d_Structure)& aStructM = myStructsComputed.Value (anIndexM);
681 const Handle(Graphic3d_Structure)& aStructD = myStructsComputed.Value (anIndexD);
682 aStructM->GraphicDisconnect (aStructD);
683 }
684}
685
686// =======================================================================
687// function : Display
688// purpose :
689// =======================================================================
690void Graphic3d_CView::Display (const Handle(Graphic3d_Structure)& theStructure)
691{
692 Display (theStructure, myStructureManager->UpdateMode());
693}
694
695// =======================================================================
696// function : Display
697// purpose :
698// =======================================================================
699void Graphic3d_CView::Display (const Handle(Graphic3d_Structure)& theStructure,
700 const Aspect_TypeOfUpdate theUpdateMode)
701{
702 if (!IsActive())
703 {
704 return;
705 }
706
707 // If Display on a structure present in the list of calculated structures while it is not
708 // or more, of calculated type =>
709 // - removes it as well as the associated old computed
710 // THis happens when hlhsr becomes again of type e non computed after SetVisual.
711 Standard_Integer anIndex = IsComputed (theStructure);
712 if (anIndex != 0
713 && theStructure->Visual() != Graphic3d_TOS_COMPUTED)
714 {
715 myStructsToCompute.Remove (anIndex);
716 myStructsComputed .Remove (anIndex);
717 anIndex = 0;
718 }
719
720 Graphic3d_TypeOfAnswer anAnswer = acceptDisplay (theStructure->Visual());
721 if (anAnswer == Graphic3d_TOA_NO)
722 {
723 return;
724 }
725
726 if (!ComputedMode())
727 {
728 anAnswer = Graphic3d_TOA_YES;
729 }
730
731 if (anAnswer == Graphic3d_TOA_YES)
732 {
733 if (!myStructsDisplayed.Add (theStructure))
734 {
735 return;
736 }
737
738 theStructure->CalculateBoundBox();
739 displayStructure (theStructure->CStructure(), theStructure->DisplayPriority());
50d06d8f 740 Update (theUpdateMode, theStructure->GetZLayer());
c357e426 741 return;
742 }
743 else if (anAnswer != Graphic3d_TOA_COMPUTE)
744 {
745 return;
746 }
747
748 if (anIndex != 0)
749 {
750 // Already computed, is COMPUTED still valid?
751 const Handle(Graphic3d_Structure)& anOldStruct = myStructsComputed.Value (anIndex);
752 if (anOldStruct->HLRValidation())
753 {
754 // Case COMPUTED valid, to be displayed
755 if (!myStructsDisplayed.Add (theStructure))
756 {
757 return;
758 }
759
760 displayStructure (anOldStruct->CStructure(), theStructure->DisplayPriority());
50d06d8f 761 Update (theUpdateMode, anOldStruct->GetZLayer());
c357e426 762 return;
763 }
764 else
765 {
766 // Case COMPUTED invalid
767 // Is there another valid representation?
768 // Find in the sequence of already calculated structures
769 // 1/ Structure having the same Owner as <AStructure>
770 // 2/ That is not <AStructure>
771 // 3/ The COMPUTED which of is valid
772 const Standard_Integer aNewIndex = HaveTheSameOwner (theStructure);
773 if (aNewIndex != 0)
774 {
775 // Case of COMPUTED invalid, WITH a valid of replacement; to be displayed
776 if (!myStructsDisplayed.Add (theStructure))
777 {
778 return;
779 }
780
781 const Handle(Graphic3d_Structure)& aNewStruct = myStructsComputed.Value (aNewIndex);
782 myStructsComputed.SetValue (anIndex, aNewStruct);
783 displayStructure (aNewStruct->CStructure(), theStructure->DisplayPriority());
50d06d8f 784 Update (theUpdateMode, aNewStruct->GetZLayer());
c357e426 785 return;
786 }
787 else
788 {
789 // Case COMPUTED invalid, WITHOUT a valid of replacement
790 // COMPUTED is removed if displayed
791 if (myStructsDisplayed.Contains (theStructure))
792 {
793 eraseStructure (anOldStruct->CStructure());
794 }
795 }
796 }
797 }
798
799 // Compute + Validation
800 Handle(Graphic3d_Structure) aStruct;
c357e426 801 if (anIndex != 0)
802 {
c357e426 803 aStruct = myStructsComputed.Value (anIndex);
1f7f5a90 804 aStruct->SetTransformation (Handle(Geom_Transformation)());
c357e426 805 if (theStructure->IsTransformed())
806 {
1f7f5a90 807 theStructure->Compute (this, theStructure->Transformation(), aStruct);
c357e426 808 }
809 else
810 {
811 theStructure->Compute (this, aStruct);
812 }
813 }
814 else
815 {
816 aStruct = theStructure->IsTransformed()
1f7f5a90 817 ? theStructure->Compute (this, theStructure->Transformation())
c357e426 818 : theStructure->Compute (this);
819 }
820
821 aStruct->SetHLRValidation (Standard_True);
822
823 // TOCOMPUTE and COMPUTED associated to sequences are added
824 myStructsToCompute.Append (theStructure);
825 myStructsComputed .Append (aStruct);
826
827 // The previous are removed if necessary
828 if (anIndex != 0)
829 {
830 myStructsToCompute.Remove (anIndex);
831 myStructsComputed .Remove (anIndex);
832 }
833
834 // Of which type will be the computed?
835 const Standard_Boolean toComputeWireframe = myVisualization == Graphic3d_TOV_WIREFRAME
836 && theStructure->ComputeVisual() != Graphic3d_TOS_SHADING;
837 const Standard_Boolean toComputeShading = myVisualization == Graphic3d_TOV_SHADING
838 && theStructure->ComputeVisual() != Graphic3d_TOS_WIREFRAME;
839 if (!toComputeShading && !toComputeWireframe)
840 {
841 anAnswer = Graphic3d_TOA_NO;
842 }
843 else
844 {
845 aStruct->SetVisual (toComputeWireframe ? Graphic3d_TOS_WIREFRAME : Graphic3d_TOS_SHADING);
846 anAnswer = acceptDisplay (aStruct->Visual());
847 }
848
849 if (theStructure->IsHighlighted())
850 {
8e5fb5ea 851 aStruct->Highlight (theStructure->HighlightStyle(), Standard_False);
c357e426 852 }
853
854 // It is displayed only if the calculated structure
855 // has a proper type corresponding to the one of the view.
856 if (anAnswer == Graphic3d_TOA_NO)
857 {
858 return;
859 }
860
861 myStructsDisplayed.Add (theStructure);
862 displayStructure (aStruct->CStructure(), theStructure->DisplayPriority());
863
50d06d8f 864 Update (theUpdateMode, aStruct->GetZLayer());
c357e426 865}
866
867// =======================================================================
868// function : Erase
869// purpose :
870// =======================================================================
871void Graphic3d_CView::Erase (const Handle(Graphic3d_Structure)& theStructure)
872{
873 Erase (theStructure, myStructureManager->UpdateMode());
874}
875
876// =======================================================================
877// function : Erase
878// purpose :
879// =======================================================================
880void Graphic3d_CView::Erase (const Handle(Graphic3d_Structure)& theStructure,
881 const Aspect_TypeOfUpdate theUpdateMode)
882{
883 if (!IsDisplayed (theStructure))
884 {
885 return;
886 }
887
888 Graphic3d_TypeOfAnswer anAnswer = acceptDisplay (theStructure->Visual());
889 if (!ComputedMode())
890 {
891 anAnswer = Graphic3d_TOA_YES;
892 }
893
894 if (anAnswer != Graphic3d_TOA_COMPUTE)
895 {
896 eraseStructure (theStructure->CStructure());
897 }
898 else if (anAnswer == Graphic3d_TOA_COMPUTE && myIsInComputedMode)
899 {
900 const Standard_Integer anIndex = IsComputed (theStructure);
901 if (anIndex != 0)
902 {
903 const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.ChangeValue (anIndex);
904 eraseStructure (aCompStruct->CStructure());
905 }
906 }
907 myStructsDisplayed.Remove (theStructure);
50d06d8f 908 Update (theUpdateMode, theStructure->GetZLayer());
c357e426 909}
910
911// =======================================================================
912// function : Highlight
913// purpose :
914// =======================================================================
8e5fb5ea 915void Graphic3d_CView::Highlight (const Handle(Graphic3d_Structure)& theStructure)
c357e426 916{
917 const Standard_Integer anIndex = IsComputed (theStructure);
918 if (anIndex != 0)
919 {
920 const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.ChangeValue (anIndex);
8e5fb5ea 921 aCompStruct->Highlight (theStructure->HighlightStyle(), Standard_False);
c357e426 922 }
923}
924
925// =======================================================================
926// function : SetTransform
927// purpose :
928// =======================================================================
929void Graphic3d_CView::SetTransform (const Handle(Graphic3d_Structure)& theStructure,
1f7f5a90 930 const Handle(Geom_Transformation)& theTrsf)
c357e426 931{
932 const Standard_Integer anIndex = IsComputed (theStructure);
933 if (anIndex != 0)
934 {
935 // Test is somewhat light !
936 // trsf is transferred only if it is :
937 // a translation
938 // a scale
1f7f5a90 939 if (!theTrsf.IsNull()
940 && (theTrsf->Form() == gp_Translation
941 || theTrsf->Form() == gp_Scale
942 || theTrsf->Form() == gp_CompoundTrsf))
c357e426 943 {
944 ReCompute (theStructure);
945 }
946 else
947 {
948 const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.ChangeValue (anIndex);
949 aCompStruct->GraphicTransform (theTrsf);
950 }
951 }
952
953 theStructure->CalculateBoundBox();
954 if (!theStructure->IsMutable()
955 && !theStructure->CStructure()->IsForHighlight
956 && !theStructure->CStructure()->IsInfinite)
957 {
958 const Graphic3d_ZLayerId aLayerId = theStructure->GetZLayer();
959 InvalidateBVHData (aLayerId);
960 }
961}
962
963// =======================================================================
964// function : UnHighlight
965// purpose :
966// =======================================================================
967void Graphic3d_CView::UnHighlight (const Handle(Graphic3d_Structure)& theStructure)
968{
969 Standard_Integer anIndex = IsComputed (theStructure);
970 if (anIndex != 0)
971 {
972 const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.ChangeValue (anIndex);
8e5fb5ea 973 aCompStruct->CStructure()->GraphicUnhighlight();
c357e426 974 }
975}
976
977// ========================================================================
978// function : IsComputed
979// purpose :
980// ========================================================================
981Standard_Boolean Graphic3d_CView::IsComputed (const Standard_Integer theStructId,
982 Handle(Graphic3d_Structure)& theComputedStruct) const
983{
984 theComputedStruct.Nullify();
985 if (!ComputedMode())
986 return Standard_False;
987
988 const Standard_Integer aNbStructs = myStructsToCompute.Length();
989 for (Standard_Integer aStructIter = 1; aStructIter <= aNbStructs; ++aStructIter)
990 {
991 if (myStructsToCompute.Value (aStructIter)->Identification() == theStructId)
992 {
993 theComputedStruct = myStructsComputed (aStructIter);
994 return Standard_True;
995 }
996 }
997 return Standard_False;
998}
999
1000// =======================================================================
1001// function : IsComputed
1002// purpose :
1003// =======================================================================
1004Standard_Integer Graphic3d_CView::IsComputed (const Handle(Graphic3d_Structure)& theStructure) const
1005{
1006 const Standard_Integer aStructId = theStructure->Identification();
1007 const Standard_Integer aNbStructs = myStructsToCompute.Length();
1008 for (Standard_Integer aStructIter = 1; aStructIter <= aNbStructs; ++aStructIter)
1009 {
1010 const Handle(Graphic3d_Structure)& aStruct = myStructsToCompute.Value (aStructIter);
1011 if (aStruct->Identification() == aStructId)
1012 {
1013 return aStructIter;
1014 }
1015 }
1016 return 0;
1017}
1018
1019// =======================================================================
1020// function : IsDisplayed
1021// purpose :
1022// =======================================================================
1023Standard_Boolean Graphic3d_CView::IsDisplayed (const Handle(Graphic3d_Structure)& theStructure) const
1024{
1025 return myStructsDisplayed.Contains (theStructure);
1026}
1027
1028// =======================================================================
1029// function : ChangePriority
1030// purpose :
1031// =======================================================================
1032void Graphic3d_CView::ChangePriority (const Handle(Graphic3d_Structure)& theStructure,
1033 const Standard_Integer /*theOldPriority*/,
1034 const Standard_Integer theNewPriority)
1035{
1036 if (!IsActive()
1037 || !IsDisplayed (theStructure))
1038 {
1039 return;
1040 }
1041
1042 if (!myIsInComputedMode)
1043 {
1044 changePriority (theStructure->CStructure(), theNewPriority);
1045 return;
1046 }
1047
1048 const Standard_Integer anIndex = IsComputed (theStructure);
1049 const Handle(Graphic3d_CStructure)& aCStruct = anIndex != 0
1050 ? myStructsComputed.Value (anIndex)->CStructure()
1051 : theStructure->CStructure();
1052
1053 changePriority (aCStruct, theNewPriority);
1054}
1055
1056// =======================================================================
1057// function : ChangeZLayer
1058// purpose :
1059// =======================================================================
1060void Graphic3d_CView::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
1061 const Graphic3d_ZLayerId theLayerId)
1062{
1063 if (!IsActive()
1064 || !IsDisplayed (theStructure))
1065 {
1066 return;
1067 }
1068
1069 if (!myIsInComputedMode)
1070 {
1071 changeZLayer (theStructure->CStructure(), theLayerId);
1072 return;
1073 }
1074
1075 const Standard_Integer anIndex = IsComputed (theStructure);
1076 Handle(Graphic3d_CStructure) aCStruct = anIndex != 0
1077 ? myStructsComputed.Value (anIndex)->CStructure()
1078 : theStructure->CStructure();
1079
1080 changeZLayer (aCStruct, theLayerId);
1081}
1082
1083// =======================================================================
1084// function : HaveTheSameOwner
1085// purpose :
1086// =======================================================================
1087Standard_Integer Graphic3d_CView::HaveTheSameOwner (const Handle(Graphic3d_Structure)& theStructure) const
1088{
1089 // Find in the sequence of already calculated structures
1090 // 1/ Structure with the same Owner as <AStructure>
1091 // 2/ Which is not <AStructure>
1092 // 3/ COMPUTED which of is valid
1093 const Standard_Integer aNbToCompStructs = myStructsToCompute.Length();
1094 for (Standard_Integer aStructIter = 1; aStructIter <= aNbToCompStructs; ++aStructIter)
1095 {
1096 const Handle(Graphic3d_Structure)& aStructToComp = myStructsToCompute.Value (aStructIter);
1097 if (aStructToComp->Owner() == theStructure->Owner()
1098 && aStructToComp->Identification() != theStructure->Identification())
1099 {
1100 const Handle(Graphic3d_Structure)& aStructComp = myStructsComputed.Value (aStructIter);
1101 if (aStructComp->HLRValidation())
1102 {
1103 return aStructIter;
1104 }
1105 }
1106 }
1107 return 0;
1108}
1109
1110// =======================================================================
1111// function : CopySettings
1112// purpose :
1113// =======================================================================
1114void Graphic3d_CView::CopySettings (const Handle(Graphic3d_CView)& theOther)
1115{
1116 ChangeRenderingParams() = theOther->RenderingParams();
c357e426 1117 SetBackground (theOther->Background());
1118 SetGradientBackground (theOther->GradientBackground());
1119 SetBackgroundImage (theOther->BackgroundImage());
1120 SetBackgroundImageStyle (theOther->BackgroundImageStyle());
1121 SetTextureEnv (theOther->TextureEnv());
1122 SetCullingEnabled (theOther->IsCullingEnabled());
1123 SetShadingModel (theOther->ShadingModel());
c357e426 1124 SetBackfacingModel (theOther->BackfacingModel());
1125 SetCamera (new Graphic3d_Camera (theOther->Camera()));
c357e426 1126 SetGLLightEnabled (theOther->IsGLLightEnabled());
1127 SetLights (theOther->Lights());
1128 SetClipPlanes (theOther->ClipPlanes());
1129}