Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

quadmesh.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- 00002 @COPYRIGHT : 00003 Copyright 1993,1994,1995 David MacDonald, 00004 McConnell Brain Imaging Centre, 00005 Montreal Neurological Institute, McGill University. 00006 Permission to use, copy, modify, and distribute this 00007 software and its documentation for any purpose and without 00008 fee is hereby granted, provided that the above copyright 00009 notice appear in all copies. The author and McGill University 00010 make no representations about the suitability of this 00011 software for any purpose. It is provided "as is" without 00012 express or implied warranty. 00013 ---------------------------------------------------------------------------- */ 00014 00015 #include <assert.h> 00016 #include <volume_io/internal_volume_io.h> 00017 #include <bicpl/objects.h> 00018 #include <bicpl/geom.h> 00019 00020 #ifndef lint 00021 static char rcsid[] = "$Header: /software/source//libraries/bicpl/Objects/quadmesh.c,v 1.20 2002/04/29 18:31:06 stever Exp $"; 00022 #endif 00023 00024 00054 /* ----------------------------- MNI Header ----------------------------------- 00055 @NAME : initialize_quadmesh 00056 @INPUT : col 00057 spr 00058 m 00059 n 00060 @OUTPUT : quadmesh 00061 @RETURNS : 00062 @DESCRIPTION: Initializes a quadmesh to the given size. 00063 @METHOD : 00064 @GLOBALS : 00065 @CALLS : 00066 @CREATED : 1993 David MacDonald 00067 @MODIFIED : 00068 ---------------------------------------------------------------------------- */ 00069 00084 public void initialize_quadmesh( 00085 quadmesh_struct *quadmesh, 00086 Colour col, 00087 Surfprop *spr, 00088 int m, 00089 int n ) 00090 { 00091 ALLOC( quadmesh->colours, 1 ); 00092 00093 quadmesh->colour_flag = ONE_COLOUR; 00094 00095 quadmesh->colours[0] = col; 00096 00097 if( spr != (Surfprop *) 0 ) 00098 quadmesh->surfprop = *spr; 00099 else 00100 get_default_surfprop( &quadmesh->surfprop ); 00101 00102 quadmesh->m = m; 00103 quadmesh->n = n; 00104 quadmesh->m_closed = FALSE; 00105 quadmesh->n_closed = FALSE; 00106 if( quadmesh->m > 0 && quadmesh->n > 0 ) 00107 { 00108 ALLOC( quadmesh->points, m * n ); 00109 ALLOC( quadmesh->normals, m * n ); 00110 } 00111 00112 quadmesh->bintree = (bintree_struct_ptr) NULL; 00113 } 00114 00115 /* ----------------------------- MNI Header ----------------------------------- 00116 @NAME : delete_quadmesh 00117 @INPUT : quadmesh 00118 @OUTPUT : 00119 @RETURNS : 00120 @DESCRIPTION: Deletes a quadmesh structure. 00121 @METHOD : 00122 @GLOBALS : 00123 @CALLS : 00124 @CREATED : 1993 David MacDonald 00125 @MODIFIED : 00126 ---------------------------------------------------------------------------- */ 00127 00130 public void delete_quadmesh( 00131 quadmesh_struct *quadmesh ) 00132 { 00133 free_colours( quadmesh->colour_flag, quadmesh->colours, 00134 quadmesh->m * quadmesh->n, 00135 (quadmesh->m-1) * (quadmesh->n-1) ); 00136 00137 if( quadmesh->m > 0 && quadmesh->n > 0 ) 00138 { 00139 FREE( quadmesh->points ); 00140 00141 if( quadmesh->normals != (Vector *) NULL ) 00142 FREE( quadmesh->normals ); 00143 } 00144 00145 delete_bintree_if_any( &quadmesh->bintree ); 00146 } 00147 00148 /* ----------------------------- MNI Header ----------------------------------- 00149 @NAME : set_quadmesh_point 00150 @INPUT : quadmesh 00151 i 00152 j 00153 point 00154 normal 00155 @OUTPUT : 00156 @RETURNS : 00157 @DESCRIPTION: Sets the point and normal of the quadmesh. 00158 @METHOD : 00159 @GLOBALS : 00160 @CALLS : 00161 @CREATED : 1993 David MacDonald 00162 @MODIFIED : 00163 ---------------------------------------------------------------------------- */ 00164 00176 public void set_quadmesh_point( 00177 quadmesh_struct *quadmesh, 00178 int i, 00179 int j, 00180 Point *point, 00181 Vector *normal ) 00182 { 00183 int ind; 00184 00185 assert( 0 <= i && i < quadmesh->m ); 00186 assert( 0 <= j && j < quadmesh->n ); 00187 00188 ind = IJ( i, j, quadmesh->n ); 00189 00190 quadmesh->points[ind] = *point; 00191 if( normal != NULL && quadmesh->normals != NULL ) 00192 quadmesh->normals[ind] = *normal; 00193 } 00194 00195 /* ----------------------------- MNI Header ----------------------------------- 00196 @NAME : get_quadmesh_point 00197 @INPUT : quadmesh 00198 i 00199 j 00200 @OUTPUT : point 00201 @RETURNS : TRUE if i,j is valid 00202 @DESCRIPTION: Passes back the specified point of the quadmesh. 00203 @METHOD : 00204 @GLOBALS : 00205 @CALLS : 00206 @CREATED : 1993 David MacDonald 00207 @MODIFIED : 00208 ---------------------------------------------------------------------------- */ 00209 00220 public BOOLEAN get_quadmesh_point( 00221 const quadmesh_struct *quadmesh, 00222 int i, 00223 int j, 00224 Point *point ) 00225 { 00226 if( i < 0 || i >= quadmesh->m || 00227 j < 0 || j >= quadmesh->n ) 00228 return( FALSE ); 00229 00230 *point = quadmesh->points[IJ(i,j,quadmesh->n)]; 00231 00232 return( TRUE ); 00233 } 00234 00235 /* ----------------------------- MNI Header ----------------------------------- 00236 @NAME : get_quadmesh_n_objects 00237 @INPUT : quadmesh 00238 @OUTPUT : mp 00239 np 00240 @RETURNS : 00241 @DESCRIPTION: Passes back the number of patches in the quadmesh. 00242 @METHOD : 00243 @GLOBALS : 00244 @CALLS : 00245 @CREATED : 1993 David MacDonald 00246 @MODIFIED : 00247 ---------------------------------------------------------------------------- */ 00248 00262 public void get_quadmesh_n_objects( 00263 const quadmesh_struct *quadmesh, 00264 int *mp, 00265 int *np ) 00266 { 00267 if( quadmesh->m_closed ) 00268 *mp = quadmesh->m; 00269 else 00270 *mp = quadmesh->m - 1; 00271 00272 if( quadmesh->n_closed ) 00273 *np = quadmesh->n; 00274 else 00275 *np = quadmesh->n - 1; 00276 } 00277 00278 /* ----------------------------- MNI Header ----------------------------------- 00279 @NAME : compute_quadmesh_normals 00280 @INPUT : quadmesh 00281 @OUTPUT : 00282 @RETURNS : 00283 @DESCRIPTION: Computes the normals of a quadmesh. 00284 @METHOD : 00285 @GLOBALS : 00286 @CALLS : 00287 @CREATED : 1993 David MacDonald 00288 @MODIFIED : 00289 ---------------------------------------------------------------------------- */ 00290 00299 public void compute_quadmesh_normals( 00300 quadmesh_struct *quadmesh ) 00301 { 00302 int i, j, m, n, n_neighs; 00303 Point neighbours[4]; 00304 progress_struct progress; 00305 00306 m = quadmesh->m; 00307 n = quadmesh->n; 00308 00309 if( quadmesh->normals == (Vector *) NULL ) 00310 ALLOC( quadmesh->normals, m * n ); 00311 00312 for_less( i, 0, m * n ) 00313 fill_Vector( quadmesh->normals[i], 0.0, 0.0, 0.0 ); 00314 00315 initialize_progress_report( &progress, FALSE, m, "Computing Normals" ); 00316 00317 for_less( i, 0, m ) 00318 { 00319 for_less( j, 0, n ) 00320 { 00321 n_neighs = 0; 00322 if( get_quadmesh_point( quadmesh, i, j-1, &neighbours[n_neighs]) ) 00323 ++n_neighs; 00324 if( get_quadmesh_point( quadmesh, i+1, j , &neighbours[n_neighs]) ) 00325 ++n_neighs; 00326 if( get_quadmesh_point( quadmesh, i, j+1, &neighbours[n_neighs]) ) 00327 ++n_neighs; 00328 if( get_quadmesh_point( quadmesh, i-1, j , &neighbours[n_neighs]) ) 00329 ++n_neighs; 00330 00331 if( n_neighs < 2 ) 00332 { 00333 handle_internal_error( "compute_quadmesh_normals" ); 00334 } 00335 else if( n_neighs == 2 ) 00336 { 00337 neighbours[n_neighs] = quadmesh->points[IJ(i,j,n)]; 00338 ++n_neighs; 00339 } 00340 00341 find_polygon_normal( n_neighs, neighbours, 00342 &quadmesh->normals[IJ(i,j,n)] ); 00343 NORMALIZE_VECTOR( quadmesh->normals[IJ(i,j,n)], 00344 quadmesh->normals[IJ(i,j,n)] ); 00345 } 00346 00347 update_progress_report( &progress, i + 1 ); 00348 } 00349 00350 terminate_progress_report( &progress ); 00351 } 00352 00353 /* ----------------------------- MNI Header ----------------------------------- 00354 @NAME : get_quadmesh_patch_indices 00355 @INPUT : quadmesh 00356 i 00357 j 00358 @OUTPUT : indices 00359 @RETURNS : 00360 @DESCRIPTION: Passes back the indices of the quadmesh points making up the 00361 i,j'th patch 00362 @METHOD : 00363 @GLOBALS : 00364 @CALLS : 00365 @CREATED : 1993 David MacDonald 00366 @MODIFIED : 00367 ---------------------------------------------------------------------------- */ 00368 00384 public void get_quadmesh_patch_indices( 00385 const quadmesh_struct *quadmesh, 00386 int i, 00387 int j, 00388 int indices[] ) 00389 { 00390 indices[0] = IJ( i, j, quadmesh->n ); 00391 indices[1] = IJ( (i+1) % quadmesh->m, j, quadmesh->n ); 00392 indices[2] = IJ( (i+1) % quadmesh->m, (j+1) % quadmesh->n, quadmesh->n ); 00393 indices[3] = IJ( i, (j+1) % quadmesh->n, quadmesh->n ); 00394 } 00395 00396 /* ----------------------------- MNI Header ----------------------------------- 00397 @NAME : get_quadmesh_patch 00398 @INPUT : quadmesh 00399 i 00400 j 00401 @OUTPUT : points 00402 @RETURNS : 00403 @DESCRIPTION: Passes back the 4 points defining the i,j'th patch. 00404 @METHOD : 00405 @GLOBALS : 00406 @CALLS : 00407 @CREATED : 1993 David MacDonald 00408 @MODIFIED : 00409 ---------------------------------------------------------------------------- */ 00410 00422 public void get_quadmesh_patch( 00423 const quadmesh_struct *quadmesh, 00424 int i, 00425 int j, 00426 Point points[] ) 00427 { 00428 int p, indices[4]; 00429 00430 get_quadmesh_patch_indices( quadmesh, i, j, indices ); 00431 00432 for_less( p, 0, 4 ) 00433 points[p] = quadmesh->points[indices[p]]; 00434 } 00435 00436 00442 public void reverse_quadmesh_vertices( 00443 quadmesh_struct *quadmesh ) 00444 { 00445 int i, j, i1, i2; 00446 Point tmp_point; 00447 Vector tmp_normal; 00448 00449 for_less( i, 0, quadmesh->m ) 00450 { 00451 for_less( j, 0, quadmesh->n / 2 ) 00452 { 00453 i1 = IJ( i, j, quadmesh->n ); 00454 i2 = IJ( i, quadmesh->n - 1 - j, quadmesh->n ); 00455 00456 tmp_point = quadmesh->points[i1]; 00457 quadmesh->points[i1] = quadmesh->points[i2]; 00458 quadmesh->points[i2] = tmp_point; 00459 00460 if( quadmesh->normals != NULL ) 00461 { 00462 tmp_normal = quadmesh->normals[i1]; 00463 quadmesh->normals[i1] = quadmesh->normals[i2]; 00464 quadmesh->normals[i2] = tmp_normal; 00465 } 00466 } 00467 } 00468 } 00469 00470

Generated on Wed Jul 28 09:10:57 2004 for BICPL by doxygen 1.3.7