Skip to content
Snippets Groups Projects
loadRigidBody.procs.tcl 18.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • cmaffeo2's avatar
    cmaffeo2 committed
    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
    proc dcd {dcdGlob args} {
        set molid ""
        set defaults { {skip 1} {beg 0} {end 0 } }
        foreach {var val} [join $defaults] { set $var $val }
        foreach {var val} [join $args] { set $var $val }
    
        if { [string equal $molid top] || [string equal $molid ""] } { set molid [molinfo top] }
    
        set startFrames [molinfo $molid get numframes] ;# use this to return the number of frames loaded
        set framesLeft 1;
    
        foreach glob $dcdGlob {
            set dcds [sortFileGlob $glob]
            foreach dcd $dcds { ;# loop through dcds in dcdGlob
                if { $framesLeft == 0 } { break }
                if { ![file exists $dcd] } { continue }
                set dcdFrames [numframes $dcd]
                puts "reading $dcd ($dcdFrames)"       
    
                ## set beg and end
                if { $beg > 0 } {
                    set animateBeg $beg
                } else {
                    set animateBeg 0
                }
                   
    
                set animateEnd ""
                if { $dcdFrames > $end && $end > 0} {
                    set animateEnd $end
                    set framesLeft 0 ;# break if no frames left!
                }
    
                ## update beg & end
                set beg [expr {$beg - $dcdFrames}]
                set end [expr {$end - $dcdFrames}]
               
                if { $animateBeg >= $dcdFrames } { continue }
                set beg [expr {$skip-1-int(fmod( ($dcdFrames-$animateBeg),$skip ))}] ;# don't load dcds that are too smaller than skip
               
                ## do the loadin
                set animateBeg "beg $animateBeg"
                if {$animateEnd > 0} { set animateEnd "end $animateEnd" }
                # puts "animate read dcd $dcd skip $skip $animateBeg $animateEnd waitfor all $molid"
                uplevel "animate read dcd $dcd skip $skip $animateBeg $animateEnd waitfor all $molid"
            }
        }
        set finalFrames [molinfo $molid get numframes]
        expr { $finalFrames - $startFrames }
    }
    proc numframes { args } {
        set r 0
        foreach dcd $args {
            set n [exec catdcd -num $dcd | awk "/^Total frames:/ \{ print \$3 \}"]
            set r [expr {$r + $n}]
        }
        return $r
    }
    proc sortFileGlob {fileGlob} {
        set files [glob $fileGlob]
    
        set sortCmd {{f1 f2} {
            set l1 [string length $f1]
            set l2 [string length $f2]
            set r [expr {($l1 > $l2) - ($l1 < $l2)}]
            if {$r == 0} { set r [string compare $f1 $f2] }
            return $r
        }}
    
        set sortCmd "apply {$sortCmd}"
        lsort -command $sortCmd $files
    }
    
    proc loadTrajectory {files {attachID top} {skip 1} {beg 0} {end -1}} {
        variable trans
        variable trans_orig
        variable trans_inv
        variable molToKey
        variable lastFrame
        variable rigidBodyIDs
        set rigidBodyIDs ""
    
        if { [string equal $attachID top] } {
    	set attachID [molinfo top]
        }
        
        array set trans [parseRigidBodyTrajectoryFiles $files $skip $beg $end]
        set keys [array names trans]
        set rbFrames [llength $trans([lindex $keys 0])]
        set numframes [molinfo $attachID get numframes]
    
        if { $rbFrames < $numframes } {
    	# error "Read $rbFrames rigid body frames < $numframes all-atom frames"
    	puts "WARNING: Read $rbFrames rigid body frames < $numframes all-atom frames; something isn't right"
        }
        while { $rbFrames < $numframes } {
    	foreach key [array names trans] {
    	    lappend trans($key) [lindex $trans($key) end]
    	}
    	incr rbFrames
        }
    
        if { $rbFrames < $numframes } {
    	error "Read $rbFrames rigid body frames < $numframes all-atom frames"
        }
    
        array set trans_orig [array get trans]
        calcTransInv
    
        set topID [molinfo top]
        if { [catch {
    	set lastFrame $::vmd_frame($attachID)
    	foreach key [array names trans] {
    	    ## load ssb
    	    set ID [mol new 1eyg.psf]; mol addfile 1eyg.pdb
    	    # mol addfile ssb.pdb
    	    molinfo $ID set {a b c} [molinfo $attachID get {a b c}]
    
    	    lappend rigidBodyIDs $ID
    	    set molToKey($ID) $key
    	    [atomselect $ID all] move [lindex $trans($key) $lastFrame]
    	}
    	
    	variable ::vmd_frame
    	foreach elem [trace info variable ::vmd_frame($attachID)] {
    	    foreach {opList cmd} $elem { 
    		trace remove variable ::vmd_frame($attachID) $opList $cmd
    	    }
    	}
    	trace variable ::vmd_frame($attachID) w frameChange
        }]} {
    	puts "WARNING: failed to set trace on rigidBody positions"
        }
        mol top $topID
        return $rigidBodyIDs
    }
    
    proc loadTrajectoryRbFrame {files {attachID top} {skip 1} {beg 0} {end -1} } {
        variable trans
        variable trans_orig
        variable trans_inv
        variable molToKey
        variable lastFrame
        variable rigidBodyIDs
        variable centerRbID
        set rigidBodyIDs ""
    
        if { [string equal $attachID top] } {
    	set attachID [molinfo top]
        }
        
        array set trans [parseRigidBodyTrajectoryFiles $files $skip $beg $end]
        # array set trans [parseRigidBodyTrajectoryFiles $files ]
        set keys [array names trans]
        set rbFrames [llength $trans([lindex $keys 0])]
        set numframes [molinfo $attachID get numframes]
        if { $rbFrames < $numframes } {
    	error "Read $rbFrames rigid body frames < $numframes all-atom frames"
        }
    
        array set trans_orig [array get trans]
        puts here
        calcTransInv
        puts here
    
        set topID [molinfo top]
        if { [catch {
    	set lastFrame $::vmd_frame($attachID)
    	foreach key [array names trans] {
    	    ## load ssb
    	    puts hi
    	    set ID [mol new 1eyg.psf]; mol addfile 1eyg.pdb
    	    # mol addfile ssb.pdb
    	    molinfo $ID set {a b c} [molinfo $attachID get {a b c}]
    
    	    lappend rigidBodyIDs $ID
    	    set molToKey($ID) $key
    	    # [atomselect $ID all] move [lindex $trans($key) $lastFrame]
    	}
    
    	## rb frame init
    	if { ! [info exists centerRbID] } { set centerRbID [lindex $rigidBodyIDs 0] }
    	# set key $molToKey($centerRbID);
    	# set m [lindex $trans_inv($key) $lastFrame]
    	# foreach tID [molinfo list] {
    	#    [atomselect $tID all] move $m
    	# }
    
    	if { [catch { ## do transform for frame ; TODO undo and redo with smoothRot call
    	    set rbID $centerRbID
    	    set key $molToKey($rbID)	
    	    foreach tID [molinfo list] {
    		if { [molinfo $tID get active] && [molinfo $tID get numframes] > 1 } {
    		    set sel [atomselect $tID all]		
    		    frameLoop frame molid $tID {
    			$sel frame $frame
    			$sel move [lindex $trans_inv($key) $frame]
    		    }
    		}
    	    }
    	}]} { puts "WARNING: failed to initialize to rigidBody" }
    
    	
    	variable ::vmd_frame
    	foreach elem [trace info variable ::vmd_frame($attachID)] {
    	    foreach {opList cmd} $elem { 
    		trace remove variable ::vmd_frame($attachID) $opList $cmd
    	    }
    	}
    	# trace variable ::vmd_frame($attachID) w frameChange
    	trace variable ::vmd_frame($attachID) w frameChangeRbFrame
        }]} {
    	puts "WARNING: failed to set trace on rigidBody positions"
        }
        mol top $topID
        return $rigidBodyIDs
    }
    
    proc ::frameChangeCallback {frame} {}
    proc frameChange {varname ID rw} {
        variable rigidBodyIDs
        variable trans
        variable trans_inv
        variable molToKey
        variable lastFrame
        set frame $::vmd_frame($ID)
        if { [catch { 
    	foreach rbID $rigidBodyIDs {
    	    set key $molToKey($rbID) 
    	    set sel [atomselect $rbID all]
    	    $sel move [lindex $trans_inv($key) $lastFrame]
    	    $sel move [lindex $trans($key) $frame]
    	}
        }]} {
    	puts "WARNING: failed to update rigidBody positions"
        }
        catch {::frameChangeCallback $frame}
        set lastFrame $frame
    }
    
    proc frameChangeRbFrame {varname ID rw} {
        variable rigidBodyIDs
        variable trans
        variable trans_inv
        variable molToKey
        variable lastFrame
        variable centerRbID
    
    
        set frame $::vmd_frame($ID)
    
        if { [catch { ## undo transform to all RBs for last frame
        	set rbID $centerRbID
        	set key $molToKey($rbID)	
        	foreach tID $rigidBodyIDs {
        	    set sel [atomselect $tID all]
    	    $sel move [lindex $trans($key) $lastFrame]
        	}
        }]} {
        	puts "WARNING: failed to update rigidBody positions"
        }
    
    
        if { [catch { 
    	foreach rbID $rigidBodyIDs {
    	    set key $molToKey($rbID)
    	    set sel [atomselect $rbID all]
    	    # $sel move [lindex $trans_inv($key) $lastFrame]
    	    # $sel move [lindex $trans($key) $frame]
    	    $sel move [transmult [lindex $trans($key) $frame] [lindex $trans_inv($key) $lastFrame]]
    	}
        }]} {
    	puts "WARNING: failed to update rigidBody positions"
        }
        if { [catch { ## do global transform for frame
    	set rbID $centerRbID
    	set key $molToKey($rbID)	
    	foreach tID $rigidBodyIDs {
    	    set sel [atomselect $tID all]
    	    $sel move [lindex $trans_inv($key) $frame]
    	}
        }]} { puts "WARNING: failed to update rigidBody positions" }
        set lastFrame $frame
    }
    
    ## support 
    proc parseRigidBodyTrajectoryFiles {files {skip 1} {beg 0} {end -1}} {
        set file [lindex $files 0]
        set files [lrange $files 1 end]
        
        
        # set counter [expr $skip-1]
        set counter 0
        lassign [parseRigidBodyTrajectory $file $skip $counter $beg $end] counter tmp
        # puts "tmp: $tmp"
        # puts "tmp has [llength $tmp] entries"
        # puts "tmp has [llength [join $tmp]] entries"
        array set trans $tmp
        set keys [array names trans]
    
        foreach file $files {
    	lassign [parseRigidBodyTrajectory $file $skip $counter $beg $end] counter tmp
    	array set newTrans $tmp
    	if { [lsort $keys] != [lsort [array names newTrans]] } {
    	    puts stderr "file $file does not share the same rigid bodies"
    	    puts stderr "('[lsort $keys]' != '[lsort [array names newTrans]]')" 
    	    exit 1
    	}
    	foreach key $keys {
    	    set trans($key) [join "{$trans($key)} {$newTrans($key)}"]
    	}
        }
        return [array get trans]
    }
    
    proc parseRigidBodyTrajectory {file {skip 1} {counter 0} {beg 0} {end -1}} {
        ## parse and set transformation matrix timeseries
    
        array set trans ""
        array set keyCounter ""
    
        set ch [open $file]
        while { [gets $ch line] > 0 } {
    	## ignore header
    	if { [regexp {^\W*#} $line] } {
    	    continue
    	}
    	# 	lassign [lindex $line 0] currentStep
    	lassign [lindex $line 1] key
    
    	if { ! [info exists trans($key)] } {
    	    set trans($key) ""
    	}
    	if { ! [info exists keyCounter($key)] } {
    	    set keyCounter($key) $counter
    	} else {
    	    incr keyCounter($key)
    	}
    	if { fmod($keyCounter($key),$skip) > 0.1 || $keyCounter($key) < $beg } { continue }
    
    	if { $end >= 0 && $keyCounter($key) > $end + 2*$skip } { break } ;# break when you are well past $end
    	if { $end >= 0 && $keyCounter($key) > $end } { continue } ;# not break in case there are multiple RBs
    	set m ""
    	lappend m [join "[lrange $line 5 7] 0"]
    	lappend m [join "[lrange $line 8 10] 0"]
    	lappend m [join "[lrange $line 11 13] 0"]
    	lappend m {0 0 0 1}
    	# set m [transmult [transoffset [lrange $line 2 4]] [measure inverse $m]]
    	set m [transmult [transoffset [lrange $line 2 4]] $m]
    	lappend trans($key) "$m"
    	# puts "Added to trans($key); length = [llength $trans($key)]"
        }
        close $ch
        # puts "trans(ssb1): $trans(ssb1)"
        # puts "array get trans: [array get trans]"
        return "[lindex [array get keyCounter] 1] {[array get trans]}"
    }
    
    
    ## smoothing
    proc matrixToQuaternion {m} {
        ## http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Rotation_matrix_.E2.86.94_quaternion
        lassign [lindex $m 0] Axx Axy Axz x1
        lassign [lindex $m 1] Ayx Ayy Ayz x2 
        lassign [lindex $m 2] Azx Azy Azz x3
    
        # set denominator1 [expr 0.5*sqrt(1+$Axx+$Ayy+$Azz )]
        set denominator1 [expr 1+$Axx+$Ayy+$Azz]
        set denominator1 [expr ($denominator1 < 0) ? 0 : $denominator1]
        set denominator1 [expr 0.5*sqrt($denominator1)]
        
        set denominator2 [expr 1+$Axx-$Ayy-$Azz]
        set denominator2 [expr ($denominator2 < 0) ? 0 : $denominator2]
        set denominator2 [expr 0.5*sqrt($denominator2)]
    
        # set denominator3 [expr 0.5*sqrt(1+$Axx+$Ayy+$Azz )]
        if {$denominator1 > $denominator2} {
    	set q4 $denominator1
    	set q1 [expr ($Azy-$Ayz)*0.25/$q4]
    	set q2 [expr ($Axz-$Azx)*0.25/$q4]
    	set q3 [expr ($Ayx-$Axy)*0.25/$q4]
        } else {
    	set q1 $denominator2
    	set q2 [expr ($Axy+$Ayx)*0.25/$q1]
    	set q3 [expr ($Axz+$Azx)*0.25/$q1]
    	set q4 [expr ($Azy-$Ayz)*0.25/$q1]
        }
        return "$q1 $q2 $q3 $q4"
    }
    proc quaternionToMatrix {quat} {
        ## http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Rotation_matrix_.E2.86.94_quaternion
        lassign $quat q1 q2 q3 q4
        return [list 
    	    [list [expr 1-2*($q2*$q2 + $q3*$q3)] [expr 2*($q1*$q2 - $q3*$q4)] [expr 2*($q1*$q3 + $q2*$q4)]]
    	    [list [expr 2*($q1*$q2 + $q3*$q4)] [expr 1-2*($q1*$q1 + $q3*$q3)] [expr 2*($q2*$q3 - $q1*$q4)]]
    	    [list [expr 2*($q1*$q3 - $q2*$q4)] [expr 2*($q1*$q4 + $q2*$q3)] [expr 1-2*($q2*$q2 + $q1*$q1)]]
    	   ]
    }
    proc quaternionAndTransToMatrix {q r} {
        ## http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Rotation_matrix_.E2.86.94_quaternion
        lassign $r r1 r2 r3
        lassign $q q1 q2 q3 q4
        set m "{[expr 1-2*($q2*$q2 + $q3*$q3)] [expr 2*($q1*$q2 - $q3*$q4)] [expr 2*($q1*$q3 + $q2*$q4)] $r1}
    	   {[expr 2*($q1*$q2 + $q3*$q4)] [expr 1-2*($q1*$q1 + $q3*$q3)] [expr 2*($q2*$q3 - $q1*$q4)] $r2}
    	   {[expr 2*($q1*$q3 - $q2*$q4)] [expr 2*($q1*$q4 + $q2*$q3)] [expr 1-2*($q2*$q2 + $q1*$q1)] $r3}
    	   {0 0 0 1}"
        return $m
    }
    
    proc smoothRot {frames} {
        variable rigidBodyIDs
        variable trans_orig
        variable trans
        variable trans_inv
        variable molToKey
        variable lastFrame
        variable centerRbID
    
        if { [catch { 
    	foreach rbID $rigidBodyIDs {
    	    set key $molToKey($rbID) 
    	    set sel [atomselect $rbID all]
    	    if { ! [info exists centerRbID] || $rbID != $centerRbID } {
    		$sel move [lindex $trans_inv($key) $lastFrame]
    	    }
    	}
    
    	## undo transform for inRbFrame
    	if { [info exists centerRbID] } {
    	    set rbID $centerRbID
    	    set key $molToKey($rbID)	
    	    foreach tID [molinfo list] {
    		if { [molinfo $tID get active] && [molinfo $tID get numframes] > 1 } {
    		    set sel [atomselect $tID all]		
    		    frameLoop frame molid $tID {
    			$sel frame $frame
    			# $sel move [lindex $trans($key) $frame]
    		    }
    		}
    	    }
    	}
        }]} {
    	puts "WARNING: failed to put rigidBody at start"
        }
        
        if { [catch { 
    	foreach rbID $rigidBodyIDs {
    	    set key $molToKey($rbID) 
    	    set trans($key) [smooth4by4RotationMatrices $frames $trans_orig($key)]
    	}
        }]} {
    	puts "WARNING: failed to smooth rigidBody positions"
        }
        calcTransInv
    
    
        if { [catch { 
    	foreach rbID $rigidBodyIDs {
    	    set key $molToKey($rbID) 
    	    set sel [atomselect $rbID all]
    	    if { ! [info exists centerRbID] || $rbID != $centerRbID } {
    		$sel move [lindex $trans($key) $lastFrame]
    	    }
    	}
    
    	## do transform for inRbFrame
    	if { [info exists centerRbID] } {
    	    set rbID $centerRbID
    	    set key $molToKey($rbID)	
    	    foreach tID [molinfo list] {
    		if { [molinfo $tID get active] && [molinfo $tID get numframes] > 1 } {
    		    set sel [atomselect $tID all]		
    		    frameLoop frame molid $tID {
    			$sel frame $frame
    			# $sel move [lindex $trans_inv($key) $frame]
    		    }
    		}
    	    }
    	}
        }]} {
    	puts "WARNING: failed to return rigidBody positions to frame $lastFrame"
        }
    }    
    proc centerAll {ref} {
        variable rigidBodyIDs
        variable trans_orig
        variable trans
        variable trans_inv
        variable molToKey
        variable lastFrame
    
        set ID [$ref molid]
        frameLoop f molid $ID {
    	$ref frame $f
    	set v [vecinvert [measure center $ref]]
    	puts "centerAll: frame $f: [vecinvert [measure center $ref]]"
        }
        if { [catch { 
    	foreach rbID $rigidBodyIDs {
    	    set key $molToKey($rbID) 
    	    set sel [atomselect $rbID all]
    	    $sel move [lindex $trans_inv($key) $lastFrame]
    	}
        }]} {
    	puts "WARNING: failed to put rigidBody at start"
        }
    
        frameLoop f molid $ID {
    	$ref frame $f
    	set v [vecinvert [measure center $ref]]
    	puts "centerAll: frame $f: [vecinvert [measure center $ref]]"
    
    	array set newTrans ""
    	foreach rbID $rigidBodyIDs {
    	    set key $molToKey($rbID) 
    	    # set m [transmult [transoffset [vecinvert [measure center $sel]]] [lindex $trans($key) $f]]
    	    # puts "centerAll: frame $f: [vecinvert [measure center $sel]]"
    	    set m [transmult [transoffset $v] [lindex $trans($key) $f]]
    	    lappend newTrans($key) $m
    	}
        }
        foreach rbID $rigidBodyIDs {
    	set key $molToKey($rbID) 
    	set trans($key) $newTrans($key)
        }
    
        # if { [catch { 
        # 	foreach rbID $rigidBodyIDs {
        # 	    set key $molToKey($rbID) 
        # 	    set newTrans ""
        # 	    frameLoop f molid $ID {
        # 		$sel frame $f
        # 		# set m [transmult [transoffset [vecinvert [measure center $sel]]] [lindex $trans($key) $f]]
        # 		puts "centerAll: $ID frame $f: [vecinvert [measure center $sel]]"
        # 		set m [transmult [transoffset [vecinvert [measure center $sel]]] [lindex $trans($key) $f]]
        # 		lappend newTrans $m
        # 	    }
        # 	    set trans($key) $newTrans
        # 	}
        # }]} {
        # 	puts "WARNING: failed to smooth rigidBody positions"
        # }
        calcTransInv
        if { [catch { 
    	foreach rbID $rigidBodyIDs {
    	    set key $molToKey($rbID) 
    	    set sel [atomselect $rbID all]
    	    $sel move [lindex $trans($key) $lastFrame]
    	}
        }]} {
    	puts "WARNING: failed to return rigidBody positions to frame $lastFrame"
        }
        set all [atomselect $ID all]
        frameLoop f molid $ID {
    	$ref frame $f
    	$all frame $f
    	$all moveby [vecinvert [measure center $ref]]
        }
    }
    
    proc smooth4by4RotationMatrices {frames rots} {
        set newRots ""
        set qs ""
        set rs "" 
    
        foreach m $rots {
    	lappend rs "[lindex $m 0 3] [lindex $m 1 3] [lindex $m 2 3]"
    	lappend qs [matrixToQuaternion $m] 
        }
    
        set numFrames [llength $rots]
    
        set rStack [lrange $rs 0 [expr $frames-1]]
        set qStack [lrange $qs 0 [expr $frames-1]]
    
        set newR  [eval "vecadd $rStack"]
        set newQ  [eval "vecadd $qStack"]
    
        set newqStack ""
        foreach q $qStack {
    	if {[vecdot $q $newQ] < 0} {
    	    lappend newqStack [vecinvert $q]
    	} else {
    	    lappend newqStack $q
    	}
        }
        set qStack $newqStack
        set newQ  [eval "vecadd $qStack"]
    
        for {set f 0} {$f < $numFrames} {incr f} {
    	if {($f % 1000) == 0 || $f < 10 } { puts $f }
    	## qstack (3 elements)
    	## 1 2 3 4 ... 7 8 9
    	
    	## 1:   1 2 
    	## 2: 1 2 3
    	## 3: 2 3 4
    	## 8: 7 8 9
    	## 9: 8 9
    	
    	if { $f > $frames } { ## drop excess frames from stack
    	    # if {($f % 1000) == 0 || $f < 10 } { 
    	    # puts "newR: $newR"
    	    # puts "newQ: $newQ"
    	    # puts "rStack: $rStack"
    	    # puts "qStack: $qStack"
    	    # }
    
    	    set newR [vecsub $newR [lindex $rStack 0]]
    	    set rStack [lrange $rStack 1 end]
    
    	    set newQ [vecsub $newQ [lindex $qStack 0]]
    	    set qStack [lrange $qStack 1 end]
    	}
    	if { $f < $numFrames-$frames } { ## add to stack
    	    set r [lindex $rs [expr $f+$frames]]
    	    if { [catch {set newR [vecadd "$newR" "$r"]}] } {
    		puts "failed adding r '$r'"
    	    }
    	    lappend rStack $r
    
    	    set q [lindex $qs [expr $f+$frames]]
    	    if {[vecdot $q $newQ] < 0} {
    		set q [vecinvert $q]
    	    }
    	    lappend qStack $q
    	    if { [catch {set newQ [vecadd "$newQ" "$q"]}] } {
    		puts "failed adding r '$r'"
    	    }
    	    # set newQ [vecadd $newQ $q]
    	}
    
    	if { [catch {
    	    lappend newRots [quaternionAndTransToMatrix [vecnorm $newQ] [vecscale [expr 1.0/[llength $rStack]] $newR]]
    	} ] } {
    	    puts "fail adding $newQ $newR"
    	}
        }
        return $newRots
    }
    
    proc calcTransInv {} {
        variable trans 
        variable trans_inv
        array set trans_inv {}
        foreach key [array names trans] {
    	set trans_inv($key) ""
    	foreach m $trans($key) {
    	    if { [catch {lappend trans_inv($key) [measure inverse $m]}] } {
    		error "could not find inverse matrix of '$m'"
    	    }
    	}
        }
    }