[vtkusers] Color PolyData based on VolumeData
Andras Lasso
lasso at queensu.ca
Mon Dec 3 01:50:06 EST 2018
You can copy voxel values from a volume to a polydata using vtkProbeFilter.
However, this won't help you if you obtained your polydata using a vtkContourFilter, because the contour filter generates isosurface, which means all extracted points have the same intensity value in the input volume. No matter what transfer function you apply, you'll always end up with a solid colored surface.
It is a common desire to replace volume rendering by displaying a colored surface, but this is not feasible (https://discourse.slicer.org/t/save-volume-rendering-as-stl-file/524/20).
If your goal is 3D printing then you may use voxel printing technique, which allows printing directly from volume rendering, without segmenting any surfaces (https://discourse.slicer.org/t/printing-volume-renderings-in-plastic/3017).
If your goal is volume-rendering-like display (for example, you want to show "volume rendering" in Unity) then you need to use an actual volume renderer.
Andras
-----Original Message-----
From: vtkusers <vtkusers-bounces at public.kitware.com> On Behalf Of Andaharoo
Sent: Sunday, December 2, 2018 11:27 PM
To: vtkusers at vtk.org
Subject: Re: [vtkusers] Color PolyData based on VolumeData
So, if I understand right, you want to color the scalar/whatever field onto the poly? Just provide vtkPolyData with scalars and set the polyData's mapper scalar visibility to on.
If you give it 1 component scalars you can give the mapper a color function to map them. If you give it 3 component uchar it should just display them as color (as long as scalar visibility is on). Like so:
// Create the scalars array
vtkDoubleArray* scalars = vtkDoubleArray::New();
scalars->SetName("Scalars");
// For every point in the poly
for (vtkIdType i = 0; i < outputPoly->GetNumberOfPoints(); i++) {
double pt[3];
outputPoly->GetPoint(i, pt);
scalars->InsertTuple1(i, trilinearSamplePoint(inputImage, pt[0], pt[1], pt[2], 1, 0)); } // Add the scalar data to the poly data
outputPoly->GetPointData()->AddArray(scalars);
outputPoly->GetPointData()->SetActiveScalars("Scalars");
This just goes through all the points in the polygon and gets the color from the image. Getting the color at a point in an image can be done by simply using the nearest color from the image (nearest neighbor) or better would be to use trilinear interpolation of the 8 nearest colors which I used in my example. Also note my code expects float image input:
static const int calcIndex(int x, int y, int z, int width, int height) { return x + width * (y + height * z); }
static float trilinearSamplePoint(vtkImageData* imageData, float x, float y, float z, int numComps, int comp) {
float* imagePtr = static_cast<float*>(imageData->GetScalarPointer());
double* spacing = imageData->GetSpacing();
double* origin = imageData->GetOrigin();
int* dim = imageData->GetDimensions();
// We assume point x, y, z is in the image
int xi = (x - origin[0]) / spacing[0];
int yi = (y - origin[1]) / spacing[1];
int zi = (z - origin[2]) / spacing[2];
// Get the intensities at the 8 nearest neighbors
float i000 = imagePtr[calcIndex(xi, yi, zi, dim[0], dim[1]) * numComps + comp];
float i100 = imagePtr[calcIndex(xi + 1, yi, zi, dim[0], dim[1]) * numComps
+ comp];
float i110 = imagePtr[calcIndex(xi + 1, yi + 1, zi, dim[0], dim[1] * numComps + comp)];
float i010 = imagePtr[calcIndex(xi, yi + 1, zi, dim[0], dim[1]) * numComps
+ comp];
float i001 = imagePtr[calcIndex(xi, yi, zi + 1, dim[0], dim[1]) * numComps
+ comp];
float i101 = imagePtr[calcIndex(xi + 1, yi, zi + 1, dim[0], dim[1]) * numComps + comp];
float i111 = imagePtr[calcIndex(xi + 1, yi + 1, zi + 1, dim[0], dim[1]) * numComps + comp];
float i011 = imagePtr[calcIndex(xi, yi + 1, zi + 1, dim[0], dim[1]) * numComps + comp];
// Get the fractional/unit distance from nearest neighbor 000
float rx = xi * spacing[0] + origin[0]; // Position of node
rx = (x - rx) / spacing[0]; // (Node - actual point position) / voxel width
float ry = yi * spacing[1] + origin[1];
ry = (y - ry) / spacing[1];
float rz = zi * spacing[2] + origin[2];
rz = (z - rz) / spacing[2];
// Now we do the trilinear interpolation
float ax = i000 + (i100 - i000) * rx;
float bx = i010 + (i110 - i010) * rx;
float cy = ax + (bx - ax) * ry;
float dx = i001 + (i101 - i001) * rx;
float ex = i011 + (i111 - i011) * rx;
float fy = dx + (ex - dx) * ry;
float gz = cy + (fy - cy) * rz;
return gz;
}
--
Sent from: https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fvtk.1045678.n5.nabble.com%2FVTK-Users-f1224199.html&data=02%7C01%7Classo%40queensu.ca%7C34224811bdf846b9c4a808d658d7898f%7Cd61ecb3b38b142d582c4efb2838b925c%7C1%7C0%7C636794080091371821&sdata=iziee8ltwEAWna3RvM3dma7A3p94NWMHCfIwclRpR0I%3D&reserved=0
_______________________________________________
Powered by https://na01.safelinks.protection.outlook.com/?url=www.kitware.com&data=02%7C01%7Classo%40queensu.ca%7C34224811bdf846b9c4a808d658d7898f%7Cd61ecb3b38b142d582c4efb2838b925c%7C1%7C0%7C636794080091371821&sdata=6Nv%2FWdIIIaOq%2B3peZlXxuGdAwkOwzORn7AfWthXw5Xo%3D&reserved=0
Visit other Kitware open-source projects at https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.kitware.com%2Fopensource%2Fopensource.html&data=02%7C01%7Classo%40queensu.ca%7C34224811bdf846b9c4a808d658d7898f%7Cd61ecb3b38b142d582c4efb2838b925c%7C1%7C0%7C636794080091381830&sdata=oEIpua2iKsLpUdJaY%2BmQdOhCHfusGYUZDRd3Ru67Qoo%3D&reserved=0
Please keep messages on-topic and check the VTK FAQ at: https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.vtk.org%2FWiki%2FVTK_FAQ&data=02%7C01%7Classo%40queensu.ca%7C34224811bdf846b9c4a808d658d7898f%7Cd61ecb3b38b142d582c4efb2838b925c%7C1%7C0%7C636794080091381830&sdata=7h18pSmCa3LapcNQJsqqxU7kyWVyaQzKbPYZPMCbDiw%3D&reserved=0
Search the list archives at: https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmarkmail.org%2Fsearch%2F%3Fq%3Dvtkusers&data=02%7C01%7Classo%40queensu.ca%7C34224811bdf846b9c4a808d658d7898f%7Cd61ecb3b38b142d582c4efb2838b925c%7C1%7C0%7C636794080091381830&sdata=luU4EgKZZ9nD1OOxN5EGfBpA5HnK%2FyuVoN6C7PE%2Bzhc%3D&reserved=0
Follow this link to subscribe/unsubscribe:
https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpublic.kitware.com%2Fmailman%2Flistinfo%2Fvtkusers&data=02%7C01%7Classo%40queensu.ca%7C34224811bdf846b9c4a808d658d7898f%7Cd61ecb3b38b142d582c4efb2838b925c%7C1%7C0%7C636794080091381830&sdata=URr5vqvvkUuWmwaPIIGXXJzsmW2LFrYs7Z7jDcvt4ek%3D&reserved=0
More information about the vtkusers
mailing list