STLLoader

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

STLLoader

mark h
Bonjour,

je suis à la recherche d'un programme qui affiche un objet STL en chargeant un file.stl
J'ai déjà établi une sorte de code qui m'affiche mon STL mais qu'à moitié... et dès que je dezoom trop, mon STL disparait
Il doit bien exister un exemple quelque part.. ou quelqu'un qui l'aurait déjà fait..

Merci d'avance.

Voici mon petit code:


package cube3dtg;

import com.sun.j3d.loaders.Scene;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.BorderLayout;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.BranchGroup;
import com.sun.j3d.utils.geometry.ColorCube;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import java.io.IOException;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3f;
import org.j3d.loaders.stl.STLFileReader;

public class Cube3dTG extends Frame implements WindowListener, MouseWheelListener
{

BranchGroup groupe;
BranchGroup objRoot;
Canvas3D canvas3D;
TransformGroup tg ;
public Cube3dTG()
{
super("STLLoader");
this.addWindowListener(this);
setLayout(new BorderLayout());
canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add(canvas3D);
// 2eme étape on crée notre scene (regroupement d'objet)
try{
File file = new File("peace.stl");
final STLFileReader reader = new STLFileReader(file);
STLLoader loader = new STLLoader();
Scene scene = loader.createScene(reader);
groupe = scene.getSceneGroup();
}catch (IOException ex){
System.out.println(ex);
}
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
canvas3D.addMouseWheelListener(this);

simpleU.getViewingPlatform().setNominalViewingTransform();
objRoot =new BranchGroup();
tg = new TransformGroup();
tg.addChild(groupe);
objRoot.addChild(tg);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); // -----------(1)
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); //--------------(2)
//fonction zoom
MouseZoom myMouseZoom = new MouseZoom();
myMouseZoom.setTransformGroup(tg); // ---------------(7)
myMouseZoom.setSchedulingBounds(new BoundingSphere()); // ---------------(8)
//fonction rotation
MouseRotate rotate = new MouseRotate(tg);
rotate.setSchedulingBounds(new BoundingSphere());
//fonction déplacement
MouseTranslate translate = new MouseTranslate(tg);
translate.setSchedulingBounds(new BoundingSphere());

objRoot.addChild(myMouseZoom);
objRoot.addChild(rotate);
objRoot.addChild(translate);
simpleU.addBranchGraph(objRoot);
canvas3D.repaint();
}


@Override
public void windowActivated(WindowEvent e){}
@Override
public void windowClosed(WindowEvent e){}
@Override
public void windowDeactivated(WindowEvent e){}
@Override
public void windowDeiconified(WindowEvent e){}
@Override
public void windowIconified(WindowEvent e){}
@Override
public void windowOpened(WindowEvent e){}
@Override
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
@Override
public void mouseWheelMoved(MouseWheelEvent e){
}
public static void main(String args[])
{
Cube3dTG myApp=new Cube3dTG();
myApp.setLocationRelativeTo(null);
myApp.setSize(600,600);
myApp.setVisible(true);
}
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

gouessej
Administrator
Bonjour [Good afternoon]

A l'avenir, faites un effort pour écrire en anglais ou bien je serai le seul à pouvoir vous répondre [In the future, make an effort to write in English or I'll be the only one who can answer]

Quand vous dézoomez, votre objet doit probablement sortir du frustum et ne plus être visible [When you zoom out, your object probably goes out from the frustum and be no longer visible]

Essayez d'appeler View.setBackClipDistance() et View.setFrontClipDistance() [Try calling View.setBackClipDistance() and View.setFrontClipDistance()]
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

mark h
Sorry for my language  and thank for your reply

Where should I use them ? I am a beginner in Java 3D, this is my first project.
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

gouessej
Administrator
SimpleUniverse.getViewer().getView() returns a View object that you have to modify to obtain the desired effect.

Maybe this tutorial in French can help you a bit to understand some concepts:
http://deven3d.free.fr/java3d/chap09.htm

This is what I meant by view frustum:
http://en.wikipedia.org/wiki/Viewing_frustum

I hope you use the very latest version of Java3D (currently 1.6.0 pre 10).
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

mark h
Thank you very much. It's resolve !


package cube3dtg;



import com.sun.j3d.loaders.Scene;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.BorderLayout;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.BranchGroup;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import java.io.IOException;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import org.j3d.loaders.stl.STLFileReader;

public class Cube3dTG extends Frame implements WindowListener, MouseWheelListener
{    
   
    BranchGroup groupe;
    BranchGroup objRoot;
    Canvas3D canvas3D;
    TransformGroup tg ;
   
