6 Arc::Arc(Point *_p1, Point *_p2, double _radius) :
7 p1(_p1), p2(_p2), radius(fabs(_radius)) {
9 // Find the center of the arc
10 double angp1p2 = p1->angleTo(*p2);
11 double m = p1->distanceTo(*p2)/2.0;
14 dbgPrintf("EEEERRRRRRRRROOOOOOOOORRRRRRRRR!!!!!!!!!!\n");
16 double angcen = acos(m/radius);
17 if (_radius < 0) angcen = -angcen;
18 center = Point(p1->x + radius * cos(angp1p2+angcen),
19 p1->y + radius * sin(angp1p2+angcen));
21 startAngle = center.angleTo(*p1);
22 angle = center.angleTo(*p2) - startAngle;
23 if (angle < -M_PI) angle += 2.0*M_PI;
24 if (angle > +M_PI) angle -= 2.0*M_PI;
26 length = fabs(angle*radius);
29 void Arc::setMaxV(const TrajectoryConstraints &constr) {
31 maxv = fmin(constr.maxv, constr.maxomega * r);
32 maxv = fmin(maxv, sqrt(constr.maxcenacc * r));
36 * not implemented, because this class is not used any more
38 double Arc::getDistance(double time) const {
42 void Arc::getPointAt(double distance, Point *p) {
45 ratio = distance/length;
48 ratio = (length-distance)/length;
50 a = startAngle + ratio*angle;
51 p->x = center.x + radius*cos(a);
52 p->y = center.y + radius*sin(a);
54 void Arc::shortenBy(double distance, Point *newEnd) {
55 getPointAt(-distance, newEnd);
57 angle *= (length-distance)/length;
61 startAngle = startAngle + distance/length*angle;
62 angle *= (length-distance)/length;
68 TrajectorySegment* Arc::splitAt(double distance, Point *newEnd) {
69 if (distance <= 0 || distance >= length)
72 getPointAt(distance, newEnd);
73 Arc *ns = new Arc(*this);
75 double a = distance/length*angle;
82 ns->length -= distance;
87 void Arc::getRefPos(double time, Pos &rp) {
89 double fraction = t/(t2-t1);
90 double distance = (v1 + 0.5*acc*t) * t;
91 double a = startAngle + distance/length*angle;
92 rp.x = center.x + radius*cos(a);
93 rp.y = center.y + radius*sin(a);
95 rp.phi = a + M_PI/2.0;
97 rp.phi = a - M_PI/2.0;
99 rp.v = v1+fraction*(v2-v1);
100 rp.omega =rp.v/radius;
101 if (angle < 0) rp.omega = -rp.omega;
104 #ifdef MATLAB_MEX_FILE
105 void Arc::plot(const char *style) {
110 mxArray *x = mxCreateDoubleMatrix(1, len, mxREAL);
111 mxArray *y = mxCreateDoubleMatrix(1, len, mxREAL);
112 mxArray *s = mxCreateCharMatrixFromStrings(1, &style);
113 mxArray *rhs[] = {x,y,s};
115 for (i=0; i < len; i++) {
116 getRefPos(t1+(t2-t1)*i/(len-1), rp);
117 mxGetPr(x)[i] = rp.x;
118 mxGetPr(y)[i] = rp.y;
120 mexCallMATLAB(0, NULL, 3, rhs, "plot");
121 sprintf(cmd, "plot([%g %g], [%g %g], 'mo')",
122 p1->x, p2->x, p1->y, p2->y);
130 } // namespace Segment