0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / Graphic3d / Graphic3d_BufferRange.hxx
1 // Copyright (c) 2014 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 #ifndef _Graphic3d_BufferRange_HeaderFile
15 #define _Graphic3d_BufferRange_HeaderFile
16
17 #include <Graphic3d_Vec.hxx>
18 #include <Standard_Integer.hxx>
19
20 //! Range of values defined as Start + Length pair.
21 struct Graphic3d_BufferRange
22 {
23   Standard_Integer Start;  //!< first element within the range
24   Standard_Integer Length; //!< number of elements within the range
25
26   //! Empty constructor.
27   Graphic3d_BufferRange() : Start (0), Length (0) {}
28
29   //! Constructor.
30   Graphic3d_BufferRange (Standard_Integer theStart, Standard_Integer theLength) : Start (theStart), Length (theLength) {}
31
32   //! Return TRUE if range is empty.
33   Standard_Boolean IsEmpty() const { return Length == 0; }
34
35   //! Return the Upper element within the range
36   Standard_Integer Upper() const { return Start + Length - 1; }
37
38   //! Clear the range.
39   void Clear()
40   {
41     Start  = 0;
42     Length = 0;
43   }
44
45   //! Add another range to this one.
46   void Unite (const Graphic3d_BufferRange& theRange)
47   {
48     if (IsEmpty())
49     {
50       *this = theRange;
51       return;
52     }
53     else if (theRange.IsEmpty())
54     {
55       return;
56     }
57
58     const Standard_Integer aStart = Min (Start,   theRange.Start);
59     const Standard_Integer aLast  = Max (Upper(), theRange.Upper());
60     Start  = aStart;
61     Length = aLast - aStart + 1;
62   }
63 };
64
65 #endif // _Graphic3d_BufferRange_HeaderFile