UnityMol  0.9.6-875
UnityMol viewer / In developement
EncodeFloatToColor.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 
4 
5 
6 public class EncodeFloatToColor: MonoBehaviour {
7 
8 
9  //Simplified float to Color4 ranged from -500 to 500 / Precision lost but way faster
10 
11  // Encoding/decoding [0..1) floats into 8 bit/channel RGBA. Note that 1.0 will not be encoded properly.
12  public static Vector4 Frac(Vector4 vector_a)
13  {
14  vector_a.x = (float)((double)vector_a.x - System.Math.Truncate((double)vector_a.x));
15  vector_a.y = (float)((double)vector_a.y - System.Math.Truncate((double)vector_a.y));
16  vector_a.z = (float)((double)vector_a.z - System.Math.Truncate((double)vector_a.z));
17  vector_a.w = (float)((double)vector_a.w - System.Math.Truncate((double)vector_a.w));
18 
19  return vector_a;
20  }
21  public static Vector4 EncodeFloatRGBA( float v )
22  {
23 
24  if(v<-500f||v>500f){
25  Debug.LogError("Cannot encode float < -500 or > 500");
26  return Vector4.zero;
27  }
28 
29  v /= 1000.0f;
30  v = v + 1.0f;
31  Vector4 kEncodeMul = new Vector4(1.0f, 255.0f, 65535.0f, 16777215.0f);
32  float kEncodeBit = 1.0f / 255.0f;
33  Vector4 enc = kEncodeMul * v;
34  enc = Frac(enc);
35  enc -= new Vector4(enc.y, enc.z, enc.w, enc.w) * kEncodeBit;
36  return enc;
37  }
38 
39 
40 
41  //IEEE754 encoding 32 bits float to 32 bits Color4
42  public static string IntToBinaryString(float number, bool fraction, int padding)
43  {
44  string s;
45  float number_float;
46  float quotient_float;
47  int number_int;
48  int quotient;
49  int reminder;
50  s = "";
51 
52  if (fraction)
53  {
54  int j = 1;
55  number_float = number;
56  while (j<30)
57  {
58  quotient_float = number_float * 2.0f;
59  if (quotient_float>=1.0f)
60  {
61  s += '1';
62  quotient_float -= 1.0f;
63  } else
64  {
65  s += '0';
66  }
67  number_float = quotient_float;
68  j++;
69  if (quotient_float==0.0f)
70  {
71  j = 100;
72  s += "0";
73  }
74  }
75  } else
76  {
77  number_int = (int)number;
78  while (0<number_int)
79  {
80  quotient = (int)Mathf.Floor(number_int / 2);
81  reminder = (number_int-(quotient*2));
82  number_int = quotient;
83  if (reminder==0)
84  {
85  s = '0' + s;
86  } else
87  {
88  s = '1' + s;
89  }
90  if (number_int==0)
91  {
92  number_int = -100;
93  }
94  }
95  if (padding>0)
96  {
97  s = "0000000000000000000000000000000000000000000000" + s;
98  s = s.Substring(s.Length-padding);
99  }
100  }
101  return s;
102  }
103  public static string FloatToBinaryString(float f)
104  {
105  string s;
106  char c;
107  char[] ca;
108  int i;
109  string si;
110  string sf;
111  string ss;
112 
113  if (f==0.0f)
114  {
115  return "00000000000000000000000000000000";
116  }
117  if (f==-0.0f)
118  {
119  return "10000000000000000000000000000000";
120  }
121  if (float.IsPositiveInfinity(f))
122  {
123  return "01111111100000000000000000000000";
124  }
125  if (float.IsNegativeInfinity(f))
126  {
127  return "11111111100000000000000000000000";
128  }
129  if (float.IsNaN(f))
130  {
131  return "11111111110000000000000000000000";
132  }
133 
134  bool is_significant;
135 
136  int exponent = 0;
137 
138  if (Mathf.Sign(f)>=0)
139  {
140  ss = "0";
141  } else
142  {
143  ss = "1";
144  }
145 
146  si = "";
147  sf = "0.";
148  s = f.ToString("0.#############################");
149  int l = s.Length;
150  is_significant = true;
151  exponent = 0;
152  ca = s.ToCharArray();
153  int izero = -1;
154 
155  for (i=0; i<l; i++)
156  {
157  c = ca[i];
158 
159  if (c=='.')
160  {
161  izero = i;
162  si += ".0";
163  }
164  if ((c=='0') || (c=='1') || (c=='2') || (c=='3') || (c=='4') || (c=='5') || (c=='6') || (c=='7') || (c=='8') || (c=='9'))
165  {
166  is_significant = (izero<0);
167  if (is_significant)
168  {
169  si += c;
170  } else
171  {
172  sf += c;
173  }
174  }
175  }
176  int biased_exponent;
177  if (si=="")
178  {
179  si = "0.0";
180  }
181  string si2bin = IntToBinaryString(float.Parse(si), false, 0);
182  string sf2bin = IntToBinaryString(float.Parse(sf), true, 0);
183 
184  if (si2bin=="")
185  {
186  si2bin = "0.0";
187  }
188  if (si2bin=="0.0")
189  {
190  exponent = -(sf2bin.IndexOf("1")+1);
191  } else
192  {
193  exponent = si2bin.Length-1;
194  }
195 
196  int last_index = 0;
197  string sbe2bin;
198  biased_exponent = 127 + exponent;
199  sbe2bin = IntToBinaryString(biased_exponent, false, 0);
200  sbe2bin = "00000000" + sbe2bin;
201  sbe2bin = sbe2bin.Substring(sbe2bin.Length-8, 8);
202  string stotal = ss + sbe2bin;
203  if (si2bin=="0.0")
204  {
205  last_index = sf2bin.LastIndexOf('1');
206  stotal += sf2bin.Substring(-exponent);
207  stotal += "0000000000000000000000000000000000000000000000";
208  if (last_index>0)
209  {
210  stotal = stotal.Substring(0, 31) + '1';
211  } else
212  {
213  stotal = stotal.Substring(0, 32);
214  }
215  } else
216  {
217  stotal += si2bin.Substring(1) + sf2bin;
218  if (stotal.Length>32)
219  {
220  stotal = stotal.Substring(0, 31) + '1';
221  } else
222  {
223  stotal += "0000000000000000000000000000000000000000000000";
224  stotal = stotal.Substring(0, 32);
225  }
226  }
227  //Debug.Log("INPUT=" + f.ToString() + " SignBin=" + ss + " sbe2bin=" + sbe2bin + " Exponent=" + exponent + " Biased exponent=" + biased_exponent + " si=" + si + " si2bin=" + si2bin + " sf=" + sf + " sf2bin=" + sf2bin + " stotal=" + stotal);
228  return stotal;
229  }
230  public static Color BinaryStringToColor(string b)
231  {
232  Color cl = Color.black;
233  char ch;
234  int len = b.Length-1;
235  float ic = 0.0f;
236  int ii = 0;
237  int ib = 1;
238 
239  if (len!=31)
240  {
241  return cl;
242  }
243  for (int i=len; 0<=i; i--)
244  {
245  ch = b[i];
246  if (ch=='1')
247  {
248  ic += Mathf.Pow(2.0f, ii);
249  }
250  ii++;
251  if ((ib==1) && (ii==8))
252  {
253  // alpha
254  cl.a = Mathf.Floor(ic) / 255;
255  ii = 0;
256  ic = 0.0f;
257  ib++;
258  } else
259  if ((ib==2) && (ii==8))
260  {
261  // blue
262  cl.b = Mathf.Floor(ic) / 255;
263  ii = 0;
264  ic = 0.0f;
265  ib++;
266  } else
267  if ((ib==3) && (ii==8))
268  {
269  // green
270  cl.g = Mathf.Floor(ic) / 255;
271  ii = 0;
272  ic = 0.0f;
273  ib++;
274  } else
275  if ((ib==4) && (ii==8))
276  {
277  // red
278  cl.r = Mathf.Floor(ic) / 255;
279  ii = 0;
280  ic = 0.0f;
281  ib++;
282  }
283  }
284  return cl;
285  }
286  public static Color FloatToColor(float f)
287  {
289  }
290 
291 }
static Vector4 Frac(Vector4 vector_a)
static string IntToBinaryString(float number, bool fraction, int padding)
static Color BinaryStringToColor(string b)
static Vector4 EncodeFloatRGBA(float v)
static Color FloatToColor(float f)
static string FloatToBinaryString(float f)