Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Minor cleanup and optimization of Sketch #24964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/path_converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,9 @@ class Sketch
m_rand(0)
{
rewind(0);
const double d_M_PI = 3.14159265358979323846;
m_p_scale = (2.0 * d_M_PI) / (m_length * m_randomness);
m_log_randomness = 2.0 * log(m_randomness);
}

unsigned vertex(double *x, double *y)
Expand All @@ -1037,18 +1040,31 @@ class Sketch
// We want the "cursor" along the sine wave to move at a
// random rate.
double d_rand = m_rand.get_double();
double d_M_PI = 3.14159265358979323846;
m_p += pow(m_randomness, d_rand * 2.0 - 1.0);
double r = sin(m_p / (m_length / (d_M_PI * 2.0))) * m_scale;
// Original computation
// p += pow(k, 2*rand - 1)
// r = sin(p * c)
// x86 computes pow(a, b) as exp(b*log(a))
// First, move -1 out, so
// p' += pow(k, 2*rand)
// r = sin(p * c') where c' = c / k
// Next, use x86 logic (will not be worse on other platforms as
// the log is only computed once and pow and exp are, at worst,
// the same)
// So p+= exp(2*rand*log(k))
// lk = 2*log(k)
// p += exp(rand*lk)
m_p += exp(d_rand * m_log_randomness);
double den = m_last_x - *x;
double num = m_last_y - *y;
double len = num * num + den * den;
m_last_x = *x;
m_last_y = *y;
if (len != 0) {
len = sqrt(len);
*x += r * num / len;
*y += r * -den / len;
double r = sin(m_p * m_p_scale) * m_scale;
double roverlen = r / len;
*x += roverlen * num;
*y -= roverlen * den;
}
} else {
m_last_x = *x;
Expand Down Expand Up @@ -1083,6 +1099,8 @@ class Sketch
bool m_has_last;
double m_p;
RandomNumberGenerator m_rand;
double m_p_scale;
double m_log_randomness;
};

#endif // MPL_PATH_CONVERTERS_H