-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathts2vmd.tcl
817 lines (680 loc) · 23.6 KB
/
ts2vmd.tcl
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
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# show_surf -mesh edge -color 1
#load_ts -in output1000.tsi -mesh surf -box on -color 12 #
# --- DISCLAIMER (by Melanie König m.konig@rug.nl): #
# #
# This script can read triangulated surface (TS) files from DTS #
# like .tsi and .q files as well as 3D mesh files. #
# #
# #
# As always, you can modify, redistribute and make everything you #
# want with these few lines of code; if you write major improvement, #
# please let me know/test it! #
# #
# #
# TCL Script to visualize and modify triangulated surface # # # # # #
# files like .tsi, .q from DTS simualtions # #
# or 3D mesh files like .STL, .OBJ # #
# # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
## TO DO ##
# add PLM
# change coordinates to A
# topo addbond instead of draw cyclinder??
### ----------------------------------------------------------------------------------------------------------------------------- USAGE
proc ts2vmd_usage {} {
puts ""
puts " USAGE"
puts "-------"
puts ""
puts "These few lines are given by the \"ts2vmd_usage\" command."
puts ""
puts "Loading TS or 3D mesh files:"
puts ""
puts " load_ts \[OPTIONS\]"
puts ""
puts "Writing TS or 3D mesh files:"
puts ""
puts " write_ts \[OPTIONS\]"
puts ""
puts "Modifying vertices by:"
puts ""
puts " adding domains"
puts " add_domain \[OPTIONS\]"
puts " adding inclusions"
puts " add_inc \[OPTIONS\]"
puts " adding exclusions"
puts " add_exc \[OPTIONS\]"
puts " removing domains/inclusions/exclusions"
puts " clean_ts \[OPTIONS\]"
puts ""
puts "Show/hide periodic box:"
puts ""
puts " show_box / hide_box"
puts ""
puts "Manully resize periodic box:"
puts ""
puts " resize_box \[OPTIONS\]"
puts ""
puts "Show surface:"
puts ""
puts " show_surf \[OPTIONS\]"
puts ""
}
ts2vmd_usage
display shadows on
display ambientocclusion on
### ---------------------------------------------------------------------------------------------------------------------------- UTILS
# check if file exists
proc file_exists { file } { if { [file exists $file] } { return -code 0 } else { return -code 1 "\nError: file $file does not exist.\n" } }
# check the if the number of entries matches the number from the keyword lines
proc check_entries {} {
if {$::nver == [llength $::vertices]} { return -code 0 } else { return -code 1 "\nError: Number vertices doesn't match.\n" }
if {$::ntri == [llength $::triangles]} { return -code 0 } else { return -code 1 "\nError: Number triangles doesn't match.\n" }
if {$::ninc == [llength $::inclusions]} { return -code 0 } else { return -code 1 "\nError: Number inclusions doesn't match.\n" }
if {$::nexc == [llength $::exclusions]} { return -code 0 } else { return -code 1 "\nError: Number exclusions doesn't match.\n" }
}
# process tsi file
proc load_ts { args } {
set args [join $args]
set args [split $args]
# default values
set mesh "off"
set color 15
set pbc "on"
# Arg parsing
if { [llength $args] < 2 } {
load_ts_usage
return -code 1
}
foreach { n m } $args {
if { $n == "-in" } {
set file_in $m
file_exists $file_in
set format [ lindex [split $file_in "."] 1 ]
}
if { $n == "-mesh" } { set mesh $m }
if { $n == "-color" } { set color $m }
if { $n == "-box" } { set pbc $m }
}
# global variables defining the TS file
set ::vertices {}
set ::triangles {}
set ::inclusions {}
set ::exclusions {}
switch $format {
tsi {
load_tsi $file_in
check_entries
get_domains
}
q { load_q $file_in }
xyz { load_xyz $file_in }
stl { load_stl $file_in }
}
gen_coords $file_in
# if inclusions and/or exclusions are present in the input file
# they will be colored accordingly
if { [llength $::domains] > 0 } { show_dom }
if { [llength $::inclusions] > 0 } { show_inc }
if { [llength $::exclusions] > 0} { show_exc }
# show box if wanted
if { $pbc == "on" } { show_box }
# show surface mesh if wanted
if { $mesh != "off" } { show_surf [ format "-mesh %s -color" $mesh $color]}
puts [format "\nYour triangulated surface %s consits of \n%s vertices connected by %s triangles\nwith %s inclusions and %s exclusions \n" $file_in $::nver $::ntri $::ninc $::nexc]
}
proc load_tsi { file_in } {
set tsi [open $file_in r]
set ::ninc 0
set ::nexc 0
# read the different sections of a .tsi file
while {[gets $tsi line] > 0} {
if {[string match box* $line]} {
set ::box "[lindex $line 1] [lindex $line 2] [lindex $line 3]"
} elseif {[string match vertex* $line]} {
set ::nver "[lindex $line 1]"
while {[gets $tsi line] >= 0 } {
if {[string match triangle* $line]} {
set ::ntri "[lindex $line 1]"
while {[gets $tsi line] >= 0} {
if {[string match inclusion* $line]} {
set ::ninc "[lindex $line 1]"
while {[gets $tsi line] >= 0} {
if {[string match exclusion* $line]} {
set ::nexc "[lindex $line 1]"
while {[gets $tsi line] >= 0} {
if {[llength $line] > 0} {lappend ::exclusions $line}
}
}
if {[llength $line] > 0} {lappend ::inclusions $line}
}
}
if {[llength $line] > 0} {lappend ::triangles $line}
}
}
if {[llength $line] > 0} {lappend ::vertices $line}
}
}
}
}
# load the converted xzy file into vmd
proc load_xyz { file_in } {
mol new $file_in
mol delrep 0 top
mol representation vdw 0.200000 100.000000
mol addrep 22
}
proc load_stl { file_in } {
set stl [open $file_in r]
set ::facet_normal {}
set ::ntri 0
set ::nver 0
set ::domains {}
set ::ninc 0
set ::nexc 0s
set ::box { 100 100 100 }
# read the different sections of an ascii .stl file
while {[gets $stl line] > 0} {
if {[string match solid* $line]} {
if { [lindex $line 1] != ""} { set name "[lindex $line 1]" }
} elseif {[string match facet* $line]} {
lappend ::facet_normal [lrange $line 1 end]
set triangle {$::ntri}
} elseif {[string match vertex* $line]} {
if { [lsearch $::vertices [lrange $line 1 end]] == -1 } {
set index [llength $::vertices]
lappend ::vertices [list $index [lrange $line 1 end]]
incr ::nver
lappend triangle $index
} else { lappend triangle [lsearch $::vertices [lrange $line 1 end]] }
} elseif {[string match endface* $line]} {
incr ::ntri
lappend ::triangles $triangle
}
}
}
proc get_domains {} {
set ::domains {}
foreach vertex $::vertices {
if { [lindex $vertex 4] != ""} { lappend ::domains [list [lindex $vertex 0] [lindex $vertex 4]] }
}
}
proc load_ts_usage {} {
puts ""
puts " USAGE"
puts "-------"
puts ""
puts "Loading TS or 3D mesh files:"
puts ""
puts " load_ts \[OPTIONS\]"
puts ""
puts "Options and default values:"
puts " -in TS.tsi input file (*.tsi | *.q) "
puts " -mesh \"off\" draw edges AND/OR faces (off | edge | face | both)"
puts " -color \"red\" color (color name or VMD-defined ID) for mesh"
puts " -box \"on\" show periodic box (on | off)"
puts ""
}
#----------------------------------------------------------------------------------------------------------------------------------------
# VMD representation
# max. 4 different representations will be generated
# 1) All vertices
# 2) Domains
# 3) Inclusions
# 4) Exclusions
#----------------------------------------------------------------------------------------------------------------------------------------
proc gen_coords { name } {
# genearte dummy coordinated {0 0 0}
set new [mol new atoms $::nver]
animate dup $new
set sel [atomselect $new all]
set vertex 0
set newcoords {}
# modify the dummy coordinates
foreach coord [$sel get {x y z}] {
set offset "[lindex [lindex $::vertices $vertex] 1] [lindex [lindex $::vertices $vertex] 2] [lindex [lindex $::vertices $vertex] 3]"
lappend newcoords [vecadd $coord $offset]
incr vertex
}
$sel set {x y z} $newcoords
set molid [molinfo top]
mol rename $molid $name
mol delrep $molid top
mol representation VDW 0.200000 100.000000
mol addrep $molid
display resetview
mol modcolor 0 $molid ColorID 9
}
proc show_dom {} {
set molid [molinfo top]
set repid [molinfo $molid get numreps]
# add domains using different names and IDs
foreach domain $::domains {
set sel [atomselect top [format "index %s" [lindex $domain 0]]]
#if {[lindex $domain 1] != 0} {$sel set name Domain}
$sel set resname Domain
$sel set type [lindex $domain 1]
}
mol addrep $molid
#display resetview
mol representation VDW 0.201000 100.000000
mol modcolor $repid $molid Type
mol modselect $repid $molid resname Domain
}
proc show_inc {} {
set molid [molinfo top]
set repid [molinfo $molid get numreps]
# add inclusions using different names
foreach inc $::inclusions {
set sel [atomselect top [format "index %s" [lindex $inc 2]]]
$sel set name Inc
$sel set resid [lindex $inc 1]
}
mol addrep $molid
#display resetview
mol representation VDW 0.205000 100.000000
mol modcolor $repid $molid Resid
mol modselect $repid $molid name Inc
}
proc show_exc {} {
set molid [molinfo top]
set repid [molinfo $molid get numreps]
# add exclusions using different names
foreach exc $::exclusions {
set sel [atomselect top [format "index %s" [lindex $exc 1]]]
$sel set name Exc
}
mol addrep $molid
#display resetview
mol representation VDW 0.205000 100.000000
mol modcolor $repid $molid ColorID 8
mol modselect $repid $molid name Exc
}
#----------------------------------------------------------------------------------------------------------------------------------------
# Modify triangulated surface
# You can add domains, inclusions and exclusions or
# remove all modifications which will give you the blank vertices
#----------------------------------------------------------------------------------------------------------------------------------------
proc vertex_avail {vertex} {
foreach inc $::inclusions exc $::exclusions {
if {[lindex $inc 2] == $vertex || [lindex $exc 1] == $vertex} {return -code 1 "\nError: The vertex is already occupied by another inclusion or exclusion.\n"}
}
}
proc add_domain { args } {
set args [join $args]
set args [split $args]
# default values
set domain_radius 0
set overwrite "no"
if {[ llength $args ] < 4 } {
add_domain_usage
return -code 1
}
foreach { n m } $args {
if { $n == "-id" } { set id $m }
if { $n == "-vertex" } { set vertex $m }
if { $n == "-within" } { set domain_radius $m }
if { $n == "-ignore" } { set overwrite $m }
}
# here we do not check if the vertex is available
# a vertex can contain both an inclusion/exclusin and a domain
set sel [atomselect top [format "within %s of index %s" $domain_radius $vertex]]
$sel set resname Domain
if { [llength $::domains] == 0 } {
foreach vertex [$sel get index] { lappend ::domains [list $vertex $id] }
$sel set type $id
show_dom
} else {
foreach vertex [$sel get index] {
set sel2 [atomselect top [format "index %s" $vertex]]
set id_used [lsearch -index 0 $::domains $vertex]
if { $id_used == -1 } {
lappend ::domains [list $vertex $id]
$sel2 set type $id
} else { if { $overwrite == "yes" } {
set ::domains [ lreplace $::domains $id_used $id_used [list $vertex $id] ]
$sel2 set type $id
}
}
}
}
}
proc add_domain_usage {} {
puts ""
puts " USAGE"
puts "-------"
puts ""
puts "Adding vertex domains:"
puts ""
puts " add_domain \[OPTIONS\]"
puts ""
puts "Options and default values:"
puts " -id 1 domain id "
puts " -vetex 0 vertex index"
puts " -ignore \"no\" overwrite existing domains"
puts " -within 3 included vertices within defined radius"
puts ""
}
proc add_inc { args } {
set args [join $args]
set args [split $args]
if {[llength $args] != 4} {return -code 1 "\nError: Please provide the inclusion type (-type) and vertex index (-vertex).\n"}
foreach { n m } $args {
if { $n == "-type" } { set type $m }
if { $n == "-vertex" } { set vertex $m }
}
# check if vertex is free
vertex_avail $vertex
set sel [atomselect top [format "index %s" $vertex]]
$sel set name Inc
$sel set resid [lindex $type]
if { [llength $::inclusions] == 0 } {
lappend ::inclusions [format "%s %s %s 0 1" [expr [lindex $::inclusions end 0] + 1 ] $type $vertex]
show_inc
} else { lappend ::inclusions [format "%s %s %s 0 1" [expr [lindex $::inclusions end 0] + 1 ] $type $vertex] }
}
proc add_exc { args } {
set args [join $args]
set args [split $args]
if {[llength $args] != 4} {return -code 1 "\nError: Please provide the exclusion radius (-radius) and vertex index (-vertex).\n"}
foreach { n m } $args {
if { $n == "-radius" } { set radius $m }
if { $n == "-vertex" } { set vertex $m }
}
# check if vertex is free
vertex_avail $vertex
set sel [atomselect top [format "index %s" $vertex]]
$sel set name Exc
if { [llength $::exclusions] == 0 } {
lappend ::exclusions [format "%s %s %s" [expr [lindex $::exclusions end 0] + 1 ] $vertex $radius]
show_exc
} else { lappend ::exclusions [format "%s %s %s" [expr [lindex $::exclusions end 0] + 1 ] $vertex $radius] }
#puts $::exclusions
}
#----------------------------------------------------------------------------------------------------------------------------------------
# clean vertices
#----------------------------------------------------------------------------------------------------------------------------------------
proc clean_ts { args } {
set args [join $args]
set args [split $args]
set molid [molinfo top]
if {[llength $args] < 2 } {return -code 1 "\nError: Please provide selection of what you want to remove (-vertex | -domains | -inclusions | -exclusions | -all ).\n"}
foreach { n m } $args {
if { $n == "-vertex" } { set vertex $m }
if { $n == "-domain" } { set dom_id $m}
if { $n == "-inclusion" } { set type_id $m}
if { $n == "-exclusion" } { set exc_id $m }
}
if { [info exists vertex] } {
if { $vertex == "all" } {
set ::domains {}
set ::inclusions {}
set ::exclusions {}
# remove reprensentations
for {set i 1} {$i < [molinfo $molid get numreps] } {incr i} {mol delrep 1 $molid}
# update entries
set sel [atomselect top "all"]
$sel set name "X"
$sel set resname "UNK"
$sel set resid 0
$sel set type "X"
} else {
# remove entries and update presentation
clean_dom $vertex 0
clean_inc $vertex 2
clean_exc $vertex 1
}
}
if { [info exists dom_id] } {
if { $dom_id == "all" } {
set ::domains {}
# remove reprensentations
for {set i 1} {$i < [molinfo $molid get numreps] } {incr i} {
if { [molinfo top get "{selection $i}"] == "{resname Domain}"} { mol delrep $i $molid }
}
# update entries
set sel [atomselect top "resname Domain"]
$sel set resname "UNK"
$sel set type "X"
} else { clean_dom $dom_id 1 }
}
if { [info exists type_id] } {
if { $type_id == "all" } {
set ::inclusions {}
# remove reprensentations
for {set i 1} {$i < [molinfo $molid get numreps] } {incr i} {
if { [molinfo top get "{selection $i}"] == "{name Inc}"} { mol delrep $i $molid }
}
# update entries
set sel [atomselect top "name Inc"]
$sel set name "X"
$sel set resid 0
} else { clean_inc $type_id 1 }
}
if { [info exists exc_id] } {
if { $exc_id == "all" } {
set ::exclusions {}
# remove reprensentations
for {set i 1} {$i < [molinfo $molid get numreps] } {incr i} {
if { [molinfo top get "{selection $i}"] == "{name Exc}"} { mol delrep $i $molid }
}
# update entries
set sel [atomselect top "name Exc"]
$sel set name "X"
} else { clean_exc $exc_id 1 }
}
}
proc clean_dom { val col } {
set new_dom {}
foreach domain $::domains { if { [lindex $domain $col ] != $val } {
lappend new_dom $domain
} else {
set sel [atomselect top [format "index %s" [lindex $domain 0 ] ] ]
$sel set type "X"
}
}
set ::domains $new_dom
}
proc clean_inc { val col } {
set new_inc {}
foreach inc $::inclusions { if { [lindex $inc $col] != $val } {
lappend new_inc $inc
} else {
set sel [atomselect top [format "index %s" [lindex $inc 2 ] ] ]
$sel set name "X"
$sel set resid 0
}
}
set ::inclusions $new_inc
}
proc clean_exc { val col } {
set new_exc {}
foreach exc $::exclusions { if { [lindex $exc $col] != $val } {
lappend new_exc $exc
} else {
set sel [atomselect top [format "index %s" [lindex $exc 1 ] ] ]
$sel set name "X"
}
}
set ::exclusions $new_exc
}
#----------------------------------------------------------------------------------------------------------------------------------------
# Write to file
#----------------------------------------------------------------------------------------------------------------------------------------
proc write_ts { args } {
set args [join $args]
set args [split $args]
foreach { n m } $args {
if { $n == "-out" } {
set file_out $m
set format [ lindex [split $m "."] 1 ]
}
}
switch $format {
tsi { write_tsi $file_out }
xyz { write_xyz $file_out }
}
}
proc write_tsi { file_out } {
set tsi [open $file_out w]
puts $tsi "version 1.1"
puts $tsi [format "box %s" $::box]
puts $tsi [format "vertex %s" [llength $::vertices]]
foreach vertex $::vertices {
set domain_id [lsearch -index 0 $::domains [lindex $vertex 0]]
if { $domain_id == -1 } { set domain 0
} else { set domain [lindex $::domains $domain_id 1]}
puts $tsi [format "%s %s" $vertex $domain ]
}
puts $tsi [format "triangle %s" [llength $::triangles]]
foreach triangle $::triangles { puts $tsi [format "%s" $triangle]}
puts $tsi [format "inclusion %s" [llength $::inclusions]]
foreach inclusion $::inclusions { puts $tsi [format "%s" $inclusion]}
puts $tsi [format "exclusion %s" [llength $::exclusions]]
foreach exclusion $::exclusions { puts $tsi [format "%s" $exclusion]}
close $tsi
}
# converts tsi vertices to xyz file format
proc write_xyz { file_out } {
set xyz [open $file_out w]
puts $xyz [format "%s\n" $::nver]
set elements "C O N P S He Li Be B F Ne Na Mg Al Si Cl Ar"
foreach vertex $::vertices {
if {[llength $vertex] != 0} {
if {[lindex $vertex 4] == ""} { set domain 0
} else {set domain [lindex $vertex 4]}
puts $xyz [format "%s %s %s %s" [lindex $elements $domain] [lindex $vertex 1] [lindex $vertex 2] [lindex $vertex 3]]
}
}
close $xyz
}
#----------------------------------------------------------------------------------------------------------------------------------------
# Draw pbc box
#----------------------------------------------------------------------------------------------------------------------------------------
proc show_box {} {
pbc set $::box -all -molid top
pbc box
}
proc hide_box {} { pbc box -off }
proc resize_box { args } {
set args [join $args]
set args [split $args]
if {[ llength $args ] < 6 } {
resize_box_usage
return -code 1
}
foreach { n m } $args {
if { $n == "-x" } { set x $m }
if { $n == "-y" } { set y $m }
if { $n == "-z" } { set z $m }
}
# set new box
remove
set ::box [list $x $y $z]
set center_box [list [expr $x/2] [expr $y/2] [expr $z/2]]
# move the coordinates
set sel [atomselect top all]
set minus_com [vecsub $center_box [measure center $sel]]
$sel moveby $minus_com
show_box
display resetview
# uppate coordinates of vertices
set vertex 0
foreach coord [$sel get {x y z}] {
set x "[format "%.10f" [lindex $coord 0]]"
set y "[format "%.10f" [lindex $coord 1]]"
set z "[format "%.10f" [lindex $coord 2]]"
set ::vertices [ lreplace $::vertices $vertex $vertex "[format "%s %s %s %s" $vertex $x $y $z]" ]
incr vertex
}
}
proc resize_box_usage {} {
puts ""
puts " USAGE"
puts "-------"
puts ""
puts "Resizing the periodic box (only rectangular boxes):"
puts ""
puts " resize box \[OPTIONS\]"
puts ""
puts "Options and default values:"
puts " -x 10 size of box in x dimension"
puts " -y 10 size of box in y dimension"
puts " -z 10 size of box in z dimension"
puts ""
}
#----------------------------------------------------------------------------------------------------------------------------------------
# Draw surface and/or bonds of triangles
#----------------------------------------------------------------------------------------------------------------------------------------
proc tri_coords {vertices triangle} {
set vert1 "[lindex $triangle 1]"
set vert2 "[lindex $triangle 2]"
set vert3 "[lindex $triangle 3]"
set points {}
if {[llength $triangle] != 0} {
lappend points "[lindex [lindex $vertices $vert1] 1] [lindex [lindex $vertices $vert1] 2] [lindex [lindex $vertices $vert1] 3]"
lappend points "[lindex [lindex $vertices $vert2] 1] [lindex [lindex $vertices $vert2] 2] [lindex [lindex $vertices $vert2] 3]"
lappend points "[lindex [lindex $vertices $vert3] 1] [lindex [lindex $vertices $vert3] 2] [lindex [lindex $vertices $vert3] 3]"
}
return $points
}
proc show_surf { args } {
set args [join $args]
set args [split $args]
# default values
set color 15
foreach { n m } $args {
if { $n == "-mesh" } { set mesh $m }
if { $n == "-color" } { set color $m }
}
draw color $color
draw material AOEdgy
foreach triangle $::triangles {
set points [tri_coords $::vertices $triangle]
# show edges and or faces
switch $mesh {
edge { draw_lines $points }
face { draw_triangles $points }
both {
draw_lines $points
draw_triangles $points
}
}
}
}
# draw triangle faces between vertices
proc draw_triangles { points } {
set cross_pbc "no"
for {set i 0} {$i < 3} {incr i} {
for {set j [expr $i + 1] } {$j < 3} {incr j} {
for {set k 0} {$k < 3} {incr k} {
set coord [expr abs([lindex [vecsub [lindex $points $i] [lindex $points $j]] $k])]
set half_box [expr [lindex $::box $k]/2]
if { $coord > $half_box } {
set cross_pbc "yes"
break
}
}
}
}
if { $cross_pbc == "no" } { draw triangle [lindex $points 0] [lindex $points 1] [lindex $points 2] }
}
# draw triangles edges between vertices
proc draw_lines { points } {
set box_x [lindex $::box 0]
set box_y [lindex $::box 1]
set box_z [lindex $::box 2]
for {set i 0} {$i < 3} {incr i} {
for {set j [expr $i + 1] } {$j < 3} {incr j} {
set x [expr abs([lindex [vecsub [lindex $points $i] [lindex $points $j]] 0])]
set y [expr abs([lindex [vecsub [lindex $points $i] [lindex $points $j]] 1])]
set z [expr abs([lindex [vecsub [lindex $points $i] [lindex $points $j]] 2])]
if { $x < [expr $box_x/2] && $y < [expr $box_y/2] && $z < [expr $box_z/2] } {
draw cylinder [lindex $points $i] [lindex $points $j] radius 0.1
} else { continue }
}
}
}
proc remove {} {draw delete all}