0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / Graphic3d / Graphic3d_CubeMapOrder.cxx
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 #include <Graphic3d_CubeMapOrder.hxx>
16
17 #include <Standard_Failure.hxx>
18
19 #include <bitset>
20
21 // =======================================================================
22 // function : Graphic3d_CubeMapOrder
23 // purpose  :
24 // =======================================================================
25 Graphic3d_CubeMapOrder::Graphic3d_CubeMapOrder()
26   :
27   myConvolution  (0),
28   myHasOverflows (false)
29 {}
30
31 // =======================================================================
32 // function : Graphic3d_CubeMapOrder
33 // purpose  :
34 // =======================================================================
35 Graphic3d_CubeMapOrder::Graphic3d_CubeMapOrder (unsigned char thePosXLocation,
36                                                 unsigned char theNegXLocation,
37                                                 unsigned char thePosYLocation,
38                                                 unsigned char theNegYLocation,
39                                                 unsigned char thePosZLocation,
40                                                 unsigned char theNegZLocation)
41   :
42   myConvolution  (0),
43   myHasOverflows (false)
44 {
45   Set (Graphic3d_CMS_POS_X, thePosXLocation);
46   Set (Graphic3d_CMS_NEG_X, theNegXLocation);
47   Set (Graphic3d_CMS_POS_Y, thePosYLocation);
48   Set (Graphic3d_CMS_NEG_Y, theNegYLocation);
49   Set (Graphic3d_CMS_POS_Z, thePosZLocation);
50   Set (Graphic3d_CMS_NEG_Z, theNegZLocation);
51 }
52
53 // =======================================================================
54 // function : Graphic3d_CubeMapOrder
55 // purpose  :
56 // =======================================================================
57 Graphic3d_CubeMapOrder::Graphic3d_CubeMapOrder (const Graphic3d_ValidatedCubeMapOrder theOrder)
58   :
59   myConvolution  (theOrder.Order.myConvolution),
60   myHasOverflows (theOrder.Order.myHasOverflows)
61 {}
62
63 // =======================================================================
64 // function : Set
65 // purpose  :
66 // =======================================================================
67 Graphic3d_CubeMapOrder& Graphic3d_CubeMapOrder::Set (const Graphic3d_CubeMapOrder& theOrder)
68 {
69   myConvolution = theOrder.myConvolution;
70   myHasOverflows = theOrder.myHasOverflows;
71   return *this;
72 }
73
74 // =======================================================================
75 // function : operator=
76 // purpose  :
77 // =======================================================================
78 Graphic3d_ValidatedCubeMapOrder Graphic3d_CubeMapOrder::Validated() const
79 {
80   if (!IsValid())
81   {
82     throw Standard_Failure("Try of Graphic3d_ValidatedCubeMapOrder creation using invalid Graphic3d_CubeMapOrder");
83   }
84
85   return *this;
86 }
87
88 // =======================================================================
89 // function : Set
90 // purpose  :
91 // =======================================================================
92 Graphic3d_CubeMapOrder& Graphic3d_CubeMapOrder::Set (Graphic3d_CubeMapSide theCubeMapSide, unsigned char theValue)
93 {
94   if (theValue > 5)
95   {
96     myHasOverflows = true;
97     return *this;
98   }
99   set (theCubeMapSide, theValue);
100   return *this;
101 }
102
103 // =======================================================================
104 // function : Get
105 // purpose  :
106 // =======================================================================
107 unsigned char Graphic3d_CubeMapOrder::Get (Graphic3d_CubeMapSide theCubeMapSide) const
108 {
109   return get (static_cast<unsigned char> (theCubeMapSide));
110 }
111
112 // =======================================================================
113 // function : operator[]
114 // purpose  :
115 // =======================================================================
116 unsigned char Graphic3d_CubeMapOrder::operator[] (Graphic3d_CubeMapSide theCubeMapSide) const
117 {
118   return Get (theCubeMapSide);
119 }
120
121 // =======================================================================
122 // function : SetDefault
123 // purpose  :
124 // =======================================================================
125 Graphic3d_CubeMapOrder& Graphic3d_CubeMapOrder::SetDefault()
126 {
127   for (unsigned char i = 0; i < 6; ++i)
128   {
129     set (Graphic3d_CubeMapSide (i), i);
130   }
131   return *this;
132 }
133
134 // =======================================================================
135 // function : Permute
136 // purpose  :
137 // =======================================================================
138 Graphic3d_CubeMapOrder& Graphic3d_CubeMapOrder::Permute (Graphic3d_ValidatedCubeMapOrder thePermutation)
139 {
140   for (unsigned char i = 0; i < 6; ++i)
141   {
142     set (i, thePermutation->get (get (i)));
143   }
144
145   return *this;
146 }
147
148 // =======================================================================
149 // function : Permuted
150 // purpose  :
151 // =======================================================================
152 Graphic3d_CubeMapOrder Graphic3d_CubeMapOrder::Permuted (Graphic3d_ValidatedCubeMapOrder thePermutation) const
153 {
154   Graphic3d_CubeMapOrder anOrder = *this;
155   anOrder.Permute (thePermutation);
156   return anOrder;
157 }
158
159 // =======================================================================
160 // function : Swap
161 // purpose  :
162 // =======================================================================
163 Graphic3d_CubeMapOrder& Graphic3d_CubeMapOrder::Swap (Graphic3d_CubeMapSide theFirstSide,
164                                                       Graphic3d_CubeMapSide theSecondSide)
165 {
166   unsigned char aTmp = Get (theFirstSide);
167   set (theFirstSide, Get(theSecondSide));
168   set (theSecondSide, aTmp);
169   return *this;
170 }
171
172 // =======================================================================
173 // function : Swapped
174 // purpose  :
175 // =======================================================================
176 Graphic3d_CubeMapOrder Graphic3d_CubeMapOrder::Swapped (Graphic3d_CubeMapSide theFirstSide,
177                                                         Graphic3d_CubeMapSide theSecondSide) const
178 {
179   Graphic3d_CubeMapOrder anOrder = *this;
180   anOrder.Swap (theFirstSide, theSecondSide);
181   return anOrder;
182 }
183
184 // =======================================================================
185 // function : Clear
186 // purpose  :
187 // =======================================================================
188 Graphic3d_CubeMapOrder& Graphic3d_CubeMapOrder::Clear()
189 {
190   myConvolution = 0;
191   myHasOverflows = false;
192   return *this;
193 }
194
195 // =======================================================================
196 // function : IsEmpty
197 // purpose  :
198 // =======================================================================
199 bool Graphic3d_CubeMapOrder::IsEmpty() const
200 {
201   return myConvolution == 0;
202 }
203
204 // =======================================================================
205 // function : HasRepetitions
206 // purpose  :
207 // =======================================================================
208 bool Graphic3d_CubeMapOrder::HasRepetitions() const
209 {
210   std::bitset<6> aBitSet;
211   for (unsigned char i = 0; i < 6; ++i)
212   {
213     std::bitset<6>::reference aFlag = aBitSet[get (i)];
214     if (aFlag)
215     {
216       return true;
217     }
218     aFlag = true;
219   }
220   return false;
221 }
222
223 // =======================================================================
224 // function : HasOverflows
225 // purpose  :
226 // =======================================================================
227 bool Graphic3d_CubeMapOrder::HasOverflows() const
228 {
229   return myHasOverflows;
230 }
231
232 // =======================================================================
233 // function : IsValid
234 // purpose  :
235 // =======================================================================
236 bool Graphic3d_CubeMapOrder::IsValid() const
237 {
238   return !HasRepetitions() && !HasOverflows();
239 }
240
241 // =======================================================================
242 // function : get
243 // purpose  :
244 // =======================================================================
245 unsigned char Graphic3d_CubeMapOrder::get (unsigned char theCubeMapSide) const
246 {
247   return (myConvolution / (1 << (theCubeMapSide * 3))) % (1 << 3);
248 }
249
250 // =======================================================================
251 // function : set
252 // purpose  :
253 // =======================================================================
254 void Graphic3d_CubeMapOrder::set (unsigned char theCubeMapSide, unsigned char theValue)
255 {
256   unsigned int aValuePlace = 1 << (theCubeMapSide * 3);
257   myConvolution -= aValuePlace * get (theCubeMapSide);
258   myConvolution += aValuePlace * theValue;
259 }
260
261 // =======================================================================
262 // function : set
263 // purpose  :
264 // =======================================================================
265 void Graphic3d_CubeMapOrder::set (Graphic3d_CubeMapSide theCubeMapSide, unsigned char theValue)
266 {
267   set (static_cast<unsigned char> (theCubeMapSide), theValue);
268 }
269
270 // =======================================================================
271 // function : Default
272 // purpose  :
273 // =======================================================================
274 const Graphic3d_ValidatedCubeMapOrder& Graphic3d_CubeMapOrder::Default()
275 {
276   static const Graphic3d_ValidatedCubeMapOrder aCubeMapOrder = Graphic3d_CubeMapOrder().SetDefault().Validated();
277   return aCubeMapOrder;
278 }