polygon.hpp 6.2 KB

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