home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / extensions / lib / PEXlib / pl_util.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  3.3 KB  |  117 lines

  1. /* $XConsortium: pl_util.h,v 1.4 92/08/26 13:06:25 mor Exp $ */
  2.  
  3. /******************************************************************************
  4. Copyright 1992 by the Massachusetts Institute of Technology
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, distribute, and sell this software and its
  9. documentation for any purpose is hereby granted without fee, provided that
  10. the above copyright notice appear in all copies and that both that
  11. copyright notice and this permission notice appear in supporting
  12. documentation, and that the name of M.I.T. not be used in advertising or
  13. publicity pertaining to distribution of the software without specific,
  14. written prior permission.  M.I.T. makes no representations about the
  15. suitability of this software for any purpose.  It is provided "as is"
  16. without express or implied warranty.
  17. ******************************************************************************/
  18.  
  19. #define ZERO_TOLERANCE 1.0e-30
  20.  
  21. #define ABS(_x) ((_x) < 0.0 ? -(_x) : (_x))
  22.  
  23. #define NEAR_ZERO(_s) (ABS (_s) < ZERO_TOLERANCE)
  24.  
  25. #define ZERO_MAG(_s) ((_s) < ZERO_TOLERANCE)
  26.  
  27. #define IN_RANGE(_low, _high, _val) ((_val) >= (_low) && (_val) <= (_high))
  28.  
  29. #define MAG_V3(_v) \
  30.     (sqrt ((_v)->x * (_v)->x + (_v)->y * (_v)->y + (_v)->z * (_v)->z))
  31.  
  32. #define MAG_V2(_v) \
  33.     (sqrt ((_v)->x * (_v)->x + (_v)->y * (_v)->y))
  34.  
  35. #define BAD_SUBVOLUME(_volume) \
  36.     (!IN_RANGE (0.0, 1.0, _volume->min.x) || \
  37.      !IN_RANGE (0.0, 1.0, _volume->max.x) || \
  38.      !IN_RANGE (0.0, 1.0, _volume->min.y) || \
  39.      !IN_RANGE (0.0, 1.0, _volume->max.y) || \
  40.      !IN_RANGE (0.0, 1.0, _volume->min.z) || \
  41.      !IN_RANGE (0.0, 1.0, _volume->max.z) || \
  42.      !(_volume->min.x <  _volume->max.x) || \
  43.      !(_volume->min.y <  _volume->max.y) || \
  44.      !(_volume->min.z <= _volume->max.z))
  45.  
  46.  
  47. /*
  48.  * Dot product of v1 * v2
  49.  */
  50.  
  51. #define DOT_PRODUCT(_v1, _v2) \
  52.     ((_v1)->x * (_v2)->x + (_v1)->y * (_v2)->y + (_v1)->z * (_v2)->z)
  53.  
  54.  
  55. /*
  56.  * Cross product of p1p2 x p3p4
  57.  */
  58.  
  59. #define CROSS_PRODUCT(_p1, _p2, _p3, _p4, _v) \
  60. { \
  61.     (_v)->x = (((_p2)->y - (_p1)->y) * ((_p4)->z - (_p3)->z)) - \
  62.               (((_p2)->z - (_p1)->z) * ((_p4)->y - (_p3)->y)); \
  63.     (_v)->y = (((_p2)->z - (_p1)->z) * ((_p4)->x - (_p3)->x)) - \
  64.               (((_p2)->x - (_p1)->x) * ((_p4)->z - (_p3)->z)); \
  65.     (_v)->z = (((_p2)->x - (_p1)->x) * ((_p4)->y - (_p3)->y)) - \
  66.               (((_p2)->y - (_p1)->y) * ((_p4)->x - (_p3)->x)); \
  67. }
  68.  
  69.  
  70. /*
  71.  * Normalize vector and return length
  72.  */
  73.  
  74. #define NORMALIZE_VECTOR(_v, _len) \
  75. { \
  76.     _len = DOT_PRODUCT (_v, _v); \
  77.     _len = sqrt (_len); \
  78.     if (!NEAR_ZERO (_len)) \
  79.     { \
  80.       (_v)->x /= _len; \
  81.       (_v)->y /= _len; \
  82.       (_v)->z /= _len; \
  83.     } \
  84. }
  85.  
  86.  
  87. /*
  88.  * Determine if a point is inside of a view
  89.  */
  90.  
  91. #define POINT3D_IN_VIEW(_point, _view) \
  92.     (_point.x >= _view.clip_limits.min.x && \
  93.      _point.x <= _view.clip_limits.max.x && \
  94.      _point.y >= _view.clip_limits.min.y && \
  95.      _point.y <= _view.clip_limits.max.y && \
  96.      _point.z >= _view.clip_limits.min.z && \
  97.      _point.z <= _view.clip_limits.max.z)
  98.  
  99. #define POINT2D_IN_VIEW(_point, _view) \
  100.     (_point.x >= _view.clip_limits.min.x && \
  101.      _point.x <= _view.clip_limits.max.x && \
  102.      _point.y >= _view.clip_limits.min.y && \
  103.      _point.y <= _view.clip_limits.max.y)
  104.  
  105.  
  106. /*
  107.  * Xlib defines min and max as macros; Must undef to compile pl_util.c
  108.  */
  109.  
  110. #ifdef min
  111. #undef min
  112. #endif
  113.  
  114. #ifdef max
  115. #undef max
  116. #endif
  117.