2
// C++ Implementation: Polygon
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2009
9
// This program is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 2 of the License, or
12
// (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program; if not, write to the Free Software
21
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.//
28
#include "PolygonPoint.h"
32
#include <wfmath/vector.h>
33
#include <wfmath/polygon.h>
35
#include "../MathConverter.h"
36
#include "../EmberOgre.h"
37
#include "framework/LoggingInstance.h"
43
namespace Manipulation {
45
Polygon::Polygon(Ogre::SceneNode* baseNode, IPolygonPositionProvider* positionProvider)
46
: mBaseNode(baseNode), mPositionProvider(positionProvider), mRenderer(*this)
56
Ogre::SceneNode* Polygon::getBaseNode()
61
WFMath::Polygon<2> Polygon::getShape() const
63
WFMath::Polygon<2> poly;
65
for (PointStore::const_iterator I = mPoints.begin(); I != mPoints.end(); ++I) {
66
poly.addCorner(i++, (*I)->getLocalPosition());
72
void Polygon::loadFromShape(const WFMath::Polygon<2>& shape)
75
for(int i = 0; i < shape.numCorners(); ++i) {
76
const WFMath::Point<2>& position = shape[i];
77
PolygonPoint* point = new PolygonPoint(*this, position);
78
mPoints.push_back(point);
85
for (PointStore::iterator I = mPoints.begin(); I != mPoints.end(); ++I) {
92
const Polygon::PointStore& Polygon::getPoints() const
98
IPolygonPositionProvider* Polygon::getPositionProvider() const
100
return mPositionProvider;
103
void Polygon::updateRender()
108
PolygonPoint* Polygon::insertPointBefore(PolygonPoint& point)
110
if (mPoints.size()) {
111
PointStore::iterator I = std::find(mPoints.begin(), mPoints.end(), &point);
112
if (I != mPoints.end()) {
113
PolygonPoint* newPoint = new PolygonPoint(*this);
114
mPoints.insert(I, newPoint);
121
bool Polygon::reInsertPointBefore(PolygonPoint& point, PolygonPoint& existingPoint)
123
if (&existingPoint.getPolygon() == this) {
124
if (mPoints.size()) {
125
PointStore::iterator I = std::find(mPoints.begin(), mPoints.end(), &point);
126
if (I != mPoints.end()) {
127
mPoints.insert(I, &existingPoint);
135
bool Polygon::reInsertPoint(size_t index, PolygonPoint& point)
137
if (&point.getPolygon() != this) {
142
PointStore::iterator I = mPoints.begin();
143
while (I != mPoints.end() && i < index) {
147
mPoints.insert(I, &point);
152
PolygonPoint* Polygon::getPointBefore(PolygonPoint& point)
154
if (mPoints.size()) {
155
PointStore::iterator I = std::find(mPoints.begin(), mPoints.end(), &point);
156
if (I != mPoints.end()) {
157
if (I == mPoints.begin()) {
158
return *(mPoints.rbegin());
167
PolygonPoint* Polygon::getPointAfter(PolygonPoint& point)
169
if (mPoints.size()) {
170
PointStore::iterator I = std::find(mPoints.begin(), mPoints.end(), &point);
171
if (I != mPoints.end()) {
172
if (I == (--mPoints.end())) {
173
return *(mPoints.begin());
182
bool Polygon::removePoint(PolygonPoint& point)
184
if (mPoints.size()) {
185
PointStore::iterator I = std::find(mPoints.begin(), mPoints.end(), &point);
186
if (I != mPoints.end()) {