~ubuntu-branches/ubuntu/wily/ginkgocadx/wily-proposed

« back to all changes in this revision

Viewing changes to src/cadxcore/widgets/wpoligono.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Tille
  • Date: 2011-05-02 08:09:26 UTC
  • Revision ID: james.westby@ubuntu.com-20110502080926-bql5wep49c7hg91t
Tags: upstream-2.4.1.1
ImportĀ upstreamĀ versionĀ 2.4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  
 
3
 *  $Id: wpoligono.cpp 3535 2011-03-18 17:57:05Z carlos $
 
4
 *  Ginkgo CADx Project
 
5
 *
 
6
 *  Copyright 2008-10 MetaEmotion S.L. All rights reserved.
 
7
 *  http://ginkgo-cadx.com
 
8
 *
 
9
 *  This file is licensed under LGPL v3 license.
 
10
 *  See License.txt for details
 
11
 *
 
12
 *
 
13
 */
 
14
//#define _GINKGO_TRACE
 
15
#include <wx/xml/xml.h>
 
16
#include <api/globals.h>
 
17
#include <api/icontexto.h>
 
18
#include "wpoligono.h"
 
19
#include <api/iwidgetsrenderer.h>
 
20
#include <api/ievento.h>
 
21
#include <eventos/eventosginkgo.h>
 
22
#include "main/managers/widgetsmanager.h"
 
23
#include <api/internacionalizacion.h>
 
24
 
 
25
#include <iterator>
 
26
#include <vtkgl.h>
 
27
#include <api/westilo.h>
 
28
#include <cmath>
 
29
 
 
30
//region "Implementacion de WPoligonoBuilder"
 
31
 
 
32
GNC::GCS::Widgets::WPoligonoBuilder::WPoligonoBuilder(ModoPoligono modo, GNC::GCS::IWidgetsManager* pManager, GNC::GCS::Widgets::Observadores::IObservadorPoligono* pObservador) : GNC::GCS::Widgets::IWidgetBuilder(pManager), GNC::GCS::Widgets::Notificadores::INotificadorPoligono(pObservador)
 
33
{
 
34
        m_NumNodos = 0;
 
35
        m_MouseDown = false;
 
36
        m_Estado = WBS_Ninguno;
 
37
        SetRadioNodo(5.0f);
 
38
        m_pPoligonoCreado = NULL;
 
39
        m_Modo = modo;
 
40
}
 
41
 
 
42
GNC::GCS::Widgets::WPoligonoBuilder::~WPoligonoBuilder()
 
43
{
 
44
        m_Vertices.clear();
 
45
        if (m_Modo == MP_Lazo) {
 
46
                if (m_pPoligonoCreado) {
 
47
                        NotificarDestruccion(m_pPoligonoCreado);
 
48
                        m_pManager->EliminarWidget(m_pPoligonoCreado, false);
 
49
                        m_pPoligonoCreado = NULL;
 
50
                }
 
51
        }
 
52
}
 
53
 
 
54
void GNC::GCS::Widgets::WPoligonoBuilder::OnMouseEvents(GNC::GCS::Eventos::EventoRaton& evento)
 
