Point Cloud Library - Writing functions to accept multiple types - point-cloud-library

static pcl::PointCloud<pcl::PointXYZRGB>::Ptr extractConcaveHull(
pcl::PointCloud<pcl::PointXYZRGB>::Ptr inputCloud,
double alpha
);
This function for example. Instead of writing two function calls, one for PointXYZ and one for PointXYZRGB and one for PointXYZRGBNormal, is there a way I can use some generic type? Like a PointCloud object, and on compile time it is able to cast accordingly

This should work (I have not tested it).
template<typename POINT_TYPE>
static typename pcl::PointCloud<POINT_TYPE>::Ptr
extractConcaveHull(
typename pcl::PointCloud<POINT_TYPE>::Ptr inputCloud,
double alpha
);
You can also pass by reference
template<typename POINT_TYPE>
static void
extractConcaveHull(
const typename pcl::PointCloud<POINT_TYPE>::Ptr& inpCloud,
const typename pcl::PointCloud<POINT_TYPE>::Ptr& outCloud,
double alpha
);
You are gonna have to define templated methods in your header file.

Related

How to expose global strings and ints to C++, QML, and JS

Our team is developing a Qt application which makes use of C++, QML, and JS. What is the best way to expose non-language specific strings, representing filenames and ints representing error codes, so that all languages can easily use them?
The most efficient and clean solution would be to implement those strings and ints as properties of a QObject, then create an instance of that object in main.cpp and register it as a singleton.
// before main()
YourType data;
static QObject * getData(QQmlEngine * e, QJSEngine *) {
e->setObjectOwnership(&data, QQmlEngine::CppOwnership); // just in case
return &data;
}
// in main()
qmlRegisterSingletonType<YourType>("Core", 1, 0, "Data", getData);
QQmlApplicationEngine engine;
//...
And now you have a global data for C++, and a global Data for QML. In the first case you will need to declare data as an extern in sources which need to access it, in the second case you will need to import Core 1.0 in order to access Data and its properties.

Access specific object from any class

I am trying to access awesome object from another class. I declare this in main()
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QtAwesome* awesome = new QtAwesome(&app);
awesome->initFontAwesome();
app.setWindowIcon(awesome->icon( "clipboard" ));
Login w;
w.show();
return app.exec();
}
Now how do I access awesome object from Login or any another class? Is it best to initialize a class in main() if I want to access from another class?
Declaring awesome in main() makes it scoped to the main() function, and thus only visible from with main(), or functions you explicitly pass it to from main().
The simple way to achieve what you want is to use an "external variable": Put the definition and declaration outside of the main() in your main executable file:
main.cpp:
QtAwesome * awesome = new QtAwesome(&app);
int main(int argc, char *argv[]) {
awesome->do_something();
// ...
}
The above is called the defining declaration of "awesome". In other files you want to use awesome in, use the extern storage specifier:
somerandommodule.cpp:
extern QtAwesome * awesome;
void some_function() {
awesome->do_something();
}
It's important that you have exactly one "defining declaration". The linker will make all extern awesome declarations point to this one definition.
If you are the one writing the QtAwesome class, another way of doing it is keeping the single instance in the QtAwesome class. This is roughly the "singleton pattern". This just makes it easier to tell where and when initialization is happening:
qtawesome.h:
class Awesome {
private:
static Awesome * singleton;
public:
static Awesome * getInstance() {
if(!Awesome::singleton)
Awesome::singleton = new Awesome();
return Awesome::singleton;
};
};
qtawesome.cpp:
#include "awesome.h"
Awesome* Awesome::singleton = 0;
somerandommodule.cpp:
#include "awesome.h"
void some_function() {
Awesome::getInstance()->do_something();
}
You have two options
pass a pointer or reference to awesome to the Login object, e.g. as an argument to the constructor of Login
use a singleton pattern or global variable (as suggested by S.Pinkus)
A global variable or singleton increases the coupling, i.e. the Login object becomes dependent on this specific QtAwesome object to exist.
Passing an object into places of usage enables you to use a specific QtAwesome object for each usage or use the same for multiple usages, etc.
Globally accessible objects, whether global variables or singletons, often make testing more complicated, since the need for such an object is not immediately visible and all tests within the same process access the same globally accessile object, potentially resulting in tests affecting each other in unexpected ways.

