aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil/cpm_map.cc
blob: 720a59cd502f7088ba8e51930b482cd7ed229f99 (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
// cpm_map.cc -- "integer +/-" mapping  i --> j = const +/- i
// $Id$
//
// cpm_map::{plus,minus}
// cpm_map::cpm_map	// mirror map, specified by fixed point
// cpm_map::cpm_map	// generic map specified by sample point & sign
// cpm_map::cpm_map	// generic map specified by *double* sample point & sign
//

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

//******************************************************************************
//******************************************************************************
// cpm_map -- ipm mapping from integers <--> floating point values
//******************************************************************************
//******************************************************************************

//
// we declared and initilized these constants in the .hh file, but
// we still have to *define* them here
//
const bool cpm_map::plus_map;
const bool cpm_map::minus_map;

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

//
// This function constructs a  cpm_map  object with a "-" sign and a
// specified fixed point (must be integer or half-integer) and domain.
//
cpm_map::cpm_map(int min_i_in, int max_i_in,
		 double fixed_point)
	: min_i_(min_i_in), max_i_(max_i_in),
	  sign_(minus_map)
{
double d_offset = 2.0 * fixed_point;
if (! fuzzy<double>::is_integer(d_offset))
   then error_exit(ERROR_EXIT,
"***** cpm_map::cpm_map (mirror):\n"
"        fixed_point=%g isn't (fuzzily) integral or half-integral!\n"
,
		   fixed_point);				/*NOTREACHED*/

offset_ = round<double>::to_integer(d_offset);
}

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

//
// This function constructs a generic  cpm_map  object, with the mapping
// specified by a sample point  sample_i --> sample_j  and by sign (one
// of  {plus,minus}_map ).  The sample point need not be in domain/range.
//
cpm_map::cpm_map(int min_i_in, int max_i_in,
		 int sample_i, int sample_j,
		 bool sign_in)
		 : min_i_(min_i_in), max_i_(max_i_in),
		   offset_(sign_in ? sample_j - sample_i
				   : sample_j + sample_i),
		   sign_(sign_in)
{
// should be guaranteed by above  offset_  setup
assert( map(sample_i) == sample_j );
}

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

//
// This function constructs a generic  cpm_map  object, with the mapping
// specified by a *double* sample point  sample_i --> sample_j  (which
// must specify an  integer --> integer  mapping, i.e.  4.2 --> 4.2  is
// ok for a + map, and 4.5 --> 4.5 is ok for a minus map, but  4.2 --> 4.7
// is never ok) and by sign (one of  {plus,minus}_map ).  The sample point
// need not be in domain/range.
//
cpm_map::cpm_map(int min_i_in, int max_i_in,
		 double sample_i, double sample_j,
		 bool sign_in)
		 : min_i_(min_i_in), max_i_(max_i_in),
		   sign_(sign_in)
{
double d_offset = sign_in ? sample_j - sample_i
			  : sample_j + sample_i;
if (! fuzzy<double>::is_integer(d_offset))
   then error_exit(ERROR_EXIT,
"***** cpm_map::cpm_map (generic via fp sample point):\n"
"        d_offset=%g isn't fuzzily integral!\n"
"        ==> sample_i=%g --> sample_j=%g\n"
"            doesn't fuzzily specify an  integer --> integer  mapping!\n"
,
		   d_offset,
		   sample_i, sample_j);				/*NOTREACHED*/

offset_ = round<double>::to_integer(d_offset);
}