Commit 32d757bc authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

Introduced basic Drawable hierarchy

parent b112d968
Loading
Loading
Loading
Loading
+205 −0
Original line number Diff line number Diff line
package cz.fidentis.analyst.gui.scene;

import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import cz.fidentis.analyst.mesh.core.MeshModel;
import java.awt.Color;
import javax.vecmath.Vector3d;

/**
 * A drawable object, i.e., an object with drawing state and capable to 
 * render itself into an OpenGL context.
 * 
 * @author Radek Oslejsek
 * @author Richard Pajersky
 */
public abstract class Drawable {
    
    private boolean display = true;
    
    /* material info */
    private Color color = Color.LIGHT_GRAY;
    private float transparency = 1; // 0 = off, 1 = max
    private Color highlights = new Color(0, 0, 0, 1);
    
    /* transformation */
    private Vector3d translation = new Vector3d(0, 0, 0);
    private Vector3d rotation = new Vector3d(0, 0, 0);
    private Vector3d scale = new Vector3d(0, 0, 0);
    
    /* rendering mode */
    /**
     * Render mode to use, one from {@code GL_FILL}, {@code GL_LINE}, {@code GL_POINT}
     */
    private int renderMode = GL2.GL_FILL; // GL_FILL, GL_LINE, or GL_POINT
    
    public void render(GL2 gl) {
        initRendering(gl);
        
        gl.glPushMatrix(); {
            // rotate
            gl.glRotated(getRotation().x, 1, 0, 0);
            gl.glRotated(getRotation().y, 0, 1, 0);
            gl.glRotated(getRotation().z, 0, 0, 1);
            // move
            gl.glTranslated(getTranslation().x, getTranslation().y, getTranslation().z);
            // scale
            gl.glScaled(1 + getScale().x, 1 + getScale().y, 1 + getScale().z);
        
            renderObject(gl);
        }
        gl.glPopMatrix();
        
        finishRendering(gl);
    }
    
    protected void initRendering(GL2 gl) {
        gl.glShadeModel(GL2.GL_SMOOTH);
        gl.glPolygonMode(GL.GL_FRONT_AND_BACK, getRenderMode());
        
        // set color
        float[] rgba = {color.getRed() / 255f, color.getGreen() / 255f, 
            color.getBlue() / 255f , getTransparency()};
        gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, rgba, 0);

    }
    
    protected abstract void renderObject(GL2 gl);

    protected void finishRendering(GL2 gl) {    
    }
        
    /**
     * This drawable mesh is included in the rendered scene.
     */
    public void show() {
        display = true;
    }
    
    /**
     * This drawable mesh is excluded from the rendered scene (skipped).
     */
    public void hide() {
        display = false;
    }
    
    /**
     * 
     * @return {@code true} if the object is included (rendered) in the scene.
     */
    public boolean isShown() {
        return display;
    }
    
    /**
     * Sets color
     * @param color Color
     */
    public void setColor(Color color) {
        this.color = color;
    }

    /**
     * @return {@link Color}
     */
    public Color getColor() {
        return color;
    }
    
    /**
     * @return Current value of transparency
     */
    public float getTransparency() {
        return transparency;
    }

    /**
     * Sets transparency
     * @param transparency Transparency
     */
    public void setTransparency(float transparency) {
        this.transparency = transparency;
    }
    
    /**
     * @return {@link Color} of highlights
     */
    public Color getHighlights() {
        return highlights;
    }

    /**
     * Sets {@link Color} of highlights
     * @param highlights 
     */
    public void setHighlights(Color highlights) {
        this.highlights = highlights;
    }
    
    /**
     * @return Current translation
     */
    public Vector3d getTranslation() {
        return translation;
    }

    /**
     * Sets tranlation
     * @param translation Translation
     */
    public void setTranslation(Vector3d translation) {
        this.translation = translation;
    }

    /**
     * @return Current rotation
     */
    public Vector3d getRotation() {
        return rotation;
    }

    /**
     * Sets rotation
     * @param rotation 
     */
    public void setRotation(Vector3d rotation) {
        this.rotation = rotation;
    }

    /**
     * @return Current scale
     */
    public Vector3d getScale() {
        return scale;
    }

    /**
     * Sets scale
     * @param scale Scale
     */
    public void setScale(Vector3d scale) {
        this.scale = scale;
    }
    
    /**
     * @return Value of {@link #renderMode}
     */
    public int getRenderMode() {
        return renderMode;
    }

    /**
     * Sets render mode
     * @param renderMode Render mode
     * @throws IllegalArgumentException if renderMode isn't {@code GL_FILL}, {@code GL_LINE} or {@coed GL_POINT}
     */
    public void setRenderMode(int renderMode) {
        if (renderMode != GL2.GL_FILL && 
                renderMode != GL2.GL_LINE &&
                renderMode != GL2.GL_POINT) {
            throw new IllegalArgumentException("invalid mode");
        }
        this.renderMode = renderMode;
    }

}
+82 −0
Original line number Diff line number Diff line
package cz.fidentis.analyst.gui.scene;

