From 02fe963d2310ee1b6c14b09e84530b853938233c Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 4 May 2017 14:26:54 -0400 Subject: [PATCH] Define consistency check for Envelope, to be used in Paste --- src/Envelope.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Envelope.h | 6 +++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/Envelope.cpp b/src/Envelope.cpp index a0a9b84d1..0cb07698c 100644 --- a/src/Envelope.cpp +++ b/src/Envelope.cpp @@ -56,6 +56,55 @@ Envelope::~Envelope() { } +bool Envelope::ConsistencyCheck() +{ + bool consistent = true; + + bool disorder; + do { + disorder = false; + for ( size_t ii = 0, count = mEnv.size(); ii < count; ) { + // Find range of points with equal T + const double thisT = mEnv[ii].GetT(); + double nextT; + auto nextI = ii + 1; + while ( nextI < count && thisT == ( nextT = mEnv[nextI].GetT() ) ) + ++nextI; + + if ( nextI < count && nextT < thisT ) + disorder = true; + + while ( nextI - ii > 2 ) { + // too many coincident time values + if (ii == mDragPoint || nextI - 1 == mDragPoint) + // forgivable + ; + else { + consistent = false; + // repair it + Delete( nextI - 2 ); + if (mDragPoint >= nextI - 2) + --mDragPoint; + --nextI, --count; + // wxLogError + } + } + + ii = nextI; + } + + if (disorder) { + consistent = false; + // repair it + std::stable_sort( mEnv.begin(), mEnv.end(), + []( const EnvPoint &a, const EnvPoint &b ) + { return a.GetT() < b.GetT(); } ); + } + } while ( disorder ); + + return consistent; +} + /// Rescale function for time tracks (could also be used for other tracks though). /// This is used to load old time track project files where the envelope used a 0 to 1 /// range instead of storing the actual time track values. This function will change the range of the envelope diff --git a/src/Envelope.h b/src/Envelope.h index 2e7145717..4a5ee1af8 100644 --- a/src/Envelope.h +++ b/src/Envelope.h @@ -86,7 +86,11 @@ public: void Initialize(int numPoints); - virtual ~ Envelope(); + virtual ~Envelope(); + + // Return true if violations of point ordering invariants were detected + // and repaired + bool ConsistencyCheck(); double GetOffset() const { return mOffset; } double GetTrackLen() const { return mTrackLen; }