0027860: Visualization - clean up Transformation Persistence API
[occt.git] / src / Graphic3d / Graphic3d_Group.cxx
CommitLineData
b311480e 1// Created by: NW,JPB,CAL
2// Copyright (c) 1991-1999 Matra Datavision
973c2be1 3// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
59ec40f8 16#include <Graphic3d_Group.hxx>
7fd59977 17
42cf5bc1 18#include <gp_Ax2.hxx>
b64d84be 19#include <gp_Pnt.hxx>
20#include <Graphic3d_ArrayOfPoints.hxx>
42cf5bc1 21#include <Graphic3d_ArrayOfPrimitives.hxx>
22#include <Graphic3d_AspectFillArea3d.hxx>
23#include <Graphic3d_AspectLine3d.hxx>
24#include <Graphic3d_AspectMarker3d.hxx>
25#include <Graphic3d_AspectText3d.hxx>
26#include <Graphic3d_CStructure.hxx>
42cf5bc1 27#include <Graphic3d_GroupDefinitionError.hxx>
b64d84be 28#include <Graphic3d_ShaderProgram.hxx>
42cf5bc1 29#include <Graphic3d_Structure.hxx>
19286d7c 30#include "Graphic3d_Structure.pxx"
42cf5bc1 31#include <Graphic3d_StructureManager.hxx>
b64d84be 32#include <Graphic3d_TextureMap.hxx>
42cf5bc1 33#include <Graphic3d_TransModeFlags.hxx>
b64d84be 34#include <Message.hxx>
35#include <Message_Messenger.hxx>
36#include <NCollection_String.hxx>
42cf5bc1 37#include <Standard_OutOfRange.hxx>
38#include <Standard_Type.hxx>
b64d84be 39#include <TCollection_AsciiString.hxx>
42cf5bc1 40#include <TCollection_ExtendedString.hxx>
7fd59977 41
92efcf78 42IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_Group,MMgt_TShared)
43
b64d84be 44// =======================================================================
45// function : Graphic3d_Group
46// purpose :
47// =======================================================================
48Graphic3d_Group::Graphic3d_Group (const Handle(Graphic3d_Structure)& theStruct)
59ec40f8 49: myStructure (theStruct.operator->()),
50 myIsClosed (false),
51 myContainsFacet (false)
b64d84be 52{
b64d84be 53 //
59ec40f8 54}
7fd59977 55
59ec40f8 56// =======================================================================
57// function : ~Graphic3d_Group
58// purpose :
59// =======================================================================
60Graphic3d_Group::~Graphic3d_Group()
61{
62 // tell graphics driver to clear internal resources of the group
63 Clear (Standard_False);
b64d84be 64}
65
66// =======================================================================
67// function : Clear
68// purpose :
69// =======================================================================
70void Graphic3d_Group::Clear (Standard_Boolean theUpdateStructureMgr)
71{
72 if (IsDeleted())
73 {
74 return;
75 }
76
b7cd4ba7 77 myBounds.Clear();
7fd59977 78
59ec40f8 79 if (myContainsFacet)
b64d84be 80 {
81 myStructure->GroupsWithFacet (-1);
59ec40f8 82 myContainsFacet = false;
b64d84be 83 }
7fd59977 84
b64d84be 85 // clear method could be used on Graphic3d_Structure destruction,
86 // and its structure manager could be already destroyed, in that
87 // case we don't need to update it;
88 if (theUpdateStructureMgr)
89 {
90 Update();
91 }
92}
7fd59977 93
b64d84be 94// =======================================================================
b64d84be 95// function : Remove
96// purpose :
97// =======================================================================
98void Graphic3d_Group::Remove()
7fd59977 99{
b64d84be 100 if (IsDeleted())
101 {
102 return;
103 }
7fd59977 104
59ec40f8 105 if (myContainsFacet)
b64d84be 106 {
107 myStructure->GroupsWithFacet (-1);
59ec40f8 108 myContainsFacet = false;
b64d84be 109 }
110 myStructure->Remove (this);
7fd59977 111
b64d84be 112 Update();
7fd59977 113
b7cd4ba7 114 myBounds.Clear();
b64d84be 115}
116
117// =======================================================================
118// function : IsDeleted
119// purpose :
120// =======================================================================
121Standard_Boolean Graphic3d_Group::IsDeleted() const
122{
123 return myStructure == NULL
124 || myStructure->IsDeleted();
125}
126
127// =======================================================================
b64d84be 128// function : IsEmpty
129// purpose :
130// =======================================================================
131Standard_Boolean Graphic3d_Group::IsEmpty() const
132{
133 if (IsDeleted())
134 {
135 return Standard_True;
136 }
137
9cc773b5 138 return !myStructure->IsInfinite()
139 && !myBounds.IsValid();
b64d84be 140}
141
142// =======================================================================
143// function : SetMinMaxValues
144// purpose :
145// =======================================================================
146void Graphic3d_Group::SetMinMaxValues (const Standard_Real theXMin, const Standard_Real theYMin, const Standard_Real theZMin,
147 const Standard_Real theXMax, const Standard_Real theYMax, const Standard_Real theZMax)
148{
ecc1d911 149 myBounds = Graphic3d_BndBox4f (Graphic3d_Vec4 (static_cast<Standard_ShortReal> (theXMin),
150 static_cast<Standard_ShortReal> (theYMin),
151 static_cast<Standard_ShortReal> (theZMin),
152 1.0f),
153 Graphic3d_Vec4 (static_cast<Standard_ShortReal> (theXMax),
154 static_cast<Standard_ShortReal> (theYMax),
155 static_cast<Standard_ShortReal> (theZMax),
156 1.0f));
b64d84be 157}
158
159// =======================================================================
b64d84be 160// function : Structure
161// purpose :
162// =======================================================================
163Handle(Graphic3d_Structure) Graphic3d_Group::Structure() const
164{
165 return myStructure;
166}
167
168// =======================================================================
59ec40f8 169// function : MinMaxValues
b64d84be 170// purpose :
171// =======================================================================
59ec40f8 172void Graphic3d_Group::MinMaxValues (Standard_Real& theXMin, Standard_Real& theYMin, Standard_Real& theZMin,
173 Standard_Real& theXMax, Standard_Real& theYMax, Standard_Real& theZMax) const
b64d84be 174{
175 if (IsEmpty())
176 {
177 // Empty Group
178 theXMin = theYMin = theZMin = ShortRealFirst();
179 theXMax = theYMax = theZMax = ShortRealLast();
180 }
b7cd4ba7 181 else if (myBounds.IsValid())
182 {
183 const Graphic3d_Vec4& aMinPt = myBounds.CornerMin();
184 const Graphic3d_Vec4& aMaxPt = myBounds.CornerMax();
185 theXMin = Standard_Real (aMinPt.x());
186 theYMin = Standard_Real (aMinPt.y());
187 theZMin = Standard_Real (aMinPt.z());
188 theXMax = Standard_Real (aMaxPt.x());
189 theYMax = Standard_Real (aMaxPt.y());
190 theZMax = Standard_Real (aMaxPt.z());
191 }
b64d84be 192 else
193 {
b7cd4ba7 194 // for consistency with old API
195 theXMin = theYMin = theZMin = ShortRealLast();
196 theXMax = theYMax = theZMax = ShortRealFirst();
b64d84be 197 }
198}
199
200// =======================================================================
201// function : Update
202// purpose :
203// =======================================================================
204void Graphic3d_Group::Update() const
205{
206 if (IsDeleted())
207 {
208 return;
209 }
210
c357e426 211 myStructure->StructureManager()->Update (myStructure->StructureManager()->UpdateMode());
b64d84be 212}
213
214// =======================================================================
b64d84be 215// function : IsGroupPrimitivesAspectSet
216// purpose :
217// =======================================================================
218Standard_Boolean Graphic3d_Group::IsGroupPrimitivesAspectSet (const Graphic3d_GroupAspect theAspect) const
219{
220 switch (theAspect)
221 {
b6472664 222 case Graphic3d_ASPECT_LINE: return !LineAspect().IsNull();
223 case Graphic3d_ASPECT_TEXT: return !TextAspect().IsNull();
224 case Graphic3d_ASPECT_MARKER: return !MarkerAspect().IsNull();
225 case Graphic3d_ASPECT_FILL_AREA: return !FillAreaAspect().IsNull();
b64d84be 226 default: return Standard_False;
227 }
228}
229
230// =======================================================================
231// function : GroupPrimitivesAspect
232// purpose :
233// =======================================================================
234void Graphic3d_Group::GroupPrimitivesAspect (const Handle(Graphic3d_AspectLine3d)& theAspLine,
235 const Handle(Graphic3d_AspectText3d)& theAspText,
236 const Handle(Graphic3d_AspectMarker3d)& theAspMarker,
237 const Handle(Graphic3d_AspectFillArea3d)& theAspFill) const
238{
b6472664 239 if (!theAspLine.IsNull())
b64d84be 240 {
b6472664 241 Handle(Graphic3d_AspectLine3d) aLineAspect = LineAspect();
242 if (!aLineAspect.IsNull())
243 {
244 *theAspLine.operator->() = *aLineAspect;
245 }
b64d84be 246 }
247
b6472664 248 if (!theAspText.IsNull())
249 {
250 Handle(Graphic3d_AspectText3d) aTextAspect = TextAspect();
251 if (!aTextAspect.IsNull())
252 {
253 *theAspText.operator->() = *aTextAspect;
254 }
255 }
b64d84be 256
b6472664 257 if (!theAspMarker.IsNull())
258 {
259 Handle(Graphic3d_AspectMarker3d) aMarkerAspect = MarkerAspect();
260 if (!aMarkerAspect.IsNull())
261 {
262 *theAspMarker.operator->() = *aMarkerAspect;
263 }
264 }
189f85a3 265
b6472664 266 if (!theAspFill.IsNull())
267 {
268 Handle(Graphic3d_AspectFillArea3d) aFillAspect = FillAreaAspect();
269 if (!aFillAspect.IsNull())
270 {
271 *theAspFill.operator->() = *aFillAspect;
272 }
273 }
b64d84be 274}
275
276// =======================================================================
b64d84be 277// function : AddPrimitiveArray
278// purpose :
279// =======================================================================
280void Graphic3d_Group::AddPrimitiveArray (const Handle(Graphic3d_ArrayOfPrimitives)& thePrim,
281 const Standard_Boolean theToEvalMinMax)
282{
283 if (IsDeleted()
284 || !thePrim->IsValid())
285 {
286 return;
287 }
288
871fa103 289 AddPrimitiveArray (thePrim->Type(), thePrim->Indices(), thePrim->Attributes(), thePrim->Bounds(), theToEvalMinMax);
290}
291
292// =======================================================================
293// function : AddPrimitiveArray
294// purpose :
295// =======================================================================
296void Graphic3d_Group::AddPrimitiveArray (const Graphic3d_TypeOfPrimitiveArray theType,
297 const Handle(Graphic3d_IndexBuffer)& ,
298 const Handle(Graphic3d_Buffer)& theAttribs,
299 const Handle(Graphic3d_BoundBuffer)& ,
300 const Standard_Boolean theToEvalMinMax)
301{
302 if (IsDeleted()
303 || theAttribs.IsNull())
304 {
305 return;
306 }
307
59ec40f8 308 if (!myContainsFacet
871fa103 309 && theType != Graphic3d_TOPA_POLYLINES
310 && theType != Graphic3d_TOPA_SEGMENTS
311 && theType != Graphic3d_TOPA_POINTS)
b64d84be 312 {
313 myStructure->GroupsWithFacet (1);
59ec40f8 314 myContainsFacet = true;
b64d84be 315 }
316
b64d84be 317 if (theToEvalMinMax)
318 {
871fa103 319 const Standard_Integer aNbVerts = theAttribs->NbElements;
320 for (Standard_Integer anAttribIter = 0; anAttribIter < theAttribs->NbAttributes; ++anAttribIter)
b64d84be 321 {
871fa103 322 const Graphic3d_Attribute& anAttrib = theAttribs->Attribute (anAttribIter);
323 if (anAttrib.Id != Graphic3d_TOA_POS)
324 {
325 continue;
326 }
327
328 const size_t anOffset = theAttribs->AttributeOffset (anAttribIter);
329 switch (anAttrib.DataType)
330 {
331 case Graphic3d_TOD_VEC2:
332 {
333 for (Standard_Integer aVertIter = 0; aVertIter < aNbVerts; ++aVertIter)
334 {
335 const Graphic3d_Vec2& aVert = *reinterpret_cast<const Graphic3d_Vec2* >(theAttribs->value (aVertIter) + anOffset);
b7cd4ba7 336 myBounds.Add (Graphic3d_Vec4 (aVert.x(), aVert.y(), 0.0f, 1.0f));
871fa103 337 }
338 break;
339 }
340 case Graphic3d_TOD_VEC3:
341 case Graphic3d_TOD_VEC4:
342 {
343 for (Standard_Integer aVertIter = 0; aVertIter < aNbVerts; ++aVertIter)
344 {
345 const Graphic3d_Vec3& aVert = *reinterpret_cast<const Graphic3d_Vec3* >(theAttribs->value (aVertIter) + anOffset);
b7cd4ba7 346 myBounds.Add (Graphic3d_Vec4 (aVert.x(), aVert.y(), aVert.z(), 1.0f));
871fa103 347 }
348 break;
349 }
350 default: break;
351 }
352 break;
b64d84be 353 }
354 }
355
356 Update();
357}
358
359// =======================================================================
360// function : Marker
361// purpose :
362// =======================================================================
363void Graphic3d_Group::Marker (const Graphic3d_Vertex& thePoint,
364 const Standard_Boolean theToEvalMinMax)
365{
366 Handle(Graphic3d_ArrayOfPoints) aPoints = new Graphic3d_ArrayOfPoints (1);
367 aPoints->AddVertex (thePoint.X(), thePoint.Y(), thePoint.Z());
368 AddPrimitiveArray (aPoints, theToEvalMinMax);
369}
370
371// =======================================================================
b64d84be 372// function : Text
373// purpose :
374// =======================================================================
375void Graphic3d_Group::Text (const Standard_CString /*theText*/,
376 const Graphic3d_Vertex& thePoint,
377 const Standard_Real /*theHeight*/,
378 const Quantity_PlaneAngle /*theAngle*/,
379 const Graphic3d_TextPath /*theTp*/,
380 const Graphic3d_HorizontalTextAlignment /*theHta*/,
381 const Graphic3d_VerticalTextAlignment /*theVta*/,
382 const Standard_Boolean theToEvalMinMax)
383{
384 if (IsDeleted())
385 {
386 return;
387 }
388
b64d84be 389 if (theToEvalMinMax)
390 {
391 Standard_ShortReal x, y, z;
392 thePoint.Coord (x, y, z);
b7cd4ba7 393 myStructure->CStructure()->Is2dText = Standard_True;
394 myBounds.Add (Graphic3d_Vec4 (static_cast<Standard_ShortReal> (x),
395 static_cast<Standard_ShortReal> (y),
396 static_cast<Standard_ShortReal> (z),
397 1.0f));
b64d84be 398 }
399 Update();
400}
401
402// =======================================================================
403// function : Text
404// purpose :
405// =======================================================================
406void Graphic3d_Group::Text (const Standard_CString theText,
407 const Graphic3d_Vertex& thePoint,
408 const Standard_Real theHeight,
409 const Standard_Boolean theToEvalMinMax)
410{
411 Text (theText, thePoint, theHeight, 0.0,
412 Graphic3d_TP_RIGHT, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM, theToEvalMinMax);
413}
414
415// =======================================================================
416// function : Text
417// purpose :
418// =======================================================================
419void Graphic3d_Group::Text (const TCollection_ExtendedString& theText,
420 const Graphic3d_Vertex& thePoint,
421 const Standard_Real theHeight,
422 const Quantity_PlaneAngle theAngle,
423 const Graphic3d_TextPath theTp,
424 const Graphic3d_HorizontalTextAlignment theHta,
425 const Graphic3d_VerticalTextAlignment theVta,
426 const Standard_Boolean theToEvalMinMax)
427{
fb0b0531 428 const NCollection_String aText (theText.ToExtString());
b64d84be 429 Text (aText.ToCString(), thePoint, theHeight, theAngle,
430 theTp, theHta, theVta, theToEvalMinMax);
431}
432
433// =======================================================================
434// function : Text
435// purpose :
436// =======================================================================
ce01ec26 437void Graphic3d_Group::Text (const TCollection_ExtendedString& theText,
438 const gp_Ax2& theOrientation,
439 const Standard_Real theHeight,
440 const Quantity_PlaneAngle theAngle,
441 const Graphic3d_TextPath theTP,
442 const Graphic3d_HorizontalTextAlignment theHTA,
443 const Graphic3d_VerticalTextAlignment theVTA,
3f1eb0ab 444 const Standard_Boolean theToEvalMinMax,
445 const Standard_Boolean theHasOwnAnchor)
ce01ec26 446{
fb0b0531 447 const NCollection_String aText (theText.ToExtString());
ce01ec26 448 Text (aText.ToCString(),
449 theOrientation,
450 theHeight,
451 theAngle,
452 theTP,
453 theHTA,
454 theVTA,
3f1eb0ab 455 theToEvalMinMax,
456 theHasOwnAnchor);
ce01ec26 457}
458
459// =======================================================================
460// function : Text
461// purpose :
462// =======================================================================
463void Graphic3d_Group::Text (const Standard_CString /*theText*/,
464 const gp_Ax2& theOrientation,
465 const Standard_Real /*theHeight*/,
466 const Quantity_PlaneAngle /*theAngle*/,
467 const Graphic3d_TextPath /*theTp*/,
468 const Graphic3d_HorizontalTextAlignment /*theHta*/,
469 const Graphic3d_VerticalTextAlignment /*theVta*/,
3f1eb0ab 470 const Standard_Boolean theToEvalMinMax,
471 const Standard_Boolean /*theHasOwnAnchor*/)
ce01ec26 472{
473 if (IsDeleted())
474 {
475 return;
476 }
477
478 if (theToEvalMinMax)
479 {
480 myStructure->CStructure()->Is2dText = Standard_False;
481 myBounds.Add (Graphic3d_Vec4 (static_cast<Standard_ShortReal> (theOrientation.Location().X()),
482 static_cast<Standard_ShortReal> (theOrientation.Location().Y()),
483 static_cast<Standard_ShortReal> (theOrientation.Location().Z()),
484 1.0f));
485 }
486 Update();
487}
488
489// =======================================================================
490// function : Text
491// purpose :
492// =======================================================================
b64d84be 493void Graphic3d_Group::Text (const TCollection_ExtendedString& theText,
494 const Graphic3d_Vertex& thePoint,
495 const Standard_Real theHeight,
496 const Standard_Boolean theToEvalMinMax)
497{
fb0b0531 498 const NCollection_String aText (theText.ToExtString());
b64d84be 499 Text (aText.ToCString(), thePoint, theHeight, 0.0,
500 Graphic3d_TP_RIGHT, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM, theToEvalMinMax);
7fd59977 501}