Qwt and QwtSeriesData - c++

I originally use C Programming language. But Now, I need to use Qt programming (by the way, Qt is like a dream). I am going to more depth step by step. But my C++ object oriented knowledge is weak, I hope that it will be stronger. Nowadays I have to use Qwt and I stucked in QwtSeriesData object. I need to know how can I set a series of data to this object in order to draw a curve with using QwtPlot.
For example my data is like below, how can I set them into QwtSeriesData.
float x[300];
float y[300];
Thanks.

My answer is for latest qwt version 6.x.x (latest for current moment)
Note: qwt internally uses double for data representation, not float. So you either should use double or you would need to implement your own QwtSeriesData implementation which holds float in memory but provides double for requests of external components (that's a really bad way of doing things)
You can use one of subclasses of QwtSeriesData provided by the qwt:
QwtCPointerData or QwtPointArrayData.

This is how I do it:
QwtPlotCurve* curve = new QwtPlotCurve;
QPolygonF points;
for(unsigned int i = 0; i < 300; i++)
{
points << QPointF(x[i], y[i]);
}
curve->setSamples(points);
you then need to attach the curve to the plot.

Related

Nvidia flex data transfer

So I'm trying to use the flex API by NVIDIA for my game engine(as a core gameplay mechanic) and I'm now arranging my data structures. I've already read the flex Manual, but the descriptions are rather sparsely. Because I'm also using CUDA, I need to know if the flex API calls like flexSetParticles etc. also accept device pointers as inputs. Also, it would be nice if someone could tell me, what exactly flexUpdateSolver does. Does it compute the velocities itself? Does it calculate gravity? If no, and you have to calculate the updated velocities yourself, what does the Solver even do?
At the moment, I calculate the new positions and velocities myself(without flex) like this:
void updateParticle(int i, float deltaTime)
{
velocities[i] = types[i].getVelocity(deltaTime);
//calculates the currently fixed velocity at a given time
positions[i] = positions[i] + velocities[i];
}
All the arrays in the function above are device pointers and the function is actually a kernel. If I now have to calculate the velocities myself, I would have to
1.) update the arrays by adding new particles if necessary(from host to device) and calculate velocities(device)
2.) copy the new positions (and velocities) back to the CPU and hand them over to flex
3.) after flex has finished, copy the new positions from flexGetParticles back to the GPU (an OpenGL buffer for rendering)
This seems pretty inefficient, so I would like to know if there is an easier solution.
Yes, the flexUpdateSolver will calculate the positions and velocities for the particles internally. So, you must not do that yourself. Remember that, you have to call NvFlexGetParticles(particleBuffer, n) to get the updated positions and velocities after each time step.
As for the flexSetParticles, it takes either a Host or Device buffer pointer. You can create the buffer using NvFlexAllocBuffer by passing the appropriate NvFlexBufferType enum.

Point Cloud Library: How visualize a set of 3D point stored in a C++ <vector>?

I've searched here about this question, but seems no one have this question.
How can I draw with PCL visualizer a set of 3D points stored in a C++ ?
The vector it's filled of structs like this:
struct point {
float x;
float y;
float z;
};
How can I draw this set of points using PCL in a window? At every elaboration of vector, the PCL window will be updated with new points of interest in the vector.
I'm on Visual C++ 2010, Win7 64bit...I've correctly installed PCL library and visual c++ enviromental variables/linker/etc to use PCL.
Can someone help me?
Just define pcl::PointXYZ pt, and pt.x=point.x, pt.y=point.y, pt.z=point.z
then apply PointView to show your point clouds.
this is very essential part in PCL

Matlab griddata equivalent in C++

