Skip to content

Commit

Permalink
Units: refactor Units to be a namespace, and the implementation class…
Browse files Browse the repository at this point in the history
… to be UnitsType, so we can define the stock units objects as const header objects instead of statics. This allows us to use qualified_doubles in static initialization safely.
  • Loading branch information
gwaldron committed Mar 20, 2024
1 parent f0868a3 commit 176a558
Show file tree
Hide file tree
Showing 45 changed files with 684 additions and 677 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set(OSGEARTH_PATCH_VERSION 0)
set(OSGEARTH_VERSION ${OSGEARTH_MAJOR_VERSION}.${OSGEARTH_MINOR_VERSION}.${OSGEARTH_PATCH_VERSION})

# Increment this each time the ABI changes
set(OSGEARTH_SOVERSION 150)
set(OSGEARTH_SOVERSION 151)

# Require C++11
set_property(GLOBAL PROPERTY CXX_STANDARD 11)
Expand Down
2 changes: 1 addition & 1 deletion src/applications/osgearth_3pv/osgearth_3pv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ main( int argc, char** argv )
{
osgEarth::initialize();

static char debugEnv[] = "OSGEARTH_REX_DEBUG=1";
const char debugEnv[] = "OSGEARTH_REX_DEBUG=1";
putenv(debugEnv);

osg::ArgumentParser arguments( &argc, argv );
Expand Down
6 changes: 3 additions & 3 deletions src/applications/osgearth_tfs/osgearth_tfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ int main(int argc, char** argv)
if (!grid.empty())
{
float gridSize;
Units units;
if ( Units::parse(grid, gridSize, units, Units::METERS) ) {
gridSizeMeters = Distance(gridSize, units).as(Units::METERS);
UnitsType units;
if (Units::parse(grid, gridSize, units, Units::METERS)) {
gridSizeMeters = Distance(gridSize, units).as(Units::METERS);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/osgEarth/AnnotationLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#include <osgEarth/AnnotationLayer>
#include <osgEarth/AnnotationRegistry>
#include <osgEarth/Registry>

using namespace osgEarth;

Expand Down Expand Up @@ -76,7 +77,7 @@ AnnotationLayer::deserialize()

// deserialize from the options:
osg::Group* group = 0L;
AnnotationRegistry::instance()->create(0L, options().getConfig(), getReadOptions(), group);
AnnotationRegistry::instance()->create(nullptr, options().getConfig(), getReadOptions(), group);
if (group)
{
_root->addChild(group);
Expand Down
3 changes: 1 addition & 2 deletions src/osgEarth/AnnotationNode
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace osgEarth

virtual void setStyle(const Style& style) { }

virtual const Style& getStyle() const { return s_emptyStyle; }
virtual const Style& getStyle() const;

protected:

Expand Down Expand Up @@ -158,7 +158,6 @@ namespace osgEarth
private:

osg::observer_ptr<MapNode> _mapNode;
static Style s_emptyStyle;
bool _mapNodeRequired;
osg::NodeCallback* _altCallback;

Expand Down
11 changes: 7 additions & 4 deletions src/osgEarth/AnnotationNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ using namespace osgEarth::Util;

//-------------------------------------------------------------------

Style AnnotationNode::s_emptyStyle;

//-------------------------------------------------------------------

AnnotationNode::AnnotationNode()
{
construct();
Expand Down Expand Up @@ -90,6 +86,13 @@ AnnotationNode::~AnnotationNode()
setMapNode(NULL);
}

const Style&
AnnotationNode::getStyle() const
{
static Style s_emptyStyle;
return s_emptyStyle;
}

void
AnnotationNode::traverse(osg::NodeVisitor& nv)
{
Expand Down
7 changes: 4 additions & 3 deletions src/osgEarth/AnnotationRegistry
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ namespace osgEarth { namespace Util
/**
* Singleton registry for annotation node types.
*/
class OSGEARTH_EXPORT AnnotationRegistry
class OSGEARTH_EXPORT AnnotationRegistry : public osg::Referenced
{
public:
AnnotationRegistry() { }

/**
* Access the singleton instance of this class.
*/
Expand Down Expand Up @@ -66,7 +68,6 @@ namespace osgEarth { namespace Util
virtual ~AnnotationRegistry() { }

private:
AnnotationRegistry() { }
typedef std::unordered_map<std::string, AnnotationFactory*> FactoryMap;
FactoryMap _factories;

Expand Down Expand Up @@ -102,7 +103,7 @@ namespace osgEarth { namespace Util
// internal: proxy class used by the registraion macro
template<typename T>
struct AnnotationRegistrationProxy : public AnnotationFactory {
AnnotationRegistrationProxy(const std::string& key) { AnnotationRegistry::instance()->add(key, this); }
AnnotationRegistrationProxy(const std::string& key) { osgEarth::Util::AnnotationRegistry::instance()->add(key, this); }
AnnotationNode* create(const Config& conf, const osgDB::Options* options) const { return new T(conf, options); }
};

Expand Down
18 changes: 4 additions & 14 deletions src/osgEarth/AnnotationRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,10 @@ namespace
AnnotationRegistry*
AnnotationRegistry::instance()
{
// OK to be in the local scope since this gets called at static init time
// by the OSGEARTH_REGISTER_ANNOTATION macro
static AnnotationRegistry* s_singleton = nullptr;
static std::mutex s_singletonMutex;

if ( !s_singleton )
{
std::lock_guard<std::mutex> lock(s_singletonMutex);
if ( !s_singleton )
{
s_singleton = new AnnotationRegistry();
}
}
return s_singleton;
return Registry::instance()->getOrCreate<AnnotationRegistry>(
"oe.AnnotationRegistry", []() {
return new AnnotationRegistry();
});
}


Expand Down
2 changes: 1 addition & 1 deletion src/osgEarth/AutoClipPlaneHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ AutoClipPlaneCullCallback::operator()( osg::Node* node, osg::NodeVisitor* nv )
}
else
{
static osg::EllipsoidModel em;
osg::EllipsoidModel em;
osg::Vec3d t;
em.convertXYZToLatLongHeight( eye.x(), eye.y(), eye.z(), loc.y(), loc.x(), loc.z() );
}
Expand Down
6 changes: 2 additions & 4 deletions src/osgEarth/BufferFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ bool
BufferFilter::isSupported()
{
#ifdef OSGEARTH_HAVE_GEOS
static bool s_isSupported = true;
return true;
#else
static bool s_isSupported = false;
return false;
#endif

return s_isSupported;
}

#define ASSERT_SUPPORT() \
Expand Down
26 changes: 12 additions & 14 deletions src/osgEarth/Chonk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "Utils"
#include "NodeUtils"
#include "DrawInstanced"
#include "Registry"

#include <osg/Switch>
#include <osg/LOD>
Expand Down Expand Up @@ -798,9 +799,6 @@ ChonkDrawable::installRenderBin(ChonkDrawable* d)
// map of render bin number to stateset
static vector_map<int, osg::ref_ptr<osg::StateSet>> s_stateSets;

// globally shared VP
static osg::ref_ptr<VirtualProgram> s_vp;

static Mutex s_mutex;
std::lock_guard<std::mutex> lock(s_mutex);

Expand All @@ -813,16 +811,17 @@ ChonkDrawable::installRenderBin(ChonkDrawable* d)
(osg::StateSet::RenderBinMode)(ss->USE_RENDERBIN_DETAILS | ss->PROTECTED_RENDERBIN_DETAILS));

// create the (shared) shader program if necessary:
if (!s_vp.valid())
{
s_vp = new VirtualProgram();
s_vp->setInheritShaders(true);
s_vp->setName("ChonkDrawable");
Shaders pkg;
pkg.load(s_vp.get(), pkg.Chonk);
}

ss->setAttribute(s_vp);
auto vp = Registry::instance()->getOrCreate<VirtualProgram>("vp.ChonkDrawable", []()
{
auto vp = new VirtualProgram();
vp->setInheritShaders(true);
vp->setName("ChonkDrawable");
Shaders pkg;
pkg.load(vp, pkg.Chonk);
return vp;
});

ss->setAttribute(vp);
}

d->setStateSet(ss);
Expand Down Expand Up @@ -1370,7 +1369,6 @@ ChonkRenderBin::ChonkRenderBin(const ChonkRenderBin& rhs, const osg::CopyOp& op)
osgUtil::RenderBin(rhs, op),
_cullSS(rhs._cullSS)
{
// The first time this happens, create the shaders and statesets.
if (!_cullSS.valid())
{
static Mutex m;
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarth/Cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ SpatialReference(key)
// Custom units. The big number there roughly converts [0..1] to meters
// on a spheroid with WGS84-ish radius. Not perfect but close enough for
// the purposes of this class
_units = Units("Cube face", "cube", Units::TYPE_LINEAR, 42949672.96/4.0);
_units = UnitsType("Cube face", "cube", Units::Domain::DISTANCE, 42949672.96/4.0);
}

CubeSpatialReference::~CubeSpatialReference()
Expand Down
6 changes: 3 additions & 3 deletions src/osgEarth/ElevationPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ ElevationPool::prepareEnvelope(
env._pxmin = env._profile->getExtent().xMin();
env._pymin = env._profile->getExtent().yMin();

const Units& units = env._map->getSRS()->getUnits();
auto& units = env._map->getSRS()->getUnits();
Distance pointRes(0.0, units);

GeoPoint refPointMap = refPoint.transform(env._map->getSRS());
Expand Down Expand Up @@ -637,7 +637,7 @@ ElevationPool::sampleMapCoords(

int lod;
int lod_prev = INT_MAX;
const Units& units = map->getSRS()->getUnits();
auto& units = map->getSRS()->getUnits();
Distance pointRes(0.0, units);

for(auto iter = begin; iter != end; ++iter)
Expand Down Expand Up @@ -778,7 +778,7 @@ ElevationPool::sampleMapCoords(

int lod;
int lod_prev = INT_MAX;
const Units& units = map->getSRS()->getUnits();
auto& units = map->getSRS()->getUnits();

for(auto iter = begin; iter != end; ++iter)
{
Expand Down
10 changes: 5 additions & 5 deletions src/osgEarth/FeatureRasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ FeatureRasterizer::render_blend2d(
double lineWidthValue = masterLine->stroke()->width().value();

// if the width units are specified, convert to pixels
const optional<Units> widthUnits = masterLine->stroke()->widthUnits();
const optional<UnitsType> widthUnits = masterLine->stroke()->widthUnits();

if (widthUnits.isSet() && widthUnits != Units::PIXELS)
{
Expand Down Expand Up @@ -1030,8 +1030,8 @@ FeatureRasterizer::render_agglite(
if (globalLineSymbol->stroke()->widthUnits().isSet() &&
globalLineSymbol->stroke()->widthUnits().get() != Units::PIXELS)
{
const Units& featureUnits = featureSRS->getUnits();
const Units& strokeUnits = globalLineSymbol->stroke()->widthUnits().value();
auto& featureUnits = featureSRS->getUnits();
auto& strokeUnits = globalLineSymbol->stroke()->widthUnits().value();

// if the units are different than those of the feature data, we need to
// do a units conversion.
Expand Down Expand Up @@ -1185,8 +1185,8 @@ FeatureRasterizer::render_agglite(
if (globalLineSymbol->stroke()->widthUnits().isSet() &&
globalLineSymbol->stroke()->widthUnits().get() != Units::PIXELS)
{
const Units& featureUnits = featureSRS->getUnits();
const Units& strokeUnits = globalLineSymbol->stroke()->widthUnits().value();
auto& featureUnits = featureSRS->getUnits();
auto& strokeUnits = globalLineSymbol->stroke()->widthUnits().value();

// if the units are different than those of the feature data, we need to
// do a units conversion.
Expand Down
3 changes: 1 addition & 2 deletions src/osgEarth/GLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ GLUtils::useNVGL(bool value)
namespace
{
struct Mapping {
Mapping() : _ptr(nullptr) { }
const osg::State* _ptr;
const osg::State* _ptr = nullptr;
};
static Mapping s_mappings[4096];
}
Expand Down
8 changes: 4 additions & 4 deletions src/osgEarth/GeoData
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ namespace osgEarth
bool transformZ(const AltitudeMode& altMode, const TerrainResolver* t, double& out_z) const;

//! Transforms a resolution distance to a cartesian value.
Distance transformResolution(const Distance& d, const Units& outUnits) const;
Distance transformResolution(const Distance& d, const UnitsType& outUnits) const;

/**
* Transforms this geopoint's Z to be absolute w.r.t. the vertical datum
Expand Down Expand Up @@ -272,7 +272,7 @@ namespace osgEarth
GeoPoint interpolate(const GeoPoint& rhs, double t) const;

//! Convenience function to return xy units
const Units& getXYUnits() const;
const UnitsType& getXYUnits() const;

bool operator == (const GeoPoint& rhs) const;
bool operator != (const GeoPoint& rhs) const { return !operator==(rhs); }
Expand Down Expand Up @@ -409,13 +409,13 @@ namespace osgEarth
inline double width() const { return _width; }

//! East-to-est span in specified units
double width(const Units& units) const;
double width(const UnitsType& units) const;

//! North-to-south span of the extent
inline double height() const { return _height; }

//! North-to-south span in specified units
double height(const Units& units) const;
double height(const UnitsType& units) const;

//! Gets the centroid of the bounds
GeoPoint getCentroid() const;
Expand Down
8 changes: 4 additions & 4 deletions src/osgEarth/GeoData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ GeoPoint::set(const SpatialReference* srs,
_altMode = altMode;
}

const Units&
const UnitsType&
GeoPoint::getXYUnits() const
{
return getSRS() ? getSRS()->getUnits() : Units::DEGREES;
Expand Down Expand Up @@ -328,7 +328,7 @@ GeoPoint::transformZ(const AltitudeMode& altMode, const TerrainResolver* terrain
}

Distance
GeoPoint::transformResolution(const Distance& resolution, const Units& outUnits) const
GeoPoint::transformResolution(const Distance& resolution, const UnitsType& outUnits) const
{
if (!isValid())
return resolution;
Expand Down Expand Up @@ -897,7 +897,7 @@ GeoExtent::getCentroid(double& out_x, double& out_y) const
}

double
GeoExtent::width(const Units& units) const
GeoExtent::width(const UnitsType& units) const
{
if (!isValid())
{
Expand All @@ -918,7 +918,7 @@ GeoExtent::width(const Units& units) const
}

double
GeoExtent::height(const Units& units) const
GeoExtent::height(const UnitsType& units) const
{
if (!isValid())
return 0.0;
Expand Down
6 changes: 3 additions & 3 deletions src/osgEarth/Geoid
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ namespace osgEarth
const RasterInterpolation& interp =INTERP_BILINEAR) const;

/** The linear units in which height values are expressed. */
const Units& getUnits() const { return _units; }
void setUnits( const Units& value );
const UnitsType& getUnits() const { return _units; }
void setUnits( const UnitsType& value );

/** Whether this is a valid object to use */
bool isValid() const { return _valid; }
Expand All @@ -77,7 +77,7 @@ namespace osgEarth

private:
std::string _name;
Units _units;
UnitsType _units;
bool _valid;
Bounds _bounds;

Expand Down

0 comments on commit 176a558

Please sign in to comment.