55
{
 
56
        if (!m_pManager) {
 
57
                return;
 
58
        }
 
59
        if (m_MouseDown && evento.ButtonUp(GNC::GCS::Eventos::EventoRaton::EB_IZQUIERDO)) {
 
60
                m_MouseDown = false;
 
61
                if (m_Estado != WBS_Creando) {
 
62
                        m_Estado = WBS_Ninguno;
 
63
                        return;
 
64
                }
 
65
                m_Estado = WBS_Ninguno;
 
66
                m_PosCursor= evento.iP;
 
67
 
 
68
                if (m_Modo == MP_Lazo && m_pPoligonoCreado != NULL) {
 
69
                        NotificarDestruccion(m_pPoligonoCreado);
 
70
                        m_pManager->EliminarWidget(m_pPoligonoCreado, false);
 
71
                        m_pPoligonoCreado = NULL;
 
72
                }
 
73
                if(m_Vertices.size()>2){
 
74
                        m_pPoligonoCreado = new WPoligono(m_Modo, m_pManager, evento.c->GetRenderer()->GetVID(), m_Vertices, m_RadioNodo, "PolĆ­gono");
 
75
                        m_pManager->InsertarWidget(m_pPoligonoCreado);
 
76
                        NotificarCreacion(m_pPoligonoCreado);
 
77
                        m_pManager->LanzarEventoCreacion(m_pPoligonoCreado);
 
78
                }
 
79
                m_Vertices.clear();
 
80
                evento.Skip(false);
 
81
                m_pManager->Modificado();
 
82
 
 
83
        }
 
84
        else if (evento.ButtonDown(GNC::GCS::Eventos::EventoRaton::EB_IZQUIERDO)) {
 
85
                if (m_Estado != WBS_Ninguno) {
 
86
                        return;
 
87
                }
 
88
 
 
89
                m_MouseDown = true;
 
90
                if (m_Modo == MP_Lazo && m_pPoligonoCreado != NULL) {
 
91
                        NotificarDestruccion(m_pPoligonoCreado);
 
92
                        m_pManager->EliminarWidget(m_pPoligonoCreado, false);
 
93
                        m_pPoligonoCreado = NULL;
 
94
                }
 
95
 
 
96
                //aviso que han soltado
 
97
                m_pManager->Modificado();
 
98
                m_Estado = WBS_Creando;
 
99
                ConsumirEvento();
 
100
 
 
101
        }
 
102
        else if (evento.Dragging() && m_MouseDown) {
 
103
 
 
104
                if (m_Estado != WBS_Creando) {
 
105
                        return;
 
106
                }
 
107
 
 
108
                GTRACE(" ---->"<<(evento.iP - m_PosCursor).Norma2Cuadrado()<<" minimo= " << m_DistanciaMinimaCuadrado);
 
109
 
 
110
                if (!m_Vertices.empty() && ((evento.iP - m_PosCursor)/evento.c->RelacionImagenPantalla()).Norma2Cuadrado() < m_DistanciaMinimaCuadrado) {
 
111
                        return;
 
112
                }
 
113
                m_PosCursor = evento.iP;
 
114
                m_Vertices.push_back(m_PosCursor);
 
115
                m_pManager->Modificado();
 
116
                ConsumirEvento();
 
117
        }
 
118
        else if( evento.Moving() ){
 
119
                m_PosCursor= evento.iP;
 
120
                m_Estado = WBS_Ninguno;
 
121
                ConsumirEvento();
 
122
        }
 
123
}
 
124
 
 
125
void GNC::GCS::Widgets::WPoligonoBuilder::OnKeyEvents(GNC::GCS::Eventos::EventoTeclado& /*evento*/)
 
126
{
 
127
        GTRACE("GNC::GCS::Widgets::WPoligonoBuilder::OnKeyEvents(wxKeyEvent&)");
 
128
}
 
129
 
 
130
void GNC::GCS::Widgets::WPoligonoBuilder::Render(GNC::GCS::Contexto3D* /*c*/)
 
131
{
 
132
 
 
133
        if (m_Estado != WBS_Creando) {
 
134
                return;
 
135
        }
 
136
 
 
137
        glLineWidth(WIDGET_GROSOR_LINEA);
 
138
        wColorNormal.Aplicar();
 
139
 
 
140
        VerticesPoligono::iterator prev = m_Vertices.begin();
 
141
        if (prev != m_Vertices.end()) {
 
142
                VerticesPoligono::iterator curr = prev;
 
143
                curr++;
 
144
                for (; curr != m_Vertices.end(); curr++) {
 
145
                        glBegin(GL_LINES);
 
146
                        glVertex2d((*prev).x, (*prev).y);
 
147
                        glVertex2d((*curr).x, (*curr).y);
 
148
                        glEnd();
 
149
                        prev = curr;
 
150
                }
 
151
        }
 
152
 
 
153
 
 
154
        if (m_Vertices.size() > 2) {
 
155
                glEnable(GL_LINE_STIPPLE);
 
156
                glColor4f(0.7f, 0.7f, 0.0f, 1.0);
 
157
                glLineStipple(3, 0xAAAA);
 
158
                glBegin(GL_LINES);
 
159
                glVertex2d(m_Vertices.front().x, m_Vertices.front().y);
 
160
                glVertex2d(m_Vertices.back().x, m_Vertices.back().y);
 
161
                glEnd();
 
162
                glDisable(GL_LINE_STIPPLE);
 
163
        }
 
164
 
 
165
 
 
166
        glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
 
167
        glPointSize(m_RadioNodo);
 
168
        glBegin(GL_POINTS);
 
169
        for (VerticesPoligono::iterator it = m_Vertices.begin(); it != m_Vertices.end(); it++) {
 
170
                glVertex2d((*it).x, (*it).y);
 
171
        }
 
172
        glEnd();
 
173
 
 
174
}
 
175
 
 
176
GNC::GCS::Widgets::TipoCursor GNC::GCS::Widgets::WPoligonoBuilder::GetCursor()
 
177
{
 
178
        return GNC::GCS::Widgets::CUR_CREAR_POLIGONO;
 
179
}
 
180
 
 
181
//endregion
 
182
 
 
183
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
184
 
 
185
//region "Constructor y destructor"
 
