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
399
400
401
402
403
404
405
406
407
408
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use length::Length;
use num::Zero;
use point::Point2D;
use size::Size2D;

use num_lib::NumCast;
use std::cmp::PartialOrd;
use std::fmt::{self, Formatter};
use std::ops::{Add, Sub, Mul, Div};

#[derive(Clone, Copy, RustcDecodable, RustcEncodable, PartialEq)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct Rect<T> {
    pub origin: Point2D<T>,
    pub size: Size2D<T>,
}

impl<T: fmt::Debug> fmt::Debug for Rect<T> {
   fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Rect({:?} at {:?})", self.size, self.origin)
    }
}

impl<T: fmt::Display> fmt::Display for Rect<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "Rect({} at {})", self.size.to_string(), self.origin.to_string())
    }
}

impl<T: Clone> Rect<T> {
    pub fn new(origin: Point2D<T>, size: Size2D<T>) -> Rect<T> {
        Rect {
            origin: origin,
            size: size
        }
    }
}

impl<T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T> + Zero> Rect<T> {
    #[inline]
    pub fn intersects(&self, other: &Rect<T>) -> bool {
        self.origin.x < other.origin.x + other.size.width &&
       other.origin.x <  self.origin.x + self.size.width &&
        self.origin.y < other.origin.y + other.size.height &&
       other.origin.y <  self.origin.y + self.size.height
    }

    #[inline]
    pub fn max_x(&self) -> T {
        self.origin.x + self.size.width
    }

    #[inline]
    pub fn min_x(&self) -> T {
        self.origin.x.clone()
    }

    #[inline]
    pub fn max_y(&self) -> T {
        self.origin.y + self.size.height
    }

    #[inline]
    pub fn min_y(&self) -> T {
        self.origin.y.clone()
    }

    #[inline]
    pub fn intersection(&self, other: &Rect<T>) -> Option<Rect<T>> {
        if !self.intersects(other) {
            return None;
        }

        let upper_left = Point2D::new(max(self.min_x(), other.min_x()),
                                      max(self.min_y(), other.min_y()));
        let lower_right = Point2D::new(min(self.max_x(), other.max_x()),
                                       min(self.max_y(), other.max_y()));

        Some(Rect::new(upper_left.clone(), Size2D::new(lower_right.x - upper_left.x,
                                                       lower_right.y - upper_left.y)))
    }

    #[inline]
    pub fn union(&self, other: &Rect<T>) -> Rect<T> {
        if self.size == Zero::zero() {
            return *other
        }
        if other.size == Zero::zero() {
            return *self
        }

        let upper_left = Point2D::new(min(self.min_x(), other.min_x()),
                                      min(self.min_y(), other.min_y()));

        let lower_right = Point2D::new(max(self.max_x(), other.max_x()),
                                       max(self.max_y(), other.max_y()));

        Rect {
            origin: upper_left.clone(),
            size: Size2D::new(lower_right.x - upper_left.x, lower_right.y - upper_left.y)
        }
    }

    #[inline]
    pub fn translate(&self, other: &Point2D<T>) -> Rect<T> {
        Rect {
            origin: Point2D::new(self.origin.x + other.x, self.origin.y + other.y),
            size: self.size.clone()
        }
    }

    #[inline]
    pub fn contains(&self, other: &Point2D<T>) -> bool {
        self.origin.x <= other.x && other.x < self.origin.x + self.size.width &&
        self.origin.y <= other.y && other.y < self.origin.y + self.size.height
    }

    #[inline]
    pub fn inflate(&self, width: T, height: T) -> Rect<T> {
        Rect {
            origin: Point2D::new(self.origin.x - width, self.origin.y - height),
            size: Size2D::new(self.size.width + width + width, self.size.height + height + height),
        }
    }

    #[inline]
    pub fn top_right(&self) -> Point2D<T> {
        Point2D::new(self.max_x(), self.origin.y.clone())
    }

    #[inline]
    pub fn bottom_left(&self) -> Point2D<T> {
        Point2D::new(self.origin.x.clone(), self.max_y())
    }

    #[inline]
    pub fn bottom_right(&self) -> Point2D<T> {
        Point2D::new(self.max_x(), self.max_y())
    }

    #[inline]
    pub fn translate_by_size(&self, size: &Size2D<T>) -> Rect<T> {
        Rect::new(Point2D::new(self.origin.x + size.width, self.origin.y + size.height),
                  self.size.clone())
    }
}

impl<T> Rect<T> {
    #[inline]
    pub fn scale<Scale: Copy>(&self, x: Scale, y: Scale) -> Rect<T>
        where T: Copy + Clone + Mul<Scale, Output=T> {
        Rect {
            origin: Point2D { x: self.origin.x * x, y: self.origin.y * y},
            size: Size2D { width: self.size.width * x, height: self.size.height * y}
        }
    }
}

