forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_image.cpp
118 lines (113 loc) · 3.22 KB
/
_image.cpp
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
/* -*- mode: c++; c-basic-offset: 4 -*- */
#define NO_IMPORT_ARRAY
#include <math.h>
// utilities for irregular grids
void _bin_indices_middle(
unsigned int *irows, int nrows, const float *ys1, unsigned long ny, float dy, float y_min)
{
int i, j, j_last;
unsigned int *rowstart = irows;
const float *ys2 = ys1 + 1;
const float *yl = ys1 + ny;
float yo = y_min + dy / 2.0f;
float ym = 0.5f * (*ys1 + *ys2);
// y/rows
j = 0;
j_last = j;
for (i = 0; i < nrows; i++, yo += dy, rowstart++) {
while (ys2 != yl && yo > ym) {
ys1 = ys2;
ys2 = ys1 + 1;
ym = 0.5f * (*ys1 + *ys2);
j++;
}
*rowstart = j - j_last;
j_last = j;
}
}
void _bin_indices_middle_linear(float *arows,
unsigned int *irows,
int nrows,
const float *y,
unsigned long ny,
float dy,
float y_min)
{
int i;
int ii = 0;
int iilast = (int)ny - 1;
float sc = 1 / dy;
int iy0 = (int)floor(sc * (y[ii] - y_min));
int iy1 = (int)floor(sc * (y[ii + 1] - y_min));
float invgap = 1.0f / (iy1 - iy0);
for (i = 0; i < nrows && i <= iy0; i++) {
irows[i] = 0;
arows[i] = 1.0;
}
for (; i < nrows; i++) {
while (i > iy1 && ii < iilast) {
ii++;
iy0 = iy1;
iy1 = (int)floor(sc * (y[ii + 1] - y_min));
invgap = 1.0f / (iy1 - iy0);
}
if (i >= iy0 && i <= iy1) {
irows[i] = ii;
arows[i] = (iy1 - i) * invgap;
} else
break;
}
for (; i < nrows; i++) {
irows[i] = iilast - 1;
arows[i] = 0.0;
}
}
void _bin_indices(int *irows, int nrows, const double *y, unsigned long ny, double sc, double offs)
{
int i;
if (sc * (y[ny - 1] - y[0]) > 0) {
int ii = 0;
int iilast = (int)ny - 1;
int iy0 = (int)floor(sc * (y[ii] - offs));
int iy1 = (int)floor(sc * (y[ii + 1] - offs));
for (i = 0; i < nrows && i < iy0; i++) {
irows[i] = -1;
}
for (; i < nrows; i++) {
while (i > iy1 && ii < iilast) {
ii++;
iy0 = iy1;
iy1 = (int)floor(sc * (y[ii + 1] - offs));
}
if (i >= iy0 && i <= iy1)
irows[i] = ii;
else
break;
}
for (; i < nrows; i++) {
irows[i] = -1;
}
} else {
int iilast = (int)ny - 1;
int ii = iilast;
int iy0 = (int)floor(sc * (y[ii] - offs));
int iy1 = (int)floor(sc * (y[ii - 1] - offs));
for (i = 0; i < nrows && i < iy0; i++) {
irows[i] = -1;
}
for (; i < nrows; i++) {
while (i > iy1 && ii > 1) {
ii--;
iy0 = iy1;
iy1 = (int)floor(sc * (y[ii - 1] - offs));
}
if (i >= iy0 && i <= iy1)
irows[i] = ii - 1;
else
break;
}
for (; i < nrows; i++) {
irows[i] = -1;
}
}
}