【VTK三维重建-体绘制】第五期 vtkLODProp3D

发布时间:2024年01月04日

很高兴在雪易的CSDN遇见你?

VTK技术爱好者 QQ:870202403


前言

本文分享VTK中体绘制中的vtkLODProp3D对象,希望对各位小伙伴有所帮助!

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的点赞就是我的动力(^U^)ノ~YO

??


1.?vtkLODProp3D

? ? ? ? vtkLODProp3D与vtkVolume用法类似,两者均继承自vtkProp3D。但vtkLODProp3D支持多个Mapper、Property和Texture对象,并由它选择Mapper对象实现绘制。例如,当绘制一个数据量非常大的不规则网格数据时,可以添加一个vtkPolyDataMapper来渲染一个表面模型,作为最低级别分辨率的渲染;然后将数据采样为vtkImageData数据,并添加一个vtkVolumeTextureMapper3D进行体绘制,作为一个中等级别渲染;最后通过ZSweep技术(vtkUnstructureGridVolumeZSWeepMapper)渲染原始数据,作为最高级别的渲染。vtkLODProp3D在渲染过程中,会为每一个Mapper估计一个渲染时间,并选择一个最优的实现渲染。

? ? ? ? 其重要参数有:

1.1 获取边界包围盒

  /**
   * Standard vtkProp method to get 3D bounds of a 3D prop
   */
  double* GetBounds() VTK_SIZEHINT(6) override;
  void GetBounds(double bounds[6]) { this->vtkProp3D::GetBounds(bounds); }

1.2 添加不同级别的Mapper

  //@{
  /**
   * 添加不同级别的的映射器、属性、背面属性、纹理和渲染时间。
   * 属性和纹理字段可以设置为NULL(其他方法包含在不允许NULL变量的脚本访问中)。
   * time字段可以设置为0.0,表示没有提供渲染时间的初始猜测。
   * 返回的整数值是一个ID,稍后可以使用该ID删除该LOD,或者将其设置为所选LOD。
   * Add a level of detail with a given mapper, property, backface property,
   * texture, and guess of rendering time.  The property and texture fields
   * can be set to NULL (the other methods are included for script access
   * where null variables are not allowed). The time field can be set to 0.0
   * indicating that no initial guess for rendering time is being supplied.
   * The returned integer value is an ID that can be used later to delete
   * this LOD, or set it as the selected LOD.
   */
  int AddLOD(vtkMapper* m, vtkProperty* p, vtkProperty* back, vtkTexture* t, double time);
  int AddLOD(vtkMapper* m, vtkProperty* p, vtkTexture* t, double time);
  int AddLOD(vtkMapper* m, vtkProperty* p, vtkProperty* back, double time);
  int AddLOD(vtkMapper* m, vtkProperty* p, double time);
  int AddLOD(vtkMapper* m, vtkTexture* t, double time);
  int AddLOD(vtkMapper* m, double time);
  int AddLOD(vtkAbstractVolumeMapper* m, vtkVolumeProperty* p, double time);
  int AddLOD(vtkAbstractVolumeMapper* m, double time);
  int AddLOD(vtkImageMapper3D* m, vtkImageProperty* p, double time);
  int AddLOD(vtkImageMapper3D* m, double time);
  //@}

1.3 获取LOD数量及当前Index

  //@{
  /**
   * Get the current number of LODs.
   */
  vtkGetMacro(NumberOfLODs, int);
  //@}

  //@{
  /**
   * Get the current index, used to determine the ID of the next LOD that is
   * added.  Useful for guessing what IDs have been used (with NumberOfLODs,
   * without depending on the constructor initialization to 1000.
   */
  vtkGetMacro(CurrentIndex, int);
  //@}

1.4 删除指定Index的LOD

  /**
   * Delete a level of detail given an ID. This is the ID returned by the
   * AddLOD method
   */
  void RemoveLOD(int id);

1.5 获取指定Index的属性

  //@{
  /**
   * Methods to set / get the property of an LOD. Since the LOD could be
   * a volume or an actor, you have to pass in the pointer to the property
   * to get it. The returned property will be NULL if the id is not valid,
   * or the property is of the wrong type for the corresponding Prop3D.
   */
  void SetLODProperty(int id, vtkProperty* p);
  void GetLODProperty(int id, vtkProperty** p);
  void SetLODProperty(int id, vtkVolumeProperty* p);
  void GetLODProperty(int id, vtkVolumeProperty** p);
  void SetLODProperty(int id, vtkImageProperty* p);
  void GetLODProperty(int id, vtkImageProperty** p);
  //@}

