aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/source/Rect2D.cpp
blob: 3aea02f512e0b2633bad245b3e5d89aff1822a99 (plain)
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
/**
 @file Rect2D.cpp
 
 @maintainer Morgan McGuire, http://graphics.cs.williams.edu
  
  @created 2003-11-13
  @created 2011-06-16

  Copyright 2000-2012, Morgan McGuire.
  All rights reserved.
 */

#include "G3D/platform.h"
#include "G3D/Rect2D.h"
#include "G3D/Any.h"
#include "G3D/stringutils.h"
#include "G3D/BinaryInput.h"
#include "G3D/BinaryOutput.h"

namespace G3D {

const Rect2D& Rect2D::empty() {
    static Rect2D r;
    return r;
}


void Rect2D::serialize(class BinaryOutput& b) const {
    min.serialize(b);
    max.serialize(b);
}

    
void Rect2D::deserialize(class BinaryInput& b) {
    min.deserialize(b);
    max.deserialize(b);
}


/** \param any Must either Rect2D::xywh(#, #, #, #) or Rect2D::xyxy(#, #, #, #)*/
Rect2D::Rect2D(const Any& any) {
    if (any.name() == "Rect2D::empty" || any.name() == "AABox2D::empty") {
        *this = empty();
        return;
    }

    any.verifyName("Rect2D::xyxy", "Rect2D::xywh");
    any.verifyType(Any::ARRAY);
    any.verifySize(4);
    if (any.name() == "Rect2D::xywh") {
        *this = Rect2D::xywh(any[0], any[1], any[2], any[3]);
    } else {
        *this = Rect2D::xyxy(any[0], any[1], any[2], any[3]);
    }
}


/** Converts the Rect2D to an Any. */
Any Rect2D::toAny() const {
    if (isEmpty()) {
        return Any(Any::ARRAY, "Rect2D::empty");
    } else {
        Any any(Any::ARRAY, "Rect2D::xywh");
        any.append(Any(x0()), Any(y0()), Any(width()), Any(height()));
        return any;
    }
}

}