forked from lucasw/vimjay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.cpp
315 lines (246 loc) · 8.25 KB
/
generate.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
/*
Copyright 2012 Lucas Walter
This file is part of Vimjay.
Vimjay is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Vimjay is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Vimjay. If not, see <http://www.gnu.org/licenses/>.
*/
#include "generate.h"
#include "config.h"
#include <glog/logging.h>
extern "C" {
#include "other/DSOnoises/noise1234.h"
#include "other/DSOnoises/simplexnoise1234.h"
#include "other/DSOnoises/sdnoise1234.h"
#include "other/DSOnoises/srdnoise23.h"
}
//#include "other/simplexnoise.h"
//#include "other/simplextextures.h"
// filter Node objects
namespace bm {
Bezier::Bezier(const std::string name) : ImageNode(name)
{
// TBD take x and y sigbuf inputs
setSignal("x0",10);
setSignal("y0",10);
setSignal("x1",100);
setSignal("y1",50);
setSignal("x2",200);
setSignal("y2",50);
setSignal("x3",300);
setSignal("y3",150);
setSignal("num", 4);
}
bool Bezier::update()
{
//if (!ImageNode::update()) return false;
if (!Node::update()) return false;
cv::Mat out = cv::Mat(Config::inst()->getImSize(), MAT_FORMAT_C3);
out = cv::Scalar(0,0,0);
std::vector<cv::Point2f> control_points;
control_points.push_back( cv::Point2f( getSignal("x0"), getSignal("y0") ));
control_points.push_back( cv::Point2f( getSignal("x1"), getSignal("y1") ));
control_points.push_back( cv::Point2f( getSignal("x2"), getSignal("y2") ));
control_points.push_back( cv::Point2f( getSignal("x3"), getSignal("y3") ));
int num = getSignal("num");
if (num < 2) { num = 2;
setSignal("num", num);
}
std::vector<cv::Point2f> bezier_points;
getBezier(control_points, bezier_points, num);
for (int i = 1; i < bezier_points.size(); i++) {
cv::line(out, bezier_points[i-1], bezier_points[i], cv::Scalar(255, 255, 255), 2, CV_AA );
}
setImage("out", out);
return true;
}
//////////////////////////////////////////////////
Circle::Circle(const std::string name) : ImageNode(name)
{
// hard to enforce these all being the same size
//setSigBuf("x");
//setSigBuf("y");
//setSigBuf("r");
setSignal("x", 5);
setSignal("y", 5);
setSignal("radius", 50);
setSignal("r", 255);
setSignal("g", 255);
setSignal("b", 255);
setSignal("thickness", -1);
}
bool Circle::update()
{
if (!Node::update()) return false;
// if any inputs have changed this will go on to draw
const bool id1 = isDirty(this,30);
//LOG(INFO) << id1;
if (!id1) {
return true;
}
const int wd = Config::inst()->im_width;
const int ht = Config::inst()->im_height;
cv::Mat out = cv::Mat(Config::inst()->getImSize(),
MAT_FORMAT_C3);
out = cv::Scalar(0,0,0,0);
cv::circle(out,
cv::Point(wd*getSignal("x")/10.0, ht*getSignal("y")/10.0),
getSignal("radius")*wd,
cv::Scalar(
getSignal("b"),
getSignal("g"),
getSignal("r")),
getSignal("thickness"));
setImage("out", out);
// clear this isDirty in advance of next loop
const bool id2 = isDirty(this,30);
const bool id3 = isDirty(this,30);
//LOG(INFO) << id2 << " " << id3;
}
//////////////////////////////////////////////////
Noise::Noise(const std::string name) : ImageNode(name)
{
setSignal("mean", 10);
setSignal("stddev", 128);
}
bool Noise::update()
{
if (!Node::update()) return false;
cv::Mat out = cv::Mat(Config::inst()->getImSize(), MAT_FORMAT_C3);
int type = (int)getSignal("type");
if (type == 0) {
// uniform
cv::randu(out, cv::Scalar(0,0,0,0), cv::Scalar(255,255,255,255));
} else {
int mean = getSignal("mean");
int stddev = getSignal("stddev");
// TBD handle alpha channel
cv::randn(out, cv::Scalar(mean,mean,mean,mean), cv::Scalar(stddev,stddev,stddev,stddev));
}
setImage("out", out);
}
SimplexNoise::SimplexNoise(const std::string name) : ImageNode(name)
{
//setSignal("octaves", 2, false, SATURATE, 1, 20);
//setSignal("persist", 0.8);//, false, SATURATE, 0.0, 1.0);
cv::Mat tmp;
setImage("dx", tmp, true);
setImage("dy", tmp, true);
setImage("dz", tmp, true);
setSignal("type", 0, false, ROLL, 0, 8);
setSignal("wrap_col", 0, false, ROLL, 0, 1);
setSignal("scale", 0.02);
setSignal("z", 0.0);
setSignal("t", 0.0);
setSignal("off_x_nrm", 0.0);
setSignal("off_y_nrm", 0.0);
setSignal("scale_v", 127.0);
setSignal("off_v", 127.0);
setSignal("t", 0); //, true); // TBD why not have ability to source t?
}
bool SimplexNoise::update()
{
if (!Node::update()) return false;
// if any inputs have changed this will go on to draw
const bool id1 = isDirty(this,30);
//LOG(INFO) << id1;
if (!id1) {
return true;
}
cv::Mat out = cv::Mat(Config::inst()->getImSize(), MAT_FORMAT_C3);
cv::Mat dx_im = out.clone();
cv::Mat dy_im = out.clone();
cv::Mat dz_im = out.clone();
//const float octaves = getSignal("octaves");
//const float persist = getSignal("persist");
const float scale = getSignal("scale");
const float off_x_nrm = getSignal("off_x_nrm");// * out.cols;
const float off_y_nrm = getSignal("off_y_nrm");// * out.rows;
const float off_z = getSignal("z");// * out.rows;
const float t = getSignal("t") * M_PI;// * out.rows;
const float scale_v = getSignal("scale_v");// * out.rows;
const float off_v = getSignal("off_v");// * out.rows;
const int mode = getSignal("type");
const bool wrap_col = getSignal("wrap_col");
// TBD decimation parameter to shrink number of pixels that the noise
// needs to be found for, resize at end of update.
for (int i = 0; i < out.rows; i++) {
for (int j = 0; j < out.cols; j++) {
// TBD scale will always zoom in and out of the center 0,0, not the center
// of the current view if off_x and off_y non-zero
const float x = (j - out.cols/2)*scale + off_x_nrm;
const float y = (i - out.rows/2)*scale + off_y_nrm;
const float z = off_z;
float val, dx, dy, dz;
if (mode == 0) {
// noise 1 will be a signal
val = noise2(x, y);
} else if (mode == 1) {
val = noise3(x, y, z);
} else if (mode == 2) {
val = noise4(x, y, z, t);
// TBD pnoise (periodic)
} else if (mode == 2) {
val = snoise2(x, y); // TBD is this faster than snoise3? otherwise get rid of it
} else if (mode == 3) {
val = snoise3(x, y, z);
} else if (mode == 4) {
val = snoise4(x, y, z, t);
// TBD the dx,dy,dz out of these seems empty
} else if (mode == 5) {
val = sdnoise2(x, y, &dx, &dy);
} else if (mode == 6) {
val = sdnoise3(x, y, z, &dx, &dy, &dz);
// TBD sdnoise4?
} else if (mode == 7) {
val = srdnoise2(x, y, t, &dx, &dy);
} else if (mode == 8) {
val = srdnoise3(x, y, z, t, &dx, &dy, &dz);
} else {
val = 0;
}
// TBD srdnoise4
val = val * scale_v + off_v;
// TBD these probably deserve separate scaling
dx = dx * scale_v + off_v;
dy = dy * scale_v + off_v;
dz = dz * scale_v + off_v;
// TBD rollover can be interesting, but saturate for now
if (wrap_col) {
if (val > 255) val = 255;
if (val < 0) val = 0;
if (dx > 255) dx = 255;
if (dx < 0) dx = 0;
if (dy > 255) dy = 255;
if (dy < 0) dy = 0;
if (dz > 255) dz = 255;
if (dz < 0) dz = 0;
}
//float val = marble_noise_2d(octaves, persist, scale, j + off_x_nrm, i + off_y_nrm)*127+127;
cv::Vec4b col = cv::Vec4b(val, val, val, 0);
out.at<cv::Vec4b>(i,j) = col;
if (mode >= 5) {
// TBD this is somewhat wasteful, could combine all dx,dy,dz into rgb channels
col = cv::Vec4b(dx, dx, dx, 0);
dx_im.at<cv::Vec4b>(i, j) = col;
col = cv::Vec4b(dy, dy, dy, 0);
dy_im.at<cv::Vec4b>(i, j) = col;
col = cv::Vec4b(dz, dz, dz, 0);
dz_im.at<cv::Vec4b>(i, j) = col;
}
}}
setImage("out", out);
if (mode >= 5) {
setImage("dx", dx_im);
setImage("dy", dy_im);
setImage("dz", dz_im);
}
}
} // namespace bm