import cz.fidentis.analyst.feature.FeaturePoint;
import cz.fidentis.analyst.mesh.core.MeshModel;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author oslejsek
 */
public class DrawableFace extends DrawableMesh {
    
    public static final Color SKIN_COLOR = new Color(224, 172, 105);
    
    /* feature points */
    private List<FeaturePoint> featurePoints = new ArrayList<>();
    private List<Color> featurePointsColor = new ArrayList<>();
    private boolean renderFeaturePoints = false;
    
    /**
     * Constructor. 
     * 
     * @param model Drawable mesh model of the face
     * @throws IllegalArgumentException if the model is {@code null}
     */
    public DrawableFace(MeshModel model) {
        super(model);
        setColor(SKIN_COLOR);
    }
    
    /**
     * @return {@link List} of {@link FeaturePoint}
     */
    public List<FeaturePoint> getFeaturePoints() {
        return featurePoints;
    }

    /**
     * Sets feature points
     * @param featurePoints Feature points
     */
    public void setFeaturePoints(List<FeaturePoint> featurePoints) {
        this.featurePoints = featurePoints;
        List<Color> colors = new ArrayList<>();
        featurePoints.forEach((_item) -> {
            colors.add(getColor());
        });
        this.setFeaturePointsColor(colors);
    }   
    
    /**
     * @return {@link List} of feature points {@link Color}
     */
    public List<Color> getFeaturePointsColor() {
        return featurePointsColor;
    }

    /**
     * Sets colors of feature points
     * @param featurePointsColor Colors of feature points
     */
    public void setFeaturePointsColor(List<Color> featurePointsColor) {
        this.featurePointsColor = featurePointsColor;
    } 
    
    /**
     * @return {@code true} if feature points should be rendered
     */
    public boolean isRenderFeaturePoints() {
        return renderFeaturePoints;
    }

    /**
     * Sets if feature points should be renderred or not
     * @param renderFeaturePoints 
     */
    public void setRenderFeaturePoints(boolean renderFeaturePoints) {
        this.renderFeaturePoints = renderFeaturePoints;
    }    
}
+96 −0
Original line number Diff line number Diff line
package cz.fidentis.analyst.gui.scene;

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.glu.GLUquadric;
import cz.fidentis.analyst.feature.FeaturePoint;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author oslejsek
 */
public class DrawableFeaturePoints extends Drawable {
    
    public static final Color DEFAULT_COLOR = Color.GREEN;
    
    private static final GLU glu = new GLU();
    
    /* feature points */
    private final List<FeaturePoint> featurePoints;
    
    /**
     * feature points with color different from the default color
     */
    private Map<Integer, Color> specialColors = new HashMap<>();
    
    /**
     * Constructor.
     * @param featurePoints Feature points
     */
    public DrawableFeaturePoints(List<FeaturePoint> featurePoints) {
        this.featurePoints = new ArrayList<>(featurePoints);
        setColor(DEFAULT_COLOR);
    }

    @Override
    protected void renderObject(GL2 gl) {
        float[] rgba = {
            getColor().getRed() / 255f, 
            getColor().getGreen() / 255f, 
            getColor().getBlue() / 255f, 
            getTransparency()
        };
        gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, rgba, 0); // set default color
        
        for (int i = 0; i < featurePoints.size(); i++) {
            FeaturePoint fp = featurePoints.get(i);
            Color specialColor = specialColors.get(i);
            if (specialColor != null) {
                float[] tmpRGB = {specialColor.getRed() / 255f, specialColor.getGreen() / 255f, specialColor.getBlue() / 255f, getTransparency()};
                gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, tmpRGB, 0);
            }
            
            //gl.glPushMatrix(); 
            //gl.glTranslated(fp.getX(), fp.getY(), fp.getZ());
            GLUquadric center = glu.gluNewQuadric();
            glu.gluQuadricDrawStyle(center, GLU.GLU_FILL);
            glu.gluQuadricNormals(center, GLU.GLU_FLAT);
            glu.gluQuadricOrientation(center, GLU.GLU_OUTSIDE);
            glu.gluSphere(center, 3f, 16, 16);
            glu.gluDeleteQuadric(center);
            //gl.glPopMatrix();
    
