~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/manipulation/Polygon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: Polygon
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2009
 
8
//
 
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.
 
13
// 
 
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.
 
18
// 
 
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.//
 
22
//
 
23
#ifdef HAVE_CONFIG_H
 
24
#include "config.h"
 
25
#endif
 
26
 
 
27
#include "Polygon.h"
 
28
#include "PolygonPoint.h"
 
29
 
 
30
 
 
31
#include <Ogre.h>
 
32
#include <wfmath/vector.h>
 
33
#include <wfmath/polygon.h>
 
34
 
 
35
#include "../MathConverter.h"
 
36
#include "../EmberOgre.h"
 
37
#include "framework/LoggingInstance.h"
 
38
 
 
39
 
 
40
 
 
41
namespace EmberOgre {
 
42
 
 
43
namespace Manipulation {
 
44
 
 
45
Polygon::Polygon(Ogre::SceneNode* baseNode, IPolygonPositionProvider* positionProvider)
 
46
: mBaseNode(baseNode), mPositionProvider(positionProvider), mRenderer(*this)
 
47
{
 
48
}
 
49
 
 
50
 
 
51
Polygon::~Polygon()
 
52
{
 
53
        clear();
 
54
}
 
55
 
 
56
Ogre::SceneNode* Polygon::getBaseNode()
 
57
{
 
58
        return mBaseNode;
 
59
}
 
60
 
 
61
WFMath::Polygon<2> Polygon::getShape() const
 
62
{
 
63
        WFMath::Polygon<2> poly;
 
64
        unsigned int i = 0;
 
65
        for (PointStore::const_iterator I = mPoints.begin(); I != mPoints.end(); ++I) {
 
66
                poly.addCorner(i++, (*I)->getLocalPosition());
 
67
        }
 
68
        return poly;
 
69
}
 
70
 
 
71
 
 
72
void Polygon::loadFromShape(const WFMath::Polygon<2>& shape)
 
73
{
 
74
        clear();
 
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);
 
79
        }
 
80
        mRenderer.update();
 
81
}
 
82
 
 
83
void Polygon::clear()
 
84
{
 
85
        for (PointStore::iterator I = mPoints.begin(); I != mPoints.end(); ++I) {
 
86
                delete *I;
 
87
        }
 
88
        mPoints.clear();
 
89
        mRenderer.update();
 
90
}
 
91
 
 
92
const Polygon::PointStore& Polygon::getPoints() const
 
93
{
 
94
        return mPoints;
 
95
}
 
96
 
 
97
 
 
98
IPolygonPositionProvider* Polygon::getPositionProvider() const
 
99
{
 
100
        return mPositionProvider;
 
101
}
 
102
 
 
103
void Polygon::updateRender()
 
104
{
 
105
        mRenderer.update();
 
106
}
 
107
 
 
108
PolygonPoint* Polygon::insertPointBefore(PolygonPoint& point)
 
109
{
 
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);
 
115
                        return newPoint;
 
116
                }
 
117
        }
 
118
        return 0;
 
119
}
 
120
 
 
121
bool Polygon::reInsertPointBefore(PolygonPoint& point, PolygonPoint& existingPoint)
 
122
{
 
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);
 
128
                                return true;
 
129
                        }
 
130
                }
 
131
        }
 
132
        return false;
 
133
}
 
134
 
 
135
bool Polygon::reInsertPoint(size_t index, PolygonPoint& point)
 
136
{
 
137
        if (&point.getPolygon() != this) {
 
138
                return false;
 
139
        }
 
140
        
 
141
        size_t i = 0;
 
142
        PointStore::iterator I = mPoints.begin();
 
143
        while (I != mPoints.end() && i < index) {
 
144
                ++i;
 
145
                ++I;
 
146
        }
 
147
        mPoints.insert(I, &point);
 
148
        return true;
 
149
}
 
150
 
 
151
 
 
152
PolygonPoint* Polygon::getPointBefore(PolygonPoint& point)
 
153
{
 
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());
 
159
                        } else {
 
160
                                return *(--I);
 
161
                        }
 
162
                }
 
163
        }
 
164
        return 0;
 
165
}
 
166
        
 
167
PolygonPoint* Polygon::getPointAfter(PolygonPoint& point)
 
168
{
 
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());
 
174
                        } else {
 
175
                                return *(++I);
 
176
                        }
 
177
                }
 
178
        }
 
179
        return 0;
 
180
}
 
181
 
 
182
bool Polygon::removePoint(PolygonPoint& point)
 
183
{
 
184
        if (mPoints.size()) {
 
185
                PointStore::iterator I = std::find(mPoints.begin(), mPoints.end(), &point);
 
186
                if (I != mPoints.end()) {
 
187
                        mPoints.erase(I);
 
188
                        return true;
 
189
                }
 
190
        }
 
191
        return false;
 
192
}
 
193
 
 
194
 
 
195
 
 
196
}
 
197
 
 
198
}