2
-----------------------------------------------------------------------------
3
MathConverter.h by Miguel Guzman Miranda (Aglanor)
4
Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2005
6
Copyright � 2003 The Worldforge Project (http://www.worldforge.org)
7
This file is part of Ember client (http://www.worldforge.org/dev/eng/clients/Ember)
9
This code is distributed under the GNU GPL (General Public License).
10
See file COPYING for details.
12
-----------------------------------------------------------------------------
14
-----------------------------------------------------------------------------
15
Filename: MathConverter.h
16
Description: Point, Vector and Quaternion converter
17
for world-centric coordinates (Atlas-wfmath like)
18
to/from screen-centric coordinates (Ogre like).
20
Atlas is World-centric (picture a map):
25
Ogre is screen-centric (picture your monitor screen):
28
z is depth (negative towards the screen,
29
positive is towards you,
30
so the coord system is also dextrogyrous)
32
--------------------------------------------------------
33
Example of Atlas --> Ogre conversion
35
Picture this is the world map, in both cases:
49
--------------------------------------------------------
50
Example of Ogre --> Atlas conversion
52
Picture this is your computer screen, in both cases:
63
(o.z)---> (-a.y)--->http://mandriva.com/en/community/start
75
-----------------------------------------------------------------------------
78
#ifndef __MATH_CONVERTER_H__
79
#define __MATH_CONVERTER_H__
81
// ------------------------------
82
// Include WFmath header files
83
// ------------------------------
84
#include <wfmath/point.h>
85
#include <wfmath/vector.h>
86
#include <wfmath/axisbox.h>
87
#include <wfmath/quaternion.h>
88
#include <wfmath/const.h>
93
* @brief Converts a 2d WFMath::Point<2> into a 3d Ogre vector.
94
* The vertical position of the resulting vector will be zero.
95
* @param p A 2d Worldforge position.
96
* @return A 3d Ogre vector.
98
Ogre::Vector3 Atlas2Ogre(const WFMath::Point<2>& p);
101
* @brief Converts a 2d WFMath::Point<2> into a 2d Ogre vector.
102
* @param p A 2d Worldforge position.
103
* @return A 2d Ogre vector.
105
Ogre::Vector2 Atlas2Ogre_Vector2(const WFMath::Point<2>& p);
108
* @brief Converts a 2d vector into a 2d Ogre vector.
109
* @param p A 2d Worldforge vector.
110
* @return A 2d Ogre vector.
112
Ogre::Vector2 Atlas2Ogre(const WFMath::Vector<2>& p);
115
* @brief Converts an Ogre 3d vector into a Worldforge 3d point.
116
* @param p A 3d Ogre vector.
117
* @return A 3d Worldforge point.
119
WFMath::Point<3> Ogre2Atlas(const Ogre::Vector3& p);
122
* @brief Converts an Ogre 2d vector into a Worldforge 2d point.
123
* @param p A 2d Ogre vector.
124
* @return A 2d Worldforge point.
126
WFMath::Point<2> Ogre2Atlas(const Ogre::Vector2& p);
129
* @brief Converts an Ogre 3d vector into a Worldforge 2d point.
130
* @param p A 3d Ogre vector.
131
* @return A 2d Worldforge point.
133
WFMath::Point<2> Ogre2Atlas_TerrainPosition(const Ogre::Vector3& p);
136
* @brief Converts an Ogre 3d vector into a Worldforge 3d vector.
137
* @param p A 3d Ogre vector.
138
* @return A 3d Worldforge vector.
140
WFMath::Vector<3> Ogre2Atlas_Vector3(const Ogre::Vector3& p);
143
* @brief Converts a Worldforge 3d point into an Ogre 3d vector.
144
* @param p A 3d Worldforge point.
145
* @return A 3d Ogre vector.
147
Ogre::Vector3 Atlas2Ogre(const WFMath::Point<3>& p);
150
* @brief Converts a Worldforge 3d vector into an Ogre 3d vector.
151
* @param v A 3d Worldforge vector.
152
* @return A 3d Ogre vector.
154
Ogre::Vector3 Atlas2Ogre(const WFMath::Vector<3>& v);
157
* @brief Converts a Worldforge quaternion into an Ogre quaternion.
158
* @param aq A Worldforge quaternion.
159
* @return An Ogre quaternion.
161
Ogre::Quaternion Atlas2Ogre(const WFMath::Quaternion& aq);
164
* @brief Converts an Ogre quaternion into a Worldforge quaternion.
165
* @param aq An Ogre quaternion.
166
* @return A Worldforge quaternion.
168
WFMath::Quaternion Ogre2Atlas(const Ogre::Quaternion& aq);
171
* @brief Converts a Worldforge 3d axisbox into an Ogre 3d axisbox.
172
* @param axisBox A Worldforge 3d axisbox.
173
* @return An Ogre 3d axisbox.
175
Ogre::AxisAlignedBox Atlas2Ogre(const WFMath::AxisBox<3>& axisBox);
178
* @brief Converts a Worldforge 2d axisbox into an Ogre TRect.
179
* @param atlasBox A Worldforge 2d axisbox.
180
* @return An Ogre TRect.
182
Ogre::TRect<Ogre::Real> Atlas2Ogre(const WFMath::AxisBox<2>& atlasBox);
186
* @brief Converts an Ogre 3d axisbox into a Worldforge 3d axisbox.
187
* @param axisBox An Ogre 3d axisbox.
188
* @return A Worldforge 3d axisbox.
190
WFMath::AxisBox<3> Ogre2Atlas(const Ogre::AxisAlignedBox& axisBox);
193
* @brief Converts an Ogre TRect into a Worldforge 2d axisbox.
194
* @param bounds An Ogre TRect instance.
195
* @return A Worldforge 2d axisbox.
197
WFMath::AxisBox<2> Ogre2Atlas(const Ogre::TRect<Ogre::Real>& bounds);
202
inline Ogre::Vector3 Atlas2Ogre(const WFMath::Point<2>& p) {
203
return Ogre::Vector3(p.x(),0,-p.y());
206
inline Ogre::Vector2 Atlas2Ogre(const WFMath::Vector<2>& p) {
207
return Ogre::Vector2(p.x(),-p.y());
210
inline Ogre::Vector2 Atlas2Ogre_Vector2(const WFMath::Point<2>& p) {
211
return Ogre::Vector2(p.x(),-p.y());
214
inline WFMath::Point<3> Ogre2Atlas(const Ogre::Vector3& p) {
215
return WFMath::Point<3>(p.x,-p.z,p.y);
218
inline WFMath::Point<2> Ogre2Atlas(const Ogre::Vector2& p) {
219
return WFMath::Point<2>(p.x,-p.y);
222
inline WFMath::Point<2> Ogre2Atlas_TerrainPosition(const Ogre::Vector3& p) {
223
return WFMath::Point<2>(p.x,-p.z);
226
inline WFMath::Vector<3> Ogre2Atlas_Vector3(const Ogre::Vector3& p) {
227
return WFMath::Vector<3>(p.x,-p.z,p.y);
230
inline Ogre::Vector3 Atlas2Ogre(const WFMath::Point<3>& p){
231
return Ogre::Vector3(p.x(),p.z(),-p.y());
234
inline Ogre::Vector3 Atlas2Ogre(const WFMath::Vector<3>& v){
235
return Ogre::Vector3(v.x(),v.z(),-v.y());
238
inline Ogre::Quaternion Atlas2Ogre(const WFMath::Quaternion& aq){
240
return Ogre::Quaternion::IDENTITY;
242
return Ogre::Quaternion(aq.scalar(),aq.vector().x(),aq.vector().z(),-aq.vector().y());
245
inline WFMath::Quaternion Ogre2Atlas(const Ogre::Quaternion& aq){
246
return WFMath::Quaternion(aq.w,aq.x,-aq.z,aq.y);
249
inline Ogre::AxisAlignedBox Atlas2Ogre(const WFMath::AxisBox<3>& axisBox){
250
if (!axisBox.isValid()) {
251
return Ogre::AxisAlignedBox();
253
return Ogre::AxisAlignedBox(axisBox.lowCorner().x(), axisBox.lowCorner().z(), -axisBox.highCorner().y(), axisBox.highCorner().x(), axisBox.highCorner().z(), -axisBox.lowCorner().y());
256
inline Ogre::TRect<Ogre::Real> Atlas2Ogre(const WFMath::AxisBox<2>& atlasBox) {
257
if (!atlasBox.isValid()) {
258
return Ogre::TRect<Ogre::Real>();
260
return Ogre::TRect<Ogre::Real>(atlasBox.lowCorner().x(), -atlasBox.highCorner().y(), atlasBox.highCorner().x(), -atlasBox.lowCorner().y());
263
inline WFMath::AxisBox<3> Ogre2Atlas(const Ogre::AxisAlignedBox& axisBox){
264
if (axisBox.isNull() || axisBox.isInfinite()) {
265
return WFMath::AxisBox<3>();
267
return WFMath::AxisBox<3>(WFMath::Point<3>(axisBox.getMinimum().x, -axisBox.getMaximum().z, axisBox.getMinimum().y), WFMath::Point<3>(axisBox.getMaximum().x, -axisBox.getMinimum().z, axisBox.getMaximum().y));
270
inline WFMath::AxisBox<2> Ogre2Atlas(const Ogre::TRect<Ogre::Real>& bounds){
271
return WFMath::AxisBox<2>(WFMath::Point<2>(bounds.left, -bounds.top), WFMath::Point<2>(bounds.right, -bounds.bottom));