FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
cellgrid.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 <cassert>
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
31 #include "util/log/logger.h"
32 
33 #include "cellgrid.h"
34 
35 namespace FIFE {
36  static Logger _log(LM_CELLGRID);
37 
39  FifeClass(),
40  m_matrix(),
41  m_inverse_matrix(),
42  m_xshift(0),
43  m_yshift(0),
44  m_zshift(0),
45  m_xscale(1),
46  m_yscale(1),
47  m_zscale(1),
48  m_rotation(0),
49  m_allow_diagonals(false) {
51  }
52 
54  }
55 
56  void CellGrid::getAccessibleCoordinates(const ModelCoordinate& curpos, std::vector<ModelCoordinate>& coordinates) {
57  coordinates.clear();
58  for (int32_t x = curpos.x - 1; x <= curpos.x + 1; x++) {
59  for (int32_t y = curpos.y - 1; y <= curpos.y + 1; y++) {
60  ModelCoordinate pt(x, y);
61  if (isAccessible(curpos, pt)) {
62  coordinates.push_back(pt);
63  }
64  }
65  }
66  }
67 
69  m_matrix.loadRotate(m_rotation, 0.0, 0.0, 1.0);
73  }
74 
76  return toMapCoordinates(intPt2doublePt(layer_coords));
77  }
78 
80  double o = (pt2.x - pt1.x) * (pt.y - pt1.y) - (pt.x - pt1.x) * (pt2.y - pt1.y);
81  if (o > 0.0) {
82  return 1;
83  } else if (o < 0.0) {
84  return -1;
85  }
86  return 0;
87  }
88 
90  double o1 = orientation(pt1, pt2, pt);
91  double o2 = orientation(pt2, pt3, pt);
92  double o3 = orientation(pt3, pt1, pt);
93  bool result = (o1 == o2) && (o2 == o3);
94  FL_DBG(_log, LMsg("ptInTriangle, pt=") << pt << " pt1=" << pt1 << " pt2=" << pt2 << " pt3=" << pt3 << " in=" << result);
95  return result;
96  }
97 }
double m_xscale
Definition: cellgrid.h:255
Matrix inverse() const
Adjoint method inverse, constant time inversion implementation.
Definition: matrix.h:63
double m_zscale
Definition: cellgrid.h:257
virtual ~CellGrid()
Destructor.
Definition: cellgrid.cpp:53
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
double m_yshift
Definition: cellgrid.h:253
Base class for all fife classes Used e.g.
Definition: fifeclass.h:42
static Logger _log(LM_AUDIO)
DoubleMatrix m_inverse_matrix
Definition: cellgrid.h:251
Matrix & applyScale(T x, T y, T z)
Apply scale into this matrix.
Definition: matrix.h:148
double m_rotation
Definition: cellgrid.h:258
DoubleMatrix m_matrix
Definition: cellgrid.h:250
void updateMatrices()
Definition: cellgrid.cpp:68
Matrix & loadRotate(T angle, T x, T y, T z)
Make this a rotation matrix.
Definition: matrix.h:113
double m_zshift
Definition: cellgrid.h:254
A 3D Point.
Definition: point.h:205
double m_yscale
Definition: cellgrid.h:256
int32_t orientation(const ExactModelCoordinate &pt, const ExactModelCoordinate &pt1, const ExactModelCoordinate &pt2)
Definition: cellgrid.cpp:79
DoublePoint intPt2doublePt(Point pt)
Convert from 2D int32_t point to 2D double point.
Definition: point.h:349
CellGrid()
Constructor.
Definition: cellgrid.cpp:38
double m_xshift
Definition: cellgrid.h:252
void getAccessibleCoordinates(const ModelCoordinate &curpos, std::vector< ModelCoordinate > &coordinates)
Gets the coordinates that are accesible from given point only cells adjacent to given cell are consid...
Definition: cellgrid.cpp:56
ExactModelCoordinate toMapCoordinates(const ModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: cellgrid.cpp:75
bool ptInTriangle(const ExactModelCoordinate &pt, const ExactModelCoordinate &pt1, const ExactModelCoordinate &pt2, const ExactModelCoordinate &pt3)
Definition: cellgrid.cpp:89
Matrix & applyTranslate(T x, T y, T z)
Apply translation into this matrix.
Definition: matrix.h:180
virtual bool isAccessible(const ModelCoordinate &curpos, const ModelCoordinate &target)=0
Tells if given target point is accessible from curpos only cells adjacent to curpos are considered in...
#define FL_DBG(logger, msg)
Definition: logger.h:70