Subversion Repositories gelsvn

Rev

Rev 665 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 665 Rev 666
1
/*
1
/*
2
 *  VisObj.cpp
2
 *  VisObj.cpp
3
 *  GEL
3
 *  GEL
4
 *
4
 *
5
 *  Created by J. Andreas Bærentzen on 20/09/08.
5
 *  Created by J. Andreas Bærentzen on 20/09/08.
6
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
6
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
7
 *
7
 *
8
 */
8
 */
9
 
9
 
10
#include "VisObj.h"
10
#include "VisObj.h"
11
 
11
 
12
#include <GLGraphics/Console.h>
12
#include <GLGraphics/Console.h>
13
#include <HMesh/Manifold.h>
13
#include <HMesh/Manifold.h>
14
#include <HMesh/AttributeVector.h>
14
#include <HMesh/AttributeVector.h>
15
#include <HMesh/load.h>
15
#include <HMesh/load.h>
16
#include <HMesh/curvature.h>
16
#include <HMesh/curvature.h>
17
 
17
 
18
#include <CGLA/Mat3x3d.h>
18
#include <CGLA/Mat3x3d.h>
19
#include <CGLA/Vec3d.h>
19
#include <CGLA/Vec3d.h>
20
#include <CGLA/Vec4d.h>
20
#include <CGLA/Vec4d.h>
21
 
21
 
22
using namespace std;
22
using namespace std;
23
using namespace CGLA;
23
using namespace CGLA;
24
using namespace HMesh;
24
using namespace HMesh;
25
using namespace GLGraphics;
25
using namespace GLGraphics;
26
 
26
 
27
int WINX=800, WINY=800;
27
int WINX=800, WINY=800;
28
 
28
 
