Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: Bounding Spheres

  1. #1
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Spain
    Posts
    273

    Bounding Spheres

    Hello, I'm programming the bounding spheres of some meshes, but i'm having troubles. This is my method: First I find te bounding box of the mesh (i need it for later tasks), then, with the max and minimun corner of the BBox, i find the center( center.x = (minCorner.x+maxCorner.x)/2;
    center.y = (minCorner.y+maxCorner.y)/2;
    center.z = (minCorner.z+maxCorner.z)/2
    and i go thought all vertices of the mesh, checking the distace form the center to the current vertex, at the end I have the radius and the center of the sphere, but...
    the mesh cut the sphere: www.telecable.es/personales/ffelagund/bbox.jpg www.telecable.es/personales/ffelagund/bbox3.jpg www.telecable.es/personales/ffelagund/bbox2.jpg

    with the cilinder all work ok but with the other mesh doesn't.
    Here the code:
    //Find max and minus corner of the BBOX:
    maxCorner.Assign(vertices[0],vertices[1],vertices[2]);
    minCorner.Assign(vertices[0],vertices[1],vertices[2]);

    for(j=0;i<nVertices;i++)
    {
    if(vertices[j]>maxCorner.x) maxCorner.x=vertices[j];
    if(vertices[j]<minCorner.x) minCorner.x=vertices[j];
    j++;

    if(vertices[j]>maxCorner.y) maxCorner.y=vertices[j];
    if(vertices[j]<minCorner.y) minCorner.y=vertices[j];
    j++;

    if(vertices[j]>maxCorner.z) maxCorner.z=vertices[j];
    if(vertices[j]<minCorner.z) minCorner.z=vertices[j];
    j++;
    }

    //find center of the sphere
    center.x = (minCorner.x+maxCorner.x)/2;
    center.y = (minCorner.y+maxCorner.y)/2;
    center.z = (minCorner.z+maxCorner.z)/2;

    //sphere render
    void BSphere::Render()
    {
    glPushMatrix();
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE,GL_ONE);
    GLUquadricObj *p = gluNewQuadric();
    glTranslatef(center.x,center.y,center.z);
    gluSphere(p,radius,30,30);
    gluDeleteQuadric(p);
    glDisable(GL_BLEND);
    glPopMatrix();
    }

    If i comment the translatef of the sphere, the sphere of the spiral/capsule mesh appears centrated and in the right place (the origin) but the cilinder sphere appears in the same place, the origin, because the two spheres are drawed in the same place, but if I use the Translatef line, the cilinder sphere are OK, but the other sphere cuts the other mesh.
    Can anyone tell me why?
    thank you
    "!I don't know... fly casual"

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Oct 2000
    Location
    Belgium
    Posts
    807

    Re: Bounding Spheres

    Originally posted by Ffelagund:
    for(j=0;i<nVertices;i++)
    {
    if(vertices[j]>maxCorner.x) maxCorner.x=vertices[j];
    if(vertices[j]<minCorner.x) minCorner.x=vertices[j];
    j++;

    if(vertices[j]>maxCorner.y) maxCorner.y=vertices[j];
    if(vertices[j]<minCorner.y) minCorner.y=vertices[j];
    j++;

    if(vertices[j]>maxCorner.z) maxCorner.z=vertices[j];
    if(vertices[j]<minCorner.z) minCorner.z=vertices[j];
    j++;
    }
    This loop is very wrong. Did you copy/paste it or are these just typos?

    Also, if you're interested in other approaches, I use this one: http://geometryalgorithms.com/Archiv...oximate%20Ball

    -- Tom

  3. #3
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Spain
    Posts
    273

    Re: Bounding Spheres

    Why are wrong? the vertices array have a size of (sizeof(float)*3*nVertices).
    And i forgot paste "unsigned int i=0,j=0;" before the loop.
    Every item of the array are a component (x,y or z) of a vertex, not a vertex.

    Edit: I forgot paste the computation of the radio:

    BAxisAlignedBox b(vertices,nVertices);
    center = b.GetCenter();
    radius = 0;
    for(unsigned int i = 0;i<nVertices;i+=3)
    {
    Vector3D p; p.Assign(&vertices[i]);
    float aux = center % p;
    if(aux>radius)
    radius = aux;
    }

    //float aux = center % p; % <-- operator distance


    [This message has been edited by Ffelagund (edited 02-25-2004).]
    "!I don't know... fly casual"

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Oct 2000
    Location
    Belgium
    Posts
    807

    Re: Bounding Spheres

    I thought the i was a typo and that you just meant j. Hence, your loop would have incremented its counter four times per iteration instead of three. My bad.

    -- Tom

  5. #5
    Intern Contributor
    Join Date
    Sep 2003
    Location
    Jersey, Channel Islands
    Posts
    68

    Re: Bounding Spheres

    Hi

    Can you reproduce the problem with a simpler model? Like a tetrehedron or something.

    What's the difference between the cylinder, where your code seems to work, and the other mesh?

    mad

  6. #6
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Spain
    Posts
    273

    Re: Bounding Spheres

    There aren't any difference, in the two cases, the spheres are calculated by the same way, there aren't any translate, scale or rotate that affects to any of the two meshes. Both are drawed in camera coordinates .
    "!I don't know... fly casual"

  7. #7
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Spain
    Posts
    273

    Re: Bounding Spheres

    I have solved this problem. The error was that I suposse that the center of the bounding box will match with the centroid of the set of vertices of the mesh, and this don't happens.
    Here are the sphere with the center of the bounding box as center: http://www.telecable.es/personales/ffelagund/bbox3.jpg

    And here are the sphere with his centroid as center: http://www.telecable.es/personales/ffelagund/bbox6.jpg
    "!I don't know... fly casual"

  8. #8
    Intern Contributor
    Join Date
    Dec 2002
    Posts
    69

    Re: Bounding Spheres

    I find this page an invaluable resource for these things:- http://www.magic-software.com/SourceCode.html

  9. #9
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Spain
    Posts
    273

    Re: Bounding Spheres

    Yes, i know it, but I'm trying do this algorithm by my self... now i'm fighting to solve the homogeneus system to find the eigenvectors...
    "!I don't know... fly casual"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •