00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
#include <volume_io/internal_volume_io.h>
00016
#include <bicpl/geom.h>
00017
00018
#ifndef lint
00019
static char rcsid[] =
"$Header: /software/source//libraries/bicpl/Geometry/geometry.c,v 1.20 2000/02/06 15:30:14 stever Exp $";
00020
#endif
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 public void find_polygon_normal_no_normalize(
00038
int n_points,
00039 Point points[],
00040 Real *nx,
00041 Real *ny,
00042 Real *nz )
00043 {
00044
int i, next_i;
00045 Vector v1, v2, normal;
00046 Real vx, vy, vz, x,
y, z, tx, ty, tz, x0, x1, x2, y0, y1, y2, z0, z1, z2;
00047
00048
if( n_points == 3 )
00049 {
00050 x0 = (Real) Point_x(points[0]);
00051 y0 = (Real) Point_y(points[0]);
00052 z0 = (Real) Point_z(points[0]);
00053 x1 = (Real) Point_x(points[1]) - x0;
00054 y1 = (Real) Point_y(points[1]) - y0;
00055 z1 = (Real) Point_z(points[1]) - z0;
00056 x2 = (Real) Point_x(points[2]) - x0;
00057 y2 = (Real) Point_y(points[2]) - y0;
00058 z2 = (Real) Point_z(points[2]) - z0;
00059 *
nx = y1 * z2 - z1 * y2;
00060 *
ny = z1 * x2 - x1 * z2;
00061 *
nz = x1 * y2 - y1 * x2;
00062
return;
00063 }
00064
00065 vx = 0.0;
00066 vy = 0.0;
00067 vz = 0.0;
00068
00069 tx = (Real) Point_x(points[0]);
00070 ty = (Real) Point_y(points[0]);
00071 tz = (Real) Point_z(points[0]);
00072
00073 for_less( i, 0, n_points )
00074 {
00075 next_i = (i + 1) % n_points;
00076
00077 x = tx;
00078
y = ty;
00079 z = tz;
00080 tx = (Real) Point_x(points[next_i]);
00081 ty = (Real) Point_y(points[next_i]);
00082 tz = (Real) Point_z(points[next_i]);
00083
00084 vx -= (
y + ty) * (z - tz);
00085 vy -= (z + tz) * (x - tx);
00086 vz -= (x + tx) * (
y - ty);
00087 }
00088
00089
00090
00091
00092
if( vx == 0.0 && vy == 0.0 && vz == 0.0 )
00093 {
00094 for_less( i, 0, n_points )
00095 {
00096 SUB_POINTS( v1, points[(i+1)%n_points], points[i] );
00097 SUB_POINTS( v2, points[(i-1)%n_points], points[i] );
00098 CROSS_VECTORS( normal, v1, v2 );
00099
if( !
null_Vector( &normal ) )
00100 {
00101 vx = (Real) Vector_x( normal );
00102 vy = (Real) Vector_y( normal );
00103 vz = (Real) Vector_z( normal );
00104
break;
00105 }
00106 }
00107 }
00108
00109 *
nx = vx;
00110 *
ny = vy;
00111 *
nz = vz;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 public void find_polygon_normal(
00130
int n_points,
00131 Point points[],
00132 Vector *normal )
00133 {
00134 Real
nx,
ny,
nz;
00135
00136
find_polygon_normal_no_normalize( n_points, points, &
nx, &
ny, &
nz );
00137
00138 fill_Vector( *normal,
nx,
ny,
nz );
00139
00140 NORMALIZE_VECTOR( *normal, *normal );
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 public void get_plane_through_points(
00161
int n_points,
00162 Point points[],
00163 Vector *normal,
00164 Real *plane_constant )
00165 {
00166 Point centroid;
00167
00168
find_polygon_normal( n_points, points, normal );
00169
00170
get_points_centroid( n_points, points, ¢roid );
00171
00172 *plane_constant = -
distance_from_plane( ¢roid, normal, 0.0 );
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 public Real
distance_from_plane(
00191 Point *point,
00192 Vector *plane_normal,
00193 Real plane_constant )
00194 {
00195
return( DOT_POINT_VECTOR( *point, *plane_normal ) + plane_constant );
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 public Real
distance_from_line(
00214 Point *point,
00215 Point *end_point1,
00216 Point *end_point2 )
00217 {
00218 Vector d, v;
00219 Real dist, len, v_dot_d, v_dot_v;
00220
00221 SUB_POINTS( d, *end_point2, *end_point1 );
00222
00223 len = DOT_VECTORS( d, d );
00224
if( len == 0.0 )
00225 {
00226 dist =
distance_between_points( point, end_point1 );
00227 }
00228
else
00229 {
00230 SUB_POINTS( v, *point, *end_point1 );
00231
00232 v_dot_d = DOT_VECTORS( v, d );
00233 v_dot_v = DOT_VECTORS( v, v );
00234
00235 dist = v_dot_v - v_dot_d * v_dot_d / len;
00236 dist = sqrt( dist );
00237 }
00238
00239
return( dist );
00240 }