void dvhat_c ( ConstSpiceDouble s1 [6], SpiceDouble sout[6] ) /* -Brief_I/O VARIABLE I/O DESCRIPTION -------- --- -------------------------------------------------- s1 I State to be normalized. sout O Unit vector s1 / |s1|, and its time derivative. -Detailed_Input s1 This is any double precision state. If the position component of the state is the zero vector, this routine will detect it and will not attempt to divide by zero. -Detailed_Output sout sout is a state containing the unit vector pointing in the direction of position component of s1 and the derivative of the unit vector with respect to time. sout may overwrite s1. -Parameters None. -Exceptions Error free. 1) If s1 represents the zero vector, then the position component of sout will also be the zero vector. The velocity component will be the velocity component of s1. -Files None. -Particulars Let s1 be a state vector with position and velocity components p and v respectively. From these components one can compute the unit vector parallel to p, call it u and the derivative of u with respect to time, du. This pair (u,du) is the state returned by this routine in sout. -Examples Any numerical results shown for this example may differ between platforms as the results depend on the SPICE kernels used as input and the machine specific arithmetic implementation. Suppose that 'state' gives the apparent state of a body with respect to an observer. This routine can be used to compute the instantaneous angular rate of the object across the sky as seen from the observers vantage. #include "SpiceUsr.h" #include <stdio.h> #include <math.h> int main() { SpiceDouble et; SpiceDouble ltime; SpiceDouble omega; SpiceDouble state [6]; SpiceDouble ustate [6]; SpiceChar * epoch = "Jan 1 2009"; SpiceChar * target = "MOON"; SpiceChar * frame = "J2000"; SpiceChar * abcorr = "LT+S"; SpiceChar * obsrvr = "EARTH BARYCENTER"; /. Load SPK, PCK, and LSK kernels, use a meta kernel for convenience. ./ furnsh_c ( "standard.tm" ); /. Define an arbitrary epoch, convert the epoch to ephemeris time. ./ str2et_c ( epoch, &et ); /. Calculate the state of the moon with respect to the earth-moon barycenter in J2000, corrected for light time and stellar aberration at ET. ./ spkezr_c ( target, et, frame, abcorr, obsrvr, state, <ime ); /. Calculate the unit vector of STATE and the derivative of the unit vector. ./ dvhat_c ( state, ustate ); /. Calculate the instantaneous angular velocity from the magnitude of the derivative of the unit vector. v = r x omega ||omega|| = ||v|| for r . v = 0 ----- ||r|| ||omega|| = ||v|| for ||r|| = 1 ./ omega = vnorm_c( &ustate[3] ); printf( "Instantaneous angular velocity, rad/sec %.10g\n", omega ); return 0; } The program outputs: Instantaneous angular velocity, rad/sec 2.48106658e-06 -Restrictions None. -Literature_References None. -Author_and_Institution W.L. Taber (JPL) E.D. Wright (JPL) -Version -CSPICE Version 1.0.1, 06-MAY-2010 (EDW) Reordered header sections to proper NAIF convention. Minor edit to code comments eliminating typo. -CSPICE Version 1.0.0, 07-JUL-1999 (EDW) -Index_Entries State of a unit vector parallel to a state vector -& */ { /* Begin dvhat_c */ /* Local variables */ SpiceDouble length; SpiceDouble posin [3]; SpiceDouble posout[3]; SpiceDouble velin [3]; SpiceDouble velout[3]; /* We'll do this the obvious way for now. Unpack the input vector into two working vectors. */ posin[0] = s1[0]; posin[1] = s1[1]; posin[2] = s1[2]; velin[0] = s1[3]; velin[1] = s1[4]; velin[2] = s1[5]; /* Get the position portion of the output state and the length of the input position. */ unorm_c ( posin, posout, &length ); if ( length == 0. ) { /* If the length of the input position is zero, just copy the input velocity to the output velocity. */ vequ_c ( velin, velout ); } else { /* Otherwise the derivative of the unit vector is just the component of the input velocity perpendicular to the input position, scaled by the reciprocal of the length of the input position. */ vperp_c ( velin , posout, velout ); vscl_c ( 1./length, velout, velout ); } /* Pack everything and return. Hazar! */ sout[0] = posout[0]; sout[1] = posout[1]; sout[2] = posout[2]; sout[3] = velout[0]; sout[4] = velout[1]; sout[5] = velout[2]; } /* End dvhat_c */
void pjelpl_c ( ConstSpiceEllipse * elin, ConstSpicePlane * plane, SpiceEllipse * elout ) /* -Brief_I/O Variable I/O Description -------- --- -------------------------------------------------- elin I A CSPICE ellipse to be projected. plane I A plane onto which elin is to be projected. elout O A CSPICE ellipse resulting from the projection. -Detailed_Input elin, plane are, respectively, a cspice ellipse and a cspice plane. The geometric ellipse represented by elin is to be orthogonally projected onto the geometric plane represented by plane. -Detailed_Output elout is a cspice ellipse that represents the geometric ellipse resulting from orthogonally projecting the ellipse represented by inel onto the plane represented by plane. -Parameters None. -Exceptions 1) If the input plane is invalid, the error will be diagnosed by routines called by this routine. 2) The input ellipse may be degenerate--its semi-axes may be linearly dependent. Such ellipses are allowed as inputs. 3) The ellipse resulting from orthogonally projecting the input ellipse onto a plane may be degenerate, even if the input ellipse is not. -Files None. -Particulars Projecting an ellipse orthogonally onto a plane can be thought of finding the points on the plane that are `under' or `over' the ellipse, with the `up' direction considered to be perpendicular to the plane. More mathematically, the orthogonal projection is the set of points Y in the plane such that for some point X in the ellipse, the vector Y - X is perpendicular to the plane. The orthogonal projection of an ellipse onto a plane yields another ellipse. -Examples 1) With center = { 1., 1., 1. }, vect1 = { 2., 0., 0. }, vect2 = { 0., 1., 1. }, normal = { 0., 0., 1. } the code fragment nvc2pl_c ( normal, 0., plane ); cgv2el_c ( center, vect1, vect2, elin ); pjelpl_c ( elin, plane, elout ); el2cgv_c ( elout, prjctr, prjmaj, prjmin ); returns prjctr = { 1., 1., 0. }, prjmaj = { 2., 0., 0. }, prjmin = { 0., 1., 0. } 2) With vect1 = { 2., 0., 0. }, vect2 = { 1., 1., 1. }, center = { 0., 0., 0. }, normal = { 0., 0., 1. }, the code fragment nvc2pl_c ( normal, 0., plane ); cgv2el_c ( center, vect1, vect2, elin ); pjelpl_c ( elin, plane, elout ); el2cgv_c ( elout, prjctr, prjmaj, prjmin ); returns prjctr = { 0., 0., 0. }; prjmaj = { -2.227032728823213, -5.257311121191336e-1, 0. }; prjmin = { 2.008114158862273e-1, -8.506508083520399e-1, 0. }; 3) An example of actual use: Suppose we wish to compute the distance from an ellipsoid to a line. Let the line be defined by a point P and a direction vector DIRECT; the line is the set of points P + t * DIRECT, where t is any real number. Let the ellipsoid have semi- axis lengths A, B, and C. We can reduce the problem to that of finding the distance between the line and an ellipse on the ellipsoid surface by considering the fact that the surface normal at the nearest point to the line will be orthogonal to DIRECT; the set of surface points where this condition holds lies in a plane, and hence is an ellipse on the surface. The problem can be further simplified by projecting the ellipse orthogonally onto the plane defined by < X, DIRECT > = 0. The problem is then a two dimensional one: find the distance of the projected ellipse from the intersection of the line and this plane (which is necessarily one point). A `paraphrase' of the relevant code is: #include "SpiceUsr.h" . . . /. Step 1. Find the candidate ellipse cand. normal is a normal vector to the plane containing the candidate ellipse. The ellipse must exist, since it's the intersection of an ellipsoid centered at the origin and a plane containing the origin. For this reason, we don't check inedpl_c's "found flag" found below. ./ normal[0] = direct[0] / (a*a); normal[1] = direct[1] / (b*b); normal[2] = direct[2] / (c*c); nvc2pl_c ( normal, 0., &candpl ); inedpl_c ( a, b, c, &candpl, cand, &found ); /. Step 2. Project the candidate ellipse onto a plane orthogonal to the line. We'll call the plane prjpl and the projected ellipse prjel. ./ nvc2pl_c ( direct, 0., &prjpl ); pjelpl_c ( &cand, &prjpl, &prjel ); /. Step 3. Find the point on the line lying in the projection plane, and then find the near point pjnear on the projected ellipse. Here prjpt is the point on the input line that lies in the projection plane. The distance between prjpt and pjnear is dist. ./ vprjp_c ( linept, &prjpl, prjpt ); npelpt_c ( &prjel, prjpt, pjnear, &dist ); /. Step 4. Find the near point pnear on the ellipsoid by taking the inverse orthogonal projection of PJNEAR; this is the point on the candidate ellipse that projects to pjnear. Note that the output dist was computed in step 3. The inverse projection of pjnear is guaranteed to exist, so we don't have to check found. ./ vprjpi_c ( pjnear, &prjpl, &candpl, pnear, &found ); /. The value of dist returned is the distance we're looking for. The procedure described here is carried out in the routine npedln_c. ./ -Restrictions None. -Literature_References None. -Author_and_Institution N.J. Bachman (JPL) -Version -CSPICE Version 1.0.0, 02-SEP-1999 (NJB) -Index_Entries project ellipse onto plane -& */ { /* Begin pjelpl_c */ /* Local variables */ SpiceDouble center[3]; SpiceDouble cnst; SpiceDouble normal[3]; SpiceDouble prjctr[3]; SpiceDouble prjvc1[3]; SpiceDouble prjvc2[3]; SpiceDouble smajor[3]; SpiceDouble sminor[3]; /* Participate in error tracing. */ chkin_c ( "pjelpl_c" ); /* Find generating vectors of the input ellipse. */ el2cgv_c ( elin, center, smajor, sminor ); /* Find a normal vector for the input plane. */ pl2nvc_c ( plane, normal, &cnst ); /* Find the components of the semi-axes that are orthogonal to the input plane's normal vector. The components are generating vectors for the projected plane. */ vperp_c ( smajor, normal, prjvc1 ); vperp_c ( sminor, normal, prjvc2 ); /* Find the projection of the ellipse's center onto the input plane. This is the center of the projected ellipse. In case the last assertion is non-obvious, note that the projection we're carrying out is the composition of a linear mapping (projection to a plane containing the origin and parallel to PLANE) and a translation mapping (adding the closest point to the origin in PLANE to every point), and both linear mappings and translations carry the center of an ellipse to the center of the ellipse's image. Let's state this using mathematical symbols. Let L be a linear mapping and let T be a translation mapping, say T(x) = x + A. Then T ( L ( center + cos(theta)smajor + sin(theta)sminor ) ) = A + L ( center + cos(theta)smajor + sin(theta)sminor ) = A + L (center) + cos(theta) L(smajor) + sin(theta) L(sminor) From the form of this last expression, we see that we have an ellipse centered at A + L (center) = T ( L (center) ) This last term is the image of the center of the original ellipse, as we wished to demonstrate. Now in the case of orthogonal projection onto a plane PL, L can be taken as the orthogonal projection onto a parallel plane PL' containing the origin. Then L is a linear mapping. Let M be the multiple of the normal vector of PL such that M is contained in PL (M is the closest point in PL to the origin). Then the orthogonal projection mapping onto PL, which we will name PRJ, can be defined by PRJ (x) = L (x) + M. So PRJ is the composition of a translation and a linear mapping, as claimed. */ vprjp_c ( center, plane, prjctr ); /* Put together the projected ellipse. */ cgv2el_c ( prjctr, prjvc1, prjvc2, elout ); chkout_c ( "pjelpl_c" ); } /* End pjelpl_c */