-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver.rs
136 lines (116 loc) · 3.79 KB
/
observer.rs
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
use agent::Crowd;
use std::io::fs::File;
use std::io::{Write, Truncate};
mod agent;
// Type Observer
pub trait Observer {
fn see(&self, time: f64, crowd: &Crowd);
fn request_at(&self, time: f64, dt: f64) -> bool;
// fn request_at(&self, time: f64, dt: f64) -> bool {
// let inter_size = self.tmax / (self.freq as f64);
// let int_num=(time/inter_size) as uint;
// (time-inter_size*(int_num as f64)).abs() < dt
// }
}
//
// * Barre de progression
//
pub struct ProgressMeter {
tmax: f64,
freq: uint
}
impl ProgressMeter {
pub fn new(tmax: f64, freq: uint) -> ProgressMeter {
ProgressMeter {tmax: tmax, freq: freq}
}
}
impl Observer for ProgressMeter {
fn see(&self, time: f64, crowd: &Crowd) {
println!("{:f} : {:u} cellules", time, crowd.size());
}
fn request_at(&self, time: f64, dt: f64) -> bool {
let inter_size = self.tmax / (self.freq as f64);
let int_num=(time/inter_size) as uint;
(time-inter_size*(int_num as f64)).abs() < dt
}
}
//
// * Affichage à l'écran
//
pub struct ScreenPrinter {
tmax: f64,
freq: uint
}
impl ScreenPrinter {
pub fn new(tmax: f64, freq: uint) -> ScreenPrinter {
ScreenPrinter { tmax: tmax, freq: freq}
}
}
impl Observer for ScreenPrinter {
fn see(&self, time: f64, crowd: &Crowd) {
println!("{:f}\n", time);
println!("{:s}",crowd.to_str());
}
fn request_at(&self, time: f64, dt: f64) -> bool {
let inter_size = self.tmax / (self.freq as f64);
let int_num=(time/inter_size) as uint;
(time-inter_size*(int_num as f64)).abs() < dt
}
}
//
// * Sortie sur le disque
//
pub struct DiskWriter {
tmax: f64,
freq: uint,
fname: ~str,
}
impl DiskWriter {
pub fn new(tmax: f64, freq:uint, name: ~str) -> DiskWriter {
DiskWriter {tmax: tmax, freq: freq, fname: name}
}
pub fn new_filename(&self, time: f64) -> ~str {
let inter_size = self.tmax / (self.freq as f64);
let int_num=(time/inter_size) as uint;
format!("./{:s}-{:03u}.vtk", self.fname, int_num)
}
}
impl Observer for DiskWriter {
fn see(&self, time: f64, crowd: &Crowd) {
let outfile=self.new_filename(time);
let file_path=Path::new(outfile);
match File::open_mode(&file_path, Truncate , Write) {
None => fail!(),
Some(ref mut f) => {
let writer= f as &mut Writer;
// Ecriture de l'entête
write!(writer, "{:c} vtk DataFile Version 3.0\n", '#');
write!(writer, "Rusty_Cells\n");
write!(writer, "ASCII\n");
write!(writer, "DATASET UNSTRUCTURED_GRID\n");
write!(writer, "POINTS {:u} float\n", crowd.size());
// Coordonnées des cellules
for cell in crowd.cells.iter() {
write!(writer, "{:f} {:f} {:f}\n", cell.x(), cell.y(), cell.z() );
}
// Données sur les nœuds
// * Age
write!(writer,"POINT_DATA {:u}\nSCALARS {:s} float\nLOOKUP_TABLE default\n", crowd.size(), "age");
for cell in crowd.cells.iter() {
write!(writer,"{:f}\n", cell.age);
}
// * Vitesse
write!(writer,"VECTORS {:s} float\n", "vitesse");
for cell in crowd.cells.iter() {
let speed=cell.velocity*1e3f64;
write!(writer,"{:f} {:f} {:f}\n", speed.x, speed.y, speed.z)
}
}
}
}
fn request_at(&self, time: f64, dt: f64) -> bool {
let inter_size = self.tmax / (self.freq as f64);
let int_num=(time/inter_size) as uint;
(time-inter_size*(int_num as f64)).abs() < dt
}
}