Subversion Repositories gelsvn

Rev

Rev 566 | Rev 572 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 *  VisObj.cpp
 *  GEL
 *
 *  Created by J. Andreas Bærentzen on 20/09/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "VisObj.h"
#include "polarize.h"

#include <GLConsole/GLConsole.h>
#include <HMesh/Manifold.h>
#include <HMesh/AttributeVector.h>
#include <HMesh/load.h>
#include <HMesh/curvature.h>

#include <CGLA/Mat3x3d.h>
#include <CGLA/Vec3d.h>
#include <CGLA/Vec4d.h>

using namespace std;
using namespace CGLA;
using namespace HMesh;
using namespace CVarUtils;
using namespace GLGraphics;

int WINX=800, WINY=800;


bool VisObj::reload(string _file)
{
        if(_file != "") file = _file;
        mani.clear();
        if(!load(file, mani))
                return false;
        bsphere(mani, bsphere_center, bsphere_radius);
    
        view_ctrl.set_centre(bsphere_center);
        view_ctrl.set_eye_dist(2*bsphere_radius);
        return true;
}

bool VisObj::add_mesh(string file)
{
        if(!load(file, mani))
                return false;
        bsphere(mani, bsphere_center, bsphere_radius);
        view_ctrl.set_centre(bsphere_center);
        view_ctrl.set_eye_dist(2*bsphere_radius);
        return true;
}

Vec3f grad(HMesh::Manifold& m, HMesh::VertexAttributeVector<float>& fun, HMesh::FaceID f)
{
    if(no_edges(m,f) != 3)
        return Vec3f(0.0);
    
    
    Vec3f n = normal(m, f);
    
    Vec3f gsum(0.0);
    for(HalfEdgeWalker w = m.halfedgewalker(f); !w.full_circle(); w = w.next())
    {
        Vec3f gdir = normalize(cross(n, m.pos(w.vertex()) - m.pos(w.opp().vertex())));
        float l = dot(m.pos(w.next().vertex())-m.pos(w.vertex()), gdir);
        gdir *= fun[w.next().vertex()]/l;
        gsum += gdir;
    }
    return gsum;
}


