2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2004-04-30 06:16:50 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2004-04-30 06:16:50 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2004-04-30 06:16:50 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2004-04-30 06:16:50 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSG_COORDINATESYSTEMNODE
|
|
|
|
#define OSG_COORDINATESYSTEMNODE 1
|
|
|
|
|
|
|
|
#include <osg/Group>
|
|
|
|
#include <osg/Matrixd>
|
|
|
|
|
|
|
|
namespace osg
|
|
|
|
{
|
|
|
|
|
|
|
|
const double WGS_84_RADIUS_EQUATOR = 6378137.0;
|
|
|
|
const double WGS_84_RADIUS_POLAR = 6356752.3142;
|
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** EllipsoidModel encapsulates the ellipsoid used to model astronomical bodies,
|
2012-03-22 01:36:20 +08:00
|
|
|
* such as sun, planets, moon etc.
|
2008-04-14 18:38:37 +08:00
|
|
|
* All distance quantities (i.e. heights + radius) are in meters,
|
|
|
|
* and latitude and longitude are in radians.*/
|
2004-04-30 06:16:50 +08:00
|
|
|
class EllipsoidModel : public Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2008-01-10 05:17:10 +08:00
|
|
|
/** WGS_84 is a common representation of the earth's spheroid */
|
2004-04-30 06:16:50 +08:00
|
|
|
EllipsoidModel(double radiusEquator = WGS_84_RADIUS_EQUATOR,
|
2004-08-31 22:49:33 +08:00
|
|
|
double radiusPolar = WGS_84_RADIUS_POLAR):
|
2004-04-30 06:16:50 +08:00
|
|
|
_radiusEquator(radiusEquator),
|
|
|
|
_radiusPolar(radiusPolar) { computeCoefficients(); }
|
|
|
|
|
|
|
|
EllipsoidModel(const EllipsoidModel& et,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
|
|
|
|
Object(et,copyop),
|
|
|
|
_radiusEquator(et._radiusEquator),
|
|
|
|
_radiusPolar(et._radiusPolar) { computeCoefficients(); }
|
|
|
|
|
|
|
|
META_Object(osg,EllipsoidModel);
|
|
|
|
|
|
|
|
void setRadiusEquator(double radius) { _radiusEquator = radius; computeCoefficients(); }
|
|
|
|
double getRadiusEquator() const { return _radiusEquator; }
|
|
|
|
|
|
|
|
void setRadiusPolar(double radius) { _radiusPolar = radius; computeCoefficients(); }
|
|
|
|
double getRadiusPolar() const { return _radiusPolar; }
|
|
|
|
|
|
|
|
inline void convertLatLongHeightToXYZ(double latitude, double longitude, double height,
|
|
|
|
double& X, double& Y, double& Z) const;
|
|
|
|
|
|
|
|
inline void convertXYZToLatLongHeight(double X, double Y, double Z,
|
|
|
|
double& latitude, double& longitude, double& height) const;
|
|
|
|
|
|
|
|
inline void computeLocalToWorldTransformFromLatLongHeight(double latitude, double longitude, double height, osg::Matrixd& localToWorld) const;
|
|
|
|
|
|
|
|
inline void computeLocalToWorldTransformFromXYZ(double X, double Y, double Z, osg::Matrixd& localToWorld) const;
|
|
|
|
|
2009-08-20 22:19:10 +08:00
|
|
|
inline void computeCoordinateFrame(double latitude, double longitude, osg::Matrixd& localToWorld) const;
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
inline osg::Vec3d computeLocalUpVector(double X, double Y, double Z) const;
|
2004-04-30 06:16:50 +08:00
|
|
|
|
2009-07-24 22:25:34 +08:00
|
|
|
// Convenience method for determining if EllipsoidModel is a stock WGS84 ellipsoid
|
|
|
|
inline bool isWGS84() const {return(_radiusEquator == WGS_84_RADIUS_EQUATOR && _radiusPolar == WGS_84_RADIUS_POLAR);}
|
|
|
|
|
|
|
|
// Compares two EllipsoidModel by comparing critical internal values.
|
|
|
|
// Ignores _eccentricitySquared since it's just a cached value derived from
|
|
|
|
// the _radiusEquator and _radiusPolar members.
|
|
|
|
friend bool operator == ( const EllipsoidModel & e1, const EllipsoidModel& e2) {return(e1._radiusEquator == e2._radiusEquator && e1._radiusPolar == e2._radiusPolar);}
|
|
|
|
|
|
|
|
|
2004-04-30 06:16:50 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
void computeCoefficients()
|
|
|
|
{
|
|
|
|
double flattening = (_radiusEquator-_radiusPolar)/_radiusEquator;
|
|
|
|
_eccentricitySquared = 2*flattening - flattening*flattening;
|
|
|
|
}
|
|
|
|
|
|
|
|
double _radiusEquator;
|
|
|
|
double _radiusPolar;
|
|
|
|
double _eccentricitySquared;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** CoordinateFrame encapsulates the orientation of east, north and up.*/
|
2004-04-30 06:16:50 +08:00
|
|
|
typedef Matrixd CoordinateFrame;
|
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** CoordinateSystem encapsulate the coordinate system that is associated with objects in a scene.
|
2004-04-30 06:16:50 +08:00
|
|
|
For an overview of common earth bases coordinate systems see http://www.colorado.edu/geography/gcraft/notes/coordsys/coordsys_f.html */
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT CoordinateSystemNode : public Group
|
2004-04-30 06:16:50 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CoordinateSystemNode();
|
|
|
|
|
2004-06-09 23:00:03 +08:00
|
|
|
CoordinateSystemNode(const std::string& format, const std::string& cs);
|
2004-04-30 06:16:50 +08:00
|
|
|
|
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
|
|
|
CoordinateSystemNode(const CoordinateSystemNode&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-05-03 20:04:25 +08:00
|
|
|
META_Node(osg,CoordinateSystemNode);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2009-07-24 22:25:34 +08:00
|
|
|
/** Set the coordinate system node up by copying the format, coordinate system string, and ellipsoid model of another coordinate system node.*/
|
2004-06-09 23:11:27 +08:00
|
|
|
void set(const CoordinateSystemNode& csn);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-06-09 23:00:03 +08:00
|
|
|
/** Set the coordinate system format string. Typical values would be WKT, PROJ4, USGS etc.*/
|
|
|
|
void setFormat(const std::string& format) { _format = format; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-06-09 23:00:03 +08:00
|
|
|
/** Get the coordinate system format string.*/
|
|
|
|
const std::string& getFormat() const { return _format; }
|
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** Set the CoordinateSystem reference string, should be stored in a form consistent with the Format.*/
|
2004-06-09 23:00:03 +08:00
|
|
|
void setCoordinateSystem(const std::string& cs) { _cs = cs; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-04-30 06:16:50 +08:00
|
|
|
/** Get the CoordinateSystem reference string.*/
|
2004-06-09 23:00:03 +08:00
|
|
|
const std::string& getCoordinateSystem() const { return _cs; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** Set EllipsoidModel to describe the model used to map lat, long and height into geocentric XYZ and back. */
|
2004-04-30 06:16:50 +08:00
|
|
|
void setEllipsoidModel(EllipsoidModel* ellipsode) { _ellipsoidModel = ellipsode; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** Get the EllipsoidModel.*/
|
2004-04-30 06:16:50 +08:00
|
|
|
EllipsoidModel* getEllipsoidModel() { return _ellipsoidModel.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** Get the const EllipsoidModel.*/
|
2004-04-30 06:16:50 +08:00
|
|
|
const EllipsoidModel* getEllipsoidModel() const { return _ellipsoidModel.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-08-31 22:49:33 +08:00
|
|
|
/** Compute the local coordinate frame for specified point.*/
|
2004-05-20 18:15:48 +08:00
|
|
|
CoordinateFrame computeLocalCoordinateFrame(const Vec3d& position) const;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-07-24 22:25:34 +08:00
|
|
|
/** Compute the local up-vector for specified point.*/
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d computeLocalUpVector(const Vec3d& position) const;
|
2004-04-30 06:16:50 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~CoordinateSystemNode() {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-06-09 23:00:03 +08:00
|
|
|
std::string _format;
|
|
|
|
std::string _cs;
|
2004-04-30 06:16:50 +08:00
|
|
|
ref_ptr<EllipsoidModel> _ellipsoidModel;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement inline methods.
|
|
|
|
//
|
|
|
|
inline void EllipsoidModel::convertLatLongHeightToXYZ(double latitude, double longitude, double height,
|
|
|
|
double& X, double& Y, double& Z) const
|
|
|
|
{
|
|
|
|
// for details on maths see http://www.colorado.edu/geography/gcraft/notes/datum/gif/llhxyz.gif
|
|
|
|
double sin_latitude = sin(latitude);
|
|
|
|
double cos_latitude = cos(latitude);
|
|
|
|
double N = _radiusEquator / sqrt( 1.0 - _eccentricitySquared*sin_latitude*sin_latitude);
|
|
|
|
X = (N+height)*cos_latitude*cos(longitude);
|
|
|
|
Y = (N+height)*cos_latitude*sin(longitude);
|
2004-08-04 16:27:43 +08:00
|
|
|
Z = (N*(1-_eccentricitySquared)+height)*sin_latitude;
|
2004-04-30 06:16:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void EllipsoidModel::convertXYZToLatLongHeight(double X, double Y, double Z,
|
|
|
|
double& latitude, double& longitude, double& height) const
|
|
|
|
{
|
2018-04-04 15:50:39 +08:00
|
|
|
// handle polar and center-of-earth cases directly.
|
|
|
|
if (X != 0.0)
|
|
|
|
longitude = atan2(Y,X);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Y > 0.0)
|
|
|
|
longitude = PI_2;
|
|
|
|
else if (Y < 0.0)
|
|
|
|
longitude = -PI_2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// at pole or at center of the earth
|
|
|
|
longitude = 0.0;
|
|
|
|
if (Z > 0.0)
|
|
|
|
{ // north pole.
|
|
|
|
latitude = PI_2;
|
|
|
|
height = Z - _radiusPolar;
|
|
|
|
}
|
|
|
|
else if (Z < 0.0)
|
|
|
|
{ // south pole.
|
|
|
|
latitude = -PI_2;
|
|
|
|
height = -Z - _radiusPolar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // center of earth.
|
|
|
|
latitude = PI_2;
|
|
|
|
height = -_radiusPolar;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-30 06:16:50 +08:00
|
|
|
// http://www.colorado.edu/geography/gcraft/notes/datum/gif/xyzllh.gif
|
|
|
|
double p = sqrt(X*X + Y*Y);
|
2007-06-13 21:41:23 +08:00
|
|
|
double theta = atan2(Z*_radiusEquator , (p*_radiusPolar));
|
2004-04-30 06:16:50 +08:00
|
|
|
double eDashSquared = (_radiusEquator*_radiusEquator - _radiusPolar*_radiusPolar)/
|
|
|
|
(_radiusPolar*_radiusPolar);
|
|
|
|
|
|
|
|
double sin_theta = sin(theta);
|
|
|
|
double cos_theta = cos(theta);
|
|
|
|
|
|
|
|
latitude = atan( (Z + eDashSquared*_radiusPolar*sin_theta*sin_theta*sin_theta) /
|
|
|
|
(p - _eccentricitySquared*_radiusEquator*cos_theta*cos_theta*cos_theta) );
|
|
|
|
|
|
|
|
double sin_latitude = sin(latitude);
|
|
|
|
double N = _radiusEquator / sqrt( 1.0 - _eccentricitySquared*sin_latitude*sin_latitude);
|
|
|
|
|
|
|
|
height = p/cos(latitude) - N;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void EllipsoidModel::computeLocalToWorldTransformFromLatLongHeight(double latitude, double longitude, double height, osg::Matrixd& localToWorld) const
|
|
|
|
{
|
|
|
|
double X, Y, Z;
|
|
|
|
convertLatLongHeightToXYZ(latitude,longitude,height,X,Y,Z);
|
2009-08-20 22:19:10 +08:00
|
|
|
|
|
|
|
localToWorld.makeTranslate(X,Y,Z);
|
|
|
|
computeCoordinateFrame(latitude, longitude, localToWorld);
|
2004-04-30 06:16:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void EllipsoidModel::computeLocalToWorldTransformFromXYZ(double X, double Y, double Z, osg::Matrixd& localToWorld) const
|
|
|
|
{
|
2009-08-20 22:19:10 +08:00
|
|
|
double latitude, longitude, height;
|
|
|
|
convertXYZToLatLongHeight(X,Y,Z,latitude,longitude,height);
|
|
|
|
|
2004-04-30 06:16:50 +08:00
|
|
|
localToWorld.makeTranslate(X,Y,Z);
|
2009-08-20 22:19:10 +08:00
|
|
|
computeCoordinateFrame(latitude, longitude, localToWorld);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void EllipsoidModel::computeCoordinateFrame(double latitude, double longitude, osg::Matrixd& localToWorld) const
|
|
|
|
{
|
|
|
|
// Compute up vector
|
|
|
|
osg::Vec3d up ( cos(longitude)*cos(latitude), sin(longitude)*cos(latitude), sin(latitude));
|
|
|
|
|
|
|
|
// Compute east vector
|
|
|
|
osg::Vec3d east (-sin(longitude), cos(longitude), 0);
|
|
|
|
|
|
|
|
// Compute north vector = outer product up x east
|
|
|
|
osg::Vec3d north = up ^ east;
|
|
|
|
|
|
|
|
// set matrix
|
|
|
|
localToWorld(0,0) = east[0];
|
|
|
|
localToWorld(0,1) = east[1];
|
|
|
|
localToWorld(0,2) = east[2];
|
2004-04-30 06:16:50 +08:00
|
|
|
|
2009-08-20 22:19:10 +08:00
|
|
|
localToWorld(1,0) = north[0];
|
|
|
|
localToWorld(1,1) = north[1];
|
|
|
|
localToWorld(1,2) = north[2];
|
2004-05-06 18:54:58 +08:00
|
|
|
|
2009-08-20 22:19:10 +08:00
|
|
|
localToWorld(2,0) = up[0];
|
|
|
|
localToWorld(2,1) = up[1];
|
|
|
|
localToWorld(2,2) = up[2];
|
2004-04-30 06:16:50 +08:00
|
|
|
}
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
inline osg::Vec3d EllipsoidModel::computeLocalUpVector(double X, double Y, double Z) const
|
2004-04-30 06:16:50 +08:00
|
|
|
{
|
2009-08-20 22:19:10 +08:00
|
|
|
// Note latitude is angle between normal to ellipsoid surface and XY-plane
|
|
|
|
double latitude;
|
|
|
|
double longitude;
|
|
|
|
double altitude;
|
|
|
|
convertXYZToLatLongHeight(X,Y,Z,latitude,longitude,altitude);
|
|
|
|
|
|
|
|
// Compute up vector
|
|
|
|
return osg::Vec3d( cos(longitude) * cos(latitude),
|
|
|
|
sin(longitude) * cos(latitude),
|
|
|
|
sin(latitude));
|
2004-04-30 06:16:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|