From 1f2c71ec8bc3b228db2d90751a5bae4e269e1dd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 17 Mar 2014 16:53:40 +0100 Subject: port of collision class --- include/llmr/text/collision.hpp | 78 ++++++++++++++++++++++++++++++++++++ include/llmr/text/rotation_range.hpp | 12 ++---- include/llmr/util/math.hpp | 20 +++++++++ 3 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 include/llmr/text/collision.hpp (limited to 'include') diff --git a/include/llmr/text/collision.hpp b/include/llmr/text/collision.hpp new file mode 100644 index 0000000000..d207a08668 --- /dev/null +++ b/include/llmr/text/collision.hpp @@ -0,0 +1,78 @@ +#ifndef LLMR_TEXT_COLLISION +#define LLMR_TEXT_COLLISION + +#include +#include +#include +#include + +#include + +namespace llmr { + +// These are the glyph boxes that we want to have placed. +struct GlyphBox { + GlyphBox() {} + GlyphBox(const CollisionRect &bbox, const CollisionRect &box, + float minScale) + : bbox(bbox), box(box), minScale(minScale) {} + + CollisionAnchor anchor; + CollisionRect bbox; + CollisionRect box; + bool rotate = false; + float minScale = 0.0f; + float maxScale = std::numeric_limits::infinity(); +}; + +typedef std::vector GlyphBoxes; + +// These are the placed boxes contained in the rtree. +struct PlacementBox { + CollisionAnchor anchor; + CollisionRect bbox; + CollisionRect box; + bool rotate = false; + PlacementRange placementRange = {{0.0f, 0.0f}}; + float placementScale = 0.0f; + float maxScale = std::numeric_limits::infinity(); + float padding = 0.0f; +}; + +struct Placement { + Placement() {} + Placement(float zoom, const PlacementRange &rotationRange) + : zoom(zoom), rotationRange(rotationRange) {} + + float zoom = -1.0f; + PlacementRange rotationRange = {{0.0f, 0.0f}}; +}; + +class Collision { + typedef boost::geometry::model::point Point; + typedef boost::geometry::model::box Box; + typedef std::pair PlacementValue; + typedef boost::geometry::index::rtree< + PlacementValue, boost::geometry::index::quadratic<16>> Tree; + + public: + Collision(); + + Placement place(const GlyphBoxes &boxes, const CollisionAnchor &anchor, + float minPlacementScale, float maxPlacementScale, float padding, + bool horizontal); + float getPlacementScale(const GlyphBoxes &glyphs, float minPlacementScale, + float maxPlacementScale, float pad); + PlacementRange getPlacementRange(const GlyphBoxes &glyphs, + float placementScale); + void insert(const GlyphBoxes &glyphs, const CollisionAnchor &anchor, + float placementScale, const PlacementRange &placementRange, + bool horizontal, float padding); + + private: + Tree tree; +}; +} + +#endif \ No newline at end of file diff --git a/include/llmr/text/rotation_range.hpp b/include/llmr/text/rotation_range.hpp index e3d3ab6987..bf40c0c6a2 100644 --- a/include/llmr/text/rotation_range.hpp +++ b/include/llmr/text/rotation_range.hpp @@ -31,13 +31,6 @@ struct CollisionRect { : tl(tl), br(br) {} }; -struct CollisionBox { - CollisionRect box; - CollisionPoint anchor; - bool rotate = false; - PlacementRange placementRange; -}; - /* * Combine an array of collision ranges to form a continuous * range that includes 0. Collisions within the ignoreRange are ignored @@ -76,8 +69,9 @@ CollisionList rotatingFixedCollisions(const CollisionRect &rotating, /* * Calculate the range a box conflicts with a second box */ -CollisionRange rotationRange(const CollisionBox &inserting, - const CollisionBox &blocker, double scale); +template +CollisionRange rotationRange(const InsertingBox &inserting, + const BlockingBox &blocker, float scale); } #endif diff --git a/include/llmr/util/math.hpp b/include/llmr/util/math.hpp index b5f0ca2895..d356cf8763 100644 --- a/include/llmr/util/math.hpp +++ b/include/llmr/util/math.hpp @@ -15,11 +15,31 @@ inline T max(T a, T b) { return b > a ? b : a; } +template +inline T max(T a, T b, T c) { + return max(max(a, b), c); +} + +template +inline T max(T a, T b, T c, T d) { + return max(max(a, b), max(c, d)); +} + template inline T min(T a, T b) { return b < a ? b : a; } +template +inline T min(T a, T b, T c) { + return min(min(a, b), c); +} + +template +inline T min(T a, T b, T c, T d) { + return min(min(a, b), min(c, d)); +} + // Find the angle of the two vectors, solving the formula for the cross product // a x b = |a||b|sin(θ) for θ. template -- cgit v1.2.1