Libraryless. Click here for Pure Java version (6044L/36K).
1 | transient sclass HexagonalBWIntegralImage { |
2 | final int w, h; |
3 | |
4 | // data[0] is the sharp edge, data[1] the blunt edge |
5 | final int[][] data = new int[2][]; |
6 | |
7 | // the column sums in three directions |
8 | // north, north west, north east |
9 | final int[][] columnSums = new int[3][]; |
10 | |
11 | int offset1; |
12 | |
13 | final static double slant = sqrtAThird(); |
14 | |
15 | *(BufferedImage img) { this(BWImage(img)); } |
16 | |
17 | *(BWImage img) { |
18 | w = img.w(); |
19 | h = img.h(); |
20 | for i to 2: |
21 | data[0] = new int[w*h]; |
22 | columnSums[0] = new int[h]; |
23 | int i = 0; |
24 | int lastShift = 0; |
25 | for y to h: { |
26 | int sum = 0; |
27 | int shift = iround(y*slant); |
28 | int inc = shift-lastShift; |
29 | for x to w: { |
30 | // grab pixels |
31 | int pixel = img.getInt(x, y); |
32 | sum += pixel; |
33 | |
34 | // calc wedges |
35 | data[0][i] = data[1][i] = sum; |
36 | |
37 | if (y != 0) { |
38 | if (x >= inc) |
39 | data[0][i] += data[0][i-w-inc]; |
40 | |
41 | if (x+inc < w) |
42 | data[1][i] += data[1][i-w+inc]; |
43 | } |
44 | |
45 | i++; |
46 | } |
47 | |
48 | columnSums[0][y] = sum; |
49 | } |
50 | |
51 | offset1 = iceil(h*slant); |
52 | columnSums[1] = new int[offset1+w]; |
53 | columnSums[2] = new int[w+offset1]; |
54 | |
55 | int sum = 0; |
56 | for (int x = w-1; x >= -offset1; x--) { |
57 | int y = 0; |
58 | double xfrac = x; |
59 | while true { |
60 | int x2 = iround(xfrac); |
61 | if (x2 >= w) |
62 | break; |
63 | sum += img.getInt(x2, y); |
64 | y++; |
65 | xfrac += slant; |
66 | } |
67 | columnSums[1][x-offset1] = sum; |
68 | } |
69 | |
70 | sum = 0; |
71 | for (int x = 0; x < w+offset1; x++) { |
72 | int y = 0; |
73 | double xfrac = x; |
74 | while true { |
75 | int x2 = iround(xfrac); |
76 | if (x2 < 0) |
77 | break; |
78 | sum += img.getInt(x2, y); |
79 | y++; |
80 | xfrac -= slant; |
81 | } |
82 | columnSums[2][x] = sum; |
83 | } |
84 | } |
85 | |
86 | int getData(int x, int y, int type) { |
87 | if (x <= 0 || y <= 0) ret 0; |
88 | ret data[type][y*w+x]; |
89 | } |
90 | |
91 | int upPointingTriangleSum(int x1, int x2, int y2) { |
92 | // formula: |
93 | // north west wedge from bottom right corner |
94 | // - left wedge from top corner |
95 | // + bottom left wedge from bottom left corner |
96 | int xMid = (x1+x2)/2, y1 = iround(y2-(x2-x1)*sqrt3()); |
97 | ret topLeftWedge(x2, y2) |
98 | -leftWedge(xMid, y1) |
99 | -bottomLeftWedge(x1, y2); |
100 | } |
101 | |
102 | int downPointingTriangleSum(int x1, int x2, int y1) { |
103 | // formula: |
104 | // top wedge from bottom corner |
105 | // - big wedge from top right corner |
106 | // + top left wedge from top left corner |
107 | int xMid = (x1+x2)/2, y2 = iround(y1+(x2-x1)*sqrt3()); |
108 | ret topWedge(xMid, y2) |
109 | -bigWedge(x2, y1) |
110 | -topLeftWedge(x1, y1); |
111 | } |
112 | |
113 | int topLeftWedge(int x, int y) { |
114 | ret getData(x, y, 0); |
115 | } |
116 | |
117 | int smallWedge(int x, int y) { |
118 | ret getData(x, y, 0); |
119 | } |
120 | |
121 | int bigWedge(int x, int y) { |
122 | ret getData(x, y, 1); |
123 | } |
124 | |
125 | // big wedge minus small wedge |
126 | int topWedge(int x, int y) { |
127 | ret bigWedge(x, y)-smallWedge(x, y); |
128 | } |
129 | |
130 | int leftWedge(int x, int y) { |
131 | ret columnSum(x, y, 1)-topWedge(x, y); |
132 | } |
133 | |
134 | int bottomLeftWedge(int x, int y) { |
135 | ret columnSum(x, y, 1)-bigWedge(x, y); |
136 | } |
137 | |
138 | int columnSum(int x, int y, int direction) { |
139 | if (direction == 0) ret columnSums[0][y*w+x]; |
140 | else if (direction == 1) ret columnSums[1][y*w+x+iround(y*slant)]; |
141 | else ret columnSums[2][y*w-x-iround(y*slant)]; |
142 | } |
143 | |
144 | public int getWidth() { ret w; } |
145 | public int getHeight() { ret h; } |
146 | int w() { ret w; } |
147 | int h() { ret h; } |
148 | } |
Began life as a copy of #1031360
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1031364 |
Snippet name: | HexagonalBWIntegralImage [dev.] |
Eternal ID of this version: | #1031364/17 |
Text MD5: | 218db30d7e9c7154a0798a66f90dc8a9 |
Transpilation MD5: | 681821ab9a1da711bea12a842c54834f |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-06-07 08:45:54 |
Source code size: | 3602 bytes / 148 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 199 / 304 |
Version history: | 16 change(s) |
Referenced in: | [show references] |