    public Cube3dTG()
    {
        super("STLLoader");
        this.addWindowListener(this);
        setLayout(new BorderLayout());
        // 1ere étape création du Canvas3d qui vas afficher votre univers virtuel
        canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        add(canvas3D);
        // 2eme étape on crée notre scene (regroupement d'objet)
        try{
            File file = new File("peace.stl");
            final STLFileReader reader = new STLFileReader(file);
            STLLoader loader = new STLLoader();
            Scene scene = loader.createScene(reader);
            groupe = scene.getSceneGroup();
        }catch (IOException ex){
            System.out.println(ex);
        }
    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
    canvas3D.addMouseWheelListener(this);

    // on met le plan de projection en arriere par rapport a l'origine
    simpleU.getViewingPlatform().setNominalViewingTransform();
    objRoot =new BranchGroup();        
    tg = new TransformGroup();
    tg.addChild(groupe);
    objRoot.addChild(tg);        
    tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); // -----------(1)
    tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); //--------------(2)
    //fonction zoom
    MouseZoom myMouseZoom = new MouseZoom();
    myMouseZoom.setTransformGroup(tg);  // ---------------(7)
    myMouseZoom.setSchedulingBounds(new BoundingSphere());  // ---------------(8)
    //fonction rotation
    MouseRotate rotate = new MouseRotate(tg);
    rotate.setSchedulingBounds(new BoundingSphere());
    //fonction déplacement
    MouseTranslate translate = new MouseTranslate(tg);
    translate.setSchedulingBounds(new BoundingSphere());

    objRoot.addChild(myMouseZoom);
    objRoot.addChild(rotate);
    objRoot.addChild(translate);
    simpleU.addBranchGraph(objRoot);
    View view = simpleU.getViewer().getView();
    view.setBackClipDistance(1000);
    canvas3D.repaint();
}


    @Override
    public void windowActivated(WindowEvent e){}
    @Override
    public void windowClosed(WindowEvent e){}
    @Override
    public void windowDeactivated(WindowEvent e){}
    @Override
    public void windowDeiconified(WindowEvent e){}
    @Override
    public void windowIconified(WindowEvent e){}
    @Override
    public void windowOpened(WindowEvent e){}
    @Override
    public void windowClosing(WindowEvent e){
            System.exit(1);
    }
    @Override
    public void mouseWheelMoved(MouseWheelEvent e){
    }
    public static void main(String args[])
    {
            Cube3dTG myApp=new Cube3dTG();
            myApp.setLocationRelativeTo(null);
            myApp.setSize(600,600);
            myApp.setVisible(true);
    }
       
}
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

gouessej
Administrator
Maybe it would be better to get the bounding volume of your mesh (Group.getCollisionBounds()) to compute the back clip distance and the front clip distance.

view.setBackClipDistance(1000) works with your example but imagine that someone else wants to load a mesh whose coordinates are greater than 1000.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

mark h
Hi,

thank for your contribution.
It is sure that it would be better, but how do I adjust the distance after getting the bounds ?
I'm interested.

Thank you !
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

gouessej
Administrator
Maybe use the diameter of the bounding sphere. As you allow the end user to translate the object, it can still become invisible but if you set the back clip distance correctly, the object can remain visible when rotating and zooming (depending on how it is internally implemented).
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

mark h
Re,

I'm beginner in java, that returns exactly bound ?


                         ??
Bound(bounds) -----> Distance(int or double)

Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

gouessej
Administrator
Look at the (old) documentation:
http://download.java.net/media/java3d/javadoc/1.5.2/javax/media/j3d/Group.html#getCollisionBounds()


Harvey, we should really put the documentation of the latest version somewhere.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

mark h
Hi,

I can not seem to do it, can you help me a little more, please ?

Thank
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

gouessej
Administrator
Hi

Get your bounds, cast this object to BoundingSphere, call getRadius(), multiply it by 2 as you want the diameter.

I'm sorry, I'm not the best person to answer questions about Java3D, I initiated its port several years ago, Harvey has gone on maintaining it and fixed a lot of stuff, several individual contributors suggested a few patches.

Some other scenegraphs provide some full featured viewers and several loaders, it would allow you to view STL files with less boilerplate code. Of course, there are probably some viewers based on Java3D.

Is there anybody here who has something better to suggest? Harvey? August? Runiter?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

mark h
There is only you in this forum ??..

My problem is:

I have a BranchGroup and SimpleUniverse, and I wish my BranchGroup is automatically placed in the center of my vue3D.

Reply | Threaded
Open this post in threaded view
|

Re: STLLoader

gouessej
Administrator
No I'm not alone, there are about 750 people registered here but it's not the best time of the year to get some help.

Maybe this article can help you but it seems difficult to do exactly the same in your case:
http://stackoverflow.com/questions/9020783/java3d-universe-size-is-too-small-to-fit-object

Look at the paragraph 9.4.1 here:
http://www-evasion.imag.fr/~Francois.Faure/enseignement/ressources/java/j3dguide/ViewModel.doc.html
"a scene graph for a simple application that draws an object in the center of a window and rotates that object about its center point"
It seems to be what you're trying to do.
Julien Gouesse | Personal blog | Website