PageRenderTime 89ms CodeModel.GetById 76ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/llquantize.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 152 lines | 82 code | 32 blank | 38 comment | 2 complexity | 3b2d0b443cc42d936cc46d432859fb0a MD5 | raw file
  1/** 
  2 * @file llquantize.h
  3 * @brief useful routines for quantizing floats to various length ints
  4 * and back out again
  5 *
  6 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#ifndef LL_LLQUANTIZE_H
 29#define LL_LLQUANTIZE_H
 30
 31const U16 U16MAX = 65535;
 32LL_ALIGN_16( const F32 F_U16MAX_4A[4] ) = { 65535.f, 65535.f, 65535.f, 65535.f };
 33
 34const F32 OOU16MAX = 1.f/(F32)(U16MAX);
 35LL_ALIGN_16( const F32 F_OOU16MAX_4A[4] ) = { OOU16MAX, OOU16MAX, OOU16MAX, OOU16MAX };
 36
 37const U8 U8MAX = 255;
 38LL_ALIGN_16( const F32 F_U8MAX_4A[4] ) = { 255.f, 255.f, 255.f, 255.f };
 39
 40const F32 OOU8MAX = 1.f/(F32)(U8MAX);
 41LL_ALIGN_16( const F32 F_OOU8MAX_4A[4] ) = { OOU8MAX, OOU8MAX, OOU8MAX, OOU8MAX };
 42
 43const U8 FIRSTVALIDCHAR = 54;
 44const U8 MAXSTRINGVAL = U8MAX - FIRSTVALIDCHAR; //we don't allow newline or null 
 45
 46
 47inline U16 F32_to_U16_ROUND(F32 val, F32 lower, F32 upper)
 48{
 49	val = llclamp(val, lower, upper);
 50	// make sure that the value is positive and normalized to <0, 1>
 51	val -= lower;
 52	val /= (upper - lower);
 53
 54	// round the value.   Sreturn the U16
 55	return (U16)(llround(val*U16MAX));
 56}
 57
 58
 59inline U16 F32_to_U16(F32 val, F32 lower, F32 upper)
 60{
 61	val = llclamp(val, lower, upper);
 62	// make sure that the value is positive and normalized to <0, 1>
 63	val -= lower;
 64	val /= (upper - lower);
 65
 66	// return the U16
 67	return (U16)(llfloor(val*U16MAX));
 68}
 69
 70inline F32 U16_to_F32(U16 ival, F32 lower, F32 upper)
 71{
 72	F32 val = ival*OOU16MAX;
 73	F32 delta = (upper - lower);
 74	val *= delta;
 75	val += lower;
 76
 77	F32 max_error = delta*OOU16MAX;
 78
 79	// make sure that zero's come through as zero
 80	if (fabsf(val) < max_error)
 81		val = 0.f;
 82
 83	return val;
 84}
 85
 86
 87inline U8 F32_to_U8_ROUND(F32 val, F32 lower, F32 upper)
 88{
 89	val = llclamp(val, lower, upper);
 90	// make sure that the value is positive and normalized to <0, 1>
 91	val -= lower;
 92	val /= (upper - lower);
 93
 94	// return the rounded U8
 95	return (U8)(llround(val*U8MAX));
 96}
 97
 98
 99inline U8 F32_to_U8(F32 val, F32 lower, F32 upper)
100{
101	val = llclamp(val, lower, upper);
102	// make sure that the value is positive and normalized to <0, 1>
103	val -= lower;
104	val /= (upper - lower);
105
106	// return the U8
107	return (U8)(llfloor(val*U8MAX));
108}
109
110inline F32 U8_to_F32(U8 ival, F32 lower, F32 upper)
111{
112	F32 val = ival*OOU8MAX;
113	F32 delta = (upper - lower);
114	val *= delta;
115	val += lower;
116
117	F32 max_error = delta*OOU8MAX;
118
119	// make sure that zero's come through as zero
120	if (fabsf(val) < max_error)
121		val = 0.f;
122
123	return val;
124}
125
126inline U8 F32_TO_STRING(F32 val, F32 lower, F32 upper)
127{
128	val = llclamp(val, lower, upper); //[lower, upper]
129	// make sure that the value is positive and normalized to <0, 1>
130	val -= lower;					//[0, upper-lower]
131	val /= (upper - lower);			//[0,1]
132	val = val * MAXSTRINGVAL;		//[0, MAXSTRINGVAL]
133	val = floor(val + 0.5f);		//[0, MAXSTRINGVAL]
134
135	U8 stringVal = (U8)(val) + FIRSTVALIDCHAR;			//[FIRSTVALIDCHAR, MAXSTRINGVAL + FIRSTVALIDCHAR]
136	return stringVal;
137}
138
139inline F32 STRING_TO_F32(U8 ival, F32 lower, F32 upper)
140{
141	// remove empty space left for NULL, newline, etc.
142	ival -= FIRSTVALIDCHAR;								//[0, MAXSTRINGVAL]
143
144	F32 val = (F32)ival * (1.f / (F32)MAXSTRINGVAL);	//[0, 1]
145	F32 delta = (upper - lower);
146	val *= delta;										//[0, upper - lower]
147	val += lower;										//[lower, upper]
148
149	return val;
150}
151
152#endif