home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / 3d / irit / misc_lib / dist_pts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-10  |  3.6 KB  |  91 lines

  1. /******************************************************************************
  2. * Distribute N points in a one/two dimensional domain, so as to minimize      *
  3. * their energy according to a given energy function.                  *
  4. *                                          *
  5. *                      Written by Gershon Elber, February 1994 *
  6. ******************************************************************************/
  7.  
  8. #include <math.h>
  9. #include "irit_sm.h"
  10. #include "imalloc.h"
  11. #include "dist_pts.h"
  12.  
  13. /*****************************************************************************
  14. * DESCRIPTION:                                                               M
  15. * Distributes N points with a given energy in the region in the X line that  M
  16. * is bounded by XMin, XMax.                             M
  17. *   Energy is specified via the EnergyFunc that recieves the X location.     M
  18. *   Resolution * N specifies how many samples to take from EnergyFunc.         M
  19. *   Returns an array of N distributed points.                     M
  20. *   The solution to the distribution is analythic provided EnergyFunc can be M
  21. * integrated. Herein, this integral is computed nomerically.             M
  22. *                                                                            *
  23. * PARAMETERS:                                                                M
  24. *   N:           Number of points to distribute,                             M
  25. *   XMin:        Minimum of domain to distribute points.                     M
  26. *   XMax:        Minimum of domain to distribute points.                     M
  27. *   Resolution:  Fineness of integral calculation.                           M
  28. *   EnergyFunc:  Energy function to use.                             M
  29. *                                                                            *
  30. * RETURN VALUE:                                                              M
  31. *   RealType *:  A vector of N points distributed as requested.              M
  32. *                                                                            *
  33. * KEYWORDS:                                                                  M
  34. *   DistPoint1DWithEnergy, point distribution                                M
  35. *****************************************************************************/
  36. RealType *DistPoint1DWithEnergy(int N,
  37.                 RealType XMin,
  38.                 RealType XMax,
  39.                 int Resolution,
  40.                 DistEnergy1DFuncType EnergyFunc)
  41. {
  42.     int i, j, 
  43.     IntegN = N * Resolution;
  44.     RealType X, *R, IntegMaxVal, IntegDelta, IntegVal, Dx, DxN, *Integ, *Dist;
  45.  
  46.     if (N < 2)
  47.     IritFatalError("DistPoint1DWithEnergy: N must be at least 2.");
  48.  
  49.     N = N < 2 ? 2 : N,
  50.     IntegN = N * Resolution;
  51.     Dx = XMax - XMin;
  52.     DxN = Dx / IntegN;
  53.     Integ = (RealType *) IritMalloc((IntegN + 2) * sizeof(RealType));
  54.     Dist = (RealType *) IritMalloc(N * sizeof(RealType));
  55.  
  56.     /* Compute first order approximation of the integral of energy function. */
  57.     Integ[0] = 0.0;
  58.     for (i = 1, R = &Integ[1], X = XMin + DxN / 2;
  59.      i < IntegN + 2;
  60.      i++, R++, X += DxN) {
  61.     RealType
  62.         E = EnergyFunc(X);
  63.  
  64.     *R = R[-1] + MAX(E, 0.0);         /* Do not allow negative energy... */
  65.     }
  66.  
  67.     if ((IntegMaxVal = Integ[IntegN]) < EPSILON) {
  68.     /* No real energy in the data. Apply uniform distribution. */
  69.     for (i = 1, R = &Integ[1]; i < IntegN + 2; i++)
  70.         *R++ = i;
  71.     IntegMaxVal = Integ[IntegN];
  72.     }
  73.  
  74.     IntegDelta = IntegMaxVal / (N - 1);
  75.     IntegVal = 0.0;
  76.  
  77.     /* Copy the points to the returned data structure. */
  78.     for (i = 0, j = 0; i < N; i++) {
  79.     while (IntegVal >= Integ[j])
  80.         j++;
  81.     X = j - 1 + (IntegVal - Integ[j - 1]) / (Integ[j] - Integ[j - 1]);
  82.     IntegVal += IntegDelta;
  83.  
  84.     Dist[i] = XMin + Dx * X / IntegN;
  85.     }
  86.  
  87.     IritFree((VoidPtr) Integ);
  88.  
  89.     return Dist;
  90. }
  91.