From 29d9b441402979c435bc3e5cb1885cb4e36da9ee Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Fri, 5 May 2023 15:03:16 +0200 Subject: [PATCH] boundingbox for the complete layer --- src/GPUFontAtlasLayerCombo.cpp | 38 ++++++++++++++++++++++++++++++++-- src/GPUFontLayer.cpp | 17 +++++++++++++++ src/GPUFontLayer.h | 4 ++++ src/Layer.h | 9 ++++++++ src/MsdfLayer.cpp | 6 ++++++ src/MsdfLayer.h | 3 +++ 6 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/GPUFontAtlasLayerCombo.cpp b/src/GPUFontAtlasLayerCombo.cpp index 9d63be9..0735b16 100644 --- a/src/GPUFontAtlasLayerCombo.cpp +++ b/src/GPUFontAtlasLayerCombo.cpp @@ -204,6 +204,7 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){ float ascender = font->getAscender(layer->getProps().fontSize_px); ofxGPUFont::Font::BoundingBox bb; std::vector bbs; + std::vector mirror_bbs; float advanceY = 0; font->collectBoundingBoxes(layer->getVariationText(), layer->getVariationTextAppearance(), @@ -218,9 +219,16 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){ ascender, advanceY, layer->getProps()); - bb.multiply(layer->getInnerNode().getGlobalTransformMatrix()); + auto bb_mirror_none = bb; + bb_mirror_none.multiply(layer->getInnerNode().getGlobalTransformMatrix()); + + mirror_bbs.push_back(std::move(bb_mirror_none)); + //layer->setBoundingBox(bb); ofVboMesh mesh; - mesh.addVertices({bb.p0, bb.p1, bb.p2, bb.p3, + mesh.addVertices({bb_mirror_none.p0, + bb_mirror_none.p1, + bb_mirror_none.p2, + bb_mirror_none.p3, glm::vec4(layer->getProps().x, layer->getProps().y, 0, 1), transformOrigin }); @@ -244,6 +252,10 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){ mirrorOuterNode.setPosition(transformOrigin); mirrorOuterNode.move(layer->getProps().mirror_x_distance * -1, 0, 0); mirrorOuterNode.setScale(-1, 1, 1); + + auto bb_mirror_x = bb; + bb_mirror_x.multiply(mirrorInnerNode.getGlobalTransformMatrix()); + mirror_bbs.push_back(std::move(bb_mirror_x)); font->collectVerticesAndIndices(mirrorInnerNode, layer->getVariationTextAppearance(), bbs_mirror_x, @@ -260,6 +272,10 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){ mirrorOuterNode.setPosition(transformOrigin); mirrorOuterNode.move(0, layer->getProps().mirror_y_distance * -1, 0); mirrorOuterNode.setScale(1, -1, 1); + + auto bb_mirror_y = bb; + bb_mirror_y.multiply(mirrorInnerNode.getGlobalTransformMatrix()); + mirror_bbs.push_back(std::move(bb_mirror_y)); font->collectVerticesAndIndices(mirrorInnerNode, layer->getVariationTextAppearance(), bbs_mirror_y, @@ -277,6 +293,10 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){ mirrorOuterNode.move(layer->getProps().mirror_x_distance * -1, layer->getProps().mirror_y_distance * -1, 0); mirrorOuterNode.setScale(-1, -1, 1); + + auto bb_mirror_xy = bb; + bb_mirror_xy.multiply(mirrorInnerNode.getGlobalTransformMatrix()); + mirror_bbs.push_back(std::move(bb_mirror_xy)); font->collectVerticesAndIndices(mirrorInnerNode, layer->getVariationTextAppearance(), bbs_mirror_xy, @@ -288,6 +308,20 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){ bbs, vertices, indices); + + float min_x = FLT_MAX; + float max_x = -FLT_MAX; + float min_y = FLT_MAX; + float max_y = -FLT_MAX; + for(const auto & mbb : mirror_bbs){ + min_x = min(min_x, min(mbb.p0.x, min(mbb.p1.x, min(mbb.p2.x, mbb.p3.x)))); + max_x = max(max_x, max(mbb.p0.x, max(mbb.p1.x, max(mbb.p2.x, mbb.p3.x)))); + min_y = min(min_y, min(mbb.p0.y, min(mbb.p1.y, min(mbb.p2.y, mbb.p3.y)))); + max_y = max(max_y, max(mbb.p0.y, max(mbb.p1.y, max(mbb.p2.y, mbb.p3.y)))); + } + float width = max_x - min_x; + float height = max_y - min_y; + layer->setBoundingBox(Layer::BoundingBox{min_x, min_y, width, height}); } { diff --git a/src/GPUFontLayer.cpp b/src/GPUFontLayer.cpp index e13b959..1183122 100644 --- a/src/GPUFontLayer.cpp +++ b/src/GPUFontLayer.cpp @@ -147,6 +147,23 @@ ofNode & GPUFontLayer::getOuterNode(){ ofNode & GPUFontLayer::getInnerNode(){ return innerNode; } +const Layer::BoundingBox & GPUFontLayer::getBoundingBox(){ + return this->boundingBox; +} +void GPUFontLayer::setBoundingBox(const Layer::BoundingBox & boundingBox){ + this->boundingBox = boundingBox; +} +void GPUFontLayer::setBoundingBox(const ofxGPUFont::Font::BoundingBox & bb){ + float min_x = min(bb.p0.x, min(bb.p1.x, min(bb.p2.x, bb.p3.x))); + float max_x = max(bb.p0.x, max(bb.p1.x, max(bb.p2.x, bb.p3.x))); + float min_y = min(bb.p0.y, min(bb.p1.y, min(bb.p2.y, bb.p3.y))); + float max_y = max(bb.p0.y, max(bb.p1.y, max(bb.p2.y, bb.p3.y))); + float width = max_x - min_x; + float height = max_y - min_y; + setBoundingBox(Layer::BoundingBox{ + min_x, min_y, width, height + }); +} void GPUFontLayer::clearPropsBuffer(){ propsBuffer.clear(); } diff --git a/src/GPUFontLayer.h b/src/GPUFontLayer.h index 192a6a2..dd38df3 100644 --- a/src/GPUFontLayer.h +++ b/src/GPUFontLayer.h @@ -45,6 +45,9 @@ class GPUFontLayer : public Layer { void setMomsComboIdentifier(const ComboIdentifier & identifier) override; ofNode & getOuterNode() override; ofNode & getInnerNode() override; + const Layer::BoundingBox & getBoundingBox() override; + void setBoundingBox(const Layer::BoundingBox & boundingBox) override; + void setBoundingBox(const ofxGPUFont::Font::BoundingBox & boundingBox); const vector & getVariationText(); const vector & getVariationTextAppearance(); @@ -67,5 +70,6 @@ class GPUFontLayer : public Layer { ofNode outerNode; ofNode innerNode; int letterDelayBufferSize = 0; + Layer::BoundingBox boundingBox; }; } diff --git a/src/Layer.h b/src/Layer.h index 678bb71..0087e96 100644 --- a/src/Layer.h +++ b/src/Layer.h @@ -3,6 +3,7 @@ #include "ofMain.h" //uuid +#include "ofRectangle.h" #include "random_id.h" #include "ofxMsdfgen.h" @@ -60,6 +61,12 @@ class Layer { //text, //fontPath) }; + struct BoundingBox { + float x; + float y; + float w; + float h; + }; static std::string asString(const ofxVariableLab::Layer::Props & props){ stringstream s; @@ -115,6 +122,8 @@ class Layer { virtual void setMomsComboIdentifier(const ComboIdentifier & identifier) = 0; virtual ofNode & getOuterNode() = 0; virtual ofNode & getInnerNode() = 0; + virtual const Layer::BoundingBox & getBoundingBox() = 0; + virtual void setBoundingBox(const Layer::BoundingBox & boundingBox) = 0; static int n_layers; }; } diff --git a/src/MsdfLayer.cpp b/src/MsdfLayer.cpp index 022d211..d39d626 100644 --- a/src/MsdfLayer.cpp +++ b/src/MsdfLayer.cpp @@ -299,6 +299,12 @@ ofNode & MsdfLayer::getOuterNode(){ ofNode & MsdfLayer::getInnerNode(){ return innerNode; } +const Layer::BoundingBox & MsdfLayer::getBoundingBox(){ + return this->boundingBox; +} +void MsdfLayer::setBoundingBox(const Layer::BoundingBox & boundingBox){ + this->boundingBox = boundingBox; +} void MsdfLayer::setAtlas(shared_ptr _atlas){ atlas = _atlas; // TODO: this does not seem proper diff --git a/src/MsdfLayer.h b/src/MsdfLayer.h index 391fdfb..7265bd4 100644 --- a/src/MsdfLayer.h +++ b/src/MsdfLayer.h @@ -37,6 +37,8 @@ class MsdfLayer : public Layer { void setMomsComboIdentifier(const ComboIdentifier & identifier) override; ofNode & getOuterNode() override; ofNode & getInnerNode() override; + const Layer::BoundingBox & getBoundingBox() override; + void setBoundingBox(const Layer::BoundingBox & boundingBox) override; void setAtlas(shared_ptr _atlas); shared_ptr getAtlas() const; @@ -59,5 +61,6 @@ class MsdfLayer : public Layer { LayerType type = MSDFGEN; ofNode outerNode; ofNode innerNode; + Layer::BoundingBox boundingBox; }; }