| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2006-2019 Adobe Systems Incorporated |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 6 |
// accordance with the terms of the Adobe license agreement accompanying it. |
| 7 |
/*****************************************************************************/ |
| 8 |
|
| 9 |
#include "dng_spline.h" |
| 10 |
|
| 11 |
#include "dng_assertions.h" |
| 12 |
#include "dng_exceptions.h" |
| 13 |
|
| 14 |
/******************************************************************************/ |
| 15 |
|
| 16 |
dng_spline_solver::dng_spline_solver () |
| 17 |
|
| 18 |
: X () |
| 19 |
, Y () |
| 20 |
, S () |
| 21 |
|
| 22 |
{ |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
/******************************************************************************/ |
| 27 |
|
| 28 |
dng_spline_solver::~dng_spline_solver () |
| 29 |
{ |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
/******************************************************************************/ |
| 34 |
|
| 35 |
void dng_spline_solver::Reset () |
| 36 |
{ |
| 37 |
|
| 38 |
X.clear (); |
| 39 |
Y.clear (); |
| 40 |
|
| 41 |
S.clear (); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/******************************************************************************/ |
| 46 |
|
| 47 |
void dng_spline_solver::Add (real64 x, real64 y) |
| 48 |
{ |
| 49 |
|
| 50 |
X.push_back (x); |
| 51 |
Y.push_back (y); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
/******************************************************************************/ |
| 56 |
|
| 57 |
void dng_spline_solver::Solve () |
| 58 |
{ |
| 59 |
|
| 60 |
// This code computes the unique curve such that: |
| 61 |
// It is C0, C1, and C2 continuous |
| 62 |
// The second derivative is zero at the end points |
| 63 |
|
| 64 |
int32 count = (int32) X.size (); |
| 65 |
|
| 66 |
DNG_REQUIRE (count >= 2, "Too few points"); |
| 67 |
|
| 68 |
int32 start = 0; |
| 69 |
int32 end = count; |
| 70 |
|
| 71 |
real64 A = X [start+1] - X [start]; |
| 72 |
real64 B = (Y [start+1] - Y [start]) / A; |
| 73 |
|
| 74 |
S.resize (count); |
| 75 |
|
| 76 |
S [start] = B; |
| 77 |
|
| 78 |
int32 j; |
| 79 |
|
| 80 |
// Slopes here are a weighted average of the slopes |
| 81 |
// to each of the adjacent control points. |
| 82 |
|
| 83 |
for (j = start + 2; j < end; ++j) |
| 84 |
{ |
| 85 |
|
| 86 |
real64 C = X [j] - X [j-1]; |
| 87 |
real64 D = (Y [j] - Y [j-1]) / C; |
| 88 |
|
| 89 |
S [j-1] = (B * C + D * A) / (A + C); |
| 90 |
|
| 91 |
A = C; |
| 92 |
B = D; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
S [end-1] = 2.0 * B - S [end-2]; |
| 97 |
S [start] = 2.0 * S [start] - S [start+1]; |
| 98 |
|
| 99 |
if ((end - start) > 2) |
| 100 |
{ |
| 101 |
|
| 102 |
dng_std_vector<real64> E; |
| 103 |
dng_std_vector<real64> F; |
| 104 |
dng_std_vector<real64> G; |
| 105 |
|
| 106 |
E.resize (count); |
| 107 |
F.resize (count); |
| 108 |
G.resize (count); |
| 109 |
|
| 110 |
F [start] = 0.5; |
| 111 |
E [end-1] = 0.5; |
| 112 |
G [start] = 0.75 * (S [start] + S [start+1]); |
| 113 |
G [end-1] = 0.75 * (S [end-2] + S [end-1]); |
| 114 |
|
| 115 |
for (j = start+1; j < end - 1; ++j) |
| 116 |
{ |
| 117 |
|
| 118 |
A = (X [j+1] - X [j-1]) * 2.0; |
| 119 |
|
| 120 |
E [j] = (X [j+1] - X [j]) / A; |
| 121 |
F [j] = (X [j] - X [j-1]) / A; |
| 122 |
G [j] = 1.5 * S [j]; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
for (j = start+1; j < end; ++j) |
| 127 |
{ |
| 128 |
|
| 129 |
A = 1.0 - F [j-1] * E [j]; |
| 130 |
|
| 131 |
if (j != end-1) F [j] /= A; |
| 132 |
|
| 133 |
G [j] = (G [j] - G [j-1] * E [j]) / A; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
for (j = end - 2; j >= start; --j) |
| 138 |
G [j] = G [j] - F [j] * G [j+1]; |
| 139 |
|
| 140 |
for (j = start; j < end; ++j) |
| 141 |
S [j] = G [j]; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
/******************************************************************************/ |
| 148 |
|
| 149 |
bool dng_spline_solver::IsIdentity () const |
| 150 |
{ |
| 151 |
|
| 152 |
int32 count = (int32) X.size (); |
| 153 |
|
| 154 |
if (count != 2) |
| 155 |
return false; |
| 156 |
|
| 157 |
if (X [0] != 0.0 || X [1] != 1.0 || |
| 158 |
Y [0] != 0.0 || Y [1] != 1.0) |
| 159 |
return false; |
| 160 |
|
| 161 |
return true; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
/******************************************************************************/ |
| 166 |
|
| 167 |
real64 dng_spline_solver::Evaluate (real64 x) const |
| 168 |
{ |
| 169 |
|
| 170 |
int32 count = (int32) X.size (); |
| 171 |
|
| 172 |
// Check for off each end of point list. |
| 173 |
|
| 174 |
if (x <= X [0]) |
| 175 |
return fUseSlopeExtensionLo ? (Y [0] + EvaluateSlope (X [0]) * (x - X [0])) |
| 176 |
: Y [0]; |
| 177 |
|
| 178 |
if (x >= X [count-1]) |
| 179 |
return fUseSlopeExtensionHi ? (Y [count-1] + EvaluateSlope (X [count-1]) * (x - X [count-1])) |
| 180 |
: Y [count-1]; |
| 181 |
|
| 182 |
// Binary search for the index. |
| 183 |
|
| 184 |
int32 lower = 1; |
| 185 |
int32 upper = count - 1; |
| 186 |
|
| 187 |
while (upper > lower) |
| 188 |
{ |
| 189 |
|
| 190 |
int32 mid = (lower + upper) >> 1; |
| 191 |
|
| 192 |
if (x == X [mid]) |
| 193 |
{ |
| 194 |
return Y [mid]; |
| 195 |
} |
| 196 |
|
| 197 |
if (x > X [mid]) |
| 198 |
lower = mid + 1; |
| 199 |
else |
| 200 |
upper = mid; |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
DNG_ASSERT (upper == lower, "Binary search error in point list"); |
| 205 |
|
| 206 |
int32 j = lower; |
| 207 |
|
| 208 |
// X [j - 1] < x <= X [j] |
| 209 |
// A is the distance between the X [j] and X [j - 1] |
| 210 |
// B and C describe the fractional distance to either side. B + C = 1. |
| 211 |
|
| 212 |
// We compute a cubic spline between the two points with slopes |
| 213 |
// S[j-1] and S[j] at either end. Specifically, we compute the 1-D Bezier |
| 214 |
// with control values: |
| 215 |
// |
| 216 |
// Y[j-1], Y[j-1] + S[j-1]*A, Y[j]-S[j]*A, Y[j] |
| 217 |
|
| 218 |
return EvaluateSplineSegment (x, |
| 219 |
X [j - 1], |
| 220 |
Y [j - 1], |
| 221 |
S [j - 1], |
| 222 |
X [j ], |
| 223 |
Y [j ], |
| 224 |
S [j ]); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
/*****************************************************************************/ |
| 229 |
|
| 230 |
real64 dng_spline_solver::EvaluateSlope (real64 x) const |
| 231 |
{ |
| 232 |
|
| 233 |
int32 count = (int32) X.size (); |
| 234 |
|
| 235 |
int32 lower = 1; |
| 236 |
int32 upper = count - 1; |
| 237 |
|
| 238 |
// Check for off each end of point list. |
| 239 |
|
| 240 |
if (x <= X [0]) |
| 241 |
{ |
| 242 |
x = X [0]; |
| 243 |
lower = 1; |
| 244 |
} |
| 245 |
|
| 246 |
else if (x >= X [count-1]) |
| 247 |
{ |
| 248 |
x = X [count-1]; |
| 249 |
lower = upper; |
| 250 |
} |
| 251 |
|
| 252 |
else |
| 253 |
{ |
| 254 |
|
| 255 |
// Binary search for the index. |
| 256 |
|
| 257 |
while (upper > lower) |
| 258 |
{ |
| 259 |
|
| 260 |
int32 mid = (lower + upper) >> 1; |
| 261 |
|
| 262 |
if (x == X [mid]) |
| 263 |
{ |
| 264 |
return Y [mid]; |
| 265 |
} |
| 266 |
|
| 267 |
if (x > X [mid]) |
| 268 |
lower = mid + 1; |
| 269 |
else |
| 270 |
upper = mid; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
DNG_ASSERT (upper == lower, |
| 275 |
"Binary search error in point list"); |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
int32 j = lower; |
| 280 |
|
| 281 |
return EvaluateSlopeSplineSegment (x, |
| 282 |
X [j - 1], |
| 283 |
Y [j - 1], |
| 284 |
S [j - 1], |
| 285 |
X [j ], |
| 286 |
Y [j ], |
| 287 |
S [j ]); |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
/*****************************************************************************/ |