1.6 设置/获取Mapper

  //@{
  /**
   * Methods to set / get the mapper of an LOD. Since the LOD could be
   * a volume or an actor, you have to pass in the pointer to the mapper
   * to get it. The returned mapper will be NULL if the id is not valid,
   * or the mapper is of the wrong type for the corresponding Prop3D.
   */
  void SetLODMapper(int id, vtkMapper* m);
  void GetLODMapper(int id, vtkMapper** m);
  void SetLODMapper(int id, vtkAbstractVolumeMapper* m);
  void GetLODMapper(int id, vtkAbstractVolumeMapper** m);
  void SetLODMapper(int id, vtkImageMapper3D* m);
  void GetLODMapper(int id, vtkImageMapper3D** m);
  //@}

  /**
   * Get the LODMapper as an vtkAbstractMapper3D.  It is the user's
   * respondibility to safe down cast this to a vtkMapper or vtkVolumeMapper
   * as appropriate.
   */
  vtkAbstractMapper3D* GetLODMapper(int id);

1.7 获取Backface属性

  //@{
  /**
   * Methods to set / get the backface property of an LOD. This method is only
   * valid for LOD ids that are Actors (not Volumes)
   */
  void SetLODBackfaceProperty(int id, vtkProperty* t);
  void GetLODBackfaceProperty(int id, vtkProperty** t);
  //@}

1.8 获取纹理

  //@{
  /**
   * Methods to set / get the texture of an LOD. This method is only
   * valid for LOD ids that are Actors (not Volumes)
   */
  void SetLODTexture(int id, vtkTexture* t);
  void GetLODTexture(int id, vtkTexture** t);
  //@}

1.9 EnableLOD

  //@{
  /**
   * Enable / disable a particular LOD. If it is disabled, it will not
   * be used during automatic selection, but can be selected as the
   * LOD if automatic LOD selection is off.
   */
  void EnableLOD(int id);
  void DisableLOD(int id);
  int IsLODEnabled(int id);
  //@}

1.10 设置LOD的级别

  //@{
  /**
   * 设置特定LOD的级别。当选择一个LOD进行渲染时,因为它在分配的时间内具有最大的渲染时间,
   * 然后检查所有LOD,看看是否有任何LOD可以渲染得更快,但具有较低(更高分辨率/更好)的级别。
   * 该数量为双精度,以确保可以插入2和3之间
   * Set the level of a particular LOD. When a LOD is selected for
   * rendering because it has the largest render time that fits within
   * the allocated time, all LOD are then checked to see if any one can
   * render faster but has a lower (more resolution/better) level.
   * This quantity is a double to ensure that a level can be inserted
   * between 2 and 3.
   */
  void SetLODLevel(int id, double level);
  double GetLODLevel(int id);
  double GetLODIndexLevel(int index);
  //@}

1.11 获取指定Index的LOD渲染时间

  //@{
  /**
   * Access method that can be used to find out the estimated render time
   * (the thing used to select an LOD) for a given LOD ID or index.
   * Value is returned in seconds.
   */
  double GetLODEstimatedRenderTime(int id);
  double GetLODIndexEstimatedRenderTime(int index);
  //@}

1.12 开启/关闭自动选择渲染的LOD

  //@{
  /**
   * 开启/关闭自动选择LOD。默认为开启。
   * 如果它是关闭的,那么无论呈现时间或所需的更新速率如何,都会呈现SelectedLODID。
   * Turn on / off automatic selection of LOD.
   * This is on by default. If it is off, then the SelectedLODID is
   * rendered regardless of rendering time or desired update rate.
   */
  vtkSetClampMacro(AutomaticLODSelection, vtkTypeBool, 0, 1);
  vtkGetMacro(AutomaticLODSelection, vtkTypeBool);
  vtkBooleanMacro(AutomaticLODSelection, vtkTypeBool);
  //@}

1.13 设置选择的ID

  //@{
  /**
   * Set the id of the LOD that is to be drawn when automatic LOD selection
   * is turned off.
   */
  vtkSetMacro(SelectedLODID, int);
  vtkGetMacro(SelectedLODID, int);
  //@}

1.14 返回对象

  //@{
  /**
   * For some exporters and other other operations we must be
   * able to collect all the actors or volumes. These methods
   * are used in that process.
   */
  void GetActors(vtkPropCollection*) override;
  void GetVolumes(vtkPropCollection*) override;
  //@}