29
namespace GLGraphics {
29
namespace GLGraphics {
30
    
30
    
31
    void VisObj::refit()
31
    void VisObj::refit()
32
    {
32
    {
33
        bsphere(mani, bsphere_center, bsphere_radius);
33
        bsphere(mani, bsphere_center, bsphere_radius);
34
        
34
        
35
        view_ctrl.set_centre(Vec3f(bsphere_center));
35
        view_ctrl.set_centre(Vec3f(bsphere_center));
36
        view_ctrl.set_eye_dist(2*bsphere_radius);
36
        view_ctrl.set_eye_dist(2*bsphere_radius);
37
    }
37
    }
38
    
38
    
39
    bool VisObj::reload(string _file)
39
    bool VisObj::reload(string _file)
40
    {
40
    {
41
        if(_file != "") file = _file;
41
        if(_file != "") file = _file;
42
        mani.clear();
42
        mani.clear();
43
        if(!load(file, mani))
43
        if(!load(file, mani))
44
            return false;
44
            return false;
45
        refit();
45
        refit();
46
        return true;
46
        return true;
47
    }
47
    }
48
    
48
    
49
    bool VisObj::add_mesh(string file)
49
    bool VisObj::add_mesh(string file)
50
    {
50
    {
51
        if(!load(file, mani))
51
        if(!load(file, mani))
52
            return false;
52
            return false;
53
        refit();
53
        refit();
54
        return true;
54
        return true;
55
    }
55
    }
56
    
56
    
57
    
57
    
58
    bool VisObj::select_vertex(const CGLA::Vec2i& pos)
58
    bool VisObj::select_vertex(const CGLA::Vec2i& pos)
59
    {
59
    {
60
        float d;
60
        float d;
61
        if(depth_pick(pos[0], pos[1], d))
61
        if(depth_pick(pos[0], pos[1], d))
62
        {
62
        {
63
            Vec3d c;
63
            Vec3d c;
64
            float r;
64
            float r;
65
            bsphere(mani, c, r);
65
            bsphere(mani, c, r);
66
            VertexID closest = InvalidVertexID;
66
            VertexID closest = InvalidVertexID;
67
            double min_dist = DBL_MAX;
67
            double min_dist = DBL_MAX;
68
            for(auto vid : mani.vertices())
68
            for(auto vid : mani.vertices())
69
            {
69
            {
70
                Vec3d wp = world2screen(mani.pos(vid));
70
                Vec3d wp = world2screen(mani.pos(vid));
71
                if(sqr_length(Vec2d(wp[0],wp[1])-Vec2d(pos))<100)
71
                if(sqr_length(Vec2d(wp[0],wp[1])-Vec2d(pos))<100)
72
                {
72
                {
73
                    double dist = sqr_length(screen2world(pos[0], pos[1], d)-mani.pos(vid));
73
                    double dist = sqr_length(screen2world(pos[0], pos[1], d)-mani.pos(vid));
74
                    if(dist < min_dist)
74
                    if(dist < min_dist)
75
                    {
75
                    {
76
                        min_dist = dist;
76
                        min_dist = dist;
77
                        closest = vid;
77
                        closest = vid;
78
                    }
78
                    }
79
                }
79
                }
80
            }
80
            }
81
            if(closest != InvalidVertexID) {
81
            if(closest != InvalidVertexID) {
82
                vertex_selection.resize(mani.allocated_vertices(),0);
82
                vertex_selection.resize(mani.allocated_vertices(),0);
83
                vertex_selection[closest] = !vertex_selection[closest];
83
                vertex_selection[closest] = !vertex_selection[closest];
84
                active_selection = true;
84
                active_selection = true;
85
                post_create_display_list();
85
                post_create_display_list();
86
                return true;
86
                return true;
87
            }
87
            }
88
        }
88
        }
89
        return false;
89
        return false;
90
        
90
        
91
    }
91
    }
92
 
92
 
93
    void VisObj::produce_renderer(const std::string& display_method , Console& cs, bool smooth, float gamma)
93
    void VisObj::produce_renderer(const std::string& display_method , Console& cs, bool smooth, float gamma)
94
    {
94
    {
95
        delete renderer;
95
        delete renderer;
96
        
96
        
97
        string short_name = display_method.substr(0,3);
97
        string short_name = display_method.substr(0,3);
98
        if(short_name== "wir")
98
        if(short_name== "wir")
99
            renderer = new WireframeRenderer(mani, smooth);
99
            renderer = new WireframeRenderer(mani, smooth);
100
        
100
        
101
        else if(short_name == "har")
101
        else if(short_name == "har")
102
            renderer = new HarmonicsRenderer(mani, harm, cs);
102
            renderer = new HarmonicsRenderer(mani, harm, cs);
103
        
103
        
104
        else if(short_name == "iso") {
104
        else if(short_name == "iso") {
105
            renderer = new IsophoteLineRenderer();
105
            renderer = new IsophoteLineRenderer();
106
            renderer->compile_display_list(mani,smooth);
106
            renderer->compile_display_list(mani,smooth);
107
        }
107
        }
108
        else if(short_name == "ref") {
108
        else if(short_name == "ref") {
109
            renderer = new ReflectionLineRenderer();
109
            renderer = new ReflectionLineRenderer();
110
            renderer->compile_display_list(mani,smooth);
110
            renderer->compile_display_list(mani,smooth);
111
        }
111
        }
112
        else if(short_name == "gla") {
112
        else if(short_name == "gla") {
113
            renderer = new GlazedRenderer();
113
            renderer = new GlazedRenderer();
114
            dynamic_cast<GlazedRenderer*>(renderer)->compile_display_list(mani,smooth);
114
            dynamic_cast<GlazedRenderer*>(renderer)->compile_display_list(mani,smooth);
115
        }
115
        }
116
        
116
        
117
        
117
        
118
        else if(short_name == "too") {
118
        else if(short_name == "too") {
119
            renderer = new ToonRenderer();
119
            renderer = new ToonRenderer();
120
            renderer->compile_display_list(mani,smooth);
120
            renderer->compile_display_list(mani,smooth);
121
        }
121
        }
122
        
122
        
123
        
123
        
124
        else if(short_name == "cur"){
124
        else if(short_name == "cur"){
125
            static Console::variable<string> line_direction("min");
125
            static Console::variable<string> line_direction("min");
126
            static Console::variable<string> method("tensors");
126
            static Console::variable<string> method("tensors");
127
            static Console::variable<int> smoothing_iter(1);
127
            static Console::variable<int> smoothing_iter(1);
128
            
128
            
129
            line_direction.reg(cs,"display.curvature_lines.direction", "");
129
            line_direction.reg(cs,"display.curvature_lines.direction", "");
130
            method.reg(cs, "display.curvature_lines.method", "");
130
            method.reg(cs, "display.curvature_lines.method", "");
131
            smoothing_iter.reg(cs, "display.curvature_lines.smoothing_iter", "");
131
            smoothing_iter.reg(cs, "display.curvature_lines.smoothing_iter", "");
132
            
132
            
133
            VertexAttributeVector<Mat3x3d> curvature_tensors(mani.allocated_vertices());
133
            VertexAttributeVector<Mat3x3d> curvature_tensors(mani.allocated_vertices());
134
            VertexAttributeVector<Vec3d> min_curv_direction(mani.allocated_vertices());
134
            VertexAttributeVector<Vec3d> min_curv_direction(mani.allocated_vertices());
135
            VertexAttributeVector<Vec3d> max_curv_direction(mani.allocated_vertices());
135
            VertexAttributeVector<Vec3d> max_curv_direction(mani.allocated_vertices());
136
            string _line_direction = line_direction;
136
            string _line_direction = line_direction;
137
            VertexAttributeVector<Vec3d>& lines = (_line_direction == "min") ? min_curv_direction : max_curv_direction;
137
            VertexAttributeVector<Vec3d>& lines = (_line_direction == "min") ? min_curv_direction : max_curv_direction;
138
            VertexAttributeVector<Vec2d> curvature(mani.allocated_vertices());
138
            VertexAttributeVector<Vec2d> curvature(mani.allocated_vertices());
139
            
139
            
140
            if(string(method) == "tensors")
140
            if(string(method) == "tensors")
141
            {
141
            {
142
                curvature_tensors_from_edges(mani, curvature_tensors);
142
                curvature_tensors_from_edges(mani, curvature_tensors);
143
                for(int i=0;i<smoothing_iter; ++i)
143
                for(int i=0;i<smoothing_iter; ++i)
144
                    smooth_curvature_tensors(mani,curvature_tensors);
144
                    smooth_curvature_tensors(mani,curvature_tensors);
145
                
145
                
146
                curvature_from_tensors(mani, curvature_tensors,
146
                curvature_from_tensors(mani, curvature_tensors,
147
                                       min_curv_direction,
147
                                       min_curv_direction,
148
                                       max_curv_direction,
148
                                       max_curv_direction,
149
                                       curvature);
149
                                       curvature);
150
            }
150
            }
151
            else
151
            else
152
                curvature_paraboloids(mani,
152
                curvature_paraboloids(mani,
153
                                      min_curv_direction,
153
                                      min_curv_direction,
154
                                      max_curv_direction,
154
                                      max_curv_direction,
155
                                      curvature);
155
                                      curvature);
156
            
156
            
157
            renderer = new LineFieldRenderer();
157
            renderer = new LineFieldRenderer();
158
            dynamic_cast<LineFieldRenderer*>(renderer)->compile_display_list(mani, lines);
158
            dynamic_cast<LineFieldRenderer*>(renderer)->compile_display_list(mani, lines);
159
        }
159
        }
160
        else if(short_name == "gau"){
160
        else if(short_name == "gau"){
161
            static Console::variable<float> smoothing(2.0f);
161
            static Console::variable<float> smoothing(2.0f);
162
            smoothing.reg(cs, "display.gaussian_curvature_renderer.smoothing", "");
162
            smoothing.reg(cs, "display.gaussian_curvature_renderer.smoothing", "");
163
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
163
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
164
            gaussian_curvature_angle_defects(mani, scalars, smoothing);
164
            gaussian_curvature_angle_defects(mani, scalars, smoothing);
165
            double max_G = 0;
165
            double max_G = 0;
166
            
166
            
167
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
167
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
168
                max_G = max(abs(scalars[*v]), max_G);
168
                max_G = max(abs(scalars[*v]), max_G);
169
            
169
            
170
            renderer = new ScalarFieldRenderer();
170
            renderer = new ScalarFieldRenderer();
171
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth, scalars, max_G, gamma);
171
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth, scalars, max_G, gamma);
172
            
172
            
173
        }
173
        }
174
        else if(short_name == "mea"){
174
        else if(short_name == "mea"){
175
            static Console::variable<int> smoothing(2);
175
            static Console::variable<int> smoothing(2);
176
            smoothing.reg(cs, "display.mean_curvature_renderer.smoothing", "");
176
            smoothing.reg(cs, "display.mean_curvature_renderer.smoothing", "");
177
            
177
            
178
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
178
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
179
            mean_curvatures(mani, scalars, smoothing);
179
            mean_curvatures(mani, scalars, smoothing);
180
            double max_G = 0;
180
            double max_G = 0;
181
            double mean = 0;
181
            double mean = 0;
182
            
182
            
183
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v){
183
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v){
184
                max_G = max(abs(scalars[*v]), max_G);
184
                max_G = max(abs(scalars[*v]), max_G);
185
                mean +=scalars[*v];
185
                mean +=scalars[*v];
186
            }
186
            }
187
            
187
            
188
            cout << "avg mean curvature " << mean/mani.no_vertices();
188
            cout << "avg mean curvature " << mean/mani.no_vertices();
189
            
189
            
190
            renderer = new ScalarFieldRenderer();
190
            renderer = new ScalarFieldRenderer();
191
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth, scalars, max_G, gamma);
191
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth, scalars, max_G, gamma);
192
        }
192
        }
193
        else if(short_name == "amb"){
193
        else if(short_name == "amb"){
194
            static Console::variable<int> smoothing(2);
194
            static Console::variable<int> smoothing(2);
195
            smoothing.reg(cs, "display.ambient_occlusion_renderer.smoothing", "");
195
            smoothing.reg(cs, "display.ambient_occlusion_renderer.smoothing", "");
196
            
196
            
197
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
197
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
198
            mean_curvatures(mani, scalars, smoothing);
198
            mean_curvatures(mani, scalars, smoothing);
199
            double max_G = 0;
199
            double max_G = 0;
200
            
200
            
201
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
201
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
202
                max_G = max(abs(scalars[*v]), max_G);
202
                max_G = max(abs(scalars[*v]), max_G);
203
            
203
            
204
            renderer = new AmbientOcclusionRenderer();
204
            renderer = new AmbientOcclusionRenderer();
205
            dynamic_cast<AmbientOcclusionRenderer*>(renderer)->compile_display_list(mani, scalars, max_G);
205
            dynamic_cast<AmbientOcclusionRenderer*>(renderer)->compile_display_list(mani, scalars, max_G);
206
            
206
            
207
        }
207
        }
208
        else if(short_name == "deb")
208
        else if(short_name == "deb")
209
        {
209
        {
-
 
210
            static Console::variable<float> debug_renderer_ball_radius(0.001);
-
 
211
            debug_renderer_ball_radius.reg(cs, "display.debug_renderer.radius","");
210
            renderer = new DebugRenderer;
212
            renderer = new DebugRenderer;
211
            renderer->compile_display_list(mani, smooth);
213
            dynamic_cast<DebugRenderer*>(renderer)->compile_display_list(mani, smooth, debug_renderer_ball_radius);
212
        }
214
        }
