nphysics2d/counters/
mod.rs1use std::fmt::{Display, Formatter, Result};
4
5pub use self::ccd_counters::CCDCounters;
6pub use self::collision_detection_counters::CollisionDetectionCounters;
7pub use self::solver_counters::SolverCounters;
8pub use self::stages_counters::StagesCounters;
9pub use self::timer::Timer;
10
11mod ccd_counters;
12mod collision_detection_counters;
13mod solver_counters;
14mod stages_counters;
15mod timer;
16
17#[derive(Clone, Copy)]
19pub struct Counters {
20 pub enabled: bool,
22 pub step_time: Timer,
24 pub custom: Timer,
26 pub stages: StagesCounters,
28 pub cd: CollisionDetectionCounters,
30 pub solver: SolverCounters,
32 pub ccd: CCDCounters,
34}
35
36impl Counters {
37 pub fn new(enabled: bool) -> Self {
39 Counters {
40 enabled,
41 step_time: Timer::new(),
42 custom: Timer::new(),
43 stages: StagesCounters::new(),
44 cd: CollisionDetectionCounters::new(),
45 solver: SolverCounters::new(),
46 ccd: CCDCounters::new(),
47 }
48 }
49
50 pub fn enable(&mut self) {
52 self.enabled = true;
53 }
54
55 pub fn enabled(&self) -> bool {
57 self.enabled
58 }
59
60 pub fn disable(&mut self) {
62 self.enabled = false;
63 }
64
65 pub fn step_started(&mut self) {
67 if self.enabled {
68 self.step_time.start();
69 }
70 }
71
72 pub fn step_completed(&mut self) {
74 if self.enabled {
75 self.step_time.pause();
76 }
77 }
78
79 pub fn step_time(&self) -> f64 {
81 self.step_time.time()
82 }
83
84 pub fn custom_started(&mut self) {
86 if self.enabled {
87 self.custom.start();
88 }
89 }
90
91 pub fn custom_completed(&mut self) {
93 if self.enabled {
94 self.custom.pause();
95 }
96 }
97
98 pub fn custom_time(&self) -> f64 {
100 self.custom.time()
101 }
102
103 pub fn set_nconstraints(&mut self, n: usize) {
105 self.solver.nconstraints = n;
106 }
107
108 pub fn set_ncontacts(&mut self, n: usize) {
110 self.solver.ncontacts = n;
111 }
112
113 pub fn set_ncontact_pairs(&mut self, n: usize) {
115 self.cd.ncontact_pairs = n;
116 }
117}
118
119macro_rules! measure_method {
120 ($started:ident, $stopped:ident, $time:ident, $info:ident. $timer:ident) => {
121 impl Counters {
122 pub fn $started(&mut self) {
124 if self.enabled {
125 self.$info.$timer.start()
126 }
127 }
128
129 pub fn $stopped(&mut self) {
131 if self.enabled {
132 self.$info.$timer.pause()
133 }
134 }
135
136 pub fn $time(&self) -> f64 {
138 if self.enabled {
139 self.$info.$timer.time()
140 } else {
141 0.0
142 }
143 }
144 }
145 };
146}
147
148measure_method!(
149 update_started,
150 update_completed,
151 update_time,
152 stages.update_time
153);
154measure_method!(
155 collision_detection_started,
156 collision_detection_completed,
157 collision_detection_time,
158 stages.collision_detection_time
159);
160measure_method!(
161 island_construction_started,
162 island_construction_completed,
163 island_construction_time,
164 stages.island_construction_time
165);
166measure_method!(
167 solver_started,
168 solver_completed,
169 solver_time,
170 stages.solver_time
171);
172measure_method!(ccd_started, ccd_completed, ccd_time, stages.ccd_time);
173
174measure_method!(
175 assembly_started,
176 assembly_completed,
177 assembly_time,
178 solver.assembly_time
179);
180measure_method!(
181 velocity_resolution_started,
182 velocity_resolution_completed,
183 velocity_resolution_time,
184 solver.velocity_resolution_time
185);
186measure_method!(
187 velocity_update_started,
188 velocity_update_completed,
189 velocity_update_time,
190 solver.velocity_update_time
191);
192measure_method!(
193 position_resolution_started,
194 position_resolution_completed,
195 position_resolution_time,
196 solver.position_resolution_time
197);
198measure_method!(
199 broad_phase_started,
200 broad_phase_completed,
201 broad_phase_time,
202 cd.broad_phase_time
203);
204measure_method!(
205 narrow_phase_started,
206 narrow_phase_completed,
207 narrow_phase_time,
208 cd.narrow_phase_time
209);
210
211impl Display for Counters {
212 fn fmt(&self, f: &mut Formatter) -> Result {
213 writeln!(f, "Total timestep time: {}", self.step_time)?;
214 self.stages.fmt(f)?;
215 self.cd.fmt(f)?;
216 self.solver.fmt(f)?;
217 writeln!(f, "Custom timer: {}", self.custom)
218 }
219}