Relation between QTransform scale and boundingRect.size() - c++

I have little concern about what is relation between QTransform scale and width and height return values in boundingRect() method of QGraphicsItem.
Actually I want to scale QGraphicsItem as its boundingRect size. i.e. if size of my item initially 100,100 that I am passing in boundingRect() method after that I am increasing the size of item by mousemove event. If my increased width and height is 400,300 respectively is my scale factors 4,3?
Any help will be appreciable.
This is code
this->setPos(minMax().first.x(), minMax().first.y());
qreal w = minMax().second.x() - minMax().first.x();
qreal h = minMax().second.y() - minMax().first.y();
qreal scaleFactorW = w / boundingRect().width();
qreal scaleFactorH = h / boundingRect().height();
QTransform trans;
trans.scale(scaleFactorW, scaleFactorH);
setTransform(trans);
bottomPoints = QPointF(w, h);
minMax function is:
float xMin = 0, xMax = 0, yMin = 0, yMax = 0;
QList<double> xValues, yValues;
xValues << shaper[0]->scenePos().x() << shaper[1]->scenePos().x() << shaper[2]->scenePos().x() << shaper[3]->scenePos().x() << shaper[4]->scenePos().x() << shaper[5]->scenePos().x() << shaper[6]->scenePos().x() << shaper[7]->scenePos().x();
yValues << shaper[0]->scenePos().y() << shaper[1]->scenePos().y() << shaper[2]->scenePos().y() << shaper[3]->scenePos().y() << shaper[4]->scenePos().y() << shaper[5]->scenePos().y() << shaper[6]->scenePos().y() << shaper[7]->scenePos().y();
qSort(xValues.begin(), xValues.end());
qSort(yValues.begin(), yValues.end());
xMin = xValues.first();
xMax = xValues.last();
yMin = yValues.first();
yMax = yValues.last();
return qMakePair(QPointF(xMin, yMin), QPointF(xMax, yMax));
shaper is qgraphicsitem by which I am resizing item.
Thanks :)

Thanks for showing your code.
As I said before,
trans.scale(scaleFactorW, scaleFactorH);
won't change the size returned by QGraphicsItem::boundingRect.
But in fact, QGraphicsItem::setScale has the same behaviour and the boundingRect() for the item doesn't change either.
QTransform::scale and QGraphicsItem::setScale are not the same, but both are useful to change the image size. Well, in the case of QTransform, you're scaling the coordinate system.
I think an example is the best way of explaining myself.
(this inherits QGraphicsItem)
qWarning() << "QGraphicsItem::scale(): " << this->scale();
QRectF br = this->boundingRect();
qWarning() << "QGraphicsItem::boundingRect() size / x / y / w / h: " << br.size() << " / "
<< br.x() << " / "
<< br.y() << " / "
<< br.width() << " / "
<< br.height();
QTransform trans = this->transform();
trans.scale(2.0, 2.0);
this->setTransform(trans);
/*
Comment trans.scale(2.0, 2.0) and uncomment the following line
to check the difference using the logs.
*/
// this->setScale(2.0);
qWarning() << "QGraphicsItem::scale(): " << this->scale();
br = this->boundingRect();
qWarning() << "QGraphicsItem::boundingRect() size / x / y / w / h: " << br.size() << " / "
<< br.x() << " / "
<< br.y() << " / "
<< br.width() << " / "
<< br.height();
qWarning() << "boundingRect * item_scale: " << this->boundingRect().size() * this->scale();

Related

How to do color filtering with PCL

