Advertisement
Guest User

sinosoidal

a guest
Nov 25th, 2011
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. /*
  2. * pmap_quad_rect: find mapping between quadrilateral and rectangle.
  3. * The correspondence is:
  4. *
  5. * quad[0] --> (u0,v0)
  6. * quad[1] --> (u1,v0)
  7. * quad[2] --> (u1,v1)
  8. * quad[3] --> (u0,v1)
  9. *
  10. * This method of computing the adjoint numerically is cheaper than
  11. * computing it symbolically.
  12. */
  13.  
  14. int pmap_quad_rect(double u0, double v0, double u1, double v1, double quad[4][2], double QR[3][3])
  15. {
  16. int ret;
  17. double du, dv;
  18. double RQ[3][3]; /* rect->quad transform */
  19.  
  20. du = u1-u0;
  21. dv = v1-v0;
  22. if (du==0. || dv==0.) {
  23. //ERROR("pmap_quad_rect: null rectangle\n");
  24. return PMAP_BAD;
  25. }
  26.  
  27. /* first find mapping from unit uv square to xy quadrilateral */
  28. ret = pmap_square_quad(quad, RQ);
  29.  
  30. qDebug() << RQ[0][0] << RQ[0][1] << RQ[0][2];
  31. qDebug() << RQ[1][0] << RQ[1][1] << RQ[1][2];
  32. qDebug() << RQ[2][0] << RQ[2][1] << RQ[2][2];
  33.  
  34. if (ret==PMAP_BAD) return PMAP_BAD;
  35.  
  36. /* concatenate transform from uv rectangle (u0,v0,u1,v1) to unit square */
  37. RQ[0][0] /= du;
  38. RQ[1][0] /= dv;
  39. RQ[2][0] -= RQ[0][0]*u0 + RQ[1][0]*v0;
  40. RQ[0][1] /= du;
  41. RQ[1][1] /= dv;
  42. RQ[2][1] -= RQ[0][1]*u0 + RQ[1][1]*v0;
  43. RQ[0][2] /= du;
  44. RQ[1][2] /= dv;
  45. RQ[2][2] -= RQ[0][2]*u0 + RQ[1][2]*v0;
  46.  
  47. qDebug() << RQ[0][0] << RQ[0][1] << RQ[0][2];
  48. qDebug() << RQ[1][0] << RQ[1][1] << RQ[1][2];
  49. qDebug() << RQ[2][0] << RQ[2][1] << RQ[2][2];
  50.  
  51. /* now RQ is transform from uv rectangle to xy quadrilateral */
  52. /* QR = inverse transform, which maps xy to uv */
  53. if (mx3d_adjoint(RQ, QR)==0.)
  54. {
  55. qDebug() << "pmap_quad_rect: warning: determinant=0";
  56. //ERROR("pmap_quad_rect: warning: determinant=0\n");
  57. }
  58. return ret;
  59. }
  60.  
  61. /*
  62. * pmap_square_quad: find mapping between unit square and quadrilateral.
  63. * The correspondence is:
  64. *
  65. * (0,0) --> quad[0]
  66. * (1,0) --> quad[1]
  67. * (1,1) --> quad[2]
  68. * (0,1) --> quad[3]
  69. */
  70.  
  71. int pmap_square_quad(double quad[4][2], double SQ[3][3])
  72. {
  73. double px, py;
  74.  
  75. px = X(0)-X(1)+X(2)-X(3);
  76. py = Y(0)-Y(1)+Y(2)-Y(3);
  77.  
  78. qDebug() << "px: " << px << "py: " << py;
  79.  
  80. if (ZERO(px) && ZERO(py)){ /* affine */
  81. qDebug() << "affine";
  82. SQ[0][0] = X(1)-X(0);
  83. SQ[1][0] = X(2)-X(1);
  84. SQ[2][0] = X(0);
  85. SQ[0][1] = Y(1)-Y(0);
  86. SQ[1][1] = Y(2)-Y(1);
  87. SQ[2][1] = Y(0);
  88. SQ[0][2] = 0.;
  89. SQ[1][2] = 0.;
  90. SQ[2][2] = 1.;
  91. return PMAP_AFFINE;
  92. }
  93. else { /* projective */
  94. qDebug() << "projective";
  95. double dx1, dx2, dy1, dy2, del;
  96.  
  97. dx1 = X(1)-X(2);
  98. dx2 = X(3)-X(2);
  99. dy1 = Y(1)-Y(2);
  100. dy2 = Y(3)-Y(2);
  101. del = DET2(dx1,dx2, dy1,dy2);
  102. if (del==0.) {
  103. //ERROR("pmap_square_quad: bad mapping\n");
  104. return PMAP_BAD;
  105. }
  106. SQ[0][2] = DET2(px,dx2, py,dy2)/del;
  107. SQ[1][2] = DET2(dx1,px, dy1,py)/del;
  108. SQ[2][2] = 1.;
  109. SQ[0][0] = X(1)-X(0)+SQ[0][2]*X(1);
  110. SQ[1][0] = X(3)-X(0)+SQ[1][2]*X(3);
  111. SQ[2][0] = X(0);
  112. SQ[0][1] = Y(1)-Y(0)+SQ[0][2]*Y(1);
  113. SQ[1][1] = Y(3)-Y(0)+SQ[1][2]*Y(3);
  114. SQ[2][1] = Y(0);
  115. return PMAP_PROJECTIVE;
  116. }
  117. }
  118.  
  119. int pmap_poly(poly *p, double ST[3][3])
  120. {
  121. double scr[4][2]; /* vertices of screen quadrilateral */
  122.  
  123. //if (p->n!=4)
  124. //ERROR("only do quadrilaterals at the moment\n");
  125.  
  126. /* if edges 0-1 and 2-3 are horz, 1-2 and 3-0 are vert */
  127. if (V(0)==V(1) && V(2)==V(3) && U(1)==U(2) && U(3)==U(0)) {
  128. qDebug() << " if edges 0-1 and 2-3 are horz, 1-2 and 3-0 are vert";
  129. scr[0][0] = X(0); scr[0][1] = Y(0);
  130. scr[1][0] = X(1); scr[1][1] = Y(1);
  131. scr[2][0] = X(2); scr[2][1] = Y(2);
  132. scr[3][0] = X(3); scr[3][1] = Y(3);
  133. return pmap_quad_rect(U(0), V(0), U(2), V(2), scr, ST);
  134. }
  135.  
  136. /* if edges 0-1 and 2-3 are vert, 1-2 and 3-0 are horz */
  137. else if (U(0)==U(1) && U(2)==U(3) && V(1)==V(2) && V(3)==V(0)) {
  138. qDebug() << "if edges 0-1 and 2-3 are vert, 1-2 and 3-0 are horz";
  139. scr[0][0] = X(1); scr[0][1] = Y(1);
  140. scr[1][0] = X(2); scr[1][1] = Y(2);
  141. scr[2][0] = X(3); scr[2][1] = Y(3);
  142. scr[3][0] = X(0); scr[3][1] = Y(0);
  143. return pmap_quad_rect(U(1), V(1), U(3), V(3), scr, ST);
  144. }
  145.  
  146. /* if texture is not an orthogonally-oriented rectangle */
  147. else
  148. {
  149. qDebug() << "if texture is not an orthogonally-oriented rectangle";
  150. return pmap_quad_quad(p, ST);
  151. }
  152. }
  153.  
  154. /*
  155. * pmap_quad_quad: find the projective mapping ST from screen to texture space
  156. * given the screen and texture coordinates at the vertices of a quadrilateral.
  157. *
  158. * Method: concatenate screen_quad->mid_square and
  159. * mid_square->texture_quad transform matrices.
  160. * The alternate method, solving an 8x8 system of equations, takes longer.
  161. */
  162.  
  163. int pmap_quad_quad(poly *p, double ST[3][3])
  164. {
  165. int type1, type2;
  166. double quad[4][2], MS[3][3];
  167. double SM[3][3], MT[3][3]; /* screen->mid and mid->texture */
  168.  
  169. quad[0][0] = X(0); quad[0][1] = Y(0);
  170. quad[1][0] = X(1); quad[1][1] = Y(1);
  171. quad[2][0] = X(2); quad[2][1] = Y(2);
  172. quad[3][0] = X(3); quad[3][1] = Y(3);
  173. type1 = pmap_square_quad(quad, MS);
  174. if (mx3d_adjoint(MS, SM) == 0.)
  175. {
  176. //ERROR("pmap_quad_quad: warning: determinant=0\n");
  177. }
  178.  
  179. quad[0][0] = U(0); quad[0][1] = V(0);
  180. quad[1][0] = U(1); quad[1][1] = V(1);
  181. quad[2][0] = U(2); quad[2][1] = V(2);
  182. quad[3][0] = U(3); quad[3][1] = V(3);
  183. type2 = pmap_square_quad(quad, MT);
  184.  
  185. if (type1==PMAP_BAD || type2==PMAP_BAD) return PMAP_BAD;
  186.  
  187. mx3d_mul(SM, MT, ST);
  188.  
  189. return type1|type2; /* PMAP_PROJECTIVE prevails */
  190. }
  191.  
  192. double mx3d_adjoint(double a[3][3], double b[3][3])
  193. {
  194. b[0][0] = DET2(a[1][1], a[1][2], a[2][1], a[2][2]);
  195. b[1][0] = DET2(a[1][2], a[1][0], a[2][2], a[2][0]);
  196. b[2][0] = DET2(a[1][0], a[1][1], a[2][0], a[2][1]);
  197. b[0][1] = DET2(a[2][1], a[2][2], a[0][1], a[0][2]);
  198. b[1][1] = DET2(a[2][2], a[2][0], a[0][2], a[0][0]);
  199. b[2][1] = DET2(a[2][0], a[2][1], a[0][0], a[0][1]);
  200. b[0][2] = DET2(a[0][1], a[0][2], a[1][1], a[1][2]);
  201. b[1][2] = DET2(a[0][2], a[0][0], a[1][2], a[1][0]);
  202. b[2][2] = DET2(a[0][0], a[0][1], a[1][0], a[1][1]);
  203.  
  204. qDebug() << b[0][0] << b[0][1] << b[0][2];
  205. qDebug() << b[1][0] << b[1][1] << b[1][2];
  206. qDebug() << b[2][0] << b[2][1] << b[2][2];
  207.  
  208. return a[0][0]*b[0][0] + a[0][1]*b[0][1] + a[0][2]*b[0][2];
  209. }
  210.  
  211. /* mx3_mul: matrix multiply: c = a*b */
  212.  
  213. void mx3d_mul(double a[3][3], double b[3][3], double c[3][3])
  214. {
  215. c[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0] + a[0][2]*b[2][0];
  216. c[0][1] = a[0][0]*b[0][1] + a[0][1]*b[1][1] + a[0][2]*b[2][1];
  217. c[0][2] = a[0][0]*b[0][2] + a[0][1]*b[1][2] + a[0][2]*b[2][2];
  218. c[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0] + a[1][2]*b[2][0];
  219. c[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1] + a[1][2]*b[2][1];
  220. c[1][2] = a[1][0]*b[0][2] + a[1][1]*b[1][2] + a[1][2]*b[2][2];
  221. c[2][0] = a[2][0]*b[0][0] + a[2][1]*b[1][0] + a[2][2]*b[2][0];
  222. c[2][1] = a[2][0]*b[0][1] + a[2][1]*b[1][1] + a[2][2]*b[2][1];
  223. c[2][2] = a[2][0]*b[0][2] + a[2][1]*b[1][2] + a[2][2]*b[2][2];
  224. }
  225.  
  226. /* mx3d_transform: transform point p by matrix a: q = p*a */
  227.  
  228. void mx3d_transform(double p[3], double a[3][3], double q[3])
  229. {
  230. q[0] = p[0]*a[0][0] + p[1]*a[1][0] + p[2]*a[2][0];
  231. q[1] = p[0]*a[0][1] + p[1]*a[1][1] + p[2]*a[2][1];
  232. q[2] = p[0]*a[0][2] + p[1]*a[1][2] + p[2]*a[2][2];
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement