/*****************************************************************************
 * $LastChangedDate: 2009-11-22 22:39:11 -0500 (Sun, 22 Nov 2009) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Simulation conversion functions.
 * @remarks physics/conversion.hh converts between different physical units.
 *          world/conversion.hh converts between graphical coordinates.
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define WORLD_CONVERSION_CC 1
#define NEED_PLANET_CLASS 1
#include "base/module.hh"
using namespace base;
#include "object/module.hh"
using namespace object;
#include "world/module.hh"
using namespace world;
#include "physics/module.hh"
#include "physics/types.hh"
#include "physics/physics_control.hh"
using namespace physics;
#include "world/module.hh"
#include "world/conversion.hh"
#include "world/conf.hh"
#include "world/planet.hh"

namespace world {
namespace conv {

/*****************************************************************************
 * Convert distance from graphical coordinates to meter.
 *****************************************************************************/
Meter
Sim2Meters( const fp simDistance )
{
ASSERT( GET_PLANET().MeterSimRatio() > 0 );

    return Meter(simDistance * GET_PLANET().MeterSimRatio());
}

/*****************************************************************************
 * Convert meters to a distance in graphical coordinates.
 *****************************************************************************/
fp
Meters2Sim( const Meter meter )
{
ASSERT( GET_PLANET().MeterSimRatio() > 0 );

    // M / (M/sim) = M/1 * (sim/M) = sim/1
    return meter.FP() / GET_PLANET().MeterSimRatio();
}

/*****************************************************************************
 * Convert graphical coordinates to geospatial coordinates.
 *****************************************************************************/
GeoVertex
WorldVertex2GeoVertex( const WorldVertex& wv )
{
    return GeoVertex( Sim2Meters(wv[XX]),
                      Sim2Meters(wv[YY]),
                      Sim2Meters(wv[ZZ]) );
}

/*****************************************************************************
 * Convert geospatial coordinates to graphical coordinates.
 *****************************************************************************/
WorldVertex
GeoVertex2WorldVertex( const GeoVertex& gv )
{
    return WorldVertex( Meters2Sim( Meter(gv[XX]) ),
                        Meters2Sim( Meter(gv[YY]) ),
                        Meters2Sim( Meter(gv[ZZ]) ) );
}

/*****************************************************************************
 * Convert graphical coordinates to spherical coordinates.
 *****************************************************************************/
SphereVertex
WorldVertex2SphereVertex( const WorldVertex& wv )
{
    return GET_PLANET().WorldVertex2SphereVertex( wv );
}

/*****************************************************************************
 * Convert spherical coordinates to a 3D vertex.
 *****************************************************************************/
WorldVertex
SphereVertex2WorldVertex( const SphereVertex& sv )
{
    return GET_PLANET().SphereVertex2WorldVertex( sv );
}

} // namespace conv
} // namespace world
