spheres.tcl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # test performance of display of heavy scene involving multiple interactive
  2. # objects, on example of 1000 spheres
  3. #Category: Visualization
  4. #Title: Display of complex scene and animation
  5. pload MODELING
  6. pload VISUALIZATION
  7. vinit View1 w=1024 h=1024
  8. vclear
  9. vdefaults -autoTriang 0
  10. vrenderparams -stats basic
  11. # parameter NB defines number of spheres by each coordinate
  12. set NB 10
  13. puts "Creating [expr $NB * $NB * $NB] spheres..."
  14. set slist {}
  15. for {set i 0} {$i < $NB} {incr i} {
  16. for {set j 0} {$j < $NB} {incr j} {
  17. for {set k 0} {$k < $NB} {incr k} {
  18. psphere s$i$j$k 1.
  19. lappend slist s$i$j$k
  20. ttranslate s$i$j$k 3.*$i 3.*$j 3.*$k
  21. }
  22. }
  23. }
  24. eval compound $slist c
  25. incmesh c 0.006
  26. puts "Measuring FPS of display of spheres as separate objects..."
  27. vaxo
  28. eval vdisplay -dispMode 1 $slist
  29. vfit
  30. # measure FPS
  31. puts [set fps_separate [vfps]]
  32. vclear
  33. puts "Measuring FPS of display of spheres as single object..."
  34. vdisplay -dispMode 1 c
  35. # measure FPS
  36. puts [set fps_compound [vfps]]
  37. vclear
  38. # redisplay individual spheres, trying to avoid unnecessary internal updates
  39. eval vdisplay -dispMode 1 $slist
  40. # auxiliary procedure to make random update of variable
  41. proc upd {theValueName theDeltaName theTime theToRand} {
  42. upvar $theValueName aValue
  43. upvar $theDeltaName aDelta
  44. # set colors to corner spheres
  45. if { $theToRand == 1 } {
  46. set aValue [expr $aValue + $aDelta * $theTime / 100.0]
  47. set aDelta [expr 0.5 * (rand() - 0.5)]
  48. return $aValue
  49. }
  50. set aRes [expr $aValue + $aDelta * $theTime / 100.0]
  51. }
  52. # move corner spheres in cycle
  53. proc animateSpheres {{theDuration 10.0}} {
  54. set nb [expr $::NB - 1]
  55. # set colors to corner spheres
  56. for {set i 0} {$i < $::NB} {incr i $nb} {
  57. for {set j 0} {$j < $::NB} {incr j $nb} {
  58. for {set k 0} {$k < $::NB} {incr k $nb} {
  59. # mark animated spheres mutable for faster updates
  60. uplevel #0 vdisplay -dispMode 1 -mutable s$i$j$k
  61. # vaspects -noupdate s$i$j$k -setcolor red -setmaterial plastic
  62. uplevel #0 vaspects -noupdate s$i$j$k -setcolor red
  63. set x$i$j$k 0.0
  64. set y$i$j$k 0.0
  65. set z$i$j$k 0.0
  66. set dx$i$j$k 0.0
  67. set dy$i$j$k 0.0
  68. set dz$i$j$k 0.0
  69. }
  70. }
  71. }
  72. set aDuration 0.0
  73. set aPrevRand 0.0
  74. set aTimeFrom [clock clicks -milliseconds]
  75. uplevel #0 chrono anAnimTimer reset
  76. uplevel #0 chrono anAnimTimer start
  77. set toRand 1
  78. for {set aFrameIter 1} { $aFrameIter > 0 } {incr aFrameIter} {
  79. set aCurrTime [expr [clock clicks -milliseconds] - $aTimeFrom]
  80. if { $aCurrTime >= [expr $theDuration * 1000.0] } {
  81. puts "Nb Frames: $aFrameIter"
  82. puts "Duration: [expr $aCurrTime * 0.001] s"
  83. set fps [expr ($aFrameIter - 1) / ($aDuration * 0.001) ]
  84. puts "FPS: $fps"
  85. uplevel #0 chrono anAnimTimer stop
  86. uplevel #0 chrono anAnimTimer show
  87. return $fps
  88. }
  89. set aRandTime [expr $aCurrTime - $aPrevRand]
  90. if { $aRandTime > 1000 } {
  91. set toRand 1
  92. set aPrevRand $aCurrTime
  93. }
  94. #puts "PTS: $aCurrTime ms"
  95. for {set i 0} {$i < $::NB} {incr i $nb} {
  96. for {set j 0} {$j < $::NB} {incr j $nb} {
  97. for {set k 0} {$k < $::NB} {incr k $nb} {
  98. uplevel #0 vsetlocation -noupdate s$i$j$k [upd x$i$j$k dx$i$j$k $aRandTime $toRand] [upd y$i$j$k dy$i$j$k $aRandTime $toRand] [upd z$i$j$k dz$i$j$k $aRandTime $toRand]
  99. }
  100. }
  101. }
  102. uplevel #0 vrepaint
  103. set aDuration [expr [clock clicks -milliseconds] - $aTimeFrom]
  104. set toRand 0
  105. # sleep 1 ms allowing the user to interact with the viewer
  106. after 1 set waiter 1
  107. vwait waiter
  108. }
  109. }
  110. puts "Animating movements of corner spheres (10 sec)..."
  111. puts "(you can interact with the view during the process)"
  112. set fps_animation [animateSpheres 10.0]
  113. puts ""
  114. puts "Performance counters (FPS = \"Frames per second\"):"
  115. puts ""
  116. puts "Spheres as separate interactive objects:"
  117. puts " Actual FPS: [lindex $fps_separate 1]"
  118. puts " FPS estimate by CPU load: [expr 1000. / [lindex $fps_separate 3]]"
  119. puts ""
  120. puts "Spheres as one interactive object (compound):"
  121. puts " Actual FPS: [lindex $fps_compound 1]"
  122. puts " FPS estimate by CPU load: [expr 1000. / [lindex $fps_compound 3]]"
  123. puts ""
  124. puts "Animation FPS: $fps_animation"
  125. puts ""
  126. puts "Scene contains [lindex [trinfo c] 3] triangles"
  127. puts ""
  128. puts "Print 'animateSpheres 10.0' to restart animation"