Subversion Repositories gelsvn

Rev

Rev 662 | Rev 665 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 662 Rev 663
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
                cout << "curv : " << scalars[*v] << endl;
185
                mean +=scalars[*v];
186
            }
186
            }
187
            
187
            
-
 
188
            cout << "avg mean curvature " << mean/mani.no_vertices();
-
 
189
            
188
            renderer = new ScalarFieldRenderer();
190
            renderer = new ScalarFieldRenderer();
189
            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);
190
        }
192
        }
191
        else if(short_name == "amb"){
193
        else if(short_name == "amb"){
192
            static Console::variable<int> smoothing(2);
194
            static Console::variable<int> smoothing(2);
193
            smoothing.reg(cs, "display.ambient_occlusion_renderer.smoothing", "");
195
            smoothing.reg(cs, "display.ambient_occlusion_renderer.smoothing", "");
194
            
196
            
195
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
197
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
196
            mean_curvatures(mani, scalars, smoothing);
198
            mean_curvatures(mani, scalars, smoothing);
197
            double max_G = 0;
199
            double max_G = 0;
198
            
200
            
199
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
201
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
200
                max_G = max(abs(scalars[*v]), max_G);
202
                max_G = max(abs(scalars[*v]), max_G);
201
            
203
            
202
            renderer = new AmbientOcclusionRenderer();
204
            renderer = new AmbientOcclusionRenderer();
203
            dynamic_cast<AmbientOcclusionRenderer*>(renderer)->compile_display_list(mani, scalars, max_G);
205
            dynamic_cast<AmbientOcclusionRenderer*>(renderer)->compile_display_list(mani, scalars, max_G);
204
            
206
            
205
        }
207
        }
206
        else if(short_name == "deb")
208
        else if(short_name == "deb")
207
        {
209
        {
208
            renderer = new DebugRenderer;
210
            renderer = new DebugRenderer;
209
            renderer->compile_display_list(mani, smooth);
211
            renderer->compile_display_list(mani, smooth);
210
        }
212
        }
211
        else if(short_name == "che")
213
        else if(short_name == "che")
212
        {
214
        {
213
            renderer = new CheckerBoardRenderer;
215
            renderer = new CheckerBoardRenderer;
214
            renderer->compile_display_list(mani, smooth);
216
            renderer->compile_display_list(mani, smooth);
215
        }
217
        }
216
        else if(short_name == "sca")
218
        else if(short_name == "sca")
217
        {
219
        {
218
            double max_G = abs(scalar_field[*mani.vertices_begin()]);
220
            double max_G = abs(scalar_field[*mani.vertices_begin()]);
219
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
221
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
220
                max_G = max(abs(scalar_field[*v]), max_G);
222
                max_G = max(abs(scalar_field[*v]), max_G);
221
            renderer = new ScalarFieldRenderer();
223
            renderer = new ScalarFieldRenderer();
222
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth,
224
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth,
223
                                                                               scalar_field, max_G, gamma);
225
                                                                               scalar_field, max_G, gamma);
224
        }
226
        }
-
 
227
        else if(short_name == "lin")
-
 
228
        {
-
 
229
            renderer = new LineFieldRenderer();
-
 
230
            dynamic_cast<LineFieldRenderer*>(renderer)->compile_display_list(mani, get_line_field_attrib_vector());
-
 
231
        }
225
        else {
232
        else {
226
            renderer = new NormalRenderer();
233
            renderer = new NormalRenderer();
227
            renderer->compile_display_list(mani, smooth);
234
            renderer->compile_display_list(mani, smooth);
228
        }
235
        }
229
 
236
 
230
    
237
    
231
    }
238
    }
232
    
239
    
233
    void VisObj::draw_selection()
240
    void VisObj::draw_selection()
234
    {
241
    {
235
        Vec3d c;
242
        Vec3d c;
236
        float r;
243
        float r;
237
        bsphere(mani, c, r);
244
        bsphere(mani, c, r);
238
        r *= 0.003;
245
        r *= 0.003;
239
        glDisable(GL_LIGHTING);
246
        glDisable(GL_LIGHTING);
240
        for(auto vid : mani.vertices())
247
        for(auto vid : mani.vertices())
241
        {
248
        {
242
            Vec3d p = mani.pos(vid);
249
            Vec3d p = mani.pos(vid);
243
            if(vertex_selection[vid])
250
            if(vertex_selection[vid])
244
                glColor3f(1,1,0);
251
                glColor3f(1,1,0);
245
            else
252
            else
246
                glColor3f(0, 0, 0.3);
253
                glColor3f(0, 0, 0.3);
247
            glPushMatrix();
254
            glPushMatrix();
248
            glTranslated(p[0], p[1], p[2]);
255
            glTranslated(p[0], p[1], p[2]);
249
            glScalef(r, r, r);
256
            glScalef(r, r, r);
250
            draw_ball();
257
            draw_ball();
251
            glPopMatrix();
258
            glPopMatrix();
252
        }
259
        }
253
        glEnable(GL_LIGHTING);
260
        glEnable(GL_LIGHTING);
254
    }
261
    }
255
 
262
 
256
    
263
    
257
    void VisObj::display(const std::string& display_method , Console& cs, bool smooth, float gamma)
264
    void VisObj::display(const std::string& display_method , Console& cs, bool smooth, float gamma)
258
    {
265
    {
259
        if(create_display_list){
266
        if(create_display_list){
260
            create_display_list = false;
267
            create_display_list = false;
261
            produce_renderer(display_method, cs, smooth, gamma);
268
            produce_renderer(display_method, cs, smooth, gamma);
262
        }
269
        }
263
        view_ctrl.set_gl_modelview();
270
        view_ctrl.set_gl_modelview();
264
        renderer->draw();
271
        renderer->draw();
265
        if(active_selection)
272
        if(active_selection)
266
            draw_selection();
273
            draw_selection();
267
    }
274
    }
268
}
275
}
269
 
276
 
270

Generated by GNU Enscript 1.6.6.
277

Generated by GNU Enscript 1.6.6.
271
 
278
 
272
 
279
 
273
 
280