213
        else if(short_name == "che")
215
        else if(short_name == "che")
214
        {
216
        {
215
            renderer = new CheckerBoardRenderer;
217
            renderer = new CheckerBoardRenderer;
216
            renderer->compile_display_list(mani, smooth);
218
            renderer->compile_display_list(mani, smooth);
217
        }
219
        }
218
        else if(short_name == "sca")
220
        else if(short_name == "sca")
219
        {
221
        {
220
            double max_G = abs(scalar_field[*mani.vertices_begin()]);
222
            double max_G = abs(scalar_field[*mani.vertices_begin()]);
221
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
223
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
222
                max_G = max(abs(scalar_field[*v]), max_G);
224
                max_G = max(abs(scalar_field[*v]), max_G);
223
            renderer = new ScalarFieldRenderer();
225
            renderer = new ScalarFieldRenderer();
224
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth,
226
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth,
225
                                                                               scalar_field, max_G, gamma);
227
                                                                               scalar_field, max_G, gamma);
226
        }
228
        }
227
        else if(short_name == "lin")
229
        else if(short_name == "lin")
228
        {
230
        {
229
            renderer = new LineFieldRenderer();
231
            renderer = new LineFieldRenderer();
230
            dynamic_cast<LineFieldRenderer*>(renderer)->compile_display_list(mani, get_line_field_attrib_vector());
232
            dynamic_cast<LineFieldRenderer*>(renderer)->compile_display_list(mani, get_line_field_attrib_vector());
231
        }
233
        }
