Speedup for flatcurves and NURBS curves getval() functions

This commit is contained in:
heckflosse
2016-05-15 16:07:23 +02:00
parent 56804fca69
commit b23778eebc
4 changed files with 30 additions and 35 deletions

View File

@@ -70,6 +70,18 @@ void Curve::AddPolygons ()
poly_y.push_back(y3); poly_y.push_back(y3);
} }
void Curve::fillDyByDx ()
{
dyByDx.resize(poly_x.size() - 1);
for(unsigned int i = 0; i < poly_x.size() - 1; i++) {
double dx = poly_x[i + 1] - poly_x[i];
double dy = poly_y[i + 1] - poly_y[i];
dyByDx[i] = dy / dx;
}
}
void Curve::fillHash() void Curve::fillHash()
{ {
hash.resize(hashSize + 2); hash.resize(hashSize + 2);

View File

@@ -336,6 +336,7 @@ protected:
// end of variables used in Parametric curves only // end of variables used in Parametric curves only
std::vector<double> poly_x; // X points of the faceted curve std::vector<double> poly_x; // X points of the faceted curve
std::vector<double> poly_y; // Y points of the faceted curve std::vector<double> poly_y; // Y points of the faceted curve
std::vector<double> dyByDx;
std::vector<HashEntry> hash; std::vector<HashEntry> hash;
unsigned short hashSize; // hash table's size, between [10, 100, 1000] unsigned short hashSize; // hash table's size, between [10, 100, 1000]
@@ -369,6 +370,7 @@ protected:
} }
void fillHash(); void fillHash();
void fillDyByDx();
public: public:
Curve (); Curve ();
@@ -406,7 +408,7 @@ public:
class FlatCurve : public Curve class FlatCurve : public Curve
{ {
protected: private:
FlatCurveType kind; FlatCurveType kind;
double* leftTangent; double* leftTangent;
double* rightTangent; double* rightTangent;

View File

@@ -72,11 +72,15 @@ DiagonalCurve::DiagonalCurve (const std::vector<double>& p, int poly_pn)
if(x[0] == 0.f && x[1] == 0.f) if(x[0] == 0.f && x[1] == 0.f)
// Avoid crash when first two points are at x = 0 (git Issue 2888) // Avoid crash when first two points are at x = 0 (git Issue 2888)
{
x[1] = 0.01f; x[1] = 0.01f;
}
if(x[0] == 1.f && x[1] == 1.f) if(x[0] == 1.f && x[1] == 1.f)
// Avoid crash when first two points are at x = 1 (100 in gui) (git Issue 2923) // Avoid crash when first two points are at x = 1 (100 in gui) (git Issue 2923)
{
x[0] = 0.99f; x[0] = 0.99f;
}
if (!identity) { if (!identity) {
if (kind == DCT_Spline && N > 2) { if (kind == DCT_Spline && N > 2) {
@@ -260,6 +264,8 @@ void DiagonalCurve::NURBS_set ()
// adding the final horizontal segment, always (see under) // adding the final horizontal segment, always (see under)
poly_x.push_back(3.0); // 3.0 is a hack for optimization purpose of the getVal method (the last value has to be beyond the normal range) poly_x.push_back(3.0); // 3.0 is a hack for optimization purpose of the getVal method (the last value has to be beyond the normal range)
poly_y.push_back(y[N - 1]); poly_y.push_back(y[N - 1]);
fillDyByDx();
} }
double DiagonalCurve::getVal (double t) const double DiagonalCurve::getVal (double t) const
@@ -303,7 +309,7 @@ double DiagonalCurve::getVal (double t) const
// do a binary search for the right interval: // do a binary search for the right interval:
unsigned int k_lo = 0, k_hi = N - 1; unsigned int k_lo = 0, k_hi = N - 1;
while (k_hi - k_lo > 1) { while (k_hi > 1 + k_lo) {
unsigned int k = (k_hi + k_lo) / 2; unsigned int k = (k_hi + k_lo) / 2;
if (x[k] > t) { if (x[k] > t) {
@@ -347,7 +353,7 @@ double DiagonalCurve::getVal (double t) const
k_hi = hash.at(i).higherValue; k_hi = hash.at(i).higherValue;
// do a binary search for the right interval : // do a binary search for the right interval :
while (k_hi - k_lo > 1) { while (k_hi > 1 + k_lo) {
unsigned int k = (k_hi + k_lo) / 2; unsigned int k = (k_hi + k_lo) / 2;
if (poly_x[k] > t) { if (poly_x[k] > t) {
@@ -357,13 +363,7 @@ double DiagonalCurve::getVal (double t) const
} }
} }
if (k_lo == k_hi) { return poly_y[k_lo] + (t - poly_x[k_lo]) * dyByDx[k_lo];
k_hi = k_lo + 1;
}
double dx = poly_x[k_hi] - poly_x[k_lo];
double dy = poly_y[k_hi] - poly_y[k_lo];
return poly_y[k_lo] + (t - poly_x[k_lo]) * ( dy ) / dx;
break; break;
} }

View File

@@ -16,21 +16,14 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>. * along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <glib.h>
#include <glib/gstdio.h>
#include "curves.h" #include "curves.h"
#include <cmath> #include <cmath>
#include <vector> #include <vector>
#include "mytime.h"
#include <cstring>
#include <gtkmm.h>
#include <fstream>
namespace rtengine namespace rtengine
{ {
FlatCurve::FlatCurve (const std::vector<double>& p, bool isPeriodic, int poly_pn) : kind(FCT_Empty), leftTangent(NULL), rightTangent(NULL), identityValue(0.5), periodic(isPeriodic) FlatCurve::FlatCurve (const std::vector<double>& p, bool isPeriodic, int poly_pn) : kind(FCT_Empty), leftTangent(nullptr), rightTangent(nullptr), identityValue(0.5), periodic(isPeriodic)
{ {
ppn = poly_pn > 65500 ? 65500 : poly_pn; ppn = poly_pn > 65500 ? 65500 : poly_pn;
@@ -341,17 +334,7 @@ void FlatCurve::CtrlPoints_set ()
poly_x.push_back(3.0); // 3.0 is a hack for optimization purpose of the getVal method (the last value has to be beyond the normal range) poly_x.push_back(3.0); // 3.0 is a hack for optimization purpose of the getVal method (the last value has to be beyond the normal range)
poly_y.push_back(sc_y[j - 1]); poly_y.push_back(sc_y[j - 1]);
/* fillDyByDx();
// Checking the values
Glib::ustring fname = "Curve.xyz"; // TopSolid'Design "plot" file format
std::ofstream f (fname.c_str());
f << "$" << std::endl;;
for (unsigned int iter = 0; iter < poly_x.size(); iter++) {
f << poly_x[iter] << ", " << poly_y[iter] << ", 0." << std::endl;;
}
f << "$" << std::endl;;
f.close ();
*/
} }
double FlatCurve::getVal (double t) const double FlatCurve::getVal (double t) const
@@ -367,10 +350,10 @@ double FlatCurve::getVal (double t) const
} }
// do a binary search for the right interval: // do a binary search for the right interval:
int k_lo = 0, k_hi = poly_x.size() - 1; unsigned int k_lo = 0, k_hi = poly_x.size() - 1;
while (k_hi - k_lo > 1) { while (k_hi > 1 + k_lo) {
int k = (k_hi + k_lo) / 2; unsigned int k = (k_hi + k_lo) / 2;
if (poly_x[k] > t) { if (poly_x[k] > t) {
k_hi = k; k_hi = k;
@@ -379,9 +362,7 @@ double FlatCurve::getVal (double t) const
} }
} }
double dx = poly_x[k_hi] - poly_x[k_lo]; return poly_y[k_lo] + (t - poly_x[k_lo]) * dyByDx[k_lo];
double dy = poly_y[k_hi] - poly_y[k_lo];
return poly_y[k_lo] + (t - poly_x[k_lo]) * dy / dx;
break; break;
} }