transform.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * The following routines transform points and planes from one space
  11. * to another. Points and planes are represented by their
  12. * homogeneous coordinates, stored in variables of type Point3.
  13. */
  14. #include <u.h>
  15. #include <libc.h>
  16. #include <draw.h>
  17. #include <geometry.h>
  18. /*
  19. * Transform point p.
  20. */
  21. Point3 xformpoint(Point3 p, Space *to, Space *from){
  22. Point3 q, r;
  23. register double *m;
  24. if(from){
  25. m=&from->t[0][0];
  26. q.x=*m++*p.x; q.x+=*m++*p.y; q.x+=*m++*p.z; q.x+=*m++*p.w;
  27. q.y=*m++*p.x; q.y+=*m++*p.y; q.y+=*m++*p.z; q.y+=*m++*p.w;
  28. q.z=*m++*p.x; q.z+=*m++*p.y; q.z+=*m++*p.z; q.z+=*m++*p.w;
  29. q.w=*m++*p.x; q.w+=*m++*p.y; q.w+=*m++*p.z; q.w+=*m *p.w;
  30. }
  31. else
  32. q=p;
  33. if(to){
  34. m=&to->tinv[0][0];
  35. r.x=*m++*q.x; r.x+=*m++*q.y; r.x+=*m++*q.z; r.x+=*m++*q.w;
  36. r.y=*m++*q.x; r.y+=*m++*q.y; r.y+=*m++*q.z; r.y+=*m++*q.w;
  37. r.z=*m++*q.x; r.z+=*m++*q.y; r.z+=*m++*q.z; r.z+=*m++*q.w;
  38. r.w=*m++*q.x; r.w+=*m++*q.y; r.w+=*m++*q.z; r.w+=*m *q.w;
  39. }
  40. else
  41. r=q;
  42. return r;
  43. }
  44. /*
  45. * Transform point p with perspective division.
  46. */
  47. Point3 xformpointd(Point3 p, Space *to, Space *from){
  48. p=xformpoint(p, to, from);
  49. if(p.w!=0){
  50. p.x/=p.w;
  51. p.y/=p.w;
  52. p.z/=p.w;
  53. p.w=1;
  54. }
  55. return p;
  56. }
  57. /*
  58. * Transform plane p -- same as xformpoint, except multiply on the
  59. * other side by the inverse matrix.
  60. */
  61. Point3 xformplane(Point3 p, Space *to, Space *from){
  62. Point3 q, r;
  63. register double *m;
  64. if(from){
  65. m=&from->tinv[0][0];
  66. q.x =*m++*p.x; q.y =*m++*p.x; q.z =*m++*p.x; q.w =*m++*p.x;
  67. q.x+=*m++*p.y; q.y+=*m++*p.y; q.z+=*m++*p.y; q.w+=*m++*p.y;
  68. q.x+=*m++*p.z; q.y+=*m++*p.z; q.z+=*m++*p.z; q.w+=*m++*p.z;
  69. q.x+=*m++*p.w; q.y+=*m++*p.w; q.z+=*m++*p.w; q.w+=*m *p.w;
  70. }
  71. else
  72. q=p;
  73. if(to){
  74. m=&to->t[0][0];
  75. r.x =*m++*q.x; r.y =*m++*q.x; r.z =*m++*q.x; r.w =*m++*q.x;
  76. r.x+=*m++*q.y; r.y+=*m++*q.y; r.z+=*m++*q.y; r.w+=*m++*q.y;
  77. r.x+=*m++*q.z; r.y+=*m++*q.z; r.z+=*m++*q.z; r.w+=*m++*q.z;
  78. r.x+=*m++*q.w; r.y+=*m++*q.w; r.z+=*m++*q.w; r.w+=*m *q.w;
  79. }
  80. else
  81. r=q;
  82. return r;
  83. }