source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/principal.cpp @ 774

Revision 774, 33.9 KB checked in by gumbau, 18 years ago (diff)

GTGeometry and GeoTool? initial imports

Line 
1//Headers:
2//#include <Ogre.h>
3#include <iostream>
4#include <fstream>
5
6//#include <stdio.h>
7#include <string.h>
8#include <math.h>
9#include "principal.h"
10#include <ctime>
11
12time_t first, second;
13
14using namespace std;
15using namespace Geometry;
16//using namespace principal;
17//--------------------------------------------------------------------------------------------------------------------------------
18//  RELLENA LA ESTRUCTURA DE VERTICES
19//--------------------------------------------------------------------------------------------------------------------------------
20
21void Mesh2Estructura(Geometry::Mesh *mesh){
22        // Calcular el número de vértices
23        long int countv=0;
24        long int counth=0;
25        long int pos=0;
26        long int v1, v2, v3;
27
28        for (unsigned int i=0; i<mesh->mSubMeshCount; i++){
29                countv+=mesh->mSubMesh[i].mVertexBuffer->mVertexCount;
30        }
31        Vertex  = new float[countv][3];
32       
33        for (unsigned int i=0; i<mesh->mSubMeshCount; i++){
34                for (unsigned int j=0; j<mesh->mSubMesh[i].mVertexBuffer->mVertexCount; j++) {
35                        Vertex[pos][0] = mesh->mSubMesh[i].mVertexBuffer->mPosition[j].x;
36                        Vertex[pos][1] = mesh->mSubMesh[i].mVertexBuffer->mPosition[j].y;
37                        Vertex[pos][2] = mesh->mSubMesh[i].mVertexBuffer->mPosition[j].z;
38                        pos++;
39                }
40        }
41
42        // Calcular el número de hojas
43        for (unsigned int i=0; i<mesh->mSubMeshCount; i++){
44                counth+=mesh->mSubMesh[i].mIndexCount;
45        }
46        counth=counth/3; // Se supone que cada 3 índices forman una hoja.
47        if ( counth > 0){
48                Hojasa  = new Hoja[counth];
49        }
50        activas = counth;
51
52        // Insertar las hojas en la estructura
53        pos=0;
54        for (unsigned int i=0; i<mesh->mSubMeshCount; i++){
55                for (unsigned int j=0; j<mesh->mSubMesh[i].mIndexCount/3; j++) {
56                        v1=mesh->mSubMesh[i].mIndex[j];
57                        v2=mesh->mSubMesh[i].mIndex[j];
58                        v3=mesh->mSubMesh[i].mIndex[j];
59                        Hojasa[pos].Vertices[0]=(v1-1);
60                        Hojasa[pos].Vertices[1]=(v2-1);
61                        Hojasa[pos].Vertices[2]=(v3-1);
62                        Hojasa[pos].Vertices[3]=(v3-1);
63                        Centroh( Hojasa[pos]);
64                        GetNormal ( Hojasa[pos]);
65                        pos++;
66                }
67        }
68}
69
70//long int LeeVertices (char * arbol)
71//
72//{
73//              FILE* fp_origen;
74//              char linea[256], str[20];
75//              float x,y,z;
76//              long int countv=0;
77//              bool fin_vertices = false;
78//             
79//              //cuento todos los vertices, y relleno la estructura
80//              if ((fp_origen = fopen (arbol, "r")) == NULL)
81//                      printf ("No he podido abrir el fichero %s\n", arbol);
82//              else
83//              {
84//                      while ((fgets (linea, 255, fp_origen) != NULL) && (fin_vertices == false))
85//             
86//                             
87//                              switch (linea[0])
88//                              {
89//                                      case '#':
90//                                              switch ( linea[2])
91//                                              {
92//                                              case 'v':
93//                                                      sscanf(linea,"# %s %lu",  str, &countv);
94//                                                      if ( countv > 0)        Vertex  = new float[2*countv][3];
95//                                                      printf ( "hay %lu vertices\n",countv);
96//                                                      countv=0;
97//                                                      break;
98//                                             
99//                                              default:
100//                                                      break;
101//
102//                                              }
103//                                              break;
104//                                      case 'v':
105//                                              switch (linea[1])
106//                                              {
107//                                                      case ' ':
108//                                                      sscanf (linea, "v %f %f %f ", &x, &y, &z);
109//                                                      Vertex[countv][0] = x; Vertex[countv][1] = y; Vertex[countv][2] = z;
110//                                                              countv++;
111//                                                              break;
112//             
113//                                                      default :
114//                                                              break;
115//
116//                                              }
117//                                              break;
118//
119//
120//                                      default:
121//                                              break;
122//     
123//                              }
124//
125//              fclose (fp_origen);
126//              }
127//              return ( countv);
128//
129//             
130//}
131//--------------------------------------------------------------------------------------------------------------------------------
132// CUENTA LAS HOJAS QUE HAY EN EL FICHERO
133//--------------------------------------------------------------------------------------------------------------------------------
134
135//long int CuentaHojas ( char *hojas)
136//{
137//
138//      FILE* fp_hojas;
139//      char linea[256];
140//      long int counth=0;
141//
142//      if ((fp_hojas = fopen (hojas, "r")) == NULL)
143//                      printf ("No he podido abrir el fichero %s\n", hojas);
144//      else
145//              {
146//
147//              while (fgets (linea, 255, fp_hojas) != NULL)
148//     
149//                      switch (linea[0])
150//                      {
151//                              case 'H':
152//                                      counth++;
153//                                      break;
154//             
155//                              default :
156//                                      break;
157//                      }
158//
159//
160//
161//                      fclose (fp_hojas);
162//
163//                      if ( counth > 0){
164//                              Hojasa  = new Hoja[2*counth];
165//                              }
166//      }
167//              activas = counth;
168//              return (counth);
169//}
170//--------------------------------------------------------------------------------------------------------------------------------
171// LEO LAS HOJAS Y RELLENO LA ESTRUCTURA DE DATOS
172//--------------------------------------------------------------------------------------------------------------------------------
173
174//void LeoHojas (char* hojas)
175//{
176//      FILE* fp_hojas;
177//      char linea[256];
178//      long int v1, v2, v3, h=0;
179//             
180//      if ((fp_hojas = fopen (hojas, "r")) == NULL)
181//                      printf ("No he podido abrir el fichero %s\n", hojas);
182//             
183//      else
184//      {
185//
186//
187//              while (fgets (linea, 255, fp_hojas) != NULL)
188//             
189//                      switch (linea[0])
190//                      {
191//                              case 'H':
192//                             
193//                                      fgets (linea, 255, fp_hojas);
194//                                      sscanf(linea, "f %lu %lu %lu", &v1, &v2, &v3);
195//
196//                                      Hojasa[h].Vertices[0]=(v1-1);
197//                                      Hojasa[h].Vertices[1]=(v2-1);
198//                                      Hojasa[h].Vertices[2]=(v3-1);
199//                                     
200//                                      fgets (linea, 255, fp_hojas);
201//                                      sscanf(linea, "f %lu %lu %lu", &v1, &v2, &v3);
202//                                      Hojasa[h].Vertices[3]=(v3-1);
203//
204//                                      Centroh( Hojasa[h]);
205//                                      GetNormal ( Hojasa[h]);
206//                                     
207//                                      h++;
208//                                      break;
209//                                     
210//                              default :
211//                                      printf( "Hay un error al leer las hojas\n");
212//                                      break;
213//     
214//                      }
215//             
216//                      fclose(fp_hojas);
217//      }
218//
219//             
220//}
221
222//--------------------------------------------------------------------------------------------------------------------------------
223//  FUNCIONES AUXILIARES
224//--------------------------------------------------------------------------------------------------------------------------------
225
226float max ( float a, float b)
227{
228        if (a>b) return (a);
229        else return(b);
230}
231
232float min ( float a, float b)
233{
234        if (a>b) return (b);
235        else return(a);
236}
237
238float distan (float x1, float y1, float z1, float x2, float y2, float z2)
239{
240        float dist = 0;
241
242        dist = ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) + ((z2-z1)*(z2-z1));
243
244        return ( dist);
245}
246
247//--------------------------------------------------------------------------------------------------------------------------------
248//  CALCULA EL CENTRO DE UNA HOJA
249//--------------------------------------------------------------------------------------------------------------------------------
250
251
252void Centroh ( Hoja& Hoja1)
253{
254
255        float max_x, min_x, max_y, min_y, max_z, min_z;
256
257       
258        //x1
259        max_x = max ( max (Vertex[Hoja1.Vertices[0]][0], Vertex[Hoja1.Vertices[1]][0]),
260                                  max (Vertex[Hoja1.Vertices[2]][0], Vertex[Hoja1.Vertices[3]][0]));
261       
262        min_x = min ( min (Vertex[Hoja1.Vertices[0]][0], Vertex[Hoja1.Vertices[1]][0]),
263                                  min (Vertex[Hoja1.Vertices[2]][0], Vertex[Hoja1.Vertices[3]][0]));
264
265        Hoja1.Centro[0] = (max_x + min_x)/2;
266
267        //y1
268        max_y = max ( max (Vertex[Hoja1.Vertices[0]][1], Vertex[Hoja1.Vertices[1]][1]),
269                                  max (Vertex[Hoja1.Vertices[2]][1], Vertex[Hoja1.Vertices[3]][1]));
270       
271        min_y = min ( min (Vertex[Hoja1.Vertices[0]][1], Vertex[Hoja1.Vertices[1]][1]),
272                                  min (Vertex[Hoja1.Vertices[2]][1], Vertex[Hoja1.Vertices[3]][1]));
273
274        Hoja1.Centro[1] = (max_y + min_y)/2;
275       
276        //z1
277        max_z = max ( max (Vertex[Hoja1.Vertices[0]][2], Vertex[Hoja1.Vertices[1]][2]),
278                                  max (Vertex[Hoja1.Vertices[2]][2], Vertex[Hoja1.Vertices[3]][2]));
279       
280        min_z = min ( min (Vertex[Hoja1.Vertices[0]][2], Vertex[Hoja1.Vertices[1]][2]),
281                                  min (Vertex[Hoja1.Vertices[2]][2], Vertex[Hoja1.Vertices[3]][2]));
282
283        Hoja1.Centro[2] = (max_z + min_z)/2;
284
285
286}
287
288//--------------------------------------------------------------------------------------------------------------------------------
289//  CALCULA LA NORMAL DE UNA HOJA
290//--------------------------------------------------------------------------------------------------------------------------------
291void GetNormal (Hoja &aHoja)
292{
293
294        float onex, oney, onez;
295        float twox, twoy, twoz;
296        float threex, threey, threez;
297
298        onex=Vertex[aHoja.Vertices[0]][0]; oney= Vertex[aHoja.Vertices[0]][1]; onez = Vertex[aHoja.Vertices[0]][2];
299        twox = Vertex[aHoja.Vertices[1]][0]; twoy = Vertex[aHoja.Vertices[1]][1]; twoz = Vertex[aHoja.Vertices[1]][2];
300        threex = Vertex[aHoja.Vertices[2]][0]; threey = Vertex[aHoja.Vertices[2]][1]; threez = Vertex[aHoja.Vertices[2]][2];
301       
302        aHoja.Normal[0] = ((twoz-onez)*(threey-oney)) - ((twoy-oney)*(threez-onez));
303        aHoja.Normal[1] = ((twox-onex)*(threez-onez)) - ((threex-onex)*(twoz-onez));
304        aHoja.Normal[2] = ((threex-onex)*(twoy-oney)) - ((twox-onex)*(threey-oney));
305
306       
307}
308//--------------------------------------------------------------------------------------------------------------------------------
309//  CALCULA LA distancia de Hausdorff ( distancia entre nubes de puntos)
310//--------------------------------------------------------------------------------------------------------------------------------
311float Hausdorff (Hoja &aHoja, Hoja& h2)
312{
313
314        float onex, oney, onez;
315        float twox, twoy, twoz;
316        float threex, threey, threez;
317        float fourx, foury, fourz;
318        float x1, y1, z1;
319        float x2, y2, z2;
320        float x3, y3, z3;
321        float x4, y4, z4;
322
323        float dist1, dist2, dist3, dist4, distmp, dista, distb, dist;
324
325        onex=Vertex[aHoja.Vertices[0]][0]; oney= Vertex[aHoja.Vertices[0]][1]; onez = Vertex[aHoja.Vertices[0]][2];
326        twox = Vertex[aHoja.Vertices[1]][0]; twoy = Vertex[aHoja.Vertices[1]][1]; twoz = Vertex[aHoja.Vertices[1]][2];
327        threex = Vertex[aHoja.Vertices[2]][0]; threey = Vertex[aHoja.Vertices[2]][1]; threez = Vertex[aHoja.Vertices[2]][2];
328        fourx = Vertex[aHoja.Vertices[3]][0]; foury = Vertex[aHoja.Vertices[3]][1]; fourz = Vertex[aHoja.Vertices[3]][2];
329       
330
331        x1 = Vertex[h2.Vertices[0]][0]; y1 = Vertex[h2.Vertices[0]][1]; z1 = Vertex[h2.Vertices[0]][2];
332        x2 = Vertex[h2.Vertices[1]][0]; y2 = Vertex[h2.Vertices[1]][1]; z2 = Vertex[h2.Vertices[1]][2];
333        x3 = Vertex[h2.Vertices[2]][0]; y3 = Vertex[h2.Vertices[2]][1]; z3 = Vertex[h2.Vertices[2]][2];
334        x4 = Vertex[h2.Vertices[3]][0]; y4 = Vertex[h2.Vertices[3]][1]; z4 = Vertex[h2.Vertices[3]][2];
335
336        // guardo las distancias mínimas de cada vértice a los 4 contrarios.
337        dist1 = distan ( onex, oney,onez,x1,y1,z1);
338        // vertice 1 con los 4 de la otra hoja
339
340        distmp = distan ( onex, oney,onez,x2,y2,z2);
341        if ( distmp < dist1) dist1 = distmp;
342
343        distmp = distan ( onex, oney,onez,x3,y3,z3);
344        if ( distmp < dist1) dist1 = distmp;
345
346        distmp = distan ( onex, oney,onez,x4,y4,z4);
347        if ( distmp < dist1) dist1 = distmp;
348
349        // vertice 2 con los 4 de la otra hoja
350
351        dist2 = distan ( twox, twoy,twoz,x1,y1,z1);
352       
353        distmp = distan ( twox, twoy,twoz,x2,y2,z2);
354        if ( distmp < dist2) dist2 = distmp;
355       
356        distmp = distan ( twox, twoy,twoz,x3,y3,z3);
357        if ( distmp < dist2) dist2 = distmp;
358       
359        distmp = distan ( twox, twoy,twoz,x4,y4,z4);
360        if ( distmp < dist2) dist2 = distmp;
361
362        // vertice 3 con los 4 de la otra hoja
363
364        dist3 = distan ( threex, threey,threez,x1,y1,z1);
365       
366        distmp = distan ( threex, threey,threez,x2,y2,z2);
367        if ( distmp < dist3) dist3 = distmp;
368
369        distmp = distan ( threex, threey,threez,x3,y3,z3);
370        if ( distmp < dist3) dist3 = distmp;
371
372        distmp = distan ( threex, threey,threez,x4,y4,z4);
373        if ( distmp < dist3) dist3 = distmp;
374
375
376        // vertice 4 con los 4 de la otra hoja
377
378        dist4 = distan ( fourx, foury,fourz,x1,y1,z1);
379       
380        distmp = distan ( fourx, foury,fourz,x2,y2,z2);
381        if ( distmp < dist4) dist4 = distmp;
382
383        distmp = distan ( fourx, foury,fourz,x3,y3,z3);
384        if ( distmp < dist4) dist4 = distmp;
385       
386        distmp = distan ( fourx, foury,fourz,x4,y4,z4);
387        if ( distmp < dist4) dist4 = distmp;
388
389        //de entre estos cojo el máximo
390
391        dista =  max(dist1, max(dist2, max(dist3, dist4)));
392
393        //LO MISMO PERO A LA INVERSA
394
395        dist1 = distan ( x1,y1,z1, onex, oney, onez);
396        // vertice 1 con los 4 de la otra hoja
397
398        distmp = distan ( x1,y1,z1, twox, twoy, twoz);
399        if ( distmp < dist1) dist1 = distmp;
400
401        distmp = distan ( x1,y1,z1, threex, threey, threez);
402        if ( distmp < dist1) dist1 = distmp;
403
404        distmp = distan ( x1,y1,z1, fourx, foury, fourz);
405        if ( distmp < dist1) dist1 = distmp;
406
407        //2
408        dist2 = distan ( x2,y2,z2, onex, oney, onez);
409        // vertice 2 con los 4 de la otra hoja
410
411        distmp = distan ( x2,y2,z2, twox, twoy, twoz);
412        if ( distmp < dist2) dist2 = distmp;
413
414        distmp = distan ( x2,y2,z2, threex, threey, threez);
415        if ( distmp < dist2) dist2 = distmp;
416
417        distmp = distan ( x2,y2,z2, fourx, foury, fourz);
418        if ( distmp < dist2) dist2 = distmp;
419
420
421                //3
422        dist3 = distan ( x3,y3,z3, onex, oney, onez);
423        // vertice 3 con los 4 de la otra hoja
424
425        distmp = distan ( x3,y3,z3, twox, twoy, twoz);
426        if ( distmp < dist3) dist3 = distmp;
427
428        distmp = distan ( x3,y3,z3, threex, threey, threez);
429        if ( distmp < dist3) dist3 = distmp;
430
431        distmp = distan ( x3,y3,z3, fourx, foury, fourz);
432        if ( distmp < dist3) dist3 = distmp;
433
434                //4
435        dist4 = distan ( x4,y4,z4, onex, oney, onez);
436        // vertice 4 con los 4 de la otra hoja
437
438        distmp = distan ( x4,y4,z4, twox, twoy, twoz);
439        if ( distmp < dist4) dist4 = distmp;
440
441        distmp = distan ( x4,y4,z4, threex, threey, threez);
442        if ( distmp < dist4) dist4 = distmp;
443
444        distmp = distan ( x4,y4,z4, fourx, foury, fourz);
445        if ( distmp < dist4) dist4 = distmp;
446
447        //
448        distb =  max(dist1, max(dist2, max(dist3, dist4)));
449
450        dist  = max ( dista, distb);
451        return ( dist);
452       
453}
454
455//--------------------------------------------------------------------------------------------------------------------------------
456// CALCULA LA DISTANCIA ENTRE HOJAS
457//--------------------------------------------------------------------------------------------------------------------------------
458
459void DistanciaEntreHojas(void)
460{
461        float dist, distmp ;
462        int j;
463
464        for ( int i=0;i<counth;i++)
465        {
466                if ( Hojasa[i].existe == true) // si la hoja aun existe
467                {
468                        // inicializo para poder comparar
469                        if ( i == (counth-1)) j = 0;
470                                else j = i+1;
471                        while (Hojasa[j].existe == false) j++;
472                        //dist = Hojasa[i].Distancia(Hojasa[j]);
473                        dist = Hausdorff( Hojasa[i], Hojasa[j]);
474
475                        Hojasa[i].hoja_cerca = j;
476                        // empiezo los calculos
477                        for ( j =0; j<(counth-1);j++)
478                        {
479                                if ( j == i)
480                                        break;
481                                if ( Hojasa[j].existe == true) // si la hoja aun existe
482                                {
483                                        distmp = Hausdorff(Hojasa[i], Hojasa[j]);
484                                        if ( distmp < dist )
485                                        {
486                                                dist = distmp;
487                                                Hojasa[i].hoja_cerca = j;
488                                        }
489                                }
490                               
491                        }
492                        Hojasa[i].dist = dist;
493                }
494        }
495
496}
497
498//--------------------------------------------------------------------------------------------------------------------------------
499// DEVUELVE LA HOJA QUE TIENE UNA DISTANCIA MENOR
500//--------------------------------------------------------------------------------------------------------------------------------
501long int MinimaDistancia ( void)
502{
503        float mindist =0;
504        long int cual=-1;
505        int i=0;
506       
507        //inicializo
508        while (Hojasa[i].existe != true) i++;
509        mindist = Hojasa[i].dist;
510        cual =i;
511        //busco la minima
512        for ( i=0;i<counth;i++)
513        {
514                if (Hojasa[i].existe == true)
515                {
516                        if ( mindist> Hojasa[i].dist)
517                        {
518                                mindist = Hojasa[i].dist;
519                                cual = i;
520                        }
521                }
522               
523        }
524        return (cual);
525}
526
527
528//--------------------------------------------------------------------------------------------------------------------------------
529// CALCULA LA COPLANARIDAD ENTRE HOJAS
530//--------------------------------------------------------------------------------------------------------------------------------
531
532void CoplanarEntreHojas(FILE* fp_coplanar)
533{
534        float cop, coptmp ;
535        int j, i;
536//      char linea[250];
537
538        for ( i=0;i<counth;i++)
539        {
540                if ( Hojasa[i].existe == true) // si la hoja aun existe
541                {
542                        // inicializo para poder comparar
543                        if ( i == (counth-1)) j = 0;
544                                else j = i+1;
545                        while (Hojasa[j].existe == false) j++;
546                        cop = Hojasa[i].Coplanaridad(Hojasa[j]);
547
548               
549
550                        Hojasa[i].hoja_cop = j;
551                        // empiezo los calculos
552                        for ( j =0; j<(counth-1);j++)
553                        {
554                                if (( j != i) && ( Hojasa[j].existe == true)) // si la hoja aun existe
555                                {
556                                        coptmp = Hojasa[i].Coplanaridad(Hojasa[j]);
557                                        /*printf ("Hoja %i coplanaridad con hoja %i es %f\n", i, j,cop);
558                                        sprintf (linea,"%i %f\n",j, coptmp);
559                                        fputs (linea, fp_coplanar);*/
560
561                                        if ( coptmp > cop )  // COJO EL MAS COPLANAR , CERCANO A 1
562                                        {
563                                                cop = coptmp;
564                                                Hojasa[i].hoja_cop = j;
565                                        }
566                                }
567                               
568                        }
569                        Hojasa[i].coplanar = 1 - cop; //  8/6/01 LO INVIERTO
570                }
571        }
572       
573
574}
575//--------------------------------------------------------------------------------------------------------------------------------
576// DEVUELVE LA HOJA QUE TIENE MÁS COPLANARIDAD// ya no vale. Ahora la buena será la minima 8/6/01
577//--------------------------------------------------------------------------------------------------------------------------------
578/*long int MaximaCoplanar ( void)
579{
580        float maxcop =0;
581        long int cual=-1;
582        int i=0;
583       
584        //inicializo
585        while (Hojasa[i].existe != true) i++;
586        maxcop = Hojasa[i].coplanar;
587        cual =i;
588        //busco la minima
589        for ( i=0;i<counth;i++)
590        {
591                if (Hojasa[i].existe == true)
592                {
593                        if ( maxcop < Hojasa[i].coplanar)
594                        {
595                                maxcop = Hojasa[i].coplanar;
596                                cual = i;
597                        }
598                }
599               
600        }
601        return (cual);
602}
603
604*/
605//--------------------------------------------------------------------------------------------------------------------------------
606//
607//--------------------------------------------------------------------------------------------------------------------------------
608void DosMayores(float *mayores, int* indices)
609{
610       
611        float m1;
612        int i;
613
614        if  ( mayores[0] < mayores[1])
615        {
616                m1= mayores[0];
617                mayores[0] = mayores[1];
618                mayores[1] = m1;
619
620                i = indices[0];
621                indices[0] = indices[1];
622                indices[1] = i;
623               
624        }
625
626        if ( mayores [2] < mayores [3])
627        {
628                m1 = mayores[2];
629                mayores[2] = mayores[3];
630                mayores [3] = m1;
631
632                i = indices[2];
633                indices[2] = indices[3];
634                indices[3] = i;
635
636        }
637
638        if  ( mayores[0] < mayores[2])
639        {
640                m1= mayores[0];
641                mayores[0] = mayores[2];
642                mayores[2] = m1;
643
644                i = indices[0];
645                indices[0] = indices[2];
646                indices[2] = i;
647
648        }
649
650
651        if ( mayores [2] < mayores [3])
652        {
653                m1 = mayores[2];
654                mayores[2] = mayores[3];
655                mayores [3] = m1;
656
657                i = indices[2];
658                indices[2] = indices[3];
659                indices[3] = i;
660
661        }
662
663        if ( mayores [1] < mayores [2])
664        {
665                m1 = mayores[1];
666                mayores[1] = mayores[2];
667                mayores [2] = m1;
668
669                i = indices[1];
670                indices[1] = indices[2];
671                indices[2] = i;
672
673        }
674
675                       
676}
677//--------------------------------------------------------------------------------------------------------------------------------
678//
679//--------------------------------------------------------------------------------------------------------------------------------
680float DiametroEsferaEnvolvente (FILE* fp_simplifica)
681{
682        float xmax, xmin, ymax, ymin, zmax, zmin;
683        float diametro, cx, cy, cz;
684        int j;
685        char linea[250];
686
687        //inicializo al primero de los vértices de las hojas
688
689        xmax = Vertex[Hojasa[0].Vertices[0]][0];
690        xmin = xmax;
691
692        ymax = Vertex[Hojasa[0].Vertices[0]][1];
693        ymin = ymax;
694
695        zmax = Vertex[Hojasa[0].Vertices[0]][2];
696        zmin = zmax;
697
698        // ahora busco los maximos y los minimos correspondientes
699
700
701        for ( int i=1;i<counth;i++)
702        {
703                for ( j=0;j<4;j++)
704                {
705                       
706                if ( xmax < Vertex[Hojasa[i].Vertices[j]][0]) xmax = Vertex[Hojasa[i].Vertices[j]][0];
707                if ( xmin > Vertex[Hojasa[i].Vertices[j]][0]) xmin = Vertex[Hojasa[i].Vertices[j]][0];
708
709                if ( ymax < Vertex[Hojasa[i].Vertices[j]][1]) ymax = Vertex[Hojasa[i].Vertices[j]][1];
710                if ( ymin > Vertex[Hojasa[i].Vertices[j]][1]) ymin = Vertex[Hojasa[i].Vertices[j]][1];
711               
712                if ( zmax < Vertex[Hojasa[i].Vertices[j]][2]) zmax = Vertex[Hojasa[i].Vertices[j]][2];
713                if ( zmin > Vertex[Hojasa[i].Vertices[j]][2]) zmin = Vertex[Hojasa[i].Vertices[j]][2];
714
715                }
716        }
717
718        cx = (xmax + xmin)/2;
719        cy = (ymax + ymin)/2;
720        cz = (zmax + zmin)/2;
721
722
723        diametro = ((xmax-xmin)*(xmax-xmin)) + ((ymax-ymin)*(ymax-ymin)) + ((zmax-zmin)*(zmax-zmin));
724        //en la pantalla
725        printf ("Centro %f %f %f\n", cx,cy,cz );
726        //en el fichero
727        sprintf (linea,"Centro %f %f %f \n",cx, cy, cz);
728        fputs (linea, fp_simplifica);
729
730        // x
731        sprintf (linea,"x %f %f\n",xmax, xmin);
732        printf (linea,"x %f %f\n",xmax, xmin);
733        fputs (linea, fp_simplifica);
734        // y
735        sprintf (linea,"y %f %f\n",ymax, ymin);
736        printf (linea,"y %f %f\n",ymax, ymin);
737        fputs (linea, fp_simplifica);
738        // z
739        sprintf (linea,"z %f %f\n",zmax, zmin);
740        printf (linea,"z %f %f\n",zmax, zmin);
741        fputs (linea, fp_simplifica);
742
743
744return (diametro);
745}
746
747//--------------------------------------------------------------------------------------------------------------------------------
748//  normaliza las distancia
749//--------------------------------------------------------------------------------------------------------------------------------
750void NormalizaDistancia (float diametro)
751{
752        float dtmp;
753        for (int i=0; i<counth;i++)
754        {
755                dtmp = Hojasa[i].dist;
756                Hojasa[i].dist = dtmp / diametro;
757               
758        }
759}
760
761//--------------------------------------------------------------------------------------------------------------------------------
762//  establece el criterio como (K1 * dist + K2 * cop)/ K1+K2
763//--------------------------------------------------------------------------------------------------------------------------------
764void EstableceCriterio ( float diametro)
765{
766        //Para empezar establezco K1 y K2 con 0,5
767        float coptmp2, coptmp, distmp2, distmp, criteriotmp;
768        int i, j, nhojasi, nhojasj;
769
770
771        for ( i=0; i<counth;i++)
772
773        {
774                if (Hojasa[i].existe == true)
775                {
776                        //incializo criterio a un numero elevado
777                        Hojasa[i].criterio = 1000;
778                        nhojasi = Hojasa[i].Cuantas_hojas;
779                        //coplanaridad
780                        for ( j =0; j<counth;j++)
781                        {
782                                if (( j != i) && ( Hojasa[j].existe == true)) // si la hoja aun existe
783                                {
784                                        //17/09/01 ANTES DE CALCULAR NADA, COMPRUEBO QUE ESTAS DOS HOJAS
785                                        // SE PODRIAN COLAPSAR, ED, QUE LA DIFERENCIA DE HOJAS QUE COLAPSAN
786                                        // ES COMO MÁXIMO 1
787
788                                        nhojasj = Hojasa[j].Cuantas_hojas;
789
790                                        if ( abs((nhojasi - nhojasj)) < 2)
791                                        {
792                                        //coplanaridad y lo invierto
793                                        coptmp2 = Hojasa[i].Coplanaridad(Hojasa[j]);
794                                        coptmp = 1 - coptmp2;
795                                        //distancia y la normalizo
796                                        distmp2 = Hausdorff( Hojasa[i], Hojasa[j]);
797                                        distmp = distmp2 / diametro;
798                                        // calculo el criterio para esa hoja
799                                        criteriotmp = (( K1 * distmp * distmp ) + (K2 * coptmp * distmp))/ (K1 + K2);
800                                        //selecciono el criterio menor
801                                        if (Hojasa[i].criterio > criteriotmp)
802                                        {Hojasa[i].criterio = criteriotmp;
803                                        Hojasa[i].hoja_crit = j;}
804
805                                        }
806                                }
807                               
808                        }
809                //      printf ( "Criterio establecido para hoja %i \n", i);
810                }
811        }
812}
813//--------------------------------------------------------------------------------------------------------------------------------
814//  establece el criterio despues de colapsar
815//--------------------------------------------------------------------------------------------------------------------------------
816void EstableceCriterio2 ( float diametro, long int hojanueva)
817{
818        //Para empezar establezco K1 y K2 con 0,5
819        float  coptmp2, coptmp, distmp2, distmp, criteriotmp;
820        int i, j, nhojasi, nhojasj;
821
822        //ESTAN EN tres PROCEDIMIENTOS: COLAPSA, ESTABLECECRITERIO Y ESTABLECECRITERIO2
823       
824
825        for ( i=0; i<counth;i++)
826        {
827                if ((Hojasa[i].existe == true) && (i != hojanueva))
828                {       
829                        nhojasi = Hojasa[i].Cuantas_hojas;
830                        //¿ SE HA DESACTIVADO LA HOJA_CRIT QUE GUARDABA LA HOJA?
831                        if ( Hojasa[Hojasa[i].hoja_crit].existe == false)
832                        {
833                                Hojasa[i].criterio = 1000;
834
835                                //coplanaridad
836                                for ( j =0; j<counth;j++)
837                                {
838                                        if (( j != i) && ( Hojasa[j].existe == true)) // si la hoja aun existe
839                                        {
840
841                               
842                                        //17/09/01 ANTES DE CALCULAR NADA, COMPRUEBO QUE ESTAS DOS HOJAS
843                                        // SE PODRIAN COLAPSAR, ED, QUE LA DIFERENCIA DE HOJAS QUE COLAPSAN
844                                        // ES COMO MÁXIMO 1
845
846                                        nhojasj = Hojasa[j].Cuantas_hojas;
847
848                                        if ( abs((nhojasi - nhojasj)) < 2)
849                                        {
850
851                                                //coplanaridad y lo invierto
852                                                coptmp2 = Hojasa[i].Coplanaridad(Hojasa[j]);
853                                                coptmp = 1 - coptmp2;   
854                                                //distancia y la normalizo     
855                                        //      distmp2 = Hojasa[i].Distancia(Hojasa[j]);
856                                                distmp2 = Hausdorff( Hojasa[i],  Hojasa[j]);
857                                                distmp = distmp2 / diametro;
858                                                // calculo el criterio para esa hoja
859                                                criteriotmp = (( K1 * distmp * distmp ) + (K2 * coptmp * distmp))/ (K1 + K2);
860                                                //selecciono el criterio menor
861                                                if (Hojasa[i].criterio > criteriotmp)
862                                                        {Hojasa[i].criterio = criteriotmp;
863                                                        Hojasa[i].hoja_crit = j;}
864                                        }
865                                        }
866                                }
867                        }
868                        else
869                        { // CALCULARE SI EL CRITERIO CON ESTA HOJA ES MENOR QUE EL ANTERIOR
870
871                                        nhojasj = Hojasa[hojanueva].Cuantas_hojas;//17/09/01
872
873                                                if ( abs((nhojasi - nhojasj)) < 2)
874                                                {
875
876
877                                                //coplanaridad y lo invierto
878                                                coptmp2 = Hojasa[i].Coplanaridad(Hojasa[hojanueva]);
879                                                coptmp = 1 - coptmp2;
880                                                //distancia y la normalizo
881                                                //distmp2 = Hojasa[i].Distancia(Hojasa[hojanueva]);
882                                                distmp2 = Hausdorff( Hojasa[i], Hojasa[hojanueva]);
883                                                distmp = distmp2 / diametro;
884                                                // calculo el criterio para esa hoja
885                                                criteriotmp = (( K1 * distmp * distmp ) + (K2 * coptmp * distmp))/ (K1 + K2);
886                                                //selecciono el criterio menor
887                                                if (Hojasa[i].criterio > criteriotmp)
888                                                        {Hojasa[i].criterio = criteriotmp;
889                                                        Hojasa[i].hoja_crit = hojanueva;
890                                                        //printf ( "Cambio el criterio  para hoja %i \n", i);
891                                                        }
892                                        }
893                                        }
894                        }
895                                                                       
896                       
897        }
898}
899//--------------------------------------------------------------------------------------------------------------------------------
900// DEVUELVE LA HOJA QUE TIENE EL NUMERO EN EL CAMPO CRITERIO MENOR
901//--------------------------------------------------------------------------------------------------------------------------------
902
903long int MinimoCriterio ( void)
904{
905        float mincrit =0;
906        long int cual=-1;
907        int i=0;
908       
909        //inicializo
910        while (Hojasa[i].existe != true) i++;
911        mincrit = Hojasa[i].criterio;
912        cual =i;
913        //busco la minima
914        for ( i=0;i<counth;i++)
915        {
916                if (Hojasa[i].existe == true)
917                {
918                        if ( mincrit> Hojasa[i].criterio)
919                        {
920                                mincrit = Hojasa[i].criterio;
921                                cual = i;
922                        }
923                }
924               
925        }
926        return (cual);
927}
928
929
930//--------------------------------------------------------------------------------------------------------------------------------
931//
932//--------------------------------------------------------------------------------------------------------------------------------
933void ElijeVertices(Hoja& Hoja1, Hoja& Hoja2, long int count)
934{
935        // criterio es el de los dos vertices más alejados del centro de la otra hoja
936       
937        float a,b,c;
938        float dist[4];
939        int indices[4];
940       
941        // primero de la primera hoja cojo los dos primeros vertices
942
943
944        a = Vertex[Hoja1.Vertices[0]][0];
945        b = Vertex[Hoja1.Vertices[0]][1];
946        c = Vertex[Hoja1.Vertices[0]][2];
947
948        dist[0] = ((Hoja2.Centro[0]-a)*(Hoja2.Centro[0]-a)) + ((Hoja2.Centro[1]-b)*(Hoja2.Centro[1]-b)) +
949                   ((Hoja2.Centro[2]-c)*(Hoja2.Centro[2]-c));
950
951       
952        a = Vertex[Hoja1.Vertices[1]][0];
953        b = Vertex[Hoja1.Vertices[1]][1];
954        c = Vertex[Hoja1.Vertices[1]][2];
955
956        dist[1] = ((Hoja2.Centro[0]-a)*(Hoja2.Centro[0]-a)) + ((Hoja2.Centro[1]-b)*(Hoja2.Centro[1]-b)) +
957                   ((Hoja2.Centro[2]-c)*(Hoja2.Centro[2]-c));
958
959        a = Vertex[Hoja1.Vertices[2]][0];
960        b = Vertex[Hoja1.Vertices[2]][1];
961        c = Vertex[Hoja1.Vertices[2]][2];
962
963        dist[2] = ((Hoja2.Centro[0]-a)*(Hoja2.Centro[0]-a)) + ((Hoja2.Centro[1]-b)*(Hoja2.Centro[1]-b)) +
964                   ((Hoja2.Centro[2]-c)*(Hoja2.Centro[2]-c));
965
966
967        a = Vertex[Hoja1.Vertices[3]][0];
968        b = Vertex[Hoja1.Vertices[3]][1];
969        c = Vertex[Hoja1.Vertices[3]][2];
970
971        dist[3] = ((Hoja2.Centro[0]-a)*(Hoja2.Centro[0]-a)) + ((Hoja2.Centro[1]-b)*(Hoja2.Centro[1]-b)) +
972                   ((Hoja2.Centro[2]-c)*(Hoja2.Centro[2]-c));
973
974        for ( int i=0;i<4;i++) indices[i]=i;
975
976        DosMayores(dist, indices);
977       
978        Hojasa[counth].Vertices[0] = Hoja1.Vertices[indices[0]];
979        Hojasa[counth].Vertices[1] = Hoja1.Vertices[indices[1]];
980       
981        // segunda hoja los dos ultimos vertices
982
983       
984        a = Vertex[Hoja2.Vertices[0]][0];
985        b = Vertex[Hoja2.Vertices[0]][1];
986        c = Vertex[Hoja2.Vertices[0]][2];
987
988        dist[0] = ((Hoja1.Centro[0]-a)*(Hoja1.Centro[0]-a)) + ((Hoja1.Centro[1]-b)*(Hoja1.Centro[1]-b)) +
989                   ((Hoja1.Centro[2]-c)*(Hoja1.Centro[2]-c));
990
991       
992        a = Vertex[Hoja2.Vertices[1]][0];
993        b = Vertex[Hoja2.Vertices[1]][1];
994        c = Vertex[Hoja2.Vertices[1]][2];
995
996        dist[1] = ((Hoja2.Centro[0]-a)*(Hoja2.Centro[0]-a)) + ((Hoja1.Centro[1]-b)*(Hoja1.Centro[1]-b)) +
997                   ((Hoja2.Centro[2]-c)*(Hoja2.Centro[2]-c));
998
999        a = Vertex[Hoja2.Vertices[2]][0];
1000        b = Vertex[Hoja2.Vertices[2]][1];
1001        c = Vertex[Hoja2.Vertices[2]][2];
1002
1003        dist[2] = ((Hoja1.Centro[0]-a)*(Hoja1.Centro[0]-a)) + ((Hoja1.Centro[1]-b)*(Hoja1.Centro[1]-b)) +
1004                   ((Hoja1.Centro[2]-c)*(Hoja1.Centro[2]-c));
1005
1006
1007        a = Vertex[Hoja2.Vertices[3]][0];
1008        b = Vertex[Hoja2.Vertices[3]][1];
1009        c = Vertex[Hoja2.Vertices[3]][2];
1010
1011        dist[3] = ((Hoja1.Centro[0]-a)*(Hoja1.Centro[0]-a)) + ((Hoja1.Centro[1]-b)*(Hoja1.Centro[1]-b)) +
1012                   ((Hoja1.Centro[2]-c)*(Hoja1.Centro[2]-c));
1013
1014        for ( int i=0;i<4;i++) indices[i]=i;
1015
1016        DosMayores(dist, indices);
1017       
1018        Hojasa[counth].Vertices[2] = Hoja2.Vertices[indices[0]];
1019        Hojasa[counth].Vertices[3] = Hoja2.Vertices[indices[1]];
1020
1021
1022
1023       
1024}
1025
1026//--------------------------------------------------------------------------------------------------------------------------------
1027// AÑADE HOJAS
1028//--------------------------------------------------------------------------------------------------------------------------------
1029long int Colapsa (FILE *fp_simplifica, float diametro)
1030{
1031        long int i=0, cual=-1;
1032        long int otra = -1;
1033        float coptmp, coptmp2, distmp, distmp2, criteriotmp;
1034        char linea[250];
1035
1036
1037//      cual=MinimaDistancia();
1038        cual=MinimoCriterio();
1039//      otra = Hojasa[cual].hoja_cerca; //para la distancia
1040//      otra = Hojasa[cual].hoja_cop;
1041        otra = Hojasa[cual].hoja_crit;
1042
1043
1044        //desactivo las hojas cercanas
1045        Hojasa[cual].existe = false;
1046        Hojasa[otra].existe = false;
1047        //creo la hoja nueva
1048
1049        Hojasa[counth].hoja_cerca = -1;
1050        Hojasa[counth].dist = 0;
1051        Hojasa[counth].hoja_cop = -1;
1052        Hojasa[counth].coplanar = 0;
1053
1054        Hojasa[counth].Cuantas_hojas = Hojasa[cual].Cuantas_hojas + Hojasa[otra].Cuantas_hojas;
1055        ElijeVertices(Hojasa[cual], Hojasa[otra], counth);
1056        Centroh (Hojasa[counth]);
1057        GetNormal ( Hojasa[counth]);
1058
1059        //18/09/01
1060        if (    Hojasa[counth].Cuantas_hojas > 60 )
1061        {
1062                        Hojasa[counth].existe = false;
1063                        activas--;
1064        }
1065        else
1066        {
1067
1068
1069         //establezco el criterio para la hoja nueva
1070         Hojasa[counth].existe = true;
1071                Hojasa[counth].criterio = 1000;
1072                //coplanaridad
1073                for ( int j =0; j<(counth+1);j++)
1074                {
1075                        if (( j != counth) && ( Hojasa[j].existe == true)) // si la hoja aun existe
1076                        {
1077                        //coplanaridad y lo invierto
1078                        coptmp2 = Hojasa[counth].Coplanaridad(Hojasa[j]);
1079                        coptmp = 1 - coptmp2;
1080                        //distancia y la normalizo
1081                        //distmp2 = Hojasa[counth].Distancia(Hojasa[j]);
1082                        distmp2 = Hausdorff( Hojasa[counth], Hojasa[j]);
1083                        distmp = distmp2 / diametro;
1084                        // calculo el criterio para esa hoja
1085                        criteriotmp = (( K1 * distmp * distmp ) + (K2 * coptmp * distmp))/ (K1 + K2);
1086                        //selecciono el criterio menor
1087                        if (Hojasa[counth].criterio > criteriotmp)
1088                                {Hojasa[counth].criterio = criteriotmp;
1089                                Hojasa[counth].hoja_crit = j;}
1090
1091                        }
1092                }
1093
1094        }       
1095        //en la pantalla
1096//      printf ("Nuevo %i Ver %lu %lu %lu %lu  Tviej %lu %lu Engloba %i\n",
1097//                    counth, Hojasa[counth].Vertices[0], Hojasa[counth].Vertices[1], Hojasa[counth].Vertices[2],
1098//                        Hojasa[counth].Vertices[3], cual, otra, Hojasa[counth].Cuantas_hojas);
1099
1100        //en el fichero
1101        sprintf (linea,"Nuevo %i Ver %lu %lu %lu %lu  T_viej %lu %lu Engloba %i\n",
1102                      counth, Hojasa[counth].Vertices[0], Hojasa[counth].Vertices[1], Hojasa[counth].Vertices[2],
1103                          Hojasa[counth].Vertices[3], cual, otra, Hojasa[counth].Cuantas_hojas);
1104        fputs (linea, fp_simplifica);
1105
1106
1107        //incremento el numero de hojas
1108        counth++;
1109        activas--;
1110
1111        return (counth-1);//porque lo he incrementado en la linea de antes
1112
1113}
1114
1115void simplifica(void){
1116        long int totalv=0, hnueva;
1117        float diam;
1118
1119        FILE* fp_simplifica;
1120        diam = DiametroEsferaEnvolvente(fp_simplifica );
1121        EstableceCriterio (diam);
1122       
1123        while ( activas > 20) {
1124                hnueva=Colapsa(fp_simplifica, diam);
1125                EstableceCriterio2 (diam, hnueva);
1126        }
1127}
1128
1129//--------------------------------------------------------------------------------------------------------------------------------
1130// MAIN DEL PROGRAMA
1131//--------------------------------------------------------------------------------------------------------------------------------
1132
1133//int main (int argc, char ** argv)
1134//{
1135//
1136//      long int totalv=0, hnueva;
1137//      float diam;
1138//      FILE* fp_simplifica;
1139////    FILE* fp_criterio;
1140//
1141//      first = time(NULL);
1142//
1143//      char fvertices[100],fhojas[100],fsalida[100];
1144//      strcpy(fvertices,"H:\\GTGeometry\\tests\\bin\\vertices.obj\0");
1145//      strcpy(fhojas,"H:\\GTGeometry\\tests\\bin\\hojas.obj\0");
1146//      strcpy(fsalida,"H:\\GTGeometry\\tests\\bin\\salida.kk\0");
1147//
1148//      argc=4;
1149//      if (argc != 4) {// el 0 es el nombre del programa
1150//              puts ("Sintaxis: fichero_arbol fichero_hojas fichero_simplif");
1151//              getchar();
1152//              return -1;
1153//      }
1154//     
1155//      //cuento todos los vertices, y relleno la estructura
1156//      totalv = LeeVertices(fvertices);
1157//
1158//      //cuento las hojas
1159//      counth= CuentaHojas (fhojas);
1160//      printf ("Hay %lu hojas\n", counth);
1161//
1162//      // lleno la estructura de datos de las hojas ( inicial y activas)
1163//      printf ("Ahora las leo\n");
1164//      LeoHojas(fhojas);
1165////    DistanciaEntreHojas();
1166////    CoplanarEntreHojas(fp_coplanar);
1167//      if ((fp_simplifica = fopen (fsalida, "w")) == NULL)
1168//              printf ("No puedo abrir el fichero %s\n", argv[3]);
1169//      else
1170//      {       
1171//     
1172//              diam = DiametroEsferaEnvolvente(fp_simplifica );
1173//              EstableceCriterio (diam);
1174//      //
1175//              /*if ((fp_criterio = fopen ("criterio.obj", "w")) == NULL)
1176//              printf ("No puedo abrir el fichero distancias\n");
1177//              else
1178//              EscribeCriterio(fp_criterio);
1179//
1180//              */while ( activas > 20)
1181//              {
1182//                      hnueva=Colapsa(fp_simplifica, diam);
1183//                      EstableceCriterio2 (diam, hnueva);
1184//              //      EscribeCriterio(fp_criterio);
1185//
1186//
1187//              }
1188//     
1189//              fclose ( fp_simplifica);
1190//      //      fclose ( fp_criterio);
1191//      }
1192//
1193//      second = time(NULL);
1194//      printf("Diferencia: %f segundos\n", difftime(second, first));
1195//      getchar();     
1196//      return 0;
1197//}//main
Note: See TracBrowser for help on using the repository browser.