-
Notifications
You must be signed in to change notification settings - Fork 8
/
tex2CurvesTet.m
1968 lines (1617 loc) · 71.9 KB
/
tex2CurvesTet.m
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
function dataOut = tex2CurvesTet(dataIn, res, boundaryOff, snapVertIdx)
% Trace the isolines of a texture parametrization to generate a truss
% layout
%
% Input
% dataIn: struct with the mesh description (dataIn.V, dataIn.T) and the
% parametrization (dataIn.u, dataIn.v, dataIn.w)
% res: the resolution parameter (`rho` in the paper)
% boundaryOff: If true, then does not trace boundary elements
% snapVertIdx: Optional. Set of indices into dataIn.V to snap the truss
% layout vertices to. That is, a truss node will lie on each of the indexed
% vertices
% Single-character abbreviations:
% (U, V, W): the three texture coordinates
% F: Faces (triangles)
% T: Simplices (tris for 2D, tets for 3D)
% E: Edges
% P: Vertices/positions/points
% I: integer
% (X, Y, Z): the three world coordinates
% B: Domain boundary
if nargin < 3
boundaryOff = false;
end
if nargin < 4
snapVertIdx = [];
end
DONOTTRACEINTERNAL = false; % removes both nodes and elements
DONOTTRACEBOUNDARY = boundaryOff; % removes elements only
P = dataIn.V;
T = sort(dataIn.T, 2);
% get F from T by enumerating all 4 choices on each tet, and then
% taking a set union by removing duplicates
choice = nchoosek(1:4, 3);
F = unique(sort([...
T(:, choice(1, :));...
T(:, choice(2, :));...
T(:, choice(3, :));...
T(:, choice(4, :))...
], 2), 'rows');
% Get E from T
E = edges(T);
nP = size(P, 1);
nT = size(T, 1);
nF = size(F, 1);
nE = size(E, 1);
tex = dataIn.u;
adjacency = adjacency_list(E);
tex = 1 + snapGridToVertices(P, T, tex, res, snapVertIdx);
epsLarge = 1e-7;
epsSmall = 1e-10;
oneRingMax = [cellfun(@(x) max(tex(x, 1)), adjacency),...
cellfun(@(x) max(tex(x, 2)), adjacency), ...
cellfun(@(x) max(tex(x, 3)), adjacency)];
texRound = round(tex);
idxTexInt = abs(texRound-tex) < epsSmall;
idxTexLocalMax = tex > oneRingMax - epsLarge;
tex(idxTexInt & ~idxTexLocalMax) = ...
tex(idxTexInt & ~idxTexLocalMax) - epsLarge;
tex(idxTexInt & idxTexLocalMax) = ...
tex(idxTexInt & idxTexLocalMax) + epsLarge;
U = tex(:, 1);
V = tex(:, 2);
W = tex(:, 3);
clear tex;
[~, E_F1] = ismember(F(:, [1, 2]), E, 'rows');
[~, E_F2] = ismember(F(:, [2, 3]), E, 'rows');
[~, E_F3] = ismember(F(:, [1, 3]), E, 'rows');
E_F = [E_F1, E_F2, E_F3];
[~, F_T1] = ismember(T(:, [1 2 3]), F, 'rows');
[~, F_T2] = ismember(T(:, [1 2 4]), F, 'rows');
[~, F_T4] = ismember(T(:, [1 3 4]), F, 'rows');
[~, F_T3] = ismember(T(:, [2 3 4]), F, 'rows');
F_T = [F_T1, F_T2, F_T3, F_T4];
isFBoundary = is_boundary_facet(F, T);
F_E = cellfun(...
@(x) find(isFBoundary & (E_F1 == x | E_F2 == x | E_F3 == x)),...
num2cell((1:nE)'),...
'UniformOutput', false);
clear E_F1 E_F2 E_F3;
clear F_T1 F_T2 F_T3 F_T4;
% Points of intersection between an edge and an integral isosurface
PUI_E = cell(nE, 1);
PVI_E = PUI_E;
PWI_E = PUI_E;
% Values of the possibly non-integral tex-coordinates on these points
VonPUI_E = PUI_E;
WonPUI_E = PUI_E;
UonPVI_E = PUI_E;
WonPVI_E = PUI_E;
UonPWI_E = PUI_E;
VonPWI_E = PUI_E;
% Values of the integral tex-coordinates on these points
UI_E = cell(nE, 1);
VI_E = UI_E;
WI_E = UI_E;
% Points of intersection between integral isocurves (curves with two
% integral tex-coordiantes) and faces (tris)
PUVI_F = cell(nF, 1);
PVWI_F = PUVI_F;
PWUI_F = PUVI_F;
% Values of the third tex-coordinate on these points
WonPUVI_F = PUVI_F;
UonPVWI_F = PUVI_F;
VonPWUI_F = PUVI_F;
% Values of the integral tex-coordinates on these points
U_UVI_F = PUVI_F;
V_UVI_F = PUVI_F;
V_VWI_F = PUVI_F;
W_VWI_F = PUVI_F;
W_WUI_F = PUVI_F;
U_WUI_F = PUVI_F;
allocSize = 2*1e6;
% For final output: nodes (graph vertices)
node = zeros(allocSize, 3);
nodeSize = 0;
% node properties
nodeIsBoundary = false(allocSize, 1);
nodeUI = int32(-ones(allocSize, 1));
nodeVI = nodeUI;
nodeWI = nodeUI;
nodeU = -inf(allocSize, 1);
nodeV = nodeU;
nodeW = nodeU;
nodeSizeTillPrevFace = zeros(nF, 1);
nodeSizeTillPrevTet = zeros(nT, 1);
% stores the edge idx for nodes lying on edges
E_node = int32(zeros(allocSize, 1));
% face idx for nodes lying on faces
F_node = E_node;
T_node = E_node;
isEBoundary = false(nE, 1);
for i=1:3
isEBoundary(E_F(isFBoundary, i)) = true;
end
% For final output: elements (graph edges)
elem = zeros(2*allocSize, 2);
elemSize = 0;
boundaryAngleCosine_E = ones(nE, 1);
for i = 1:nE
if isEBoundary(i)
a = E(i, 1);
b = E(i, 2);
edge = P(a, :) - P(b, :);
faces = F_E{i};
c1 = setdiff(F(faces(1), :), E(i, :));
c2 = setdiff(F(faces(2), :), E(i, :));
n1 = cross(edge, P(c1, :) - P(b, :));
n2 = cross(P(c2, :) - P(b, :), edge);
boundaryAngleCosine_E(i) = dot(n1, n2)/(norm(n1) * norm(n2));
end
end
isEBCurved = boundaryAngleCosine_E < 0.9; % is E on a curved part of the boundary
disp(['# of boundary edges: ' num2str(sum(isEBCurved))]);
isPBoundary = false(nP, 1);
isPBoundary(E(isEBoundary, 1)) = true;
isPBoundary(E(isEBoundary, 2)) = true;
EBCurved = E(isEBCurved, :);
numIncidentEBCurved_P = accumarray(EBCurved(:), 1);
numIncidentEBCurved_P = [numIncidentEBCurved_P; zeros(nP - max(EBCurved(:)), 1)];
boundaryAngleCosine_P = ones(nP, 1);
for i = 1:nP
if isPBoundary(i) && numIncidentEBCurved_P(i) >= 2
neighbours = adjacency{i}(isPBoundary(adjacency{i}));
n1 = -1;
n2 = -1;
for n = neighbours
v = sort([i, n]);
% find the sharp edge {i, n}
if numel(find(E(:, 1)==v(1) & E(:, 2)==v(2) & isEBCurved))
if (n1 == -1)
n1 = n;
else
n2 = n;
break;
end
end
end
vec1 = P(i, :) - P(n1, :);
vec2 = P(n2, :) - P(i, :);
boundaryAngleCosine_P(i) = dot(vec1, vec2)/(norm(vec1) * norm(vec2));
end
end
isPBCurved = boundaryAngleCosine_P < 0.5; % is P on a highly curved part of the boundary
numPBCurved = sum(isPBCurved);
disp(['# of boundary corners: ' num2str(numPBCurved)]);
for i = 1:nE
% disp(['Edge # ' num2str(i)]);
edge = E(i, :);
% find points on intersections b/w integral u-isosurfaces and edges
[U12, p1Idx] = sort([U(edge(1)), U(edge(2))]);
edge = edge(p1Idx);
p1 = P(edge(1), :);
p2 = P(edge(2), :);
v1 = V(edge(1));
v2 = V(edge(2));
w1 = W(edge(1));
w2 = W(edge(2));
UI_E{i} = integersOpen(U12(1), U12(2));
alphaU = (UI_E{i} - U12(1)) / (U12(2) - U12(1));
PUI_E{i} = (1 - alphaU)*p1 + alphaU*p2;
VonPUI_E{i} = (1 - alphaU)*v1 + alphaU*v2;
WonPUI_E{i} = (1 - alphaU)*w1 + alphaU*w2;
% if edge is on the boundary, add these points to nodes
if isEBoundary(i)
n = numel(alphaU);
nodeIdx = nodeSize + (1:n);
node(nodeIdx, :) = PUI_E{i};
nodeUI(nodeIdx) = UI_E{i};
nodeU(nodeIdx) = UI_E{i};
nodeV(nodeIdx) = VonPUI_E{i};
nodeW(nodeIdx) = WonPUI_E{i};
E_node(nodeIdx) = i;
nodeIsBoundary(nodeIdx) = true;
nodeSize = nodeSize + n;
end
% do the same for integral v-isosurfaces
[V12, p1Idx] = sort([V(edge(1)), V(edge(2))]);
edge = edge(p1Idx);
p1 = P(edge(1), :);
p2 = P(edge(2), :);
u1 = U(edge(1));
u2 = U(edge(2));
w1 = W(edge(1));
w2 = W(edge(2));
VI_E{i} = integersOpen(V12(1), V12(2));
alphaV = (VI_E{i} - V12(1)) / (V12(2) - V12(1));
PVI_E{i} = (1 - alphaV)*p1 + alphaV*p2;
UonPVI_E{i} = (1 - alphaV)*u1 + alphaV*u2;
WonPVI_E{i} = (1 - alphaV)*w1 + alphaV*w2;
if isEBoundary(i)
n = numel(alphaV);
nodeIdx = nodeSize + (1:n);
node(nodeIdx, :) = PVI_E{i};
nodeVI(nodeIdx) = VI_E{i};
nodeU(nodeIdx) = UonPVI_E{i};
nodeV(nodeIdx) = VI_E{i};
nodeW(nodeIdx) = WonPVI_E{i};
E_node(nodeIdx) = i;
nodeIsBoundary(nodeIdx) = true;
nodeSize = nodeSize + n;
end
% finally, do this for integral w-isosurfaces
[W12, p1Idx] = sort([W(edge(1)), W(edge(2))]);
edge = edge(p1Idx);
p1 = P(edge(1), :);
p2 = P(edge(2), :);
u1 = U(edge(1));
u2 = U(edge(2));
v1 = V(edge(1));
v2 = V(edge(2));
WI_E{i} = integersOpen(W12(1), W12(2));
alphaW = (WI_E{i} - W12(1)) / (W12(2) - W12(1));
PWI_E{i} = (1 - alphaW)*p1 + alphaW*p2;
UonPWI_E{i} = (1 - alphaW)*u1 + alphaW*u2;
VonPWI_E{i} = (1 - alphaW)*v1 + alphaW*v2;
if isEBoundary(i)
n = numel(alphaW);
nodeIdx = nodeSize + (1:n);
node(nodeIdx, :) = PWI_E{i};
nodeWI(nodeIdx) = WI_E{i};
nodeU(nodeIdx) = UonPWI_E{i};
nodeV(nodeIdx) = VonPWI_E{i};
nodeW(nodeIdx) = WI_E{i};
E_node(nodeIdx) = i;
nodeIsBoundary(nodeIdx) = true;
nodeSize = nodeSize + n;
end
end
% disp('# boundary edges:');
% disp(elemSize);
% State of final graph so far:
% node-> all nodes lying on boundary edges
% elem-> empty
for i=1:nF
nodeSizeTillPrevFace(i) = nodeSize;
% look at all U-lines on all faces, and find intersection points with
% V-lines and with W-lines. For boundary faces, also build the
% connectivity graph by adding elements forming the U-lines
% disp(['Face # ' num2str(i)]);
% disp('U-lines');
for j=1:3
e1 = E_F(i, mod(j+1, 3)+1);
e2 = E_F(i, mod(j+2, 3)+1);
minUI = max(min(UI_E{e1}), min(UI_E{e2}));
maxUI = min(max(UI_E{e1}), max(UI_E{e2}));
% idx into UIonE{e1} and UIonE{e2}
[~, shift1] = ismember(minUI, UI_E{e1});
if isempty(shift1)
shift1 = 0;
end
idx1 = shift1 + (0:maxUI-minUI);
[~, shift2] = ismember(minUI, UI_E{e2});
if isempty(shift2)
shift2 = 0;
end
idx2 = shift2 + (0:maxUI-minUI);
for k=1:numel(idx1)
% We have to interpolate b/w the vertices at idx1(k)
% position in PUI_E{e1} and idx2(k) in PUI_E{e2}.
% UI_E{e1/2} and VonUI_E{e1/2} follow the same indexing
k1 = idx1(k);
k2 = idx2(k);
ePair = [e1 e2];
P12 = [PUI_E{e1}(k1, :); PUI_E{e2}(k2, :)];
u = UI_E{e1}(k1); % U is the same for both
V12 = [VonPUI_E{e1}(k1); VonPUI_E{e2}(k2)];
W12 = [WonPUI_E{e1}(k1); WonPUI_E{e2}(k2)];
% point with smaller v or w is at alpha = 0
if abs(diff(V12)) > abs(diff(W12))
[~, idxSort] = sort(V12);
sorter = 2;
else
[~, idxSort] = sort(W12);
sorter = 3;
end
V12 = V12(idxSort);
P12 = P12(idxSort, :);
ePair = ePair(idxSort);
W12 = W12(idxSort);
if V12(1) > V12(2)
rangeV = integersHalfOpen(V12(2), V12(1));
alphaV = (rangeV - V12(2)) / (V12(1) - V12(2));
alphaV = 1 - alphaV;
else
rangeV = integersHalfOpen(V12(1), V12(2));
alphaV = (rangeV - V12(1)) / diff(V12);
end
if W12(1) > W12(2)
rangeW = integersHalfOpen(W12(2), W12(1));
alphaW = (rangeW - W12(2)) / (W12(1) - W12(2));
alphaW = 1 - alphaW;
else
rangeW = integersHalfOpen(W12(1), W12(2));
alphaW = (rangeW - W12(1)) / diff(W12);
end
nvi = numel(alphaV);
viNodes = (1 - alphaV)*P12(1, :) + alphaV*P12(2, :);
w_vi = (1 - alphaV)*W12(1) + alphaV*W12(2);
PUVI_F{i} = [PUVI_F{i}; viNodes];
U_UVI_F{i} = [U_UVI_F{i}; repmat(u, nvi, 1)];
V_UVI_F{i} = [V_UVI_F{i}; rangeV];
WonPUVI_F{i} = [WonPUVI_F{i}; w_vi];
nodeIdx_v = nodeSize + (1:nvi)';
node(nodeIdx_v, :) = viNodes;
nodeUI(nodeIdx_v) = u;
nodeVI(nodeIdx_v) = rangeV;
nodeU(nodeIdx_v) = u;
nodeV(nodeIdx_v) = rangeV;
nodeW(nodeIdx_v) = w_vi;
F_node(nodeIdx_v) = i;
nwi = numel(alphaW);
wiNodes = (1 - alphaW)*P12(1, :) + alphaW*P12(2, :);
v_wi = (1 - alphaW)*V12(1) + alphaW*V12(2);
PWUI_F{i} = [PWUI_F{i}; wiNodes];
U_WUI_F{i} = [U_WUI_F{i}; repmat(u, nwi, 1)];
W_WUI_F{i} = [W_WUI_F{i}; rangeW];
VonPWUI_F{i} = [VonPWUI_F{i}; v_wi];
nodeIdx_w = nodeSize + nvi + (1:nwi)';
node(nodeIdx_w, :) = wiNodes;
nodeUI(nodeIdx_w) = u;
nodeWI(nodeIdx_w) = rangeW;
nodeU(nodeIdx_w) = u;
nodeV(nodeIdx_w) = v_wi;
nodeW(nodeIdx_w) = rangeW;
F_node(nodeIdx_w) = i;
% add elements on the boundary
if isFBoundary(i)
% find existing nodes on edges
e1NodeIdx = find(E_node==ePair(1) & nodeUI==u);
e2NodeIdx = find(E_node==ePair(2) & nodeUI==u);
% combine vars for vi and wi and sort by v
v = [double(rangeV); v_wi];
w = [w_vi; double(rangeW)];
nodeIdx = [nodeIdx_v; nodeIdx_w];
n = nvi + nwi;
if sorter==2
[~, sortIdx] = sort(v);
else
[~, sortIdx] = sort(w);
end
nodeIdx = nodeIdx(sortIdx);
nodeIsBoundary(nodeIdx) = true;
if n
% add the internal elems
elem(elemSize + (1:n-1), :) = [nodeIdx(1:n-1), nodeIdx(2:n)];
elemSize = elemSize + n-1;
% add two elems if there is any internal node
elem(elemSize + (1:2), :) = [...
e1NodeIdx, nodeIdx(1);
nodeIdx(end), e2NodeIdx];
elemSize = elemSize + 2;
else
% otherwise, just add the elem connecting the two boundary
% nodes
elem(elemSize + 1, :) = [e1NodeIdx, e2NodeIdx];
elemSize = elemSize + 1;
end
end
nodeSize = nodeSize + nvi + nwi;
end
end
% Repeat for V-lines now. But we now have to search for UV points
% in the existing set of nodes. VW points have still not been
% inserted into the nodeset, which we'll do now. Similar to
% U-line processing, we have to insert boundary elements as well.
% disp('V-lines');
nodeSearchFilter = false(allocSize, 1);
nodeSearchFilter(nodeSizeTillPrevFace(i)+1 : nodeSize) = true;
for j=1:3
e1 = E_F(i, mod(j+1, 3)+1);
e2 = E_F(i, mod(j+2, 3)+1);
minVI = max(min(VI_E{e1}), min(VI_E{e2}));
maxVI = min(max(VI_E{e1}), max(VI_E{e2}));
% idx into UIonE{e1} and UIonE{e2}
[~, shift1] = ismember(minVI, VI_E{e1});
if isempty(shift1)
shift1 = 0;
end
idx1 = shift1 + (0:maxVI-minVI);
[~, shift2] = ismember(minVI, VI_E{e2});
if isempty(shift2)
shift2 = 0;
end
idx2 = shift2 + (0:maxVI-minVI);
for k=1:numel(idx1)
% We have to interpolate b/w the vertices at idx1(k)
% position in PUI_E{e1} and idx2(k) in PUI_E{e2}.
% UI_E{e1/2} and VonUI_E{e1/2} follow the same indexing
k1 = idx1(k);
k2 = idx2(k);
ePair = [e1 e2];
P12 = [PVI_E{e1}(k1, :); PVI_E{e2}(k2, :)];
v = VI_E{e1}(k1); % V is the same for both
U12 = [UonPVI_E{e1}(k1); UonPVI_E{e2}(k2)];
W12 = [WonPVI_E{e1}(k1); WonPVI_E{e2}(k2)];
% point with smaller U is at alpha = 0
if abs(diff(U12)) > abs(diff(W12))
[~, idxSort] = sort(U12);
sorter = 1;
else
[~, idxSort] = sort(W12);
sorter = 3;
end
U12 = U12(idxSort);
P12 = P12(idxSort, :);
ePair = ePair(idxSort);
W12 = W12(idxSort);
if W12(1) > W12(2)
rangeW = integersHalfOpen(W12(2), W12(1));
alphaW = (rangeW - W12(2)) / (W12(1) - W12(2));
alphaW = 1 - alphaW;
else
rangeW = integersHalfOpen(W12(1), W12(2));
alphaW = (rangeW - W12(1)) / diff(W12);
end
localNodeSearchFilter = nodeSearchFilter &...
nodeVI==v & nodeUI>=0;
rangeU = nodeUI(localNodeSearchFilter);
nui = numel(rangeU);
nodeIdx_u = find(localNodeSearchFilter);
% sort the searched list
[rangeU, sortIdx] = sort(rangeU);
nodeIdx_u = nodeIdx_u(sortIdx);
w_ui = nodeW(nodeIdx_u);
nwi = numel(alphaW);
wiNodes = (1 - alphaW)*P12(1, :) + alphaW*P12(2, :);
u_wi = (1 - alphaW)*U12(1) + alphaW*U12(2);
PVWI_F{i} = [PVWI_F{i}; wiNodes];
V_VWI_F{i} = [V_VWI_F{i}; repmat(v, nwi, 1)];
W_VWI_F{i} = [W_VWI_F{i}; rangeW];
UonPVWI_F{i} = [UonPVWI_F{i}; u_wi];
nodeIdx_w = nodeSize + (1:nwi)';
node(nodeIdx_w, :) = wiNodes;
nodeVI(nodeIdx_w) = v;
nodeWI(nodeIdx_w) = rangeW;
nodeU(nodeIdx_w) = u_wi;
nodeV(nodeIdx_w) = v;
nodeW(nodeIdx_w) = rangeW;
F_node(nodeIdx_w) = i;
% add elements on the boundary
if isFBoundary(i)
% find existing nodes on edges
e1NodeIdx = find(E_node==ePair(1) & nodeVI==v);
e2NodeIdx = find(E_node==ePair(2) & nodeVI==v);
% combine vars for ui and wi and sort by u
u = [double(rangeU); u_wi];
w = [w_ui; double(rangeW)];
nodeIdx = [nodeIdx_u; nodeIdx_w];
n = nui + nwi;
if sorter==1
[~, sortIdx] = sort(u);
else
[~, sortIdx] = sort(w);
end
nodeIdx = nodeIdx(sortIdx);
nodeIsBoundary(nodeIdx_w) = true;
if n
% add the internal elems
elem(elemSize + (1:n-1), :) = [nodeIdx(1:n-1), nodeIdx(2:n)];
elemSize = elemSize + n-1;
% add two elems if there is any internal node
elem(elemSize + (1:2), :) = [...
e1NodeIdx, nodeIdx(1);
nodeIdx(end), e2NodeIdx];
elemSize = elemSize + 2;
else
% otherwise, just add the elem connecting the two boundary
% nodes
elem(elemSize + 1, :) = [e1NodeIdx, e2NodeIdx];
elemSize = elemSize + 1;
end
end
nodeSize = nodeSize + nwi;
end
end
% Finally for W-lines. Now, we don't need to insert any new nodes.
% disp('W-lines');
nodeSearchFilter(nodeSizeTillPrevFace(i)+1 : nodeSize) = true;
for j=1:3
e1 = E_F(i, mod(j+1, 3)+1);
e2 = E_F(i, mod(j+2, 3)+1);
minWI = max(min(WI_E{e1}), min(WI_E{e2}));
maxWI = min(max(WI_E{e1}), max(WI_E{e2}));
% idx into UIonE{e1} and UIonE{e2}
[~, shift1] = ismember(minWI, WI_E{e1});
if isempty(shift1)
shift1 = 0;
end
idx1 = shift1 + (0:maxWI-minWI);
[~, shift2] = ismember(minWI, WI_E{e2});
if isempty(shift2)
shift2 = 0;
end
idx2 = shift2 + (0:maxWI-minWI);
for k=1:numel(idx1)
% We have to interpolate b/w the vertices at idx1(k)
% position in PUI_E{e1} and idx2(k) in PUI_E{e2}.
% UI_E{e1/2} and VonUI_E{e1/2} follow the same indexing
k1 = idx1(k);
k2 = idx2(k);
ePair = [e1 e2];
w = WI_E{e1}(k1); % W is the same for both
U12 = [UonPWI_E{e1}(k1); UonPWI_E{e2}(k2)];
V12 = [VonPWI_E{e1}(k1); VonPWI_E{e2}(k2)];
% point with smaller U or V is at alpha = 0
if abs(diff(U12)) > abs(diff(V12))
[~, idxSort] = sort(U12);
sorter = 1;
else
[~, idxSort] = sort(V12);
sorter = 2;
end
ePair = ePair(idxSort);
localNodeSearchFilter = nodeSearchFilter &...
nodeWI==w & nodeUI>=0;
rangeU = nodeUI(localNodeSearchFilter);
nodeIdx_u = find(localNodeSearchFilter);
nui = numel(rangeU);
% sort the searched lists
[rangeU, sortIdx] = sort(rangeU);
nodeIdx_u = nodeIdx_u(sortIdx);
v_ui = nodeV(nodeIdx_u);
localNodeSearchFilter = nodeSearchFilter &...
nodeWI==w & nodeVI>=0;
nodeIdx_v = find(localNodeSearchFilter);
nvi = numel(nodeIdx_v);
% sort V-lists by U-values as well
[u_vi, sortIdx] = sort(nodeU(nodeIdx_v));
nodeIdx_v = nodeIdx_v(sortIdx);
rangeV = nodeV(nodeIdx_v);
% add elements on the boundary
if isFBoundary(i)
% find existing nodes on edges
e1NodeIdx = find(E_node==ePair(1) & nodeWI==w);
e2NodeIdx = find(E_node==ePair(2) & nodeWI==w);
% combine vars for ui and vi and sort by u
u = [double(rangeU); u_vi];
v = [v_ui; double(rangeV)];
nodeIdx = [nodeIdx_u; nodeIdx_v];
n = nui + nvi;
if sorter==1
[~, sortIdx] = sort(u);
else
[~, sortIdx] = sort(v);
end
nodeIdx = nodeIdx(sortIdx);
if n
% add the internal elems
elem(elemSize + (1:n-1), :) = [nodeIdx(1:n-1), nodeIdx(2:n)];
elemSize = elemSize + n-1;
% add two elems if there is any internal node
elem(elemSize + (1:2), :) = [...
e1NodeIdx, nodeIdx(1);
nodeIdx(end), e2NodeIdx];
elemSize = elemSize + 2;
% otherwise, just add the elem connecting the two boundary
% nodes
else
elem(elemSize + 1, :) = [e1NodeIdx, e2NodeIdx];
elemSize = elemSize + 1;
end
end
end
end
end
numBNode = nodeSize;
% State of final graph so far:
% node-> all nodes lying on faces (tris)
% elem-> all elems on the boundary
% includes extra nodes/elems which need to be collapsed (later)
% for each tet, we have the nodes on the bounding faces
% first, we iterate over each (unordered) pair of these bounding faces
% Then, for each such pair, we iterate over each (unordered) pair of
% tex-coordinates. We move between the two faces along each integral
% isoline of this pair, and find the intersections with the isoplane of
% the third tex-coordinate.
for i=1:nT
% disp(['Tet # ' num2str(i)]);
f_choice = nchoosek(1:4, 2);
nodeSizeTillPrevTet(i) = nodeSize;
% First, UV-lines. This will find ALL the internal nodes and
% also all the elems lying on UV-lines
% disp('UV-lines');
for j=1:size(f_choice, 1)
f1 = F_T(i, f_choice(j, 1));
f2 = F_T(i, f_choice(j, 2));
idx1 = (1:numel(U_UVI_F{f1}))';
for k1 = 1:numel(idx1)
u = U_UVI_F{f1}(k1);
v = V_UVI_F{f1}(k1);
k2 = find(U_UVI_F{f2} == u & V_UVI_F{f2} == v);
if isempty(k2)
continue;
end
fPair = [f1 f2]';
P12 = [PUVI_F{f1}(k1, :); PUVI_F{f2}(k2, :)];
W12 = [WonPUVI_F{f1}(k1); WonPUVI_F{f2}(k2)];
[W12, idxSort] = sort(W12);
P12 = P12(idxSort, :);
fPair = fPair(idxSort);
range = integersOpen(W12(1), W12(2));
alpha = (range - W12(1)) / diff(W12);
n = numel(alpha);
allNodes = (1 - alpha)*P12(1, :) + alpha*P12(2, :);
% find existing nodes on faces
f1NodeIdx = find(F_node==fPair(1) & nodeUI==u & nodeVI==v);
f2NodeIdx = find(F_node==fPair(2) & nodeUI==u & nodeVI==v);
% add the internal nodes first
nodeIdx = nodeSize + (1:n)';
node(nodeIdx, :) = allNodes;
nodeUI(nodeIdx) = u;
nodeVI(nodeIdx) = v;
nodeWI(nodeIdx) = range;
nodeU(nodeIdx) = u;
nodeV(nodeIdx) = v;
nodeW(nodeIdx) = range;
T_node(nodeIdx) = i;
if n
% add the internal elems
elem(elemSize + (1:n-1), :) = [nodeIdx(1:n-1), nodeIdx(2:n)];
elemSize = elemSize + n-1;
% add two elems if there is any internal node
elem(elemSize + (1:2), :) = [...
f1NodeIdx, nodeIdx(1);...
nodeIdx(end), f2NodeIdx];
elemSize = elemSize + 2;
else
% otherwise, just add the elem connecting the two nodes
% on the faces f1 and f2
elem(elemSize + 1, :) = [f1NodeIdx, f2NodeIdx];
elemSize = elemSize + 1;
end
nodeSize = nodeSize + n;
end
end
nodeSearchFilter = false(allocSize, 1);
nodeSearchFilter(nodeSizeTillPrevTet(i)+1 : nodeSize) = true;
% Then, trace out the elems on VW-lines. Note that the nodes
% are already there.
% disp('VW-lines');
for j=1:size(f_choice, 1)
f1 = F_T(i, f_choice(j, 1));
f2 = F_T(i, f_choice(j, 2));
idx1 = (1:numel(V_VWI_F{f1}))';
for k1 = 1:numel(idx1)
v = V_VWI_F{f1}(k1);
w = W_VWI_F{f1}(k1);
k2 = find(V_VWI_F{f2} == v & W_VWI_F{f2} == w, 1);
if isempty(k2)
continue;
end
U12 = [UonPVWI_F{f1}(k1); UonPVWI_F{f2}(k2)];
fPair = [f1 f2]';
[~, sortIdx] = sort(U12);
fPair = fPair(sortIdx);
localNodeSearchFilter = nodeSearchFilter &...
nodeVI==v & nodeWI==w & nodeUI>=0;
range = nodeUI(localNodeSearchFilter);
n = numel(range);
nodeIdx = find(localNodeSearchFilter);
%sort the searched lists
[~, sortIdx] = sort(range);
nodeIdx = nodeIdx(sortIdx);
% find existing nodes on faces
f1NodeIdx = find(F_node==fPair(1) & nodeVI==v & nodeWI==w);
f2NodeIdx = find(F_node==fPair(2) & nodeVI==v & nodeWI==w);
if n
% add the internal elems
elem(elemSize + (1:n-1), :) = [nodeIdx(1:n-1), nodeIdx(2:n)];
elemSize = elemSize + n-1;
% add two elems if there is any internal node
elem(elemSize + (1:2), :) = [...
f1NodeIdx, nodeIdx(1);...
nodeIdx(end), f2NodeIdx];
elemSize = elemSize + 2;
else
% otherwise, just add the elem connecting the two nodes
% on the faces f1 and f2
elem(elemSize + 1, :) = [f1NodeIdx, f2NodeIdx];
elemSize = elemSize + 1;
end
end
end
% Finally, trace out the elems on WU-lines. This works exactly the
% same way as tracing VW-lines.
% disp('WU-lines');
for j=1:size(f_choice, 1)
f1 = F_T(i, f_choice(j, 1));
f2 = F_T(i, f_choice(j, 2));
idx1 = (1:numel(W_WUI_F{f1}))';
for k1 = 1:numel(idx1)
w = W_WUI_F{f1}(k1);
u = U_WUI_F{f1}(k1);
k2 = find(W_WUI_F{f2} == w & U_WUI_F{f2} == u, 1);
if isempty(k2)
continue;
end
V12 = [VonPWUI_F{f1}(k1); VonPWUI_F{f2}(k2)];
fPair = [f1 f2]';
[~, sortIdx] = sort(V12);
fPair = fPair(sortIdx);
localNodeSearchFilter = nodeSearchFilter &...
nodeWI==w & nodeUI==u & nodeVI>=0;
range = nodeVI(localNodeSearchFilter);
n = numel(range);
nodeIdx = find(localNodeSearchFilter);
%sort the searched lists
[~, sortIdx] = sort(range);
nodeIdx = nodeIdx(sortIdx);
% find existing nodes on faces
f1NodeIdx = find(F_node==fPair(1) & nodeWI==w & nodeUI==u);
f2NodeIdx = find(F_node==fPair(2) & nodeWI==w & nodeUI==u);
if n
% add the internal elems
elem(elemSize + (1:n-1), :) = [nodeIdx(1:n-1), nodeIdx(2:n)];
elemSize = elemSize + n-1;
% add two elems if there is any internal node
elem(elemSize + (1:2), :) = [...
f1NodeIdx, nodeIdx(1);...
nodeIdx(end), f2NodeIdx];
elemSize = elemSize + 2;
else
% otherwise, just add the elem connecting the two nodes
% on the faces f1 and f2
elem(elemSize + 1, :) = [f1NodeIdx, f2NodeIdx];
elemSize = elemSize + 1;
end
end
end
end
node(nodeSize+1:end, :) = [];
elem(elemSize+1:end, :) = [];
nodeIsBoundary(nodeSize+1:end) = [];
nodeUI(nodeSize+1:end) = [];
nodeVI(nodeSize+1:end) = [];
nodeWI(nodeSize+1:end) = [];
E_node(nodeSize+1:end) = [];
F_node(nodeSize+1:end) = [];
T_node(nodeSize+1:end) = [];
clear nodeU nodeV nodeW;
disp(['Pre-collapse graph size: (' num2str(nodeSize) ', ' num2str(elemSize) ')']);
% And finally, we need to collapse edges. This is done by
% following isolines emanating from each boundary vertex, and
% collapsing all invalid vertices along a line in one go.
newIdx_node = zeros(nodeSize, 1);
isEBCurved_node = false(nodeSize, 1);
isEBCurved_node(E_node > 0) = isEBCurved(E_node(E_node > 0));
node2 = zeros(nodeSize, 3);
nodeUI2 = -int32(ones(nodeSize, 1));
nodeVI2 = -int32(ones(nodeSize, 1));
nodeWI2 = -int32(ones(nodeSize, 1));
nodeB2 = false(nodeSize, 1);
nodeC2 = false(nodeSize, 1);
oldIdx_node2 = zeros(nodeSize, 1);
elem2 = zeros(elemSize, 2);
elemC2 = false(elemSize, 1);