1
0

geometry.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. typedef double Matrix[4][4];
  10. typedef struct Point3 Point3;
  11. typedef struct Quaternion Quaternion;
  12. typedef struct Space Space;
  13. struct Point3{
  14. double x, y, z, w;
  15. };
  16. struct Quaternion{
  17. double r, i, j, k;
  18. };
  19. struct Space{
  20. Matrix t;
  21. Matrix tinv;
  22. Space *next;
  23. };
  24. /*
  25. * 3-d point arithmetic
  26. */
  27. Point3 add3(Point3 a, Point3 b);
  28. Point3 sub3(Point3 a, Point3 b);
  29. Point3 neg3(Point3 a);
  30. Point3 div3(Point3 a, double b);
  31. Point3 mul3(Point3 a, double b);
  32. int eqpt3(Point3 p, Point3 q);
  33. int closept3(Point3 p, Point3 q, double eps);
  34. double dot3(Point3 p, Point3 q);
  35. Point3 cross3(Point3 p, Point3 q);
  36. double len3(Point3 p);
  37. double dist3(Point3 p, Point3 q);
  38. Point3 unit3(Point3 p);
  39. Point3 midpt3(Point3 p, Point3 q);
  40. Point3 lerp3(Point3 p, Point3 q, double alpha);
  41. Point3 reflect3(Point3 p, Point3 p0, Point3 p1);
  42. Point3 nearseg3(Point3 p0, Point3 p1, Point3 testp);
  43. double pldist3(Point3 p, Point3 p0, Point3 p1);
  44. double vdiv3(Point3 a, Point3 b);
  45. Point3 vrem3(Point3 a, Point3 b);
  46. Point3 pn2f3(Point3 p, Point3 n);
  47. Point3 ppp2f3(Point3 p0, Point3 p1, Point3 p2);
  48. Point3 fff2p3(Point3 f0, Point3 f1, Point3 f2);
  49. Point3 pdiv4(Point3 a);
  50. Point3 add4(Point3 a, Point3 b);
  51. Point3 sub4(Point3 a, Point3 b);
  52. /*
  53. * Quaternion arithmetic
  54. */
  55. void qtom(Matrix, Quaternion);
  56. Quaternion mtoq(Matrix);
  57. Quaternion qadd(Quaternion, Quaternion);
  58. Quaternion qsub(Quaternion, Quaternion);
  59. Quaternion qneg(Quaternion);
  60. Quaternion qmul(Quaternion, Quaternion);
  61. Quaternion qdiv(Quaternion, Quaternion);
  62. Quaternion qunit(Quaternion);
  63. Quaternion qinv(Quaternion);
  64. double qlen(Quaternion);
  65. Quaternion slerp(Quaternion, Quaternion, double);
  66. Quaternion qmid(Quaternion, Quaternion);
  67. Quaternion qsqrt(Quaternion);
  68. void qball(Rectangle, Mouse *, Quaternion *, void (*)(void), Quaternion *);
  69. /*
  70. * Matrix arithmetic
  71. */
  72. void ident(Matrix);
  73. void matmul(Matrix, Matrix);
  74. void matmulr(Matrix, Matrix);
  75. double determinant(Matrix);
  76. void adjoint(Matrix, Matrix);
  77. double invertmat(Matrix, Matrix);
  78. /*
  79. * Space stack routines
  80. */
  81. Space *pushmat(Space *);
  82. Space *popmat(Space *);
  83. void rot(Space *, double, int);
  84. void qrot(Space *, Quaternion);
  85. void scale(Space *, double, double, double);
  86. void move(Space *, double, double, double);
  87. void xform(Space *, Matrix);
  88. void ixform(Space *, Matrix, Matrix);
  89. void look(Space *, Point3, Point3, Point3);
  90. int persp(Space *, double, double, double);
  91. void viewport(Space *, Rectangle, double);
  92. Point3 xformpoint(Point3, Space *, Space *);
  93. Point3 xformpointd(Point3, Space *, Space *);
  94. Point3 xformplane(Point3, Space *, Space *);
  95. #define radians(d) ((d)*.01745329251994329572)