I am learning to use pcl.I want to filter out point clouds whose color is red(rgb 255,0,0),But not work.what should I do?
The PCL version I am using is 1.12.1.
#include <pcl/point_types.h>
#include <pcl/filters/conditional_removal.h>
int main()
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(new
pcl::PointCloud<pcl::PointXYZRGB>);
cloud->width = 5;
cloud->height = 1;
cloud->points.resize((cloud->width) * (cloud->height));
//creat point cloud
for (size_t i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].r = 110;
cloud->points[i].g = 110;
cloud->points[i].b = 110;
}
//set rbg 255,0,0
cloud->points[2].r = 255;cloud->points[2].g = 0;cloud->points[2].b = 0;
pcl::ConditionAnd<pcl::PointXYZRGB>::Ptr range_cond(new pcl::ConditionAnd<pcl::PointXYZRGB>());
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("r", pcl::ComparisonOps::EQ, 255)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("g", pcl::ComparisonOps::EQ, 0)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("b", pcl::ComparisonOps::EQ, 0)));
pcl::ConditionalRemoval<pcl::PointXYZRGB> condrem;
condrem.setCondition(range_cond);
condrem.setInputCloud(cloud);
condrem.setKeepOrganized(true);
condrem.filter(*cloud_filtered);
std::cerr << "Cloud before filtering: " << std::endl;
for (size_t i = 0; i < cloud->points.size(); ++i)
std::cerr << " " << cloud->points[i].x << " "
<< cloud->points[i].y << " "
<< cloud->points[i].z << " "
<< (int)cloud->points[i].r << " "
<< (int)cloud->points[i].g << " "
<< (int)cloud->points[i].b << std::endl;
std::cerr << "Cloud after filtering: " << std::endl;
for (size_t i = 0; i < cloud_filtered->points.size(); ++i)
std::cerr << " " << cloud_filtered->points[i].x << " "
<< cloud_filtered->points[i].y << " "
<< cloud_filtered->points[i].z << " "
<< (int)cloud_filtered->points[i].r << " "
<< (int)cloud_filtered->points[i].g << " "
<< (int)cloud_filtered->points[i].b << std::endl;
return (0);
}
Cloud after filtering is None.
result:
enter image description here
The warning message gives you a hint: "field not found!" (three times). PointXYZRGB does not have r, g, and b fields. You can use getFields() to find out which fields a point type has. PointXYZRGB has a combined field rgb that you can use for filtering. However, you might want to consider using PointXYZRGBA instead (with field rgba) because the other point type uses a float for storage (historic reasons) and I am not sure how well that works with filtering.
My modified code is here:
//creat point cloud
for (size_t i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cstd::uint32_t rgb = ((std::uint32_t)110 << 16 | (std::uint32_t)110 << 8 | (std::uint32_t)110);
cloud->points[i].rgb = *reinterpret_cast<float*>(&rgb);
}
//set red rbg 255,0,0
std::uint32_t red_rgb = ((std::uint32_t)255 << 16 | (std::uint32_t)0 << 8 | (std::uint32_t)0);
cloud->points[2].rgb = *reinterpret_cast<float*>(&red_rgb);
std::cerr << " FieldsList:" << pcl::getFieldsList(*cloud) <<std::endl;
pcl::ConditionAnd<pcl::PointXYZRGB>::Ptr range_cond(new pcl::ConditionAnd<pcl::PointXYZRGB>());
std::uint32_t rgb_filter = ((std::uint32_t)255 << 16 | (std::uint32_t)0 << 8 | (std::uint32_t)0);
//Add a comparison operator less than (255,0,0) on the rgb field
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("rgb", pcl::ComparisonOps::LT, *reinterpret_cast<float*>(&rgb_filter))));
Final result:
enter image description here

C++ round(49.5) returns 49 when called with variable (in Qt)