QtConcurrent::blockingMapped calling function with more than 1 argument

I use QtConcurrent::blockingMapped() to execute the function on the list of single arguments on multiple threads. It's really great!
But I'd like to do same thing calling the function that takes more than one argument, i.e:
// prototype:
static void openAndProcess(QString FileName, QImage &image);
And this is my data:
QList<QString> fileList;
QList<QImage> qImageList;
And I would like to execute QtConcurrent::blockingMapped() on my openAndProcess() function using both above QLists...
How should I do it?
Thanks in advance!
Create a POD struct with pointers to the data. This is the only way to do this without reimplementing lots of unfun things in QtConcurrent.
Or, consider using QList<QPair<QString, QImage> >, which is effectively the same thing.

method overloading with QString or std::string : call is ambiguous

I have a class which looks like this :
class MyClass {
public:
void drawText(const QString& rText);
void drawText(const std::string& rText);
};
I overloaded the drawText() method because I want to accept QString as well as std::string.
But when I write something like this :
MyClass foo;
foo.drawText("Hello");
The compiler is complaining that the call to drawText() is ambiguous.
I understand that from an array of char, the compiler cannot decide between a QString or a std::string, because both provide a suitable constructor.
But is there a way for me to make sure the user can use the drawText() method either by passing a QString or a std::stringor an array of char ?
To answer your question, yes: add another overload which takes const char*
The implicit conversion from const char* to QString is problematic because it assumes that the input is ASCII. I suspect the Qt folks would like to remove that constructor altogether but it would break source compatibility. If you want to disable it in your app, you can define QT_NO_CAST_FROM_ASCII.

Convert a QStandardItemModel to a QVariant

I'm trying to send a QStandardItemModel-derived object to PythonQt, but I'm a little confused on how it needs to be sent. When I was using boost::python I had several controls like boost::noncopyable to ensure I wasn't recreating this object, but sharing it with python. I also had constructs to provide a boost shared pointer to python from inside python.
class Scene : public boost::enable_shared_from_this<Scene>, public QStandardItemModel
In PythonQt, however, I'm not sure what's available. The function call takes a QVariantList for all the function parameters.
QVariant PythonQt::call(PyObject* object, const QString &callable, const QVariantList &args = QVariantList))
What I'm confused about now is how to get my object to python via a QVariant. Since its derived from QStandardItemModel, I figured it would already be register
void MyObject::someFunction(QString fileName)
{
QVariant myObjectV = qVariantFromValue(this);
// send to python
...
}
But this gives me the following error:
'qt_metatype_id' : is not a member of 'QMetaTypeId<MyObject>'
I've tried registering it after I declare my class, but this throws a different error.
class MyObject : public QStandardItemModel
{
Q_OBJECT
...
};
Q_DECLARE_METATYPE(MyObject)
QStandardItemModel::QStandardItemModel(const QStandardItemModel&) is private within this context.
I actually get the error twice--once in header where I add the Q_DECLARE_METATYPE and in another header, which has a class which always derives from QStandardItemModel but is otherwise unrelated.
Is Q_DECLARE_METATYPE even the correct way to go about converting this object to a QVariant?
BOOST_PYTHON_MODULE(scene)
{
class_("Scene");
}
Yes, by default, QVariant can take one of te following types - http://doc.qt.io/qt-4.8/qvariant.html#Type-enum - and they are not enough for your task. You should declare additional types by yourself via qmetatype system. Thus you shoud call qRegisterMetaType() function.

Resources