再贴一个设置某一特定scalar的color后,自动插值其余scalar的color的例子:
- #include <string>
- #include <vtkMath.h>
- #include <vtkLookupTable.h>
- #include <vtkColorTransferFunction.h>
- #include <vtkScalarBarActor.h>
- #include <vtkRenderer.h>
- #include <vtkRenderWindow.h>
- #include <vtkRenderWindowInteractor.h>
- #include <vtkInteractorStyleTrackballCamera.h>
- #include <vtkCallbackCommand.h>
- vtkRenderer *ren;
- vtkRenderWindow *win;
- vtkRenderWindowInteractor *iren;
- vtkInteractorStyleTrackballCamera *style;
- vtkCallbackCommand *callback;
- vtkColorTransferFunction *lut1;
- vtkScalarBarActor *bar1;
- void OnRightButtonPress(vtkObject *, unsigned long, void *, void *)
- {
- double red=(rand()%10)/10.0;
- lut1->AddRGBPoint(13.5, red, 0, 0);
- printf("point num: %d, red value: %f\n", lut1->GetSize(), red);
- win->Render();
- }
- void Setup()
- {
- ren=vtkRenderer::New();
- win=vtkRenderWindow::New();
- win->AddRenderer(ren);
- iren=vtkRenderWindowInteractor::New();
- iren->SetRenderWindow(win);
- style=vtkInteractorStyleTrackballCamera::New();
- iren->SetInteractorStyle(style);
- style->SetInteractor(iren);
- callback=vtkCallbackCommand::New();
- callback->SetCallback(OnRightButtonPress);
- style->AddObserver(vtkCommand::RightButtonPressEvent, callback);
- ren->SetBackground(0.5, 0.5, 0.5);
- lut1=vtkColorTransferFunction::New();
- bar1=vtkScalarBarActor::New();
- }
- void mainloop()
- {
- win->Render();
- iren->Start();
- }
- void DeleteAll()
- {
- ren->Delete();
- win->Delete();
- iren->Delete();
- style->Delete();
- callback->Delete();
- lut1->Delete();
- bar1->Delete();
- }
- void Description()
- {
- std::string des="点击右键设置scalar13.5的红颜色值\n";
- cout<<des;
- }
- void Result()
- {
- std::string res="本例说明:\n\
- vtkColorTransferFunction可以用于设置对应某个scalar的color,然后插值其余scalar对应的color\n\
- 对应同一个scalar调用AddRGBPoint,并不会增加点的个数\n\n按回车退出";
- cout<<res;
- getchar();
- }
- int main()
- {
- Description();
- Setup();
- double color1[3]={1, 1, 0}, hsv1[3];
- double color2[3]={0, 1, 1}, hsv2[3];
- vtkMath::RGBToHSV(color1, hsv1);
- vtkMath::RGBToHSV(color2, hsv2);
- lut1->AddRGBPoint(13, 0, 0, 0);
- lut1->AddRGBPoint(14, 0, 0, 0);
- bar1->SetLookupTable(lut1);
- const char *title="Scalar & Color";
- bar1->SetTitle(title);
- bar1->GetPositionCoordinate()-> SetValue(0.1, 0.1);
- bar1->SetOrientationToVertical();
- bar1->SetWidth (0.1);
- bar1->SetHeight (0.8);
- ren->AddActor(bar1);
- mainloop();
- DeleteAll();
- Result();
- return 0;
- }
复制代码
|