186
 
 
187
GNC::GCS::Widgets::WPoligono::WPoligono(GNC::GCS::Widgets::ModoPoligono modo, IWidgetsManager* pManager, unsigned int vid, VerticesPoligono& vertices, float radioNodo, const char* nombre) : GNC::GCS::Widgets::IWidget(pManager, vid, nombre), GNC::GCS::Widgets::IWidgetSerializable()
 
188
{
 
189
        m_Vertices = vertices;
 
190
 
 
191
        m_RadioNodo = radioNodo;
 
192
        m_Modo = modo;
 
193
        Recalcular();
 
194
        m_MouseDown = false;
 
195
        m_PosCursor = m_Centro;
 
196
}
 
197
 
 
198
GNC::GCS::Widgets::WPoligono::~WPoligono()
 
199
{
 
200
        if(m_Modo != GNC::GCS::Widgets::MP_Lazo){
 
201
                LanzarEventoDestruccion();
 
202
        }
 
203
}
 
204
 
 
205
//endregion
 
206
 
 
207
//region "Serializado y desserializado"
 
208
GNC::GCS::Widgets::WPoligono::WPoligono(IWidgetsManager* pManager, long vid, wxXmlNode* nodo, float radioNodo): GNC::GCS::Widgets::IWidget(pManager, vid, "PolĆ­gono"), GNC::GCS::Widgets::IWidgetSerializable(nodo)
 
209
{
 
210
        if(nodo->GetName() != wxT("poligono_widget")){
 
211
                std::cerr<< "Ha ocurrido un error al desserializar el widget poligono"<<std::endl;
 
212
        }
 
213
 
 
214
        if(nodo->GetPropVal(wxT("mode"),wxT("normal")) == wxT("lazo")){
 
215
                m_Modo = MP_Lazo;
 
216
        } else {
 
217
                m_Modo = MP_Normal;
 
218
        }
 
219
 
 
220
        wxXmlNode *child = nodo->GetChildren();
 
221
        GNC::GCS::Nodo nodoTmp;
 
222
        while (child) {
 
223
                nodoTmp = GNC::GCS::Nodo::Deserializar(child);
 
224
                GNC::GCS::Vector v(nodoTmp);
 
225
                m_Vertices.push_back(v);
 
226
                child = child->GetNext();
 
227
        }
 
228
 
 
229
        m_RadioNodo = radioNodo;
 
230
        m_MouseDown = false;
 
231
        Recalcular();
 
232
        m_PosCursor = m_Centro;
 
233
}
 
234
 
 
235
wxXmlNode* GNC::GCS::Widgets::WPoligono::Serializar(const std::string& nombreMedico)
 
236
{
 
237
        wxXmlNode* resultado = new wxXmlNode(NULL,wxXML_ELEMENT_NODE,wxT("poligono_widget"));
 
238
        if(m_Modo == MP_Lazo){
 
239
                resultado->AddProperty(wxT("mode"),wxT("lazo"));
 
240
        } else {
 
241
                resultado->AddProperty(wxT("mode"),wxT("normal"));
 
242
        }
 
243
 
 
244
        int i=0;
 
245
        for (VerticesPoligono::iterator it = m_Vertices.begin(); it != m_Vertices.end(); it++) {
 
246
                wxXmlNode* nodo = new wxXmlNode(NULL,wxXML_ELEMENT_NODE,wxString::Format(wxT("nodo%d"),i));
 
247
                nodo->AddProperty(new wxXmlProperty(wxT("x"),wxString::Format(wxT("%f"),(*it).x)));
 
248
                nodo->AddProperty(new wxXmlProperty(wxT("y"),wxString::Format(wxT("%f"),(*it).y)));
 
249
                resultado->AddChild(nodo);
 
250
                i++;
 
251
        }
 
252
        //metadatos
 
253
        SerializarMetadatos(resultado,nombreMedico);
 
254
        return resultado;
 
255
}
 
256
//endregion
 
257
 
 
258
void GNC::GCS::Widgets::WPoligono::OnMouseEvents(GNC::GCS::Eventos::EventoRaton& evento)
 
