-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstipplecurves.cpp
353 lines (318 loc) · 13.6 KB
/
stipplecurves.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>
#include "opencv2/core.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/optflow.hpp>
#include "Mask.cpp"
#include "Show.cpp"
#include "Draw.cpp"
#include "PLY.cpp"
using namespace std;
using namespace cv;
int main(int argc, const char* argv[])
{
FastNoise fn;
string ply_in_name;
string image_in_name;
string image_out_name;
//parameters
ply_in_name = argv[1];
image_in_name = argv[2];
image_out_name = argv[3];
int maxCount =atoi(argv[4]); //points in stroke
cout << "\033[1;32m(params)\033[0m strokes per point : " << maxCount << endl;
//pixelforce
bool dopixelforce =atoi(argv[5]);
float pixelInfluence =atof(argv[6]);
if (dopixelforce) {cout << "\033[1;32m(params)\033[0m using pixelforce (" << pixelInfluence << ")" << endl;}
float minpixforce =atof(argv[7]);
int halfperception =atoi(argv[8]); //2
float pixeldiv =100.;
//gradientforce,tangentforce from image fields
bool dogradientforce =atoi(argv[9]);
int gradientblur =atoi(argv[10]); //blur image for gradient
float gradientInfluence =atof(argv[11]);
bool dotangentforce =atoi(argv[12]);
float tangentInfluence =atof(argv[13]);
//dragforce
float dragInfluence =atof(argv[14]);
//fastnoiseforce
bool donoiseforce =atoi(argv[15]);
bool docurlnoise =atoi(argv[16]);
float noiseInfluence =atof(argv[17]);
if (donoiseforce) {cout << "\033[1;32m(params)\033[0m using noiseforce (" << noiseInfluence << ")" << endl;}
float zstep =atof(argv[18]);
float noisepower =3;
float circularboost =5;
int fnnoisetype =atoi(argv[19]); //4
int fnseed =1337;
float fnfrequency =atof(argv[20]); //0.005
int fnoctaves =4;
float fnlacunarity =2;
float fngain =0.5;
//boundforce
bool doboundforce =atoi(argv[21]); //true
float bound =atof(argv[22]); //80
float boundForceFactor =atof(argv[23]);
float boundSuperness =5;
//colinear
bool colinearlimit =atoi(argv[24]);
if (colinearlimit) {cout << "\033[1;32m(params)\033[0m colinear limit on ..." << endl;}
//display
float maxSpeed =atof(argv[25]); //2000
float lineopacity =atof(argv[26]); //.5
float colorpower =atof(argv[27]); //3
float sizemin =atof(argv[28]); //1
float sizemax =atof(argv[29]); //6
float linewidthmin =atof(argv[30]); //0
float linewidthmax =atof(argv[31]); //6
float splinestep =atof(argv[32]); //.01
float oversampling =atof(argv[33]); //2
int output_x =atoi(argv[34]); //1920
bool debug =atoi(argv[35]); //false
//lets go
Mat ima_in = imread(image_in_name, IMREAD_GRAYSCALE);
int width =ima_in.cols;
int height=ima_in.rows;
cout << "reading : " << image_in_name << " (" << width << "/" << height << ")" << endl;
//cout << "input width/height : " << width << "/" << height << endl;
//calculate gradient
Mat dx,dy,ima_in_blur;
GaussianBlur(ima_in,ima_in_blur,Size(gradientblur, gradientblur),0,0,BORDER_DEFAULT);
Sobel(ima_in_blur, dx, CV_32F, 1,0);
Sobel(ima_in_blur, dy, CV_32F, 0,1);
Mat sobel_angle, sobel_mag;
cartToPolar(dx, dy, sobel_mag, sobel_angle);
//stipples
vector<Point2f> stippleList;
vector<float> stipplesizeList;
//cout << "reading : " << ply_in_name << endl;
ReadPLY(ply_in_name,width,height,stippleList,stipplesizeList);
int strokes =stippleList.size();
//cout << "pixels to draw : " << strokes << endl;
//if (1./maxCount < splinestep) {
bool forcelines=0;
if (splinestep == 0) {
splinestep=1./maxCount;
cout << "splinestep set to : " << splinestep << endl;
}
if (splinestep == -1) {
forcelines=1;
cout << "drawing lines only ..."<< endl;
}
//initialize fastnoise
//https://github.com/Auburns/FastNoise
cout << "initializing fast noise" << endl;
fn.SetSeed(fnseed);
fn.SetFrequency(fnfrequency);
fn.SetFractalOctaves(fnoctaves);
fn.SetFractalLacunarity(fnlacunarity);
fn.SetFractalGain(fngain);
switch(fnnoisetype) {
case 0 : fn.SetNoiseType(FastNoise::Value);break;
case 1 : fn.SetNoiseType(FastNoise::ValueFractal);break;
case 2 : fn.SetNoiseType(FastNoise::Perlin);break;
case 3 : fn.SetNoiseType(FastNoise::PerlinFractal);break;
case 4 : fn.SetNoiseType(FastNoise::Simplex);break;
case 5 : fn.SetNoiseType(FastNoise::SimplexFractal);break;
case 6 : fn.SetNoiseType(FastNoise::Cellular);break;
case 7 : fn.SetNoiseType(FastNoise::WhiteNoise);break;
case 8 : fn.SetNoiseType(FastNoise::Cubic);break;
case 9 : fn.SetNoiseType(FastNoise::CubicFractal);break;
}
//initialize
float absSpeed=0.;
int clampedstroke=0;
int colinearstroke=0;
int allstroke=0;
float z=0;
float dragcoefficient;
//image size
//cout << "input width/height : " << width << "/" << height << endl;
float ratio=1.;
if (output_x != 0) {
ratio=(float)output_x/(float)width;
//cout << "resize ratio : " << ratio << endl;
cout << "output width/height : " << (int)(width*ratio) << "/" << (int)(height*ratio) << endl;
}
float oversamplingratio=ratio*oversampling;
//cout << "oversampling ratio : x" << oversampling << endl;
cout << "oversampling width/height : " << (int)(width*oversamplingratio) << "/" << (int)(height*oversamplingratio) << " (x" << oversampling << ")" << endl;
Mat overlay;
Mat ima_messy(floor(height*oversamplingratio), floor(width*oversamplingratio), CV_8UC1, Scalar(255));
//
Point2f ppos,pos;
Point2f vel(0.0,0.0);
Point2f force(0.0,0.0);
Point2f previousforce(0.0,0.0);
Point2f pixelforce,gradientforce,tangentforce,noiseforce,boundforce,dragforce;
float stipplesize;
int mcount=0;
//see pixelforce animated
/*
float maxpixforce=0.;
Mat PixelForceMat=ShowPixelForce(ima_in,5,30,halfperception,pixeldiv,maxpixforce);
string imanumber = argv[];
string finalname = "pixelforce." + imanumber + ".png";
cout << "ima" << finalname << endl;
imwrite(finalname,PixelForceMat);
*/
//debug
if (debug) {
int sampling=5;
int vscale=100;
//visualizing fastnoise
Mat fastnoisemat(height, width, CV_8UC1, Scalar(0));
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
fastnoisemat.at<uchar>(i, j)=(sigmoid(fn.GetNoise(j,i,1),noisepower))*255.;
}
}
cout << "\033[1;34m(debug)\033[0m writing : fastnoise.png" << endl;
imwrite("debug_fastnoise.png",fastnoisemat);
//visualizing pixel force
float maxpixforce=0.;
Mat PixelForceMat=ShowPixelForce(ima_in,sampling,vscale,halfperception,pixeldiv,maxpixforce);
imwrite("debug_pixelforce.png",PixelForceMat);
cout << "\033[1;34m(debug)\033[0m writing : pixelforce.png" << endl;
cout << "\033[1;34m(debug)\033[0m max pixelforce magnitude : " << maxpixforce << endl;
Mat PixelForceAngleMat=ShowPixelForceAngle(ima_in,halfperception,pixeldiv,maxpixforce);
imwrite("debug_pixelforceangle.png",PixelForceAngleMat);
cout << "\033[1;34m(debug)\033[0m writing : pixelforceangle.png" << endl;
//visualizing noise force
float maxnoiseforce=0.;
Mat NoiseForceMat=ShowFastNoiseForce(ima_in,sampling,vscale,z,noisepower,circularboost,fn,docurlnoise,maxnoiseforce);
if (docurlnoise) {
imwrite("debug_noiseforce.png",NoiseForceMat);
cout << "\033[1;34m(debug)\033[0m writing : (curl) noiseforce.png" << endl;
}
else {
imwrite("debug_noiseforce.png",NoiseForceMat);
cout << "\033[1;34m(debug)\033[0m writing : (nocurl) noiseforce.png" << endl;
}
cout << "\033[1;34m(debug)\033[0m max noiseforce magnitude : " << maxnoiseforce << endl;
//visualizing bounding force
float maxboundforce=0.;
Mat SuperEllipseForceMat=ShowSuperEllipseForce(ima_in,sampling,vscale,bound,boundSuperness,maxboundforce);
imwrite("debug_superellipse.png",SuperEllipseForceMat);
cout << "\033[1;34m(debug)\033[0m writing : superellipse.png" << endl;
cout << "\033[1;34m(debug)\033[0m max boundforce magnitude : " << maxboundforce << endl;
//visualizing gradient force
float maxgradientforce=0.;
Mat GradientForceMat=ShowGradientForce(dx,dy,sampling,vscale,maxgradientforce);
imwrite("debug_gradient.png",GradientForceMat);
cout << "\033[1;34m(debug)\033[0m writing : gradient.png" << endl;
cout << "\033[1;34m(debug)\033[0m max gradientforce magnitude : " << maxgradientforce << endl;
//visualising gradient norm
imwrite("debug_gradientnorm.png",sobel_mag);
cout << "\033[1;34m(debug)\033[0m writing : gradientnorm.png" << endl;
//visualizing curl force
float maxcurlforce=0.;
Mat CurlForceMat=ShowCurlForce(ima_in_blur,sampling,vscale,maxcurlforce);
imwrite("debug_curlforce.png",CurlForceMat);
cout << "\033[1;34m(debug)\033[0m writing : curlforce.png" << endl;
cout << "\033[1;34m(debug)\033[0m max curlforce magnitude : " << maxcurlforce << endl;
}
//stroke
for(int st=0; st<strokes; st++) {//main loop
ima_messy.copyTo(overlay);
vector<Point2f> pointList;
pos=stippleList[st];
//circle(overlay,pos*ratio,5,(0),1,CV_AA);
stipplesize=range(stipplesizeList[st],sizemin,sizemax,linewidthmin,linewidthmax);
//stipplesize=1;
pointList.push_back(pos);
vel=vel*0;
mcount++;
//strokes
for(int step=0; step<maxCount; step++) //stroke loop
{
//update
ppos=pos;
force=force*0.0;
//dampen vel
dragcoefficient=pow((1.-((float)step/(float)(maxCount-1))),dragInfluence);
vel=vel*dragcoefficient;
//add pixel force
if (dopixelforce) {
pixelforce=PixelForce(pos,ima_in_blur,halfperception,pixeldiv);
if (mag(pixelforce) < minpixforce) {pixelforce=pixelforce*0.;}
force=force+(pixelInfluence*pixelforce);
}
if (dogradientforce) {
//gradientforce=FlowForce(pos,gradientflow);
gradientforce=GradientForce(dx,dy,pos);
force=force+(gradientInfluence*gradientforce);
}
//add tangentforce
if (dotangentforce) {
//tangentforce=ccwtangent(gradientforce);
tangentforce=CurlForce(ima_in_blur,pos);
force=force+(tangentInfluence*tangentforce);
}
//add noise force
if (donoiseforce) {
if (docurlnoise) {
noiseforce=CurlNoiseForce(pos,z,noisepower,fn);
}
else {
noiseforce=FastNoiseForce(pos,z,noisepower,circularboost,fn);
}
force=force+(noiseInfluence*noiseforce);
}
//add bound force
if (doboundforce) {
boundforce=SuperEllipseForce(pos,bound,boundSuperness,ima_in);
force=force+(boundForceFactor*boundforce);
}
vel=vel+force;
if (colinearlimit) {
float colinear=colineardamping(previousforce,vel);
if (colinear == 1 && step != 0) {
pointList.push_back(pos);
colinearstroke++;
break;
//vel=vel*colineardamp;
}
}
previousforce=vel;
float normvel=mag(vel);
allstroke++;
if (normvel > absSpeed) { absSpeed = normvel;}
if (normvel > maxSpeed) {
vel=vel*(maxSpeed/normvel);
absSpeed = maxSpeed;
clampedstroke++;
}
pos=pos+vel;
if (pos.x > width || pos.x < 0 || pos.y > height || pos.y < 0) {
pointList.push_back(pos);
break;
}
pointList.push_back(pos);
//circle(overlay,pos*ratio,2,(0),1,CV_AA);
z+=zstep;
}//end stroke
if (!(mcount%100)) {
cout << "scribbling : " << mcount << "\r" << std::flush;
}
drawCurve(overlay,pointList,oversamplingratio,splinestep,stipplesize,colorpower,forcelines);
addWeighted(overlay, lineopacity,ima_messy,1-lineopacity,0,ima_messy);
}//end while()
//finished !
cout << endl;
cout << "\033[1;33m(statistics)\033[0m total strokes : " << allstroke << endl;
int clampedstrokepercent=round(100.*(float)clampedstroke/(float)allstroke);
cout << "\033[1;33m(statistics)\033[0m maximum speed : " << absSpeed << " (" << clampedstrokepercent << "% (" << clampedstroke << ") strokes clamped over " << maxSpeed << ")" << endl;
cout << "\033[1;33m(statistics)\033[0m colinear blocked strokes : " << colinearstroke << endl;
//resize ima_messy
resize(ima_messy, ima_messy, Size(width*ratio,height*ratio), 0, 0, INTER_AREA);
cout << "writing result : " << image_out_name << endl << endl;
imwrite(image_out_name,ima_messy);
return 0;
}//end main
//gmic blondie1.jpg -fx_normalize_local 2,6,5,40,1,11,0,50,50 -o blondie1_normalize.png
//https://www.khanacademy.org/computing/computer-programming/programming-natural-simulations/programming-forces/a/newtons-laws-of-motion