0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / Graphic3d / Graphic3d_CubeMapOrder.hxx
1 // Author: Ilya Khramov
2 // Copyright (c) 2019 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _Graphic3d_CubeMapOrder_HeaderFile
16 #define _Graphic3d_CubeMapOrder_HeaderFile
17
18 #include <Graphic3d_CubeMapSide.hxx>
19 #include <Standard_Macro.hxx>
20
21 class Graphic3d_ValidatedCubeMapOrder;
22
23 //! Graphic3d_CubeMapOrder maps sides of cubemap on tiles in packed cubemap image
24 //! to support different tiles order in such images.
25 //! Also it can be considered as permutation of numbers from 0 to 5.
26 //! It stores permutation in one integer as convolution.
27 class Graphic3d_CubeMapOrder
28 {
29
30 public:
31
32   //! Default constructor.
33   //! Creates empty order with zero convolution.
34   Standard_EXPORT Graphic3d_CubeMapOrder();
35
36   //! Initializes order with values.
37   Standard_EXPORT Graphic3d_CubeMapOrder (unsigned char thePosXLocation,
38                                           unsigned char theNegXLocation,
39                                           unsigned char thePosYLocation,
40                                           unsigned char theNegYLocation,
41                                           unsigned char thePosZLocation,
42                                           unsigned char theNegZLocation);
43
44   //! Creates Graphic3d_CubeMapOrder using Graphic3d_ValidatedCubeMapOrder.
45   Standard_EXPORT Graphic3d_CubeMapOrder (const Graphic3d_ValidatedCubeMapOrder theOrder);
46
47   //! Alias of 'operator='.
48   Standard_EXPORT Graphic3d_CubeMapOrder& Set (const Graphic3d_CubeMapOrder& theOrder);
49
50   //! Checks whether order is valid and returns object containing it.
51   //! If order is invalid then exception will be thrown.
52   //! This method is only way to create Graphic3d_ValidatedCubeMapOrder except copy constructor.
53   Standard_EXPORT Graphic3d_ValidatedCubeMapOrder Validated() const;
54
55 public:
56
57   //! Sets number of tile in packed cubemap image according passed cubemap side.
58   Standard_EXPORT Graphic3d_CubeMapOrder& Set (Graphic3d_CubeMapSide theCubeMapSide, unsigned char theValue);
59
60   //! Sets default order (just from 0 to 5)
61   Standard_EXPORT Graphic3d_CubeMapOrder& SetDefault();
62
63   //! Applies another cubemap order as permutation for the current one.
64   Standard_EXPORT Graphic3d_CubeMapOrder& Permute (Graphic3d_ValidatedCubeMapOrder anOrder);
65
66   //! Returns permuted by other cubemap order copy of current one. 
67   Standard_EXPORT Graphic3d_CubeMapOrder Permuted (Graphic3d_ValidatedCubeMapOrder anOrder) const;
68
69   //! Swaps values of two cubemap sides.
70   Standard_EXPORT Graphic3d_CubeMapOrder& Swap (Graphic3d_CubeMapSide theFirstSide,
71                                                 Graphic3d_CubeMapSide theSecondSide);
72
73   //! Returns copy of current order with swapped values of two cubemap sides. 
74   Standard_EXPORT Graphic3d_CubeMapOrder Swapped (Graphic3d_CubeMapSide theFirstSide,
75                                                   Graphic3d_CubeMapSide theSecondSide) const;
76
77   //! Returns value of passed cubemap side.
78   Standard_EXPORT unsigned char Get (Graphic3d_CubeMapSide theCubeMapSide) const;
79
80   //! Alias of 'Get'.
81   Standard_EXPORT unsigned char operator[] (Graphic3d_CubeMapSide theCubeMapSide) const;
82
83   //! Makes order empty.
84   Standard_EXPORT Graphic3d_CubeMapOrder& Clear();
85
86   //! Checks whether order is empty.
87   Standard_EXPORT bool IsEmpty() const;
88
89   //! Checks whether order has repetitions.
90   Standard_EXPORT bool HasRepetitions() const;
91
92   //! Checks whether attempts to assign index greater than 5 to any side happed.
93   Standard_EXPORT bool HasOverflows() const;
94
95   //! Checks whether order is valid.
96   //! Order is valid when it doesn't have repetitions
97   //! and there were not attempts to assign indexes greater than 5.
98   Standard_EXPORT bool IsValid() const;
99
100 public:
101
102   //! Returns default order in protector container class.
103   //! It is guaranteed to be valid.
104   Standard_EXPORT static const Graphic3d_ValidatedCubeMapOrder& Default();
105
106 private:
107
108   //! Alias of 'Get' with other parameter's type for more handful iteration.
109   unsigned char get (unsigned char theCubeMapSide) const;
110
111   //! Alias of 'set' with other parameter's type for more handful iteration and applying permutations.
112   void set (unsigned char theCubeMapSide, unsigned char theValue);
113
114   //! 'Set' without overflow's checking.
115   void set (Graphic3d_CubeMapSide theCubeMapSide, unsigned char theValue);
116
117 private:
118
119   unsigned int myConvolution;  //!< Contains all values of permutation as power convolution
120   bool         myHasOverflows; //!< Indicates if there are attempts to assign index greater than 5
121 };
122
123 //! Graphic3d_ValidatedCubeMapOrder contains completely valid order object.
124 //! The only way to create this class except copy constructor is 'Validated' method of Graphic3d_CubeMapOrder.
125 //! This class can initialize Graphic3d_CubeMapOrder.
126 //! It is supposed to be used in case of necessity of completely valid order (in function argument as example).
127 //! It helps to automate order's valid checks.
128 class Graphic3d_ValidatedCubeMapOrder
129 {
130
131 public:
132
133   friend class Graphic3d_CubeMapOrder;
134
135   //! Allows skip access to 'Order' field and work directly.
136   const Graphic3d_CubeMapOrder* operator->() const
137   {
138     return &Order;
139   }
140
141   //! Copy constructor.
142   Graphic3d_ValidatedCubeMapOrder (const Graphic3d_ValidatedCubeMapOrder& theOther)
143   : Order (theOther.Order) {}
144
145 public:
146
147   const Graphic3d_CubeMapOrder Order; //!< Completely valid order
148
149 private:
150
151   //! Only Graphic3d_CubeMapOrder can generate Graphic3d_ValidatedCubeMapOrder in 'Validated' method.
152   Graphic3d_ValidatedCubeMapOrder(const Graphic3d_CubeMapOrder theOrder)
153   : Order(theOrder) {}
154
155   //! Deleted 'operator='
156   Graphic3d_ValidatedCubeMapOrder& operator= (const Graphic3d_ValidatedCubeMapOrder&);
157
158 };
159
160 #endif // _Graphic3d_CubeMapOrder_HeaderFile