259
{
 
260
        if(EstaOculto()){
 
261
                return;
 
262
        }
 
263
        //------------------------------------------------------
 
264
        // Leaving
 
265
        if (evento.Leaving()){
 
266
                Iluminar(false);
 
267
        }
 
268
        //------------------------------------------------------
 
269
        // Entering
 
270
        else if (evento.Entering() ) {
 
271
                if (m_MouseDown) {
 
272
                        Iluminar(true);
 
273
                }
 
274
        }
 
275
        //------------------------------------------------------
 
276
        // Dragging
 
277
        else if (evento.Dragging() && m_MouseDown) {
 
278
                Vector delta = evento.iP - m_PosCursor;
 
279
                m_PosCursor = evento.iP;
 
280
 
 
281
                if (EstaSeleccionado()) {
 
282
                        for (VerticesPoligono::iterator it = m_Vertices.begin(); it != m_Vertices.end(); it++) {
 
283
                                (*it) += delta;
 
284
                        }
 
285
 
 
286
                        Recalcular();
 
287
 
 
288
                        Modificar(true);
 
289
 
 
290
                        LanzarEventoModificacion();
 
291
                }
 
292
 
 
293
                ConsumirEvento();
 
294
 
 
295
                return;
 
296
        }
 
297
        //------------------------------------------------------
 
298
        // LeftDown
 
299
        else if (evento.LeftDown()) {
 
300
 
 
301
                if (EventoConsumido() && !evento.m_controlDown) {
 
302
                        Seleccionar(false);
 
303
                        return;
 
304
                }
 
305
 
 
306
                bool dentro = false;
 
307
 
 
308
                if (evento.iP.DentroDePoligono2(m_Vertices)) {
 
309
                        dentro = true;
 
310
                        if (evento.m_controlDown) { // Inversion de seleccion
 
311
                                InvertirSeleccion();
 
312
                        }
 
313
                        else{
 
314
                                Seleccionar(true);
 
315
                        }
 
316
                }
 
317
                else {
 
318
                        if (!evento.m_controlDown) {
 
319
                                Seleccionar(false);
 
320
                        }
 
321
                        else {
 
322
                                ConsumirEvento();
 
323
                                if (EstaSeleccionado()) {
 
324
                                        m_MouseDown = true;
 
325
                                        m_PosCursor= evento.iP;
 
326
                                }
 
327
                        }
 
328
                }
 
329
 
 
330
                if (dentro) {
 
331
                        m_MouseDown = true;
 
332
                        m_PosCursor= evento.iP;
 
333
                        m_Modificado = true;
 
334
                        ConsumirEvento();
 
335
                }
 
336
 
 
337
                return;
 
338
        }
 
339
        //------------------------------------------------------
 
340
        // LeftUP
 
341
        else if (evento.LeftUp()) {
 
342
                if (m_MouseDown) {
 
343
                        ActualizarTimestampModificacion();
 
344
                        m_MouseDown = false;
 
345
                        ConsumirEvento();
 
346
                }
 
347
        }
 
348
        //------------------------------------------------------
 
349
        // Moving
 
350
        else if (evento.Moving()) {
 
351
 
 
352
                if (EventoConsumido()) {
 
353
                        Iluminar(false);
 
354
                        return;
 
355
                }
 
356
 
 
357
                bool dentro = false;
 
358
 
 
359
                m_PosCursor= evento.iP;
 
360
 
 
361
                if (evento.iP.DentroDePoligono2(m_Vertices)) {
 
362
                        Iluminar(true);
 
363
                        dentro = true;
 
364
                }
 
365
                else {
 
366
                        Iluminar(false);
 
367
                }
 
368
                if (dentro)
 
369
                {
 
370
                        ConsumirEvento();
 
371
                }
 
372
        }
 
373
 
 
374
}
 
375
 
 
376
void GNC::GCS::Widgets::WPoligono::OnKeyEvents(GNC::GCS::Eventos::EventoTeclado& evento)
 
377
{
 
378
        if (evento.GetKeyCode() == WXK_ESCAPE) {
 
379
                Seleccionar(false);
 
380
        }
 
381
}
 
382
 
 
383
bool GNC::GCS::Widgets::WPoligono::HitTest(float x, float y, float umbralCuadrado)
 
384
{
 
385
        GNC::GCS::Vector punto(x, y);
 
386
 
 
387
        bool hits = punto.DentroDePoligono2(m_Vertices);
 
388
 
 
389
        if (! hits ) {
 
390
                punto.Asignar(x - umbralCuadrado, y - umbralCuadrado);
 
391
                hits = punto.DentroDePoligono2(m_Vertices);
 
392
        }
 
393
        if (! hits ) {
 
394
                punto.Asignar(x, y - umbralCuadrado);
 
395
                hits = punto.DentroDePoligono2(m_Vertices);
 
396
        }
 
397
        if (! hits ) {
 
398
                punto.Asignar(x + umbralCuadrado, y + umbralCuadrado);
 
399
                hits = punto.DentroDePoligono2(m_Vertices);
 
400
        }
 
401
        if (! hits ) {
 
402
                punto.Asignar(x + umbralCuadrado, y);
 
403
                hits = punto.DentroDePoligono2(m_Vertices);
 
404
        }
 
405
        if (! hits ) {
 
406
                punto.Asignar(x + umbralCuadrado, y - umbralCuadrado);
 
407
                hits = punto.DentroDePoligono2(m_Vertices);
 
408
        }
 
409
        if (! hits ) {
 
410
                punto.Asignar(x , y - umbralCuadrado);
 
411
                hits = punto.DentroDePoligono2(m_Vertices);
 
412
        }
 
413
        if (! hits ) {
 
414
                punto.Asignar(x - umbralCuadrado, y - umbralCuadrado);
 
415
                hits = punto.DentroDePoligono2(m_Vertices);
 
416
        }
 
417
        if (! hits ) {
 
418
                punto.Asignar(x - umbralCuadrado, y);
 
419
                hits = punto.DentroDePoligono2(m_Vertices);
 
420
        }
 
421
        return hits;
 
422
}
 
