FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
angles.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <iostream>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
32 #include "model/structures/layer.h"
33 
34 #include "angles.h"
35 
36 namespace FIFE {
37  int32_t getIndexByAngle(int32_t angle, const type_angle2id& angle2id, int32_t& closestMatchingAngle) {
38  if (angle2id.empty()) {
39  return -1;
40  }
41  if (angle2id.size() == 1) {
42  closestMatchingAngle = angle2id.begin()->first;
43  return angle2id.begin()->second;
44  }
45 
46  int32_t wangle = (360 + angle) % 360;
47  type_angle2id::const_iterator u(angle2id.upper_bound(wangle));
48  type_angle2id::const_iterator tmp;
49 
50  // take care of the forward wrapping case
51  if (u == angle2id.end()) {
52  int32_t ud = wangle - (--u)->first;
53  int32_t ld = 360 - wangle + angle2id.begin()->first;
54  if (ud > ld) {
55  // wrapped value (first)
56  closestMatchingAngle = angle2id.begin()->first;
57  return angle2id.begin()->second;
58  }
59  // non-wrapped value
60  closestMatchingAngle = u->first;
61  return u->second;
62  }
63 
64  // take care of the backward wrapping case
65  if (u == angle2id.begin()) {
66  tmp = angle2id.end();
67  tmp--;
68  int32_t ld = u->first - wangle;
69  int32_t ud = 360 - tmp->first + wangle;
70  if (ud > ld) {
71  // non-wrapped value (first)
72  closestMatchingAngle = angle2id.begin()->first;
73  return angle2id.begin()->second;
74  }
75  // wrapped value (last)
76  closestMatchingAngle = tmp->first;
77  return tmp->second;
78  }
79 
80  // value in the middle...
81  int32_t ud = u->first - wangle;
82  int32_t ucm = u->first;
83  int32_t ui = u->second;
84  u--;
85  int32_t ld = wangle - u->first;
86  int32_t lcm = u->first;
87  int32_t li = u->second;
88 
89  // if ud and ls is equal then lcm is prefered (next angle)
90  if (ud < ld) {
91  closestMatchingAngle = ucm;
92  return ui;
93  }
94  closestMatchingAngle = lcm;
95  return li;
96  }
97 
98  int32_t getAngleBetween(const Location& loc1, const Location& loc2) {
101 
102  double dy = (c2.y - c1.y);
103  double dx = (c2.x - c1.x);
104  // add grid rotation to angle, to guarantee uniform angles (not grid based)
105  int32_t angle = round(Mathd::ATan2(-dy,dx)*(180.0/Mathd::pi()) + loc1.getLayer()->getCellGrid()->getRotation());
106  if (angle < 0) {
107  angle += 360;
108  }
109  angle %= 360;
110  return angle;
111  }
112 
113  Location getFacing(const Location& loc, const int32_t angle) {
114  Location facing(loc);
116  // remove grid rotation from angle, to guarantee uniform angles (not grid based)
117  double tmpAngle = static_cast<double>(angle) - loc.getLayer()->getCellGrid()->getRotation();
118  emc.x += Mathd::Cos(tmpAngle * (Mathd::pi()/180.0));
119  emc.y -= Mathd::Sin(tmpAngle * (Mathd::pi()/180.0));
120  facing.setMapCoordinates(emc);
121 
122  return facing;
123  }
124 
125  int32_t getAngleBetween(const ExactModelCoordinate& emc1, const ExactModelCoordinate& emc2) {
126  double dy = (emc2.y - emc1.y);
127  double dx = (emc2.x - emc1.x);
128 
129  int32_t angle = round(Mathd::ATan2(-dy,dx)*(180.0/Mathd::pi()));
130  if (angle < 0) {
131  angle += 360;
132  }
133  angle %= 360;
134  return angle;
135  }
136 
137  ExactModelCoordinate getFacing(const ExactModelCoordinate& emc, const int32_t angle) {
138  ExactModelCoordinate result = emc;
139  result.x += Mathd::Cos(static_cast<double>(angle) * (Mathd::pi()/180.0));
140  result.y -= Mathd::Sin(static_cast<double>(angle) * (Mathd::pi()/180.0));
141  return result;
142  }
143 }
static T ATan2(T _x, T _y)
Definition: fife_math.h:207
int32_t getAngleBetween(const Location &loc1, const Location &loc2)
Gets angle of vector defined by given locations.
Definition: angles.cpp:98
static T Cos(T _val)
Definition: fife_math.h:217
void setMapCoordinates(const ExactModelCoordinate &coordinates)
Sets map coordinates to this location.
Definition: location.cpp:98
Layer * getLayer() const
Gets the layer where this location is pointing to.
Definition: location.cpp:83
static T Sin(T _val)
Definition: fife_math.h:267
const double getRotation() const
Get the cellgrid rotation.
Definition: cellgrid.h:228
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
A 3D Point.
Definition: point.h:205
int32_t getIndexByAngle(int32_t angle, const type_angle2id &angle2id, int32_t &closestMatchingAngle)
Returns id for given angle from angle2id map in case there are no elements in the map...
Definition: angles.cpp:37
static num_type pi()
Definition: fife_math.h:134
std::map< uint32_t, int32_t > type_angle2id
Definition: angles.h:37
Location getFacing(const Location &loc, const int32_t angle)
Gets facing location defined by given angle and location.
Definition: angles.cpp:113
ExactModelCoordinate getMapCoordinates() const
Gets map coordinates set to this location.
Definition: location.cpp:117