[vtkusers] gradient background direction
Florian Bruckner
e0425375 at gmail.com
Sat Sep 3 06:02:19 EDT 2011
hi again,
i now use the following code to generate a 2d actor with the wanted
colorgradient in a given direction:
background_source = vtk.vtkImageCanvasSource2D()
background_source.SetExtent(-max_x/2, max_x/2,
-max_y/2, max_y/2, 0, 0)
background_source.SetScalarTypeToUnsignedChar()
background_source.SetNumberOfScalarComponents(3)
background_source.SetDrawColor(255, 255, 255)
background_source.FillBox(0, max_x, 0, max_y)
background_source.Update()
array =
background_source.GetOutput().GetPointData().GetArray(0)
data = VN.vtk_to_numpy(array)
color1=[10,20,30]
color2=[150,160,170]
colorgrad=[1.0, 0.5]
norm = sqrt(colorgrad[0]**2 + colorgrad[1]**2)
colorgrad=[colorgrad[0]/norm, colorgrad[1]/norm]
L = (max_x/self.cols+1)*colorgrad[0] +
(max_y/self.rows+1)*colorgrad[1]
for j in range(max_y + 1):
for i in range(max_x + 1):
l =
(i%(max_x/self.cols+1))*colorgrad[0] +
(j%(max_y/self.rows+1))*colorgrad[1]
#l = i*colorgrad[0] + j*colorgrad[1]
data[j*(max_x-0+1)+i][0] =
color1[0]*l/L+color2[0]*(1-l/L)
data[j*(max_x-0+1)+i][1] =
color1[1]*l/L+color2[1]*(1-l/L)
data[j*(max_x-0+1)+i][2] =
color1[2]*l/L+color2[2]*(1-l/L)
background_actor = vtk.vtkImageActor()
background_actor.SetInput(background_source.GetOutput())
renderer = vtk.vtkRenderer()
renderer.SetBackground(0,0,0)
renderer.AddActor(background_actor)
....
it works but it has some serious drawbacks:
.) the for loops spend a lot of time if the resolution is big.
.) color seems to be interpolated between pixels by the renderer,
which leads to problems if the number of rows/cols != 1.
then you should have a sharp colortransition, but this is blured
out by the renderer
does anyone have a more convenient method? can i somehow speed up the
for loops using some python tricks?
thanks
FloB
2011/8/26 Dženan Zukić <dzenanz at gmail.com>:
> I think that you can set a custom bitmap as a background. In it you can make
> any kind of gradient you like (I guess it will be smoothly stretched).
>
> On Fri, Aug 26, 2011 at 10:42, Florian Bruckner <e0425375 at gmail.com> wrote:
>>
>> hi,
>>
>> is it somehow possible to set the direction of the gradient background?
>> I would need this to make different renderers in a renderer-window
>> visible to the user.
>>
>> Adding a box around the viewports would be another possibility, but i
>> also didn't find an easy solution for this! (where no additional
>> renderers / actors / ... have to be added, which could make the
>> application slower)
>>
>> thanks
>> FloB
>>
>>
>> _______________________________________________
>> 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
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
More information about the vtkusers
mailing list