void VisObj::display(const std::string& display_method , bool smooth, float gamma)
{
        if(create_display_list){
                create_display_list = false;
        
                delete renderer;
                
                string short_name = display_method.substr(0,3);
                if(short_name== "wir")
                        renderer = new WireframeRenderer(mani, smooth);
        
                else if(short_name == "har")
                        renderer = new HarmonicsRenderer(harmonics);
        
                else if(short_name == "iso")
                        renderer = new IsophoteLineRenderer(mani, smooth);
        
                else if(short_name == "ref")
                        renderer = new ReflectionLineRenderer(mani, smooth);
        
                else if(short_name == "gla")
                        renderer = new GlazedRenderer(mani, smooth, bsphere_radius);
        
                else if(short_name == "too")
                        renderer = new ToonRenderer(mani, smooth);
        
                else if(short_name == "cur"){
                        static string& line_direction = CreateCVar<string>("display.curvature_lines.direction", "min");
                        static string& method = CreateCVar<string>("display.curvature_lines.method", "tensors");
                        static int& smoothing_iter = CreateCVar<int>("display.curvature_lines.smoothing_iter", 1);
                        
                        VertexAttributeVector<Mat3x3d> curvature_tensors(mani.total_vertices());
                        VertexAttributeVector<Vec3d> min_curv_direction(mani.total_vertices());
                        VertexAttributeVector<Vec3d> max_curv_direction(mani.total_vertices());
                        string _line_direction = line_direction;
                        VertexAttributeVector<Vec3d>& lines = (_line_direction == "min") ? min_curv_direction : max_curv_direction;
                        VertexAttributeVector<double> curvature(mani.total_vertices());
                        
            if(method == "tensors")
            {
                curvature_tensors_from_edges(mani, curvature_tensors);
                for(int i=0;i<smoothing_iter; ++i)
                    smooth_curvature_tensors(mani,curvature_tensors);
                
                curvature_from_tensors(mani, curvature_tensors,
                                       min_curv_direction,
                                       max_curv_direction,
                                       curvature);
            }
            else
                curvature_paraboloids(mani,
                                      min_curv_direction,
                                      max_curv_direction,
                                      curvature); 
                        
                        renderer = new LineFieldRenderer(mani, smooth, lines, bsphere_radius);
                }
                else if(short_name == "gau"){
                        static float& smoothing = CreateCVar("display.gaussian_curvature_renderer.smoothing",2.0f);
            
                        VertexAttributeVector<double> scalars(mani.total_vertices());
                        gaussian_curvature_angle_defects(mani, scalars, smoothing);
                        double max_G = 0;
            
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
                                max_G = max(abs(scalars[*v]), max_G);
            
                        renderer = new ScalarFieldRenderer(mani, smooth, scalars, max_G, gamma);
                        
                }
                else if(short_name == "mea"){
                        static int& smoothing = CreateCVar("display.mean_curvature_renderer.smoothing",2);
            
                        VertexAttributeVector<double> scalars(mani.total_vertices());
                        mean_curvatures(mani, scalars, smoothing);
                        double max_G = 0;
                        double mean = 0;
            
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v){
                                max_G = max(abs(scalars[*v]), max_G);
                                mean += scalars[*v];
                        }
            
                        renderer = new ScalarFieldRenderer(mani, smooth, scalars, max_G, gamma);
                }
                else if(short_name == "amb"){
                        static int& smoothing = CreateCVar("display.ambient_occlusion_renderer.smoothing",1);
            
                        VertexAttributeVector<double> scalars(mani.total_vertices());
                        mean_curvatures(mani, scalars, smoothing);
                        double max_G = 0;
            
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
                                max_G = max(abs(scalars[*v]), max_G);
            
                        renderer = new AmbientOcclusionRenderer(mani, smooth, scalars, max_G);
                        
                }
                else if(short_name == "dua"){
                        VertexAttributeVector<Vec4d> colors(mani.total_vertices());
                        Vec3f c;
                        float r;
            
                        bsphere(mani, c,r);
            
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v){
                if(mani.pos(*v)[2] < c[2]){
                                        colors[*v] = Vec4d(1, -1e10, c[2], mani.pos(*v)[2]);
                }
                else{
                    colors[*v] = Vec4d(0, c[2], 1e10, mani.pos(*v)[2]);
                }
                        }
                        renderer = new DualVertexRenderer(mani, colors);
                }
        else if(short_name == "exp"){
            
            int divisions = 32;
            float vmin, vmax;
            VertexAttributeVector<float> fun;
            make_height_fun(mani, fun, vmin, vmax);
            polarize_mesh(mani, fun, vmin, vmax, divisions);
            
            IDRemap map;
            mani.cleanup(map);
            fun.cleanup(map.vmap);

            VertexAttributeVector<double> scalars(mani.total_vertices(), 1.0);
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
                scalars[*v] = 1.9*gel_rand()/float(GEL_RAND_MAX)-0.95;
            double max_G = 0;

            
            for(int iter=0;iter<0;++iter)
            {
                
                VertexAttributeVector<double> scalars_new(mani.total_vertices(), 0.0);
                VertexAttributeVector<int> count(mani.total_vertices(), 0);

                
                for(HalfEdgeIDIterator h = mani.halfedges_begin(); h != mani.halfedges_end(); ++h)
                {
                    HalfEdgeWalker w = mani.halfedgewalker(*h);
                    if(abs(fun[w.vertex()] - fun[w.opp().vertex()])< 1e-6 && no_edges(mani, w.face())==3)
                    {
                        w = w.next();
                        VertexID v = w.vertex();
                        Vec3f p2 = mani.pos(v);
                        w = w.next();
                        
                        FaceID f = w.face();
                        Vec3f Z = cross(normal(mani,f), grad(mani, fun, f));
                        Vec3f p[2];
                        float fh[2];
                        for(int i=0; i<2; ++i, w = w.next())
                        {
                            p[i] = mani.pos(w.vertex());
                            fh[i] = acos(scalars[w.vertex()]);
                        }
                        float s = (fh[1] - fh[0])/dot(Z,p[1]-p[0]);
                        scalars_new[v] += cos(dot(Z, p2-p[0])*s + fh[0]);
                        ++count[v];
                    }
                }
                
                for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
                    if(!isnan(scalars_new[*v]))
                        scalars[*v] = 0.5* scalars[*v] + 0.5 * scalars_new[*v]/count[*v];
            }
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
                                max_G = max(abs(scalars[*v]), max_G);
            
                        renderer = new ScalarFieldRenderer(mani, smooth, scalars, max_G, gamma);
                        
                }
        
                else
                        renderer = new NormalRenderer(mani, smooth);
                
        }
        view_ctrl.set_gl_modelview();
        renderer->draw();
}