            if (specialColor != null) {
                gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, rgba, 0); // set default color
            }
        }
    }
       
    /**
     * @return {@link List} of {@link FeaturePoint}
     */
    //public List<FeaturePoint> getFeaturePoints() {
    //    return Collections.unmodifiableList(featurePoints);
    //}

    /**
     * Sets feature points
     * @param featurePoints Feature points
     */
    /*
    public void setFeaturePoints(List<FeaturePoint> featurePoints) {
        this.featurePoints = featurePoints;
        List<Color> colors = new ArrayList<>();
        featurePoints.forEach((_item) -> {
            colors.add(getColor());
        });
        this.setFeaturePointsColor(colors);
    } 
    */
    
}
+42 −233
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import cz.fidentis.analyst.mesh.core.MeshModel;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

/**
@@ -17,64 +18,15 @@ import javax.vecmath.Vector3d;
 * @author Radek Oslejsek
 * @author Richard Pajersky
 */
public class DrawableMesh {
public class DrawableMesh extends Drawable {
    
    private final MeshModel model;
    
    private boolean display = true;
    
    /* material info */
    /**
     * {@link Color} of mesh
     */
    private Color color = new Color(224, 172, 105);
    /**
     * {@link Color} of highlights
     */
    private Color highlights = new Color(0, 0, 0, 1);
    /**
     * Transparency of mesh
     */
    private float transparency = 1;
    
    /* transformation info */
    /**
     * Translation
     */
    private Vector3d translation = new Vector3d(0, 0, 0);
    /**
     * Rotation
     */
    private Vector3d rotation = new Vector3d(0, 0, 0);
    /**
     * Scale
     */
    private Vector3d scale = new Vector3d(0, 0, 0);
    
    /* render mode */
    /**
     * Render mode to use, one from {@code GL_FILL}, {@code GL_LINE}, {@code GL_POINT}
     */
    private int renderMode = GL2.GL_FILL;
    /**
     * Flag if back should be rendered
     */
    private boolean showBackface = true;
    
    /* feature points */
    /**
     * {@link List} of feature points
     */
    private List<FeaturePoint> featurePoints = new ArrayList<>();
    /**
     * {@link List} of feature points color
     */
    private List<Color> featurePointsColor = new ArrayList<>();
    /**
     * Flag if feature points should be rendered
     */
    private boolean renderFeaturePoints = false;
    
    /**
     * Constructor. 
     * 
@@ -88,110 +40,53 @@ public class DrawableMesh {
        this.model = model;
    }
    
    /**
     * Returns list of individual facets.
     * 
     * @return list of individual facets.
     */
    public List<MeshFacet> getFacets() {
        return model.getFacets();
    }
    @Override
    protected void initRendering(GL2 gl) {
        super.initRendering(gl);
        
    /**
     * This drawable mesh is included in the rendered scene.
     */
    public void show() {
        display = true;
    }
        // showBackface
        //float[] pos = {0f, 0f, -1f, 0f};
        //gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, pos, 0);
        //if (isShowBackface()) {
        //    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, Color.white.getComponents(null), 0);
        //} else {
        //    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, Color.black.getComponents(null), 0);
        //}
    
    /**
     * This drawable mesh is excluded from the rendered scene (skipped).
     */
    public void hide() {
        display = false;
        // set color of highlights
        Color col = getHighlights();
        float[] highlights = {col.getRed(), col.getGreen(), col.getBlue(), 1};
        gl.glMaterialf(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 50);  
        gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, highlights, 0);
    }
    
