store facade ptr in c'tor, save a param in sub-sequent function calls

This commit is contained in:
Dennis Luxen 2014-05-18 12:37:53 +02:00
parent ef206eb4d9
commit a47467f29b
5 changed files with 97 additions and 13 deletions

View File

@ -329,3 +329,92 @@ void FixedPointCoordinate::Output(std::ostream &out) const
{
out << "(" << lat / COORDINATE_PRECISION << "," << lon / COORDINATE_PRECISION << ")";
}
// double PointSegmentDistanceSquared( double px, double py,
// double p1x, double p1y,
// double p2x, double p2y,
// double& t,
// double& qx, double& qy)
// {
// static const double kMinSegmentLenSquared = 0.00000001; // adjust to suit. If you use float, you'll probably want something like 0.000001f
// static const double kEpsilon = 1.0E-14; // adjust to suit. If you use floats, you'll probably want something like 1E-7f
// double dx = p2x - p1x;
// double dy = p2y - p1y;
// double dp1x = px - p1x;
// double dp1y = py - p1y;
// const double segLenSquared = (dx * dx) + (dy * dy);
// if (segLenSquared >= -kMinSegmentLenSquared && segLenSquared <= kMinSegmentLenSquared)
// {
// // segment is a point.
// qx = p1x;
// qy = p1y;
// t = 0.0;
// return ((dp1x * dp1x) + (dp1y * dp1y));
// }
// else
// {
// // Project a line from p to the segment [p1,p2]. By considering the line
// // extending the segment, parameterized as p1 + (t * (p2 - p1)),
// // we find projection of point p onto the line.
// // It falls where t = [(p - p1) . (p2 - p1)] / |p2 - p1|^2
// t = ((dp1x * dx) + (dp1y * dy)) / segLenSquared;
// if (t < kEpsilon)
// {
// // intersects at or to the "left" of first segment vertex (p1x, p1y). If t is approximately 0.0, then
// // intersection is at p1. If t is less than that, then there is no intersection (i.e. p is not within
// // the 'bounds' of the segment)
// if (t > -kEpsilon)
// {
// // intersects at 1st segment vertex
// t = 0.0;
// }
// // set our 'intersection' point to p1.
// qx = p1x;
// qy = p1y;
// // Note: If you wanted the ACTUAL intersection point of where the projected lines would intersect if
// // we were doing PointLineDistanceSquared, then qx would be (p1x + (t * dx)) and qy would be (p1y + (t * dy)).
// }
// else if (t > (1.0 - kEpsilon))
// {
// // intersects at or to the "right" of second segment vertex (p2x, p2y). If t is approximately 1.0, then
// // intersection is at p2. If t is greater than that, then there is no intersection (i.e. p is not within
// // the 'bounds' of the segment)
// if (t < (1.0 + kEpsilon))
// {
// // intersects at 2nd segment vertex
// t = 1.0;
// }
// // set our 'intersection' point to p2.
// qx = p2x;
// qy = p2y;
// // Note: If you wanted the ACTUAL intersection point of where the projected lines would intersect if
// // we were doing PointLineDistanceSquared, then qx would be (p1x + (t * dx)) and qy would be (p1y + (t * dy)).
// }
// else
// {
// // The projection of the point to the point on the segment that is perpendicular succeeded and the point
// // is 'within' the bounds of the segment. Set the intersection point as that projected point.
// qx = p1x + (t * dx);
// qy = p1y + (t * dy);
// }
// // return the squared distance from p to the intersection point. Note that we return the squared distance
// // as an optimization because many times you just need to compare relative distances and the squared values
// // works fine for that. If you want the ACTUAL distance, just take the square root of this value.
// double dpqx = px - qx;
// double dpqy = py - qy;
// return ((dpqx * dpqx) + (dpqy * dpqy));
// }
// }
// public float DistanceOfPointToLine2(PointF p1, PointF p2, PointF p)
// {
// // (y1-y2)x + (x2-x1)y + (x1y2-x2y1)
// //d(P,L) = --------------------------------
// // sqrt( (x2-x1)pow2 + (y2-y1)pow2 )
// double ch = (p1.Y - p2.Y) * p.X + (p2.X - p1.X) * p.Y + (p1.X * p2.Y - p2.X * p1.Y);
// double del = Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
// double d = ch / del;
// return (float)d;
// }

