TComposedColor.h
Go to the documentation of this file.
1 #ifndef icmm_TComposedColor_included
2 #define icmm_TComposedColor_included
3 
4 
5 // STL includes
6 #include <cstring>
7 
8 // ACF includes
9 #include <istd/CChangeNotifier.h>
10 #include <iser/ISerializable.h>
11 #include <iser/CArchiveTag.h>
12 #include <imath/IDoubleManip.h>
13 #include <imath/TVector.h>
14 #include <imath/CVarVector.h>
15 #include <icmm/icmm.h>
16 
17 
18 namespace icmm
19 {
20 
21 
25 template <int Size>
26 class TComposedColor: public imath::TVector<Size>, virtual public iser::ISerializable
27 {
28 public:
30  typedef typename BaseClass::Elements Elements;
31 
33  TComposedColor(const BaseClass& value);
34  explicit TComposedColor(const imath::CVarVector& vector);
35 
41  void GetRounded(const imath::IDoubleManip& manipulator, TComposedColor<Size>& result);
42 
48  bool IsRoundedEqual(const TComposedColor<Size>& color, const imath::IDoubleManip& manipulator) const;
49 
53  bool IsSimilar(const TComposedColor<Size>& color, double tolerance = I_BIG_EPSILON) const;
54 
59  bool IsNormalized() const;
60 
65  void Normalize();
66 
71  void GetNormalized(TComposedColor<Size>& result) const;
72 
77 
78  TComposedColor<Size> operator*(double value) const;
79  TComposedColor<Size> operator/(double value) const;
80 
82  const TComposedColor<Size>& operator=(double colorValue);
83 
88 
89  const TComposedColor<Size>& operator*=(double value);
90  const TComposedColor<Size>& operator/=(double value);
91 
92  // reimplemented (iser::ISerializable)
93  virtual bool Serialize(iser::IArchive& archive);
94 };
95 
96 
97 // inline methods
98 
99 template <int Size>
101  :BaseClass()
102 {
103 }
104 
105 
106 template <int Size>
108 : BaseClass(value),
109  iser::ISerializable(),
110  istd::IChangeable(),
111  istd::IPolymorphic()
112 {
113 }
114 
115 
116 template <int Size>
118 {
119  int commonSize = qMin(vector.GetElementsCount(), Size);
120  for (int copyIndex = 0; copyIndex < commonSize; ++copyIndex){
121  BaseClass::SetElement(copyIndex, vector[copyIndex]);
122  }
123 
124  for (int resetIndex = commonSize; resetIndex < Size; ++resetIndex){
125  BaseClass::SetElement(resetIndex, 0.0);
126  }
127 }
128 
129 
130 template <int Size>
131 inline bool TComposedColor<Size>::IsSimilar(const TComposedColor<Size>& color, double tolerance) const
132 {
133  return GetDistance(color) <= tolerance;
134 }
135 
136 
137 template <int Size>
139 {
140  for (int i = 0; i < Size; ++i){
141  double component = BaseClass::GetElement(i);
142 
143  if ((component < 0) || (component > 1)){
144  return false;
145  }
146  }
147 
148  return true;
149 }
150 
151 
152 template <int Size>
154 {
155  TComposedColor<Size> retVal;
156 
157  for (int i = 0; i < Size; ++i){
158  retVal[i] = BaseClass::GetElement(i) + color[i];
159  }
160 
161  return retVal;
162 }
163 
164 
165 template <int Size>
167 {
168  TComposedColor<Size> retVal;
169 
170  for (int i = 0; i < Size; ++i){
171  retVal[i] = BaseClass::GetElement(i) - color[i];
172  }
173 
174  return retVal;
175 }
176 
177 
178 template <int Size>
180 {
181  TComposedColor<Size> retVal;
182 
183  for (int i = 0; i < Size; ++i){
184  retVal[i] = BaseClass::GetElement(i) * color[i];
185  }
186 
187  return retVal;
188 }
189 
190 
191 template <int Size>
193 {
194  TComposedColor<Size> retVal;
195 
196  for (int i = 0; i < Size; ++i){
197  retVal[i] = BaseClass::GetElement(i) / color[i];
198  }
199 
200  return retVal;
201 }
202 
203 
204 template <int Size>
206 {
207  TComposedColor<Size> retVal;
208 
209  for (int i = 0; i < Size; ++i){
210  retVal[i] = BaseClass::GetElement(i) * value;
211  }
212 
213  return retVal;
214 }
215 
216 
217 template <int Size>
219 {
220  TComposedColor<Size> retVal;
221 
222  for (int i = 0; i < Size; ++i){
223  retVal[i] = BaseClass::GetElement(i) / value;
224  }
225 
226  return retVal;
227 }
228 
229 
230 template <int Size>
232 {
233  for (int i = 0; i < Size; ++i){
234  BaseClass::SetElement(i, color[i]);
235  }
236 
237  return *this;
238 }
239 
240 
241 template <int Size>
243 {
244  std::memset(&BaseClass::GetElementsRef()[0], 0.0, sizeof(double) * Size);
245 
246  BaseClass::SetElement(0, colorValue);
247 
248  return *this;
249 }
250 
251 
252 template <int Size>
254 {
255  BaseClass::operator+=(color);
256 
257  return *this;
258 }
259 
260 
261 template <int Size>
263 {
264  BaseClass::operator-=(color);
265 
266  return *this;
267 }
268 
269 
270 template <int Size>
272 {
273  Elements& elements = BaseClass::GetElementsRef();
274 
275  for (int index = 0; index < Size; ++index){
276  elements[index] *= color[index];
277  }
278 
279  return *this;
280 }
281 
282 
283 template <int Size>
285 {
286  Elements& elements = BaseClass::GetElementsRef();
287 
288  for (int index = 0; index < Size; ++index){
289  elements[index] /= color[index];
290  }
291 
292  return *this;
293 }
294 
295 
296 template <int Size>
298 {
299  BaseClass::operator*=(value);
300 
301  return *this;
302 }
303 
304 
305 template <int Size>
307 {
308  BaseClass::operator/=(value);
309 
310  return *this;
311 }
312 
313 
314 // public normal methods
315 
316 template <int Size>
318 {
319  for (int i = 0; i < Size; ++i){
320  result[i] = manipulator.GetRounded(BaseClass::GetElement(i));
321  }
322 }
323 
324 
325 template <int Size>
327 {
328  for (int i = 0; i < Size; ++i){
329  if (!manipulator.IsEqual(BaseClass::GetElement(i), color[i])){
330  return false;
331  }
332  }
333 
334  return true;
335 }
336 
337 
338 template <int Size>
340 {
341  Elements& elements = BaseClass::GetElementsRef();
342 
343  for (int i = 0; i < Size; ++i){
344  if (elements[i] < 0.0){
345  elements[i] = 0.0;
346  }
347 
348  if (elements[i] > 1.0){
349  elements[i] = 1.0;
350  }
351  }
352 }
353 
354 
355 template <int Size>
357 {
358  result = *this;
359 
360  result.Normalize();
361 }
362 
363 
364 // reimplemented (iser::ISerializable)
365 
366 template <int Size>
368 {
369  static iser::CArchiveTag colorComponentsTag("ColorComponents", "List of color components", iser::CArchiveTag::TT_MULTIPLE);
370  static iser::CArchiveTag componentTag("Component", "Single component", iser::CArchiveTag::TT_LEAF, &colorComponentsTag);
371 
372  istd::CChangeNotifier notifier(archive.IsStoring()? NULL: this, &GetAllChanges());
373  Q_UNUSED(notifier);
374 
375  bool retVal = true;
376 
377  int elementsCount = BaseClass::GetElementsCount();
378 
379  retVal = retVal && archive.BeginMultiTag(colorComponentsTag, componentTag, elementsCount);
380 
381  if ((!retVal) || (!archive.IsStoring() && (elementsCount != Size))){
382  return false;
383  }
384 
385  for (int i = 0; i < elementsCount; ++i){
386  retVal = retVal && archive.BeginTag(componentTag);
387  retVal = retVal && archive.Process(BaseClass::GetElementRef(i));
388  retVal = retVal && archive.EndTag(componentTag);
389  }
390 
391  retVal = retVal && archive.EndTag(colorComponentsTag);
392 
393  return retVal;
394 }
395 
396 
397 // related global functions
398 
399 template <int Size>
400 uint qHash(const TComposedColor<Size>& color, uint seed = 0)
401 {
402  quint64 retVal = seed;
403 
404  union{
405  double value;
406  quint64 raw;
407  } element;
408  element.raw = 0;
409 
410  int elementsCount = color.GetElementsCount();
411  for (int i = 0; i < elementsCount; ++i){
412  element.value = color[i];
413 
414  retVal = (retVal >> 1) ^ (element.raw + 1);
415  }
416 
417  return uint(retVal);
418 }
419 
420 
421 } // namespace icmm
422 
423 
424 #endif // !icmm_TComposedColor_included
425 
426 
double Elements[Size]
Definition: TVector.h:31
const TComposedColor< Size > & operator/=(const TComposedColor< Size > &color)
virtual bool IsStoring() const =0
Check if this archive is loading or storing.
TComposedColor< Size > operator*(const TComposedColor< Size > &color) const
TComposedColor< Size > operator+(const TComposedColor< Size > &color) const
TVector< Size, double > operator-() const
virtual ValueType GetRounded(const ValueType &value) const =0
Get the nearest value rounded used this arithmetik.
bool IsSimilar(const TComposedColor< Size > &color, double tolerance=I_BIG_EPSILON) const
Allows to compare two colors with tolerance.
int GetElementsCount() const
Get number of elements.
Definition: CVarVector.h:283
Interface for all manipulation using values represent as double.
Definition: IDoubleManip.h:20
Common class for all classes which objects can be archived or restored from archive.
Definition: ISerializable.h:23
virtual bool Process(bool &value)=0
Process primitive type.
virtual bool BeginTag(const CArchiveTag &tag)=0
Begin of archive tag.
Multiple tag containing variable number of child tags.
Definition: CArchiveTag.h:42
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
Begin of archive tag containing set of subelements of the same type.
void Normalize()
Make this color to be normalized.
Generic color implementation.
BaseClass::Elements Elements
const TComposedColor< Size > & operator=(const TComposedColor< Size > &color)
bool IsNormalized() const
Check if this color value is normalized.
Represent input/output persistence archive.
Definition: IArchive.h:30
void GetNormalized(TComposedColor< Size > &result) const
Get normalized color.
Implementation of fixed-size mathematical vector with specified type of elements. ...
Definition: TVector.h:27
static int GetElementsCount()
Get number of elements.
const TComposedColor< Size > & operator*=(const TComposedColor< Size > &color)
Help class which provides the automatic update mechanism of the model.
const TComposedColor< Size > & operator-=(const TComposedColor< Size > &color)
Leaf tag, it can contain only one primitive element.
Definition: CArchiveTag.h:48
#define NULL
Definition: istd.h:64
Simple implementation of variable-size vector.
Definition: CVarVector.h:34
imath::TVector< Size > BaseClass
static const double I_BIG_EPSILON
Definition: istd.h:26
const TComposedColor< Size > & operator+=(const TComposedColor< Size > &color)
Process tag used to group data in archive stream.
Definition: CArchiveTag.h:21
uint qHash(const CFastColor &color, uint seed=0)
virtual bool EndTag(const CArchiveTag &tag)=0
End of archive tag.
bool IsRoundedEqual(const TComposedColor< Size > &color, const imath::IDoubleManip &manipulator) const
Check if two values are equal after rounding.
TComposedColor< Size > operator/(const TComposedColor< Size > &color) const
void GetRounded(const imath::IDoubleManip &manipulator, TComposedColor< Size > &result)
Get color after components value rounding with specified precision.
virtual bool IsEqual(const ValueType &value1, const ValueType &value2) const =0
Compare two values and check, if there are equal.
virtual bool Serialize(iser::IArchive &archive)
Load or store state of this object as a archive stream.

© 2007-2017 Witold Gantzke and Kirill Lepskiy