validate_grid_util.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* To compile: gcc -lm validate_grid_util.c grid_util.c rng.c */
  2. /* To run: ./a.out */
  3. /* Generates a bunch of random points inside a triangle, finds
  4. their index, then finds the center of that triangle. If you
  5. plot starting and ending location in Matlab, you can visually
  6. check to make sure it's working right.
  7. Output format is xi yi zi idx xf yf zf where xi yi zi are the
  8. location of the random point, idx is the index into the grid,
  9. and xf yf zf are the coordinates of the center of that tile.
  10. */
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include "rng.h"
  14. #include "mcell_structs.h"
  15. #include "grid_util.h"
  16. void init_wall(struct wall *w,struct vector3 *v0,struct vector3 *v1,struct vector3 *v2)
  17. {
  18. double f,fx,fy,fz;
  19. struct vector3 vA,vB,vX;
  20. w->vert[0] = v0;
  21. w->vert[1] = v1;
  22. w->vert[2] = v2;
  23. w->edges[0] = NULL;
  24. w->edges[1] = NULL;
  25. w->edges[2] = NULL;
  26. w->nb_walls[0] = NULL;
  27. w->nb_walls[1] = NULL;
  28. w->nb_walls[2] = NULL;
  29. vectorize(v0, v1, &vA);
  30. vectorize(v0, v2, &vB);
  31. cross_prod(&vA , &vB , &vX);
  32. w->area = 0.5 * vect_length(&vX);
  33. /*
  34. w->area = sqrt(0.5 * ( ((v1->x - v0->x)*(v1->x - v0->x)+
  35. (v1->y - v0->y)*(v1->y - v0->y)+
  36. (v1->z - v0->z)*(v1->z - v0->z)) *
  37. ((v2->x - v0->x)*(v2->x - v0->x)+
  38. (v2->y - v0->y)*(v2->y - v0->y)+
  39. (v2->z - v0->z)*(v2->z - v0->z)) +
  40. ((v1->x - v0->x)*(v2->x - v0->x)+
  41. (v1->y - v0->y)*(v2->y - v0->y)+
  42. (v1->z - v0->z)*(v2->z - v0->z)) *
  43. ((v1->x - v0->x)*(v2->x - v0->x)+
  44. (v1->y - v0->y)*(v2->y - v0->y)+
  45. (v1->z - v0->z)*(v2->z - v0->z)) ) );
  46. */
  47. fx = (v1->x - v0->x);
  48. fy = (v1->y - v0->y);
  49. fz = (v1->z - v0->z);
  50. f = 1 / sqrt( fx*fx + fy*fy + fz*fz );
  51. w->unit_u.x = fx * f;
  52. w->unit_u.y = fy * f;
  53. w->unit_u.z = fz * f;
  54. fx = (v2->x - v0->x);
  55. fy = (v2->y - v0->y);
  56. fz = (v2->z - v0->z);
  57. w->normal.x = w->unit_u.y * fz - w->unit_u.z * fy;
  58. w->normal.y = w->unit_u.z * fx - w->unit_u.x * fz;
  59. w->normal.z = w->unit_u.x * fy - w->unit_u.y * fx;
  60. f = 1 / sqrt( w->normal.x*w->normal.x + w->normal.y*w->normal.y + w->normal.z*w->normal.z );
  61. w->normal.x *= f;
  62. w->normal.y *= f;
  63. w->normal.z *= f;
  64. w->unit_v.x = w->normal.y * w->unit_u.z - w->normal.z * w->unit_u.y;
  65. w->unit_v.y = w->normal.z * w->unit_u.x - w->normal.x * w->unit_u.z;
  66. w->unit_v.z = w->normal.x * w->unit_u.y - w->normal.y * w->unit_u.x;
  67. w->d = v0->x * w->normal.x + v0->y * w->normal.y + v0->z * w->normal.z;
  68. w->uv_vert1_u = (w->vert[1]->x - w->vert[0]->x)*w->unit_u.x +
  69. (w->vert[1]->y - w->vert[0]->y)*w->unit_u.y +
  70. (w->vert[1]->z - w->vert[0]->z)*w->unit_u.z;
  71. w->uv_vert2.u = (w->vert[2]->x - w->vert[0]->x)*w->unit_u.x +
  72. (w->vert[2]->y - w->vert[0]->y)*w->unit_u.y +
  73. (w->vert[2]->z - w->vert[0]->z)*w->unit_u.z;
  74. w->uv_vert2.v = (w->vert[2]->x - w->vert[0]->x)*w->unit_v.x +
  75. (w->vert[2]->y - w->vert[0]->y)*w->unit_v.y +
  76. (w->vert[2]->z - w->vert[0]->z)*w->unit_v.z;
  77. w->viz_state = EXCLUDE_OBJ;
  78. }
  79. int main()
  80. {
  81. int seed = 1;
  82. struct surface_grid g;
  83. struct wall w;
  84. struct vector3 v,vcent,wv0,wv1,wv2;
  85. int i,idx;
  86. struct rng_state rng;
  87. rng_init(&rng, seed);
  88. g.n = 5;
  89. g.surface = &w;
  90. wv0.x = 0;
  91. wv0.y = 0;
  92. wv0.z = 0;
  93. wv1.x = 3;
  94. wv1.y = 0;
  95. wv1.z = 0;
  96. wv2.x = -0.5;
  97. wv2.y = 1;
  98. wv2.z = 0;
  99. init_wall(&w,&wv0,&wv1,&wv2);
  100. init_grid_geometry(&g);
  101. for (i=0;i<10000;i++)
  102. {
  103. v.x = 3.5*rng_double(&rng, 3*i)-0.5;
  104. v.y = 1.0*rng_double(&rng, 3*i+1);
  105. v.z = 2.0*rng_double(&rng, 3*i+2)-1.0;
  106. if (v.y > 0 && 3.5*v.y + v.x < 3.0 && v.y+2.0*v.x > 0.0)
  107. {
  108. idx = xyz2grid(&v , &g);
  109. grid2xyz(&g , idx , &vcent);
  110. printf("%6.4f %6.4f %6.4f %3d %6.4f %6.4f %6.4f\n",
  111. v.x,v.y,v.z,idx,vcent.x,vcent.y,vcent.z);
  112. }
  113. }
  114. }