LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   A Funny C++ Bug (http://www.linuxquestions.org/questions/programming-9/a-funny-c-bug-657669/)

 Asuralm 07-23-2008 08:18 AM

A Funny C++ Bug

Hi all:

I wrote a piece of code for calculating the intersection between two planes. Here are my code:

Code:

```bool ML_Plane::IntersectionPlane(ML_Plane& plane, ML_Line& line){     if ( Parallel(plane) ) return false;     double a1, b1, c1, d1;     double a2, b2, c2, d2;         a1 = GetNormal()[0]; b1 = GetNormal()[1]; c1 = GetNormal()[2];     d1 = GetNormal() * (*p0);         a2 = plane.GetNormal()[0]; b2 = plane.GetNormal()[1]; c2 = plane.GetNormal()[2];     d2 = plane.GetNormal() * plane[0];         double x=0;     double y=0;     if ( abs(a1*b2-a2*b1) > EPSILON){         x = (b1*d2-b2*d1)/(a1*b2-a2*b1);         y = (a2*d1-a1*d2)/(a1*b2*a2*b1);     }     //cout << "x=" << x << " y=" << y << endl;         ML_Vertex O(x,y,0);     ML_Vertex P = O + Cross(GetNormal(), plane.GetNormal() );     line = ML_Line(O,P);     return true; }```
Note that there is a line I comment out
//cout &lt;&lt; "x=" &lt;&lt; x &lt;&lt; " y=" &lt;&lt; y &lt;&lt; endl;

Here is the driven code:
Code:

```    ML_Vertex A(1,0,0), n1(0,1,0);     ML_Vertex n2(0,0,1);     ML_Plane p1(A, n1), p2(A, n2);         ML_Line line;     p1.IntersectionPlane(p2, line);     cout << line[0] << endl;     cout << line.GetNormal() << endl;```
line[0] is a point on the line basically. This calculates the intersection between X plane and Y Plane.

The problem is:
when I comment the line " cout &lt;&lt; "x=" &lt;&lt; x &lt;&lt; " y=" &lt;&lt; y &lt;&lt; endl;" out, the result is:
0 0 0
-4.94321e-37 -1 8.38816e-38

which is wrong.

But if I leave the line uncommented, the result is :
x=0 y=0
0 0 0
1 -1.52385e-314 0

which is correct, the intersection line normal direction is X axis.

Can anyone tell me how to solve the problem please?

My compiler is GNU C++ 4.1.2

Thanks

 PTrenholme 07-23-2008 09:07 AM

Not an answer to your question, but (if the code you presented is the code you actually use) the denominator in your equation for y appears to be incorrect.

Also, when you calculate the cross product, you seem to be using GetNormal both as a function in the "plane" class and as a stand-alone function. Is it really defined that way?

 Asuralm 07-23-2008 09:17 AM

Quote:
 Originally Posted by PTrenholme (Post 3223674) Not an answer to your question, but (if the code you presented is the code you actually use) the denominator in your equation for y appears to be incorrect. Also, when you calculate the cross product, you seem to be using GetNormal both as a function in the "plane" class and as a stand-alone function. Is it really defined that way?

Thanks for your reply. The denominator for y is a typing error, and it's not the problem.

The GetNormal() is a function in the ML_Plane Class, and it can be considered as:

Cross(this->GetNormal(), plane.GetNormal() );

Note that the signature of the function is :

bool ML_Plane::IntersectionPlane(ML_Plane& plane, ML_Line& line);

I have tried many different ways but the problem is still there.

Could this be the problem of the compiler's optimization problem?

 graemef 07-24-2008 03:56 AM

is p0 set up?

 graemef 07-24-2008 05:23 AM

Also what does GetNormal() return?

a1 = GetNormal()[0]; an array?
GetNormal() * (*p0); a scalar?

 vladmihaisima 07-24-2008 06:42 AM

Quote:
 Originally Posted by Asuralm (Post 3223678) Could this be the problem of the compiler's optimization problem?
Try to compile with -O0 (disable optimizations). Also it would be helpfull if you would post the whole source so others can try maybe...

 All times are GMT -5. The time now is 11:46 AM.