<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Hi Andy,</div><div class="gmail_quote"><br></div><div class="gmail_quote">Smart pointers shouldn't be causing this sort of issue, and from glancing at the example code they appear to be used correctly.</div><div class="gmail_quote"><br></div><div class="gmail_quote">Have you set breakpoints in these functions or added debug output to ensure that the correct functions are being called as-expected? I suspect that walking through the code with a debugger would shine some light on the issue.</div><div class="gmail_quote"><br></div><div class="gmail_quote">HTH,</div><div class="gmail_quote">Dave</div><div class="gmail_quote"><br></div><div class="gmail_quote">On Sun, Jun 5, 2016 at 4:47 PM, andyjk <span dir="ltr"><<a href="mailto:andrewkeeling@hotmail.com" target="_blank">andrewkeeling@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have a feeling I am using smart pointers wrongly in a class, and it is<br>
causing weird rendering results. Can anyone help ?<br>
I am trying to compare the distances between 2 polydatas using different<br>
techniques. One function measures the vertex to surface distance. The other<br>
takes lots of samples on the test polydata and measures the distance to the<br>
surface of the reference polydata. The test and reference polydatas do not<br>
change, and the numbers (mean distance etc ) given by the functions are<br>
correct. However, the display deteriorates with use.<br>
<br>
I guess I need to know how to completely reset the pipeline in the class,<br>
ensuring the members are wiped before being reused (polydata, actor, mapper<br>
and renderer), although I had hoped I could just reset the polydata witha<br>
DeepCopy, and the pipeline would update.<br>
<br>
I have a class and in the header  I declare :<br>
(QTCompare.h)<br>
<br>
vtkSmartPointer<vtkPolyDataMapper> resultMapper_;<br>
vtkSmartPointer<vtkRenderer> resultRenderer_;<br>
vtkSmartPointer<vtkActor> resultActor_;<br>
<br>
vtkSmartPointer<vtkPolyData> resultPolydata_;<br>
<br>
vtkSmartPointer<vtkFloatArray> distances_; // The scalar array holding the<br>
distances<br>
vtkSmartPointer<vtkScalarBarActor> scalarBar_; // To show the colour scale<br>
<br>
(I also declare two more polydatas : TestPolydata and targetPolydata, which<br>
are read in fine from a file)<br>
<br>
When the class is instantiated :<br>
(QTCompare.cpp)<br>
<br>
  resultPolydata_ = vtkSmartPointer<vtkPolyData>::New();<br>
  resultMapper_ = vtkSmartPointer<vtkPolyDataMapper>::New();<br>
  resultActor_ = vtkSmartPointer<vtkActor>::New();<br>
  resultMapper_->SetInputData(resultPolydata_);<br>
  resultActor_->SetMapper(resultMapper_);<br>
  resultRenderer_ = vtkSmartPointer<vtkRenderer>::New();<br>
  resultRenderer_->AddActor(resultActor_);<br>
  // VTK/Qt wedded<br>
  this->qvtkWidgetResult->GetRenderWindow()->AddRenderer(resultRenderer_);<br>
<br>
  // The distances array<br>
  distances_ = vtkSmartPointer<vtkFloatArray>::New();<br>
  scalarBar_ = vtkSmartPointer<vtkScalarBarActor>::New();<br>
<br>
<br>
So far, I have created the pipeline with a blank polydata. Then, when a<br>
certain button is pressed I do :<br>
<br>
void QtCompare::sampleVertices()<br>
{<br>
<br>
       txtOutput->append("Sampling vertices...");<br>
<br>
       vtkIdType nPoints = testPolydata_->GetNumberOfPoints();<br>
       if (nPoints == 0) return;<br>
       distances_->Initialize();<br>
       distances_->SetNumberOfValues(nPoints);<br>
<br>
       resultPolydata_->DeepCopy(testPolydata_);<br>
<br>
       // Create the tree<br>
       if (referencePolydata_->GetNumberOfCells() == 0) return;<br>
       vtkSmartPointer<vtkCellLocator> cellLocator =<br>
vtkSmartPointer<vtkCellLocator>::New();<br>
       cellLocator->SetDataSet(referencePolydata_);<br>
       cellLocator->BuildLocator();<br>
<br>
<br>
       txtOutput->append("Number of Samples : " + QString::number(nPoints));<br>
<br>
<br>
       // Main loop<br>
       for (vtkIdType i = 0; i < nPoints; i++) {<br>
<br>
             double testPoint[3];<br>
             testPolydata_->GetPoint(i, testPoint);<br>
<br>
             //Find the closest points to TestPoint<br>
             double closestPoint[3];//the coordinates of the closest point<br>
will be returned here<br>
             double closestPointDist2; //the squared distance to the closest<br>
point will be returned here<br>
             vtkIdType cellId; //the cell id of the cell containing the<br>
closest point will be returned here<br>
             int subId; //this is rarely used (in triangle strips only, I<br>
believe)<br>
             cellLocator->FindClosestPoint(testPoint, closestPoint, cellId,<br>
subId, closestPointDist2);<br>
<br>
             double distance = sqrt(closestPointDist2);<br>
<br>
             distances_->SetValue(i, (float)distance);<br>
<br>
       }<br>
       cout << "N scalars :" << this->distances_->GetSize() << endl;<br>
       // Add the distances to the result polydata (for colour)<br>
       resultPolydata_->GetPointData()->SetScalars(distances_);<br>
