polygon.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef POLYGON_HPP
  2. #define POLYGON_HPP
  3. #include <cstring>
  4. #include <cassert>
  5. #include <stack>
  6. #include <map>
  7. #include "config.hpp"
  8. #include "avx_matrix.hpp"
  9. #include "coeffs.hpp"
  10. #include "polygon_step.hpp"
  11. using namespace std;
  12. //***********
  13. //* Polygon *
  14. //***********
  15. //! Class for self avoiding polygon in the square lattice
  16. //! Polygon are drown on a grid and hace length bounded
  17. //! by max_len. The grid have a border prevent the polygon
  18. //! to go too far. A typical example of the grid is
  19. //! ##########
  20. //! # #
  21. //! ####S #
  22. //! ##########
  23. //! where S is the starting point and # is border.
  24. //! Polygon will grow applyng step. A grid case contains
  25. //! contains the path distance from the starting point.
  26. //! We store the unique id of a step in an historic grid.
  27. //! In order to perfoem backward without clearing the grid,
  28. //! we associate the step id to the corresponding path
  29. //! distance. Hence the case (i,j) of the grid contribute
  30. //! to the polygon if grid[(i,j)]=t and histo_grid[t]=s
  31. //! and step_histo[s]=t.
  32. class Polygon{
  33. public:
  34. //! Width of the grid
  35. static const size_t grid_width=max_len-1;
  36. //! Height of the grid
  37. static const size_t grid_height=max_len/2+2;
  38. //! Size of the grid
  39. static const size_t grid_size=grid_width*grid_height;
  40. //! Position of the starting point
  41. static const int64 xinit=max_len/2-2;
  42. static const int64 yinit=1;
  43. //! Border value
  44. static const size_t border=0;
  45. //! Empty value
  46. static const size_t empty=-1;
  47. //! The living grid the polygon
  48. size_t grid[grid_size];
  49. //! The history grid of the polygon
  50. size_t grid_histo[grid_size];
  51. //! The history step of the polygon
  52. size_t step_histo[max_len+1];
  53. //! Head poistion on the grid
  54. size_t head;
  55. //! Lenght of the polygon
  56. size_t length;
  57. //! Empty constructor
  58. Polygon();
  59. //! Copy operator
  60. Polygon& operator=(const Polygon& P);
  61. //! Init data of the polygon
  62. void init();
  63. //! Display the polygon
  64. void display() const;
  65. //! Apply a step to the polygon
  66. //! \param s step to apply
  67. void apply(Step& s);
  68. //! Test is the case at position ind is admissible
  69. //! as the next case of the polygon; this at path
  70. //! length t.
  71. //! \param t path_length of the next case
  72. //! \param ind index of the case to test
  73. //! \param s last step applied to the polygon
  74. bool test(size_t t,size_t ind,Step* s) const;
  75. //! Test if the polygon is closed
  76. bool is_closed() const;
  77. //! Return indice of the next vertex of the polygon
  78. //! \param t path length of the current vertex
  79. //! \param c current vertex indice
  80. size_t next_vertex(size_t t,size_t c) const;
  81. //! Return the fp coefficient of the polygon
  82. Reel fp() const;
  83. //------------------
  84. // Static functions
  85. //------------------
  86. //! Return x coordinate of the case with indice ind.
  87. //! \param ind indice of the case
  88. static int64 x(size_t ind);
  89. //! Return y coordinate of the case with indice ind.
  90. //! \param ind indice of the case
  91. static int64 y(size_t ind);
  92. //! Return indice of the case on the left of case with
  93. //! indice ind.
  94. //! \param ind indice of the case
  95. static size_t left(size_t ind);
  96. //! Return indice of the case on the right of case with
  97. //! indice ind.
  98. //! \param ind indice of the case
  99. static size_t right(size_t ind);
  100. //! Return indice of the case on the up of case with
  101. //! indice ind.
  102. //! \param ind indice of the case
  103. static size_t up(size_t ind);
  104. //! Return indice of the case on the down of case with
  105. //! indice ind.
  106. //! \param ind indice of the case
  107. static size_t down(size_t ind);
  108. //! Return indice of the case at position (x,y).
  109. //! \param x x-coordinate of the case
  110. //! \prama y y-coordiante of the case
  111. static size_t pos(size_t x,size_t y);
  112. };
  113. //**************
  114. //* InfoVertex *
  115. //**************
  116. //! This class is used to obtained the adjacence matrix
  117. //! of the haired polygon.
  118. class InfoVertex{
  119. public:
  120. //! Indice of the vertex
  121. size_t id;
  122. //! Coordinates of the vertex
  123. int64 x,y;
  124. //! Degree of the vertex
  125. size_t deg;
  126. //! Neighbhorhoods of the vertex
  127. size_t v[4];
  128. //! Empty constructor (not explicety used)
  129. InfoVertex();
  130. //! Constructor for a vertex with not yet detected neighbhors.
  131. //! \param id indice of the vertex
  132. //! \param x x-coordinate of the vertex
  133. //! \param y y-coordinate of the vertex
  134. InfoVertex(size_t id,int64 x,int64 y);
  135. //! Constructor for a vertex with four identified neighbhors.
  136. //! \param id indice of the vertex
  137. //! \param x x-coordinate of the vertex
  138. //! \param y y-coordinate of the vertex
  139. //! \param l indice of the vertex on the left
  140. //! \param r indice of the vertex on the right
  141. //! \param u indice of the vertex on the up
  142. //! \param d indice of the vertex on the dwon
  143. InfoVertex(size_t id,int64 x,int64 y,size_t l,size_t r,size_t u,size_t d);
  144. };
  145. //********************
  146. //* Inline functions *
  147. //********************
  148. //---------
  149. // Polygon
  150. //---------
  151. inline
  152. Polygon::Polygon(){
  153. }
  154. inline bool
  155. Polygon::test(size_t t,size_t ind,Step* s) const{
  156. size_t v=grid[ind];
  157. if(v>s->t) return true;
  158. return grid_histo[ind]!=s->histo[v];
  159. }
  160. inline void
  161. Polygon::apply(Step& step){
  162. length=step.t;
  163. grid[head=step.newh]=step.t;
  164. grid_histo[head]=step.num();
  165. memcpy(step_histo,step.histo,(step.t+1)*sizeof(size_t));
  166. }
  167. inline bool
  168. Polygon::is_closed() const{
  169. return grid[down(head)]==1;
  170. }
  171. //----------------
  172. // Static Polygon
  173. //----------------
  174. inline size_t
  175. Polygon::left(size_t ind){
  176. return ind-1;
  177. }
  178. inline size_t
  179. Polygon::right(size_t ind){
  180. return ind+1;
  181. }
  182. inline size_t
  183. Polygon::up(size_t ind){
  184. return ind+grid_width;
  185. }
  186. inline size_t
  187. Polygon::down(size_t ind){
  188. return ind-grid_width;
  189. }
  190. inline int64
  191. Polygon::x(size_t ind){
  192. return ind%grid_width;
  193. }
  194. inline int64
  195. Polygon::y(size_t ind){
  196. return ind/grid_width;
  197. }
  198. inline size_t
  199. Polygon::pos(size_t x,size_t y){
  200. return x+y*grid_width;
  201. }
  202. //------------
  203. // InfoVertex
  204. //------------
  205. inline
  206. InfoVertex::InfoVertex(){
  207. }
  208. inline
  209. InfoVertex::InfoVertex(size_t _id,int64 _x,int64 _y):
  210. id(_id),x(_x),y(_y),deg(0){
  211. }
  212. inline
  213. InfoVertex::InfoVertex(size_t _id,int64 _x,int64 _y,size_t l,size_t r,size_t u,size_t d):
  214. id(_id),x(_x),y(_y),deg(4),v{l,r,u,d}{
  215. }
  216. #endif