pkgeomd.h

Go to the documentation of this file.
00001 
00002 /* ///////////////////////////////////////////////////////////////////////// */
00003 /* This file is a part of the BSTools package                                */
00004 /* written by Przemyslaw Kiciak                                              */
00005 /* ///////////////////////////////////////////////////////////////////////// */
00006 /* (C) Copyright by Przemyslaw Kiciak, 2005, 2013                            */
00007 /* this package is distributed under the terms of the                        */
00008 /* Lesser GNU Public License, see the file COPYING.LIB                       */
00009 /* ///////////////////////////////////////////////////////////////////////// */
00010 
00011 /* Header file for the libgeom library of C procedures - */
00012 /* basic 2D, 3D and 4D affine geometry                   */ 
00013 
00014 #ifndef PKGEOMD_H
00015 #define PKGEOMD_H
00016 
00017 #ifndef PKVARIA_H
00018 #include "pkvaria.h"
00019 #endif
00020 
00021 #ifdef __cplusplus   
00022 extern "C" {
00023 #endif
00024 
00025 typedef union trans2d {
00026   struct {
00027     /* affine transformation of a 2D space  */
00028     double a11, a12, a13,   /* coefficients of the transformation   */
00029            a21, a22, a23;
00030   } U0;
00031   struct {
00032     double a[2][3];
00033     short detsgn;   /* sign of the determinant : -1, 0 or 1 */
00034   } U1;
00035 } trans2d;
00036 
00037 typedef struct Box2d {
00038     double x0, x1, y0, y1;
00039   } Box2d;
00040 
00041 typedef union trans3d {
00042   struct {
00043     /* affine transformation of a 3D space  */
00044     double a11, a12, a13, a14,   /* coefficients of the transformation   */
00045            a21, a22, a23, a24,
00046            a31, a32, a33, a34;
00047   } U0;
00048   struct {
00049     double a[3][4];
00050     short detsgn;   /* sign of the determinant : -1, 0 or 1 */
00051   } U1;
00052 } trans3d;
00053 
00054 typedef struct Box3d {
00055     double x0, x1, y0, y1, z0, z1;
00056   } Box3d;
00057 
00058 typedef struct ray3d {
00059   /* ray, ie. a halfline in 3D space */
00060   point3d  p;  /* origin point of the ray */
00061   vector3d v;  /* direction               */
00062 } ray3d;
00063 
00064 typedef vector4d quaterniond;
00065 
00066 typedef struct {
00067     double amin, amax;
00068     short  code;
00069   } ConvexCone2d;
00070 
00071 /* /////////////////////////////////////////////////////////////////////////// */
00072 
00073 void SetPoint2d ( point2d *p, double x, double y );
00074 #define SetVector2d(v,x,y) SetPoint2d ( v, x, y )
00075 
00076 void TransPoint2d ( const trans2d *tr, const point2d *p, point2d *q );
00077 void TransVector2d ( const trans2d *tr, const vector2d *p, vector2d *q );
00078 void IdentTrans2d ( trans2d *tr );
00079 void CompTrans2d ( trans2d *s, trans2d *t, trans2d *u );
00080 void ShiftTrans2d ( trans2d *tr, double tx, double ty );
00081 void RotTrans2d ( trans2d *tr, double angle );
00082 void ScaleTrans2d ( trans2d *tr, double sx, double sy );
00083 void Trans2Shiftd ( trans2d *tr, double tx, double ty );
00084 void Trans2Rotd ( trans2d *tr, double angle );
00085 void Trans2Scaled ( trans2d *tr, double sx, double sy );
00086 boolean InvertTrans2d ( trans2d *tr );
00087 
00088 void MultVector2d ( double a, const vector2d *v, vector2d *w );
00089 void SubtractPoints2d ( const point2d *p1, const point2d *p2, vector2d *v );
00090 void AddVector2d ( const point2d *p, const vector2d *v, point2d *q );
00091 void AddVector2Md ( const point2d *p, const vector2d *v, double t, point2d *q );
00092 void InterPoint2d ( const point2d *p1, const point2d *p2, double t, point2d *q );
00093 void MidPoint2d ( const point2d *p1, const point2d *p2, point2d *q );
00094 void Interp3Vectors2d ( const vector2d *p0, const vector2d *p1, const vector2d *p2,
00095                         const double *coeff, vector2d *p );
00096 void NormalizeVector2d ( vector2d *v );
00097 
00098 double DotProduct2d ( const vector2d *v1, const vector2d *v2 );
00099 double det2d ( const vector2d *v1, const vector2d *v2 );
00100 void OrtVector2d ( const vector2d *v1, const vector2d *v2, vector2d *v );
00101 
00102 void ProjectPointOnLine2d ( const point2d *p0, const point2d *p1,
00103                             point2d *q );
00104 
00105 double Point2Distanced ( point2d *p, point2d *q );
00106 
00107 short ExtendConvexCone2d ( ConvexCone2d *cone, double a );
00108 short ExtendConvexCone2dv ( ConvexCone2d *cone, vector2d *v );
00109 boolean InsideConvexCone2d ( ConvexCone2d *cone, double a );
00110 
00111 /* /////////////////////////////////////////////////////////////////////////// */
00112 void Trans2Point3d ( const trans2d *tr, const point3d *p, point3d *q );
00113 
00114 /* /////////////////////////////////////////////////////////////////////////// */
00115 
00116 void SetPoint3d ( point3d *p, double x, double y, double z );
00117 #define SetVector3d(v,x,y,z) SetPoint3d ( v, x, y, z )
00118 
00119 void Point3to2d ( const point3d *P, point2d *p );
00120 void Point2to3d ( const point2d *p, double w, point3d *P );
00121 void Point4to2d ( const point4d *P, point2d *p );
00122 void Point2to4d ( const point2d *p, float w, point4d *P );
00123 
00124 void TransPoint3d ( const trans3d *tr, const point3d *p, point3d *q );
00125 void TransVector3d ( const trans3d *tr, const vector3d *v, vector3d *w );
00126 void TransContra3d ( const trans3d *tri, const vector3d *v, vector3d *w );
00127 void Trans3Point2d ( const trans3d *tr, const point2d *p, point2d *q );
00128 void Trans3Point4d ( const trans3d *tr, const point4d *p, point4d *q );
00129 
00130 void Trans3Shiftd ( trans3d *tr, double tx, double ty, double tz );
00131 void Trans3Mirrord ( trans3d *tr, vector3d *n );
00132 void Trans3Rotd ( trans3d *tr, byte j, byte k, double angle );
00133 #define Trans3RotXd(tr,angle) Trans3Rotd ( tr, 2, 3, angle )
00134 #define Trans3RotYd(tr,angle) Trans3Rotd ( tr, 3, 1, angle )
00135 #define Trans3RotZd(tr,angle) Trans3Rotd ( tr, 1, 2, angle )
00136 void Trans3RotVd ( trans3d *tr, vector3d *v, double angle );
00137 void Trans3Scaled ( trans3d *tr, double sx, double sy, double sz );
00138 
00139 void IdentTrans3d ( trans3d *tr );
00140 void CompTrans3d ( trans3d *s, trans3d *t, trans3d *u );
00141 void GeneralAffineTrans3d ( trans3d *tr, vector3d *v1, vector3d *v2,
00142                             vector3d *v3 );
00143 void ShiftTrans3d ( trans3d *tr, double tx, double ty, double tz );
00144 void MirrorTrans3d ( trans3d *tr, vector3d *n );
00145 
00146 void RotTrans3d ( trans3d *tr, byte j, byte k, double angle );
00147 void EulerRotTrans3d ( trans3d *tr, double psi, double theta, double phi );
00148 #define RotXTrans3d(tr,angle) RotTrans3d ( tr, 2, 3, angle )
00149 #define RotYTrans3d(tr,angle) RotTrans3d ( tr, 3, 1, angle )
00150 #define RotZTrans3d(tr,angle) RotTrans3d ( tr, 1, 2, angle )
00151 
00152 void FindRotVEulerd ( const vector3d *v, double angle,
00153                       double *psi, double *theta, double *phi );
00154 void RotVTrans3d ( trans3d *tr, vector3d *v, double angle );
00155 void ScaleTrans3d ( trans3d *tr, double sx, double sy, double sz );
00156 boolean InvertTrans3d ( trans3d *tr );
00157 double TrimAngled ( double angle );
00158 void CompEulerRotd ( double psi1, double theta1, double phi1, double psi2,
00159                      double theta2, double phi2, double *psi, double *theta,
00160                      double *phi );
00161 void CompRotV3d ( const vector3d *v1, double a1, const vector3d *v2, double a2,
00162                   vector3d *v, double *a );
00163 
00164 void MultVector3d ( double a, const vector3d *v, vector3d *w );
00165 void SubtractPoints3d ( const point3d *p1, const point3d *p2, vector3d *v );
00166 void AddVector3d ( const point3d *p, const vector3d *v, point3d *q );
00167 void AddVector3Md ( const point3d *p, const vector3d *v, double t,
00168                     point3d *q );
00169 void InterPoint3d ( const point3d *p1, const point3d *p2, double t,
00170                     point3d *q );
00171 void MidPoint3d ( const point3d *p1, const point3d *p2, point3d *q );
00172 void Interp3Vectors3d ( const vector3d *p0, const vector3d *p1, const vector3d *p2,
00173                         const double *coeff, vector3d *p );
00174 void NormalizeVector3d ( vector3d *v );
00175 
00176 double DotProduct3d ( const vector3d *v1, const vector3d *v2 );
00177 void CrossProduct3d ( const vector3d *v1, const vector3d *v2, vector3d *v );
00178 double det3d ( const vector3d *v1, const vector3d *v2, const vector3d *v3 );
00179 void OrtVector3d ( const vector3d *v1, const vector3d *v2, vector3d *v );
00180 
00181 void ProjectPointOnLine3d ( const point3d *p0, const point3d *p1,
00182                             point3d *q );
00183 void ProjectPointOnPlane3d ( const point3d *p0, const point3d *p1, const point3d *p2,
00184                              point3d *q );
00185 
00186 double Point3Distanced ( point3d *p, point3d *q );
00187 
00188 /* /////////////////////////////////////////////////////////////////////////// */
00189 void SetPoint4d ( point4d *p, double X, double Y, double Z, double W );
00190 #define SetVector4d(v,X,Y,Z,W) SetPoint4d ( v, X, Y, Z, W )
00191 
00192 void Point4to3d ( const point4d *P, point3d *p );
00193 void Point3to4d ( const point3d *p, double w, point4d *P );
00194 
00195 void SubtractPoints4d ( const point4d *p1, const point4d *p2, vector4d *v );
00196 void AddVector4d ( const point4d *p, const vector4d *v, point4d *q );
00197 void AddVector4Md ( const point4d *p, const vector4d *v, double t,
00198                     point4d *q );
00199 void MultVector4d ( double a, const vector4d *v, vector4d *w );
00200 
00201 void InterPoint4d ( const point4d *p1, const point4d *p2, double t,
00202                     point4d *q );
00203 void MidPoint4d ( const point4d *p1, const point4d *p2, point4d *q );
00204 void Interp3Vectors4d ( const vector4d *p0, const vector4d *p1, const vector4d *p2,
00205                         const double *coeff, vector4d *p );
00206 void NormalizeVector4d ( vector4d *v );
00207 
00208 double det4d ( const vector4d *v0, const vector4d *v1,
00209                const vector4d *v2, const vector4d *v3 );
00210 void CrossProduct4d (const vector4d *v0, const vector4d *v1,
00211                      const vector4d *v2, vector4d *v );
00212 void CrossProduct4P3d ( const vector4d *v0, const vector4d *v1,
00213                         const vector4d *v2, vector3d *v );
00214 double DotProduct4d ( const vector4d *v0, const vector4d *v1 );
00215 void OutProduct4P3d ( const vector4d *v0, const vector4d *v1, vector3d *v );
00216 
00217 void OrtVector4d ( const vector4d *v1, const vector4d *v2, vector4d *v );
00218 
00219 void ProjectPointOnLine4d ( const point4d *p0, const point4d *p1,
00220                             point4d *q );
00221 void ProjectPointOnPlane4d ( const point4d *p0, const point4d *p1, const point4d *p2,
00222                              point4d *q );
00223 
00224 double pkg_LineRayDist3d ( ray3d *ray, point3d *q0, point3d *q1,
00225                            double *rparam, double *lparam,
00226                            point3d *rp, point3d *lp );
00227 
00228 /* /////////////////////////////////////////////////////////////////////////// */
00229 #define SetQuaterniond(q,a,x,y,z) SetVector4d ( q, x, y, z, a )
00230 #define AddQuaternionsd AddVectors4d
00231 
00232 void QuaternionMultd ( quaterniond *q1, quaterniond *q2, quaterniond *q );
00233 void QuaternionMultInvd ( quaterniond *q1, quaterniond *q2, quaterniond *q );
00234 void QuaternionInvMultd ( quaterniond *q1, quaterniond *q2, quaterniond *q );
00235 
00236 void QuaternionRotTrans3d ( trans3d *tr, quaterniond *q );
00237 void Trans3QuaternionRotd ( trans3d *tr, quaterniond *q );
00238 
00239 #ifdef __cplusplus
00240 }
00241 #endif
00242 
00243 #endif /*PKGEOMD_H*/
00244