vtk、osg的智能指针vtkSmartPointer、ref_ptr学习

本文详细对比了VTK与OSG库中的智能指针实现,包括构造函数、复制构造函数、赋值构造函数、析构函数等关键部分,解析了两者在引用计数、所有权转移上的异同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在vtk和osg库中,都自己的智能指针,其实现原理基本相同。二者都是引用计数的侵入式智能指针。
vtk的智能指针模板类为vtkSmartPointer<T>,而所有要实现引用计数的类都要继承自vtkObjectBase。其中vtkSmartPointer继承自vtkSmartPointerBase,这是一个非模板类,实现了关于引用计数的大部分功能。
osg的智能指针模板类为ref_ptr<T>,而所有要实现引用计数的类都要继承自Referenced。

 

 

 

1、构造函数
vtk
vtkSmartPointerBase::vtkSmartPointerBase():
Object(nullptr)
{
this->Register();
}

vtkSmartPointerBase::vtkSmartPointerBase(vtkObjectBase* r):
Object(r)
{
this->Register();
}

vtkSmartPointer() {}
vtkSmartPointer(T* r): vtkSmartPointerBase(r) {}

osg
ref_ptr() : _ptr(0) {}
ref_ptr(T* ptr) : _ptr(ptr) { if (_ptr) _ptr->ref(); }

可以看到,二者的构造函数做了相同的工作,存储一个对象的指针,并且把对象的引用计数加1。该引用计数变量保存在对象内部,并且是原子变量,是线程安全的。

2、复制构造函数
vtk
vtkSmartPointerBase::vtkSmartPointerBase(const vtkSmartPointerBase& r):
Object(r.Object)
{
this->Register();
}
template <class U>
vtkSmartPointer(const vtkSmartPointer<U>& r):
vtkSmartPointerBase(CheckType(r.GetPointer())) {}

osg
ref_ptr(const ref_ptr& rp) : _ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
template<class Other> ref_ptr(const ref_ptr<Other>& rp) : _ptr(rp._ptr) { if (_ptr) _ptr->ref(); }

二者原理相同,都是把对象的引用计数再加1。注意这里有类型转换的问题,如果构造函数的右侧不是该对象的同类或子类的话,则构造失败。

3、赋值构造函数
vtk
vtkSmartPointerBase&
vtkSmartPointerBase::operator=(vtkObjectBase* r)
{
vtkSmartPointerBase(r).Swap(*this);
return *this;
}
vtkSmartPointerBase&
vtkSmartPointerBase::operator=(const vtkSmartPointerBase& r)
{
vtkSmartPointerBase(r).Swap(*this);
return *this;
}
void vtkSmartPointerBase::Swap(vtkSmartPointerBase& r)
{
vtkObjectBase* temp = r.Object;
r.Object = this->Object;
this->Object = temp;
}
vtkSmartPointer& operator=(T* r)
{
this->vtkSmartPointerBase::operator=(r);
return *this;
}
template <class U>
vtkSmartPointer& operator=(const vtkSmartPointer<U>& r)
{
this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
return *this;
}

osg
ref_ptr& operator = (const ref_ptr& rp)
{
assign(rp);
return *this;
}

template<class Other> ref_ptr& operator = (const ref_ptr<Other>& rp)
{
assign(rp);
return *this;
}
inline ref_ptr& operator = (T* ptr)
{
if (_ptr==ptr) return *this;
T* tmp_ptr = _ptr;
_ptr = ptr;
if (_ptr) _ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr) tmp_ptr->unref();
return *this;
}

template<class Other> void assign(const ref_ptr<Other>& rp)
{
if (_ptr==rp._ptr) return;
T* tmp_ptr = _ptr;
_ptr = rp._ptr;
if (_ptr) _ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr) tmp_ptr->unref();
}

vtk用了copy and swap模式,保证了自我赋值和异常安全,而osg则稍显冗余。其实vtk有个地方不太明白,赋值之后原始的引用没有减少啊,而osg则调用了unref()。

