Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

circutil.c

Go to the documentation of this file.
00001 /*
00002   circutil.c
00003 
00004   Generalized circle utilities
00005 
00006   Modification history:
00007 
00008   18-Jun-1997  FMP created
00009   */
00010 
00011 #include <math.h>               /* acos() */
00012 #include "posemath.h"           /* these decls, PM_CARTESIAN */
00013 
00014 /*
00015   pmCircleInit() takes the defining parameters of a generalized circle
00016   and sticks them in the structure. It also computes the radius and vectors
00017   in the plane that are useful for other functions and that don't need
00018   to be recomputed every time.
00019   */
00020 int pmCircleInit(PM_CIRCLE * circle,
00021                  PM_CARTESIAN start, PM_CARTESIAN end,
00022                  PM_CARTESIAN center, PM_CARTESIAN normal,
00023                  int turn)
00024 {
00025   double dot;
00026   PM_CARTESIAN rEnd;
00027 
00028   if (0 == circle)
00029     {
00030       return -1;
00031     }
00032 
00033   /* load and normalize required descriptions */
00034   circle->start = start;
00035   circle->end = end;
00036   circle->center = center;
00037   pmCartNorm(normal, &circle->normal);
00038   circle->turn = turn;          /* 0 = 0..360, 1 = 360..720,
00039                                    -1 = 0..-360, -2 = -360..-720, etc. */
00040 
00041   /* radius */
00042   pmCartCartDisp(start, center, &circle->radius);
00043 
00044   /* vector in plane of circle from center to start, magnitude radius */
00045   pmCartCartSub(start, center, &circle->rTan);
00046 
00047   /* vector in plane of circle perpendicular to uTan, magnitude radius */
00048   pmCartCartCross(circle->normal, circle->rTan, &circle->rPerp);
00049 
00050   /* do rHelix, rEnd */
00051   pmCartCartSub(end, center, &circle->rHelix);
00052   pmCartPlaneProj(circle->rHelix, circle->normal, &rEnd);
00053   pmCartMag(rEnd, &circle->spiral);
00054   circle->spiral -= circle->radius;
00055   pmCartCartSub(circle->rHelix, rEnd, &circle->rHelix);
00056   pmCartNorm(rEnd, &rEnd);
00057   pmCartScalMult(rEnd, circle->radius, &rEnd);
00058 
00059   /* angle */
00060   pmCartCartDot(start, rEnd, &dot);
00061   dot = dot / (circle->radius * circle->radius);
00062   circle->angle = acos(dot);
00063   if (turn > 0)
00064     {
00065       circle->angle += turn * 2.0 * PM_PI;
00066     }
00067   else if (turn < 0)
00068     {
00069       circle->angle = - circle->angle - (turn + 1) * 2.0 * PM_PI;
00070     }
00071 
00072   return pmErrno = 0;
00073 }
00074 
00075 /*
00076   pmCirclePoint() returns the vector to the point at the given angle along
00077   the circle. If the circle is a helix or spiral or combination, the
00078   point will include interpolation off the actual circle.
00079   */
00080 int pmCirclePoint(PM_CIRCLE * circle, double angle, PM_CARTESIAN * point)
00081 {
00082   PM_CARTESIAN par, perp;
00083   double scale;
00084 
00085   /* compute components rel to center */
00086   pmCartScalMult(circle->rTan, cos(angle), &par);
00087   pmCartScalMult(circle->rPerp, sin(angle), &perp);
00088 
00089   /* add to get radius vector rel to center */
00090   pmCartCartAdd(par, perp, point);
00091 
00092   /* get scale for spiral, helix interpolation */
00093   scale = angle / circle->angle;
00094 
00095   /* add scaled vector in radial dir for spiral */
00096   pmCartNorm(*point, &par);
00097   pmCartScalMult(par, scale * circle->spiral, &par);
00098   pmCartCartAdd(*point, par, point);
00099 
00100   /* add scaled vector in helix dir */
00101   pmCartScalMult(circle->rHelix, scale, &perp);
00102   pmCartCartAdd(*point, perp, point);
00103 
00104   return pmErrno = 0;
00105 }

Generated on Sun Dec 2 15:56:48 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001