232
        else {
234
        else {
233
            renderer = new NormalRenderer();
235
            renderer = new NormalRenderer();
234
            renderer->compile_display_list(mani, smooth);
236
            renderer->compile_display_list(mani, smooth);
235
        }
237
        }
236
 
238
 
237
    
239
    
238
    }
240
    }
239
    
241
    
240
    void VisObj::draw_selection()
242
    void VisObj::draw_selection()
241
    {
243
    {
242
//        Vec3d c;
244
//        Vec3d c;
243
//        float r;
245
//        float r;
244
//        bsphere(mani, c, r);
246
//        bsphere(mani, c, r);
245
        float r = view_ctrl.get_eye_dist();
247
        float r = view_ctrl.get_eye_dist();
246
        r *= 0.001;
248
        r *= 0.003;
247
        glDisable(GL_LIGHTING);
249
        glDisable(GL_LIGHTING);
248
        for(auto vid : mani.vertices())
250
        for(auto vid : mani.vertices())
249
        {
251
        {
250
            Vec3d p = mani.pos(vid);
252
            Vec3d p = mani.pos(vid);
251
            if(vertex_selection[vid])
253
            if(vertex_selection[vid])
252
                glColor3f(1,1,0);
254
                glColor3f(1,1,0);
253
            else
255
            else
254
                glColor3f(0, 0, 0.3);
256
                glColor3f(0, 0, 0.3);
255
            glPushMatrix();
257
            glPushMatrix();
256
            glTranslated(p[0], p[1], p[2]);
258
            glTranslated(p[0], p[1], p[2]);
257
            glScalef(r, r, r);
259
            glScalef(r, r, r);
258
            draw_ball();
260
            draw_ball();
259
            glPopMatrix();
261
            glPopMatrix();
260
        }
262
        }
261
        glEnable(GL_LIGHTING);
263
        glEnable(GL_LIGHTING);
262
    }
264
    }
263
 
265
 
264
    
266
    
265
    void VisObj::display(const std::string& display_method , Console& cs, bool smooth, float gamma)
267
    void VisObj::display(const std::string& display_method , Console& cs, bool smooth, float gamma)
266
    {
268
    {
267
        if(create_display_list){
269
        if(create_display_list){
268
            create_display_list = false;
270
            create_display_list = false;
269
            produce_renderer(display_method, cs, smooth, gamma);
271
            produce_renderer(display_method, cs, smooth, gamma);
270
        }
272
        }
271
        view_ctrl.set_gl_modelview();
273
        view_ctrl.set_gl_modelview();
272
        renderer->draw();
274
        renderer->draw();
273
        if(active_selection)
275
        if(active_selection)
274
            draw_selection();
276
            draw_selection();
275
    }
277
    }
276
}
278
}
277
 
279
 
278

Generated by GNU Enscript 1.6.6.
280

Generated by GNU Enscript 1.6.6.
279
 
281
 
280
 
282
 
281
 
283