<br>
       // Get min and max<br>
       double range[2];<br>
       distances_->Modified();<br>
       distances_->GetRange(range);<br>
       txtOutput->append("Range of Samples : " +<br>
QString::number(range[0],'f',4) + " to " + QString::number(range[1],'f',4));<br>
       range[0] = 0.0; range[1] = 0.5;<br>
<br>
       // Add in the mapper and set up scalar colours<br>
       resultMapper_->ScalarVisibilityOn();<br>
       resultMapper_->SetScalarModeToUsePointData();<br>
       resultMapper_->SetColorModeToMapScalars();<br>
       resultMapper_->SetScalarRange(range[0], range[1]);<br>
       resultMapper_->Modified();<br>
<br>
       //resultActor_->SetMapper(resultMapper_);<br>
       resultActor_->GetProperty()->SetRepresentationToSurface();<br>
       resultActor_->Modified();<br>
<br>
<br>
       scalarBar_->SetTitle("Vertex Distances");<br>
       scalarBar_->SetNumberOfLabels(6);<br>
<br>
       vtkSmartPointer<vtkLookupTable> hueLUT =<br>
vtkSmartPointer<vtkLookupTable>::New();<br>
       hueLUT->SetNumberOfTableValues(10);<br>
       hueLUT->SetTableRange(range[0], range[1]);<br>
       hueLUT->SetHueRange(0.625, 0.0);<br>
       hueLUT->SetValueRange(1, 1);<br>
       hueLUT->SetSaturationRange(1, 1);<br>
       hueLUT->Build();<br>
<br>
       resultMapper_->SetLookupTable(hueLUT);<br>
       scalarBar_->SetLookupTable(hueLUT);<br>
<br>
       this->resultRenderer_->Clear();<br>
       this->resultRenderer_->AddActor2D(scalarBar_);<br>
       this->resultRenderer_->ResetCamera();<br>
       this->qvtkWidgetResult->GetRenderWindow()->Render();<br>
<br>
       float sd = standard_deviation(); // Reports mean and SD in the output<br>
window<br>
<br>
}<br>
<br>
This all works once, but I have another function (almost the same) which<br>
refills the class members distances_, resultPolydata_, resultMapper_ and<br>
resultActor_. In this function the pointrepresentation is set to points, and<br>
the polydata has many more points than the original.<br>
<br>
This function looks like :<br>
void QtCompare::uniformSample()<br>
{<br>
       txtOutput->append("Uniform sampling at 10 microns...");<br>
<br>
       // Sample points at 0.01mm intervals<br>
       vtkSmartPointer<vtkPolyDataPointSampler> pointSampler =<br>
vtkSmartPointer<vtkPolyDataPointSampler>::New();<br>
       pointSampler->SetDistance(.01);<br>
       pointSampler->SetInputData(testPolydata_);<br>
       pointSampler->Update();<br>
<br>
       vtkIdType nPoints = pointSampler->GetOutput()->GetNumberOfPoints();<br>
<br>
       if (nPoints == 0) return;<br>
       distances_->Initialize();<br>
       distances_->SetNumberOfValues(nPoints);<br>
<br>
       txtOutput->append("Number of Samples : " + QString::number(nPoints));<br>
<br>
       // Create a polydata made from all the samples<br>
       resultPolydata_->DeepCopy(pointSampler->GetOutput());<br>
<br>
       // Create the tree<br>
       if (referencePolydata_->GetNumberOfCells() == 0) return;<br>
       vtkSmartPointer<vtkCellLocator> cellLocator =<br>
vtkSmartPointer<vtkCellLocator>::New();<br>
       cellLocator->SetDataSet(referencePolydata_);<br>
       cellLocator->BuildLocator();<br>
<br>
       // Main loop<br>
       for (vtkIdType i = 0; i < nPoints; i++) {<br>
<br>
             double testPoint[3];<br>
             resultPolydata_->GetPoint(i, testPoint);<br>
<br>
<br>
             //Find the closest points to TestPoint<br>
             double closestPoint[3];//the coordinates of the closest point<br>
will be returned here<br>
             double closestPointDist2; //the squared distance to the closest<br>
point will be returned here<br>
             vtkIdType cellId; //the cell id of the cell containing the<br>
closest point will be returned here<br>
             int subId; //this is rarely used (in triangle strips only, I<br>
believe)<br>
             cellLocator->FindClosestPoint(testPoint, closestPoint, cellId,<br>
subId, closestPointDist2);<br>
<br>
             double distance = sqrt(closestPointDist2);<br>
<br>
             distances_->SetValue(i, (float)distance);<br>
<br>
       }<br>
