#ifndef TOOL_H
#define TOOL_H
#include <cassert>
#include "raster.h"
#include "shape.h"
class Tool {
public:
virtual ~Tool() { }
virtual Real get_radius() const = 0; // must be >= 0
virtual Real get_height(Real offset) const = 0; // must be >= 0
virtual Real get_height_polar(Real distance, Real offset, Real angle) const { // must be >= distance
// not exact solution, only for small angles
if (distance < 1e-5) return 1e-5;
Vector2 v(offset, tan(angle)*distance);
return sqrt(distance*distance + v.y*v.y) + get_height(v.len());
}
virtual Shape* create_collision_shape(const Vector3 &pos, const Vector3 &dir) const = 0;
virtual void draw(const Vector3 &pos, const Vector3 &dir) const = 0;
};
class FlatTool: public Tool {
public:
Real radius;
explicit FlatTool(Real radius = 0.5):
radius(radius) { }
Real get_radius() const override
{ return radius; }
Real get_height(Real offset) const override
{ return fabs(offset) <= fabs(radius) ? 0 : INFINITY; }
Real get_height_polar(Real distance, Real offset, Real angle) const override {
if (distance < 1e-5) return 1e-5;
if (!(fabs(offset) <= fabs(radius))) return INFINITY;
Real t2 = tan(angle)*distance; t2 *= t2;
Real r2 = radius*radius - offset*offset;
return t2 <= r2 ? sqrt(distance*distance + t2) : INFINITY;
}
Shape* create_collision_shape(const Vector3 &pos, const Vector3 &dir) const override {
return new CilinderShape(pos, dir, radius);
}
void draw(const Vector3 &pos, const Vector3 &dir) const override;
};
class ConeTool: public Tool {
public:
Real radius;
Real height;
Real cut_radius;
explicit ConeTool(Real radius = 0.5, Real height = 1, Real cut_radius = 0):
radius(radius), height(height), cut_radius(cut_radius) { }
Real get_radius() const override
{ return radius; }
Real get_height(Real offset) const override {
Real r = fabs(radius);
offset = fabs(offset);
if (!(offset < r)) return INFINITY;
Real cr = fabs(cut_radius);
if (offset <= cr) return 0;
Real h = fabs(height);
if (h < precision) return 0;
return height*(offset - cr)/(r - cr);
}
Real get_height_polar(Real distance, Real offset, Real angle) const override {
if (distance < 1e-5) return 1e-5;
offset = fabs(offset);
angle = fabs(angle);
Real r = fabs(radius);
Real cr = fabs(cut_radius);
Real h = fabs(height);
if (h <= precision) cr = r;
if (!(offset <= r)) return INFINITY;
Real t2 = tan(angle)*distance; t2 *= t2;
Real r2 = cr*cr - offset*offset;
if (t2 <= r2) return sqrt(distance*distance + t2);
if (h <= precision) return INFINITY;
Real tt2 = tan(angle)*(distance + h); tt2 *= tt2;
Real rr2 = radius*radius - offset*offset;
if (!(t2 <= rr2)) return INFINITY;
Real c = cos(angle);
Real s = sin(angle);
Real kz = (1 - cr)/h;
Real kz2 = kz*kz;
Real d = distance - cr/kz;
if (d < precision) return INFINITY;
Real A = kz2*c*c - s*s;
Real B = -2*kz2*d*c;
Real C = kz2*d*d - offset*offset;
Real roots[2];
int count = solve(roots, C, B, A);
for(int i = 0; i < count; ++i)
if (roots[i]*c > distance && roots[i] > distance)
return roots[i];
return INFINITY;
}
Shape* create_collision_shape(const Vector3 &pos, const Vector3 &dir) const override {
return new ConeShape(pos, dir, radius, height, cut_radius);
}
void draw(const Vector3 &pos, const Vector3 &dir) const override;
};
class ToolPainter: public Painter {
public:
const Tool *tool;
Vector2 scale;
Real distance;
explicit ToolPainter(const Tool *tool = 0, const Vector2 scale = Vector2(1, 1)):
tool(tool), scale(scale), distance() { }
Real get_min_height() const override
{ return distance; }
Vector2 get_radius_xy() const override {
if (!tool) return Vector2();
Vector2 rxy = get_tool_radius_xy();
return Vector2(rxy.x/scale.x, rxy.y/scale.y);
}
Real get_height_xy(const Vector2 &offset) const override {
if (!tool) return INFINITY;
return get_tool_height_xy(Vector2(offset.x*scale.x, offset.y*scale.y));
}
virtual Vector2 get_tool_radius_xy() const = 0;
virtual Real get_tool_height_xy(const Vector2 &offset) const = 0;
};
class FlatToolPainter: public ToolPainter {
using ToolPainter::ToolPainter; // parent constructors
Vector2 get_tool_radius_xy() const override {
Real r = tool->get_radius();
return Vector2(r, r);
}
Real get_tool_height_xy(const Vector2 &offset) const override
{ return distance + tool->get_height(offset.len()); }
};
class PolarToolPainter: public ToolPainter {
public:
int coord;
explicit PolarToolPainter(const Tool *tool = 0, const Vector2 scale = Vector2(1, 1), int coord = 0):
ToolPainter(tool, scale), coord(coord) { }
Vector2 get_tool_radius_xy() const override {
assert(coord > 0 && coord < 2);
Real r = tool->get_radius();
Vector2 rxy(r, r);
rxy.c[coord] = distance > 1e-5 ? rxy.c[coord]/distance : INFINITY;
return rxy;
}
Real get_tool_height_xy(const Vector2 &offset) const override {
if (!tool) return INFINITY;
assert(coord > 0 && coord < 2);
return tool->get_height_polar(distance, offset.c[1-coord], offset.c[coord]);
}
};
#endif