LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-23-2008, 07:18 AM   #1
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Rep: Reputation: 15
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
 
Old 07-23-2008, 08:07 AM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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?
 
Old 07-23-2008, 08:17 AM   #3
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by PTrenholme View Post
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?
 
Old 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: Reputation: 148Reputation: 148
is p0 set up?
 
Old 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: Reputation: 148Reputation: 148
Also what does GetNormal() return?

a1 = GetNormal()[0]; an array?
GetNormal() * (*p0); a scalar?
 
Old 07-24-2008, 05:42 AM   #6
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: 2008 CES: Bug Labs Introduces BUG, BUGbase. So Cool! LXer Syndicated Linux News 0 01-09-2008 01:21 AM
2038 bug-Is Debian Bug-Proof? deepclutch Debian 1 08-02-2007 10:59 AM
FC4's sci_calculator has a funny bug! snow_in_quarks Linux - Software 0 03-05-2006 09:13 AM
LAMP with OpenLdap-Support; Bug//Funny error // SOLVED Trucker Linux - Software 0 02-27-2003 10:27 AM
LAMP with OpenLdap-Support; Bug//Funny e Trucker Linux - Software 0 02-27-2003 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration