Re: Color PolyData based on VolumeData

Andaharoo <[email protected]>
Newsgroups gmane.comp.lib.vtk.user
Message-ID <[email protected]>
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: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.