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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#pragma once
#include "Configuration/UnityConfigure.h"
#if ENABLE_SPRITES
#include "Runtime/BaseClasses/NamedObject.h"
#include "Runtime/Math/Rect.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Shaders/Material.h"
#include "Runtime/Graphics/Texture2D.h"
#include "Runtime/Utilities/dynamic_bitset.h"
class SpriteMeshGenerator
{
public:
struct s_cost
{
s_cost(){};
s_cost(float _c, float _w)
{
c=_c;
w=_w;
};
float c;
float w;
};
struct vertex
{
vertex(){};
vertex(Vector2f pos)
{
p = pos;
};
Vector2f p;
Vector2f n; // normal
int s; // sign -> indicating concavity
float c;
struct s_cost cost;
};
class path_segment
{
public:
path_segment(std::vector<vertex> path, int i0, int i1)
{
m_i0 = i0;
m_i1 = i1;
m_mx = max_distance(path, i0, i1);
};
int m_i0;
int m_i1;
int m_mx;
int m_cnt;
private:
int max_distance(std::vector<vertex> path, int i0, int i1);
};
class compare_path_segment {
public:
bool operator()(path_segment& s0, path_segment& s1)
{
return (s0.m_cnt < s1.m_cnt);
}
};
class path
{
public:
path(){};
path(const std::vector<vertex>& p, int w, int h, int sign, float area, float bias)
{
m_bx = w;
m_by = h;
m_area = area;
m_sign = sign;
m_path = p;
opt (bias);
bbox();
m_path0 = m_path;
}
std::vector<vertex> m_path;
void bbox ();
void simplify (float q, int mode);
bool isHole() const { return m_sign == '-'; }
const Vector2f& GetMin() const { return m_min; }
const Vector2f& GetMax() const { return m_max; }
private:
int find_max_distance(int i0);
void fit (std::vector<int>& ci, int i0, int i1);
bool opt (float bias);
bool dec (int i);
bool inf (int i);
bool select();
bool cvx_cost(int i);
bool cve_cost(int i);
int min_cost();
int self_intersect(Vector2f p0, Vector2f p1);
void clip();
void clip_edge(int e);
bool clip_test(Vector2f p, int side);
Vector2f clip_isec(Vector2f p, Vector2f q, int e);
int m_bx;
int m_by;
int m_sign;
float m_area;
Vector2f m_min;
Vector2f m_max;
std::vector<vertex> m_path0;
std::vector<int> m_invalid;
};
struct mask
{
mask(){};
mask(ColorRGBA32 *img, int width, int height, unsigned char acut, unsigned int dn)
{
w = width;
h = height;
int n = w*h;
dynamic_bitset bv;
bv.resize(n);
for (int y=0; y<height; y++)
for (int x=0; x<width; x++) {
if (img[x+y*width].a > acut)
bv.set(x+y*w);
}
if (dn)
this->dilate(dn, bv);
w+=1;
h+=1;
m_bv.resize(w*h);
for (int y=0; y<height; y++)
for (int x=0; x<width; x++) {
if (bv.test(x+y*width)) {
m_bv.set((x+0)+(y+0)*w);
m_bv.set((x+1)+(y+1)*w);
m_bv.set((x+0)+(y+1)*w);
m_bv.set((x+1)+(y+0)*w);
}
}
}
bool tst(int x, int y)
{
if ((x<0) || (x>=w) ||
(y<0) || (y>=h) )
return 0;
int i=x+y*w;
return m_bv.test(i);
}
void set(int x, int y)
{
if ((x<0) || (x>=w) ||
(y<0) || (y>=h) )
return;
int i=x+y*w;
m_bv.set(i);
}
void rst(int x, int y)
{
if ((x<0) || (x>=w) ||
(y<0) || (y>=h) )
return;
int i=x+y*w;
m_bv[i]=0;
}
void inv(int x, int y)
{
if ((x<0) || (x>=w) ||
(y<0) || (y>=h) )
return;
int i=x+y*w;
m_bv[i].flip();
}
int first()
{
int n=(int)m_bv.size();
for (int i=0; i<n; i++)
if (m_bv.test(i))
return i;
return -1;
}
bool dilate(int n, dynamic_bitset &bv)
{
if ((w==0) || (h==0))
return false;
UInt32 *md = new UInt32[w*h];
if (!mdist(md, bv))
return false;
for (int i=0; i<w*h; i++) {
if (md[i] <= n)
bv.set(i);
}
delete md;
return true;
}
int w;
int h;
dynamic_bitset m_bv;
private:
bool mdist(UInt32 *md, dynamic_bitset& bv )
{
if (!md)
return false;
for (int y=0; y<h; y++)
for (int x=0; x<w; x++) {
int i = x+y*w;
if (bv.test(i))
md[i] = 0;
else {
md[i] = w+h;
if (y>0) md[i] = min(md[i], md[i-w]+1);
if (x>0) md[i] = min(md[i], md[i-1]+1);
}
}
for (int y=h-1; y>=0; y--)
for (int x=w-1; x>=0; x--) {
int i = x+y*w;
if ((y+1) < h) md[i] = min(md[i], md[i+w]+1);
if ((x+1) < w) md[i] = min(md[i], md[i+1]+1);
}
return true;
}
};
public:
void Decompose(std::vector<Vector2f>* vertices, std::vector<int>* indices);
void MakeShape(ColorRGBA32* image, int imageWidth, int imageHeight, float hullTolerance, unsigned char alphaTolerance, bool holeDetection, unsigned int extrude, float bias, int mode);
bool FindBounds(Rectf& bounds);
const std::vector<path>& GetPaths() const { return m_paths; }
private:
bool trace(Vector2f p0, Vector2f p1, Vector2f &p);
bool invmask(std::vector<vertex>& outline);
bool contour(std::vector<vertex>& outline, int &sign, float &area);
std::vector<path> m_paths;
float evaluateLOD(const float areaHint, float area);
struct mask m_mask_org;
struct mask m_mask_cur;
};
#endif //ENABLE_SPRITES
|