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

points.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 <volume_io/internal_volume_io.h> 00016 #include <bicpl/geom.h> 00017 #include <bicpl/trans.h> 00018 00019 #ifndef lint 00020 static char rcsid[] = "$Header: /software/source//libraries/bicpl/Geometry/points.c,v 1.8 2000/02/06 15:30:16 stever Exp $"; 00021 #endif 00022 00023 /* ----------------------------- MNI Header ----------------------------------- 00024 @NAME : null_Point 00025 @INPUT : p 00026 @OUTPUT : 00027 @RETURNS : TRUE if point is ( 0, 0, 0 ). 00028 @DESCRIPTION: 00029 @METHOD : 00030 @GLOBALS : 00031 @CALLS : 00032 @CREATED : 1993 David MacDonald 00033 @MODIFIED : 00034 ---------------------------------------------------------------------------- */ 00035 00036 public BOOLEAN null_Point( 00037 Point *p ) 00038 { 00039 return( Point_x(*p) == 0.0f && 00040 Point_y(*p) == 0.0f && 00041 Point_z(*p) == 0.0f ); 00042 } 00043 00044 /* ----------------------------- MNI Header ----------------------------------- 00045 @NAME : null_Vector 00046 @INPUT : 00047 @OUTPUT : 00048 @RETURNS : TRUE if vector is ( 0, 0, 0 ). 00049 @DESCRIPTION: 00050 @METHOD : 00051 @GLOBALS : 00052 @CALLS : 00053 @CREATED : 1993 David MacDonald 00054 @MODIFIED : 00055 ---------------------------------------------------------------------------- */ 00056 00057 public BOOLEAN null_Vector( 00058 Vector *v ) 00059 { 00060 return( Vector_x(*v) == 0.0f && 00061 Vector_y(*v) == 0.0f && 00062 Vector_z(*v) == 0.0f ); 00063 } 00064 00065 /* ----------------------------- MNI Header ----------------------------------- 00066 @NAME : distance_between_points 00067 @INPUT : p1 00068 p2 00069 @OUTPUT : 00070 @RETURNS : Euclidean distance between points 00071 @DESCRIPTION: 00072 @METHOD : 00073 @GLOBALS : 00074 @CALLS : 00075 @CREATED : 1993 David MacDonald 00076 @MODIFIED : 00077 ---------------------------------------------------------------------------- */ 00078 00079 public Real distance_between_points( 00080 Point *p1, 00081 Point *p2 ) 00082 { 00083 Real dx, dy, dz; 00084 00085 dx = (Real) Point_x(*p2) - (Real) Point_x(*p1); 00086 dy = (Real) Point_y(*p2) - (Real) Point_y(*p1); 00087 dz = (Real) Point_z(*p2) - (Real) Point_z(*p1); 00088 00089 return( sqrt( dx * dx + dy * dy + dz * dz ) ); 00090 } 00091 00092 /* ----------------------------- MNI Header ----------------------------------- 00093 @NAME : points_within_distance 00094 @INPUT : p1 00095 p2 00096 distance 00097 @OUTPUT : 00098 @RETURNS : TRUE if points within distance 00099 @DESCRIPTION: Determines if two points are within the specified distance. Tries 00100 to be efficient by not performing a sqrt. 00101 @METHOD : 00102 @GLOBALS : 00103 @CALLS : 00104 @CREATED : 1993 David MacDonald 00105 @MODIFIED : 00106 ---------------------------------------------------------------------------- */ 00107 00108 public BOOLEAN points_within_distance( 00109 Point *p1, 00110 Point *p2, 00111 Real distance ) 00112 { 00113 Real dx, dy, dz; 00114 00115 dx = (Real) Point_x(*p1) - (Real) Point_x(*p2); 00116 dy = (Real) Point_y(*p1) - (Real) Point_y(*p2); 00117 dz = (Real) Point_z(*p1) - (Real) Point_z(*p2); 00118 00119 return( dx * dx + dy * dy + dz * dz <= distance * distance ); 00120 } 00121 00122 /* ----------------------------- MNI Header ----------------------------------- 00123 @NAME : apply_point_to_min_and_max 00124 @INPUT : point 00125 min_point - current min position 00126 max_point - current max position 00127 @OUTPUT : 00128 @RETURNS : 00129 @DESCRIPTION: Sets the range of space specified by min_point and max_point 00130 to the smallest rectangular range containing both the current 00131 range and the specified point. This is 00132 performed by expanding the rectangular range if needed, to 00133 contain the specified point. 00134 @METHOD : 00135 @GLOBALS : 00136 @CALLS : 00137 @CREATED : 1993 David MacDonald 00138 @MODIFIED : 00139 ---------------------------------------------------------------------------- */ 00140 00141 public void apply_point_to_min_and_max( 00142 Point *point, 00143 Point *min_point, 00144 Point *max_point ) 00145 { 00146 int c; 00147 00148 for_less( c, 0, N_DIMENSIONS ) 00149 { 00150 if( Point_coord(*point,c) < Point_coord(*min_point,c) ) 00151 Point_coord(*min_point,c) = Point_coord(*point,c); 00152 00153 if( Point_coord(*point,c) > Point_coord(*max_point,c) ) 00154 Point_coord(*max_point,c) = Point_coord(*point,c); 00155 } 00156 } 00157 00158 /* ----------------------------- MNI Header ----------------------------------- 00159 @NAME : expand_min_and_max_points 00160 @INPUT : min_point 00161 max_point 00162 @OUTPUT : min_to_check 00163 max_to_check 00164 @RETURNS : 00165 @DESCRIPTION: Sets the rectangular range of space specified by min_point and 00166 max_point to the rectangular range containing the four argument 00167 points. 00168 @METHOD : 00169 @GLOBALS : 00170 @CALLS : 00171 @CREATED : 1993 David MacDonald 00172 @MODIFIED : 00173 ---------------------------------------------------------------------------- */ 00174 00175 public void expand_min_and_max_points( 00176 Point *min_point, 00177 Point *max_point, 00178 Point *min_to_check, 00179 Point *max_to_check ) 00180 { 00181 int c; 00182 00183 for_less( c, 0, N_DIMENSIONS ) 00184 { 00185 if( Point_coord(*min_to_check,c) < Point_coord(*min_point,c) ) 00186 Point_coord(*min_point,c) = Point_coord(*min_to_check,c); 00187 if( Point_coord(*max_to_check,c) > Point_coord(*max_point,c) ) 00188 Point_coord(*max_point,c) = Point_coord(*max_to_check,c); 00189 } 00190 } 00191 00192 /* ----------------------------- MNI Header ----------------------------------- 00193 @NAME : get_range_points 00194 @INPUT : n_points 00195 points 00196 @OUTPUT : min_corner 00197 max_corner 00198 @RETURNS : 00199 @DESCRIPTION: Finds the rectangular range containing all the points. 00200 @METHOD : 00201 @GLOBALS : 00202 @CALLS : 00203 @CREATED : 1993 David MacDonald 00204 @MODIFIED : 00205 ---------------------------------------------------------------------------- */ 00206 00207 public void get_range_points( 00208 int n_points, 00209 Point points[], 00210 Point *min_corner, 00211 Point *max_corner ) 00212 { 00213 int i; 00214 00215 if( n_points > 0 ) 00216 { 00217 *min_corner = points[0]; 00218 *max_corner = points[0]; 00219 00220 for_less( i, 1, n_points ) 00221 { 00222 apply_point_to_min_and_max( &points[i], min_corner, max_corner ); 00223 } 00224 } 00225 } 00226 00227 /* ----------------------------- MNI Header ----------------------------------- 00228 @NAME : get_points_centroid 00229 @INPUT : n_points 00230 points 00231 @OUTPUT : centroid 00232 @RETURNS : 00233 @DESCRIPTION: Finds the centroid of the set of points. 00234 @METHOD : 00235 @GLOBALS : 00236 @CALLS : 00237 @CREATED : 1993 David MacDonald 00238 @MODIFIED : 00239 ---------------------------------------------------------------------------- */ 00240 00241 public void get_points_centroid( 00242 int n_points, 00243 Point points[], 00244 Point *centroid ) 00245 { 00246 int i; 00247 Real x, y, z; 00248 00249 x = 0.0; 00250 y = 0.0; 00251 z = 0.0; 00252 00253 for_less( i, 0, n_points ) 00254 { 00255 x += (Real) Point_x(points[i]); 00256 y += (Real) Point_y(points[i]); 00257 z += (Real) Point_z(points[i]); 00258 } 00259 00260 fill_Point( *centroid, x / (Real) n_points, 00261 y / (Real) n_points, 00262 z / (Real) n_points ); 00263 } 00264 00265 /* ----------------------------- MNI Header ----------------------------------- 00266 @NAME : reverse_vectors 00267 @INPUT : n_vectors 00268 vectors 00269 @OUTPUT : 00270 @RETURNS : 00271 @DESCRIPTION: Reverses the direction of each vector in the list. 00272 @METHOD : 00273 @GLOBALS : 00274 @CALLS : 00275 @CREATED : 1993 David MacDonald 00276 @MODIFIED : 00277 ---------------------------------------------------------------------------- */ 00278 00279 public void reverse_vectors( 00280 int n_vectors, 00281 Vector vectors[] ) 00282 { 00283 int i; 00284 00285 for_less( i, 0, n_vectors ) 00286 SCALE_VECTOR( vectors[i], vectors[i], -1.0 ); 00287 } 00288 00289 /* ----------------------------- MNI Header ----------------------------------- 00290 @NAME : get_angle_between_points 00291 @INPUT : prev_point 00292 this_one 00293 next_point 00294 @OUTPUT : 00295 @RETURNS : angle in radians. 00296 @DESCRIPTION: Returns the angle between the three points, in the range 0 to 00297 PI. 00298 @METHOD : 00299 @GLOBALS : 00300 @CALLS : 00301 @CREATED : 1993 David MacDonald 00302 @MODIFIED : 00303 ---------------------------------------------------------------------------- */ 00304 00305 public Real get_angle_between_points( 00306 Point *prev_point, 00307 Point *this_point, 00308 Point *next_point ) 00309 { 00310 Real angle, c; 00311 Vector v1, v2; 00312 00313 SUB_POINTS( v1, *prev_point, *this_point ); 00314 SUB_POINTS( v2, *next_point, *this_point ); 00315 00316 NORMALIZE_VECTOR( v1, v1 ); 00317 NORMALIZE_VECTOR( v2, v2 ); 00318 00319 c = DOT_VECTORS( v1, v2 ); 00320 00321 if( c >= 1.0 ) 00322 angle = 0.0; 00323 else if( c <= -1.0 ) 00324 angle = PI; 00325 else 00326 angle = acos( c ); 00327 00328 return( angle ); 00329 }

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