    /**
     * 
     * @return {@code true} if the object is included (rendered) in the scene.
     */
    public boolean isShown() {
        return display;
    }
    protected void renderObject(GL2 gl) {
        for (MeshFacet facet: getFacets()) {
            gl.glBegin(GL2.GL_TRIANGLES); //vertices are rendered as triangles
        
    /**
     * Sets color
     * @param color Color
     */
    public void setColor(Color color) {
        this.color = color;
            // get the normal and tex coords indicies for face i  
            for (int v = 0; v < facet.getCornerTable().getSize(); v++) {            
                // render the normals
                Vector3d norm = facet.getVertices().get(facet.getCornerTable().getRow(v).getVertexIndex()).getNormal(); 
                if(norm != null) {
                    gl.glNormal3d(norm.x, norm.y, norm.z);
                }

    /**
     * @return {@link Color}
     */
    public Color getColor() {
        return color;
    }
    
    /**
     * @return Current value of transparency
     */
    public float getTransparency() {
        return transparency;
    }

    /**
     * Sets transparency
     * @param transparency Transparency
     */
    public void setTransparency(float transparency) {
        this.transparency = transparency;
                // render the vertices
                Point3d vert = facet.getVertices().get(facet.getCornerTable().getRow(v).getVertexIndex()).getPosition();
                gl.glVertex3d(vert.x, vert.y, vert.z);
            }
        
    /**
     * @return Current translation
     */
    public Vector3d getTranslation() {
        return translation;
            gl.glEnd();
        }

    /**
     * Sets tranlation
     * @param translation Translation
     */
    public void setTranslation(Vector3d translation) {
        this.translation = translation;
    }
    
    /**
     * @return Current rotation
     */
    public Vector3d getRotation() {
        return rotation;
    }

    /**
     * Sets rotation
     * @param rotation 
     */
    public void setRotation(Vector3d rotation) {
        this.rotation = rotation;
    }

    /**
     * @return Current scale
     */
    public Vector3d getScale() {
        return scale;
    }

    /**
     * Sets scale
     * @param scale Scale
     * Returns list of individual facets.
     * 
     * @return list of individual facets.
     */
    public void setScale(Vector3d scale) {
        this.scale = scale;
    public List<MeshFacet> getFacets() {
        return model.getFacets();
    }
    
    /**
@@ -201,92 +96,6 @@ public class DrawableMesh {
        return this.model;
    }

    /**
     * @return {@link Color} of highlights
     */
    public Color getHighlights() {
        return highlights;
    }

    /**
     * Sets {@link Color} of highlights
     * @param highlights 
     */
    public void setHighlights(Color highlights) {
        this.highlights = highlights;
    }
    
    /**
     * @return Value of {@link #renderMode}
     */
    public int getRenderMode() {
        return renderMode;
    }

    /**
     * Sets render mode
     * @param renderMode Render mode
     * @throws IllegalArgumentException if renderMode isn't {@code GL_FILL}, {@code GL_LINE} or {@coed GL_POINT}
     */
    public void setRenderMode(int renderMode) {
        if (renderMode != GL2.GL_FILL && 
                renderMode != GL2.GL_LINE &&
                renderMode != GL2.GL_POINT) {
            throw new IllegalArgumentException("invalid mode");
        }
        this.renderMode = renderMode;
    }

    /**
     * @return {@link List} of {@link FeaturePoint}
     */
    public List<FeaturePoint> getFeaturePoints() {
        return featurePoints;
    }

    /**
     * Sets feature points
     * @param featurePoints Feature points
     */
    public void setFeaturePoints(List<FeaturePoint> featurePoints) {
        this.featurePoints = featurePoints;
        List<Color> colors = new ArrayList<>();
        featurePoints.forEach((_item) -> {
            colors.add(this.color);
        });
        this.setFeaturePointsColor(colors);
    }   
    
    /**
     * @return {@link List} of feature points {@link Color}
     */
    public List<Color> getFeaturePointsColor() {
        return featurePointsColor;
    }

    /**
     * Sets colors of feature points
     * @param featurePointsColor Colors of feature points
     */
    public void setFeaturePointsColor(List<Color> featurePointsColor) {
        this.featurePointsColor = featurePointsColor;
    } 
    
    /**
     * @return {@code true} if feature points should be rendered
     */
    public boolean isRenderFeaturePoints() {
        return renderFeaturePoints;
    }

    /**
     * Sets if feature points should be renderred or not
     * @param renderFeaturePoints 
     */
    public void setRenderFeaturePoints(boolean renderFeaturePoints) {
        this.renderFeaturePoints = renderFeaturePoints;
    }

    /**
     * @return {@code true} if back face shoud be shown
     */
+5 −8
Original line number Diff line number Diff line
@@ -3,10 +3,7 @@ package cz.fidentis.analyst.gui.scene;
import cz.fidentis.analyst.face.HumanFace;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Abstract class for ...
@@ -19,7 +16,7 @@ public class Scene {
    
    private final HumanFace secondaryFace ;
    
    private final List<DrawableMesh> drawables = new ArrayList<>();
    private final List<DrawableFace> drawables = new ArrayList<>();
    
    /**
     * Constructor for single face analysis.
@@ -34,7 +31,7 @@ public class Scene {
        this.primaryFace = face;
        this.secondaryFace = null;
        
        drawables.add(new DrawableMesh(primaryFace.getMeshModel()));
        drawables.add(new DrawableFace(primaryFace.getMeshModel()));
    }
    
    /**
@@ -54,8 +51,8 @@ public class Scene {
        this.primaryFace = primary;
        this.secondaryFace = secondary;
        
        drawables.add(new DrawableMesh(primaryFace.getMeshModel()));
        drawables.add(new DrawableMesh(secondaryFace.getMeshModel()));
        drawables.add(new DrawableFace(primaryFace.getMeshModel()));
        drawables.add(new DrawableFace(secondaryFace.getMeshModel()));
    }
    
    /**
@@ -63,7 +60,7 @@ public class Scene {
     * 
     * @return all drawable objects 
     */
    public Collection<DrawableMesh> getDrawables() {
    public Collection<DrawableFace> getDrawables() {
        return this.drawables;
    }
    
Loading