aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil/linear_map.cc
blob: b3b6e609527a2d04741752a6da88cf99e396723c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// linear_map.cc -- linear mapping from integers <--> floating point values
// $Id$
//
// linear_map::linear_map - basic constructor
// linear_map::linear_map - constructor for subrange of existing linear_map
// linear_map::fp_int_of_fp - convert fpt --> int coord, return as fpt
// linear_map::int_of_fp - convert fpt --> int, check for being fuzzily integral
// linear_map::delta_int_of_delta_fp - ... same for spacings (delta_* values)
//
// ***** template instantiation *****
//

#include <assert.h>
#include <stdio.h>
#include "jt/stdc.h"
#include "jt/util.hh"
#include "jt/linear_map.hh"
using jtutil::error_exit;

//******************************************************************************
//******************************************************************************
// linear_map -- linear mapping from integers <--> floating point values
//******************************************************************************
//******************************************************************************

//
// This function constructs a  linear_map<fpt>  object.
//
template <typename fpt>
linear_map<fpt>::linear_map(int min_int_in, int max_int_in,
			    fpt min_fp_in, fpt delta_fp_in, fpt max_fp_in)
	: delta_(delta_fp_in), inverse_delta_(1.0 / delta_fp_in),
	  min_int_(min_int_in), max_int_(max_int_in)
{
constructor_common(min_fp_in, max_fp_in);
}

//******************************************************************************

//
// This function constructs a  linear_map<fpt>  object with a subrange
// of an existing one.
//
template <typename fpt>
linear_map<fpt>::linear_map(const linear_map<fpt> &lm_in,
			    int min_int_in, int max_int_in)	// subrange
	: delta_(lm_in.delta_fp()), inverse_delta_(lm_in.inverse_delta_fp()),
	  min_int_(min_int_in), max_int_(max_int_in)
{
if (! (in_range(min_int_in) && in_range(max_int_in)) )
   then error_exit(ERROR_EXIT,
"***** linear_map<fpt>::linear_map:\n"
"        min_int_in=%d and/or max_int_in=%d\n"
"        aren't in integer range [%d,%d] of existing linear_map!\n"
,
		   min_int_, max_int_,
		   lm_in.min_int(), lm_in.max_int());		/*NOTREACHED*/

constructor_common(lm_in.fp_of_int_unchecked(min_int_in),
		   lm_in.fp_of_int_unchecked(max_int_in));
}

//******************************************************************************

//
// This function does the common argument validation and setup for
// all the constructors of class  linear_map<fpt>:: .
//
template <typename fpt>
void linear_map<fpt>::constructor_common(fpt min_fp_in, fpt max_fp_in)
	// assumes
	//	min_int_, max_int_, delta_, inverse_delta_
	// are already initialized
	// ==> ok to use min_int(), max_int(), delta_fp(), inverse_delta_fp()
	// ... other class members *not* yet initialized
{
offset_ = 0.0;		// temp value
offset_ = min_fp_in - fp_of_int_unchecked(min_int());

// this should be guaranteed by the above calculation
assert(fuzzy<fpt>::EQ(fp_of_int_unchecked(min_int()), min_fp_in));

// this is a test of the consistency of the input arguments
if (fuzzy<fpt>::NE(fp_of_int_unchecked(max_int()), max_fp_in))
   then error_exit(ERROR_EXIT,
"***** linear_map<fpt>::linear_map:\n"
"        int range [%d,%d]\n"
"        and fpt range [%g(%g)%g]\n"
"        are (fuzzily) inconsistent!\n"
,
		   min_int(), max_int(),
		   double(min_fp_in), double(delta_fp()), double(max_fp_in));
								/*NOTREACHED*/
}

//******************************************************************************

//
// This function converts  fpt  --> int  coordinate, returning the result
// as an fpt (which need not be fuzzily integral).
//
template <typename fpt>
fpt linear_map<fpt>::fp_int_of_fp(fpt x)
	const
{
if (! in_range(x))
   then error_exit(ERROR_EXIT,
"***** linear_map<fpt>::fp_int_of_fp:\n"
"        fpt value x=%g is (fuzzily) outside the grid!\n"
"        {min(delta)max}_fp = %g(%g)%g\n"
,
		   double(x),
		   double(min_fp()), double(delta_fp()), double(max_fp()));
								/*NOTREACHED*/

return inverse_delta_ * (x - offset_);
}

//******************************************************************************

