我想在voreen中创建一个处理器(就像这个.cpp | .h)移植这个OTB-Application:
http://hg.orfeo-toolbox.org/OTB/file/ca4366bb972e/Applications/Segmentation/otbSegmentation.cxx
我已将几乎所有参数编码为属性等,但是......
如果你看一下376,你会看到一个类模板FloatVectorImageType :: SizeType,一个typedef类型。
我不熟悉c ++模板所以我的第一个问题是我应该把这个模板的实现放在处理器的.cpp或.h文件中?简要介绍一下c ++教程和其他处理器示例,例如one above,我发现我必须在头文件中声明模板并在.cpp中定义它。
问题是编译器不允许我在.cpp中定义typedef类型的模板类。 typedef不被识别..
那么,有人能指出我正确的方向吗?
的 segmentationprocessor.h 的
#ifndef OTBSEGMENTATIONAPPLICATION_H
#define OTBSEGMENTATIONAPPLICATION_H
#include "otbVectorImage.h"
#include "modules/otb/ports/otbimageport.h"
#include "modules/otb/ports/otbvectorimageport.h"
#include "voreen/core/properties/boolproperty.h"
//..more includes here
namespace voreen {
class OTBSegmentationApplication : public OTBImageFilterProcessor
{
public:
OTBSegmentationApplication();
virtual ~OTBSegmentationApplication();
virtual Processor* create() const;
virtual std::string getCategory() const { return "Applications"; }
virtual std::string getClassName() const { return "Segmentation Application"; }
virtual CodeState getCodeState() const { return CODE_STATE_EXPERIMENTAL;}//STABLE, TESTING, EXPERIMENTAL
virtual std::string getProcessorInfo() const;
/** Images typedefs */
typedef otb::VectorImage<double, 2> VectorImageType;
typedef ImageType LabelImageType;
typedef ImageType MaskImageType;
typedef VectorImageType::SizeType size;
// Segmentation filters typedefs
// Edison mean-shift
typedef otb::MeanShiftVectorImageFilter<VectorImageType,VectorImageType,LabelImageType> EdisonSegmentationFilterType;
EdisonSegmentationFilterType::Pointer edisonFilter;
// Home made mean-shift
typedef otb::MeanShiftSegmentationFilter<VectorImageType, LabelImageType, VectorImageType> MeanShiftSegmentationFilterType;
MeanShiftSegmentationFilterType::Pointer meanshiftFilter;
// Simple connected components
typedef otb::Functor::ConnectedComponentMuParserFunctor<VectorImageType::PixelType> FunctorType;
typedef itk::ConnectedComponentFunctorImageFilter <VectorImageType, LabelImageType, FunctorType, MaskImageType> ConnectedComponentSegmentationFilterType;
ConnectedComponentSegmentationFilterType::Pointer ccFilter;
typedef itk::ScalarConnectedComponentImageFilter<LabelImageType, LabelImageType> LabeledConnectedComponentSegmentationFilterType;
LabeledConnectedComponentSegmentationFilterType::Pointer labeledCCFilter;
//..more typedefs here
protected:
virtual void setDescriptions() {
setDescription("Performs segmentation of an image, and output either a raster or a vector file. In vector mode, large input datasets are supported.");
}
void process();
virtual void initialize() throw (tgt::Exception);
virtual void deinitialize() throw (tgt::Exception);
/** TEMPLATE DECLARATION (?) */
template<class TInputImage, class TSegmentationFilter>
VectorImageType::SizeType GenericApplySegmentation(otb::StreamingImageToOGRLayerSegmentationFilter
<TInputImage, TSegmentationFilter> * streamingVectorizedFilter, TInputImage * inputImage,
const otb::ogr::Layer& layer, const unsigned int outputNb);
virtual void updateFilterSelection();
virtual void updateModeSelection();
private:
OTBVectorImagePort inPort_;
StringOptionProperty filter_; ///< Select segmentation algorithm
OTBVectorImagePort vectorOutPort_;
OTBImagePort vectorMaskInPort_;
OTBImagePort outPort_;
//..more property definitions here
static const std::string loggerCat_; ///< Category used in logging
};
} // namespace
#endif // OTBSEGMENTATIONAPPLICATION_H
的 segmentationprocessor.cpp 的
#include "segmentationprocessor.h"
#include "voreen/core/voreenapplication.h"
namespace voreen {
const std::string OTBSegmentationApplication::loggerCat_("voreen.OTBSegmentationApplication");
OTBSegmentationApplication::OTBSegmentationApplication()
:OTBImageFilterProcessor(),
inPort_(Port::INPORT, "IN Multiband Image", 0),
vectorOutPort_(Port::OUTPORT, "OUT Multiband Image", 0),
vectorMaskInPort_(Port::INPORT, "IN Mask Image", 0),
outPort_(Port::OUTPORT, "OUT OTB Image", 0),
filter_("selectFilter", "Segmentation algorithm"),
//.. more properties code here
{
addPort(inPort_);
addPort(vectorOutPort_);
addPort(vectorMaskInPort_);
addPort(outPort_);
addProperty(filter_);
//.. adding the rest of properties here
edisonFilter = EdisonSegmentationFilterType::New();
meanshiftFilter = MeanShiftSegmentationFilterType::New();
ccFilter = ConnectedComponentSegmentationFilterType::New();
//..instantiating more filters needed in implementation here
}
Processor* OTBSegmentationApplication::create() const {
return new OTBSegmentationApplication();
}
OTBSegmentationApplication::~OTBSegmentationApplication() {
}
void OTBSegmentationApplication::initialize() throw (tgt::Exception) {
Processor::initialize();
}
void OTBSegmentationApplication::deinitialize() throw (tgt::Exception) {
Processor::deinitialize();
}
std::string OTBSegmentationApplication::getProcessorInfo() const {
return "Segmentation Application";
}
void OTBSegmentationApplication::updateFilterSelection() {
//code for visual updates on properties here
}
void OTBSegmentationApplication::updateModeSelection() {
//code for visual updates on properties here
}
//TEMPLATE IMPLEMENTATION HERE (?)
template<class TInputImage, class TSegmentationFilter>
OTBSegmentationApplication::VectorImageType::SizeType OTBSegmentationApplication::GenericApplySegmentation(otb::StreamingImageToOGRLayerSegmentationFilter<TInputImage,
TSegmentationFilter> * streamingVectorizedFilter, TInputImage * inputImage, const otb::ogr::Layer& layer, const unsigned int outputNb)
{
typedef TSegmentationFilter SegmentationFilterType;
typedef typename SegmentationFilterType::Pointer SegmentationFilterPointerType;
typedef otb::StreamingImageToOGRLayerSegmentationFilter<TInputImage,SegmentationFilterType> StreamingVectorizedSegmentationOGRType;
//..the rest of template code here
}
void OTBSegmentationApplication::process() {
try
{
//PROCESSOR IMPLEMENTATION GOES HERE
LINFO("Segmentation Application Connected");
}
catch (int e)
{
LERROR("Error in Segmentation Applicationn");
return;
}
}
} // namespace
错误:'VectorImageType'未命名类型(已修复)
答案 0 :(得分:7)
我不熟悉c ++模板,所以我的第一个问题是我应该把这个模板的实现放在处理器的.cpp或.h文件中?
将它放在头文件中。这是最简单,最强大的解决方案。通常,您希望将函数的定义(即它们的函数体)放在源文件(.cpp)中,因为源文件可以独立编译。 但这不适用于模板(*)。
(*)略有简化。
类模板只是类的蓝图,函数模板是函数的蓝图。也就是说,函数模板不是函数,换句话说“模板函数”具有误导性,它不是函数而是模板/蓝图。
从函数模板(或类模板中的类)构建函数的过程称为 instantiation 。结果是一个实例化的函数,或者更一般地说,是一个函数模板 specialization 。
模板专精是无模板。函数模板特化只是一个具有奇怪名称的普通函数;类模板特化只是一个具有奇怪名称的类。
模板只会针对某些特定的模板参数集进行实例化:
第二种方式到目前为止更常见。一个例子:
template<class T>
struct my_type
{
T mem;
};
// using the specialization -> implicit instantiation
my_type<int> o;
my_type<double> p;
这将为模板参数my_type
实例化类模板 int
,并为模板参数double
实例化一次。这会创建两个名称相似的独立和无关类型:my_type<int>
和my_type<double>
类似的功能;除了对于函数,您通常不显式提供模板参数。相反,您让编译器从函数参数的类型中推导出模板参数。例如:
template<class T>
void foo(T param)
{
std::cout << param;
}
foo<int>(42); // explicitly specifying the template arguments -- DON'T DO THAT
foo(21); // template argument *deduction*
第二个调用将自动推断模板参数为int
。同样,我们创建(隐式实例化)两个具有相似名称的函数:foo<int>(int)
(名称为foo<int>
,它有一个类型为int
的函数参数)和foo<double>(double)
为什么将模板的定义放在源文件中是不好的:请参阅Why can templates only be implemented in the header file?
简短版本:由于模板是蓝图,为了使用它们,编译器必须实例化它们。但它只能实例化知道的内容。
如果您在标题文件foo
中声明了功能模板templ.h
,请在templ.cpp
中对其进行定义,并在main.cpp
中使用,然后:
在main.cpp
中,编译器不知道函数模板的定义。它只能实例化foo
的声明,而不是实例。
在templ.cpp
中,编译器确实知道定义,并可以实例化它。但是,它不知道templ.cpp
之外的用法 - 因此它不能为在外面使用的所有参数集实例化它。
在OP中的示例中,这有效,但似乎是一种疏忽:
[templ.h]
template<class T>
void foo(T);
void ordinary_function();
[templ.cpp]
#include "templ.h"
template<class T>
void foo(T p)
{
std::cout << p;
}
void ordinary_function()
{
foo(42); // implicit instantiation of foo<int>
foo(2.5); // implicit instantiation of foo<double>
}
[main.cpp中]
#include "templ.h"
int main()
{
foo(23); // works fine, uses the foo<int> defined in templ.cpp
foo('a'); // linker error: foo<char> not defined
return 0;
}
由于foo<char>
的定义尚未在templ.cpp
中实例化,并且无法在main.cpp
中实例化,因此会产生链接器错误。< / p>
这就是为什么你不应该依赖这种行为。如果由于某种原因,不想要在头文件中定义函数模板,则可以使用显式实例化。至少,显式实例化是显式的,应该记录在案,以免发生意外。
编译器实际抱怨的问题与模板无关;)这只是一个名称查找问题。一个简化的例子:
#include <iostream>
struct Foo
{
typedef int Bar;
void do_something();
};
Bar Foo::do_something()
{
std::cout << "something\n";
}
当编译器看到行Bar Foo::do_something()
时,它会看到Bar
,并且无法找到该名称所指的内容。因此错误。另一方面:
#include <iostream>
struct Foo
{
typedef int Bar;
void do_something();
};
Foo::Bar Foo::do_something()
{
std::cout << "something\n";
}
现在您告诉编译器在哪里查找名称Bar
,即在Foo
内。
答案 1 :(得分:0)
以下是我为了帮助您而编写的模板的简短示例:
标题中的
typedef TemplatedClassName< ParameterValue0, ParameterValue1 > TemplateAlias;
源文件中的 :
//显式模板实例化
template class TemplatedClassName< ParameterValue0, ParameterValue1 >;