<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif;">
<div>
<div>There are a few examples on the Wiki page that could be contorted into what you were looking for, some of it is not very straightforward for beginners with VTK though. The following is a python script that could be run from the command line with “python
 display_stl_with_color.py your_stl_file.stl” It isn’t hard to convert this to C if that is what you were working in.</div>
<div><br>
</div>
<div>Here starts the script:</div>
<div><br>
</div>
<div>
<div># A lot of this was taken from the example on the Wiki:</div>
<div># http://www.vtk.org/Wiki/VTK/Examples/Cxx/Meshes/ColoredElevationMap</div>
<div><br>
</div>
<div>import vtk, sys</div>
<div><br>
</div>
<div># Read in your STL file</div>
<div>f = vtk.vtkSTLReader()</div>
<div>f.SetFileName(sys.argv[1])</div>
<div>f.Update() # This is necessary to have the data ready to read.</div>
<div><br>
</div>
<div># The vtkSTLReader reads in your file as a vtkPolyData, 'obj' is a reference to</div>
<div># that output. I'm using the bounds that are automatically calculated during</div>
<div># the import phase to give a range for the height of the points in the file.</div>
<div># I believe that the bounds are (xmin, xmax, ymin, ymax, zmin, zmax).</div>
<div>obj = f.GetOutputDataObject(0)</div>
<div>min_z, max_z = obj.GetBounds()[4:]</div>
<div><br>
</div>
<div># I am creating a lookup table to correspond to the height field. I am using</div>
<div># the default values. Remember that the lookup table is a rather complex and</div>
<div># handy object, and there are lots of options to set if you need something</div>
<div># special.</div>
<div>lut = vtk.vtkLookupTable()</div>
<div>lut.SetTableRange(min_z, max_z)</div>
<div>lut.Build()</div>
<div><br>
</div>
<div># This is an array that I am creating to store the heights of the points. I</div>
<div># will use this as a scalar field on the 'obj' so that the lookup table can be</div>
<div># used to color it. You could obviously make the array anything you wanted,</div>
<div># such as ‘x’ or ‘y’ or squared distance from some other point, for instance.</div>
<div>heights = vtk.vtkDoubleArray()</div>
<div>heights.SetName("Z_Value")</div>
<div><br>
</div>
<div># Loop through the points in the vtkPolyData and record the height in the</div>
<div># 'heights' array.</div>
<div>for i in range(obj.GetNumberOfPoints()):</div>
<div>z = obj.GetPoint(i)[-1]</div>
<div>heights.InsertNextValue(z)</div>
<div><br>
</div>
<div># Add this array to the point data as a scalar.</div>
<div>obj.GetPointData().SetScalars(heights)</div>
<div><br>
</div>
<div># Visualization stuff ... you need to tell the mapper about the scalar field</div>
<div># and the lookup table. The rest of this is pretty standard stuff.</div>
<div>mapper = vtk.vtkPolyDataMapper()</div>
<div>mapper.SetInputDataObject(obj)</div>
<div>mapper.SetScalarRange(min_z, max_z)</div>
<div>mapper.SetLookupTable(lut)</div>
<div><br>
</div>
<div>actor = vtk.vtkActor()</div>
<div>actor.SetMapper(mapper)</div>
<div><br>
</div>
<div>renderer = vtk.vtkRenderer()</div>
<div>renderer.AddActor(actor)</div>
<div>renderer.SetBackground(.1, .2, .4)</div>
<div><br>
</div>
<div>renw = vtk.vtkRenderWindow()</div>
<div>renw.AddRenderer(renderer)</div>
<div><br>
</div>
<div>iren = vtk.vtkRenderWindowInteractor()</div>
<div>iren.SetRenderWindow(renw)</div>
<div><br>
</div>
<div>renw.Render()</div>
<div>iren.Start()</div>
</div>
</div>
<div><br>
</div>
<span id="OLK_SRC_BODY_SECTION">
<div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt">
<span style="font-weight:bold">From: </span>vtkusers <<a href="mailto:vtkusers-bounces@vtk.org">vtkusers-bounces@vtk.org</a>> on behalf of Anusha Balasubramoniam <<a href="mailto:anushaba@buffalo.edu">anushaba@buffalo.edu</a>><br>
<span style="font-weight:bold">Date: </span>Monday, July 13, 2015 at 7:17 AM<br>
<span style="font-weight:bold">To: </span>"<a href="mailto:vtkusers@vtk.org">vtkusers@vtk.org</a>" <<a href="mailto:vtkusers@vtk.org">vtkusers@vtk.org</a>><br>
<span style="font-weight:bold">Subject: </span>[vtkusers] Color mapping on stl file<br>
</div>
<div><br>
</div>
<div>
<div>
<div dir="ltr">Dear All,
<div><br>
</div>
<div>I am not able to get a color map on an stl file.</div>
<div>Please, kindly help.</div>
<div>I am not able to figure out how to do it.</div>
<div><br>
</div>
<div>Thank you,</div>
<div>Anusha</div>
</div>
</div>
</div>
</span>
</body>
</html>