aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/llquantize.h
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmath/llquantize.h
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llmath/llquantize.h')
-rw-r--r--linden/indra/llmath/llquantize.h122
1 files changed, 122 insertions, 0 deletions
diff --git a/linden/indra/llmath/llquantize.h b/linden/indra/llmath/llquantize.h
new file mode 100644
index 0000000..6045911
--- /dev/null
+++ b/linden/indra/llmath/llquantize.h
@@ -0,0 +1,122 @@
1/**
2 * @file llquantize.h
3 * @brief useful routines for quantizing floats to various length ints
4 * and back out again
5 *
6 * Copyright (c) 2001-2007, Linden Research, Inc.
7 *
8 * The source code in this file ("Source Code") is provided by Linden Lab
9 * to you under the terms of the GNU General Public License, version 2.0
10 * ("GPL"), unless you have obtained a separate licensing agreement
11 * ("Other License"), formally executed by you and Linden Lab. Terms of
12 * the GPL can be found in doc/GPL-license.txt in this distribution, or
13 * online at http://secondlife.com/developers/opensource/gplv2
14 *
15 * There are special exceptions to the terms and conditions of the GPL as
16 * it is applied to this Source Code. View the full text of the exception
17 * in the file doc/FLOSS-exception.txt in this software distribution, or
18 * online at http://secondlife.com/developers/opensource/flossexception
19 *
20 * By copying, modifying or distributing this software, you acknowledge
21 * that you have read and understood your obligations described above,
22 * and agree to abide by those obligations.
23 *
24 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
25 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
26 * COMPLETENESS OR PERFORMANCE.
27 */
28
29#ifndef LL_LLQUANTIZE_H
30#define LL_LLQUANTIZE_H
31
32const U16 U16MAX = 65535;
33const F32 OOU16MAX = 1.f/(F32)(U16MAX);
34
35const U8 U8MAX = 255;
36const F32 OOU8MAX = 1.f/(F32)(U8MAX);
37
38const U8 FIRSTVALIDCHAR = 54;
39const U8 MAXSTRINGVAL = U8MAX - FIRSTVALIDCHAR; //we don't allow newline or null
40
41
42inline U16 F32_to_U16(F32 val, F32 lower, F32 upper)
43{
44 val = llclamp(val, lower, upper);
45 // make sure that the value is positive and normalized to <0, 1>
46 val -= lower;
47 val /= (upper - lower);
48
49 // return the U16
50 return (U16)(llfloor(val*U16MAX));
51}
52
53inline F32 U16_to_F32(U16 ival, F32 lower, F32 upper)
54{
55 F32 val = ival*OOU16MAX;
56 F32 delta = (upper - lower);
57 val *= delta;
58 val += lower;
59
60 F32 max_error = delta*OOU16MAX;
61
62 // make sure that zero's come through as zero
63 if (fabsf(val) < max_error)
64 val = 0.f;
65
66 return val;
67}
68
69inline U8 F32_to_U8(F32 val, F32 lower, F32 upper)
70{
71 val = llclamp(val, lower, upper);
72 // make sure that the value is positive and normalized to <0, 1>
73 val -= lower;
74 val /= (upper - lower);
75
76 // return the U8
77 return (U8)(llfloor(val*U8MAX));
78}
79
80inline F32 U8_to_F32(U8 ival, F32 lower, F32 upper)
81{
82 F32 val = ival*OOU8MAX;
83 F32 delta = (upper - lower);
84 val *= delta;
85 val += lower;
86
87 F32 max_error = delta*OOU8MAX;
88
89 // make sure that zero's come through as zero
90 if (fabsf(val) < max_error)
91 val = 0.f;
92
93 return val;
94}
95
96inline U8 F32_TO_STRING(F32 val, F32 lower, F32 upper)
97{
98 val = llclamp(val, lower, upper); //[lower, upper]
99 // make sure that the value is positive and normalized to <0, 1>
100 val -= lower; //[0, upper-lower]
101 val /= (upper - lower); //[0,1]
102 val = val * MAXSTRINGVAL; //[0, MAXSTRINGVAL]
103 val = floor(val + 0.5f); //[0, MAXSTRINGVAL]
104
105 U8 stringVal = (U8)(val) + FIRSTVALIDCHAR; //[FIRSTVALIDCHAR, MAXSTRINGVAL + FIRSTVALIDCHAR]
106 return stringVal;
107}
108
109inline F32 STRING_TO_F32(U8 ival, F32 lower, F32 upper)
110{
111 // remove empty space left for NULL, newline, etc.
112 ival -= FIRSTVALIDCHAR; //[0, MAXSTRINGVAL]
113
114 F32 val = (F32)ival * (1.f / (F32)MAXSTRINGVAL); //[0, 1]
115 F32 delta = (upper - lower);
116 val *= delta; //[0, upper - lower]
117 val += lower; //[lower, upper]
118
119 return val;
120}
121
122#endif