-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses second item in https://github.com/rive-app/rive/issues/7946 ~~Draft as I need to add WASM side.~~ Diffs= 5719c5662 Add contour measure to Rive Native (#8145) Co-authored-by: Luigi Rosso <[email protected]>
- Loading branch information
1 parent
28e7d45
commit 6e9a2a5
Showing
7 changed files
with
95 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
8441098ec49bc6dcf6022db2af677d7dc7b590b6 | ||
5719c566261148150e788c80a9672e940baa0542 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef _RIVE_PATH_MEASURE_HPP_ | ||
#define _RIVE_PATH_MEASURE_HPP_ | ||
|
||
#include "rive/math/contour_measure.hpp" | ||
#include <vector> | ||
|
||
namespace rive | ||
{ | ||
class PathMeasure | ||
{ | ||
public: | ||
PathMeasure(); | ||
PathMeasure(const RawPath* path); | ||
ContourMeasure::PosTanDistance atDistance(float distance) const; | ||
ContourMeasure::PosTanDistance atPercentage(float percentageDistance) const; | ||
|
||
float length() const { return m_length; } | ||
|
||
private: | ||
float m_length; | ||
std::vector<rcp<ContourMeasure>> m_contours; | ||
}; | ||
} // namespace rive | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "rive/math/path_measure.hpp" | ||
|
||
using namespace rive; | ||
|
||
PathMeasure::PathMeasure() : m_length(0.0f) {} | ||
|
||
PathMeasure::PathMeasure(const RawPath* path) : m_length(0.0f) | ||
{ | ||
auto measure = ContourMeasureIter(path); | ||
for (auto contour = measure.next(); contour != nullptr; contour = measure.next()) | ||
{ | ||
m_length += contour->length(); | ||
m_contours.push_back(contour); | ||
} | ||
} | ||
|
||
ContourMeasure::PosTanDistance PathMeasure::atDistance(float distance) const | ||
{ | ||
float currentDistance = distance; | ||
for (auto contour : m_contours) | ||
{ | ||
float contourLength = contour->length(); | ||
if (currentDistance - contourLength <= 0) | ||
{ | ||
return ContourMeasure::PosTanDistance(contour->getPosTan(currentDistance), distance); | ||
} | ||
currentDistance -= contourLength; | ||
} | ||
return ContourMeasure::PosTanDistance(); | ||
} | ||
|
||
ContourMeasure::PosTanDistance PathMeasure::atPercentage(float percentageDistance) const | ||
{ | ||
float inRangePercentage = fmodf(percentageDistance, 1.0f); | ||
if (inRangePercentage < 0.0f) | ||
{ | ||
inRangePercentage += 1.0f; | ||
} | ||
|
||
// Mod to correct percentage (0-100%) and make sure we actually reach | ||
// 100%. | ||
if (percentageDistance != 0.0f && inRangePercentage == 0.0f) | ||
{ | ||
inRangePercentage = 1.0f; | ||
} | ||
|
||
return atDistance(m_length * inRangePercentage); | ||
} |