/
General.cpp
802 lines (640 loc) · 21.4 KB
/
General.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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#include "General.h"
#include "i18n.h"
#include "imainframe.h"
#include "itextstream.h"
#include "ipatch.h"
#include "patch/PatchNode.h"
#include "patch/Patch.h"
#include "messages/TextureChanged.h"
#include "selection/algorithm/Primitives.h"
#include "string/convert.h"
#include "scenelib.h"
#include "command/ExecutionFailure.h"
namespace patch
{
namespace algorithm
{
void thicken(const PatchNodePtr& sourcePatch, float thickness, bool createSeams, int axis)
{
if (axis < 0 || axis > 3) throw cmd::ExecutionFailure(fmt::format(_("Invalid axis value: {0}"), string::to_string(axis)));
// Get a shortcut to the patchcreator
auto& patchCreator = GlobalPatchModule();
// Create a new patch node
scene::INodePtr node(patchCreator.createPatch(patch::PatchDefType::Def2));
scene::INodePtr parent = sourcePatch->getParent();
assert(parent != NULL);
// Insert the node into the same parent as the existing patch
parent->addChildNode(node);
// Retrieve the contained patch from the node
Patch* targetPatch = Node_getPatch(node);
// Create the opposite patch with the given thickness = distance
targetPatch->createThickenedOpposite(sourcePatch->getPatchInternal(), thickness, axis);
// Select the newly created patch
Node_setSelected(node, true);
if (createSeams && thickness > 0.0f)
{
// Allocate four new patches
scene::INodePtr nodes[4] = {
patchCreator.createPatch(patch::PatchDefType::Def2),
patchCreator.createPatch(patch::PatchDefType::Def2),
patchCreator.createPatch(patch::PatchDefType::Def2),
patchCreator.createPatch(patch::PatchDefType::Def2)
};
// Now create the four walls
for (int i = 0; i < 4; i++)
{
// Retrieve the contained patch from the node
Patch* wallPatch = Node_getPatch(nodes[i]);
// Create the wall patch by passing i as wallIndex
wallPatch->createThickenedWall(sourcePatch->getPatchInternal(), *targetPatch, i);
if (!wallPatch->isDegenerate())
{
// Insert each node into the same parent as the existing patch
// It's vital to do this first, otherwise these patches won't have valid shaders
parent->addChildNode(nodes[i]);
// Now the shader is realised, apply natural scale
wallPatch->scaleTextureNaturally();
// Now select the newly created patch
Node_setSelected(nodes[i], true);
}
else
{
rMessage() << "Thicken: Discarding degenerate patch." << std::endl;
}
}
}
// Invert the target patch so that it faces the opposite direction
targetPatch->invertMatrix();
}
void stitchTextures(const cmd::ArgumentList& args)
{
// Get all the selected patches
PatchPtrVector patchList = selection::algorithm::getSelectedPatches();
if (patchList.size() == 2)
{
UndoableCommand undo("patchStitchTexture");
// Fetch the instances from the selectionsystem
auto targetNode = GlobalSelectionSystem().ultimateSelected();
auto sourceNode = GlobalSelectionSystem().penultimateSelected();
// Cast the instances onto a patch
Patch* source = Node_getPatch(sourceNode);
Patch* target = Node_getPatch(targetNode);
if (source && target)
{
// Stitch the texture leaving the source patch intact
target->stitchTextureFrom(*source);
}
else
{
throw cmd::ExecutionFailure(_("Cannot stitch textures. \nCould not cast nodes to patches."));
}
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
else
{
throw cmd::ExecutionFailure(_("Cannot stitch patch textures. \nExactly 2 patches must be selected."));
}
}
void bulge(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rWarning() << "Usage: BulgePatch <maxNoiseAmplitude>" << std::endl;
return;
}
// Get the list of selected patches
PatchPtrVector patches = selection::algorithm::getSelectedPatches();
if (patches.empty())
{
throw cmd::ExecutionFailure(_("Cannot bulge patch. No patches selected."));
}
double maxValue = args[0].getDouble();
UndoableCommand cmd("BulgePatch");
// Cycle through all patches and apply the bulge algorithm
for (const PatchNodePtr& p : patches)
{
Patch& patch = p->getPatchInternal();
patch.undoSave();
for (PatchControl& control : patch)
{
int randomNumber = int(maxValue * (float(std::rand()) / float(RAND_MAX)));
control.vertex.set(control.vertex.x(), control.vertex.y(), control.vertex.z() + randomNumber);
}
patch.controlPointsChanged();
}
}
PatchNodePtr createdMergedPatch(const PatchNodePtr& patchNode1, const PatchNodePtr& patchNode2,
int sourceCol1, int sourceCol2, int sourceRow1, int sourceRow2, bool invert1, bool invert2)
{
float sScale, tScale;
float s, t;
int col1, col2;
int row1, row2;
int adj1, adj2;
int in, out;
int w, h;
auto& patch1 = patchNode1->getPatch();
auto& patch2 = patchNode2->getPatch();
auto node = GlobalPatchModule().createPatch(patch1.subdivisionsFixed() ? PatchDefType::Def3 : PatchDefType::Def2);
auto newPatchNode = std::dynamic_pointer_cast<PatchNode>(node);
auto& newPatch = newPatchNode->getPatch();
newPatch.setShader(patch1.getShader());
// Merge the patches
if (sourceCol1 >= 0)
{
// Adding width
if (sourceCol2 >= 0)
{
// From width
newPatch.setDims(patch1.getWidth() + patch2.getWidth() - 1, patch1.getHeight());
col1 = 0;
col2 = 1;
adj1 = 1;
adj2 = 1;
if (sourceCol1 != 0)
{
adj1 = -1;
col1 = patch1.getWidth() - 1;
}
if (sourceCol2 != 0)
{
adj2 = -1;
col2 = patch2.getWidth() - 2;
}
out = 0;
for (w = 0; w < patch1.getWidth(); w++, col1 += adj1)
{
in = invert1 ? patch1.getHeight() - 1 : 0;
for (h = 0; h < patch1.getHeight(); h++)
{
newPatch.ctrlAt(h, out) = patch1.ctrlAt(in, col1);
in += invert1 ? -1 : 1;
}
out++;
}
for (w = 1; w < patch2.getWidth(); w++, col2 += adj2)
{
in = invert2 ? patch2.getHeight() - 1 : 0;
for (h = 0; h < patch2.getHeight(); h++)
{
newPatch.ctrlAt(h, out) = patch2.ctrlAt(in, col2);
in += invert2 ? -1 : 1;
}
out++;
}
}
else
{
// From height
newPatch.setDims(patch1.getWidth() + patch2.getHeight() - 1, patch1.getHeight());
col1 = 0;
row2 = 1;
adj1 = 1;
adj2 = 1;
if (sourceCol1 != 0)
{
adj1 = -1;
col1 = patch1.getWidth() - 1;
}
if (sourceRow2 != 0)
{
adj2 = -1;
row2 = patch2.getHeight() - 2;
}
out = 0;
for (w = 0; w < patch1.getWidth(); w++, col1 += adj1)
{
in = invert1 ? patch1.getHeight() - 1 : 0;
for (h = 0; h < patch1.getHeight(); h++)
{
newPatch.ctrlAt(h, out) = patch1.ctrlAt(in, col1);
in += invert1 ? -1 : 1;
}
out++;
}
for (h = 1; h < patch2.getHeight(); h++, row2 += adj2)
{
in = invert2 ? patch2.getWidth() - 1 : 0;
for (w = 0; w < patch2.getWidth(); w++)
{
newPatch.ctrlAt(w, out) = patch2.ctrlAt(row2, in);
in += invert2 ? -1 : 1;
}
out++;
}
}
}
else
{
// Adding height
if (sourceRow1 >= 0)
{
// From height
newPatch.setDims(patch1.getWidth(), patch1.getHeight() + patch2.getHeight() - 1);
row1 = 0;
row2 = 0;
adj1 = 1;
adj2 = 1;
if (sourceRow1 != 0)
{
adj1 = -1;
row1 = patch1.getHeight() - 1;
}
if (sourceRow2 != 0)
{
adj2 = -1;
row2 = patch2.getHeight() - 2;
}
out = 0;
for (h = 0; h < patch1.getHeight(); h++, row1 += adj1)
{
in = invert1 ? patch1.getWidth() - 1 : 0;
for (w = 0; w < patch1.getWidth(); w++)
{
newPatch.ctrlAt(out, w) = patch1.ctrlAt(row1, in);
in += invert1 ? -1 : 1;
}
out++;
}
for (h = 1; h < patch2.getHeight(); h++, row2 += adj2)
{
in = invert2 ? patch1.getWidth() - 1 : 0;
for (w = 0; w < patch2.getWidth(); w++)
{
newPatch.ctrlAt(out, w) = patch2.ctrlAt(row2, in);
in += invert1 ? -1 : 1;
}
out++;
}
}
else
{
// From width
newPatch.setDims(patch1.getWidth() + patch2.getHeight() - 1, patch1.getHeight());
row1 = 0;
col2 = 0;
adj1 = 1;
adj2 = 1;
if (sourceRow1 != 0)
{
adj1 = -1;
row1 = patch1.getHeight() - 1;
}
if (sourceCol2 != 0)
{
adj2 = -1;
col2 = patch2.getWidth() - 2;
}
out = 0;
for (h = 0; h < patch1.getHeight(); h++, row1 += adj1)
{
in = invert1 ? patch1.getWidth() - 1 : 0;
for (w = 0; w < patch1.getWidth(); w++)
{
newPatch.ctrlAt(out, w) = patch1.ctrlAt(row1, in);
in += invert1 ? -1 : 1;
}
out++;
}
for (w = 1; w < patch2.getWidth(); w++, col2 += adj2)
{
in = invert2 ? patch1.getHeight() - 1 : 0;
for (h = 0; h < patch2.getHeight(); h++)
{
newPatch.ctrlAt(out, h) = patch2.ctrlAt(in, col2);
in += invert1 ? -1 : 1;
}
out++;
}
}
}
#if 0
// Generate the texture coords
GetTextureMappingScales(newPatch->material->editorImage, &sScale, &tScale);
for (w = 0; w < newPatch->width; w++) {
s = Patch_WidthDistanceTo(newPatch, w);
for (h = 0; h < newPatch->height; h++) {
t = Patch_HeightDistanceTo(newPatch, h);
newPatch.ctrlAt(h, w).st[0] = s * sScale;
newPatch.ctrlAt(h, w).st[1] = 1.0f - t * tScale;
}
}
#endif
newPatch.controlPointsChanged();
return newPatchNode;
}
PatchNodePtr createdMergedPatch(const PatchNodePtr& patchNode1, const PatchNodePtr& patchNode2)
{
constexpr double WELD_EPSILON = 0.001;
// Algorithm ported from ODRadiant https://svn.code.sf.net/p/odblur/code/code/OverDose%20Tools/ODRadiant/Patch.cpp) rev8348
int col, col1, col2;
int row, row1, row2;
int adj1, adj2;
bool match;
if (patchNode1->getParent() != patchNode2->getParent())
{
throw cmd::ExecutionFailure(_("Patches have different parent entities, cannot weld."));
}
auto& patch1 = patchNode1->getPatch();
auto& patch2 = patchNode2->getPatch();
if (patch1.getWidth() == patch2.getWidth())
{
row1 = 0;
row2 = 0;
while (true)
{
match = false;
col1 = 0;
col2 = 0;
adj1 = 0;
adj2 = 0;
if (patch1.ctrlAt(row1, 0).vertex.isEqual(patch2.ctrlAt(row2, 0).vertex, WELD_EPSILON))
{}
else if (patch1.ctrlAt(row1, 0).vertex.isEqual(patch2.ctrlAt(patch2.getWidth() - 1, row2).vertex, WELD_EPSILON))
{
col2 = patch2.getWidth() - 1;
adj2 = -1;
}
else if (patch1.ctrlAt(patch1.getWidth() - 1, row1).vertex.isEqual(patch2.ctrlAt(patch2.getWidth() - 1, row2).vertex, WELD_EPSILON))
{
col2 = patch2.getWidth() - 1;
adj2 = -1;
col1 = patch1.getWidth() - 1;
adj1 = -1;
}
else if (patch1.ctrlAt(patch1.getWidth() - 1, row1).vertex.isEqual(patch2.ctrlAt(row2, 0).vertex, WELD_EPSILON))
{
col1 = patch1.getWidth() - 1;
adj1 = -1;
}
else
{
adj1 = 0;
}
if (adj1)
{
match = true;
for (col = 0; col < patch1.getWidth(); col++, col2 += adj2, col1 += adj1)
{
if (!patch1.ctrlAt(row1, col1).vertex.isEqual(patch2.ctrlAt(row2, col2).vertex, WELD_EPSILON))
{
match = false;
break;
}
}
}
if (match)
{
rMessage() << "Welding row " << row1 << " with row " << row2 << std::endl;
row1 = (row1 == 0) ? patch1.getHeight() - 1 : 0;
return createdMergedPatch(patchNode1, patchNode2, -1, -1, row1, row2, (adj1 == -1), (adj2 == -1));
}
if (row2 == 0)
{
row2 = patch2.getHeight() - 1;
}
else if (row1 == 0)
{
row1 = patch1.getHeight() - 1;
row2 = 0;
}
else
{
break;
}
}
}
if (patch1.getWidth() == patch2.getHeight())
{
row1 = 0;
col2 = 0;
while (1)
{
match = false;
col1 = 0;
adj1 = 1;
row2 = 0;
adj2 = 1;
if (patch1.ctrlAt(row1, 0).vertex.isEqual(patch2.ctrlAt(0, col2).vertex, WELD_EPSILON))
{}
else if (patch1.ctrlAt(row1, 0).vertex.isEqual(patch2.ctrlAt(patch2.getHeight() - 1, col2).vertex, WELD_EPSILON))
{
row2 = patch2.getHeight() - 1;
adj2 = -1;
}
else if (patch1.ctrlAt(row1, patch1.getWidth() - 1).vertex.isEqual(patch2.ctrlAt(patch2.getHeight() - 1, col2).vertex, WELD_EPSILON))
{
row2 = patch2.getHeight() - 1;
adj2 = -1;
col1 = patch1.getWidth() - 1;
adj1 = -1;
}
else if (patch1.ctrlAt(row1, patch1.getWidth() - 1).vertex.isEqual(patch2.ctrlAt(0, col2).vertex, WELD_EPSILON))
{
col1 = patch1.getWidth() - 1;
adj1 = -1;
}
else
{
adj1 = 0;
}
if (adj1)
{
match = true;
for (col = 0; col < patch1.getWidth(); col++, col1 += adj1, row2 += adj2)
{
if (!patch1.ctrlAt(row1, col1).vertex.isEqual(patch2.ctrlAt(row2, col2).vertex, WELD_EPSILON))
{
match = false;
break;
}
}
}
if (match)
{
rMessage() << "Welding row " << row1 << " with column " << col2 << std::endl;
row1 = (row1 == 0) ? patch1.getHeight() - 1 : 0;
return createdMergedPatch(patchNode1, patchNode2, -1, col2, row1, -1, (adj1 == -1), (adj2 == -1));
}
if (col2 == 0)
{
col2 = patch2.getWidth() - 1;
}
else if (row1 == 0)
{
row1 = patch1.getHeight() - 1;
col2 = 0;
}
else
{
break;
}
}
}
if (patch1.getHeight() == patch2.getWidth())
{
col1 = 0;
row2 = 0;
while (1)
{
match = false;
row1 = 0;
adj1 = 1;
col2 = 0;
adj2 = 1;
if (patch1.ctrlAt(0, col1).vertex.isEqual(patch2.ctrlAt(row2, 0).vertex, WELD_EPSILON))
{}
else if (patch1.ctrlAt(0, col1).vertex.isEqual(patch2.ctrlAt(patch2.getWidth() - 1, row2).vertex, WELD_EPSILON))
{
col2 = patch2.getWidth() - 1;
adj2 = -1;
}
else if (patch1.ctrlAt(patch1.getHeight() - 1, col1).vertex.isEqual(patch2.ctrlAt(patch2.getWidth() - 1, row2).vertex, WELD_EPSILON))
{
col2 = patch2.getWidth() - 1;
adj2 = -1;
row1 = patch2.getHeight() - 1;
adj1 = -1;
}
else if (patch1.ctrlAt(patch1.getHeight() - 1, col1).vertex.isEqual(patch2.ctrlAt(row2, 0).vertex, WELD_EPSILON))
{
row1 = patch2.getHeight() - 1;
adj2 = -1;
}
else
{
adj1 = 0;
}
if (adj1)
{
match = true;
for (row = 0; row < patch1.getHeight(); row++, row1 += adj1, col2 += adj2)
{
if (!patch1.ctrlAt(row1, col1).vertex.isEqual(patch2.ctrlAt(row2, col2).vertex, WELD_EPSILON))
{
match = false;
break;
}
}
}
if (match)
{
rMessage() << "Welding column " << col1 << " with row " << row2 << std::endl;
col1 = (col1 == 0) ? patch1.getWidth() - 1 : 0;
return createdMergedPatch(patchNode1, patchNode2, col1, -1, -1, row2, (adj1 == -1), (adj2 == -1));
}
if (row2 == 0)
{
row2 = patch2.getHeight() - 1;
}
else if (col1 == 0)
{
col1 = patch1.getWidth() - 1;
row2 = 0;
}
else
{
break;
}
}
}
if (patch1.getHeight() == patch2.getHeight())
{
col1 = 0;
col2 = 0;
while (1)
{
match = false;
row1 = 0;
adj1 = 1;
row2 = 0;
adj2 = 1;
if (patch1.ctrlAt(0, col1).vertex.isEqual(patch2.ctrlAt(0, col2).vertex, WELD_EPSILON))
{}
else if (patch1.ctrlAt(0, col1).vertex.isEqual(patch2.ctrlAt(patch2.getHeight() - 1, col2).vertex, WELD_EPSILON))
{
row2 = patch2.getHeight() - 1;
adj2 = -1;
}
else if (patch1.ctrlAt(patch1.getHeight() - 1, col1).vertex.isEqual(patch2.ctrlAt(patch2.getHeight() - 1, col2).vertex, WELD_EPSILON))
{
row2 = patch2.getHeight() - 1;
adj2 = -1;
row1 = patch1.getHeight() - 1;
adj1 = -1;
}
else if (patch1.ctrlAt(patch1.getHeight() - 1, col1).vertex.isEqual(patch2.ctrlAt(0, col2).vertex, WELD_EPSILON))
{
row1 = patch1.getHeight() - 1;
adj1 = -1;
}
else
{
adj1 = 0;
}
if (adj1)
{
match = true;
for (row = 0; row < patch1.getHeight(); row++, row1 += adj1, row2 += adj2)
{
if (!patch1.ctrlAt(row1, col1).vertex.isEqual(patch2.ctrlAt(row2, col2).vertex, WELD_EPSILON))
{
match = false;
break;
}
}
}
if (match)
{
rMessage() << "Welding column " << col1 << " with column " << col2 << std::endl;
col1 = (col1 == 0) ? patch1.getWidth() - 1 : 0;
return createdMergedPatch(patchNode1, patchNode2, col1, col2, -1, -1, (adj1 == -1), (adj2 == -1));
}
if (col2 == 0)
{
col2 = patch2.getWidth() - 1;
}
else if (col1 == 0)
{
col1 = patch1.getWidth() - 1;
col2 = 0;
}
else
{
break;
}
}
}
throw cmd::ExecutionFailure(_("Unable to weld patches, no suitable edges of same length found"));
}
void weldPatches(const PatchNodePtr& patchNode1, const PatchNodePtr& patchNode2)
{
auto mergedPatch = createdMergedPatch(patchNode1, patchNode2);
assert(mergedPatch);
patchNode1->getParent()->addChildNode(mergedPatch);
mergedPatch->assignToLayers(patchNode1->getLayers());
// TODO: selection grouping
Node_setSelected(mergedPatch, true);
scene::removeNodeFromParent(patchNode1);
scene::removeNodeFromParent(patchNode2);
}
void weldSelectedPatches(const cmd::ArgumentList& args)
{
if (args.size() > 0)
{
rWarning() << "Usage: WeldSelectedPatches" << std::endl;
return;
}
// Get the list of selected patches
auto patches = selection::algorithm::getSelectedPatches();
if (patches.size() != 2)
{
throw cmd::ExecutionFailure(_("Cannot weld patches, select two patches to weld them."));
}
UndoableCommand cmd("WeldSelectedPatches");
weldPatches(patches[0], patches[1]);
}
} // namespace
} // namespace