VTK修炼之道2_VTK体系结构1
生活随笔
收集整理的這篇文章主要介紹了
VTK修炼之道2_VTK体系结构1
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.OverView綜述
The Visualization Toolkit consists of two basic subsystems: a compiled C++ class library (一個(gè)已經(jīng)編譯好的C++類庫)and an “interpreted” wrapper layer(一個(gè)用于解釋的語言層) that lets you manipulate thecompiled classes using the languages Java, Tcl, and?Python.The Visualization Toolkit is an object-oriented system(面向?qū)ο蟮捏w系). The key to using VTK effectively is to
develop a good understanding of the underlying object models(想使用好VTK,就要充分理解最基本的對(duì)象模型). Doing so will remove much of the?mystery surrounding the use of the hundreds of objects in the system. With this understanding in?place it’s much easier to combine objects to build applications. You’ll also need to know something?about the capabilities of the many objects in the system(同時(shí),我們也應(yīng)該理解體系中很多對(duì)象的基本功能)。
2.Low-Level Object Model低級(jí)對(duì)象模型
The VTK object model can be thought of as being rooted in the superclass vtkObject. Nearly all VTKclasses are derived from this class, or in some special cases from its superclass vtkObjectBase.(幾乎所有的VTK對(duì)象都源于超類:vtkObject;出了一些特出情況來源于vtkObjectBase;這里提及到的超類是指一個(gè)或者多個(gè)來由此派生;) All?VTK must be created using the object's New() method, and must be destroyed using the object's?Delete() method. VTK objects cannot be allocated on the stack because the constructor is a protected?method. (因?yàn)閂TK的構(gòu)造函數(shù)采用保護(hù)類型,所以VTK的對(duì)象無法在堆上進(jìn)行創(chuàng)建并分配內(nèi)存,只能通過New()和Delete()方法進(jìn)行對(duì)象的創(chuàng)建與銷毀)Using a common superclass and a unified method of creating and destroying object, VTK is?able to provide several basic object-oriented operations.
2.1?Reference Counting引用計(jì)數(shù)
Objects explicitly store a count of the number of pointers referencing them.?When an object is created through the static New() method of a class its initial reference count is 1?because a raw pointer must be used to refer to the new object:vtkObjectBase* obj = vtkExampleClass::New();When other references to the object are created or destroyed the reference count is incremented and
decremented using the Register() and UnRegister() methods. Usually this is handled automatically by
the various “set” methods provided in the object’s API: otherObject->SetExample(obj);The reference count is now 2 because both the original pointer and a pointer stored inside the otherobject both refer to it. When the raw pointer originally storing the object is no longer needed the reference is removed using the Delete() method: obj->Delete();
從上面我們可以看到,對(duì)象的創(chuàng)建以及銷毀完全靠手動(dòng)完成,萬一我們不小心,無疑這將會(huì)造成內(nèi)存泄漏。為了避免這是,可以采用“智能指針”。(智能指針是通過類模板進(jìn)行定義的) 因此,上面的程序代碼我們可以改寫成: vtkSmartPointer<vtkObjectBase> obj = vtkSmartPointer<vtkExampleClass>::New(); otherObject->SetExample(obj);此時(shí),我們就沒有必要再去調(diào)用Delete()方法撤銷對(duì)象。
2.2?Run-Time Type Information運(yùn)行時(shí)類型信息
In C++ the real type of an object may be different from the type of?pointer used to reference it. (真實(shí)的對(duì)象類型可能和引用的指針類型不同)All classes in the public interface of VTK have simple identifiers for class?names (no templates), so a string is sufficient to identify them. (用一個(gè)字符串來標(biāo)識(shí))The type of a VTK object may beobtained at run-time with the GetClassName() method(用GetClassName()可以得到VTK對(duì)象的類型):
<span style="font-size:18px;">const char* type = obj->GetClassName();</span>An object may be tested for whether it is an instance of a particular class or one of its subclasses using
the IsA() method(通過下面的方法我們可以檢測一個(gè)對(duì)象是否是一個(gè)類的實(shí)例):
<span style="font-size:18px;">if(obj->IsA("vtkExampleClass")) { ... }</span>
2.3?Object State Display對(duì)象狀態(tài)顯示
When debugging(調(diào)試中很有用,可以了解到對(duì)象當(dāng)前的描述) it is often useful to display a human-readable description of?the current state of an object. This can be obtained for VTK objects using the Print() method:<span style="font-size:18px;">obj->Print(cout);</span>
3.The Rendering Engine渲染引擎
The VTK rendering engine consists of the classes in VTK that are responsible for taking the results of?the visualization pipeline and displaying them into a window(渲染引擎的工作在于獲得可視化管道的結(jié)果,然后將他們顯示在窗口上). This involves the following components. Note that this is not an exhaustive list(此處沒有給出詳細(xì)的清單), but rather a sense of the most commonly used objects in?the rendering engine. The subheadings used here are the highest level superclass in VTK that represents this type of object, and in many cases where there are multiple choices these are abstract classes defining the basic API across the various concrete subclasses that implement the functionality.3.1?vtkProp(道具?)
Visible depictions of data that exist in the scene are represented by a subclass of vtkProp(vtkProp子類用來呈現(xiàn)數(shù)據(jù)的可視化).The most commonly used subclasses of vtkProp for displaying objects in 3D are vtkActor (used torepresent geometric data in the scene) and vtkVolume (used to represent volumetric data in the scene).There are also props that represent data in 2D such as vtkActor2D. The vtkProp subclass is generally?responsible for knowing its position, size, and orientation in the scene(vtkProp子類通常負(fù)責(zé)知曉視圖的位置、尺寸和方向). The parameters used to control?the placement of the prop generally depend on whether the prop is for example a 3D object in the?scene, or a 2D annotation. For 3D props such as vtkActor and vtkVolume , you can either directly control parameters such as?the object's 3D position, orientation and scale, or you can use a 4x4 transformation matrix(利用一個(gè)4*4的矩陣直接控制3D立體的位置、方向和尺寸). For 2D?props that provide annotation such as the vtkScalarBarActor, the size and position of the annotation?can be defined in a variety of ways including specifying a position, width, and height relative to the?size of the entire viewport. In addition to providing placement control, props generally have a mapper?object that holds the data and knows how to render it, and a property object that controls parameters?such as color and opacity.(除了提供位置上的控制,Prop通常也會(huì)提供一個(gè)映射器對(duì)象,這個(gè)映射其對(duì)象持有數(shù)據(jù)并且知道如何渲染這些數(shù)據(jù);此外還控制一個(gè)屬性對(duì)象,控制顏色和透明度參數(shù))
There are a large number (over 50) of specialized props(專用道具) such as vtkImageActor (used to display
an image) and vtkPieChartActor (used to create a pie chart visual representation of an array of datavalues). Some of these specialized props directly contain the parameters that control appearance, and?directly have a reference to the input data to be rendered, and therefore do not require the use of a?property or a mapper. The vtkFollower prop is a specialized subclass of vtkActor that will automatically update its orientation in order to continually face a specified camera. This is useful for displaying billboards or text in the 3D scene and having them remain visible as the user rotates. The?vtkLODActor is also a subclass of vtkActor that automatically changes its geometric representation?in order to maintain interactive frame rates, and vtkLODProp3D is a subclass of vtkProp3D that
selects between a number of different mappers (perhaps even a mixture of volumetric and geometric
mappers) in order to provide interactivity. vtkAssembly allows hierarchies of actors, properly managing the transformations when the hierarchy is translated, rotated or scaled.
3.1?vtkAbstractMapper 抽象映射器
Some props such as vtkActor and vtkVolume use a subclass of vtkAbstractMapper to hold a reference to the input data(保持對(duì)輸入數(shù)據(jù)的引用) and to provide the actual rendering functionality(提供實(shí)際的渲染功能). The ?vtkPolyDataMapper is the primary mapper for rendering polygonal geometry.(對(duì)于渲染多邊形幾何 vtkPolyDataMapper是最主要的映射器) For volumetric objects(對(duì)于容積對(duì)象),?VTK provides several rendering techniques including the vtkFixedPointVolumeRayCastMapper that?can be used to rendering vtkImageData, and the vtkProjectedTetrahedra mapper that can be used torender vtkUnstructuredGrid data.
3.2?vtkProperty and vtkVolumeProperty 屬性和體積屬性
Some props use a separate property object to hold the various parameters that control the appearance of the data.(屬性對(duì)象用來控制數(shù)據(jù)的外觀) This allows you to more easily share appearance settings between different objects in your scene. The vtkActor object uses a vtkProperty to store?parameters such as color, opacity, and the ambient, diffuse, and specular coefficient of the material.(顏色、透明度、陰影、鏡面反射系數(shù))The vtkVolume object instead uses a vtkVolumeProperty to capture the parameters that are applicable?to a volumetric object, such as the transfer functions that map the scalar value to color and opacity.?Many mappers also provide functionality to set clipping planes that can be used to reveal interior?structure.3.3?vtkCamera 攝像機(jī)
The vtkCamera contains the parameters that control how you view the scene.(如何去看一個(gè)場景) The vtkCamera has a position, a focal point, and a vector defining the direction of "up" in the scene(位置信息、焦點(diǎn)信息、定義“向上”). Other?parameters control the specific viewing transformation (parallel or perspective), the scale or view?angle of the image, and the near and far clipping planes of the view frustum.(平行/透視、尺寸、角度、視錐體的遠(yuǎn)近裁剪平面).3.4?vtkLight 燈光
When lighting is computed for a scene, one or more vtkLight objects are required. The vtkLight objects store the position and orientation of the light, as well as the color and intensity.(該對(duì)象儲(chǔ)存著燈光的位置的方向信息,以及燈光的顏色和強(qiáng)度信息) Lights?also have a type that describes how the light will move with respect to the camera.(燈光還有一個(gè)參數(shù)信息,那就是定義燈光如何相對(duì)于攝像機(jī)移動(dòng)) For example, a?Headlight is always located at the camera's position and shines on the camera's focal point, whereas a?SceneLight is located at a stationary position in the scene.3.5?vtkRenderer?渲染器
The objects that make up a scene including the props, the camera and the lights are collected together in a vtkRenderer. (渲染器對(duì)象用于將道具、攝像機(jī)、燈光收集到一起組成一個(gè)場景)The vtkRenderer is responsible for managing the rendering process?for the scene. Multiple vtkRenderer objects can be used together in a single vtkRenderWindow. (在一個(gè)舞臺(tái)上我們可以構(gòu)建多個(gè)渲染器對(duì)象,這就是我們平常說的多視窗技術(shù))These ?renderers may render into different rectangular regions (known as viewports) of the render window,or may be overlapping.3.6?vtkRenderWindow 渲染窗口
The vtkRenderWindow provides a connection between the operating system?and the VTK rendering engine(渲染窗口類用于聯(lián)系當(dāng)?shù)夭僮飨到y(tǒng)和VTK渲染引擎). Platform specific subclasses of vtkRenderWindow are responsible for?opening a window in the native windowing system on your computer and managing the display pro-cess(主要負(fù)責(zé)打開一個(gè)本地的窗口,然后管理圖像顯示過程). When you develop with VTK, you simply use the platform-independent vtkRenderWindow?which is automatically replaced with the correct platform-specific subclass at runtime. The vtkRenderWindow contains a collection of vtkRenderers, and parameters that control rendering features such?as stereo, anti-aliasing, motion blur and focal depth(渲染窗口類主要負(fù)責(zé)收集渲染器信息以及控制渲染特征的信息,比如立體?運(yùn)動(dòng)模糊?焦點(diǎn)深度?).3.7?vtkRenderWindowInteractor 渲染窗口交互
The vtkRenderWindowInteractor is responsible for processing?mouse, key, and timer events (負(fù)責(zé)處理鼠標(biāo)、鍵盤和定時(shí)器事件的消息)and routing these through VTK's implementation of the command /observer design pattern. A vtkInteractorStyle listens for these events and processes them in order to?provide motion controls such as rotating, panning and zooming. (可以提供類似平移、旋轉(zhuǎn)、縮放的功能)The vtkRenderWindowInteractor?automatically creates a default interactor style that works well for 3D scenes, but you can instead select one for 2D image viewing for example, or create your own custom interactor style.3.8?vtkLookupTable, vtkColorTransferFunction, and vtkPiecewiseFunction
Visualizing scalar data?often involves defining a mapping from a scalar value to a color and opacity(標(biāo)量數(shù)據(jù)可視化通常會(huì)涉及到定義一個(gè)標(biāo)量值到顏色/透明度的映射). This is true both in geometric surface rendering where the opacity will define the translucency of the surface, and in volume?rendering where the opacity will represent the opacity accumulated along some length of of ray passing through the volume. For geometric rendering, this mapping is typically created using a vtkLookupTable, and in volume rendering both the vtkColorTransferFunction and the?vtkPiecewiseFunction will be utilized.4.渲染引擎的測試程序
#include "vtkCylinderSource.h" #include "vtkPolyDataMapper.h" #include "vtkActor.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkProperty.h" #include "vtkCamera.h"#include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRenderingOpenGL);int main() {// This creates a polygonal cylinder model with eight circumferential facets.vtkCylinderSource *cylinder = vtkCylinderSource::New();cylinder->SetResolution(8);// The mapper is responsible for pushing the geometry into the graphics// library. It may also do color mapping, if scalars or other attributes// are defined.vtkPolyDataMapper *cylinderMapper = vtkPolyDataMapper::New();cylinderMapper->SetInputConnection(cylinder->GetOutputPort());// The actor is a grouping mechanism: besides the geometry (mapper), it// also has a property, transformation matrix, and/or texture map.// Here we set its color and rotate it -22.5 degrees.vtkActor *cylinderActor = vtkActor::New();cylinderActor->SetMapper(cylinderMapper);cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);cylinderActor->RotateX(30.0);cylinderActor->RotateY(-45.0);// Create the graphics structure. The renderer renders into the// render window. The render window interactor captures mouse events// and will perform appropriate camera or actor manipulation// depending on the nature of the events.vtkRenderer *ren1 = vtkRenderer::New();vtkRenderWindow *renWin = vtkRenderWindow::New();renWin->AddRenderer(ren1);vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();iren->SetRenderWindow(renWin);// Add the actors to the renderer, set the background and sizeren1->AddActor(cylinderActor);ren1->SetBackground(0.1, 0.2, 0.4);renWin->SetSize(200, 200);// We'll zoom in a little by accessing the camera and invoking a "Zoom"// method on it.ren1->ResetCamera();ren1->GetActiveCamera()->Zoom(1.5);renWin->Render();// This starts the event loop and as a side effect causes an initial render.iren->Start();// Exiting from here, we have to delete all the instances that// have been created.cylinder->Delete();cylinderMapper->Delete();cylinderActor->Delete();ren1->Delete();renWin->Delete();iren->Delete();return 0; }程序運(yùn)行結(jié)果:總結(jié)
以上是生活随笔為你收集整理的VTK修炼之道2_VTK体系结构1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。