aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil/cpm_map.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/jtutil/cpm_map.cc')
-rw-r--r--src/jtutil/cpm_map.cc99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/jtutil/cpm_map.cc b/src/jtutil/cpm_map.cc
new file mode 100644
index 0000000..db6ac8e
--- /dev/null
+++ b/src/jtutil/cpm_map.cc
@@ -0,0 +1,99 @@
+// 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>
+
+//*****************************************************************************
+//*****************************************************************************
+// 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);
+}