Did you know LQ has a Linux Hardware Compatibility List?
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org A Funny C++ Bug
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 07-23-2008, 07:18 AM #1 Asuralm LQ Newbie   Registered: Nov 2007 Posts: 26 Rep: 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 << "x=" << x << " y=" << y << 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 << "x=" << x << " y=" << y << 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
 07-23-2008, 08:07 AM #2 PTrenholme Senior Member Contributing Member   Registered: Dec 2004 Location: Olympia, WA, USA Distribution: Fedora, (K)Ubuntu Posts: 4,157 Rep: 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?
07-23-2008, 08:17 AM   #3
Asuralm
LQ Newbie

Registered: Nov 2007
Posts: 26

Original Poster
Rep:
Quote:
 Originally Posted by PTrenholme 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?

 07-24-2008, 02:56 AM #4 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: is p0 set up?
 07-24-2008, 04:23 AM #5 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: Also what does GetNormal() return? a1 = GetNormal()[0]; an array? GetNormal() * (*p0); a scalar?
07-24-2008, 05:42 AM   #6
Member

Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep:
Quote:
 Originally Posted by Asuralm 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...