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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! This module contains the `BBox` struct for representing bounding boxes and functions for calculating overlap and union.
//!
//! # Examples
//!
//! ```
//! use object_detector_rust::bbox::BBox;
//!
//! let bbox1 = BBox {
//!     x: 10,
//!     y: 20,
//!     width: 30,
//!     height: 40,
//! };
//!
//! let bbox2 = BBox {
//!     x: 15,
//!     y: 25,
//!     width: 35,
//!     height: 45,
//! };
//!
//! let overlap = bbox1.overlap(&bbox2);
//! let union = bbox1.union_area(&bbox2);
//!
//! assert!(overlap > 0.0 && overlap < 1.0);
//! assert!(union > 1);
//! ```
//!
//! This example shows how to use the `BBox` struct to represent bounding boxes and how to calculate the overlap and union between two bounding boxes.

/// Struct for representing a bounding box
#[derive(Debug, Clone, PartialEq, Copy)]
pub struct BBox {
    /// X coordinate of the top-left corner of the bounding box
    pub x: i32,
    /// Y coordinate of the top-left corner of the bounding box
    pub y: i32,
    /// Width of the bounding box
    pub width: u32,
    /// Height of the bounding box
    pub height: u32,
}

impl Default for BBox {
    fn default() -> Self {
        BBox {
            x: 0,
            y: 0,
            width: 1,
            height: 1,
        }
    }
}

impl BBox {
    /// Creates a new bounding box with the specified coordinates and dimensions
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> BBox {
        BBox {
            x,
            y,
            width,
            height,
        }
    }

    /// Returns the left coordinate of the bounding box
    pub fn left(&self) -> i32 {
        self.x
    }

    /// Returns the right coordinate of the bounding box
    pub fn right(&self) -> i32 {
        self.x + self.width as i32
    }

    /// Returns the top coordinate of the bounding box
    pub fn top(&self) -> i32 {
        self.y
    }

    /// Returns the bottom coordinate of the bounding box
    pub fn bottom(&self) -> i32 {
        self.y + self.height as i32
    }

    /// Calculates the width of the intersection between two bounding boxes
    ///
    /// # Examples
    ///
    /// ```
    /// use object_detector_rust::bbox::BBox;
    ///
    /// let bbox1 = BBox {
    ///     x: 0,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox2 = BBox {
    ///     x: 50,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox3 = BBox {
    ///     x: 150,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox4 = BBox {
    ///     x: -50,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    ///
    /// assert_eq!(bbox1.intersect_width(&bbox2), 50);
    /// assert_eq!(bbox1.intersect_width(&bbox3), 0);
    /// assert_eq!(bbox1.intersect_width(&bbox4), 50);
    /// assert_eq!(bbox2.intersect_width(&bbox3), 0);
    /// assert_eq!(bbox2.intersect_width(&bbox4), 0);
    /// assert_eq!(bbox3.intersect_width(&bbox4), 0);
    /// ```
    pub fn intersect_width(&self, other: &BBox) -> u32 {
        (self.right().min(other.right()) - self.left().max(other.left())).max(0) as u32
    }

    /// Calculates the height of the intersection between two bounding boxes
    ///
    /// # Examples
    ///
    /// ```
    /// use object_detector_rust::bbox::BBox;
    ///
    /// let bbox1 = BBox {
    ///     x: 0,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox2 = BBox {
    ///     x: 0,
    ///     y: 50,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox3 = BBox {
    ///     x: 0,
    ///     y: 150,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox4 = BBox {
    ///     x: 0,
    ///     y: -50,
    ///     width: 100,
    ///     height: 100,
    /// };
    ///
    /// assert_eq!(bbox1.intersect_height(&bbox2), 50);
    /// assert_eq!(bbox1.intersect_height(&bbox3), 0);
    /// assert_eq!(bbox1.intersect_height(&bbox4), 50);
    /// assert_eq!(bbox2.intersect_height(&bbox3), 0);
    /// assert_eq!(bbox2.intersect_height(&bbox4), 0);
    /// assert_eq!(bbox3.intersect_height(&bbox4), 0);
    /// ```
    pub fn intersect_height(&self, other: &BBox) -> u32 {
        (self.bottom().min(other.bottom()) - self.top().max(other.top())).max(0) as u32
    }

    /// Calculates the area of the intersection between two bounding boxes
    ///
    /// # Examples
    ///
    /// ```
    /// use object_detector_rust::bbox::BBox;
    ///
    /// let bbox1 = BBox {
    ///     x: 0,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox2 = BBox {
    ///     x: 50,
    ///     y: 50,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox3 = BBox {
    ///     x: 150,
    ///     y: 150,
    ///     width: 100,
    ///     height: 100,
    /// };
    ///
    /// assert_eq!(bbox1.intersect_area(&bbox2), 2500);
    /// assert_eq!(bbox1.intersect_area(&bbox3), 0);
    /// assert_eq!(bbox2.intersect_area(&bbox3), 0);
    /// ```
    pub fn intersect_area(&self, other: &BBox) -> u32 {
        self.intersect_width(other) * self.intersect_height(other)
    }