423
 
 
424
bool GNC::GCS::Widgets::WPoligono::HitTest(GNC::GCS::Vector* vertices, int numVertices)
 
425
{
 
426
        bool hits = true;
 
427
 
 
428
        for (VerticesPoligono::iterator it = m_Vertices.begin(); hits && it != m_Vertices.end(); it++) {
 
429
                if (!(*it).DentroDePoligono2(vertices, numVertices) ) {
 
430
                        hits = false;
 
431
                }
 
432
        }
 
433
 
 
434
        return hits;
 
435
}
 
436
 
 
437
void GNC::GCS::Widgets::WPoligono::Render(GNC::GCS::Contexto3D* c)
 
438
{
 
439
        if(m_Oculto){
 
440
                return;
 
441
        }
 
442
 
 
443
 
 
444
        GNC::GCS::Vector escala = c->RelacionImagenPantalla();
 
445
        glLineWidth(WIDGET_GROSOR_LINEA);
 
446
 
 
447
        float fsombrax, fsombray;
 
448
 
 
449
        if (m_Seleccionado && m_MouseDown)
 
450
        {
 
451
                fsombrax = WIDGET_OFFSET_X_SOMBRA_SELECCIONADO * escala.x;
 
452
                fsombray = WIDGET_OFFSET_Y_SOMBRA_SELECCIONADO * escala.y;
 
453
        }
 
454
        else
 
455
        {
 
456
                fsombrax = WIDGET_OFFSET_X_SOMBRA * escala.x;
 
457
                fsombray = WIDGET_OFFSET_Y_SOMBRA * escala.y;
 
458
        }
 
459
 
 
460
        wColorSombra.Aplicar();
 
461
        glBegin(GL_LINE_LOOP);
 
462
        for (VerticesPoligono::iterator it = m_Vertices.begin(); it != m_Vertices.end(); it++) {
 
463
                glVertex2d((*it).x + fsombrax, (*it).y - fsombray);
 
464
        }
 
465
        glEnd();
 
466
 
 
467
        glBegin(GL_POINTS);
 
468
        for (VerticesPoligono::iterator it = m_Vertices.begin(); it != m_Vertices.end(); it++) {
 
469
                glVertex2d((*it).x + fsombrax, (*it).y - fsombray);
 
470
        }
 
471
        glEnd();
 
472
 
 
473
        // Dibujo el poligono
 
474
        wAplicarColor(m_Iluminado, m_MouseDown, m_Seleccionado);
 
475
        glBegin(GL_LINE_LOOP);
 
476
        for (VerticesPoligono::iterator it = m_Vertices.begin(); it != m_Vertices.end(); it++) {
 
477
                glVertex2d((*it).x, (*it).y);
 
478
        }
 
479
        glEnd();
 
480
 
 
481
 
 
482
        // Dibujo los puntos
 
483
        glPointSize(WIDGET_SIZE_PUNTO);
 
484
        for (VerticesPoligono::iterator it = m_Vertices.begin(); it != m_Vertices.end(); it++) {
 
485
                glBegin(GL_POINTS);
 
486
                        glVertex2d((*it).x, (*it).y);
 
487
                glEnd();
 
488
        }
 
489
 
 
490
        m_Modificado = false;
 
491
}
 
492
 
 
493
void GNC::GCS::Widgets::WPoligono::Seleccionar(bool seleccionado)
 
494
{
 
495
        if (m_Seleccionado != seleccionado) {
 
496
                m_Seleccionado = seleccionado;
 
497
                m_Modificado = true;
 
498
        }
 
499
}
 
500
 
 
501
void GNC::GCS::Widgets::WPoligono::Iluminar(bool iluminado)
 
502
{
 
503
        if (m_Iluminado != iluminado) {
 
504
                m_Iluminado = iluminado;
 
505
                m_Modificado = true;
 
506
        }
 
507
}
 
508
 
 
509
void GNC::GCS::Widgets::WPoligono::Ocultar(bool oculto){
 
510
        if(oculto != m_Oculto){
 
511
                m_Oculto = oculto;
 
512
                m_Modificado = true;
 
513
        }
 
514
}
 
