ScummVM API documentation
vectors.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22/*
23 * This code is based on the CRAB engine
24 *
25 * Copyright (c) Arvind Raja Yadav
26 *
27 * Licensed under MIT
28 *
29 */
30
31#ifndef CRAB_VECTORS_H
32#define CRAB_VECTORS_H
33
34#include "common/debug.h"
35#include "crab/loaders.h"
36
37namespace Crab {
38
39//------------------------------------------------------------------------
40// Purpose: A simple 2D vector class
41//------------------------------------------------------------------------
42template<typename T>
43class Vector2D {
44public:
45 T x, y;
46
47 Vector2D(T X = 0, T Y = 0) { Set(X, Y); }
48 void Set(T X = 0, T Y = 0) {
49 x = X;
50 y = Y;
51 }
52
53 bool load(rapidxml::xml_node<char> *node, const bool &echo = true) {
54 return loadNum(x, "x", node, echo) && loadNum(y, "y", node, echo);
55 }
56
57 void saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root, const char *name) {
58 warning("STUB: Vector2D::saveState()");
59#if 0
60 rapidxml::xml_node<char> *child = doc.allocate_node(rapidxml::node_element, name);
61 child->append_attribute(doc.allocate_attribute("x", g_engine->_stringPool->Get((int)x)));
62 child->append_attribute(doc.allocate_attribute("y", g_engine->_stringPool->Get((int)y)));
63 root->append_node(child);
64#endif
65 }
66
67 double magnitude() {
68 return sqrt(x * x + y * y);
69 }
70
71 double magSqr() { // Added to avoid unnecessary sqrt calls (SZ)
72 return (x * x + y * y);
73 }
74
75 void normalize() {
76 double m = magnitude();
77 x = x / m;
78 y = y / m;
79 }
80
81 float dotProduct(const Vector2D<T> &v) const {
82 return x * v.x + y * v.y;
83 }
84
85 // operators
86 void operator+=(const Vector2D &v) {
87 x += v.x;
88 y += v.y;
89 }
90 void operator-=(const Vector2D &v) {
91 x -= v.x;
92 y -= v.y;
93 }
94 bool operator==(const Vector2D &v) const { return x == v.x && y == v.y; }
95 bool operator!=(const Vector2D &v) const { return x != v.x || y != v.y; }
96 friend Vector2D operator-(const Vector2D &v1, const Vector2D &v2) {
97 return Vector2D(v1.x - v2.x, v1.y - v2.y);
98 }
99};
100
101typedef Vector2D<int> Vector2i;
103
104//------------------------------------------------------------------------
105// Purpose: A simple 3D vector class
106//------------------------------------------------------------------------
107template<typename T>
108class Vector3D {
109public:
110 T x, y, z;
111
112 Vector3D(T X = 0, T Y = 0, T Z = 0) {
113 set(X, Y, Z);
114 }
115
116 Vector3D(rapidxml::xml_node<char> *node) {
117 load(node);
118 }
119
120 void set(T X = 0, T Y = 0, T Z = 0) {
121 x = X;
122 y = Y;
123 z = Z;
124 }
125
126 bool load(rapidxml::xml_node<char> *node) {
127 return loadXYZ<T>(x, y, z, node);
128 }
129
130 // operators
131 void operator+=(const Vector3D &v) {
132 x += v.x;
133 y += v.y;
134 z += v.z;
135 }
136 void operator-=(const Vector3D &v) {
137 x -= v.x;
138 y -= v.y;
139 z -= v.z;
140 }
141 bool operator==(const Vector3D &v) { return x == v.x && y == v.y && z == v.z; }
142};
143
144typedef Vector3D<int> Vector3i;
146
147} // End of namespace Crab
148
149#endif // CRAB_VECTORS_H
Definition: vectors.h:43
Definition: vectors.h:108
void warning(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Engine * g_engine