00001
00002
00003
00004
00005
00006
00007 #include "EngaugeAssert.h"
00008 #include "FormatCoordsUnits.h"
00009 #include "GeometryStrategyAbstractBase.h"
00010 #include <qmath.h>
00011 #include <QPointF>
00012 #include "Spline.h"
00013 #include "SplinePair.h"
00014 #include "Transformation.h"
00015 #include <vector>
00016
00017 using namespace std;
00018
00019 GeometryStrategyAbstractBase::GeometryStrategyAbstractBase()
00020 {
00021 }
00022
00023 GeometryStrategyAbstractBase::~GeometryStrategyAbstractBase()
00024 {
00025 }
00026
00027 void GeometryStrategyAbstractBase::calculatePositionsGraph (const Points &points,
00028 const Transformation &transformation,
00029 QVector<QPointF> &positionsGraph) const
00030 {
00031 positionsGraph.clear();
00032
00033 for (int i = 0; i < points.size(); i++) {
00034 const Point &pointScreen = points [i];
00035 QPointF posScreen = pointScreen.posScreen ();
00036 QPointF posGraph;
00037
00038 transformation.transformScreenToRawGraph (posScreen,
00039 posGraph);
00040
00041 positionsGraph.push_back (posGraph);
00042 }
00043 }
00044
00045 double GeometryStrategyAbstractBase::functionArea (const QVector<QPointF> &positionsGraph) const
00046 {
00047
00048 double sum = 0, xLast = 0, yLast = 0;
00049 for (int i = 1; i < positionsGraph.size (); i++) {
00050 double x = positionsGraph [i].x();
00051 double y = positionsGraph [i].y();
00052 double area = 0.5 * (y + yLast) * (x - xLast);
00053 sum += area;
00054 xLast = x;
00055 yLast = y;
00056 }
00057
00058 return sum;
00059 }
00060
00061 void GeometryStrategyAbstractBase::insertSubintervalsAndLoadDistances (int subintervalsPerInterval,
00062 const QVector<QPointF> &positionsGraph,
00063 QVector<QPointF> &positionsGraphWithSubintervals,
00064 QVector<QString> &distanceGraphForward,
00065 QVector<QString> &distancePercentForward,
00066 QVector<QString> &distanceGraphBackward,
00067 QVector<QString> &distancePercentBackward) const
00068 {
00069 if (positionsGraph.size () > 0) {
00070
00071 int i;
00072
00073
00074 vector<double> t;
00075 vector<SplinePair> xy;
00076 for (int i = 0; i < positionsGraph.size (); i++) {
00077 t.push_back ((double) i);
00078 xy.push_back (SplinePair (positionsGraph [i].x(),
00079 positionsGraph [i].y()));
00080 }
00081
00082 Spline spline (t,
00083 xy);
00084
00085
00086 QVector<double> distanceGraphDouble;
00087 double xLast = 0, yLast = 0, distance = 0;
00088 for (i = 0; i < positionsGraph.size(); i++) {
00089
00090
00091 for (int subinterval = 0; subinterval < subintervalsPerInterval; subinterval++) {
00092
00093
00094 double t = (double) (i - 1.0) + (double) (subinterval + 1) / (double) (subintervalsPerInterval);
00095
00096 SplinePair splinePair = spline.interpolateCoeff (t);
00097
00098 double x = splinePair.x ();
00099 double y = splinePair.y ();
00100
00101
00102 if (i > 0 || subinterval == subintervalsPerInterval - 1) {
00103
00104
00105 positionsGraphWithSubintervals.push_back (QPointF (x, y));
00106
00107 }
00108
00109 if (i > 0) {
00110
00111
00112 distance += qSqrt ((x - xLast) * (x - xLast) + (y - yLast) * (y - yLast));
00113
00114 }
00115
00116 xLast = x;
00117 yLast = y;
00118 }
00119
00120
00121 distanceGraphDouble.push_back (distance);
00122 }
00123
00124
00125 double dTotal = qMax (1.0, distanceGraphDouble [distanceGraphDouble.size() - 1]);
00126 for (i = 0; i < distanceGraphDouble.size (); i++) {
00127 double d = distanceGraphDouble [i];
00128 distanceGraphForward.push_back (QString::number (d));
00129 distancePercentForward.push_back (QString::number (100.0 * d / dTotal));
00130 distanceGraphBackward.push_back (QString::number (dTotal - d));
00131 distancePercentBackward.push_back (QString::number (100.0 * (dTotal - d) / dTotal));
00132 }
00133 }
00134 }
00135
00136 void GeometryStrategyAbstractBase::loadXY (const QVector<QPointF> &positionsGraph,
00137 const DocumentModelCoords &modelCoords,
00138 const DocumentModelGeneral &modelGeneral,
00139 const MainWindowModel &modelMainWindow,
00140 const Transformation &transformation,
00141 QVector<QString> &x,
00142 QVector<QString> &y) const
00143 {
00144 FormatCoordsUnits formatCoordsUnits;
00145
00146 for (int i = 0; i < positionsGraph.size(); i++) {
00147
00148 double xI = positionsGraph [i].x();
00149 double yI = positionsGraph [i].y();
00150
00151 QString xFormatted, yFormatted;
00152 formatCoordsUnits.unformattedToFormatted (xI,
00153 yI,
00154 modelCoords,
00155 modelGeneral,
00156 modelMainWindow,
00157 xFormatted,
00158 yFormatted,
00159 transformation);
00160 x.push_back (xFormatted);
00161 y.push_back (yFormatted);
00162
00163 }
00164 }
00165
00166 double GeometryStrategyAbstractBase::polygonAreaForSimplyConnected (const QVector<QPointF> &points) const
00167 {
00168
00169 int N = points.size ();
00170
00171 double sum = 0.0;
00172 if (N > 0) {
00173
00174
00175 for (int i = 0; i < N - 1; i++) {
00176 sum += points [i].x() * points [i + 1].y() - points [i + 1].x() * points [i].y();
00177 }
00178
00179 sum += points [N - 1].x() * points [0].y() - points [0].x() * points [N - 1].y ();
00180 }
00181
00182 return qAbs (sum) / 2.0;
00183 }