tesseract  5.0.0
mod128.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: mod128.h (Formerly dir128.h)
3  * Description: Header for class which implements modulo arithmetic.
4  * Author: Ray Smith
5  *
6  * (C) Copyright 1991, Hewlett-Packard Ltd.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #ifndef MOD128_H
20 #define MOD128_H
21 
22 #include "points.h"
23 
24 namespace tesseract {
25 
26 #define MODULUS 128 /*range of directions */
27 #define DIRBITS 7 // no of bits used
28 #define DIRSCALE 1000 // length of vector
29 
30 class DIR128 {
31 public:
32  DIR128() = default;
33 
34  DIR128( // constructor
35  int16_t value) { // value to assign
36  value %= MODULUS; // modulo arithmetic
37  if (value < 0) {
38  value += MODULUS; // done properly
39  }
40  dir = static_cast<int8_t>(value);
41  }
42  DIR128(const FCOORD fc); // quantize vector
43 
44  DIR128 &operator=( // assign of int16_t
45  int16_t value) { // value to assign
46  value %= MODULUS; // modulo arithmetic
47  if (value < 0) {
48  value += MODULUS; // done properly
49  }
50  dir = static_cast<int8_t>(value);
51  return *this;
52  }
53  int8_t operator-( // subtraction
54  const DIR128 &minus) const // for signed result
55  {
56  // result
57  int16_t result = dir - minus.dir;
58 
59  if (result > MODULUS / 2) {
60  result -= MODULUS; // get in range
61  } else if (result < -MODULUS / 2) {
62  result += MODULUS;
63  }
64  return static_cast<int8_t>(result);
65  }
66  DIR128 operator+( // addition
67  const DIR128 &add) const // of itself
68  {
69  DIR128 result; // sum
70 
71  result = dir + add.dir; // let = do the work
72  return result;
73  }
74  DIR128 &operator+=( // same as +
75  const DIR128 &add) {
76  *this = dir + add.dir; // let = do the work
77  return *this;
78  }
79  int8_t get_dir() const { // access function
80  return dir;
81  }
82 
83  int8_t dir; // a direction
84 };
85 
86 } // namespace tesseract
87 
88 #endif
#define MODULUS
Definition: mod128.h:26
DIR128 operator+(const DIR128 &add) const
Definition: mod128.h:66
DIR128 & operator+=(const DIR128 &add)
Definition: mod128.h:74
int8_t operator-(const DIR128 &minus) const
Definition: mod128.h:53
int8_t get_dir() const
Definition: mod128.h:79
DIR128(int16_t value)
Definition: mod128.h:34
int8_t dir
Definition: mod128.h:83
DIR128 & operator=(int16_t value)
Definition: mod128.h:44