View File

@ -51,12 +51,11 @@ struct DescriptorConfig
template <class DataFacadeT> class BaseDescriptor
{
public:
BaseDescriptor() {} //TODO: initialize facade here.
BaseDescriptor() {}
// Maybe someone can explain the pure virtual destructor thing to me (dennis)
virtual ~BaseDescriptor() {}
virtual void Run(const RawRouteData &raw_route,
const PhantomNodes &phantom_nodes,
DataFacadeT *facade,
http::Reply &reply) = 0;
virtual void SetConfig(const DescriptorConfig &config) = 0;
};

View File

@ -35,6 +35,7 @@ template <class DataFacadeT> class GPXDescriptor : public BaseDescriptor<DataFac
private:
DescriptorConfig config;
FixedPointCoordinate current;
DataFacadeT * facade;
void AddRoutePoint(const FixedPointCoordinate & coordinate, std::vector<char> & output)
{
@ -56,12 +57,13 @@ template <class DataFacadeT> class GPXDescriptor : public BaseDescriptor<DataFac
}
public:
GPXDescriptor(DataFacadeT *facade) : facade(facade) {}
void SetConfig(const DescriptorConfig &c) { config = c; }
// TODO: reorder parameters
void Run(const RawRouteData &raw_route,
const PhantomNodes &phantom_node_list,
DataFacadeT *facade,
http::Reply &reply)
{
std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"

View File

@ -72,7 +72,7 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
public:
JSONDescriptor() : facade(nullptr), entered_restricted_area_count(0)
JSONDescriptor(DataFacadeT *facade) : facade(facade), entered_restricted_area_count(0)
{
shortest_leg_end_indices.emplace_back(0);
alternative_leg_end_indices.emplace_back(0);
@ -98,12 +98,9 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
void Run(const RawRouteData &raw_route,
const PhantomNodes &phantom_nodes,
// TODO: move facade initalization to c'tor
DataFacadeT *f,
http::Reply &reply)
{
JSON::Object json_result;
facade = f;
if (INVALID_EDGE_WEIGHT == raw_route.shortest_path_length)
{
@ -147,7 +144,6 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
BuildTextualDescription(description_factory,
json_route_instructions,
raw_route.shortest_path_length,
facade,
shortest_path_segments);
json_result.values["route_instructions"] = json_route_instructions;
}
@ -209,7 +205,6 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
BuildTextualDescription(alternate_description_factory,
json_current_alt_instructions,
raw_route.alternative_path_length,
facade,
alternative_path_segments);
json_alt_instructions.values.push_back(json_current_alt_instructions);
json_result.values["alternative_instructions"] = json_alt_instructions;
@ -276,7 +271,6 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
inline void BuildTextualDescription(DescriptionFactory &description_factory,
JSON::Array & json_instruction_array,
const int route_length,
const DataFacadeT *facade,
std::vector<Segment> &route_segments_list)
{
// Segment information has following format:

View File

@ -160,13 +160,13 @@ template <class DataFacadeT> class ViaRoutePlugin : public BasePlugin
// descriptor = std::make_shared<JSONDescriptor<DataFacadeT>>();
// break;
case 1:
descriptor = std::make_shared<GPXDescriptor<DataFacadeT>>();
descriptor = std::make_shared<GPXDescriptor<DataFacadeT>>(facade);
break;
// case 2:
// descriptor = std::make_shared<GEOJSONDescriptor<DataFacadeT>>();
// break;
default:
descriptor = std::make_shared<JSONDescriptor<DataFacadeT>>();
descriptor = std::make_shared<JSONDescriptor<DataFacadeT>>(facade);
break;
}
@ -175,7 +175,7 @@ template <class DataFacadeT> class ViaRoutePlugin : public BasePlugin
phantom_nodes.target_phantom = raw_route.segment_end_coordinates.back().target_phantom;
descriptor->SetConfig(descriptor_config);
TIMER_START(descriptor);
descriptor->Run(raw_route, phantom_nodes, facade, reply);
descriptor->Run(raw_route, phantom_nodes, reply);
TIMER_STOP(descriptor);
SimpleLogger().Write() << "descriptor took " << TIMER_MSEC(descriptor) << "ms";
}