45a3d13f551f3b8cf0fb73feb3b8eef813e3249e
[antikythera] / src / gear.h
1 #ifndef GEAR_H_
2 #define GEAR_H_
3
4 #include <vector>
5 #include <string>
6 #include <gmath/gmath.h>
7 #include "mesh.h"
8
9 /* distance unit: millimeters
10  * angle unit: radians
11  */
12
13 class Gear;
14 struct GearPin;
15 struct GearSlot;
16
17 struct GearPin {
18         float radius;
19         float height;
20
21         Gear *parent;
22         /* position in polar coordinates on the parent gear */
23         float pos_dist, pos_angle;
24
25         GearSlot *conn_slot;    /* slot connection */
26 };
27
28 struct GearSlot {
29         float radius, length;
30
31         Gear *parent;
32         /* position in polar coordinates on the parent gear */
33         float pos_dist_min, pos_dist_max, pos_angle;
34
35         GearPin *conn_pin;              /* pin connection */
36 };
37
38 class Gear {
39 private:
40         Mesh *mesh;
41
42         mutable Mat4 xform, dir_xform;
43         mutable bool xform_valid;
44         void calc_matrix() const;
45
46         float contour(float u);
47
48 public:
49
50         /* TODO hide most of this shit, especially the stuff which invalidate
51          * the transformation matrices
52          */
53         std::string name;
54         Vec3 pos, axis; // implicitly defines a plane eqn.
55         float pdist;    // derived: distance of plane from origin
56
57         float init_angle;       // initial starting angle
58         float angle;    // current angle of the gear
59
60         int nteeth;             // number of teeth
61
62         float radius;   // total radius of the gear, including teeth
63         float teeth_length;     // how far teeth extend past the radius
64         float thickness;        // thickness of the gear along the Z axis
65
66         float bevel;    // bevel size
67
68         std::vector<GearPin> pins;
69         std::vector<GearSlot> slots;
70
71         Gear();
72         ~Gear();
73
74         // sets the supplied number of teeth, and calculates the radius
75         // of the gear, to achieve the required tooth pitch
76         void set_teeth(int nt, float tooth_pitch);
77         void set_axis(const Vec3 &axis);
78         void set_position(const Vec3 &pos);
79         const Vec3 &get_position() const;
80         Vec3 get_global_position() const;       // taking parent gear into account
81
82         Vec3 get_planar_position() const;       // 2D pos of gear on its plane
83
84         void set_angle(float angle);
85         float get_angle() const;
86
87         float get_vis_rotation() const;
88         const Mat4 &get_matrix() const;
89         const Mat4 &get_dir_matrix() const;
90
91         // returns the angle (in radians) from one tooth to the next
92         float get_angular_pitch() const;
93
94         void draw() const;
95         void draw_wire(float line_width = 1.0f) const;
96
97         bool gen_mesh();
98 };
99
100 #endif  // GEAR_H_