geometry.h 3.0 KB

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