I am looking for a C++ equivalent to Matlab's griddata function, or any 2D global interpolation method.
I have a C++ code that uses Eigen 3. I will have an Eigen Vector that will contain x,y, and z values, and two Eigen matrices equivalent to those produced by Meshgrid in Matlab. I would like to interpolate the z values from the Vectors onto the grid points defined by the Meshgrid equivalents (which will extend past the outside of the original points a bit, so minor extrapolation is required).
I'm not too bothered by accuracy--it doesn't need to be perfect. However, I cannot accept NaN as a solution--the interpolation must be computed everywhere on the mesh regardless of data gaps. In other words, staying inside the convex hull is not an option.
I would prefer not to write an interpolation from scratch, but if someone wants to point me to pretty good (and explicit) recipe I'll give it a shot. It's not the most hateful thing to write (at least in an algorithmic sense), but I don't want to reinvent the wheel.
Effectively what I have is scattered terrain locations, and I wish to define a rectilinear mesh that nominally follows some distance beneath the topography for use later. Once I have the node points, I will be good.
My research so far:
The question asked here: MATLAB functions in C++ produced a close answer, but unfortunately the suggestion was not free (SciMath).
I have tried understanding the interpolation function used in Generic Mapping Tools, and was rewarded with a headache.
I briefly looked into the Grid Algorithms library (GrAL). If anyone has commentary I would appreciate it.
Eigen has an unsupported interpolation package, but it seems to just be for curves (not surfaces).
Edit: VTK has a matplotlib functionality. Presumably there must be an interpolation used somewhere in that for display purposes. Does anyone know if that's accessible and usable?
Thank you.
This is probably a little late, but hopefully it helps someone.
Method 1.) Octave: If you're coming from Matlab, one way is to embed the gnu Matlab clone Octave directly into the c++ program. I don't have much experience with it, but you can call the octave library functions directly from a cpp file.
See here, for instance. http://www.gnu.org/software/octave/doc/interpreter/Standalone-Programs.html#Standalone-Programs
griddata is included in octave's geometry package.
Method 2.) PCL: They way I do it is to use the point cloud library (http://www.pointclouds.org) and VoxelGrid. You can set x, and y bin sizes as you please, then set a really large z bin size, which gets you one z value for each x,y bin. The catch is that x,y, and z values are the centroid for the points averaged into the bin, not the bin centers (which is also why it works for this). So you need to massage the x,y values when you're done:
Ex:
//read in a list of comma separated values (x,y,z)
FILE * fp;
fp = fopen("points.xyz","r");
//store them in PCL's point cloud format
pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
int numpts=0;
double x,y,z;
while(fscanf(fp, "%lg, %lg, %lg", &x, &y, &z)!=EOF)
{
pcl::PointXYZ basic_point;
basic_point.x = x; basic_point.y = y; basic_point.z = z;
basic_cloud_ptr->points.push_back(basic_point);
}
fclose(fp);
basic_cloud_ptr->width = (int) basic_cloud_ptr->points.size ();
basic_cloud_ptr->height = 1;
// create object for result
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>());
// create filtering object and process
pcl::VoxelGrid<pcl::PointXYZ> sor;
sor.setInputCloud (basic_cloud_ptr);
//set the bin sizes here. (dx,dy,dz). for 2d results, make one of the bins larger
//than the data set span in that axis
sor.setLeafSize (0.1, 0.1, 1000);
sor.filter (*cloud_filtered);
So that cloud_filtered is now a point cloud that contains one point for each bin. Then I just make a 2-d matrix and go through the point cloud assigning points to their x,y bins if I want an image, etc. as would be produced by griddata. It works pretty well, and it's much faster than matlab's griddata for large datasets.

OpenCV, C++: Distance between two points

For a group project, we are attempting to make a game, where functions are executed whenever a player forms a set of specific hand gestures in front of a camera. To process the images, we are using Open-CV 2.3.
During the image-processing we are trying to find the length between two points.
We already know this can be done very easily with Pythagoras law, though it is known that Pythagoras law requires much computer power, and we wish to do this as low-resource as possible.
We wish to know if there exist any build-in function within Open-CV or standard library for C++, which can handle low-resource calculations of the distance between two points.
We have the coordinates for the points, which are in pixel values (Of course).
Extra info:
Previous experience have taught us, that OpenCV and other libraries are heavily optimized. As an example, we attempted to change the RGB values of the live image feed from the camera with a for loop, going through each pixel. This provided with a low frame-rate output. Instead we decided to use an Open-CV build-in function instead, which instead gave us a high frame-rate output.
You should try this
cv::Point a(1, 3);
cv::Point b(5, 6);
double res = cv::norm(a-b);//Euclidian distance
As you correctly pointed out, there's an OpenCV function that does some of your work :)
(Also check the other way)
It is called magnitude() and it calculates the distance for you. And if you have a vector of more than 4 vectors to calculate distances, it will use SSE (i think) to make it faster.
Now, the problem is that it only calculate the square of the powers, and you have to do by hand differences. (check the documentation). But if you do them also using OpenCV functions it should be fast.
Mat pts1(nPts, 1, CV_8UC2), pts2(nPts, 1, CV_8UC2);
// populate them
Mat diffPts = pts1-pts2;
Mat ptsx, ptsy;
// split your points in x and y vectors. maybe separate them from start
Mat dist;
magnitude(ptsx, ptsy, dist); // voila!
The other way is to use a very fast sqrt:
// 15 times faster than the classical float sqrt.
// Reasonably accurate up to root(32500)
// Source: http://supp.iar.com/FilesPublic/SUPPORT/000419/AN-G-002.pdf
unsigned int root(unsigned int x){
unsigned int a,b;
b = x;
a = x = 0x3f;
x = b/x;
a = x = (x+a)>>1;
x = b/x;
a = x = (x+a)>>1;
x = b/x;
x = (x+a)>>1;
return(x);
}
This ought to a comment, but I haven't enough rep (50?) |-( so I post it as an answer.
What the guys are trying to tell you in the comments of your questions is that if it's only about comparing distances, then you can simply use
d=(dx*dx+dy*dy) = (x1-x2)(x1-x2) + (y1-y2)(y1-y2)
thus avoiding the square root. But you can't of course skip the square elevation.
Pythagoras is the fastest way, and it really isn't as expensive as you think. It used to be, because of the square-root. But modern processors can usually do this within a few cycles.
If you really need speed, use OpenCL on the graphics card for image processing.

