/
General.cpp
1015 lines (816 loc) · 29.7 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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#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"
#include "patch/PatchIterators.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();
}
}
// Adapter method, since the ODRadiant patches allows accessing the patch controls
// with row and col indices outside the control array bounds, with wrap around behaviour
inline PatchControl& getControl(IPatch& patch, int row, int col)
{
if (col < 0 || col >= patch.getWidth() || row < 0 || row >= patch.getHeight())
{
return patch.ctrlAt(row % patch.getHeight(), col % patch.getWidth());
}
return patch.ctrlAt(row, col);
}
PatchNodePtr createdMergedPatch(const PatchNodePtr& patchNode1, const PatchNodePtr& patchNode2,
int sourceCol1, int sourceCol2, int sourceRow1, int sourceRow2, bool invert1, bool invert2)
{
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++)
{
getControl(newPatch, h, out) = getControl(patch1, 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++)
{
getControl(newPatch, h, out) = getControl(patch2, 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++)
{
getControl(newPatch, h, out) = getControl(patch1, 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++)
{
getControl(newPatch, w, out) = getControl(patch2, 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++)
{
getControl(newPatch, out, w) = getControl(patch1, 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++)
{
getControl(newPatch, out, w) = getControl(patch2, 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++)
{
getControl(newPatch, out, w) = getControl(patch1, 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++)
{
getControl(newPatch, out, h) = getControl(patch2, in, col2);
in += invert1 ? -1 : 1;
}
out++;
}
}
}
newPatch.controlPointsChanged();
return newPatchNode;
}
namespace
{
// Returns true if <num> elements in the given sequences are equal
inline bool firstNItemsAreEqual(const PatchControlIterator& sequence1, const PatchControlIterator& sequence2, std::size_t num, double epsilon)
{
// If the iterators are invalid from the start, return false
if (!sequence1.isValid() || !sequence2.isValid())
{
rMessage() << "Sequences are not valid" << std::endl;
return false;
}
auto p1 = sequence1;
auto p2 = sequence2;
for (std::size_t n = 0; n < num && p1.isValid() && p2.isValid(); ++n, ++p1, ++p2)
{
rMessage() << "P1: " << p1->vertex << ", P2: " << p2->vertex << std::endl;
if (!p1->vertex.isEqual(p2->vertex, epsilon))
{
rMessage() << "Sequences are not equal" << std::endl;
return false;
}
}
rMessage() << "Sequences are equal" << std::endl;
return true;
}
// Copies all values from source to target, until the source sequence is depleted
// Returns the new position of the target iterator
inline void assignPatchControls(const PatchControlIterator& source, PatchControlIterator& target)
{
auto& t = target;
for (auto s = source; s.isValid() && t.isValid(); ++s, ++t)
{
t->vertex = s->vertex;
t->texcoord = s->texcoord;
}
}
}
scene::INodePtr createdMergedPatch(const PatchNodePtr& patchNode1, const PatchNodePtr& patchNode2)
{
constexpr double WELD_EPSILON = 0.001;
#if 0
// 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;
#endif
if (patchNode1->getParent() != patchNode2->getParent())
{
throw cmd::ExecutionFailure(_("Patches have different parent entities, cannot weld."));
}
auto& patch1 = patchNode1->getPatch();
auto& patch2 = patchNode2->getPatch();
enum EdgeLocation
{
Begin,
End,
};
enum EdgeType
{
Row,
Column,
};
// Construct edge iterators for the first patch
// Iterator, EdgeLocation, Edge Length
auto patch1FirstRow = std::make_tuple(SinglePatchRowIterator(patch1, 0), Begin, Row, patch1.getWidth());
auto patch1FirstCol = std::make_tuple(SinglePatchColumnIterator(patch1, 0), Begin, Column, patch1.getHeight());
auto patch1LastRow = std::make_tuple(SinglePatchRowIterator(patch1, patch1.getHeight() - 1), End, Row, patch1.getWidth());
auto patch1LastCol = std::make_tuple(SinglePatchColumnIterator(patch1, patch1.getWidth() - 1), End, Column, patch1.getHeight());
// We'll be comparing each of the above edges to all other four edges of the second patch
// and we're doing it in forward and backward iteration, so we're trying to orient patch 2
// such that the edge vertices are matching up with patch 1
auto patch2FirstRow = std::make_tuple(RowWisePatchIterator(patch2), Begin, patch2.getWidth());
auto patch2FirstCol = std::make_tuple(ColumnWisePatchIterator(patch2), Begin, patch2.getHeight());
auto patch2LastRow = std::make_tuple(RowWisePatchIterator(patch2, patch2.getHeight() - 1, 0), End, patch2.getWidth());
auto patch2LastCol = std::make_tuple(ColumnWisePatchIterator(patch2, patch2.getWidth() - 1, 0), End, patch2.getHeight());
auto patch2FirstRowReverse = std::make_tuple(RowWisePatchReverseIterator(patch2), Begin, patch2.getWidth());
auto patch2FirstColReverse = std::make_tuple(ColumnWisePatchReverseIterator(patch2), Begin, patch2.getHeight());
auto patch2LastRowReverse = std::make_tuple(RowWisePatchReverseIterator(patch2, patch2.getHeight() - 1, 0), End, patch2.getWidth());
auto patch2LastColReverse = std::make_tuple(ColumnWisePatchReverseIterator(patch2, patch2.getWidth() - 1, 0), End, patch2.getHeight());
std::vector<std::tuple<PatchControlIterator, EdgeLocation, EdgeType, std::size_t>> patch1Edges =
{ patch1FirstRow, patch1FirstCol, patch1LastRow, patch1LastCol };
std::vector<std::tuple<PatchControlIterator, EdgeLocation, std::size_t>> patch2Edges =
{
patch2FirstRow, patch2FirstCol, patch2LastRow, patch2LastCol,
patch2FirstRowReverse, patch2FirstColReverse, patch2LastRowReverse, patch2LastColReverse
};
for (const auto& patch1Edge : patch1Edges)
{
for (const auto& patch2Edge : patch2Edges)
{
if (std::get<3>(patch1Edge) != std::get<2>(patch2Edge))
{
continue; // length don't match
}
if (!firstNItemsAreEqual(std::get<0>(patch1Edge), std::get<0>(patch2Edge), std::get<2>(patch1Edge), WELD_EPSILON))
{
continue;
}
// Found a shared edge
rMessage() << "Found a shared edge" << std::endl;
// Expand the patch dimensions
std::size_t numNewRows = patch1.getHeight();
std::size_t numNewColumns = patch1.getWidth();
std::size_t numNewElements = patch2.getWidth() * patch2.getHeight() / std::get<2>(patch2Edge) - 1;
auto& dimensionToExpand = std::get<2>(patch1Edge) == Row ? numNewRows : numNewColumns;
dimensionToExpand += numNewElements;
auto newPatchNode = GlobalPatchModule().createPatch(patch1.subdivisionsFixed() ? PatchDefType::Def3 : PatchDefType::Def2);
auto& newPatch = std::dynamic_pointer_cast<IPatchNode>(newPatchNode)->getPatch();
newPatch.setDims(numNewColumns, numNewRows);
if (std::get<1>(patch1Edge) == Begin)
{
if (std::get<2>(patch1Edge) == Row)
{
RowWisePatchIterator target(newPatch);
RowWisePatchIterator source(patch1, patch1.getHeight() - 1, 1);
assignPatchControls(source, target);
assignPatchControls(std::get<0>(patch2Edge), target);
newPatch.controlPointsChanged();
return newPatchNode;
}
}
}
}
#if 0
rMessage() << "Comparing P1FirstRow to P2FirstRow" << std::endl;
if (isEqual(patch1FirstRow, patch2FirstRow, WELD_EPSILON))
{
auto newPatchNode = GlobalPatchModule().createPatch(patch1.subdivisionsFixed() ? PatchDefType::Def3 : PatchDefType::Def2);
auto& newPatch = std::dynamic_pointer_cast<IPatchNode>(newPatchNode)->getPatch();
newPatch.setDims(patch1.getWidth(), patch1.getHeight() + patch2.getHeight() - 1);
// prepend
// use rows of other patch
}
rMessage() << "Comparing P1FirstRow to P2FirstRowReverse" << std::endl;
if (isEqual(patch1FirstRow, patch2FirstRowReverse, WELD_EPSILON))
{
int i = 0;
}
rMessage() << "Comparing P1FirstRow to P2LastRow" << std::endl;
if (isEqual(patch1FirstRow, patch2LastRow, WELD_EPSILON))
{
int i = 0;
// prepend
// use rows of other patch from end
}
rMessage() << "Comparing P1FirstRow to P2LastRowReverse" << std::endl;
if (isEqual(patch1FirstRow, patch2LastRowReverse, WELD_EPSILON))
{
int i = 0;
}
rMessage() << "Comparing P1LastRow to P2FirstRow" << std::endl;
if (isEqual(patch1LastRow, patch2FirstRow, WELD_EPSILON))
{
auto sceneNode = GlobalPatchModule().createPatch(patch1.subdivisionsFixed() ? PatchDefType::Def3 : PatchDefType::Def2);
auto& newPatch = std::dynamic_pointer_cast<IPatchNode>(sceneNode)->getPatch();
newPatch.setDims(patch1.getWidth(), patch1.getHeight() + patch2.getHeight() - 1);
RowWisePatchIterator p1(patch1);
RowWisePatchIterator p2(patch2, 1, patch2.getWidth() - 1); // skip the first row of the second patch
RowWisePatchIterator target(newPatch);
assignPatchControls(p1, target);
assignPatchControls(p2, target);
newPatch.controlPointsChanged();
return sceneNode;
}
rMessage() << "Comparing P1LastRow to P2FirstRowReverse" << std::endl;
if (isEqual(patch1LastRow, patch2FirstRowReverse, WELD_EPSILON))
{
int i = 6;
}
rMessage() << "Comparing P1LastRow to P2LastRow" << std::endl;
if (isEqual(patch1LastRow, patch2LastRow, WELD_EPSILON))
{
int i = 0;
// append
// use rows of other patch from end
}
rMessage() << "Comparing P1LastRow to P2LastRowReverse" << std::endl;
if (isEqual(patch1LastRow, patch2LastRowReverse, WELD_EPSILON))
{
int i = 6;
}
#endif
#if 0
if (patch1.getWidth() == patch2.getWidth())
{
row1 = 0;
row2 = 0;
while (true)
{
match = false;
col1 = 0;
col2 = 0;
adj1 = 0;
adj2 = 0;
if (getControl(patch1, row1, 0).vertex.isEqual(getControl(patch2, row2, 0).vertex, WELD_EPSILON))
{}
else if (getControl(patch1, row1, 0).vertex.isEqual(getControl(patch2, patch2.getWidth() - 1, row2).vertex, WELD_EPSILON))
{
col2 = patch2.getWidth() - 1;
adj2 = -1;
}
else if (getControl(patch1, patch1.getWidth() - 1, row1).vertex.isEqual(getControl(patch2, patch2.getWidth() - 1, row2).vertex, WELD_EPSILON))
{
col2 = patch2.getWidth() - 1;
adj2 = -1;
col1 = patch1.getWidth() - 1;
adj1 = -1;
}
else if (getControl(patch1, patch1.getWidth() - 1, row1).vertex.isEqual(getControl(patch2, 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 (!getControl(patch1, row1, col1).vertex.isEqual(getControl(patch2, 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;
}
}
}
#endif
return scene::INodePtr();
#if 0
if (patch1.getWidth() == patch2.getHeight())
{
row1 = 0;
col2 = 0;
while (1)
{
match = false;
col1 = 0;
adj1 = 1;
row2 = 0;
adj2 = 1;
if (getControl(patch1, row1, 0).vertex.isEqual(getControl(patch2, 0, col2).vertex, WELD_EPSILON))
{}
else if (getControl(patch1, row1, 0).vertex.isEqual(getControl(patch2, patch2.getHeight() - 1, col2).vertex, WELD_EPSILON))
{
row2 = patch2.getHeight() - 1;
adj2 = -1;
}
else if (getControl(patch1, row1, patch1.getWidth() - 1).vertex.isEqual(getControl(patch2, patch2.getHeight() - 1, col2).vertex, WELD_EPSILON))
{
row2 = patch2.getHeight() - 1;
adj2 = -1;
col1 = patch1.getWidth() - 1;
adj1 = -1;
}
else if (getControl(patch1, row1, patch1.getWidth() - 1).vertex.isEqual(getControl(patch2, 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 (!getControl(patch1, row1, col1).vertex.isEqual(getControl(patch2, 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 (getControl(patch1, 0, col1).vertex.isEqual(getControl(patch2, row2, 0).vertex, WELD_EPSILON))
{}
else if (getControl(patch1, 0, col1).vertex.isEqual(getControl(patch2, patch2.getWidth() - 1, row2).vertex, WELD_EPSILON))
{
col2 = patch2.getWidth() - 1;
adj2 = -1;
}
else if (getControl(patch1, patch1.getHeight() - 1, col1).vertex.isEqual(getControl(patch2, patch2.getWidth() - 1, row2).vertex, WELD_EPSILON))
{
col2 = patch2.getWidth() - 1;
adj2 = -1;
row1 = patch2.getHeight() - 1;
adj1 = -1;
}
else if (getControl(patch1, patch1.getHeight() - 1, col1).vertex.isEqual(getControl(patch2, 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 (!getControl(patch1, row1, col1).vertex.isEqual(getControl(patch2, 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 (getControl(patch1, 0, col1).vertex.isEqual(getControl(patch2, 0, col2).vertex, WELD_EPSILON))
{}
else if (getControl(patch1, 0, col1).vertex.isEqual(getControl(patch2, patch2.getHeight() - 1, col2).vertex, WELD_EPSILON))
{
row2 = patch2.getHeight() - 1;
adj2 = -1;
}
else if (getControl(patch1, patch1.getHeight() - 1, col1).vertex.isEqual(getControl(patch2, patch2.getHeight() - 1, col2).vertex, WELD_EPSILON))
{
row2 = patch2.getHeight() - 1;
adj2 = -1;
row1 = patch1.getHeight() - 1;
adj1 = -1;
}
else if (getControl(patch1, patch1.getHeight() - 1, col1).vertex.isEqual(getControl(patch2, 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 (!getControl(patch1, row1, col1).vertex.isEqual(getControl(patch2, 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;
}
}
}
#endif
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);
if (!mergedPatch)
{
throw cmd::ExecutionFailure(_("Failed to weld patches."));
}
patchNode1->getParent()->addChildNode(mergedPatch);
mergedPatch->assignToLayers(patchNode1->getLayers());
// TODO: selection grouping
std::dynamic_pointer_cast<IPatchNode>(mergedPatch)->getPatch().scaleTextureNaturally();
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;
}