515
 
 
516
void GNC::GCS::Widgets::WPoligono::Recalcular()
 
517
{
 
518
        m_Centro = GNC::GCS::Vector::Centroide(m_Vertices);
 
519
        m_Perimetro = GNC::GCS::Vector::PerimetroDePoligono(m_Vertices);
 
520
 
 
521
}
 
522
 
 
523
//endregion
 
524
void GNC::GCS::Widgets::WPoligono::InsertarPropiedades(TListaMapasPropiedades &listaMapaPropiedades)
 
525
{
 
526
        TMapaPropiedades& primerMapa = listaMapaPropiedades.front();
 
527
        {
 
528
                std::ostringstream ostr;
 
529
                ostr <<  m_Centro;
 
530
                primerMapa[_Std("Center (I)")] = ostr.str();
 
531
        }
 
532
 
 
533
        {
 
534
                std::stringstream out;
 
535
                out << PuntoToStringMundo(m_Centro);
 
536
                primerMapa[_Std("Center (M)")] = out.str();
 
537
        }
 
538
 
 
539
        {
 
540
                std::ostringstream ostr;
 
541
                ostr <<  m_Vertices.size();
 
542
                primerMapa[_Std("Number of Vertex")] = ostr.str();
 
543
        }
 
544
}
 
545
 
 
546
void GNC::GCS::Widgets::WPoligono::OffscreenRender(GNC::GCS::Contexto3D* c)
 
547
{
 
548
        if(m_Vertices.size() < 2) {
 
549
                return;
 
550
        }
 
551
 
 
552
        //const GNC::GCS::Vector& escala = c->RefRelacionMundoPantallaOffscreen();
 
553
 
 
554
        VerticesPoligono Vertices;
 
555
 
 
556
        for (VerticesPoligono::iterator it = m_Vertices.begin(); it != m_Vertices.end(); it++)
 
557
        {
 
558
                Vertices.push_back( c->Reproyectar(*it) * c->factorReescalado );
 
559
        }
 
560
 
 
561
        cairo_set_line_width(c->cr, WIDGET_GROSOR_LINEA);
 
562
 
 
563
        // Color del cuerpo
 
564
        wColorNormal.AplicarCairo(c->cr);
 
565
 
 
566
        // Dibujado del cuerpo
 
567
        for (VerticesPoligono::iterator it = Vertices.begin(); it != Vertices.end(); it++) {
 
568
                if(it == Vertices.begin()) {
 
569
                        cairo_move_to(c->cr, (*it).x, (*it).y);
 
570
                }
 
571
                cairo_line_to(c->cr, (*it).x, (*it).y);
 
572
        }
 
573
        cairo_line_to(c->cr, Vertices.front().x, Vertices.front().y);
 
574
        cairo_stroke(c->cr);
 
575
}
 
576
 
 
577
float GNC::GCS::Widgets::WPoligono::GetAreaMetrica() const
 
