00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "toolkits/geom/geometria.h"
00023
00024 #ifdef VEL_ROSITA
00025 #include "toolkits/geom/octree_n.h"
00026 #endif
00027
00028 #include <stdlib.h>
00029
00030
00031
00032
00033
00040 int DIRECCIONES_octantes[8][3] = {
00041 {1, 1, 1},
00042 {-1, 1, 1},
00043 {1, -1, 1},
00044 {-1, -1, 1},
00045 {1, 1, -1},
00046 {-1, 1, -1},
00047 {1, -1, -1},
00048 {-1, -1, -1}
00049 };
00050
00051
00052
00053
00054
00055 void
00056 NODO_OCTREE::init(void)
00057 {
00058 relleno = FALSE;
00059 hijos = NULL;
00060 }
00061
00062 void
00063 NODO_OCTREE::elim(void)
00064 {
00065 int i;
00066
00067 if ( hijos ) {
00068 for ( i = 0; i < 8; i++ ) {
00069 hijos[i].elim();
00070 }
00071 }
00072
00073 if ( hijos ) {
00074
00075
00076
00077 free((void *)hijos);
00078 hijos = NULL;
00079 }
00080 }
00081
00082 void
00083 NODO_OCTREE::compactar(void)
00084 {
00085 if ( !hijos ) return;
00086
00087 int i;
00088 BOOLEAN inicial = hijos[0].relleno;
00089
00090 for ( i = 0; i < 8; i++ ) {
00091 if ( hijos[i].hijos ||
00092 hijos[i].relleno != inicial ) return;
00093 }
00094 elim();
00095 relleno = inicial;
00096 }
00097
00098 void
00099 NODO_OCTREE::calcular(GEOMETRIA *Fuente, int nivel, VECTOR origen, double lado)
00104 {
00105
00106 if ( nivel == 0 ) {
00107 if ( Fuente->clasificar_punto(origen) == 1 ) {
00108 relleno = TRUE;
00109 }
00110 else {
00111 relleno = FALSE;
00112 }
00113 return;
00114 }
00115
00116
00117 int i;
00118 VECTOR sub_origen;
00119
00120
00121
00122 hijos = (NODO_OCTREE *) malloc(sizeof(NODO_OCTREE) * 8);
00123 for ( i = 0; i < 8; i++ ) {
00124 hijos[i].init();
00125 }
00126
00127 for ( i = 0; i < 8; i++ ) {
00128 sub_origen.x = origen.x + DIRECCIONES_octantes[i][0] * (lado/4);
00129 sub_origen.y = origen.y + DIRECCIONES_octantes[i][1] * (lado/4);
00130 sub_origen.z = origen.z + DIRECCIONES_octantes[i][2] * (lado/4);
00131 hijos[i].calcular(Fuente, nivel-1, sub_origen, lado/2);
00132 hijos[i].compactar();
00133 }
00134 compactar();
00135 }
00136
00137 #ifdef GL_ENABLED
00138 void
00139 NODO_OCTREE::pintar_gl(VECTOR origen, double lado)
00140 {
00141
00142 if ( !hijos ) {
00143 if ( relleno ) {
00144 glPushMatrix();
00145 glTranslated(origen.x, origen.y, origen.z);
00146 pintar_cubo_solido(lado);
00147 glPopMatrix();
00148 }
00149 return;
00150 }
00151
00152
00153 int i;
00154 VECTOR sub_origen;
00155
00156 for ( i = 0; i < 8; i++ ) {
00157 sub_origen.x = origen.x + DIRECCIONES_octantes[i][0] * (lado/4);
00158 sub_origen.y = origen.y + DIRECCIONES_octantes[i][1] * (lado/4);
00159 sub_origen.z = origen.z + DIRECCIONES_octantes[i][2] * (lado/4);
00160 hijos[i].pintar_gl(sub_origen, lado/2);
00161 }
00162 }
00163 #endif // GL_ENABLED
00164
00165 void
00166 NODO_OCTREE::leer(FILE *fd)
00167 {
00168 char d = leer_elemento_octree(fd);
00169
00170
00171 init();
00172 if ( d == 0 ) {
00173 relleno = TRUE;
00174 return;
00175 }
00176 if ( d == 1 ) {
00177 relleno = FALSE;
00178 return;
00179 }
00180
00181
00182 int i;
00183
00184
00185
00186 hijos = (NODO_OCTREE *) malloc(sizeof(NODO_OCTREE) * 8);
00187 for ( i = 0; i < 8; i++ ) {
00188 hijos[i].init();
00189 hijos[i].leer(fd);
00190 }
00191
00192 }
00193
00194 void
00195 NODO_OCTREE::escribir(FILE *fd)
00196 {
00197
00198 if ( !hijos ) {
00199 if ( relleno ) {
00200 escribir_elemento_octree(fd, 0);
00201 }
00202 else {
00203 escribir_elemento_octree(fd, 1);
00204 }
00205 return;
00206 }
00207
00208
00209 int i;
00210
00211 escribir_elemento_octree(fd, 2);
00212 for ( i = 0; i < 8; i++ ) {
00213 hijos[i].escribir(fd);
00214 }
00215
00216 }
00217
00218
00219
00220
00221