1 #include "bgui_vtol2D_tableau.h"
2 //:
3 // \file
4 #include <bgui/bgui_vtol_soview2D.h>
5 #include "vgui/vgui.h"
6 #include "vgui/vgui_style.h"
7 #include <vtol/vtol_face_2d.h>
8 #include <vdgl/vdgl_edgel_chain.h>
9 #include <vdgl/vdgl_interpolator.h>
10 
11 
bgui_vtol2D_tableau(const char * n)12 bgui_vtol2D_tableau::bgui_vtol2D_tableau(const char* n) :
13   bgui_vsol2D_tableau(n) { this->init(); }
14 
bgui_vtol2D_tableau(vgui_image_tableau_sptr const & it,const char * n)15 bgui_vtol2D_tableau::bgui_vtol2D_tableau(vgui_image_tableau_sptr const& it,
16                                          const char* n) :
17   bgui_vsol2D_tableau(it, n) { this->init(); }
18 
bgui_vtol2D_tableau(vgui_tableau_sptr const & t,const char * n)19 bgui_vtol2D_tableau::bgui_vtol2D_tableau(vgui_tableau_sptr const& t,
20                                          const char* n) :
21   bgui_vsol2D_tableau(t, n) { this->init(); }
22 
~bgui_vtol2D_tableau()23 bgui_vtol2D_tableau::~bgui_vtol2D_tableau()
24 {
25 #if 0 //not needed if vgui::quit() is used
26    this->clear_all();
27 #endif
28 }
29 
30 #ifdef DEBUG
print_edgels(vtol_edge_2d_sptr const & e)31 static void print_edgels(vtol_edge_2d_sptr const & e)
32 {
33   vsol_curve_2d_sptr c = e->curve();
34   if (!c) return;
35   vdgl_digital_curve_sptr dc = c->cast_to_vdgl_digital_curve();
36   if (!dc) return;
37   vdgl_interpolator_sptr trp = dc->get_interpolator();
38   if (!trp) return;
39   vdgl_edgel_chain_sptr ec = trp->get_edgel_chain();
40   if (!ec)
41     return;
42   int N = ec->size();
43   for (int i = 0; i<N; i++)
44     std::cout << "egl(" << i << ")=" << (*ec)[i] << '\n';
45 }
46 #endif
47 
init()48 void bgui_vtol2D_tableau::init()
49 {
50   //define default soview styles
51   //these can be overridden by later set_*_syle commands prior to drawing.
52   //
53   vertex_style_     = vgui_style::new_style(1.0f, 0.0f, 0.0f, 3.0f, 0.0f);
54   edge_style_       = vgui_style::new_style(0.0f, 1.0f, 0.0f, 1.0f, 3.0f);
55   edge_group_style_ = vgui_style::new_style(0.0f, 1.0f, 0.0f, 1.0f, 3.0f);
56   face_style_       = vgui_style::new_style(0.0f, 1.0f, 0.0f, 1.0f, 3.0f);
57 
58   //call the init() function of bgui_vsol2D_tableau
59   bgui_vsol2D_tableau::init();
60 }
61 
handle(vgui_event const & e)62 bool bgui_vtol2D_tableau::handle(vgui_event const &e)
63 {
64   // We aren't interested in other events so pass them to the base class.
65   return bgui_vsol2D_tableau::handle(e);
66 }
67 
68 //display topological objects
69 bgui_vtol_soview2D_vertex*
add_vertex(vtol_vertex_2d_sptr const & v,const vgui_style_sptr & style)70 bgui_vtol2D_tableau::add_vertex(vtol_vertex_2d_sptr const& v,
71                                 const vgui_style_sptr& style)
72 {
73   bgui_vtol_soview2D_vertex* obj = new bgui_vtol_soview2D_vertex();
74   obj->x = (float)v->x();
75   obj->y = (float)v->y();
76   add(obj);
77   if (style)
78     obj->set_style( style );
79   else
80     obj->set_style( vertex_style_ );
81 
82   if (obj) {
83     int id = obj->get_id();
84     obj_map_[id]=v->cast_to_topology_object();
85   }
86   return obj;
87 }
88 
89 
90 bgui_vtol_soview2D_edge*
add_edge(vtol_edge_2d_sptr const & e,const vgui_style_sptr & style)91 bgui_vtol2D_tableau::add_edge(vtol_edge_2d_sptr const& e,
92                               const vgui_style_sptr& style)
93 {
94 #ifdef DEBUG
95   print_edgels(e);
96 #endif
97   bgui_vtol_soview2D_edge* obj = new bgui_vtol_soview2D_edge(e);
98 
99   add(obj);
100   if (style)
101     obj->set_style( style );
102   else
103     obj->set_style( edge_style_ );
104 
105   if (obj) {
106     int id = obj->get_id();
107     obj_map_[id]=e->cast_to_topology_object();
108   }
109   return obj;
110 }
111 
112 
113 bgui_vtol_soview2D_edge_group*
add_edge_group(std::vector<vtol_edge_2d_sptr> & edges,const vgui_style_sptr & style)114 bgui_vtol2D_tableau::add_edge_group(std::vector<vtol_edge_2d_sptr>& edges,
115                                     const vgui_style_sptr& style )
116 {
117   bgui_vtol_soview2D_edge_group* obj =
118     new bgui_vtol_soview2D_edge_group(edges);
119   add(obj);
120   if (style)
121     obj->set_style( style );
122   else
123     obj->set_style( edge_group_style_ );
124 
125   return obj;
126 }
127 
128 
129 bgui_vtol_soview2D_face*
add_face(vtol_face_2d_sptr const & f,const vgui_style_sptr & style)130 bgui_vtol2D_tableau::add_face(vtol_face_2d_sptr const& f,
131                               const vgui_style_sptr& style)
132 {
133   bgui_vtol_soview2D_face* obj = new bgui_vtol_soview2D_face(f);
134 
135   add(obj);
136   if (style)
137     obj->set_style( style );
138   else
139     obj->set_style( face_style_ );
140 
141   if (obj) {
142     int id = obj->get_id();
143     obj_map_[id]=f->cast_to_topology_object();
144   }
145   return obj;
146 }
147 
148 //--------------------------------------------------------------
149 // Add a list of generic topology objects
150 //
151 void bgui_vtol2D_tableau::
add_topology_objects(std::vector<vtol_topology_object_sptr> const & tos,const vgui_style_sptr & style)152 add_topology_objects(std::vector<vtol_topology_object_sptr> const& tos,
153                      const vgui_style_sptr& style)
154 {
155   for (std::vector<vtol_topology_object_sptr>::const_iterator tot = tos.begin();
156        tot != tos.end(); tot++)
157     {
158       add_topology_object((*tot) , style );
159     }
160 }
161 
162 
163 void bgui_vtol2D_tableau::
add_topology_object(vtol_topology_object_sptr const & tos,const vgui_style_sptr & style)164 add_topology_object(vtol_topology_object_sptr const& tos,
165                     const vgui_style_sptr& style)
166 {
167   if (tos->cast_to_vertex()) {
168     if (tos->cast_to_vertex()->cast_to_vertex_2d())
169     {
170       vtol_vertex_2d_sptr v = tos->cast_to_vertex()->cast_to_vertex_2d();
171       this->add_vertex(v , style );
172     }
173   }
174   else if (tos->cast_to_edge()) {
175     if (tos->cast_to_edge()->cast_to_edge_2d())
176     {
177       vtol_edge_2d_sptr e = tos->cast_to_edge()->cast_to_edge_2d();
178       this->add_edge(e , style );
179     }
180   }
181   else if (tos->cast_to_face()) {
182     if (tos->cast_to_face()->cast_to_face_2d())
183     {
184       vtol_face_2d_sptr f = tos->cast_to_face()->cast_to_face_2d();
185       this->add_face(f , style );
186     }
187   }
188 }
189 
190 
add_edges(std::vector<vtol_edge_2d_sptr> const & edges,bool verts,const vgui_style_sptr & style)191 void bgui_vtol2D_tableau::add_edges(std::vector<vtol_edge_2d_sptr> const& edges,
192                                     bool verts ,
193                                     const vgui_style_sptr& style )
194 {
195   for (std::vector<vtol_edge_2d_sptr>::const_iterator eit = edges.begin();
196        eit != edges.end(); eit++)
197   {
198     this->add_edge(*eit , style );
199     //optionally display the edge vertices
200     if (verts)
201     {
202       std::vector<vtol_vertex_sptr> vts; (*eit)->vertices(vts);
203       for (std::vector<vtol_vertex_sptr>::iterator vit = vts.begin();
204            vit != vts.end(); vit++)
205         this->add_vertex((*vit)->cast_to_vertex_2d(), style);
206     }
207   }
208 }
209 
210 
211 void
add_faces(std::vector<vtol_face_2d_sptr> const & faces,bool verts,const vgui_style_sptr & style)212 bgui_vtol2D_tableau::add_faces(std::vector<vtol_face_2d_sptr> const& faces,
213                                bool verts,
214                                const vgui_style_sptr& style )
215 {
216   for (std::vector<vtol_face_2d_sptr>::const_iterator fit = faces.begin();
217        fit != faces.end(); fit++)
218   {
219     vtol_face_2d_sptr f = (*fit);
220     this->add_face(f , style );
221     if (verts)
222     {
223       std::vector<vtol_vertex_sptr> vts; f->vertices(vts);
224       for (std::vector<vtol_vertex_sptr>::iterator vit = vts.begin();
225            vit != vts.end(); vit++)
226         this->add_vertex((*vit)->cast_to_vertex_2d(), style);
227     }
228   }
229 }
230 
231 
get_mapped_edge(const int id)232 vtol_edge_2d_sptr bgui_vtol2D_tableau::get_mapped_edge(const int id)
233 {
234   vtol_topology_object_sptr to = obj_map_[id];
235   if (!to)
236   {
237     std::cout << "In bgui_vtol2D_tableau::get_mapped_edge(..) - null map entry\n";
238     return nullptr;
239   }
240   return to->cast_to_edge()->cast_to_edge_2d();
241 }
242 
get_mapped_face(const int id)243 vtol_face_2d_sptr bgui_vtol2D_tableau::get_mapped_face(const int id)
244 {
245   vtol_topology_object_sptr to = obj_map_[id];
246   if (!to)
247   {
248     std::cout << "In bgui_vtol2D_tableau::get_mapped_face(..) - null map entry\n";
249     return nullptr;
250   }
251   return to->cast_to_face()->cast_to_face_2d();
252 }
253 
254 
clear_all()255 void bgui_vtol2D_tableau::clear_all()
256 {
257   obj_map_.clear();
258   //now call the clear_all from bgui_vsol2D_tableau
259 
260   vgui_easy2D_tableau::clear();
261 }
262 
set_vtol_topology_object_style(vtol_topology_object_sptr tos,const vgui_style_sptr & style)263 void bgui_vtol2D_tableau::set_vtol_topology_object_style(vtol_topology_object_sptr tos,
264                                                          const vgui_style_sptr& style)
265 {
266   if (tos->cast_to_vertex())
267     set_vertex_style(style);
268   else if (tos->cast_to_edge()) {
269     set_edge_style(style);
270     set_edge_group_style(style);
271   }
272   else if (tos->cast_to_face())
273     set_face_style(style);
274 }
275 
set_vertex_style(const vgui_style_sptr & style)276 void bgui_vtol2D_tableau::set_vertex_style(const vgui_style_sptr& style)
277 {
278   vertex_style_ = style;
279 }
280 
set_edge_style(const vgui_style_sptr & style)281 void bgui_vtol2D_tableau::set_edge_style(const vgui_style_sptr& style)
282 {
283   edge_style_ = style;
284 }
285 
set_edge_group_style(const vgui_style_sptr & style)286 void bgui_vtol2D_tableau::set_edge_group_style(const vgui_style_sptr& style)
287 {
288   edge_group_style_ = style;
289 }
290 
set_face_style(const vgui_style_sptr & style)291 void bgui_vtol2D_tableau::set_face_style(const vgui_style_sptr& style)
292 {
293   face_style_ = style;
294 }
295