1 // Copyright 2009 Michal Sojka <sojkam1@fel.cvut.cz>
2 // Copyright 2009 Petr Beneš
4 // This file is part of Trgen library.
6 // Trgen is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // Trgen is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Trgen. If not, see <http://www.gnu.org/licenses/>.
25 Line::Line(Point *_p1, Point *_p2) : p1(_p1), p2(_p2) {
26 length = p1->distanceTo(*p2);
27 angle = p1->angleTo(*p2);
28 sinphi = (p2->y-p1->y)/length;
29 cosphi = (p2->x-p1->x)/length;
34 void Line::setMaxV(const TrajectoryConstraints &constr) {
40 double Line::getDistance(double time) const {
42 return (0.5*acc*time*time + v1*time);
45 void Line::getPointAt(double distance, Point *p) {
48 ratio = distance/length;
50 ratio = (length+distance)/length;
52 p->x = p1->x + ratio*(p2->x - p1->x);
53 p->y = p1->y + ratio*(p2->y - p1->y);
56 void Line::shortenBy(double distance, Point *newEnd) {
57 getPointAt(-distance, newEnd);
59 if (distance > 0) { // end cut off
61 } else { // beginning cut off
68 TrajectorySegment* Line::splitAtByTime(double time, Point *newEnd) {
69 if (time <= t1 || time >= t2) {
70 dbgPrintf("splitAt: time=%g length=%g\n", time, length);
74 double dst = 0.5 * acc * time * time + v1 * time; // where to split from beginning
75 getPointAt(dst, newEnd);
76 Line *ns = new Line(*this);
82 ns->v1 = v2 = v1 + acc*time; // need to update speeds
87 TrajectorySegment* Line::splitAt(double distance, Point *newEnd) {
88 if (distance <= 0 || distance >= length) {
89 dbgPrintf("splitAt: distance=%g length=%g\n", distance, length);
93 getPointAt(distance, newEnd);
94 Line *ns = new Line(*this);
96 double orig_length = length;
98 ns->length -= distance;
101 // s=1/2 a t2 t2 = 2as t=sqrt(2as) time
102 v2 = v1 + ( v2- v1) * ( length/orig_length); // need to update speeds
103 ns->v1 = ns->v2 - (ns->v2-ns->v1) * (ns->length/orig_length);
108 void Line::getRefPos(double time, Pos &rp) {
110 double fraction = t/(t2-t1);
111 double distance = (v1 + 0.5*acc*t) * t;
112 rp.x = p1->x + distance*cosphi;
113 rp.y = p1->y + distance*sinphi;
116 rp.v = v1+fraction*(v2-v1);
120 #ifdef MATLAB_MEX_FILE
121 void Line::plot(const char *style) {
123 sprintf(cmd, "plot([%g %g], [%g %g], '%so')",
124 p1->x, p2->x, p1->y, p2->y, style);
129 } // namespace Segment