I am trying to do something as simple as:
std::cout << e << std::endl;
where e is of type Eigen::Affine3d. However, I am getting unhelpful error messages like:
cannot bind 'std::ostream {aka std::basic_ostream<char>}'
lvalue to 'std::basic_ostream<char>&&'
The reason for which is helpfully explained here, but where the answer does not apply.
The official documentation is curt, implying only that Affine3d and Affine3f objects are matrices. Eigen matrices and vectors can be printed by std::cout without issue though. So what is the problem?
Annoyingly, the << operator is not defined for Affine objects. You have to call the matrix() function to get the printable representation:
std::cout << e.matrix() << std::endl;
If you're not a fan of homogenous matrices:
Eigen::Matrix3d m = e.rotation();
Eigen::Vector3d v = e.translation();
std::cout << "Rotation: " << std::endl << m << std::endl;
std::cout << "Translation: " << std::endl << v << std::endl;
Hopefully someone can save a few minutes of annoyance.
PS:Another lonely SO question mentioned this solution in passing.
To be honest, I would prefer to overload the stream operator. This makes the repeated use more convinient. This you can do like this
std::ostream& operator<<(std::ostream& stream, const Eigen::Affine3d& affine)
{
stream << "Rotation: " << std::endl << affine.rotation() << std::endl;
stream << "Translation: " << std::endl << affine.translation() << std::endl;
return stream;
}
int main()
{
Eigen::Affine3d l;
std::cout << l << std::endl;
return 0;
}
Be aware that l is uninitialized
Related
I have a transformation matrix of type Eigen::Matrix4d. And I would like to get its inverse. I write a function my self to compute it by the following formular.
.
And here is my code:
Eigen::Matrix4d inverseTransformation(Eigen::Matrix4d T)
{
Eigen::MatrixX3d R;
Eigen::Vector3d t;
R = T.block<3, 3>(0, 0);
t = T.block<3, 1>(0, 3);
Eigen::Matrix4d result;
result.setIdentity();
result.block<3, 3>(0, 0) = R.transpose();
result.block<3, 1>(0, 3) = -R.transpose() * t;
return result;
}
std::cout<<"Input transformation matrix is:\n" << T << std::endl;
std::cout << "inverse of T , my implementation:\n" << inverseTransformation(T) << std::endl << std::endl;
std::cout << "inverse of T , Eigen implementation::\n" << T.inverse() << std::endl << std::endl;
std::cout << "T * T^(-1), my implementation\n " << T * inverseTransformation(T) << std::endl << std::endl;
std::cout << "T * T^(-1), eigen's implementation\n " << T * T.inverse() << std::endl << std::endl;
Ideally, T * T^(1) should be I. However, there is some error in my result (the red part in the following picture.)
By contrast, the result from T * T.inverse() is much more accurate.
Could someone please tell me why? Thanks a lot in advance!
Update:
Inverse of a rotation matrix is its transpose. The result will be more accurate if I replace R.tranpose() with R.inverse().
Thanks to the comments. Since I would like to close this question, I will answer it my self.
Theoretically, R.inverse() equals to R.tranpose(). But this is not correct in floating point system. Once I use R.inverse(), the result is more accurate.
on a project I'm working on we are trying to test out the line_walk() function and work with the Line_face_circulator. Would there be a way to visualise the faces in the Line_face_circulator? The problem that I am having is figuring out how to extract information from the circulator. I've tried some of the methods below which didn't work but the documentation for the LFC is a bit difficult to maneuver around.
Delaunay::Line_face_circulator lfc = dt.line_walk(Point_2(13, 166), Point_2(42, 1761));
std::cout << "linewalk res" << std::endl;
Container container(lfc);
Iterator i = container.begin();
std::cout << typeid(i).name() << std::endl;
//std::cout << (container.begin()->vertex(0)) << std::endl;
std::cout << lfc.is_empty() << std::endl;
//std::cout << lfc->vertex(0) << std::endl;
//std::cout << *i->vertex(0)->point() << std::endl;
while (i != container.end()){
std::cout << *i << std::endl;
i++;
}
May I ask if there are particular methods to extract the faces or the vertices of the faces from the line_face_circulator?
Thank you!
Something like the following should work:
Delaunay::Line_face_circulator lfc = dt.line_walk(Point_2(13, 166), Point_2(42, 1761)), start=lfc;
std::vector<Delaunay::Face_handle> visited_faces;
if (lfc!=nullptr)
{
do{
visited_face.push_back(lfc);
} while(++lfc!=start);
}
Circulator doc is here.
I am using a code that stores points into a Vector data type as follows:
RECT head;
head.left = pt1.x;
head.right = pt2.x;
head.top = pt1.y;
head.bottom = pt2.y;
detectBox->push_back(head);
This falls inside a function that has a for loop that stores multple instances of "head" into the detectBox. This function is delared like this:
void GetHeads(cv::Mat Img, vector<RECT>* detectBox)
Where Img is just a normal black and white image being fed in with the heads that have been extracted through other processes. My question now is how to I see the points that have been stored inside of detectBox? I would like to access them outside of the for loop to use for other things. When i try and print out the variables Ive only been able to have the addresses returned (0000004FA8F6F31821, etc)
Also, detectBox is a shade of grey in the code(not sure what that means).
Full code can be found in my other question related to the same function, here:
C++ CvSeq Accessing arrays that are stored
-----EDIT-----
Methods tried and associated errors/Outputs:
First:
std::cout << "Back : " << &detectBox->back << std::endl;
'&': illegal operation on bound member function expression
Second:
std::cout << "Back : " << detectBox->back << std::endl;
'std::vector>::back': non-standard syntax; use '&' to create a pointer to member
Third:(Note, No Error, but no useful information output)
std::cout << "Detect Box : " << detectBox << std::endl;
Detect Box : 00000062BF0FF488
Fourth:
std::cout << "Detect Box : " << &detectBox[1] << std::endl;
Detect Box : 000000CB75CFF108
First, detectBox is a pointer to the array so it needs to be dereferenced before trying to index into it.
std::cout << (*detectBox)[1]; // Outputs the 2nd RECT in the vector
// assuming an operator<< is implemented
// for RECT.
Including the address of '&' operator before it will print out the address of the 2nd RECT instance at index 1 which you do not need to do because the index operator gives you the reference.
std::cout << &(*detectBox)[1];
Without an operator<< for the RECT instance to output it to the stream you will need to access the members directly. The following should work:
std::cout << "Left: " << (*detectBox)[1].left;
std::cout << "Right: " << (*detectBox)[1].right;
std::cout << "Top: " << (*detectBox)[1].top;
std::cout << "Bottom: " << (*detectBox)[1].bottom;
This can be improved by saving off a reference to the RECT you are trying to get first and then using that:
RECT& secondRect = (*detectBox)[1];
std::cout << "Left: " << secondRect.left;
std::cout << "Right: " << secondRect.right;
std::cout << "Top: " << secondRect.top;
std::cout << "Bottom: " << secondRect.bottom;
Finally, I notice that you push the new RECT on the back of the Vector, but then you always output the 2nd RECT in the vector. Assuming you want to print the RECT you just added you could either output the head local variable or use the back() method on the vector as so:
RECT& lastRect = detectBox->back();
std::cout << "Left: " << lastRect.left;
std::cout << "Right: " << lastRect.right;
std::cout << "Top: " << lastRect.top;
std::cout << "Bottom: " << lastRect.bottom;
detectBox->push_back(head);
std::cout << &detectBox[1];
Unless you've got an overloaded operator "<<" for RECT (which you probably don't) it's going to try and match this to the best thing it can, which clearly isn't going to be what you want it to be.
Try something more like
std::cout << "L: " << &detectBox[1].left <<
"R: " << &detectBox[1].right << std::endl
I'd also be worried that you're using [1] when what you probably want is ->back()
This is my third question on this topic, Instead of asking a new question in the comments I thought it would be better to start a new thread.
The full code can be found here:
C++ CvSeq Accessing arrays that are stored
And using the following code I can display the most recent vector that has been added to the RECT array(Note that this is placed inside of the for loop):
RECT& lastRect = detectBox->back();
std::cout << "Left: " << lastRect.left << std::endl;
std::cout << "Right: " << lastRect.right << std::endl;
std::cout << "Top: " << lastRect.top << std::endl;
std::cout << "Bottom: " << lastRect.bottom << std::endl;
What I am now trying to do is create a loop outside of this for loop that will display all of the vectors present in detectBox. I havent been able to determine how many vectors are actually present in the array, and therefore cannot loop through the vectors.
I tried using the following:
int i = 0;
while ((*detectBox)[i].left!=NULL)
{
std::cout << "Left: " << (*detectBox)[i].left << std::endl;
std::cout << "Right: " << (*detectBox)[i].right << std::endl;
std::cout << "Top: " << (*detectBox)[i].top << std::endl;
std::cout << "Bottom: " << (*detectBox)[i].bottom << std::endl;
i++;
}
And have also tried playing around with sizeof(*detectBox) , but only have an answer of 32 being returned...
Okay, you are using the wrong terms here. The variable detectBox is a vector (or rather a pointer to a vector it seems). There are three ways to iterate over it (I'll show them a little later). It is not an array, it is not an array of vectors. It is a pointer to a vector of RECT structures.
Now as for how to iterate over the vector. It is like you iterate over any vector.
The first way is to use the C way, by using indexes:
for (unsigned i = 0; i < detectBox->size(); ++i)
{
RECT rect = detectBox->at(i);
std::cout << "Left: " << rect.left << std::endl;
...
}
The second way is the traditional C++ way using iterators:
for (std::vector<RECT>::iterator i = detectBox->begin();
i != detectBox->end();
++i)
{
std::cout << "Left: " << i->left << std::endl;
...
}
The last way is to use range for loops introduced in the C++11 standard:
for (RECT const& rect : *detectBox)
{
std::cout << "Left: " << rect.left << std::endl;
...
}
The propblem with your attempt of the loop, with the condition (*detectBox)[i].left!=NULL is that the member variable left is not a pointer and that when you go out of bounds you are not guaranteed to have a "NULL" value (instead it will be indeterminate and will seem random).
I have been searching on Google an in this forum for a while, but I could not find any answer or tip for my problem. Tutorials couldn't help me either...
I want to redistribute some points, stored in a vector p_org. (x-value is stored as double).
Therefore I have the function distribute, which is defined in maths.h
distribute_tanh(&p_org_temp,&p_new_temp,iz,spacing[0],spacing[1],l_rot[(kk+1)*iz-2],status);
The function distribute_tanh does look like this:
inline void distribute_tanh (std::vector<double> *p_org, std::vector<double> *p_new, const int n_points, double spacing_begin, double spacing_end, const double total_length, double status){
//if status == 0: FLAP, if status == 1: SLAT
std::cout << "spacing_begin: " << spacing_begin << " spacing_end: " << spacing_end << std::endl;
double s_begin = spacing_begin / total_length;
double s_end = spacing_end / total_length;
double A = sqrt(s_end/s_begin);
double B = 1 / (sqrt(s_end*s_begin)*n_points);
std::cout << "A: " << A << " B: " << B << std::endl;
std::vector<double> u (n_points);
std::vector<double> sn (n_points);
double dx;
double dy;
std::cout << "Control at the beginning: p_org: " << (p_org) << " p_new: " << (p_new) << " n_points: " << n_points << " s_begin: " << s_begin << " s_end: " << s_end << " total_length: " << total_length << std::endl;
//problem no. 1
for (int i=0;i<n_points;i++){
if (B > 1.001) {
if (B < 2.7829681) {
double Bq=B-1;
dy=sqrt(6*Bq)*(1-0.15*Bq+0.057321429*pow(Bq,2)-0.024907295*pow(Bq,3)+0.0077424461*pow(Bq,4)-0.0010794123*pow(Bq,5));
} else if (B > 2.7829681) {
double Bv=log(B);
double Bw=1/B-0.028527431;
dy=Bv+(1+1/Bv)*log(2*Bv)-0.02041793+0.24902722*Bw+1.9496443*pow(Bw,2)-2.6294547*pow(Bw,3)+8.56795911*pow(Bw,4);
}
u[i]=0.5+(tanh(dy*(i*(1.0/n_points)-0.5))/(2*tanh(dy/2)));
}
else if (B < 0.999) {
if (B < 0.26938972) {
dx=M_PI*(1-B+pow(B,2)-(1+(pow(M_PI,2))/6)*pow(B,3)+6.794732*pow(B,4)-13.205501*pow(B,5)+11.726095*pow(B,6));
} else if (B > 0.26938972) {
double Bq=1-B;
dx=sqrt(6*Bq)*(1+0.15*Bq+0.057321429*pow(Bq,2)+0.048774238*pow(Bq,3)-0.053337753*pow(Bq,4)+0.075845134*pow(Bq,5));
}
u[i]=0.5+(tan(dx*(i*(1.0/n_points)-0.5))/(2*tan(dx/2)));
}
else {
u[i]=i*(1.0/n_points)*(1+2*(B-1)*(i*(1.0/n_points)-0.5)*(1-i*(1.0/n_points)));
}
sn[i]=u[i]/(A+(1.0-A)*u[i]);
std::cout << "sn(i): " << sn[i] << std::endl;
std::cout << "p_org[n_points]: " << &p_org[n_points-1] << std::endl;
if(status==0){
//p_new[i]=p_org[0]+(total_length*sn[i]);
std::cout << "FLAP maths.h" << std::endl;
}
//Here is the problem no. 2
else if(status==1){
//p_new[i]=p_org[0]-(total_length*sn[i]);
std::cout << "SLAT maths.h" << std::endl;
}
//std::cout << "p_new in math: " << p_new << std::endl;
}
}
My problem is, that I am unable to access the value of p_org or p_new. At the beginning I would like to give out the value of p_org and p_new. If I try it with a *, the compiler is complaining: error: no operator "<<" matches these operands
operand types are: std::basic_ostream> << std::vector>
std::cout << "Control at the beginning: p_org: " << (*p_org) << " p_new: " << (*p_new) << " n_points: " << n_points << " s_begin: " << s_begin << " s_end: " << s_end << " total_length: " << total_length << std::endl;
If I leave the * off, I get the addresses of p_org and p_new.
At the end of the code I would like to write the new value to p_new. If I use * to access the value, the compiler is complaining, if I leave it off, its complaining too with the following message:
error: no operator "-" matches these operands
operand types are: std::vector<double, std::allocator<double>> - double
p_new[i]=p_org[0]-(total_length*sn[i]);
^
I tried to understand both problems, but until now I had no success.
Thanks for your advice.
Your issue with the compiler error can be cut down to a very simple program.
#include <vector>
void foo(std::vector<int>* pV)
{
pV[0] = 10; // error.
}
int main()
{
std::vector<int> v(10);
foo(&v);
}
The issue is that operator[] as done above works for objects and references, not pointers. Since pv is a pointer, you must dereference it first to obtain the object, and then apply [] to the dereferenced pointer.
void foo(std::vector<int>* pV)
{
(*pV)[0] = 10; // No error
}
The other form of calling operator[] can be also used, but is a bit more verbose:
void foo(std::vector<int>* pV)
{
pv->operator[](0) = 10; // No error
}
However, to alleviate having to do this, pass the vector by reference. Then the "normal" way of using operator[] can be used.
#include <vector>
void foo(std::vector<int>& pV)
{
pV[0] = 10; // No error.
}
int main()
{
std::vector<int> v(10);
foo(v);
}