forked from emilk/sproxel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.cpp
651 lines (535 loc) · 15.9 KB
/
Tools.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include "Tools.h"
#include "RayWalk.h"
#include "GLModelWidget.h"
////////////////////////////////////////
void SplatToolState::execute()
{
p_undoManager->beginMacro("Splat");
std::vector<Imath::V3i> voxels = voxelsAffected();
for (size_t i = 0; i < voxels.size(); i++)
{
p_undoManager->setVoxelColor(p_gvg, voxels[i], m_color, m_index);
}
p_undoManager->endMacro();
decrementClicks();
}
std::vector<Imath::V3i> SplatToolState::voxelsAffected()
{
std::vector<Imath::V3i> voxels;
// Intersect and check
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0)
return voxels;
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
// Hit a voxel at the close edge of the grid? Abort.
if (i == 0)
break;
// Hit a voxel in the middle? Return previous voxel.
voxels.push_back(intersects[i-1]);
break;
}
// Didn't hit anything? Just return the last voxel.
if (i == intersects.size()-1)
{
voxels.push_back(intersects[i]);
break;
}
}
return voxels;
}
////////////////////////////////////////
void FloodToolState::execute()
{
std::vector<Imath::V3i> voxels = voxelsAffected();
if (voxels.size() == 0) return;
const Imath::V3i& hit = voxels[0];
// Get the color we're replacing.
const Imath::Color4f repColor = p_gvg->get(hit);
// Die early if there's nothing to do
if (repColor == m_color)
return;
// Recurse
p_undoManager->beginMacro("Flood Fill");
p_undoManager->setVoxelColor(p_gvg, hit, m_color, m_index);
setNeighborsRecurse(hit, repColor, m_color, m_index);
p_undoManager->endMacro();
decrementClicks();
}
void FloodToolState::setNeighborsRecurse(const Imath::V3i& alreadySet,
const Imath::Color4f& repColor,
const Imath::Color4f& newColor,
int newIndex)
{
// Directions
Imath::V3i doUs[6];
doUs[0] = Imath::V3i(alreadySet.x+1, alreadySet.y, alreadySet.z);
doUs[3] = Imath::V3i(alreadySet.x-1, alreadySet.y, alreadySet.z);
doUs[1] = Imath::V3i(alreadySet.x, alreadySet.y+1, alreadySet.z);
doUs[4] = Imath::V3i(alreadySet.x, alreadySet.y-1, alreadySet.z);
doUs[2] = Imath::V3i(alreadySet.x, alreadySet.y, alreadySet.z+1);
doUs[5] = Imath::V3i(alreadySet.x, alreadySet.y, alreadySet.z-1);
for (int i = 0; i < 6; i++)
{
// Bounds protection
if (!p_gvg->bounds().intersects(doUs[i])) continue;
// Recurse
if (p_gvg->get(doUs[i]) == repColor)
{
p_undoManager->setVoxelColor(p_gvg, doUs[i], newColor, newIndex);
setNeighborsRecurse(doUs[i], repColor, newColor, newIndex);
}
}
}
std::vector<Imath::V3i> FloodToolState::voxelsAffected()
{
// TODO: It may make the most sense to recurse in here, but it could be slow
std::vector<Imath::V3i> voxels;
// Intersect and check
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0)
return voxels;
// Get the first voxel hit
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
voxels.push_back(intersects[i]);
break;
}
}
return voxels;
}
////////////////////////////////////////
void EraserToolState::execute()
{
p_undoManager->beginMacro("Eraser");
std::vector<Imath::V3i> voxels = voxelsAffected();
for (size_t i = 0; i < voxels.size(); i++)
{
p_undoManager->setVoxelColor(p_gvg, voxels[i],
Imath::Color4f(0.0f, 0.0f, 0.0f, 0.0f), 0);
}
p_undoManager->endMacro();
decrementClicks();
}
std::vector<Imath::V3i> EraserToolState::voxelsAffected()
{
std::vector<Imath::V3i> voxels;
// Intersect and check
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0)
return voxels;
// Get the first voxel hit
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
voxels.push_back(intersects[i]);
break;
}
}
return voxels;
}
////////////////////////////////////////
void ReplaceToolState::execute()
{
p_undoManager->beginMacro("Replace");
std::vector<Imath::V3i> voxels = voxelsAffected();
for (size_t i = 0; i < voxels.size(); i++)
{
// Don't replace if you're already identical
if (p_gvg->get(voxels[i]) != m_color)
p_undoManager->setVoxelColor(p_gvg, voxels[i], m_color, m_index);
}
p_undoManager->endMacro();
decrementClicks();
}
std::vector<Imath::V3i> ReplaceToolState::voxelsAffected()
{
std::vector<Imath::V3i> voxels;
// Intersect and check
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0)
return voxels;
// Get the first voxel hit
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
voxels.push_back(intersects[i]);
break;
}
}
return voxels;
}
////////////////////////////////////////
void RayToolState::execute()
{
std::vector<Imath::V3i> voxels = voxelsAffected();
p_undoManager->beginMacro("Ray Blast");
for (size_t i = 0; i < voxels.size(); i++)
{
p_undoManager->setVoxelColor(p_gvg, voxels[i], m_color, m_index);
}
p_undoManager->endMacro();
decrementClicks();
}
std::vector<Imath::V3i> RayToolState::voxelsAffected()
{
return rayIntersection(m_ray);
}
////////////////////////////////////////
void SlabToolState::execute()
{
std::vector<Imath::V3i> voxels = voxelsAffected();
switch (m_workingAxis)
{
case X_AXIS: p_undoManager->beginMacro("Fill X Slice"); break;
case Y_AXIS: p_undoManager->beginMacro("Fill Y Slice"); break;
case Z_AXIS: p_undoManager->beginMacro("Fill Z Slice"); break;
}
for (size_t i = 0; i < voxels.size(); i++)
{
p_undoManager->setVoxelColor(p_gvg, voxels[i], m_color, m_index);
}
p_undoManager->endMacro();
decrementClicks();
}
std::vector<Imath::V3i> SlabToolState::voxelsAffected()
{
std::vector<Imath::V3i> voxels;
// TODO: Should this do an intersection at all, or maybe just fill in the
// row based on the first hit?
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0)
return voxels;
// Get the position to fill from
Imath::V3i fillPos=intersects[0];
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
// Hit a voxel at the near edge of the grid? Start there.
if (i == 0)
{
fillPos = intersects[0];
break;
}
else
{
// Hit a voxel in the middle?
fillPos = intersects[i-1];
break;
}
}
}
// Fill out the slab
Imath::Box3i dim=p_gvg->bounds();
switch (m_workingAxis)
{
case X_AXIS:
for (int y=dim.min.y; y <= dim.max.y; y++)
{
for (int z=dim.min.z; z <= dim.max.z; z++)
{
voxels.push_back(Imath::V3i(fillPos.x, y, z));
}
}
break;
case Y_AXIS:
for (int x=dim.min.x; x <= dim.max.x; x++)
{
for (int z=dim.min.z; z <= dim.max.z; z++)
{
voxels.push_back(Imath::V3i(x, fillPos.y, z));
}
}
break;
case Z_AXIS:
for (int x=dim.min.x; x <= dim.max.x; x++)
{
for (int y=dim.min.y; y <= dim.max.y; y++)
{
voxels.push_back(Imath::V3i(x, y, fillPos.z));
}
}
break;
}
return voxels;
}
////////////////////////////////////////
void LineToolState::execute()
{
std::vector<Imath::V3i> voxels = voxelsAffected();
// First click sets the start point
if (m_clicksRemain == 2)
{
if (voxels.size())
{
m_startPoint = voxels[0];
decrementClicks();
}
}
// Second click fills in the line
else if (m_clicksRemain == 1)
{
p_undoManager->beginMacro("Line");
for (size_t i = 0; i < voxels.size(); i++)
{
p_undoManager->setVoxelColor(p_gvg, voxels[i], m_color, m_index);
}
p_undoManager->endMacro();
decrementClicks();
}
}
std::vector<Imath::V3i> LineToolState::voxelsAffected()
{
std::vector<Imath::V3i> voxels;
// Intersect and check
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0)
return voxels;
Imath::V3i intersect;
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
// Hit a voxel at the close edge of the grid? Abort.
if (i == 0)
return voxels;
// Hit a voxel in the middle? Return previous voxel.
intersect = intersects[i-1];
break;
}
// Didn't hit anything? Just return the last voxel.
if (i == intersects.size()-1)
{
intersect = intersects[i];
break;
}
}
if (m_clicksRemain == 2)
{
voxels.push_back(intersect);
}
else if (m_clicksRemain == 1)
{
Imath::Line3d ray;
ray.pos = p_gvg->voxelTransform(m_startPoint).translation();
ray.dir = (p_gvg->voxelTransform(intersect).translation() - ray.pos).normalized();
if (ray.pos == intersect)
{
// An infinitely small ray is bad
voxels.push_back(intersect);
}
else
{
// Trim off the end, making it a ray segment instead of an entire ray
std::vector<Imath::V3i> initialInts = rayIntersection(ray);
for (size_t i = 0; i < initialInts.size(); i++)
{
voxels.push_back(initialInts[i]);
if (initialInts[i] == intersect)
break;
}
}
}
return voxels;
}
////////////////////////////////////////
void BoxToolState::execute()
{
std::vector<Imath::V3i> voxels = voxelsAffected();
// First click sets the start point
if (m_clicksRemain == 2)
{
if (voxels.size())
{
m_startPoint = voxels[0];
decrementClicks();
}
}
// Second click fills in the box
else if (m_clicksRemain == 1)
{
p_undoManager->beginMacro("Box");
for (size_t i = 0; i < voxels.size(); i++)
{
p_undoManager->setVoxelColor(p_gvg, voxels[i], m_color, m_index);
}
p_undoManager->endMacro();
decrementClicks();
}
}
std::vector<Imath::V3i> BoxToolState::voxelsAffected()
{
std::vector<Imath::V3i> voxels;
// Intersect and check
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0)
return voxels;
Imath::V3i intersect;
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
// Hit a voxel at the close edge of the grid? Abort.
if (i == 0)
return voxels;
// Hit a voxel in the middle? Return previous voxel.
intersect = intersects[i-1];
break;
}
// Didn't hit anything? Just return the last voxel.
if (i == intersects.size()-1)
{
intersect = intersects[i];
break;
}
}
if (m_clicksRemain == 2)
{
voxels.push_back(intersect);
}
else if (m_clicksRemain == 1)
{
Imath::Box3i box(m_startPoint);
box.extendBy(intersect);
for (int z=box.min.z; z<=box.max.z; ++z)
for (int y=box.min.y; y<=box.max.y; ++y)
for (int x=box.min.x; x<=box.max.x; ++x)
voxels.push_back(Imath::V3i(x, y, z));
}
return voxels;
}
////////////////////////////////////////
void ExtrudeToolState::execute()
{
doExtrude(false);
}
void ExtrudeToolState::executeErase()
{
doExtrude(true);
}
void ExtrudeToolState::doExtrude(bool is_erase)
{
std::vector<Imath::V3i> voxels = voxelsAffected();
p_undoManager->beginMacro(is_erase?"Extrude erase":"Extrude");
if (is_erase)
{
for (size_t i=0; i<voxels.size(); i++)
{
Imath::V3i sp=voxels[i]-m_dir;
p_undoManager->setVoxelColor(p_gvg, sp, SproxelColor(0, 0, 0, 0), 0);
}
}
else
{
for (size_t i=0; i<voxels.size(); i++)
{
Imath::V3i sp=voxels[i]-m_dir;
SproxelColor color=p_gvg->get(sp);
int index=p_gvg->getInd(sp);
p_undoManager->setVoxelColor(p_gvg, voxels[i], color, index);
}
}
p_undoManager->endMacro();
decrementClicks();
}
void ExtrudeToolState::fillExtrudeMap(GameVoxelGrid<char> &map, const Imath::V3i &mapOfs,
const Imath::V3i &pos, int axis)
{
Imath::V3i mp=pos+mapOfs;
if (mp.x<0 || mp.y<0 || mp.z<0) return;
const Imath::V3i &size=map.cellDimensions();
if (mp.x>=size.x || mp.y>=size.y || mp.z>=size.z) return;
if (map.get(mp)!=0) return; // already marked
if (p_gvg->get(pos).a!=0 || p_gvg->get(pos-m_dir).a==0)
{
// out of extruded area, mark and stop
map.set(mp, 2);
return;
}
int paxis=1, saxis=2;
if (axis==1) paxis=0;
else if (axis==2) saxis=0;
// mark area
map.set(mp, 1);
//== TODO: use linear fill instead, for less recursion depth
Imath::V3i newPos=pos;
newPos[paxis]--;
fillExtrudeMap(map, mapOfs, newPos, axis);
newPos[paxis]+=2;
fillExtrudeMap(map, mapOfs, newPos, axis);
newPos[paxis]--;
newPos[saxis]--;
fillExtrudeMap(map, mapOfs, newPos, axis);
newPos[saxis]+=2;
fillExtrudeMap(map, mapOfs, newPos, axis);
}
std::vector<Imath::V3i> ExtrudeToolState::voxelsAffected()
{
std::vector<Imath::V3i> voxels;
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0) return voxels;
// Get the position to fill from
Imath::V3i fillPos=intersects[0];
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
if (i==0) return voxels; // can't extrude beyond bounds
fillPos=intersects[i-1];
m_dir=fillPos-intersects[i];
break;
}
}
// determine axis
int axis=0;
if (m_dir.y!=0) axis=1;
else if (m_dir.z!=0) axis=2;
// allocate map
Imath::Box3i bounds=p_gvg->bounds();
Imath::V3i mapSize=bounds.size()+Imath::V3i(1);
mapSize[axis]=1;
Imath::V3i mapOfs=-bounds.min;
mapOfs[axis]=-fillPos[axis];
GameVoxelGrid<char> map(mapSize);
map.setAll(0);
fillExtrudeMap(map, mapOfs, fillPos, axis);
// get voxels from map
for (int z=0; z<mapSize.z; ++z)
for (int y=0; y<mapSize.y; ++y)
for (int x=0; x<mapSize.x; ++x)
{
if (map.get(Imath::V3i(x, y, z))!=1) continue;
voxels.push_back(Imath::V3i(x, y, z)-mapOfs);
}
return voxels;
}
////////////////////////////////////////
void DropperToolState::execute()
{
decrementClicks();
}
std::vector<Imath::V3i> DropperToolState::voxelsAffected()
{
std::vector<Imath::V3i> voxels;
// Intersect and check
std::vector<Imath::V3i> intersects = rayIntersection(m_ray);
if (intersects.size() == 0)
return voxels;
// Get the first voxel hit
for (size_t i = 0; i < intersects.size(); i++)
{
if (p_gvg->get(intersects[i]).a != 0.0f)
{
voxels.push_back(intersects[i]);
break;
}
}
return voxels;
}