4、析构函数
vtk
vtkSmartPointerBase::~vtkSmartPointerBase()
{
// The main pointer must be set to nullptr before calling UnRegister,
// so use a local variable to save the pointer. This is because the
// garbage collection reference graph traversal may make it back to
// this smart pointer, and we do not want to include this reference.
vtkObjectBase* object = this->Object;
if(object)
{
this->Object = nullptr;
object->UnRegister(nullptr);
}
}

osg
~ref_ptr() { if (_ptr) _ptr->unref(); _ptr = 0; }

都是减少引用计数

5、指针逻辑运算
vtk中是显式的实现了逻辑比较符号,同时也提供了隐式类型转换。
operator T* () const
{
return static_cast<T*>(this->Object);
}

而osg则提供了隐式转换
perator T*() const { return _ptr; }

6、指针操作
/**
* Dereference the pointer and return a reference to the contained
* object.
*/
T& operator*() const
{
return *static_cast<T*>(this->Object);
}

/**
* Provides normal pointer target member access using operator ->.
*/
T* operator->() const
{
return static_cast<T*>(this->Object);
}

T& operator*() const
{
return *_ptr;
}
T* operator->() const
{
return _ptr;
}

7、get函数
vtk
T* GetPointer() const
{
return static_cast<T*>(this->Object);
}
T* Get() const
{
return static_cast<T*>(this->Object);
}

osg
T* get() const { return _ptr; }

8、swap及转换函数
template<class T> inline
void swap(ref_ptr<T>& rp1, ref_ptr<T>& rp2) { rp1.swap(rp2); }

template<class T> inline
T* get_pointer(const ref_ptr<T>& rp) { return rp.get(); }

template<class T, class Y> inline
ref_ptr<T> static_pointer_cast(const ref_ptr<Y>& rp) { return static_cast<T*>(rp.get()); }

template<class T, class Y> inline
ref_ptr<T> dynamic_pointer_cast(const ref_ptr<Y>& rp) { return dynamic_cast<T*>(rp.get()); }

template<class T, class Y> inline
ref_ptr<T> const_pointer_cast(const ref_ptr<Y>& rp) { return const_cast<T*>(rp.get()); }

9、osg特有
/** release the pointer from ownership by this ref_ptr<>, decrementing the objects refencedCount() via unref_nodelete() to prevent the Object
* object from being deleted even if the reference count goes to zero. Use when using a local ref_ptr<> to an Object that you want to return
* from a function/method via a C pointer, whilst preventing the normal ref_ptr<> destructor from cleaning up the object. When using release()
* you are implicitly expecting other code to take over management of the object, otherwise a memory leak will result. */
T* release() { T* tmp=_ptr; if (_ptr) _ptr->unref_nodelete(); _ptr=0; return tmp; }
返回一个原始指针,同时接收方负责所有权。

10、vtk特有

/**
* Transfer ownership of one reference to the given VTK object to
* this smart pointer. This does not increment the reference count
* of the object, but will decrement it later. The caller is
* effectively passing ownership of one reference to the smart
* pointer. This is useful for code like:

* vtkSmartPointer<vtkFoo> foo;
* foo.TakeReference(bar->NewFoo());

* The input argument may not be another smart pointer.
*/
void TakeReference(T* t)
{
*this = vtkSmartPointer<T>(t, NoReference());
}

/**
* Create an instance of a VTK object.
*/
static vtkSmartPointer<T> New()
{
return vtkSmartPointer<T>(T::New(), NoReference());
}

/**
* Create a new instance of the given VTK object.
*/
static vtkSmartPointer<T> NewInstance(T* t)
{
return vtkSmartPointer<T>(t->NewInstance(), NoReference());
}
其实与osg的release函数类似,都是所有权的转移。

转载于:https://www.cnblogs.com/ljy339/p/10685886.html

