# TechReport: Code review about HoughMappingGenerator
## 1. Dimension
### 1.1 Full

### 1.2 xy-plane(polar location)

## 2. Constant
### 2.1 Mechanical parameter
| L | Lr | r_SL | N_TS_SL |
|---|----|--------|---------|
| 0 | 0 | 19.80 | 160 |
| 1 | 2 | 40.16 | 192 |
| 2 | 4 | 62.00 | 256 |
| 3 | 6 | 83.84 | 320 |
| 4 | 8 | 105.68 | 384 |
* L: Index in code
* Lr: Real index No. of vaild super layers in 2D-Trigger
* r_SL: Radius from center of circle to specific SL, fixed (radius of machine)
* for instance: r_SL[2] = 83.84 (see [above pictrure: r-SL4](###1.2-xy-plane(polar-location)))
* N_TS_SL: Max count of *trigger segment*
* for instance: N_TS_SL[4] = 384 (see [above pictrure: N_TS_SL-8](###1.2-xy-plane(polar-location)))
### 2.2 HoughPlane

* HoughPlane_nx = 160: The count of x scale(phi) in Hough Plane
* HoughPlane_ny = 16: The count of y scale\(r\) in Hough Plane
## 3. Space transform (pole to xy)
See [above pictrure: N_TS_SL-8](###1.2-xy-plane(polar-location)),
our 2D trigger plane is cricle plane.
We easily describe the location of TS with polar location, that's $(r,$ $\phi$$)$ .
But the [Hough Line/Curve Transform](https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html) work on the xy-plane.
So we need to convert spcace from ploar space to xy-plane for each TS-location in the first step:
```python
for i in 0 to L
x = r_SL[i] * cos( i * ( (2*pi) / (N_TS_SL[i]) ) )
y = r_SL[i] * sin( i * ( (2*pi) / (N_TS_SL[i]) ) )
```
note: Regarding the `L`, see the [table of Mechanical parameter](###2.1-Mechanical-parameter)
## 4. Hough curve Transform
Old version:
``` python
for i in 0 to n_TS_SL
r[i] =
( (x[i])^2 + (y[i])^2 ) /
2 * ( x[i]*cos(phi[i]) + y[i]*sin(phi[i]) )
```
New version:
``` python
for i in 0 to n_TS_SL
r[i] =
(2 / r_SL) *
sin( phi[i] - ( (2*pi) / n_TS_SL )*i )
```
## Files generated
### TSIM Files
### HDL Files
* MapLoop
* i -> {0,2,4,6,8}
* j -> 0~79(minus), 80~159(plus)
* k -> 0~15
#### Original
* <SL[0],SL[2],SL[4],SL[6],SL[8]> * <Plus,Minus>, total 12 files
* Output
* SL[n]_Plus -> [39:0]
* SL[n]_Minus ->[79:40]
* Input
* SL[0]_TS -> [80:0]
* 80*2 = 160 = N_TS_SL[0]
* SL[2]_TS -> [96:0]
* 96*2 = 192 = N_TS_SL[2]
* SL[4]_TS -> [128:0]
* 128*2 = 256 = N_TS_SL[4]
* SL[6]_TS -> [160:0]
* 160*2 = 320 = N_TS_SL[6]
* SL[8]_TS -> [192:0]
* 192*2 = 384 = N_TS_SL[8]
* **problem:** Why is the range **[80:0]** instead of **[79:0]**?
#### Objective of optimize
* Using **array**, **index**, **struct** manage data -> **only 2 HDL files in total**
* File 1: Golbal parameter file (UT4_HoughMapping_param)
``` vhdl
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_misc.all;
package UT4_HoughMapping_param is
constant MAX_SL_COUNT_2D : integer := 5;
constant MAX_ROW_COUNT_2D : integer := 16;
----count of Hough cell at log-r
constant N_TS_SL_0 : integer := 160;
constant N_TS_SL_2 : integer := 192;
constant N_TS_SL_4 : integer := 256;
constant N_TS_SL_6 : integer := 320;
constant N_TS_SL_8 : integer := 384;
constant I_dont_know : integer := 40;
type TS_Data_t is record
SL0 : std_logic_vector( ( N_TS_SL_0 -1) downto 0 );
SL2 : std_logic_vector( ( N_TS_SL_2 -1) downto 0 );
SL4 : std_logic_vector( ( N_TS_SL_4 -1) downto 0 );
SL6 : std_logic_vector( ( N_TS_SL_6 -1) downto 0 );
SL8 : std_logic_vector( ( N_TS_SL_8 -1) downto 0 );
end record TS_Data_t;
type row_data is array ( 0 to (MAX_ROW_COUNT_2D-1) ) of
std_logic_vector( (I_dont_know -1) downto 0 );
type HoughMapping_Result_t is record
SL0 : row_data;
SL2 : row_data;
SL4 : row_data;
SL6 : row_data;
SL8 : row_data;
end record HoughMapping_Result_t;
end package UT4_HoughMapping_param;
```
* File 2: The main HDL File (HoughMapping Munus/Plus)
``` VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_misc.all;
library work;
use work.UT4_HoughMapping_param.all;
--invoke our defined type and parameter
entity UT4_HoughMapping is
port (
reset_n : std_logic;
clk : std_logic;
TS_input : TS_Data_t;
minus_Result : HoughMapping_Result_t;
plus_Result : HoughMapping_Result_t);
end entity UT4_HoughMapping;
...
...
...
```
* Add reset_n/clock for timing stable
* [Modified formula of Hough curve transformation](https://hackmd.io/@BelleII-CDC-Trig-NTU-DeWei/H1SM44b2U#Modified-formula-of-Hough-curve-transformation)
#### [All of code here](https://github.com/curly-wei/HoughTrigMapping_dev)
###### tags: `TechReport` `DeWei` `2D`