aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/include/G3D/Vector2int16.h
blob: d24662b18b9ca045cf28e9ed9692cf8a7772b412 (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
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
/**
  @file Vector2int16.h
  
  @maintainer Morgan McGuire, matrix@brown.edu

  @created 2003-08-09
  @edited  2010-01-03

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

#ifndef Vector2int16_h
#define Vector2int16_h

#include "G3D/platform.h"
#include "G3D/g3dmath.h"
#include "G3D/HashTrait.h"

namespace G3D {

class Any;
/**
 \class Vector2int16 
 A Vector2 that packs its fields into G3D::int16 s.
 */
G3D_BEGIN_PACKED_CLASS(2)
Vector2int16 {
private:
    // Hidden operators
    bool operator<(const Vector2int16&) const;
    bool operator>(const Vector2int16&) const;
    bool operator<=(const Vector2int16&) const;
    bool operator>=(const Vector2int16&) const;

public:
    G3D::int16              x;
    G3D::int16              y;

    Vector2int16() : x(0), y(0) {}
    Vector2int16(G3D::int16 _x, G3D::int16 _y) : x(_x), y(_y){}
    explicit Vector2int16(const class Vector2& v);
    explicit Vector2int16(class BinaryInput& bi);
    explicit Vector2int16(const class Any& a);
    explicit Vector2int16(const class Vector2int32& v);

    Any toAny() const;
    
    Vector2int16& operator=(const Any& a);

    inline G3D::int16& operator[] (int i) {
        debugAssert(((unsigned int)i) <= 1);
        return ((G3D::int16*)this)[i];
    }

    inline const G3D::int16& operator[] (int i) const {
        debugAssert(((unsigned int)i) <= 1);
        return ((G3D::int16*)this)[i];
    }

    inline Vector2int16 operator+(const Vector2int16& other) const {
        return Vector2int16(x + other.x, y + other.y);
    }

    inline Vector2int16 operator-(const Vector2int16& other) const {
        return Vector2int16(x - other.x, y - other.y);
    }

    inline Vector2int16 operator*(const Vector2int16& other) const {
        return Vector2int16(x * other.x, y * other.y);
    }

    Vector2int16 operator-() const {
        return Vector2int16(-x, -y);
    }

    inline Vector2int16 operator*(const int s) const {
        return Vector2int16(x * s, y * s);
    }

    inline Vector2int16& operator+=(const Vector2int16& other) {
        x += other.x;
        y += other.y;
        return *this;
    }

    bool isZero() const {
        return (x == 0) && (y == 0);
    }

    /** Shifts both x and y */
    inline Vector2int16 operator>>(const int s) const {
        return Vector2int16(x >> s, y >> s);
    }

    /** Shifts both x and y */
    inline Vector2int16 operator<<(const int s) const {
        return Vector2int16(x << s, y << s);
    }

    inline Vector2int16& operator-=(const Vector2int16& other) {
        x -= other.x;
        y -= other.y;
        return *this;
    }

    inline Vector2int16& operator*=(const Vector2int16& other) {
        x *= other.x;
        y *= other.y;
        return *this;
    }

    Vector2int16 clamp(const Vector2int16& lo, const Vector2int16& hi);

    inline bool operator== (const Vector2int16& rkVector) const {
        return ((int32*)this)[0] == ((int32*)&rkVector)[0];
    }

    inline bool operator!= (const Vector2int16& rkVector) const {
        return ((int32*)this)[0] != ((int32*)&rkVector)[0];
    }

    Vector2int16 max(const Vector2int16& v) const {
        return Vector2int16(iMax(x, v.x), iMax(y, v.y));
    }

    Vector2int16 min(const Vector2int16& v) const {
        return Vector2int16(iMin(x, v.x), iMin(y, v.y));
    }

    void serialize(class BinaryOutput& bo) const;
    void deserialize(class BinaryInput& bi);
}
G3D_END_PACKED_CLASS(2)

typedef Vector2int16 Point2int16;    

}

template<> struct HashTrait<G3D::Vector2int16> {
    static size_t hashCode(const G3D::Vector2int16& key) { return static_cast<size_t>(key.x + ((int)key.y << 16)); }
};

#endif