Skip to content

Commit

Permalink
Merge branch 'master' of github.com:gwaldron/osgearth
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbeverage committed Jan 28, 2025
2 parents b92a9fe + 34b7415 commit 1b2e286
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 209 deletions.
1 change: 1 addition & 0 deletions src/osgEarth/FeatureImageLayer
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace osgEarth
OE_OPTION_LAYER(FeatureSource, featureSource);
OE_OPTION_VECTOR(ConfigOptions, filters);
OE_OPTION_LAYER(StyleSheet, styleSheet);
OE_OPTION(Distance, bufferWidth, {});
OE_OPTION(double, gamma);
OE_OPTION(bool, sdf);
OE_OPTION(bool, sdf_invert);
Expand Down
6 changes: 5 additions & 1 deletion src/osgEarth/FeatureImageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ FeatureImageLayer::Options::getConfig() const
Config conf = ImageLayer::Options::getConfig();
featureSource().set(conf, "features");
styleSheet().set(conf, "styles");
conf.set("buffer_width", bufferWidth());
conf.set("gamma", gamma());
conf.set("sdf", sdf());
conf.set("sdf_invert", sdf_invert());
Expand All @@ -65,6 +66,7 @@ FeatureImageLayer::Options::fromConfig(const Config& conf)

featureSource().get(conf, "features");
styleSheet().get(conf, "styles");
conf.get("buffer_width", bufferWidth());
conf.get("gamma", gamma());
conf.get("sdf", sdf());
conf.get("sdf_invert", sdf_invert());
Expand Down Expand Up @@ -315,9 +317,11 @@ FeatureImageLayer::createImageImplementation(const TileKey& key, ProgressCallbac

FeatureStyleSorter sorter;

// a buffer will pull in data from nearby tiles to mitigate edge artifacts

sorter.sort(
key,
Distance(0, Units::METERS),
options().bufferWidth().value(),
local._session.get(),
local._filterChain,
renderer,
Expand Down
7 changes: 5 additions & 2 deletions src/osgEarth/FeatureRasterizer
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,19 @@ namespace osgEarth

private:
GeoExtent _extent;
GeoExtent _originalExtent;
unsigned _width = 0, _height = 0;
osg::ref_ptr< osg::Image > _image;
osg::ref_ptr< MapboxGLGlyphManager > _glyphManager;
float _pixelScale = 1.0f;
unsigned _bufferPixels = 2;

enum RenderFormat {
RF_BGRA,
RF_ABGR
};
RenderFormat _implPixelFormat;
bool _inverted;
RenderFormat _implPixelFormat = RF_BGRA;
bool _inverted = false;

SymbolBoundingBoxes _symbolBoundingBoxes;

Expand Down
96 changes: 61 additions & 35 deletions src/osgEarth/FeatureRasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ namespace osgEarth {
void rasterizeLines(
const Geometry* geometry,
const Color& color,
float lineWidth_px,
double lineWidth_px,
RenderFrame& frame,
BLContext& ctx)
{
Expand Down Expand Up @@ -284,11 +284,6 @@ namespace osgEarth {
}
});

//BLImage texture;
//texture.readFromFile("../data/icon.png");
//BLPattern pattern(texture);
//ctx.setStrokeStyle(pattern);

ctx.setStrokeStyle(BLRgba(color.r(), color.g(), color.b(), color.a()));
ctx.setStrokeWidth(lineWidth_px);
ctx.strokePath(path);
Expand Down Expand Up @@ -654,15 +649,41 @@ using namespace osgEarth::FeatureImageLayerImpl;


FeatureRasterizer::FeatureRasterizer(
unsigned int width, unsigned int height,
unsigned width, unsigned height,
const GeoExtent& extent,
const Color& backgroundColor) :

_extent(extent)
_width(width),
_height(height),
_originalExtent(extent)
{
unsigned imageWidth = width;
unsigned imageHeight = height;

if (_bufferPixels > 0)
{
imageWidth = width + 2 * _bufferPixels;
imageHeight = height + 2 * _bufferPixels;

double pixelSizeS = extent.width() / (double)width;
double pixelSizeT = extent.height() / (double)height;

_extent = GeoExtent(
extent.getSRS(),
extent.xMin() - pixelSizeS * (double)_bufferPixels,
extent.yMin() - pixelSizeT * (double)_bufferPixels,
extent.xMax() + pixelSizeS * (double)_bufferPixels,
extent.yMax() + pixelSizeT * (double)_bufferPixels);
}
else
{
_extent = _originalExtent;
}

// Allocate the image and initialize it to the background color
_image = new osg::Image();
_image->allocateImage(width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE);
//_image->allocateImage(width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE);
_image->allocateImage(imageWidth, imageHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
ImageUtils::PixelWriter write(_image.get());

#ifdef USE_BLEND2D
Expand All @@ -679,10 +700,7 @@ FeatureRasterizer::FeatureRasterizer(
}


FeatureRasterizer::FeatureRasterizer(
osg::Image* image,
const GeoExtent& extent) :

FeatureRasterizer::FeatureRasterizer(osg::Image* image, const GeoExtent& extent) :
_image(image),
_extent(extent)
{
Expand Down Expand Up @@ -730,12 +748,18 @@ FeatureRasterizer::render_blend2d(
const TextSymbol* masterText = style.getSymbol<TextSymbol>();
const SkinSymbol* masterSkin = style.getSymbol<SkinSymbol>();

// Converts coordinates to image space (s,t):
// Converts coordinates to image space (double s, t).
// Blend2D uses a coordinate system where (0,0) is the top-left corner of the image (on the actual corner,
// not the center up the upper-left pixel) and (s,t) is the bottom-right corner of the bottom-right
// pixel. The y-axis is positive down.

RenderFrame frame;
frame.xmin = _extent.xMin();
frame.ymin = _extent.yMin();
frame.xmax = _extent.xMax();
frame.ymax = _extent.yMax();
//frame.xf = (double)(_image->s()-1) / _extent.width();
//frame.yf = (double)(_image->t()-1) / _extent.height();
frame.xf = (double)_image->s() / _extent.width();
frame.yf = (double)_image->t() / _extent.height();

Expand All @@ -760,8 +784,8 @@ FeatureRasterizer::render_blend2d(

if (masterLine)
{
float lineWidth_px = 1.0f;
float outlineWidth_px = 0.0f;
double lineWidth_px = 1.0f;
double outlineWidth_px = 0.0f;

// Calculate the line width in pixels:
if (masterLine->stroke()->width().isSet())
Expand Down Expand Up @@ -791,22 +815,14 @@ FeatureRasterizer::render_blend2d(
_extent.getSRS()->getUnits(),
_extent.yMax());

//double lineWidth_map_south = lineWidth.asDistance(
// _extent.getSRS()->getUnits(),
// _extent.yMin());

//double lineWidth_map_north = lineWidth.asDistance(
// _extent.getSRS()->getUnits(),
// _extent.yMax());

double lineWidth_map = std::min(lineWidth_map_south, lineWidth_map_north);

double pixelSize_map = _extent.height() / (double)_image->t();

lineWidth_px = (lineWidth_map / pixelSize_map);

// enfore a minimum width of one pixel.
float minPixels = masterLine->stroke()->minPixels().getOrUse(1.0f);
double minPixels = masterLine->stroke()->minPixels().getOrUse(1.0);
lineWidth_px = osg::clampAbove(lineWidth_px, minPixels);
}

Expand All @@ -832,20 +848,12 @@ FeatureRasterizer::render_blend2d(
_extent.getSRS()->getUnits(),
_extent.yMax());

//double lineWidth_map_south = width.asDistance(
// _extent.getSRS()->getUnits(),
// _extent.yMin());

//double lineWidth_map_north = width.asDistance(
// _extent.getSRS()->getUnits(),
// _extent.yMax());

double lineWidth_map = std::min(lineWidth_map_south, lineWidth_map_north);
double pixelSize_map = _extent.height() / (double)_image->t();
outlineWidth_px = (lineWidth_map / pixelSize_map);

// enfore a minimum width of one pixel.
float minPixels = masterLine->stroke()->minPixels().getOrUse(1.0f);
double minPixels = masterLine->stroke()->minPixels().getOrUse(1.0);
outlineWidth_px = std::max(outlineWidth_px, minPixels);
}

Expand Down Expand Up @@ -1355,10 +1363,28 @@ FeatureRasterizer::finalize()
}
}

// unbuffer
if (_bufferPixels > 0)
{
auto finalImage = new osg::Image();
finalImage->allocateImage(_width, _height, 1, _image->getPixelFormat(), _image->getDataType());
ImageUtils::PixelReader read(_image.get());
ImageUtils::PixelWriter write(finalImage);

osg::Vec4 pixel;
ImageUtils::ImageIterator iter(finalImage);
iter.forEachPixel([&](auto& i) {
read(pixel, i.s() + _bufferPixels, i.t() + _bufferPixels, i.r());
write(pixel, i.s(), i.t(), i.r());
});

_image = finalImage;
}

if (_inverted)
{
_image->flipVertical();
}

return GeoImage(_image.release(), _extent);
return GeoImage(_image.release(), _originalExtent);
}
78 changes: 42 additions & 36 deletions src/osgEarth/FeatureStyleSorter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,51 +93,57 @@ FeatureStyleSorter::sort_usingSelectors(
{
Feature* feature = itr->get();

const std::string& styleString = feature->eval(styleExprCopy, &context);
if (!styleString.empty() && styleString != "null")
const std::string& delimitedStyleStrings = feature->eval(styleExprCopy, &context);
if (!delimitedStyleStrings.empty() && delimitedStyleStrings != "null")
{
// resolve the style:
const Style* resolved_style = nullptr;
int resolved_index = 0;
std::vector<std::string> styleStrings;
StringTokenizer(delimitedStyleStrings, styleStrings, ",", "", false, true);

// if the style string begins with an open bracket, it's an inline style definition.
if (styleString.length() > 0 && styleString[0] == '{')
for (auto& styleString : styleStrings)
{
Config conf("style", styleString);
conf.setReferrer(sel.styleExpression().get().uriContext().referrer());
conf.set("type", "text/css");
auto& literal_style_and_index = literal_styles[conf.toJSON()];
if (literal_style_and_index.first.empty())
// resolve the style:
const Style* resolved_style = nullptr;
int resolved_index = 0;

// if the style string begins with an open bracket, it's an inline style definition.
if (styleString.length() > 0 && styleString[0] == '{')
{
literal_style_and_index.first = Style(conf);
// literal styles always come AFTER sheet styles
literal_style_and_index.second = literal_styles.size() + session->styles()->getStyles().size();
Config conf("style", styleString);
conf.setReferrer(sel.styleExpression().get().uriContext().referrer());
conf.set("type", "text/css");
auto& literal_style_and_index = literal_styles[conf.toJSON()];
if (literal_style_and_index.first.empty())
{
literal_style_and_index.first = Style(conf);
// literal styles always come AFTER sheet styles
literal_style_and_index.second = literal_styles.size() + session->styles()->getStyles().size();
}
resolved_style = &literal_style_and_index.first;
resolved_index = literal_style_and_index.second;
}
resolved_style = &literal_style_and_index.first;
resolved_index = literal_style_and_index.second;
}

// otherwise, look up the style in the stylesheet. Do NOT fall back on a default
// style in this case: for style expressions, the user must be explicit about
// default styling; this is because there is no other way to exclude unwanted
// features.
else
{
auto style_and_index = session->styles()->getStyleAndIndex(styleString);

//const Style* selected_style = session->styles()->getStyle(styleString, false);
if (style_and_index.first)
// otherwise, look up the style in the stylesheet. Do NOT fall back on a default
// style in this case: for style expressions, the user must be explicit about
// default styling; this is because there is no other way to exclude unwanted
// features.
else
{
resolved_style = style_and_index.first;
resolved_index = style_and_index.second;
auto style_and_index = session->styles()->getStyleAndIndex(styleString);

//const Style* selected_style = session->styles()->getStyle(styleString, false);
if (style_and_index.first)
{
resolved_style = style_and_index.first;
resolved_index = style_and_index.second;
}
}
}

if (resolved_style)
{
auto& bucket = style_buckets[resolved_index];
bucket.first = resolved_style;
bucket.second.emplace_back(feature);
if (resolved_style)
{
auto& bucket = style_buckets[resolved_index];
bucket.first = resolved_style;
bucket.second.emplace_back(feature);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/osgEarth/StyleSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,16 @@ StyleSheet::Options::fromConfig(const Config& conf)

auto_script << "// __oe_auto__\n";
auto_script << "function __oe_select_style() {\n";
auto_script << " var combo = '';\n";
}

auto_script << " if (" << selector_symbol->predicate().get() << ") return \"" << style.getName() << "\";\n";
auto_script << " if (" << selector_symbol->predicate().get() << ") combo = combo + '" << style.getName() << ",';\n";
}
}

if (auto_selector)
{
auto_script << " if (combo.length > 0) return combo.substring(0, combo.length-1);\n";
auto_script << " return 'default';\n}\n";
auto new_code = auto_script.str();

Expand Down
13 changes: 1 addition & 12 deletions src/osgEarthProcedural/RoadSurfaceLayer
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,14 @@ namespace osgEarth { namespace Procedural
protected: // Layer

// post-ctor initialization
virtual void init() override;

protected:

virtual ~RoadSurfaceLayer() { }
void init() override;

private:
osg::ref_ptr<Session> _session;
mutable Gate<TileKey> _keygate;
using FeatureListCache = LRUCache<TileKey, FeatureList>;
mutable std::unique_ptr<FeatureListCache> _lru;
FeatureFilterChain _filterChain;

void getFeatures(
FeatureSource* featureSource,
const TileKey& key,
FeatureList& output,
ProgressCallback* progress) const;

osg::ref_ptr<TileRasterizer> _rasterizer;
};

Expand Down
Loading

0 comments on commit 1b2e286

Please sign in to comment.