1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added some extra tolerance settings to some Path methods

This commit is contained in:
jules 2016-04-11 16:00:15 +01:00
parent 0a0c550198
commit 9abd4eeace
5 changed files with 33 additions and 27 deletions

View file

@ -60,6 +60,9 @@ const float Path::quadMarker = 100003.0f;
const float Path::cubicMarker = 100004.0f; const float Path::cubicMarker = 100004.0f;
const float Path::closeSubPathMarker = 100005.0f; const float Path::closeSubPathMarker = 100005.0f;
const float Path::defaultToleranceForTesting = 1.0f;
const float Path::defaultToleranceForMeasurement = 0.6f;
//============================================================================== //==============================================================================
Path::PathBounds::PathBounds() noexcept Path::PathBounds::PathBounds() noexcept
: pathXMin (0), pathXMax (0), pathYMin (0), pathYMax (0) : pathXMin (0), pathXMax (0), pathYMin (0), pathYMax (0)
@ -1060,7 +1063,7 @@ bool Path::contains (const Point<float> point, const float tolerance) const
return contains (point.x, point.y, tolerance); return contains (point.x, point.y, tolerance);
} }
bool Path::intersectsLine (const Line<float>& line, const float tolerance) bool Path::intersectsLine (Line<float> line, const float tolerance)
{ {
PathFlatteningIterator i (*this, AffineTransform(), tolerance); PathFlatteningIterator i (*this, AffineTransform(), tolerance);
Point<float> intersection; Point<float> intersection;
@ -1072,7 +1075,7 @@ bool Path::intersectsLine (const Line<float>& line, const float tolerance)
return false; return false;
} }
Line<float> Path::getClippedLine (const Line<float>& line, const bool keepSectionOutsidePath) const Line<float> Path::getClippedLine (Line<float> line, const bool keepSectionOutsidePath) const
{ {
Line<float> result (line); Line<float> result (line);
const bool startInside = contains (line.getStart()); const bool startInside = contains (line.getStart());
@ -1103,10 +1106,10 @@ Line<float> Path::getClippedLine (const Line<float>& line, const bool keepSectio
return result; return result;
} }
float Path::getLength (const AffineTransform& transform) const float Path::getLength (const AffineTransform& transform, float tolerance) const
{ {
float length = 0; float length = 0;
PathFlatteningIterator i (*this, transform); PathFlatteningIterator i (*this, transform, tolerance);
while (i.next()) while (i.next())
length += Line<float> (i.x1, i.y1, i.x2, i.y2).getLength(); length += Line<float> (i.x1, i.y1, i.x2, i.y2).getLength();
@ -1114,9 +1117,11 @@ float Path::getLength (const AffineTransform& transform) const
return length; return length;
} }
Point<float> Path::getPointAlongPath (float distanceFromStart, const AffineTransform& transform) const Point<float> Path::getPointAlongPath (float distanceFromStart,
const AffineTransform& transform,
float tolerance) const
{ {
PathFlatteningIterator i (*this, transform); PathFlatteningIterator i (*this, transform, tolerance);
while (i.next()) while (i.next())
{ {
@ -1133,9 +1138,10 @@ Point<float> Path::getPointAlongPath (float distanceFromStart, const AffineTrans
} }
float Path::getNearestPoint (const Point<float> targetPoint, Point<float>& pointOnPath, float Path::getNearestPoint (const Point<float> targetPoint, Point<float>& pointOnPath,
const AffineTransform& transform) const const AffineTransform& transform,
float tolerance) const
{ {
PathFlatteningIterator i (*this, transform); PathFlatteningIterator i (*this, transform, tolerance);
float bestPosition = 0, bestDistance = std::numeric_limits<float>::max(); float bestPosition = 0, bestDistance = std::numeric_limits<float>::max();
float length = 0; float length = 0;
Point<float> pointOnLine; Point<float> pointOnLine;

View file

@ -83,12 +83,14 @@ public:
bool operator== (const Path&) const noexcept; bool operator== (const Path&) const noexcept;
bool operator!= (const Path&) const noexcept; bool operator!= (const Path&) const noexcept;
static const float defaultToleranceForTesting;
static const float defaultToleranceForMeasurement;
//============================================================================== //==============================================================================
/** Returns true if the path doesn't contain any lines or curves. */ /** Returns true if the path doesn't contain any lines or curves. */
bool isEmpty() const noexcept; bool isEmpty() const noexcept;
/** Returns the smallest rectangle that contains all points within the path. /** Returns the smallest rectangle that contains all points within the path. */
*/
Rectangle<float> getBounds() const noexcept; Rectangle<float> getBounds() const noexcept;
/** Returns the smallest rectangle that contains all points within the path /** Returns the smallest rectangle that contains all points within the path
@ -110,7 +112,7 @@ public:
@see closeSubPath, setUsingNonZeroWinding @see closeSubPath, setUsingNonZeroWinding
*/ */
bool contains (float x, float y, bool contains (float x, float y,
float tolerance = 1.0f) const; float tolerance = defaultToleranceForTesting) const;
/** Checks whether a point lies within the path. /** Checks whether a point lies within the path.
@ -126,7 +128,7 @@ public:
@see closeSubPath, setUsingNonZeroWinding @see closeSubPath, setUsingNonZeroWinding
*/ */
bool contains (const Point<float> point, bool contains (const Point<float> point,
float tolerance = 1.0f) const; float tolerance = defaultToleranceForTesting) const;
/** Checks whether a line crosses the path. /** Checks whether a line crosses the path.
@ -138,8 +140,8 @@ public:
so this method could return a false positive when your point is up to this distance so this method could return a false positive when your point is up to this distance
outside the path's boundary. outside the path's boundary.
*/ */
bool intersectsLine (const Line<float>& line, bool intersectsLine (Line<float> line,
float tolerance = 1.0f); float tolerance = defaultToleranceForTesting);
/** Cuts off parts of a line to keep the parts that are either inside or /** Cuts off parts of a line to keep the parts that are either inside or
outside this path. outside this path.
@ -153,12 +155,13 @@ public:
that will be kept; if false its the section inside that will be kept; if false its the section inside
the path the path
*/ */
Line<float> getClippedLine (const Line<float>& line, bool keepSectionOutsidePath) const; Line<float> getClippedLine (Line<float> line, bool keepSectionOutsidePath) const;
/** Returns the length of the path. /** Returns the length of the path.
@see getPointAlongPath @see getPointAlongPath
*/ */
float getLength (const AffineTransform& transform = AffineTransform()) const; float getLength (const AffineTransform& transform = AffineTransform(),
float tolerance = defaultToleranceForMeasurement) const;
/** Returns a point that is the specified distance along the path. /** Returns a point that is the specified distance along the path.
If the distance is greater than the total length of the path, this will return the If the distance is greater than the total length of the path, this will return the
@ -166,15 +169,17 @@ public:
@see getLength @see getLength
*/ */
Point<float> getPointAlongPath (float distanceFromStart, Point<float> getPointAlongPath (float distanceFromStart,
const AffineTransform& transform = AffineTransform()) const; const AffineTransform& transform = AffineTransform(),
float tolerance = defaultToleranceForMeasurement) const;
/** Finds the point along the path which is nearest to a given position. /** Finds the point along the path which is nearest to a given position.
This sets pointOnPath to the nearest point, and returns the distance of this point from the start This sets pointOnPath to the nearest point, and returns the distance of this point from the start
of the path. of the path.
*/ */
float getNearestPoint (const Point<float> targetPoint, float getNearestPoint (Point<float> targetPoint,
Point<float>& pointOnPath, Point<float>& pointOnPath,
const AffineTransform& transform = AffineTransform()) const; const AffineTransform& transform = AffineTransform(),
float tolerance = defaultToleranceForMeasurement) const;
//============================================================================== //==============================================================================
/** Removes all lines and curves, resetting the path completely. */ /** Removes all lines and curves, resetting the path completely. */

View file

@ -26,8 +26,6 @@
#pragma optimize ("t", on) #pragma optimize ("t", on)
#endif #endif
const float PathFlatteningIterator::defaultTolerance = 0.6f;
//============================================================================== //==============================================================================
PathFlatteningIterator::PathFlatteningIterator (const Path& path_, PathFlatteningIterator::PathFlatteningIterator (const Path& path_,
const AffineTransform& transform_, const AffineTransform& transform_,

View file

@ -53,7 +53,7 @@ public:
*/ */
PathFlatteningIterator (const Path& path, PathFlatteningIterator (const Path& path,
const AffineTransform& transform = AffineTransform(), const AffineTransform& transform = AffineTransform(),
float tolerance = defaultTolerance); float tolerance = Path::defaultToleranceForMeasurement);
/** Destructor. */ /** Destructor. */
~PathFlatteningIterator(); ~PathFlatteningIterator();
@ -90,9 +90,6 @@ public:
/** Returns true if the current segment is the last in the current sub-path. */ /** Returns true if the current segment is the last in the current sub-path. */
bool isLastInSubpath() const noexcept; bool isLastInSubpath() const noexcept;
/** This is the default value that should be used for the tolerance value (see the constructor parameters). */
static const float defaultTolerance;
private: private:
//============================================================================== //==============================================================================
const Path& path; const Path& path;

View file

@ -570,7 +570,7 @@ namespace PathStrokeHelpers
// Iterate the path, creating a list of the // Iterate the path, creating a list of the
// left/right-hand lines along either side of it... // left/right-hand lines along either side of it...
PathFlatteningIterator it (*sourcePath, transform, PathFlatteningIterator::defaultTolerance / extraAccuracy); PathFlatteningIterator it (*sourcePath, transform, Path::defaultToleranceForMeasurement / extraAccuracy);
Array <LineSection> subPath; Array <LineSection> subPath;
subPath.ensureStorageAllocated (512); subPath.ensureStorageAllocated (512);
@ -668,7 +668,7 @@ void PathStrokeType::createDashedStroke (Path& destPath,
return; return;
Path newDestPath; Path newDestPath;
PathFlatteningIterator it (sourcePath, transform, PathFlatteningIterator::defaultTolerance / extraAccuracy); PathFlatteningIterator it (sourcePath, transform, Path::defaultToleranceForMeasurement / extraAccuracy);
bool first = true; bool first = true;
int dashNum = 0; int dashNum = 0;