chiark / gitweb /
found in ijackson@chiark:things/moebius.old-before-cvs
[moebius.git] / library.hh
1 /*
2  * Points library
3  */
4
5 #ifndef POINTS_HH
6 #define POINTS_HH
7
8 #include <stdlib.h>
9 #include <math.h>
10
11 /*
12  * Coordinate system:
13  *
14  *          Y |
15  *            |
16  *            |
17  *            /-------- X
18  *           /
19  *          /
20  *       Z (coming out of the monitor)
21  */
22
23 struct Onscreen {
24   double x, y;
25   Onscreen(){};
26   Onscreen(double ix, double iy) { x=ix; y=iy; }
27 };
28
29 class Point;
30
31 class Transform {
32 public:
33   virtual Point operator()(Point)= 0;
34 };
35
36 class TransformList {
37   Transform **a;
38   int size,used;
39   void clearcontents();
40  public:
41   TransformList() { size=used=0; a=0; }
42   ~TransformList() { clearcontents(); free(a); }
43   void reset() { clearcontents(); used=0; }
44   Point operator()(Point);
45   void append(Transform*);
46 };
47
48 class Point {
49   static TransformList usertransforms;
50   static TransformList povtransform;
51   static double planedistance, eyedistance, cutoffdistance, eyex;
52   double xyz[3];
53 public:
54   Point(){}
55   Point(double x, double y, double z) { xyz[0]=x; xyz[1]=y; xyz[2]=z; }
56   Point operator+(Point r) { return Point(xyz[0]+r[0], xyz[1]+r[1], xyz[2]+r[2]); }
57   Point operator-(Point r) { return Point(xyz[0]-r[0], xyz[1]-r[1], xyz[2]-r[2]); }
58   Point operator*(double f) { return Point(xyz[0]*f, xyz[1]*f, xyz[2]*f); }
59   double& operator[](int i) { return xyz[i]; }
60   double magnitude() { return sqrt(xyz[0]*xyz[0]+xyz[1]*xyz[1]+xyz[2]*xyz[2]); }
61   const char *printing();
62
63   operator Onscreen() const;
64   Point transformed() const { return povtransform(usertransforms(*this)); }
65   double index() const { return transformed()[2]; }
66
67   static int indexvisible(double index) { return index < cutoffdistance; }
68   static void appendtransform(Transform *t) { usertransforms.append(t); }
69   static void cleartransform() { usertransforms.reset(); }
70   static void setobserver(double theta, double eta=0.0,
71                           double planedist=1.0, double eyedist=1.0,
72                           double cutoff=10.0);
73   static void seteyex(double n) { eyex=n; }
74 };
75
76 #endif