//
// This function converts  fpt  --> int  and checks that the result is
// fuzzily integral.  (The  nia  argument specifies what to do if the
// result *isn't* fuzzily integral.)
//
// FIXME:
// Having to explicitly specify the global namespace for ::round<fpt>::
// is ++ugly. :(
//
template <typename fpt>
int linear_map<fpt>::int_of_fp(fpt x, noninteger_action nia = error)
	const
{
const fpt fp_int = fp_int_of_fp(x);

if (fuzzy<fpt>::is_integer(fp_int))
   then {
	// x is (fuzzily) a grid point ==> return that
	return ::round<fpt>::to_integer(fp_int);		// *** EARLY RETURN ***
	}

// get to here ==> x isn't (fuzzily) a grid point
static const char *const noninteger_msg =
   "%s linear_map<fpt>::int_of_fp:\n"
   "        x=%g isn't (fuzzily) a grid point!\n"
   "        {min(delta)max}_fp() = %g(%g)%g\n"
   ;
switch	(nia)
	{
case error:
	error_exit(ERROR_EXIT,
		   noninteger_msg,
		   "*****",
		   double(x),
		   double(min_fp()), double(delta_fp()), double(max_fp()));
								/*NOTREACHED*/

case warning:
	printf(noninteger_msg,
	       "---",
	       double(x),
	       double(min_fp()), double(delta_fp()), double(max_fp()));
	// fall through

case round:
	return ::round<fpt>::to_integer(fp_int);	// *** EARLY RETURN ***

case floor:
	return ::round<fpt>::floor(fp_int);		// *** EARLY RETURN ***

case ceiling:
	return ::round<fpt>::ceiling(fp_int);		// *** EARLY RETURN ***

default:
	error_exit(PANIC_EXIT,
"***** linear_map<fpt>::int_of_fp: illegal nia=(int)%d\n"
"                                 (this should never happen!)\n"
,
		   int(nia));					/*NOTREACHED*/
	}
return 0;	// dummy return to quiet gcc
		// (which doesn't grok that error_exit() never returns)
}

//******************************************************************************

//
// This function converts "delta" spacings in the fpt coordinate to
// corresponding "delta" spacings in the int coordinate, and checks that
// the result is fuzzily integral.  (The  nia  argument specifies what to
// do if the result *isn't* fuzzily integral.)
//
// FIXME:
// Having to explicitly specify the global namespace for ::round<fpt>::
// is ++ugly. :(
//
template <typename fpt>
int linear_map<fpt>::delta_int_of_delta_fp(fpt delta_x,
					    noninteger_action nia = error)
	const
{
const fpt fp_delta_int = inverse_delta_ * delta_x;

if (fuzzy<fpt>::is_integer(fp_delta_int))
   then {
	// delta_x is (fuzzily) an integer number of grid spacings
	// ==> return that
	return ::round<fpt>::to_integer(fp_delta_int);	// *** EARLY RETURN ***
	}

// get to here ==> delta_x isn't (fuzzily) an integer number of grid spacings
static const char *const noninteger_msg =
   "%s linear_map<fpt>::delta_int_of_delta_fp:\n"
   "        delta_x=%g isn't (fuzzily) an integer number of grid spacings!\n"
   "        {min(delta)max}_fp() = %g(%g)%g\n"
   ;
switch	(nia)
	{
case error:
	error_exit(ERROR_EXIT,
		   noninteger_msg,
		   "*****",
		   double(delta_x),
		   double(min_fp()), double(delta_fp()), double(max_fp()));
								/*NOTREACHED*/

case warning:
	printf(noninteger_msg,
	       "---",
	       double(delta_x),
	       double(min_fp()), double(delta_fp()), double(max_fp()));
	// fall through

case round:
	return ::round<fpt>::to_integer(fp_delta_int);	// *** EARLY RETURN ***

case floor:
	return ::round<fpt>::floor(fp_delta_int);	// *** EARLY RETURN ***

case ceiling:
	return ::round<fpt>::ceiling(fp_delta_int);	// *** EARLY RETURN ***

default:
	error_exit(PANIC_EXIT,
"***** linear_map<fpt>::delta_int_of_delta_fp: illegal nia=(int)%d\n"
"                                 (this should never happen!)\n"
,
		   int(nia));					/*NOTREACHED*/
	}
return 0;	// dummy return to quiet gcc
		// (which doesn't grok that error_exit() never returns)
}

//******************************************************************************
//******************************************************************************
//******************************************************************************

//
// ***** template instantiation *****
//

template class linear_map<double>;