openGL for matrix stack

I have a win32 application, in which I want to use openGL just for its matrix stack not for any rendering. That is, I want to use openGL to specify the camera, viewport etc so that I dont have to do the maths again. While creating the scene, I just want to project the points using gluProject and use it. The projected points are passed to another library which creates the scene for me, all the windows handles are created by library itself and I dont have access to that.
The problem is, windows needs a device context for initialization. But, since I am not using openGL for any rendering, is there a way to use openGL without any Window handle at all?
Without any explicit initialization, when I read back the matrices using glGet, it returns a garbage. Any thought on how to fix it?
I want to use openGL just for its matrix stack not for any rendering.
That's not what OpenGL is meant for. OpenGL is a drawing/rendering API, not a math library. Actually the whole matrix math stuff has been stripped away from the latest OpenGL versions (OpenGL-3 core and later), for that very reason.
Also doing this matrix math stuff is so simple, you can write it down in less than 1k lines of C code. There's absolutely no benefit in abusing OpenGL for this.
The Matrix stack could potentially live on graphics hardware in your implementation. OpenGL is quite reasonable therefore in insisting you have an OpenGL context in order to be able to use such functions. This is because the act of creating a context probably includes setting up the necessary implementation mechanics required to store the matrix stack.
Even in a purely software based OpenGL implementation one would still expect the act of creating a context to call some equivalent to malloc to secure the storage space for the stack. If you happened to find an OpenGL implementation where creating a context wasn't necessary I'd still keep clear of relying on that behavior since it's most likely undefined and could be broken in the next release of that implementation.
If it's C++ I'd just use std::stack with the Matrix class from your favorite linear algebra package if you're not using OpenGL for anything other than that.
I present to you my complete (open source) matrix class. Enjoy.
https://github.com/TheBuzzSaw/paroxysm/blob/master/newsource/CGE/Matrix4x4.h
I can recommend trying to implement those calls yourself. I did that once for a Palm app I wrote, tinyGL. What I learnt was that the documentation basically tells you in plain text what is done.
i.e the verbatim code for tglFrustum and tglOrth are (note that I was using fix point math to get some performance)
void tglFrustum(fix_t w, fix_t h, fix_t n, fix_t f) {
matrix_t fm, m;
fix_t f_sub_n;
f_sub_n = sub_fix_t(f,n);
fm[0][0] = mult_fix_t(_two_,div_fix_t(n,w));
fm[0][1] = 0;
fm[0][2] = 0;
fm[0][3] = 0;
fm[1][0] = 0;
fm[1][1] = mult_fix_t(_two_,div_fix_t(n,h));
fm[1][2] = 0;
fm[1][3] = 0;
fm[2][0] = 0;
fm[2][1] = 0;
fm[2][2] = inv_fix_t(div_fix_t(add_fix_t(f,n),f_sub_n));
f = mult_fix_t(_two_,f);
fm[2][3] = inv_fix_t(div_fix_t(mult_fix_t(f,n),f_sub_n));
fm[3][0] = 0;
fm[3][1] = 0;
fm[3][2] = _minus_one_;
fm[3][3] = 0;
set_matrix_t(m,_matrix_stack[_toms]);
mult_matrix_t(_matrix_stack[_toms],m,fm);
}
void tglOrtho(fix_t w, fix_t h, fix_t n, fix_t f) {
matrix_t om, m;
fix_t f_sub_n;
f_sub_n = sub_fix_t(f,n);
MemSet(om,sizeof(matrix_t),0);
om[0][0] = div_fix_t(_two_,w);
om[1][1] = div_fix_t(_two_,h);
om[2][2] = div_fix_t(inv_fix_t(_two_),f_sub_n);
om[2][3] = inv_fix_t(div_fix_t(add_fix_t(f,n),f_sub_n));
om[3][3] = _one_;
set_matrix_t(m,_matrix_stack[_toms]);
mult_matrix_t(_matrix_stack[_toms],m,om);
}
Compare those with the man pages for glFrustum and glOrtho