home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iritsm3s.zip / cagd_lib / cagdsrev.c < prev    next >
C/C++ Source or Header  |  1991-05-19  |  3KB  |  95 lines

  1. /******************************************************************************
  2. * CagdSRev.c - Surface of revolution out of a given profile.              *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Mar. 91.                          *
  5. ******************************************************************************/
  6.  
  7. #ifdef __MSDOS__
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <alloc.h>
  11. #endif /* __MSDOS__ */
  12.  
  13. #include "cagd_loc.h"
  14.  
  15. static int CircKnotVector[12] = { 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4 };
  16.  
  17. /******************************************************************************
  18. * Constructs a surface of revolution around the Z axis of the given profile   *
  19. * curve. Resulting surface will be a Bspline surface, while input may be      *
  20. * either a Bspline or a Bezier curve. Curve must be 3d (I.e. P3 or E3).          *
  21. ******************************************************************************/
  22. CagdSrfStruct *CagdSurfaceRev(CagdCrvStruct *Crv)
  23. {
  24.     int i, j, i9,
  25.     Len = Crv -> Length;
  26.     CagdPointType
  27.     PType = Crv -> PType;
  28.     CagdRType **SrfPoints,
  29.     sin45 = sin( M_PI / 4.0 ),
  30.     **CrvPoints = Crv -> Points;
  31.     CagdSrfStruct
  32.     *Srf = BspSrfNew(9, Len, 3, Crv -> Order, CAGD_PT_P3_TYPE);
  33.  
  34.     for (i = 0; i < 12; i++)
  35.     Srf -> UKnotVector[i] = (CagdRType) CircKnotVector[i];
  36.  
  37.     switch (Crv -> GType) {
  38.     case CAGD_CBEZIER_TYPE:
  39.         BspKnotUniformOpen(Len, Crv -> Order, Srf -> VKnotVector);
  40.         break;
  41.     case CAGD_CBSPLINE_TYPE:
  42.         GEN_COPY(Srf -> VKnotVector, Crv -> KnotVector, sizeof(CagdRType) *
  43.                                  (Len + Crv -> Order));
  44.         break;
  45.     case CAGD_CPOWER_TYPE:
  46.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  47.         return NULL;
  48.     default:
  49.         FATAL_ERROR(CAGD_ERR_UNDEF_CRV);
  50.         return NULL;
  51.     }
  52.  
  53.     SrfPoints = Srf -> Points;
  54.  
  55.     /* For each control point in the original curve - generate 9 points that */
  56.     /* Form a circle perpendicular to the Z axis.                 */
  57.     for (i = i9 = 0; i < Len; i++, i9 += 9) {
  58.     SrfPoints[W][i9] = 1.0;
  59.     switch (PType) {
  60.         case CAGD_PT_P3_TYPE:
  61.         SrfPoints[W][i9] = CrvPoints[W][i];
  62.         case CAGD_PT_E3_TYPE:
  63.         SrfPoints[X][i9] = CrvPoints[X][i];
  64.         SrfPoints[Y][i9] = CrvPoints[Y][i];
  65.         SrfPoints[Z][i9] = CrvPoints[Z][i];
  66.         break;
  67.         default:
  68.         FATAL_ERROR(CAGD_ERR_UNSUPPORT_PT);
  69.         break;
  70.     }
  71.  
  72.     /* Last point is exactly the same as first one in circle - copy it.  */
  73.     for (j = W; j <= Z; j++) SrfPoints[j][i9 + 8] = SrfPoints[j][i9];
  74.  
  75.     /* The Z components are identical in all circle, while the XY        */
  76.     /* components are rotated 45 degrees at a time:                 */
  77.     for (j = 1; j < 8; j++) {
  78.         SrfPoints[W][i9 + j] = SrfPoints[W][i9];
  79.         SrfPoints[X][i9 + j] = SrfPoints[X][i9 + j - 1] * sin45 -
  80.                    SrfPoints[Y][i9 + j - 1] * sin45;
  81.         SrfPoints[Y][i9 + j] = SrfPoints[X][i9 + j - 1] * sin45 +
  82.                    SrfPoints[Y][i9 + j - 1] * sin45;
  83.         SrfPoints[Z][i9 + j] = SrfPoints[Z][i9];
  84.     }
  85.  
  86.     /* And finally we need to compensate for the W's on every second pt. */
  87.     for (j = 1; j < 8; j += 2) {
  88.         SrfPoints[W][i9 + j] *= sin45;
  89.         SrfPoints[Z][i9 + j] *= sin45;
  90.     }
  91.     }
  92.  
  93.     return Srf;
  94. }
  95.