impl<T: PartialEq + Zero> Rect<T> {
    pub fn zero() -> Rect<T> {
        Rect {
            origin: Point2D::zero(),
            size: Size2D::zero(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.size.width == Zero::zero() || self.size.height == Zero::zero()
    }
}


pub fn min<T:Clone + PartialOrd>(x: T, y: T) -> T {
    if x <= y { x } else { y }
}

pub fn max<T:Clone + PartialOrd>(x: T, y: T) -> T {
    if x >= y { x } else { y }
}

impl<Scale: Copy, T0: Mul<Scale, Output=T1>, T1: Clone> Mul<Scale> for Rect<T0> {
    type Output = Rect<T1>;
    #[inline]
    fn mul(self, scale: Scale) -> Rect<T1> {
        Rect::new(self.origin * scale, self.size * scale)
    }
}

impl<Scale: Copy, T0: Div<Scale, Output=T1>, T1: Clone> Div<Scale> for Rect<T0> {
    type Output = Rect<T1>;
    #[inline]
    fn div(self, scale: Scale) -> Rect<T1> {
        Rect::new(self.origin / scale, self.size / scale)
    }
}

// Convenient aliases for Rect with typed units
pub type TypedRect<Unit, T> = Rect<Length<Unit, T>>;

impl<Unit, T: Clone> Rect<Length<Unit, T>> {
    /// Drop the units, preserving only the numeric value.
    pub fn to_untyped(&self) -> Rect<T> {
        Rect::new(self.origin.to_untyped(), self.size.to_untyped())
    }

    /// Tag a unitless value with units.
    pub fn from_untyped(r: &Rect<T>) -> TypedRect<Unit, T> {
        Rect::new(Point2D::from_untyped(&r.origin), Size2D::from_untyped(&r.size))
    }
}

impl<Unit, T0: NumCast + Clone> Rect<Length<Unit, T0>> {
    /// Cast from one numeric representation to another, preserving the units.
    pub fn cast<T1: NumCast + Clone>(&self) -> Option<Rect<Length<Unit, T1>>> {
        match (self.origin.cast(), self.size.cast()) {
            (Some(origin), Some(size)) => Some(Rect::new(origin, size)),
            _ => None
        }
    }
}

// Convenience functions for common casts
impl<Unit, T: NumCast + Clone> Rect<Length<Unit, T>> {
    pub fn as_f32(&self) -> Rect<Length<Unit, f32>> {
        self.cast().unwrap()
    }

    pub fn as_uint(&self) -> Rect<Length<Unit, usize>> {
        self.cast().unwrap()
    }
}

#[test]
fn test_min_max() {
    assert!(min(0u32, 1u32) == 0u32);
    assert!(min(-1.0f32, 0.0f32) == -1.0f32);

    assert!(max(0u32, 1u32) == 1u32);
    assert!(max(-1.0f32, 0.0f32) == 0.0f32);
}

#[test]
fn test_translate() {
    let p = Rect::new(Point2D::new(0u32, 0u32), Size2D::new(50u32, 40u32));
    let pp = p.translate(&Point2D::new(10,15));

    assert!(pp.size.width == 50);
    assert!(pp.size.height == 40);
    assert!(pp.origin.x == 10);
    assert!(pp.origin.y == 15);


    let r = Rect::new(Point2D::new(-10, -5), Size2D::new(50, 40));
    let rr = r.translate(&Point2D::new(0,-10));

    assert!(rr.size.width == 50);
    assert!(rr.size.height == 40);
    assert!(rr.origin.x == -10);
    assert!(rr.origin.y == -15);
}

#[test]
fn test_union() {
    let p = Rect::new(Point2D::new(0, 0), Size2D::new(50, 40));
    let q = Rect::new(Point2D::new(20,20), Size2D::new(5, 5));
    let r = Rect::new(Point2D::new(-15, -30), Size2D::new(200, 15));
    let s = Rect::new(Point2D::new(20, -15), Size2D::new(250, 200));

    let pq = p.union(&q);
    assert!(pq.origin == Point2D::new(0, 0));
    assert!(pq.size == Size2D::new(50, 40));

    let pr = p.union(&r);
    assert!(pr.origin == Point2D::new(-15, -30));
    assert!(pr.size == Size2D::new(200, 70));

    let ps = p.union(&s);
    assert!(ps.origin == Point2D::new(0, -15));
    assert!(ps.size == Size2D::new(270, 200));

}

#[test]
fn test_intersection() {
    let p = Rect::new(Point2D::new(0, 0), Size2D::new(10, 20));
    let q = Rect::new(Point2D::new(5, 15), Size2D::new(10, 10));
    let r = Rect::new(Point2D::new(-5, -5), Size2D::new(8, 8));

    let pq = p.intersection(&q);
    assert!(pq.is_some());
    let pq = pq.unwrap();
    assert!(pq.origin == Point2D::new(5, 15));
    assert!(pq.size == Size2D::new(5, 5));

    let pr = p.intersection(&r);
    assert!(pr.is_some());
    let pr = pr.unwrap();
    assert!(pr.origin == Point2D::new(0, 0));
    assert!(pr.size == Size2D::new(3, 3));

    let qr = q.intersection(&r);
    assert!(qr.is_none());
}

#[test]
fn test_contains() {
    let r = Rect::new(Point2D::new(-20, 15), Size2D::new(100, 200));

    assert!(r.contains(&Point2D::new(0, 50)));
    assert!(r.contains(&Point2D::new(-10, 200)));

    // The `contains` method is inclusive of the top/left edges, but not the
    // bottom/right edges.
    assert!(r.contains(&Point2D::new(-20, 15)));
    assert!(!r.contains(&Point2D::new(80, 15)));
    assert!(!r.contains(&Point2D::new(80, 215)));
    assert!(!r.contains(&Point2D::new(-20, 215)));

    // Points beyond the top-left corner.
    assert!(!r.contains(&Point2D::new(-25, 15)));
    assert!(!r.contains(&Point2D::new(-15, 10)));

    // Points beyond the top-right corner.
    assert!(!r.contains(&Point2D::new(85, 20)));
    assert!(!r.contains(&Point2D::new(75, 10)));

    // Points beyond the bottom-right corner.
    assert!(!r.contains(&Point2D::new(85, 210)));
    assert!(!r.contains(&Point2D::new(75, 220)));

    // Points beyond the bottom-left corner.
    assert!(!r.contains(&Point2D::new(-25, 210)));
    assert!(!r.contains(&Point2D::new(-15, 220)));
}

#[test]
fn test_scale() {
    let p = Rect::new(Point2D::new(0u32, 0u32), Size2D::new(50u32, 40u32));
    let pp = p.scale(10, 15);

    assert!(pp.size.width == 500);
    assert!(pp.size.height == 600);
    assert!(pp.origin.x == 0);
    assert!(pp.origin.y == 0);

    let r = Rect::new(Point2D::new(-10, -5), Size2D::new(50, 40));
    let rr = r.scale(1, 20);

    assert!(rr.size.width == 50);
    assert!(rr.size.height == 800);
    assert!(rr.origin.x == -10);
    assert!(rr.origin.y == -100);
}

#[test]
fn test_inflate() {
    let p = Rect::new(Point2D::new(0, 0), Size2D::new(10, 10));
    let pp = p.inflate(10, 20);

    assert!(pp.size.width == 30);
    assert!(pp.size.height == 50);
    assert!(pp.origin.x == -10);
    assert!(pp.origin.y == -20);

    let r = Rect::new(Point2D::new(0, 0), Size2D::new(10, 20));
    let rr = r.inflate(-2, -5);

    assert!(rr.size.width == 6);
    assert!(rr.size.height == 10);
    assert!(rr.origin.x == 2);
    assert!(rr.origin.y == 5);
}

#[test]
fn test_min_max_x_y() {
    let p = Rect::new(Point2D::new(0u32, 0u32), Size2D::new(50u32, 40u32));
    assert!(p.max_y() == 40);
    assert!(p.min_y() == 0);
    assert!(p.max_x() == 50);
    assert!(p.min_x() == 0);

    let r = Rect::new(Point2D::new(-10, -5), Size2D::new(50, 40));
    assert!(r.max_y() == 35);
    assert!(r.min_y() == -5);
    assert!(r.max_x() == 40);
    assert!(r.min_x() == -10);
}

#[test]
fn test_is_empty() {
    assert!(Rect::new(Point2D::new(0u32, 0u32), Size2D::new(0u32, 0u32)).is_empty());
    assert!(Rect::new(Point2D::new(0u32, 0u32), Size2D::new(10u32, 0u32)).is_empty());
    assert!(Rect::new(Point2D::new(0u32, 0u32), Size2D::new(0u32, 10u32)).is_empty());
    assert!(!Rect::new(Point2D::new(0u32, 0u32), Size2D::new(1u32, 1u32)).is_empty());
    assert!(Rect::new(Point2D::new(10u32, 10u32), Size2D::new(0u32, 0u32)).is_empty());
    assert!(Rect::new(Point2D::new(10u32, 10u32), Size2D::new(10u32, 0u32)).is_empty());
    assert!(Rect::new(Point2D::new(10u32, 10u32), Size2D::new(0u32, 10u32)).is_empty());
    assert!(!Rect::new(Point2D::new(10u32, 10u32), Size2D::new(1u32, 1u32)).is_empty());
}