forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkStreamingTessellatorGenerator.py
executable file
·1698 lines (1552 loc) · 60 KB
/
vtkStreamingTessellatorGenerator.py
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
#!/usr/bin/python2.2
# -*- coding: latin-1 -*-
#
# Copyright 2003 Sandia Corporation.
# Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
# license for use of this work by or on behalf of the
# U.S. Government. Redistribution and use in source and binary forms, with
# or without modification, are permitted provided that this Notice and any
# statement of authorship are reproduced on all copies.
#
import sys, re, math
QualityThang = 0
# This script writes some of the tessellator code along with
# a few static variables containing configuration information
# for tetrahedral decompositions.
# It is broken into 4 sections:
# - the class definition of vtkTessCase (represent a decomposition)
# (no output)
# - the declaration of decompositions and permutations
# (outputs static arrays)
# - the logic that produces test
# (outputs C++ code that references the static arrays)
# ============================================================================
# A class to organize the list of output tetrahedra
class vtkTessCase:
"""
vtkTessCase holds the tetrahedral decomposition of a simplex.
"""
CurrentOffset = 0
AllCases = {}
def PrintAll( fout ):
print >> fout, "vtkIdType vtkStreamingTessellator::TetrahedralDecompositions[] = \n{"
tmp = []
for k in vtkTessCase.AllCases.keys():
tmp.append( ( vtkTessCase.AllCases[k].Offset, k ) )
tmp.sort()
#for t in tmp:
# print t
for t in tmp:
vtkTessCase.AllCases[t[1]].Print( fout )
print >> fout, "};\n"
PrintAll = staticmethod( PrintAll )
def GetOffset( label ):
if label == '':
return -1
try:
val = vtkTessCase.AllCases[label].Offset
except:
val = -1
return val
GetOffset = staticmethod( GetOffset )
def __init__( self, label ):
"""Create an empty decomposition with a unique name."""
#print >> sys.stderr, ' Tetrahedra: %s' % label
self.Tets = []
self.Label = label
self.Offset = vtkTessCase.CurrentOffset
vtkTessCase.CurrentOffset += 1
vtkTessCase.AllCases[ self.Label ] = self
def InsertTet( self, indices ):
"""Add tetrahedra to the decomposition. The argument must be
a vector of integer numbers whose length is a multiple of 4.
Every tetrahedron should be followed by an integer bit vector
calling out any edges that are internal to the subdivision
with a nonzero bit."""
self.Tets += indices
vtkTessCase.CurrentOffset += len(indices)
if len(indices) % 4:
raise 'Bad number of indices!'
def Print( self, fout ):
"""Write the decomposition out to the given file."""
print >> fout, '// case %s' % self.Label
print >> fout, ' %2d,' % (len(self.Tets)/4)
for i in range(len(self.Tets)/4):
print >> fout, ' %2d, %2d, %2d, %2d,' % tuple(self.Tets[(i*4):(i*4+4)])
print >> fout, ''
#self.Offset = vtkTessCase.CurrentOffset
#vtkTessCase.CurrentOffset += len(self.Tets)+2
# ============================================================================
# Global variables that hold code generation state
currentCase = 'invalid'
currentCaseCtr = -1
currentBits = {}
caseLabel = 0
# ============================================================================
# Constant global variables
genCode = file('vtkStreamingTessellator.cxx','w')
# These are EDGE numbers of the edges on a given face, NOT vertex numbers
FaceEdges = [ [0,1,2], [0,3,4], [1,4,5], [2,5,3] ]
# These are the vertices of a given edge
EdgeVerts = [ [0,1], [1,2], [0,2], [0,3], [1,3], [2,3] ]
# This dictionary maps from vertices to a face index
FaceFromVerts = { (0,1,2):0, (0,1,3):1, (1,2,3):2, (0,2,3):3 }
# The Ruprecht-Müller cases
# Each case is assigned a tuple containing:
# 1. A unique integer (used in switch statements)
# 2. A list of edges to be subdivided. This is the list
# of edges for the canonical configuration.
RMCases = {\
'1' : ( 0, [0]), \
'2a' : ( 1, [0,1]), \
'2b' : ( 2, [0,5]), \
'3a' : ( 3, [0,2,3]), \
'3b' : ( 4, [0,3,4]), \
'3c' : ( 5, [0,1,3]), \
'3d' : ( 6, [0,2,4]), \
'4a' : ( 7, [2,3,4,5]), \
'4b' : ( 8, [1,2,3,4]), \
'5' : ( 9, [1,2,3,4,5]), \
'6' : (10, [0,1,2,3,4,5]) \
}
re_if = re.compile( '([0-9]+)[ \t]*([<>][=]?|=)[ \t]*([0-9]+)' )
re_switch = re.compile( '(\d+)' )
PermutationIndices = { \
() : ( 0,+1), \
(0,1,2,3) : ( 0,+1), \
(1,2,0,3) : ( 1,+1), \
(2,0,1,3) : ( 2,+1), \
(0,3,1,2) : ( 3,+1), \
(3,1,0,2) : ( 4,+1), \
(1,0,3,2) : ( 5,+1), \
(1,3,2,0) : ( 6,+1), \
(3,2,1,0) : ( 7,+1), \
(2,1,3,0) : ( 8,+1), \
(2,3,0,1) : ( 9,+1), \
(3,0,2,1) : (10,+1), \
(0,2,3,1) : (11,+1), \
(0,2,1,3) : (12,-1), \
(2,1,0,3) : (13,-1), \
(1,0,2,3) : (14,-1), \
(0,1,3,2) : (15,-1), \
(1,3,0,2) : (16,-1), \
(3,0,1,2) : (17,-1), \
(1,2,3,0) : (18,-1), \
(2,3,1,0) : (19,-1), \
(3,1,2,0) : (20,-1), \
(2,0,3,1) : (21,-1), \
(0,3,2,1) : (22,-1), \
(3,2,0,1) : (23,-1), \
}
# ============================================================================
# Utility routines
def GetInverse( op ):
if op == '<':
return '>'
elif op == '>':
return '<'
return '?'
def GetBitcodeFromConditional( conditional, indices ):
#print 'Indices: %s' % indices
stuff = re_switch.split( conditional )
bits = 0
v = []
for t in stuff:
if t == '':
continue
v.append( t )
for i in range(1,len(v)-1,2):
if v[i] == ',':
# This conditional requires multiple disjoint comparisons
# and the ',' just serves as a separator. Skip it.
continue
#print '%d: %s %s %s' % ( i, v[i-1], v[i], v[i+1] )
cind = ( int(v[i-1]), int(v[i+1]) )
flip = 0
if not indices.has_key( cind ):
cind = ( cind[1], cind[0] )
flip = 1
if not indices.has_key( cind ):
# This edge pair is unimportant (inequality contains
# more constraints than are required to characterize
# this case). Print warning and skip:
print '*** WARNING *** Edge comparison %s %s %s unnecessary!' % ( v[i-1], v[i], v[i+1] )
continue
if v[i] == ',':
continue
elif v[i] == '=':
m = indices[ cind ]
if not flip:
bits += 2**m + 2**(m+1)
else:
bits += 2**(m+1) + 2**m
elif v[i] == '<':
m = indices[ cind ]
if not flip:
bits += 2**m
else:
bits += 2**(m+1)
elif v[i] == '>':
m = indices[ cind ]
if not flip:
bits += 2**(m+1)
else:
bits += 2**m
return bits
def GetVerticesFromPair( edgePair ):
a = []
for e in edgePair:
a += EdgeVerts[ e ]
top = -1
for i in range(len(a)):
if a.count(a[i]) == 2:
top = i
break
if top < 0:
raise 'Given a bad pair of edges! They\'re not on the same face.'
v0 = a[top]
a.remove(v0)
a.remove(v0)
a = [ v0, a[0], a[1] ]
return a
def GetFaceFromVertices( verts ):
verts.sort()
return FaceFromVerts[ (verts[0],verts[1],verts[2]) ]
def EdgesToSubdivide( edges ):
global currentCaseCtr, currentCase, currentBits
possibleFaces = []
edgePairs = {}
#print 'These edges are subdivided: %s' % edges
for f in range(4):
edgePair = ()
for edge in FaceEdges[f]:
#print ' Is %s in the list?' % edge
if edge in edges:
edgePair = edgePair + tuple([ edge ])
#print 'edgePairs are %s' % str(edgePair)
if len(edgePair) == 2:
possibleFaces += [ f ]
edgePairs[ edgePair ] = 1
#print 'Possible faces: %s' % possibleFaces
edgePairs = edgePairs.keys()
#print 'Edge pairs : %s' % edgePairs
if len(edgePairs):
print >> genCode, ' comparisonBits = '
tmp = 1;
for p in edgePairs:
print >> genCode, ' (permlen[%d] <= permlen[%d] ? %d : 0) | (permlen[%d] >= permlen[%d] ? %d : 0) |' % ( p[0], p[1], tmp, p[0], p[1], 2*tmp )
tmp = tmp * 4
if len(edgePairs):
print >> genCode, ' 0;'
tmp = 1
for p in edgePairs:
currentBits[ (p[0],p[1]) ] = math.frexp(tmp)[1]-1
bits = tmp + 2*tmp
tmp = tmp*4
[v0,v1,v2] = GetVerticesFromPair( p )
faceidx = 10 + GetFaceFromVertices( [v0,v1,v2] )
print >> genCode, ' if ( (comparisonBits & %d) == %d )' % (bits,bits)
print >> genCode, ' {'
print >> genCode, ' // Compute face point'
print >> genCode, ' for ( i=0; i<this->PointDimension[3]; i++ )'
print >> genCode, ' {'
print >> genCode, ' permuted[%d][i] = (permuted[%d][i] + permuted[%d][i])*0.375 + permuted[%d][i]/4.;' % ( faceidx, v1, v2, v0 )
print >> genCode, ' }'
print >> genCode, ' }'
faceidx += 1
def BeginCase( label ):
global currentCaseCtr, currentCase, currentBits, caseLabel
print >> genCode, ' case %d: // Ruprecht-Müller Case %s' % ( RMCases[ label ][0]+1, label )
currentCase = label
currentCaseCtr = 0
currentBits = {}
EdgesToSubdivide( RMCases[ label ][1] )
caseLabel = 0
print >> genCode, ' VTK_TESSELLATOR_INCR_CASE_COUNT(%d);' % RMCases[ currentCase ][0]
def EndCase():
global currentCaseCtr, currentCase, currentBits, caseLabel
print >> genCode, ' break;'
currentCase = 'invalid'
currentCaseCtr = -1
currentBits = {}
caseLabel = -1
def __Unconditional( tets, perm, sign, indent='', label='', alternates=() ):
global currentCaseCtr, currentCase, currentBits, caseLabel
if PermutationIndices[ perm ][1] != sign:
if sign < 0:
sgnstr = '-'
else:
sgnstr = '+'
raise '%s %s (%d): Permutation(%s) and sign(%s) do not match!' % (currentCase, label, currentCaseCtr, str(perm), sgnstr)
tc = []
if label == '':
label = currentCase + '_%d' % currentCaseCtr
else:
label = currentCase + ', ' + label
stuff = vtkTessCase( label )
tc.append( stuff )
stuff.InsertTet( tets )
if QualityThang:
altcount = 97
for a in alternates:
if label == '':
altlabel = currentCase + '_%d%c' % (currentCaseCtr,altcount)
else:
altlabel = label + ', %c' % altcount
stuff = vtkTessCase( altlabel )
stuff.InsertTet( a )
altcount += 1
tc.append( stuff )
currentCaseCtr += 1
if len(tc) > 1 and QualityThang:
altstring = '{ '
for stuff in tc:
altstring += str(stuff.Offset) + ', '
altstring += '-1 }'
print >> genCode, ' %s{' % indent
print >> genCode, ' %s int alternates[] = %s;' % (indent, altstring)
print >> genCode, ' %s outputTets.push( vtkStreamingTessellator::TetrahedralDecompositions + this->BestTets( alternates, permuted, %d, %d ) );' % ( indent, PermutationIndices[ perm ][0], sign )
print >> genCode, ' %s}' % indent
else:
print >> genCode, ' %soutputTets.push( vtkStreamingTessellator::TetrahedralDecompositions + %d );' % (indent, tc[0].Offset)
print >> genCode, ' %soutputPerm.push( vtkStreamingTessellator::PermutationsFromIndex[%d] );' % (indent, PermutationIndices[ perm ][0])
print >> genCode, ' %soutputSign.push( %d );' % (indent, sign)
print >> genCode, ' %sVTK_TESSELLATOR_INCR_SUBCASE_COUNT(%d,%d);' % (indent,RMCases[ currentCase ][0],caseLabel)
caseLabel += 1
def __Permuted( perm, sign, source, indent, label ):
global currentCaseCtr, currentCase, currentBits, caseLabel
if PermutationIndices[ perm ][1] != sign:
if sign < 0:
sgnstr = '-'
else:
sgnstr = '+'
raise '%s %s (%d): Permutation(%s) and sign(%s) do not match!' % (currentCase, label, currentCaseCtr, str(perm), sgnstr)
# Create a list of all the possible tessellations
tc = []
stuff = vtkTessCase.GetOffset( currentCase + ', ' + source )
tc.append( stuff )
if QualityThang:
altcount = 97
stuff = vtkTessCase.GetOffset( currentCase + ', ' + source + ', %c' % altcount )
while stuff > 0:
tc.append( stuff )
altcount += 1
stuff = vtkTessCase.GetOffset( currentCase + ', ' + source + ', %c' % altcount )
if len(tc) > 1 and QualityThang:
altstring = str( tc ).replace( '[', '{' ).replace( ']', ', -1 }' )
print >> genCode, ' %s{' % indent
print >> genCode, ' %s int alternates[] = %s;' % (indent, altstring)
print >> genCode, ' %s outputTets.push( vtkStreamingTessellator::TetrahedralDecompositions + this->BestTets( alternates, permuted, %d, %d ) );' % ( indent, PermutationIndices[ perm ][0], sign )
print >> genCode, ' %s}' % indent
else:
print >> genCode, ' %soutputTets.push( vtkStreamingTessellator::TetrahedralDecompositions + %d );' % (indent, tc[0])
print >> genCode, ' %soutputPerm.push( vtkStreamingTessellator::PermutationsFromIndex[%d] );' % (indent, PermutationIndices[ perm ][0])
print >> genCode, ' %soutputSign.push( %d );' % (indent, sign)
print >> genCode, ' %sVTK_TESSELLATOR_INCR_SUBCASE_COUNT(%d,%d);' % (indent,RMCases[ currentCase ][0],caseLabel)
caseLabel += 1
def __BeginSubcase():
print >> genCode, ' switch (comparisonBits)'
print >> genCode, ' {'
def __EndSubcase():
print >> genCode, ' }'
def __SubCase( ctxt, tets, sgn, alternates=() ):
global currentCaseCtr, currentCase, currentBits, caseLabel
code = GetBitcodeFromConditional( ctxt, currentBits )
print >> genCode, ' case %d: // %s' % ( code, ctxt )
if sgn > 0:
__Unconditional( tets, (0,1,2,3), +1, ' ', ctxt, alternates )
else:
__Unconditional( tets, (1,0,2,3), -1, ' ', ctxt, alternates )
print >> genCode, ' %sbreak;' % ' '
def __PrmCase( ctxt, csrc, perm, sgn ):
global currentCaseCtr, currentCase, currentBits, caseLabel
code = GetBitcodeFromConditional( ctxt, currentBits )
print >> genCode, ' case %d: // %s' % ( code, ctxt )
__Permuted( perm, sgn, csrc, ' ', ctxt )
print >> genCode, ' %sbreak;' % ' '
print >> genCode, \
"""/*
* Copyright 2003 Sandia Corporation.
* Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
* license for use of this work by or on behalf of the
* U.S. Government. Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that this Notice and any
* statement of authorship are reproduced on all copies.
*/
/* Do not edit this file! It was generated by hand. Mostly.
* Edit vtkStreamingTessellatorGenerator.py instead.
*/
#include "vtkObjectFactory.h"
#include "vtkStreamingTessellator.h"
#include "vtkEdgeSubdivisionCriterion.h"
"""
if QualityThang:
print >> genCode, """
#include "vtkMeshQuality.h"
#include "vtkPoints.h"
#include "vtkTetra.h"
// how's this for avoiding namespace conflicts?! 8-)
static vtkTetra* argyle = 0;
static vtkPoints* goCallTheCops;
"""
print >> genCode, """
#undef UGLY_ASPECT_RATIO_HACK
#undef DBG_MIDPTS
#include <stack>
#include <algorithm>
#ifdef PARAVIEW_DEBUG_TESSELLATOR
# define VTK_TESSELLATOR_INCR_CASE_COUNT(cs) this->CaseCounts[cs]++
# define VTK_TESSELLATOR_INCR_SUBCASE_COUNT(cs,sc) this->SubcaseCounts[cs][sc]++
#else // PARAVIEW_DEBUG_TESSELLATOR
# define VTK_TESSELLATOR_INCR_CASE_COUNT(cs)
# define VTK_TESSELLATOR_INCR_SUBCASE_COUNT(cs,sc)
#endif // PARAVIEW_DEBUG_TESSELLATOR
static void DefaultFacet3Callback(
const double* a, const double* b, const double* c, const double* d,
vtkEdgeSubdivisionCriterion*, void* pd, const void* )
{
(void)a;
(void)b;
(void)c;
(void)d;
(void)pd;
}
static void DefaultFacet2Callback(
const double* a, const double* b, const double* c,
vtkEdgeSubdivisionCriterion*, void* pd, const void* )
{
(void)a;
(void)b;
(void)c;
(void)pd;
}
static void DefaultFacet1Callback(
const double* a, const double* b,
vtkEdgeSubdivisionCriterion*, void* pd, const void* )
{
(void)a;
(void)b;
(void)pd;
}
static void DefaultFacet0Callback(
const double* a,
vtkEdgeSubdivisionCriterion*, void* pd, const void* )
{
(void)a;
(void)pd;
}
vtkStandardNewMacro(vtkStreamingTessellator);
void vtkStreamingTessellator::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "PointDimension: "
<< this->PointDimension[1] << " " << this->PointDimension[2] << " " << this->PointDimension[3] << endl;
os << indent << "EmbeddingDimension: "
<< this->EmbeddingDimension[1] << " " << this->EmbeddingDimension[2] << " " << this->EmbeddingDimension[3] << endl;
os << indent << "PrivateData: " << this->PrivateData << endl;
os << indent << "ConstPrivateData: " << this->ConstPrivateData << endl;
os << indent << "SubdivisionAlgorithm: " << this->Algorithm << endl;
os << indent << "VertexCallback: " << this->Callback0 << endl;
os << indent << "EdgeCallback: " << this->Callback1 << endl;
os << indent << "TriangleCallback: " << this->Callback2 << endl;
os << indent << "TetrahedronCallback: " << this->Callback3 << endl;
#ifdef PARAVIEW_DEBUG_TESSELLATOR
os << indent << "CaseCounts: " << this->CaseCounts << endl;
os << indent << "SubcaseCounts: " << this->SubcaseCounts << endl;
#endif // PARAVIEW_DEBUG_TESSELLATOR
}
vtkStreamingTessellator::vtkStreamingTessellator()
{
this->PrivateData = 0;
this->ConstPrivateData = 0;
this->Algorithm = 0;
this->Callback0 = DefaultFacet0Callback;
this->Callback1 = DefaultFacet1Callback;
this->Callback2 = DefaultFacet2Callback;
this->Callback3 = DefaultFacet3Callback;
this->MaximumNumberOfSubdivisions = 3;
for ( int i=0; i<4; ++i )
{
this->EmbeddingDimension[i] = i;
this->PointDimension[i] = i+3; // By default, FieldSize = 0
}"""
if QualityThang:
print >> genCode, """
if ( ! argyle )
{
argyle = vtkTetra::New();
goCallTheCops = argyle->GetPoints();
}"""
print >> genCode, """
}
vtkStreamingTessellator::~vtkStreamingTessellator()
{
if ( this->Algorithm )
this->Algorithm->UnRegister( this );
}
void vtkStreamingTessellator::SetEmbeddingDimension( int k, int d )
{
if ( d > 8 )
{
vtkErrorMacro( "Embedding dimension may not be > 8. (You asked for " << d << "). Truncating to 8." );
d = 8;
}
if ( k == 0 || k < -1 || k >= 4 )
{
vtkWarningMacro( "Invalid argument k=" << k );
return;
}
if ( k < 0 )
{
for ( k=0; k<4; k++ )
if ( this->EmbeddingDimension[k] != d )
{
this->PointDimension[k] += d - this->EmbeddingDimension[k] ;
this->EmbeddingDimension[k] = d;
this->Modified();
}
return;
}
if ( this->EmbeddingDimension[k] != d )
{
this->PointDimension[k] += d - this->EmbeddingDimension[k] ;
this->EmbeddingDimension[k] = d;
this->Modified();
}
}
void vtkStreamingTessellator::SetFieldSize( int k, int s )
{
if ( s > vtkStreamingTessellator::MaxFieldSize )
{
vtkErrorMacro( "Embedding dimension may not be > " << MaxFieldSize << ". (You asked for " << s << "). Truncating to " << MaxFieldSize );
s = vtkStreamingTessellator::MaxFieldSize;
}
if ( k == 0 || k < -1 || k >= 4 )
{
vtkWarningMacro( "Invalid argument k=" << k );
return;
}
if ( k < 0 )
{
// Use field size for all facet types (point, line, triangle, tet, ...)
for ( k=0; k<4; k++ )
if ( this->PointDimension[k] != s + this->EmbeddingDimension[k] + 3 )
{
this->PointDimension[k] = s + this->EmbeddingDimension[k] + 3;
this->Modified();
}
return;
}
if ( this->PointDimension[k] != s + this->EmbeddingDimension[k] + 3 )
{
this->PointDimension[k] = s + this->EmbeddingDimension[k] + 3;
this->Modified();
}
}
void vtkStreamingTessellator::SetMaximumNumberOfSubdivisions( int num_subdiv_in )
{
if ( this->MaximumNumberOfSubdivisions == num_subdiv_in )
return;
if ( num_subdiv_in < 0 )
{
vtkErrorMacro( "MaximumNumberOfSubdivisions must be 0 or greater (you requested " << num_subdiv_in << ")" );
return;
}
this->MaximumNumberOfSubdivisions = num_subdiv_in;
this->Modified();
}
void vtkStreamingTessellator::SetTetrahedronCallback( TetrahedronProcessorFunction f )
{
this->Callback3 = f ? f : DefaultFacet3Callback;
}
vtkStreamingTessellator::TetrahedronProcessorFunction vtkStreamingTessellator::GetTetrahedronCallback() const
{
return this->Callback3;
}
void vtkStreamingTessellator::SetTriangleCallback( TriangleProcessorFunction f )
{
this->Callback2 = f ? f : DefaultFacet2Callback;
}
vtkStreamingTessellator::TriangleProcessorFunction vtkStreamingTessellator::GetTriangleCallback() const
{
return this->Callback2;
}
void vtkStreamingTessellator::SetEdgeCallback( EdgeProcessorFunction f )
{
this->Callback1 = f ? f : DefaultFacet1Callback;
}
vtkStreamingTessellator::EdgeProcessorFunction vtkStreamingTessellator::GetEdgeCallback() const
{
return this->Callback1;
}
void vtkStreamingTessellator::SetVertexCallback( VertexProcessorFunction f )
{
this->Callback0 = f ? f : DefaultFacet0Callback;
}
vtkStreamingTessellator::VertexProcessorFunction vtkStreamingTessellator::GetVertexCallback() const
{
return this->Callback0;
}
void vtkStreamingTessellator::SetPrivateData( void* Private )
{
this->PrivateData = Private;
}
void* vtkStreamingTessellator::GetPrivateData() const
{
return this->PrivateData;
}
void vtkStreamingTessellator::SetConstPrivateData( const void* ConstPrivate )
{
this->ConstPrivateData = ConstPrivate;
}
const void* vtkStreamingTessellator::GetConstPrivateData() const
{
return this->ConstPrivateData;
}
void vtkStreamingTessellator::SetSubdivisionAlgorithm( vtkEdgeSubdivisionCriterion* a )
{
if ( a != this->Algorithm )
{
if ( this->Algorithm )
this->Algorithm->UnRegister( this );
this->Algorithm = a;
this->Modified();
if ( this->Algorithm )
this->Algorithm->Register( this );
}
}
vtkEdgeSubdivisionCriterion* vtkStreamingTessellator::GetSubdivisionAlgorithm()
{
return this->Algorithm;
}
const vtkEdgeSubdivisionCriterion* vtkStreamingTessellator::GetSubdivisionAlgorithm() const
{
return this->Algorithm;
}
// Returns true if || a0a1 || < || b0b1 ||
// We use this to test which triangulation has the best
// aspect ratio when there are 2 to choose from.
bool compareHopfCrossStringDist( const double* a0, const double* a1, const double* b0, const double* b1 )
{
double SqMagA = 0.;
double SqMagB = 0.;
for (int i=0; i<3; i++)
{
double tmp;
tmp = a0[i] - a1[i];
SqMagA += tmp * tmp;
tmp = b0[i] - b1[i];
SqMagB += tmp * tmp;
}
return SqMagA < SqMagB;
}
"""
if QualityThang:
print >> genCode, """
int vtkStreamingTessellator::BestTets( int* connOffsets, double** verts, int permOffset, int sgn ) const
{
int bestOffset = -1;
double bestQuality = 0.;
double currQuality;
while ( *connOffsets >= 0 )
{
int nTets = TetrahedralDecompositions[*connOffsets];
vtkIdType* conn = &TetrahedralDecompositions[*connOffsets +1];
int v;
currQuality = 0.;
for (v=0; v<nTets; ++v)
{
goCallTheCops->SetPoint( 0, verts[ vtkStreamingTessellator::PermutationsFromIndex[ permOffset ][ conn[sgn < 0 ? 1:0]] ] );
goCallTheCops->SetPoint( 1, verts[ vtkStreamingTessellator::PermutationsFromIndex[ permOffset ][ conn[sgn < 0 ? 0:1]] ] );
goCallTheCops->SetPoint( 2, verts[ vtkStreamingTessellator::PermutationsFromIndex[ permOffset ][ conn[2]] ] );
goCallTheCops->SetPoint( 3, verts[ vtkStreamingTessellator::PermutationsFromIndex[ permOffset ][ conn[3]] ] );
currQuality += vtkMeshQuality::TetFrobeniusNorm( argyle );
conn += 4;
}
currQuality /= nTets;
std::cout << currQuality << " " << *connOffsets << " ";
if ( bestQuality > currQuality || bestOffset < 0 )
{
bestQuality = currQuality;
bestOffset = *connOffsets;
}
++connOffsets;
}
std::cout << "Choose " << bestOffset << "\\n";
return bestOffset;
}
"""
else:
print >> genCode, """
int vtkStreamingTessellator::BestTets( int* vtkNotUsed(connOffsets), double** vtkNotUsed(verts), int vtkNotUsed(permOffset), int vtkNotUsed(sgn) ) const
{
// Re-run vtkStreamingTessellatorGenerator.py with QualityThang=1
// to get this implemented (along with on-the-fly quality improvement)
return 1;
}
"""
print >> genCode, """
void vtkStreamingTessellator::AdaptivelySample0Facet( double* v0 ) const
{
Callback0( v0, this->Algorithm, this->PrivateData, this->ConstPrivateData );
}
void vtkStreamingTessellator::AdaptivelySample1Facet( double* v0, double* v1, int maxDepth ) const
{
int edgeCode = 0;
double midpt0[11+vtkStreamingTessellator::MaxFieldSize];
// make valgrind happy
std::fill(midpt0,midpt0+this->PointDimension[1],0.);
if ( maxDepth-- > 0 )
{
for ( int i=0; i<this->PointDimension[1]; i++ )
midpt0[i] = (v0[i] + v1[i])/2.;
if ( this->Algorithm->EvaluateEdge( v0, midpt0, v1, 3+this->EmbeddingDimension[1] ) )
edgeCode += 1;
}
switch (edgeCode) {
// No edges to subdivide
case 0:
Callback1( v0, v1, this->Algorithm, this->PrivateData, this->ConstPrivateData );
break ;
// One edge to subdivide
case 1:
this->AdaptivelySample1Facet( v0, midpt0, maxDepth );
this->AdaptivelySample1Facet( midpt0, v1, maxDepth );
break;
}
}
void vtkStreamingTessellator::AdaptivelySample2Facet( double* v0, double* v1, double* v2, int maxDepth, int move ) const
{
int edgeCode = 0;
double midpt0[11+vtkStreamingTessellator::MaxFieldSize];
double midpt1[11+vtkStreamingTessellator::MaxFieldSize];
double midpt2[11+vtkStreamingTessellator::MaxFieldSize];
// Make valgrind happy
std::fill(midpt0,midpt0+this->PointDimension[2],0.);
std::fill(midpt1,midpt1+this->PointDimension[2],0.);
std::fill(midpt2,midpt2+this->PointDimension[2],0.);
if ( maxDepth-- > 0 )
{
for ( int i=0; i<this->PointDimension[2]; i++ )
{
midpt0[i] = (v0[i] + v1[i])/2.;
midpt1[i] = (v1[i] + v2[i])/2.;
midpt2[i] = (v2[i] + v0[i])/2.;
}
if ( (move & 1) && Algorithm->EvaluateEdge( v0, midpt0, v1, 3+this->EmbeddingDimension[2] ) )
edgeCode += 1;
if ( (move & 2) && Algorithm->EvaluateEdge( v1, midpt1, v2, 3+this->EmbeddingDimension[2] ) )
edgeCode += 2;
if ( (move & 4) && Algorithm->EvaluateEdge( v2, midpt2, v0, 3+this->EmbeddingDimension[2] ) )
edgeCode += 4;
#ifdef UGLY_ASPECT_RATIO_HACK
double dist0=0.;
double dist1=0.;
double dist2=0.;
double tmp;
for ( int j=0; j<3; ++j )
{
tmp = v0[j] - v1[j];
dist0 += tmp*tmp;
tmp = v1[j] - v2[j];
dist1 += tmp*tmp;
tmp = v2[j] - v0[j];
dist2 += tmp*tmp;
}
if ( edgeCode & 1 ) dist0 /= 2.;
if ( edgeCode & 2 ) dist1 /= 2.;
if ( edgeCode & 4 ) dist2 /= 2.;
#define MAR2 2.25
if ( (!(edgeCode & 1)) && (move&1) && ((dist0/dist1 > MAR2) || (dist0/dist2 > MAR2)) )
{
edgeCode += 1;
move &= 6;
}
if ( (!(edgeCode & 2)) && (move&2) && ((dist1/dist0 > MAR2) || (dist1/dist2 > MAR2)) )
{
edgeCode += 2;
move &= 5;
}
if ( (!(edgeCode & 4)) && (move&4) && ((dist2/dist1 > MAR2) || (dist2/dist0 > MAR2)) )
{
edgeCode += 4;
move &= 3;
}
#endif // UGLY_ASPECT_RATIO_HACK
}
#ifdef DBG_MIDPTS
if ( maxDepth == 0 )
{
fprintf( stderr, "midpoint of v%d (%g %g %g/%g %g %g)-v%d (%g %g %g/%g %g %g) = (%g %g %g/%g %g %g)\\n",
0, v0[0], v0[1], v0[2], v0[3], v0[4], v0[5],
1, v1[0], v1[1], v1[2], v1[3], v1[4], v1[5],
midpt0[0], midpt0[1], midpt0[2], midpt0[3], midpt0[4], midpt0[5]
);
fprintf( stderr, "midpoint of v%d (%g %g %g/%g %g %g)-v%d (%g %g %g/%g %g %g) = (%g %g %g/%g %g %g)\\n",
1, v1[0], v1[1], v1[2], v1[3], v1[4], v1[5],
2, v2[0], v2[1], v2[2], v2[3], v2[4], v2[5],
midpt1[0], midpt1[1], midpt1[2], midpt1[3], midpt1[4], midpt1[5]
);
fprintf( stderr, "midpoint of v%d (%g %g %g/%g %g %g)-v%d (%g %g %g/%g %g %g) = (%g %g %g/%g %g %g)\\n\\n",
2, v2[0], v2[1], v2[2], v2[3], v2[4], v2[5],
0, v0[0], v0[1], v0[2], v0[3], v0[4], v0[5],
midpt2[0], midpt2[1], midpt2[2], midpt2[3], midpt2[4], midpt2[5]
);
}
#endif // DBG_MIDPTS
switch (edgeCode)
{
// No edges to subdivide
case 0:
Callback2( v0, v1, v2, this->Algorithm, this->PrivateData, this->ConstPrivateData );
break ;
// One edge to subdivide
case 1:
this->AdaptivelySample2Facet( v0, midpt0, v2, maxDepth, move | 2 );
this->AdaptivelySample2Facet( midpt0, v1, v2, maxDepth, move | 4 );
break;
case 2:
this->AdaptivelySample2Facet( v0, v1, midpt1, maxDepth, move | 4 );
this->AdaptivelySample2Facet( v0, midpt1, v2, maxDepth, move | 1 );
break;
case 4:
this->AdaptivelySample2Facet( v0, v1, midpt2, maxDepth, move | 2 );
this->AdaptivelySample2Facet( midpt2, v1, v2, maxDepth, move | 1 );
break;
// Two edges to subdivide
case 3:
this->AdaptivelySample2Facet( midpt0, v1, midpt1, maxDepth, move | 4 );
if ( compareHopfCrossStringDist( v2, midpt0, v0, midpt1 ) )
{
this->AdaptivelySample2Facet( midpt0, midpt1, v2 , maxDepth, move | 5 );
this->AdaptivelySample2Facet( v0, midpt0, v2 , maxDepth, move | 2 );
}
else
{
this->AdaptivelySample2Facet( v0 , midpt0, midpt1, maxDepth, move | 6 );
this->AdaptivelySample2Facet( v0, midpt1, v2 , maxDepth, move | 1 );
}
break;
case 5:
this->AdaptivelySample2Facet( v0, midpt0, midpt2, maxDepth, move | 2 );
if ( compareHopfCrossStringDist( v2, midpt0, v1, midpt2 ) )
{
this->AdaptivelySample2Facet( midpt0, v1, v2 , maxDepth, move | 4 );
this->AdaptivelySample2Facet( midpt2, midpt0, v2 , maxDepth, move | 3 );
}
else
{
this->AdaptivelySample2Facet( midpt0, v1, midpt2, maxDepth, move | 6 );
this->AdaptivelySample2Facet( midpt2, v1, v2, maxDepth, move | 1 );
}
break;
case 6:
this->AdaptivelySample2Facet( midpt2, midpt1, v2, maxDepth, move | 1 );
if ( compareHopfCrossStringDist( v0, midpt1, v1, midpt2 ) )
{
this->AdaptivelySample2Facet( v0, midpt1, midpt2, maxDepth, move | 3 );
this->AdaptivelySample2Facet( v0, v1, midpt1, maxDepth, move | 4 );
}
else
{
this->AdaptivelySample2Facet( v0, v1, midpt2, maxDepth, move | 2 );
this->AdaptivelySample2Facet( midpt2, v1, midpt1, maxDepth, move | 5 );
}
break;
// Three edges to subdivide
case 7:
this->AdaptivelySample2Facet( midpt0, midpt1, midpt2, maxDepth, 7 );
this->AdaptivelySample2Facet( v0 , midpt0, midpt2, maxDepth, move | 2 );
this->AdaptivelySample2Facet( midpt0, v1 , midpt1, maxDepth, move | 4 );
this->AdaptivelySample2Facet( midpt2, midpt1, v2 , maxDepth, move | 1 );
break;
}
}
void vtkStreamingTessellator::AdaptivelySample3Facet( double* v0, double* v1, double* v2, double* v3, int maxDepth ) const
{
int edgeCode = 0;
double midpt0[11+vtkStreamingTessellator::MaxFieldSize];
double midpt1[11+vtkStreamingTessellator::MaxFieldSize];
double midpt2[11+vtkStreamingTessellator::MaxFieldSize];
double midpt3[11+vtkStreamingTessellator::MaxFieldSize];
double midpt4[11+vtkStreamingTessellator::MaxFieldSize];
double midpt5[11+vtkStreamingTessellator::MaxFieldSize];
double facept0[11+vtkStreamingTessellator::MaxFieldSize];
double facept1[11+vtkStreamingTessellator::MaxFieldSize];
double facept2[11+vtkStreamingTessellator::MaxFieldSize];
double facept3[11+vtkStreamingTessellator::MaxFieldSize];
// Make valgrind happy
std::fill(midpt0,midpt0+this->PointDimension[3],0.);
std::fill(midpt1,midpt1+this->PointDimension[3],0.);
std::fill(midpt2,midpt2+this->PointDimension[3],0.);
std::fill(midpt3,midpt3+this->PointDimension[3],0.);
std::fill(midpt4,midpt4+this->PointDimension[3],0.);
std::fill(midpt5,midpt5+this->PointDimension[3],0.);
double edgeLength2[6];
if ( maxDepth-- > 0 )
{
for ( int i=0; i<this->PointDimension[3]; i++ )
{
midpt0[i] = (v0[i] + v1[i])/2.;
midpt1[i] = (v1[i] + v2[i])/2.;
midpt2[i] = (v2[i] + v0[i])/2.;