    /// Calculates the area of the bounding box
    ///
    /// # Examples
    ///
    /// ```
    /// use object_detector_rust::bbox::BBox;
    ///
    /// let bbox = BBox {
    ///     x: 0,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    ///
    /// assert_eq!(bbox.self_area(), 10000);
    /// ```
    pub fn self_area(&self) -> u32 {
        self.width * self.height
    }

    /// Calculates the area of the union between two bounding boxes
    ///
    /// # Examples
    ///
    /// ```
    /// use object_detector_rust::bbox::BBox;
    ///
    /// let bbox1 = BBox {
    ///     x: 0,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox2 = BBox {
    ///     x: 50,
    ///     y: 50,
    ///     width: 100,
    ///     height: 100,
    /// };
    ///
    /// assert_eq!(bbox1.union_area(&bbox2), 17500);
    /// ```
    pub fn union_area(&self, other: &BBox) -> u32 {
        self.self_area() + other.self_area() - self.intersect_area(other)
    }

    /// Calculates the overlap between two bounding boxes as the ratio of their intersecting area to their union area
    ///
    /// # Examples
    ///
    /// ```
    /// use object_detector_rust::bbox::BBox;
    ///
    /// let bbox1 = BBox {
    ///     x: 0,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox2 = BBox {
    ///     x: 50,
    ///     y: 50,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox3 = BBox {
    ///     x: 150,
    ///     y: 150,
    ///     width: 100,
    ///     height: 100,
    /// };
    ///
    /// assert!(bbox1.overlap(&bbox2) > 0.14);
    /// assert_eq!(bbox1.overlap(&bbox3), 0.0);
    /// assert_eq!(bbox2.overlap(&bbox3), 0.0);
    /// ```
    pub fn overlap(&self, other: &BBox) -> f32 {
        self.intersect_area(other) as f32 / self.union_area(other) as f32
    }

    /// Merges two bounding boxes into a single bounding box that encloses both
    ///
    /// # Examples
    ///
    /// ```
    /// use object_detector_rust::bbox::BBox;
    ///
    /// let bbox1 = BBox {
    ///     x: 0,
    ///     y: 0,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox2 = BBox {
    ///     x: 50,
    ///     y: 50,
    ///     width: 100,
    ///     height: 100,
    /// };
    /// let bbox3 = BBox {
    ///     x: -50,
    ///     y: -50,
    ///     width: 100,
    ///     height: 100,
    /// };
    ///
    /// assert_eq!(bbox1.merge(&bbox2), BBox {
    ///     x: 0,
    ///     y: 0,
    ///     width: 150,
    ///     height: 150,
    /// });
    /// assert_eq!(bbox1.merge(&bbox3), BBox {
    ///     x: -50,
    ///     y: -50,
    ///     width: 150,
    ///     height: 150,
    /// });
    /// ```
    pub fn merge(&self, other: &BBox) -> BBox {
        let left = self.left().min(other.left());
        let right = self.right().max(other.right());
        let top = self.top().min(other.top());
        let bottom = self.bottom().max(other.bottom());

        BBox {
            x: left,
            y: top,
            width: (right - left) as u32,
            height: (bottom - top) as u32,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_copy_trait() {
        let bbox = BBox::default();
        let bbox_copy = bbox;
        assert_eq!(bbox, bbox_copy);
    }

    #[test]
    fn test_overlap() {
        let bbox1 = BBox::new(0, 0, 100, 100);
        let bbox2 = BBox {
            x: 50,
            y: 50,
            width: 100,
            height: 100,
        };
        let bbox3 = BBox {
            x: 150,
            y: 150,
            width: 100,
            height: 100,
        };
        let bbox4 = BBox {
            x: -50,
            y: -50,
            width: 100,
            height: 100,
        };

        assert!(bbox1.overlap(&bbox2) > 0.14);
        assert_eq!(bbox1.overlap(&bbox3), 0.0);
        assert!(bbox1.overlap(&bbox4) > 0.14);
        assert_eq!(bbox2.overlap(&bbox3), 0.0);
        assert_eq!(bbox2.overlap(&bbox4), 0.0);
        assert_eq!(bbox3.overlap(&bbox4), 0.0);
    }

    #[test]
    fn test_bbox_merge() {
        let bbox1 = BBox::new(0, 0, 100, 100);
        let bbox2 = BBox::new(50, 50, 100, 100);
        let bbox3 = BBox::new(200, 200, 100, 100);

        let merged_bbox1 = BBox::merge(&bbox1, &bbox2);
        assert_eq!(merged_bbox1.x, 0);
        assert_eq!(merged_bbox1.y, 0);
        assert_eq!(merged_bbox1.width, 150);
        assert_eq!(merged_bbox1.height, 150);

        let merged_bbox2 = BBox::merge(&bbox1, &bbox3);
        assert_eq!(merged_bbox2.x, 0);
        assert_eq!(merged_bbox2.y, 0);
        assert_eq!(merged_bbox2.width, 300);
        assert_eq!(merged_bbox2.height, 300);
    }
}