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
use crate::{CityFurniture, TrafficArea, WallSurface};

use crate::model::solitary_vegetation_object::SolitaryVegetationObject;
use egml::geometry::{enlarge_envelopes, Envelope};

#[derive(Debug, Clone, PartialEq, Default)]
pub struct CitygmlModel {
    city_furniture: Vec<CityFurniture>,
    traffic_area: Vec<TrafficArea>,
    solitary_vegetation_object: Vec<SolitaryVegetationObject>,
    wall_surface: Vec<WallSurface>,
}

impl CitygmlModel {
    pub fn new(
        city_furniture: Vec<CityFurniture>,
        traffic_area: Vec<TrafficArea>,
        solitary_vegetation_object: Vec<SolitaryVegetationObject>,
        wall_surface: Vec<WallSurface>,
    ) -> Self {
        Self {
            city_furniture,
            traffic_area,
            solitary_vegetation_object,
            wall_surface,
        }
    }

    pub fn from_citygml_models(citygml_models: &Vec<Self>) -> Self {
        let city_furniture: Vec<CityFurniture> = citygml_models
            .iter()
            .flat_map(|x| x.city_furniture.iter().cloned())
            .collect();
        let traffic_area: Vec<TrafficArea> = citygml_models
            .iter()
            .flat_map(|x| x.traffic_area.iter().cloned())
            .collect();
        let solitary_vegetation_object: Vec<SolitaryVegetationObject> = citygml_models
            .iter()
            .flat_map(|x| x.solitary_vegetation_object.iter().cloned())
            .collect();
        let wall_surface: Vec<WallSurface> = citygml_models
            .iter()
            .flat_map(|x| x.wall_surface.iter().cloned())
            .collect();

        CitygmlModel::new(
            city_furniture,
            traffic_area,
            solitary_vegetation_object,
            wall_surface,
        )
    }

    pub fn city_furniture(&self) -> &Vec<CityFurniture> {
        &self.city_furniture
    }
    pub fn set_city_furniture(&mut self, val: Vec<CityFurniture>) {
        self.city_furniture = val;
    }

    pub fn traffic_area(&self) -> &Vec<TrafficArea> {
        &self.traffic_area
    }
    pub fn set_traffic_area(&mut self, val: Vec<TrafficArea>) {
        self.traffic_area = val;
    }

    pub fn solitary_vegetation_object(&self) -> &Vec<SolitaryVegetationObject> {
        &self.solitary_vegetation_object
    }
    pub fn set_solitary_vegetation_object(&mut self, val: Vec<SolitaryVegetationObject>) {
        self.solitary_vegetation_object = val;
    }

    pub fn wall_surface(&self) -> &Vec<WallSurface> {
        &self.wall_surface
    }
    pub fn set_wall_surface(&mut self, val: Vec<WallSurface>) {
        self.wall_surface = val;
    }

    pub fn is_empty(&self) -> bool {
        self.city_furniture.is_empty()
            && self.traffic_area.is_empty()
            && self.wall_surface.is_empty()
    }

    pub fn push_city_furniture(&mut self, city_furniture: CityFurniture) {
        self.city_furniture.push(city_furniture);
    }

    pub fn push_traffic_area(&mut self, traffic_area: TrafficArea) {
        self.traffic_area.push(traffic_area);
    }

    pub fn push_solitary_vegetation_object(
        &mut self,
        solitary_vegetation_object: SolitaryVegetationObject,
    ) {
        self.solitary_vegetation_object
            .push(solitary_vegetation_object);
    }

    pub fn push_wall_surface(&mut self, wall_surface: WallSurface) {
        self.wall_surface.push(wall_surface);
    }

    pub fn number_of_objects(&self) -> usize {
        self.city_furniture.len() + self.traffic_area.len() + self.wall_surface.len()
    }

    pub fn get_envelope(&self) -> Option<Envelope> {
        let mut envelopes: Vec<Envelope> = Vec::new();
        let mut feature_envelopes: Vec<Envelope> = self
            .city_furniture
            .iter()
            .flat_map(|f| f.lod2_multi_surface())
            .map(|f| f.get_envelope().unwrap())
            .collect();
        envelopes.append(&mut feature_envelopes);

        let mut feature_envelopes: Vec<Envelope> = self
            .city_furniture
            .iter()
            .flat_map(|f| f.lod2_solid())
            .map(|f| f.get_envelope().unwrap())
            .collect();
        envelopes.append(&mut feature_envelopes);

        let mut feature_envelopes: Vec<Envelope> = self
            .wall_surface
            .iter()
            .flat_map(|f| f.lod2_multi_surface())
            .map(|f| f.get_envelope().unwrap())
            .collect();
        envelopes.append(&mut feature_envelopes);

        let mut feature_envelopes: Vec<Envelope> = self
            .traffic_area
            .iter()
            .flat_map(|f| f.lod2_multi_surface())
            .map(|f| f.get_envelope().unwrap())
            .collect();
        envelopes.append(&mut feature_envelopes);

        if envelopes.is_empty() {
            return None;
        }
        let city_model_envelopes = enlarge_envelopes(&envelopes).unwrap();
        Some(city_model_envelopes)
    }

    /*pub(crate) fn all_vertices(&self) -> Vec<&Point3<f64>> {
        /*let all_vertices: Vec<&Point3<f64>> = self
        .city_objects
        .iter()
        .flat_map(|m| &m.geometries)
        .flat_map(|p| p.points())
        .collect();*/

        let all_vertices: Vec<&Point3<f64>> = Vec::new();
        all_vertices
    }

    pub fn get_min(&self) -> Point3<f64> {
        let vertices: Vec<&Point3<f64>> = self.all_vertices();

        let min_x = vertices
            .iter()
            .map(|p| p.x)
            .collect::<Vec<f64>>()
            .iter()
            .fold(f64::INFINITY, |a, &b| a.min(b));
        let min_y = vertices
            .iter()
            .map(|p| p.y)
            .collect::<Vec<f64>>()
            .iter()
            .fold(f64::INFINITY, |a, &b| a.min(b));
        let min_z = vertices
            .iter()
            .map(|p| p.z)
            .collect::<Vec<f64>>()
            .iter()
            .fold(f64::INFINITY, |a, &b| a.min(b));

        Point3::new(min_x, min_y, min_z)
    }*/
}