1.15 开始/关闭自动拾取LOD

  //@{
  /**
   * 开启/关闭自动Pick的LOD。默认是开启的。
   * 如果它是关闭的,那么无论呈现时间或所需的更新速率如何,都会呈现SelectedLODID。
   * Turn on / off automatic selection of picking LOD.
   * This is on by default. If it is off, then the SelectedLODID is
   * rendered regardless of rendering time or desired update rate.
   */
  vtkSetClampMacro(AutomaticPickLODSelection, vtkTypeBool, 0, 1);
  vtkGetMacro(AutomaticPickLODSelection, vtkTypeBool);
  vtkBooleanMacro(AutomaticPickLODSelection, vtkTypeBool);
  //@}

1.16 设置选择Pick的LOD Index

  //@{
  /**
   * Set the id of the LOD that is to be used for picking when automatic
   * LOD pick selection is turned off.
   */
  void SetSelectedPickLODID(int id);
  vtkGetMacro(SelectedPickLODID, int);
  //@}

注意:vtkVolumeTextureMapper3D在9.0.3及以后的版本中已不存在,由vtkOpenGLGPUVolumeRayCastMapper代替。

2. 示例

??? ??

#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkLODProp3D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkCallbackCommand.h>
#include <vtkProperty.h>

void RefreshCallback( vtkObject* vtkNotUsed(caller),
                      long unsigned int vtkNotUsed(eventId),
                      void* clientData,
                      void* vtkNotUsed(callData) )
{
  vtkSmartPointer<vtkLODProp3D> lodProp = 
    static_cast<vtkLODProp3D*>(clientData);
  std::cout << "Last rendered LOD: " << lodProp->GetLastRenderedLODID() << std::endl;
}

int main (int, char *[])
{
  // High res sphere
  vtkSmartPointer<vtkSphereSource> highResSphereSource = 
    vtkSmartPointer<vtkSphereSource>::New();
  int res = 100;
  highResSphereSource->SetThetaResolution(res);
  highResSphereSource->SetPhiResolution(res);
  highResSphereSource->Update();
  
  vtkSmartPointer<vtkPolyDataMapper> highResMapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
  highResMapper->SetInputConnection(highResSphereSource->GetOutputPort());
  
  // Low res sphere
  vtkSmartPointer<vtkSphereSource> lowResSphereSource = 
    vtkSmartPointer<vtkSphereSource>::New();
    
  vtkSmartPointer<vtkPolyDataMapper> lowResMapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
  lowResMapper->SetInputConnection(lowResSphereSource->GetOutputPort());
  
  vtkSmartPointer<vtkProperty> propertyLowRes = 
    vtkSmartPointer<vtkProperty>::New();
  propertyLowRes->SetDiffuseColor(0.89, 0.81, 0.34);
  propertyLowRes->SetInterpolationToFlat();

  vtkSmartPointer<vtkProperty> propertyHighRes = 
    vtkSmartPointer<vtkProperty>::New();
  propertyHighRes->SetDiffuseColor(1.0, 0.3882, 0.2784);
  propertyHighRes->SetInterpolationToFlat();

  vtkSmartPointer<vtkLODProp3D> prop = 
      vtkSmartPointer<vtkLODProp3D>::New();
  prop->AddLOD(lowResMapper, propertyLowRes, 0.0);
  prop->AddLOD(highResMapper, propertyHighRes, 0.0);
  
  std::cout << "There are " << prop->GetNumberOfLODs() << " LODs" << std::endl;
    
  // A renderer and render window
  vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = 
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  //prop->SetAllocatedRenderTime(1e-6,renderer);
  prop->SetAllocatedRenderTime(1e-10,renderer);
      
  // An interactor
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actors to the scene
  renderer->AddActor(prop);
  
  vtkSmartPointer<vtkCallbackCommand> refreshCallback =
    vtkSmartPointer<vtkCallbackCommand>::New();
  refreshCallback->SetCallback (RefreshCallback);
  refreshCallback->SetClientData(prop);

  renderWindow->AddObserver(vtkCommand::ModifiedEvent,refreshCallback);
  
  renderWindow->Render();

  // Begin mouse interaction
  renderWindowInteractor->Start();
  
  return EXIT_SUCCESS;
}

结论:

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的赞赏是我的最最最最大的动力(^U^)ノ~YO

文章来源:https://blog.csdn.net/qq_40041064/article/details/135396522
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。