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

« back to all changes in this revision

Viewing changes to src/components/ogre/terrain/TerrainInfo.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: TerrainInfo
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2006
 
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 "TerrainInfo.h"
 
28
#include <Mercator/BasePoint.h>
 
29
 
 
30
namespace EmberOgre {
 
31
namespace Terrain {
 
32
 
 
33
 
 
34
TerrainInfo::TerrainInfo()
 
35
:  mXminBasePoint(0), mXmaxBasePoint(0), mYminBasePoint(0), mYmaxBasePoint(0),
 
36
         mXminBasePointAdjusted(0), mXmaxBasePointAdjusted(0), mYminBasePointAdjusted(0), mYmaxBasePointAdjusted(0)
 
37
{
 
38
        recalculateSize();
 
39
}
 
40
 
 
41
 
 
42
void TerrainInfo::setBasePoint(const WFMath::Point<2>& position, const Mercator::BasePoint& basepoint)
 
43
{
 
44
        mXminBasePoint = std::min<WFMath::CoordType>(position.x(), mXminBasePoint);
 
45
        mXmaxBasePoint = std::max<WFMath::CoordType>(position.x(), mXmaxBasePoint);
 
46
        mYminBasePoint = std::min<WFMath::CoordType>(position.y(), mYminBasePoint);
 
47
        mYmaxBasePoint = std::max<WFMath::CoordType>(position.y(), mYmaxBasePoint);
 
48
        
 
49
        adjustBasePointPositionDown(mXminBasePointAdjusted, mXminBasePoint);
 
50
        adjustBasePointPositionUp(mXmaxBasePointAdjusted, mXmaxBasePoint);
 
51
        adjustBasePointPositionDown(mYminBasePointAdjusted, mYminBasePoint);
 
52
        adjustBasePointPositionUp(mYmaxBasePointAdjusted, mYmaxBasePoint);
 
53
        
 
54
        recalculateSize();
 
55
}
 
56
 
 
57
void TerrainInfo::recalculateSize()
 
58
{
 
59
        mCalculatedSize = WFMath::AxisBox<2>(WFMath::Point<2>(mXminBasePointAdjusted * 64, mYminBasePointAdjusted * 64), WFMath::Point<2>(mXmaxBasePointAdjusted * 64, mYmaxBasePointAdjusted * 64));
 
60
        mCalculatedSegmentSize = WFMath::AxisBox<2>(WFMath::Point<2>(mXminBasePointAdjusted, mYminBasePointAdjusted ), WFMath::Point<2>(mXmaxBasePointAdjusted , mYmaxBasePointAdjusted ));
 
61
        
 
62
        int mercatorSegmentsPerOgrePage = mPageIndicesSize / 64;
 
63
        mCalculatedPageSize = WFMath::AxisBox<2>(WFMath::Point<2>(mXminBasePointAdjusted / mercatorSegmentsPerOgrePage, mYminBasePointAdjusted / mercatorSegmentsPerOgrePage), WFMath::Point<2>(mXmaxBasePointAdjusted / mercatorSegmentsPerOgrePage, mYmaxBasePointAdjusted / mercatorSegmentsPerOgrePage));
 
64
}
 
65
 
 
66
 
 
67
void TerrainInfo::adjustBasePointPositionUp(WFMath::CoordType& basePointPositionAdjusted, WFMath::CoordType newBasePointPosition)
 
68
{
 
69
        int mercatorSegmentsPerOgrePage = mPageIndicesSize / 64;
 
70
        int remainder = static_cast<int>(newBasePointPosition) % mercatorSegmentsPerOgrePage;
 
71
        if (remainder != 0) {
 
72
                ///adjust the position
 
73
                basePointPositionAdjusted = newBasePointPosition + (mercatorSegmentsPerOgrePage - remainder);
 
74
        }
 
75
}
 
76
 
 
77
void TerrainInfo::adjustBasePointPositionDown(WFMath::CoordType& basePointPositionAdjusted, WFMath::CoordType newBasePointPosition)
 
78
{
 
79
        int mercatorSegmentsPerOgrePage = mPageIndicesSize / 64;
 
80
        int remainder = std::abs(static_cast<int>(newBasePointPosition) % mercatorSegmentsPerOgrePage);
 
81
        if (remainder != 0) {
 
82
                ///adjust the position
 
83
                basePointPositionAdjusted = newBasePointPosition - (mercatorSegmentsPerOgrePage - remainder);
 
84
        }
 
85
}
 
86
 
 
87
const WFMath::AxisBox<2>& TerrainInfo::getWorldSizeInSegments() const
 
88
{
 
89
        return mCalculatedSegmentSize;
 
90
}
 
91
 
 
92
 
 
93
const WFMath::AxisBox<2>& TerrainInfo::getWorldSizeInIndices() const
 
94
{
 
95
        return mCalculatedSize;
 
96
}
 
97
 
 
98
const WFMath::AxisBox<2>& TerrainInfo::getWorldSizeInPages() const
 
99
{
 
100
        return mCalculatedPageSize;
 
101
}
 
102
 
 
103
 
 
104
double TerrainInfo::getWorldSizeX() const
 
105
{
 
106
        return mCalculatedSize.highCorner().x() - mCalculatedSize.lowCorner().x();
 
107
}
 
108
 
 
109
double TerrainInfo::getWorldSizeY() const
 
110
{
 
111
        return mCalculatedSize.highCorner().y() - mCalculatedSize.lowCorner().y();
 
112
}
 
113
 
 
114
int TerrainInfo::getTotalNumberOfPagesX() const
 
115
{
 
116
        return static_cast<int>(mCalculatedPageSize.highCorner().x() - mCalculatedPageSize.lowCorner().x());
 
117
}
 
118
int TerrainInfo::getTotalNumberOfPagesY() const
 
119
{
 
120
        return static_cast<int>(mCalculatedPageSize.highCorner().y() - mCalculatedPageSize.lowCorner().y());
 
121
}
 
122
 
 
123
int TerrainInfo::getPageOffsetX() const
 
124
{
 
125
        return static_cast<int>(-mCalculatedPageSize.lowCorner().x());
 
126
}
 
127
int TerrainInfo::getPageOffsetY() const
 
128
{
 
129
        return static_cast<int>(-mCalculatedPageSize.lowCorner().y());
 
130
}
 
131
 
 
132
void TerrainInfo::setPageIndicesSize(int size)
 
133
{
 
134
        mPageIndicesSize = size;
 
135
}
 
136
 
 
137
}
 
138
}