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