<div dir="ltr">Hi all,<br><div><br></div><div>We use a lot vtkParametricSpline here, with quite a lot of points, ~4000<br>With that number of points, the method "Initialize()" takes around 0.7 seconds (vtk 5.8, linux, intel Core i7@3.0 Ghz)</div>
<div><br></div><div>After some hack, this time is reduced to : 0.001 seconds, (700x speed up !), for the exact same result<br><br>Explanations :</div><div>While digging into the code, I've noticed that for each added point in vtkSpline, there is a call of the method SortAndUpdateRange() in vtkPiecewiseFunction, and after that, a search through all the nodes in order to return the array index of the point. </div>
<div>Now in vtkParametricSpline, all the point are added with something like :<br>/********************************************************************/<br> for ( len = 0.0, i = 0; i < npts; ++i )<br><div> {</div>
<div> this->Points->GetPoint(i,x);</div><div> len += sqrt(vtkMath::Distance2BetweenPoints(x,xPrev));</div><div><span style="white-space:pre-wrap"> </span>this->XSpline->AddPoint(len,x[0]);<br></div>
<div>
<span style="white-space:pre-wrap"> </span>this->YSpline->AddPoint(len,x[1]);</div><div><span style="white-space:pre-wrap"> </span>this->ZSpline->AddPoint(len,x[2]);</div><div> xPrev[0]=x[0]; xPrev[1]=x[1]; xPrev[2]=x[2];</div>
<div> }<br>/********************************************************************/</div><div><br>2 remarks :<br>- we don't need to sort because all the points are added already sorted (the variable "len" can only increase), or at least we should sort only once at the end</div>
<div>- we don't use the return value (no need to search)<br><br>So I've added a method in vtkSpline which looks like :</div><div>/********************************************************************/<br></div><div>
void vtkSpline::AddPoints (double* t, double* x, int n)<br></div><div>{</div><div> this->PiecewiseFunction->AddPoints (t, x, n);</div><div>}</div><div>/********************************************************************/<br>
</div><div><br></div></div><div>And another one in vtkPiecewiseFunction which looks like this :</div><div>/********************************************************************/<br></div><div><div>void vtkPiecewiseFunction::AddPoints( double* x, double* y, int n )<br>
</div><div>{</div><div> for( int i=0; i<n; i++ ){<span style="white-space:pre-wrap"> </span></div><div> vtkPiecewiseFunctionNode *node = new vtkPiecewiseFunctionNode;<br></div><div> node->X = x[i];</div>
<div> node->Y = y[i];</div><div> node->Sharpness = 0.0;</div><div> node->Midpoint = 0.5;</div><div> this->Internal->Nodes.push_back(node);</div><div> }</div><div> this->SortAndUpdateRange();<span style="white-space:pre-wrap"> </span></div>
<div>}</div><div>/********************************************************************/<br></div><div><br></div>Finally the modified vtkParametricSpline Initialize() method looks like :</div><div>/********************************************************************/<br>
</div><div><div> double lengths[npts];</div><div> double pointx[npts];</div><div> double pointy[npts];</div><div> double pointz[npts];</div><div> for ( len = 0.0, i = 0; i < npts; ++i )</div><div> {</div>
<div> this->Points->GetPoint(i,x);</div><div> len += sqrt(vtkMath::Distance2BetweenPoints(x,xPrev));</div><div> lengths[i] = len;</div><div> pointx[i] = x[0];</div><div> pointy[i] = x[1];</div>
<div> pointz[i] = x[2];<span style="white-space:pre-wrap"> </span></div><div> xPrev[0]=x[0]; xPrev[1]=x[1]; xPrev[2]=x[2];</div><div> }</div><div> this->XSpline->AddPoints(lengths,pointx,npts);</div>
<div> this->YSpline->AddPoints(lengths,pointy,npts);</div><div> this->ZSpline->AddPoints(lengths,pointz,npts);</div><div> }</div><div>/********************************************************************/<br>
</div><br>Conclusion :<br>I know 4000 points for a spline is somewhat stupid, but the gain is significant and maybe this can help some other people. Also, maybe this can be useful for TransferFunction with a big number of points. And last but no least, on windows, the underlying std::sort is especially slow in Debug, because visual studio enables some heap debugging. So the gain in performance may even be higher in that case.</div>
<div><br>Cheers,<br>Simon<br><br><br></div>-- <br>------------------------------------------------------------------<br>Simon Esneault<div>13 rue Vasselot<br>35000 Rennes, France<br>Tel : 06 64 61 30 94<br>Mail : <a href="mailto:simon.esneault@gmail.com" target="_blank" title="[GMCP] Compose a new mail to simon.esneault@gmail.com" onclick="window.open('https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&to=simon.esneault@gmail.com','Compose new message','width=640,height=480');return false" rel="noreferrer">simon.esneault@gmail.com</a><br>
------------------------------------------------------------------</div>
</div>