<br>
       // Add the distances to the result polydata (for colour)<br>
       resultPolydata_->GetPointData()->SetScalars(distances_);<br>
<br>
       // Get min and max<br>
       double range[2];<br>
       distances_->Modified();<br>
       distances_->GetRange(range);<br>
       txtOutput->append("Range of Samples : " +<br>
QString::number(range[0],'f',4) + " to " + QString::number(range[1],'f',4));<br>
<br>
       // Add in the mapper and set up scalar colours<br>
       resultMapper_->ScalarVisibilityOn();<br>
       resultMapper_->SetScalarModeToUsePointData();<br>
       resultMapper_->SetColorModeToMapScalars();<br>
       resultMapper_->SetScalarRange(range[0], range[1]);<br>
       resultMapper_->Modified();<br>
<br>
       resultActor_->SetMapper(resultMapper_);<br>
       resultActor_->Modified();<br>
       resultActor_->GetProperty()->SetRepresentationToPoints();<br>
       resultActor_->Modified();<br>
<br>
       scalarBar_->SetTitle("Uniform Sampling Distances");<br>
       scalarBar_->SetNumberOfLabels(6);<br>
<br>
       vtkSmartPointer<vtkLookupTable> hueLUT =<br>
vtkSmartPointer<vtkLookupTable>::New();<br>
       hueLUT->SetNumberOfTableValues(10);<br>
       hueLUT->SetTableRange(range[0], range[1]);<br>
       hueLUT->SetHueRange(0.3, 0.0);<br>
       hueLUT->SetValueRange(1, 1);<br>
       hueLUT->SetSaturationRange(1, 1);<br>
       hueLUT->Build();<br>
<br>
       resultMapper_->SetLookupTable(hueLUT);<br>
       scalarBar_->SetLookupTable(hueLUT);<br>
<br>
       txtOutput->append(QString::number(<br>
resultActor_->GetReferenceCount()));<br>
<br>
       this->resultRenderer_->Clear();<br>
       this->resultRenderer_->AddActor2D(scalarBar_);<br>
       this->resultRenderer_->ResetCamera();<br>
       this->qvtkWidgetResult->GetRenderWindow()->Render();<br>
<br>
<br>
       float sd = standard_deviation(); // Reports mean and SD in the output<br>
window<br>
<br>
<br>
}<br>
<br>
<br>
This also works fine. Now the strange bit. If I call the first function<br>
again, everything displays as expected. BUT, if I then call the first<br>
function again (so nothing should change) the data from the second function<br>
gets displayed. This toggles every time I call the first function! The<br>
values given by my ‘standard_deviation()’ function remain correct<br>
throughout, so the maths is being done on the correct data each time, but<br>
for some reason the display keeps changing its source data!<br>
<br>
Does anyone know what I am doing wrong ?<br>
<br>
Thanks<br>
<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://vtk.1045678.n5.nabble.com/Weird-Render-results-with-smartpointer-in-class-tp5738563.html" rel="noreferrer" target="_blank">http://vtk.1045678.n5.nabble.com/Weird-Render-results-with-smartpointer-in-class-tp5738563.html</a><br>
Sent from the VTK - Users mailing list archive at Nabble.com.<br>
_______________________________________________<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" rel="noreferrer" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Search the list archives at: <a href="http://markmail.org/search/?q=vtkusers" rel="noreferrer" target="_blank">http://markmail.org/search/?q=vtkusers</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/vtkusers" rel="noreferrer" target="_blank">http://public.kitware.com/mailman/listinfo/vtkusers</a><br>
</blockquote></div><br></div></div>