I wrote the following code:
void InputNodeInstance::changeSize(QString side, float mult) {
qDebug() << "width: " << width;
double widthMultiplied = width * mult;
qDebug() << "width * mult: " << width * mult;
qDebug() << "widthMultiplied: " << widthMultiplied;
qDebug() << "round(widthMultiplied): " << round(widthMultiplied);
qDebug() << "round(49.5): " << round(49.5);
width = width * mult + 0.5;
qDebug() << "width: " << width;
}
And when I call the function with 0.9 as mult, I get this output:
width: 55
width * mult: 49.5
widthMultiplied: 49.5
round(widthMultiplied): 49
round(49.5): 50
width: 49
I guess the question is clear.
widthMultiplied is 49.5.
round(49.5) returns 50.
round(widthMultiplied) returns 49.
Why?
That happens due to inaccuracy of the floating point numbers calculation. More info here and here.
The result of the width * mult multiplication is not exact 49.5 but something like 49.4999999, which is the reason for rounding your number to 49, not 50. Try it youself by setting the higher precision for your qDebug output:
qDebug() << qSetRealNumberPrecision(20) << widthMultiplied;
Read kefir's answer for background info.
I think a full working solution (while you don't go too far with your numbers) could be:
int number = 55;
double mult = 0.9;
double roundedNumber = round( static_cast<double>( static_cast<int>( number * mult * 100. ) ) / 100 );

Why are the matrices not initialized/printed correctly?

I am playing around with cv::Mat and think my code really behaves weird, although I follow the syntax described in here.
Code:
std::cout << "parameter for matrices: " << "x = " << X << " y = " << Y << " psi = " << Psi << std::endl;
double dataRot[] = { cos(Psi), -sin(Psi), sin(Psi), cos(Psi) };
double dataTrans[] = { X, Y };
cv::Mat matRot(2, 2, CV_32FC1, dataRot);
cv::Mat matTrans(2, 1, CV_32FC1, dataTrans);
std::cout << "matRot = " << matRot.at<double>(0,0) << "," << matRot.at<double>(0,1) << ";" << matRot.at<double>(1,0) << "," << matRot.at<double>(1,1) << std::endl;
std::cout << "matRot = " << matRot << std::endl;
std::cout << "matTrans = " << matTrans.at<double>(0,0) << "," << matTrans.at<double>(0,1) << std::endl;
std::cout << "matTrans = " << matTrans << std::endl;
matOut = matRot*matIn + matTrans*cv::Mat::ones(1, matIn.cols, CV_32FC1);
Output:
parameter for matrices: x = 20.5 y = 20 psi = 0
matRot = 1,-0;-0,0
matRot = [0, 1.875;
0, -0]
matTrans = 20.5,20
matTrans = [0; 2.8203125]
Why is the identity matrix not initalized correctly?
And why does the second way of printing a matrix deliver wrong results?
Any help is appreciated.
Since you're working with double, the OpenCV matrix type should be CV_64FC1:
cv::Mat matRot(2, 2, CV_64FC1, dataRot);
cv::Mat matTrans(2, 1, CV_64FC1, dataTrans);
For simplicity, you can also use:
cv::Matx22d matRot(cos(Psi), -sin(Psi), sin(Psi), cos(Psi));
cv::Matx21d matTrans(X, Y);
or:
cv::Mat1d matRot = (cv::Mat1d(2,2) << cos(Psi), -sin(Psi), sin(Psi), cos(Psi));
cv::Mat1d matTrans = (cv::Mat1d(2,1) << X, Y);
and access values like:
std::cout << matRot(row, col);

Rotating a vector around an axis with quaternion

I am trying to learn 3d programming, and right now I am trying to understand how to use quaternions to rotate a vector around an axis.
As far as I understand, to rotate a vector v around an axis a, after converting both vectors to quaternions, we multiply v by a, then the product by the conjugate of a.
I want to rotate v(0,1,0) around a(1,0,0) by 90 degrees, and I should get a resulting vector v(0,0,1) (or 0,0,-1, depending on the direction of the rotation).
I am not getting the output I am expecting.
Here is the code:
int main()
{
//I want to rotate this vector about the x axis by PI/2 radians:
Quaternion v(0, 1, 0, 0);
v.normalize();
float angle = PI / 2.0f;
float cos = math::cos(angle / 2.0f);
float sin = math::sin(angle / 2.0f);
Quaternion q(1.0f*sin, 0.0f*sin, 0.0f*sin, cos);
std::cout << "q not normalized = " <<"\t"<< q.x << " " << q.y << " " << q.z << " " << q.w << std::endl;
q.normalize();
std::cout << "q normalized = " <<"\t\t"<< q.x << " " << q.y << " " << q.z << " " << q.w << std::endl;
std::cout << std::endl;
Quaternion r;
//I multiply the vector v by the quaternion v, then I multiply by the conjugate.
r = q * v;
//do I need to normalize here?
r = r * q.conjugate();
//and here?
//shouldn't the resulting vector be 0,0,1?
std::cout << "r not normalized = " << "\t" << r.x << " " << r.y << " " << r.z << " " << r.w << std::endl;
r.normalize();
std::cout << "r normalized = " << "\t\t" << r.x << " " << r.y << " " << r.z << " " << r.w << std::endl;
std::cout << std::endl;
system("pause");
return 0;
}
and here is the output:
q not normalized, which is same as q normalized:
x = 0.707107, y = 0, z = 0, w = 0.707107
r not normalized:
x = 0.707107, y = 0, z = 1, w = -2.12132
r normalized:
x = 0.288675, y = 0, z = 0.408248, w = -0.866025
what am I doing wrong?
did I even understand anything from this process?
Basically to rotate an vector along x axis (1,0,0) with angle 90 deg, use below method, this works for both Euler and quaternion
| 1 0 0 | | 0 | | 0 |
| 0 cos90 -sin90 | * | 1 | = | 0 |
| 0 sin90 cos90 | | 0 | | 1 |
Read about rotation matrices http://en.wikipedia.org/wiki/Rotation_matrix

Dividing a Float by Itself Produces Very Large Integers

So I'm having what seems to me to be a very bizarre problem. I've got a crude system for applying forces to objects on 2D planes, and one of the simplest calculations seems to be causing one of my variables to overflow. I have the following line:
int ySign = m_Momentum.y / abs(m_Momentum.y);
Where Momentum has two data members, x y (m_Momentum is an SFML sf::Vector2 of floats). Now, normally the formula should always return either 1 or -1, depending on the sign of Momentum.y (unless I'm grossly mistaken).
However, it occasionally returns insanely high numbers such as -2147483648. In that particular case, the value of m_Momentum.y was 0.712165 (both values were obtained by sending to std::cout); I tried again, m_Momentum.y was -0.578988 and ySign was still -2147483648. There is a corresponding xSign that also flips out sometimes, often with the same final value. I can't confirm 100% that this is always the result, but at the moment that seems to be the case.
I'm sort of stumped as to why this is happening, and when it does, it basically invalidates my program (it instantly sends objects millions of pixels in the wrong direction). It seems logically impossible that the line above is returning such strange results.
Below is the function I am working on. Probably the wrong way to do it, but I didn't expect it to go so horribly wrong. The printout it produces reveals that all numbers look normal until the signs are printed out; one of them is invariably massive, and afterwards you see numbers like -2.727e+008 (which, as far as I'm aware, is scientific notation - i.e. -2.727 * 10 ^ 8).
///MODIFY MOMENTUM
//Reset, if necessary
if (Reset == true)
{
m_Momentum.x = 0;
m_Momentum.y = 0;
}
sf::Vector2<float> OldMoment = m_Momentum;
//Apply the force to the new momentum.
m_Momentum.x += Force.x;
m_Momentum.y += Force.y;
sf::Vector2<float> NewMoment = m_Momentum;
//Calculate total momentum.
float sqMomentum = m_Momentum.x * m_Momentum.x + m_Momentum.y * m_Momentum.y;
float tMomentum = sqrt(sqMomentum);
//Preserve signs for later use.
int xSign = m_Momentum.x / abs(m_Momentum.x);
int ySign = m_Momentum.y / abs(m_Momentum.y);
//Determine more or less the ratio of importance between x and y components
float xProp;
float yProp;
if (abs(tMomentum) > m_MaxVelocity)
{
//Get square of maximum velocity
int sqMax = m_MaxVelocity * m_MaxVelocity;
//Get proportion of contribution of each direction to velocity
xProp = (m_Momentum.x * m_Momentum.x) / sqMomentum;
yProp = (m_Momentum.y * m_Momentum.y) / sqMomentum;
//Reset such that the total does not exceed maximum velocity.
m_Momentum.x = sqrt(sqMax * xProp) * xSign;
m_Momentum.y = sqrt(sqMax * yProp) * ySign;
}
///SANITY CHECK
//Preserve old tMomentum
float tOld = tMomentum;
//Calculate current tMomentum
sqMomentum = m_Momentum.x * m_Momentum.x + m_Momentum.y * m_Momentum.y;
tMomentum = sqrt(sqMomentum);
//If it's still too high, print a report.
if (tMomentum > m_MaxVelocity)
{
std::cout << "\n\nSANITY CHECK FAILED\n";
std::cout << "-\n";
std::cout << "Old Components: " << OldMoment.x << ", " << OldMoment.y << "\n";
std::cout << "Force Components: " << Force.x << ", " << Force.y << "\n";
std::cout << "-\n";
std::cout << "New Components: " << NewMoment.x << ", " << NewMoment.y << "\n";
std::cout << "Which lead to...\n";
std::cout << "tMomentum: " << tOld << "\n";
std::cout << "-\n";
std::cout << "Found these proportions: " << xProp << ", " << yProp << "\n";
std::cout << "Using these signs: " << xSign << ", " << ySign << "\n";
std::cout << "New Components: " << m_Momentum.x << ", " << m_Momentum.y << "\n";
std::cout << "-\n";
std::cout << "Current Pos: " << m_RealPosition.x << ", " << m_RealPosition.y << "\n";
std::cout << "New Pos: " << m_RealPosition.x + m_Momentum.x << ", " << m_RealPosition.y + m_Momentum.y << "\n";
std::cout << "\n\n";
}
///APPLY FORCE
//To the object's position.
m_RealPosition.x += m_Momentum.x;
m_RealPosition.y += m_Momentum.y;
//To the sprite's position.
m_Sprite.Move(m_Momentum.x, m_Momentum.y);
Can somebody explain what's going on here?
EDIT: RedX helpfully directed me to the following post: Is there a standard sign function (signum, sgn) in C/C++? Which led me to write the following lines of code:
//Preserve signs for later use.
//int xSign = m_Momentum.x / abs(m_Momentum.x);
//int ySign = m_Momentum.y / abs(m_Momentum.y);
int xSign = (m_Momentum.x > 0) - (m_Momentum.x < 0);
int ySign = (m_Momentum.y > 0) - (m_Momentum.y < 0);
Thanks to the above, I no longer have the strange problem. For an explanation/alternative solution, see Didier's post below.
You should use fabs() instead of abs() to get the absolute value of a floating point number. If you use the integer absolute function, then the result is an integer ...
For instance, -0.5 / abs(-0.5) is treated as -0.5 / 0 which results in negative infinity (as a floating point value) that is converted to the minimum value of an int 0x80000000 = -2147483648
Taking absolute values and dividing sounds like an awful waste of cycles to me. What's wrong with
x > 0 ? 1 : -1
which you could always put in a function
template <class T>
inline int sgn(const T &x) { return x > 0 ? : 1; }