patterncMinor
Parametrization of a curve
Viewed 0 times
curveparametrizationstackoverflow
Problem
For a set of 2D or 3D points \$\{\mathbf P_0, \mathbf P_1,\cdots,\mathbf P_n\}\$, their parameters \$\mathbf T = \{t_0,t_1,\cdots,t_n \}\$ could be computed as follow:
$$\begin{align}
t_0&=0\\
t_i&=\frac{\sum_{j=1}^{i}||\mathbf P_j- \mathbf P_{j-1}||}{\sum_{j=1}^{n}||\mathbf P_j-\mathbf P_{j-1}||} \qquad i=1,2,\cdots,n
\end{align}$$
Here is my C implementation:
Is a more efficient C implemetation of
$$\begin{align}
t_0&=0\\
t_i&=\frac{\sum_{j=1}^{i}||\mathbf P_j- \mathbf P_{j-1}||}{\sum_{j=1}^{n}||\mathbf P_j-\mathbf P_{j-1}||} \qquad i=1,2,\cdots,n
\end{align}$$
Here is my C implementation:
/********************************************************************
v1 = {v1(0),v1(1),...,v1(c-1)}
v2 = {v2(0),v2(1),...,v2(c-1)}
compute the 2-norm of the difference of two vector v1 and v2
*********************************************************************/
double vector_norm(double *v1, double *v2, int c) {
int i;
double res = 0.0;
for (i = 0; i < c; i++) {
res += pow(v1[i] - v2[i], 2);
}
return sqrt(res);
}
/********************************************************
In general, P is a matrix of dimensions {n + 1, c}
here, it was saved as a vector
********************************************************/
void calc_paras(double *P, int n, int c, double *paras) {
int i;
double len = 0.0;
double chord;
//compute the length and total length
paras[0] = 0.0;
for (i = 0; i < n; i++) {
chord = vector_norm(P + (i + 1) * c, P + i * c, c);
len += chord;
paras[i+1] = len;
}
//normalize the paras to [0,1]
for (i = 0; i <= n; i++) {
paras[i] /= len;
}
}Is a more efficient C implemetation of
calc_paras() possible?Solution
That is basically the same technique that I would use.
My only suggestion is to consistently use
for both loops, to match the stated definition.
My only suggestion is to consistently use
for (i = 1; i <= n; i++)for both loops, to match the stated definition.
Code Snippets
for (i = 1; i <= n; i++)Context
StackExchange Code Review Q#140022, answer score: 2
Revisions (0)
No revisions yet.