Carna  Version 3.3.2
HUV.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Leonid Kostrykin
3  *
4  */
5 
6 #ifndef HUV_H_6014714286
7 #define HUV_H_6014714286
8 
9 #include <Carna/Carna.h>
11 
16 namespace Carna
17 {
18 
19 namespace base
20 {
21 
22 
23 
24 // ----------------------------------------------------------------------------------
25 // HUV
26 // ----------------------------------------------------------------------------------
27 
34 struct HUV
35 {
36 
40  signed short value;
41 
45  operator signed short() const;
46 
50  HUV() = default;
51 
56  explicit HUV( signed int value, bool absolute=false );
57 
62  explicit HUV( float intensity, bool absolute=false );
63 
67  template< typename T >
68  static HUV abs( T value );
69 
73  template< typename T >
74  static HUV rel( T value );
75 
79  float absIntensity() const;
80 
84  float relIntensity() const;
85 
86 }; // HUV
87 
88 
89 inline HUV::operator signed short() const
90 {
91  return value;
92 }
93 
94 
95 inline HUV::HUV( signed int value, bool absolute )
96  : value( value )
97 {
98  if( absolute )
99  {
100  if( this->value < -1024 ) this->value = -1024;
101  if( this->value > 3071 ) this->value = 3071;
102  }
103 }
104 
105 
106 inline HUV::HUV( float intensity, bool absolute )
107 {
108  const static float huvMax = 3071;
109  if( absolute )
110  {
111  if( intensity < 0 ) intensity = 0;
112  if( intensity > 1 ) intensity = 1;
113  }
114  float huvFloat = intensity * 4095.f + 0.5;
115  if( absolute ) huvFloat -= 1024;
116  if( huvFloat > huvMax ) huvFloat = huvMax;
117  value = static_cast< signed short >( huvFloat );
118 }
119 
120 
121 inline float HUV::absIntensity() const
122 {
123  return ( value + 1024 ) / 4095.f;
124 }
125 
126 
127 inline float HUV::relIntensity() const
128 {
129  return value / 4095.f;
130 }
131 
132 
133 template< typename T >
135 {
136  return HUV( value, true );
137 }
138 
139 
140 template< typename T >
142 {
143  return HUV( value, false );
144 }
145 
146 
147 
148 } // namespace Carna :: base
149 
150 } // namespace Carna
151 
152 #endif // HUV_H_6014714286
static HUV rel(T value)
Wraps a relative HU value.
Definition: HUV.h:141
HUV()=default
Constructor.
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
signed short value
Holds the HU value.
Definition: HUV.h:40
Represents values in .
Definition: HUV.h:34
float absIntensity() const
Returns the corresponding absolute intensity.
Definition: HUV.h:121
float relIntensity() const
Returns the corresponding relative intensity.
Definition: HUV.h:127
static HUV abs(T value)
Wraps an absolute HU value.
Definition: HUV.h:134