### 三维纹理映射的概念与实现方法 #### 什么是三维纹理映射? 三维纹理映射是一种扩展的纹理映射技术,它允许将一个三维的数据集直接映射到几何模型上。相比于传统的二维纹理映射,三维纹理可以提供更真实的视觉效果,尤其是在处理体积数据时[^1]。 #### 使用 `osg::Texture3D` 类实现三维纹理映射 在 OpenSceneGraph (OSG) 中,`osg::Texture3D` 是用于创建和管理三维纹理的核心类。该类继承自 `osg::Texture` 并提供了额外的功能来支持三维纹理操作。需要注意的是,`osg::Texture3D` 不支持立方图纹理,并且要求所有的二维子图具有相同的尺寸和像素格式。 以下是使用 `osg::Texture3D` 的基本流程: 1. **准备三维纹理数据** 需要准备好一组三维数组形式的数据作为输入。这些数据通常表示某种体素化的信息(如医学影像中的 CT 数据)。 2. **设置纹理对象属性** 创建并配置 `osg::Texture3D` 对象,指定其大小、过滤方式以及其他必要的参数。例如: ```cpp osg::ref_ptr<osg::Texture3D> texture3D = new osg::Texture3D; texture3D->setTextureSize(width, height, depth); // 设置纹理维度 texture3D->setInternalFormat(GL_RGBA); // 设置内部格式 texture3D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); texture3D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); ``` 3. **加载纹理数据** 将预处理好的三维数据上传到 GPU 上供后续渲染使用。可以通过 `osg::Image` 来完成这一过程: ```cpp osg::ref_ptr<osg::Image> image = new osg::Image; image->allocateImage(width, height, depth, GL_RGBA, GL_UNSIGNED_BYTE); // 假设 dataBuffer 是指向原始三维数据缓冲区的指针 unsigned char* dataBuffer = ...; image->setDataVariance(osg::Object::DYNAMIC); // 如果数据可能变化,则标记为动态 image->writePixelDataToMemory(dataBuffer); // 加载数据到 Image 对象中 texture3D->setImage(image.get()); ``` 4. **绑定材质与着色器程序** 定义合适的顶点/片段着色器脚本以控制如何应用此三维纹理。下面是一个简单的例子展示如何通过 GLSL 进行访问: ```glsl uniform sampler3D texSampler; void main() { vec3 texCoord = gl_TexCoord[0].stp; // 获取当前片元对应的纹理坐标 vec4 colorSampled = texture(texSampler, texCoord); // 查询对应位置的颜色值 gl_FragColor = colorSampled * gl_Color; // 结合其他光照计算最终颜色输出 } ``` 5. **初始化图形环境** 在实际运行之前还需要确保已经正确设置了 OpenGL 上下文以及视口等基础条件^。 --- #### VTK 中的三维纹理映射 Visualization Toolkit (VTK) 提供了一个专门针对大规模科学可视化需求设计的模块 —— `vtkVolumeTextureMapper3D` 。这个类利用现代 GPU 架构上的硬件加速能力实现了高效的体绘制功能[^2]。 然而由于受到物理存储容量的影响,在某些场景下可能会触发自动降质机制从而降低分辨率以便适应可用资源限制情况下的正常工作状态。具体来说,默认的最大支持范围如下表所示: | 数据类型 | 单通道最大尺寸 | 多通道最大尺寸 | |----------------|-----------------------|---------------------------| | Single Component Data | 256 × 256 × 128 | Not Applicable | | Four Components Non-independent Data | N/A | 256 × 128 × 128 | 此外值得注意的一点是只有 NVIDIA 和 ATI Radeon 系列显卡才完全兼容此类特性^。 为了验证目标平台是否具备执行特定任务所需的能力,开发者可调用辅助工具函数 `ISRenderSupported()` ,传入相关参数即可获得反馈结果。 --- ### 总结 无论是基于 OSG 或者 VTK 开发框架都可以方便快捷地构建起强大的三维纹理映射解决方案。前者更加灵活易于定制化调整细节部分;后者则凭借丰富的内置算法库简化了许多复杂运算环节的同时也保留了一定程度上的可控性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值