578
{
 
579
        /*
 
580
        {
 
581
                const VerticesPoligono& lista = m_Vertices;
 
582
                bool simple = true;
 
583
                if (lista.size() > 3) {
 
584
 
 
585
                        std::list<GNC::GCS::Vector>::const_iterator i = lista.begin();
 
586
                        std::list<GNC::GCS::Vector>::const_iterator j = i; j++;
 
587
 
 
588
                        std::list<GNC::GCS::Vector>::const_iterator k;
 
589
                        std::list<GNC::GCS::Vector>::const_iterator l;
 
590
 
 
591
                        GNC::GCS::Vector inter;
 
592
                        GNC::GCS::Vector bbox[2][2];
 
593
 
 
594
                        glPointSize(5.0f);
 
595
                        glBegin(GL_POINTS);
 
596
                        glColor3f(1.0f, 0.0f, 0.0f);
 
597
                        glVertex2f( (*i).x, (*i).y );
 
598
 
 
599
                        glColor3f(0.0f, 0.0f, 1.0f);
 
600
                        glVertex2f( (*j).x, (*j).y );
 
601
 
 
602
                        glEnd();
 
603
 
 
604
                        const unsigned long nl = lista.size() - 1;
 
605
                        unsigned long ni = 0;
 
606
                        unsigned long nj = 0;
 
607
 
 
608
                        std::cout << "Poligono: numNodos = " << nl << std::endl;
 
609
 
 
610
                        for (ni = 0; simple && ni < nl; ni++, i++, j++) {
 
611
 
 
612
                                (*i).IniciarBoundingBox(bbox[0][0],bbox[0][1]);
 
613
                                (*j).ExtenderBoundingBox(bbox[0][0],bbox[0][1]);
 
614
 
 
615
                                k = j; k++;
 
616
 
 
617
                                nj = ni + 2;
 
618
 
 
619
                                while (k != lista.end()) {
 
620
                                        l = k;
 
621
                                        l++;
 
622
 
 
623
                                        if (l == lista.end()) {
 
624
                                                l = lista.begin();
 
625
                                                if (l == i) {
 
626
                                                        break;
 
627
                                                }
 
628
                                        }
 
629
 
 
630
                                        (*k).IniciarBoundingBox(bbox[1][0], bbox[1][1]);
 
631
                                        (*l).ExtenderBoundingBox(bbox[1][0], bbox[1][1]);
 
632
 
 
633
                                        GNC::GCS::Vector::IntersectarBoundingBox(bbox[0], bbox[1], bbox[1]);
 
634
 
 
635
                                        if ( GNC::GCS::Vector::BoundingBoxValido(bbox[1]) ) {
 
636
                                                std::cout << "IntersecciĆ³n: [ " << ni << ", " << ni + 1 << " ] x [" << nj << ", ";
 
637
                                                if (nj == nl) {
 
638
                                                        std::cout << "0 ]" << std::endl;
 
639
                                                }
 
640
                                                else {
 
641
                                                        std::cout << nj + 1 << " ]" << std::endl;
 
642
                                                }
 
643
 
 
644
                                                inter = GNC::GCS::Vector::InterseccionEntreRectas(*i, *j, *k, *l);
 
645
 
 
646
                                                //if ( inter.DistanciaARecta(*i, *j) < GNC::GCS::Vector::EpsilonDeComponentes() && inter.DistanciaARecta(*k, *l) < inter.DistanciaARecta(*k, *l) ) {
 
647
                                                if (
 
648
                                                        (inter.x + 1e-13) >= bbox[1][0].x &&
 
649
                                                        (inter.x - 1e-13) <= bbox[1][1].x && 
 
650
                                                        (inter.y + 1e-13) >= bbox[1][0].y &&
 
651
                                                        (inter.y - 1e-13) <= bbox[1][1].y
 
652
                                                ) {
 
653
 
 
654
                                                //if (inter.DentroDeBoundingBoxCerradoConUmbral(bbox[1][0], bbox[1][1]) ) {
 
655
                                                        //simple = false;
 
656
 
 
657
                                                        glColor3f(0.0f, 0.0f, 1.0f);
 
658
                                                        glLineWidth(1.0f);
 
659
                                                        glBegin(GL_LINE_LOOP);
 
660
                                                        glVertex2f(bbox[1][0].x, bbox[1][0].y);
 
661
                                                        glVertex2f(bbox[1][1].x, bbox[1][0].y);
 
662
                                                        glVertex2f(bbox[1][1].x, bbox[1][1].y);
 
663
                                                        glVertex2f(bbox[1][0].x, bbox[1][1].y);
 
664
                                                        glEnd();
 
665
 
 
666
                                                        glColor3f(1.0f, 1.0f, 1.0f);
 
667
                                                        glBegin(GL_LINES);
 
668
                                                        glVertex2f((*i).x, (*i).y);
 
669
                                                        glVertex2f((*j).x, (*j).y);
 
670
                                                        glEnd();
 
671
 
 
672
                                                        glBegin(GL_POINTS);
 
673
                                                        glPointSize(2.0f);
 
674
                                                        glColor3f(1.0f, 0.0f, 1.0f);
 
675
                                                        glVertex2f(inter.x, inter.y);
 
676
                                                        glEnd();
 
677
 
 
678
                                                }
 
679
                                                else {
 
680
                                                        GNC::GCS::Vector diff1 = inter - bbox[1][0];
 
681
                                                        GNC::GCS::Vector diff2 = bbox[1][1] - inter;
 
682
                                                        */
 
683
                                                        /*
 
684
 
 
685
                                                        std::cout << "Diff1,2: " << diff1 << ", " << diff2 << std::endl;
 
686
 
 
687
                                                        double diff3 = inter.DistanciaARecta(*i, *j);
 
688
                                                        double diff4 = inter.DistanciaARecta(*k, *l);
 
689
 
 
690
                                                        std::cout << "Diff2,3" << diff3 << ", " << diff4 << std::endl;
 
691
 
 
692
                                                        GNC::GCS::Vector inter2 = GNC::GCS::Vector::InterseccionEntreRectas(*i, *j, *k, *l);
 
693
 
 
694
                                                        std::cout << inter2 << std::endl;
 
695
                                                        
 
696
                                                        glColor3f(0.0f, 0.0f, 0.5f);
 
697
                                                        glLineWidth(1.0f);
 
698
                                                        glBegin(GL_LINE_LOOP);
 
699
                                                        glVertex2f(bbox[1][0].x, bbox[1][0].y);
 
700
                                                        glVertex2f(bbox[1][1].x, bbox[1][0].y);
 
701
                                                        glVertex2f(bbox[1][1].x, bbox[1][1].y);
 
702
                                                        glVertex2f(bbox[1][0].x, bbox[1][1].y);
 
703
                                                        glEnd();
 
704
 
 
705
                                                        glColor3f(1.0f, 0.0f, 0.0f);
 
706
                                                        glBegin(GL_LINES);
 
707
                                                        glVertex2f((*i).x, (*i).y);
 
708
                                                        glVertex2f((*j).x, (*j).y);
 
709
                                                        glEnd();
 
710
 
 
711
                                                        glBegin(GL_POINTS);
 
712
                                                        glPointSize(2.0f);
 
713
                                                        glColor3f(0.5f, 0.5f, 0.5f);
 
714
                                                        glVertex2f(inter.x, inter.y);
 
715
                                                        glEnd();
 
716
                                                        */
 
717
                                                        /*
 
718
                                                }
 
719
                                        }
 
720
                                        k++;
 
721
                                        nj++;
 
722
                                }
 
723
                                */
 
724
                                /*
 
725
                                for (unsigned long nj = 2; simple && (ni + nj) < lista.size(); nj++, k++, l++) {
 
726
                                        //inter = GNC::GCS::Vector::InterseccionEntreRectas((*i), (*j), (*k), (*l));
 
727
                                        //if ( 1 || inter.DentroDeBoundingBoxCerrado(bbox[0][0], bbox[0][1])) {
 
728
                                        (*k).IniciarBoundingBox(bbox[1][0],bbox[1][1]);
 
729
                                        (*l).ExtenderBoundingBox(bbox[1][0],bbox[1][1]);
 
730
                                        GNC::GCS::Vector::IntersectarBoundingBox(bbox[0], bbox[1], bbox[1]);
 
731
                                        if ( GNC::GCS::Vector::BoundingBoxValido(bbox[1]) && GNC::GCS::Vector::InterseccionEntreRectas(*i, *j, *k, *l).DentroDeBoundingBoxCerrado(bbox[1][0], bbox[1][1]) ) {
 
732
                                                //simple = false;
 
733
                                                GNC::GCS::Vector inter = GNC::GCS::Vector::InterseccionEntreRectas(*i, *j, *k, *l);
 
734
 
 
735
                                                
 
736
 
 
737
                                                glColor3f(0.0f, 0.0f, 1.0f);
 
738
                                                glLineWidth(1.0f);
 
739
                                                glBegin(GL_LINE_LOOP);
 
740
                                                glVertex2f(bbox[1][0].x, bbox[1][0].y);
 
741
                                                glVertex2f(bbox[1][1].x, bbox[1][0].y);
 
742
                                                glVertex2f(bbox[1][1].x, bbox[1][1].y);
 
743
                                                glVertex2f(bbox[1][0].x, bbox[1][1].y);
 
744
                                                glEnd();
 
745
 
 
746
                                                glColor3f(1.0f, 1.0f, 1.0f);
 
747
                                                glBegin(GL_LINES);
 
748
                                                glVertex2f((*i).x, (*i).y);
 
749
                                                glVertex2f((*j).x, (*j).y);
 
750
                                                glEnd();
 
751
                                                glBegin(GL_LINES);
 
752
                                                glVertex2f((*k).x, (*k).y);
 
753
                                                glVertex2f((*l).x, (*l).y);
 
754
                                                glEnd();
 
755
 
 
756
                                                glBegin(GL_POINTS);
 
757
                                                glPointSize(2.0f);
 
758
                                                glColor3f(1.0f, 1.0f, 1.0f);
 
759
                                                glVertex2f(inter.x, inter.y);
 
760
                                                glEnd();
 
761
                                                
 
762
                                        }
 
763
                                }
 
764
 
 
765
                                if ( (++i) == lista.end() ) {
 
766
                                        i = lista.begin();
 
767
                                        glPointSize(2.0f);
 
768
                                        glBegin(GL_POINTS);
 
769
                                        glColor3f(1.0f, 0.0f, 1.0f);
 
770
                                        glVertex2f( (*i).x, (*i).y );
 
771
                                        glEnd();
 
772
                                }
 
773
                                j++;
 
774
                                */
 
775
                                /*
 
776
                        }
 
777
                }
 
778
        }
 
779
        */
 
780
 
 
781
        if (GNC::GCS::Vector::EsPoligonoSimple(m_Vertices)) {
 
782
                return GNC::GCS::Vector::AreaDePoligono(m_Vertices);
 
783
        }
 
784
        else {
 
785
                return std::numeric_limits<float>::quiet_NaN();
 
786
        }
 
787
}
 
788