File: Polygon.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (275 lines) | stat: -rw-r--r-- 7,213 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This code is based on the CRAB engine
 *
 * Copyright (c) Arvind Raja Yadav
 *
 * Licensed under MIT
 *
 */

#include "graphics/screen.h"
#include "crab/crab.h"
#include "crab/Polygon.h"

namespace Crab {

// Calculate the distance between [minA, maxA] and [minB, maxB]
// The distance will be negative if the intervals overlap
float IntervalDistance(float minA, float maxA, float minB, float maxB) {
	if (minA < minB)
		return minB - maxA;
	return minA - maxB;
}

void Polygon2D::addPoint(const Vector2f &ref, const Common::String &x, const Common::String &y, Vector2f &min, Vector2f &max) {
	Vector2f p;
	p.x = ref.x + stringToNumber<float>(x);
	p.y = ref.y + stringToNumber<float>(y);

	if (p.x < min.x)
		min.x = p.x;
	if (p.x > max.x)
		max.x = p.x;

	if (p.y < min.y)
		min.y = p.y;
	if (p.y > max.y)
		max.y = p.y;

	_point.push_back(p);
}

void Polygon2D::load(rapidxml::xml_node<char> *node, Rect &bounds) {
	Vector2f ref;
	ref.load(node);

	// Converting a polygon to an axis aligned bounding box is easy - just record the minimum and maximum values of x and y
	// for the vertices of the polygon, then minimum = top left corner, max - min = dimensions
	Vector2f min(std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
	Vector2f max(-std::numeric_limits<float>::max(), -std::numeric_limits<float>::max());

	_point.clear();
	rapidxml::xml_node<char> *polynode = node->first_node("polygon");
	if (polynode != nullptr) {
		Common::String points, x, y;
		loadStr(points, "points", polynode);

		bool comma = false;
		for (const auto &i : points) {
			if (i == ',')
				comma = true;
			else if (i == ' ') {
				addPoint(ref, x, y, min, max);
				comma = false;
				x.clear();
				y.clear();
			} else if (comma)
				y += i;
			else
				x += i;
		}

		addPoint(ref, x, y, min, max);

		bounds.x = min.x;
		bounds.y = min.y;
		bounds.w = max.x - min.x;
		bounds.h = max.y - min.y;
	}

	setEdge();
}

void Polygon2D::setEdge() {
	_edge.clear();
	Vector2f p1, p2, res;
	for (uint i = 0; i < _point.size(); i++) {
		p1 = _point[i];
		if (i + 1 >= _point.size())
			p2 = _point[0];
		else
			p2 = _point[i + 1];

		res.x = p2.x - p1.x;
		res.y = p2.y - p1.y;
		_edge.push_back(res);
	}
}

Vector2f Polygon2D::center() const {
	Vector2f total;
	for (uint i = 0; i < _point.size(); i++) {
		total.x += _point[i].x;
		total.y += _point[i].y;
	}

	Vector2f ret;
	if (_point.size() > 0) {
		ret.x = total.x / _point.size();
		ret.y = total.y / _point.size();
	}
	return ret;
}

void Polygon2D::offset(const float &x, const float &y) {
	for (auto &i : _point) {
		i.x += x;
		i.y += y;
	}
}

void Polygon2D::project(const Vector2f &axis, float &min, float &max) const {
	// To project a point on an axis use the dot product
	float d = axis.dotProduct(_point[0]);
	min = d;
	max = d;

	for (auto i = _point.begin(); i != _point.end(); ++i) {
		d = i->dotProduct(axis);

		if (d < min)
			min = d;
		else if (d > max)
			max = d;
	}
}

PolygonCollisionResult Polygon2D::collide(const Rect &rect) const {
	Polygon2D polyB;
	Vector2f p;
	p.x = rect.x;
	p.y = rect.y;
	polyB._point.push_back(p);
	p.x = rect.x + rect.w;
	p.y = rect.y;
	polyB._point.push_back(p);
	p.x = rect.x + rect.w;
	p.y = rect.y + rect.h;
	polyB._point.push_back(p);
	p.x = rect.x;
	p.y = rect.y + rect.h;
	polyB._point.push_back(p);
	polyB.setEdge();

	return collide(polyB);
}

PolygonCollisionResult Polygon2D::collide(const Polygon2D &polyB) const {
	PolygonCollisionResult result;
	result._intersect = true;

	int edgeCountA = _edge.size();
	int edgeCountB = polyB._edge.size();
	float minIntervalDistance = std::numeric_limits<float>::max();
	Vector2f translationAxis;
	Vector2f e;

	// Loop through all the edges of both polygons
	for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) {
		if (edgeIndex < edgeCountA)
			e = _edge[edgeIndex];
		else
			e = polyB._edge[edgeIndex - edgeCountA];

		// ===== 1. Find if the Polygon2Ds are currently intersecting =====

		// Find the axis perpendicular to the current edge
		Vector2f axis(-e.y, e.x);
		axis.normalize();

		// Find the projection of the Polygon2D on the current axis
		float minA = 0;
		float minB = 0;
		float maxA = 0;
		float maxB = 0;
		project(axis, minA, maxA);
		polyB.project(axis, minB, maxB);

		// Check if the Polygon2D projections are currently intersecting
		float intervalDistance = IntervalDistance(minA, maxA, minB, maxB);
		if (intervalDistance > 0) {
			// If the Polygon2Ds are not intersecting and won't intersect, exit the loop
			result._intersect = false;
			break;
		}

		// Check if the current interval distance is the minimum one. If so store
		// the interval distance and the current distance.
		// This will be used to calculate the minimum translation vector
		intervalDistance = abs(intervalDistance);
		if (intervalDistance < minIntervalDistance) {
			minIntervalDistance = intervalDistance;
			translationAxis = axis;

			Vector2f d, ca, cb;
			ca = center();
			cb = polyB.center();

			d.x = ca.x - cb.x;
			d.y = ca.y - cb.y;

			if (d.dotProduct(translationAxis) < 0) {
				translationAxis.x = -translationAxis.x;
				translationAxis.y = -translationAxis.y;
			}
		}
	}

	// The minimum translation vector can be used to push the Polygon2Ds apart.
	// First moves the Polygon2Ds by their velocity
	// then move polyA by MinimumTranslationVector.
	if (result._intersect) {
		result._mtv.x = translationAxis.x * minIntervalDistance;
		result._mtv.y = translationAxis.y * minIntervalDistance;
	}

	return result;
}

bool Polygon2D::contains(const float &x, const float &y) {
	bool result = false;

	for (uint i = 0, j = _point.size() - 1; i < _point.size(); j = i++) {
		if (((_point[i].y > y) != (_point[j].y > y)) &&
			(x < (_point[j].x - _point[i].x) * (y - _point[i].y) / (_point[j].y - _point[i].y) + _point[i].x))
			result = !result;
	}

	return result;
}

void Polygon2D::draw(const int &xOffset, const int &yOffset, const uint8 &r, const uint8 &g, const uint8 &b, const uint8 &a) {
	Vector2f p1, p2;
	for (uint i = 0; i < _point.size(); i++) {
		p1 = _point[i];
		if (i + 1 >= _point.size())
			p2 = _point[0];
		else
			p2 = _point[i + 1];

		g_engine->_screen->drawLine(p1.x + xOffset, p1.y + yOffset, p2.x + xOffset, p2.y + yOffset, g_engine->_format->ARGBToColor(a, r, g, b));
	}
}

} // End of namespace Crab