Advertisement
Guest User

Untitled

a guest
Sep 11th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include "tree.h"
  4.  
  5. dot voxel = {.c = {1.0, 1.0, 1.0, 0.0}};
  6.  
  7. void calc_bounding_box (const dot *set, unsigned int n, dot *min, dot *max)
  8. {
  9.     int i;
  10.     min->vect = set[0].vect;
  11.     max->vect = set[0].vect;
  12.  
  13.     for (i=1; i<n; i++)
  14.     {
  15.         min->vect = __builtin_ia32_minps (min->vect, set[i].vect);
  16.         max->vect = __builtin_ia32_maxps (max->vect, set[i].vect);
  17.     }
  18. }
  19.  
  20. dot calc_avg (const dot *set, unsigned int n)
  21. {
  22.     dot res;
  23.     int i;
  24.     __builtin_ia32_xorps (res.vect, res.vect);
  25.     for (i=0; i<n; i++) res.vect += set[i].vect;
  26.     res.vect /= (float)n;
  27.     return res;
  28. }
  29.  
  30. uint8_t get_subspace_idx (dot center, dot d)
  31. {
  32.     uint8_t res = 0;
  33.     int i;
  34.  
  35.     for (i=0; i<N; i++) res |= ((center.c[i] > d.c[i]) ? 1 : 0) << i;
  36.     return res;
  37. }
  38.  
  39. dot align_on_voxel (dot d)
  40. {
  41.     int i;
  42.     dot res;
  43.    
  44.     for (i=0; i<N; i++)
  45.     {
  46.         float tmp = ceilf (d.c[i] / voxel.c[i]);
  47.         res.c[i] = tmp*voxel.c[i];
  48.     }
  49.     return res;
  50. }
  51.  
  52. unsigned int filter_set (const dot *in, dot *out, unsigned int n, uint8_t idx, dot center)
  53. {
  54.     unsigned int counter = 0;
  55.     int i,j;
  56.  
  57.     for (i=0; i<n; i++)
  58.     {
  59.         if (get_subspace_idx (center, in[i]) == idx)
  60.         {
  61.             out[counter].vect =  in[i].vect;
  62.             counter++;
  63.         }
  64.     }
  65.  
  66.     return counter;
  67. }
  68.  
  69. struct node* make_tree (const dot *set, unsigned int n)
  70. {
  71.     struct node *res = malloc (sizeof (struct node));
  72.     res->flags = 0;
  73.  
  74.     if (n > 0)
  75.     {
  76.         res->flags |= 1<<FULL;
  77.         calc_bounding_box (set, n, &(res->bb_min), &(res->bb_max));
  78.     }
  79.  
  80.     if (n <= MAX_DOTS)
  81.     {
  82.         res->flags |= 1<<LEAF;
  83.         leaf_data *leaf = &(res->data.leaf);
  84.         leaf->dots_num = n;
  85.  
  86.         // FIXME: Maybe memcpy?
  87.         int i;
  88.         for (i=0; i<n; i++) leaf->dots[i].vect = set[i].vect;
  89.     }
  90.  
  91.     else
  92.     {
  93.         inner_data *inner = &(res->data.inner);
  94.         dot center = calc_avg (set, n);
  95.         inner->center = align_on_voxel (center);
  96.         int i;
  97.         dot *subset = malloc (sizeof (dot) * n);
  98.         for (i=0; i<NS; i++)
  99.         {
  100.             int sub_n = filter_set (set, subset, n, i, inner->center);
  101.             inner->children[i] = make_tree (subset, sub_n);
  102.         }
  103.         free (subset);
  104.     }
  105.  
  106.     return res;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement