1 #include "GridGraphicsItem.h"
8 #include <QGraphicsScene>
13 #include "GroundGrid.h"
14 #include "IException.h"
16 #include "Longitude.h"
17 #include "MosaicGraphicsView.h"
18 #include "MosaicSceneWidget.h"
19 #include "Projection.h"
20 #include "TProjection.h"
21 #include "UniversalGroundMap.h"
26 GridGraphicsItem::GridGraphicsItem(Latitude baseLat, Longitude baseLon,
27 Angle latInc, Angle lonInc, MosaicSceneWidget *projectionSrc,
28 int density, Latitude latMin, Latitude latMax,
29 Longitude lonMin, Longitude lonMax) {
33 if (latInc > Angle(0.0, Angle::Degrees) && lonInc > Angle(0.0, Angle::Degrees)) {
35 Projection *proj = projectionSrc->getProjection();
36 Projection::ProjectionType pType = proj->projectionType();
38 if (proj && pType == Projection::Triaxial && lonMin < lonMax && latMin < latMax) {
39 TProjection *tproj = (TProjection *) proj;
40 PvlGroup mappingGroup(tproj->Mapping());
48 if (tproj->IsPositiveWest()) {
52 Longitude temp = lonMin;
56 if (mappingGroup[
"LatitudeType"][0] ==
"Planetographic") {
58 Distance equaRad(tproj->EquatorialRadius(), Distance::Meters);
59 Distance polRad(tproj->PolarRadius(), Distance::Meters);
61 minLat = Latitude(latMin.planetographic(Angle::Degrees), mappingGroup,
63 maxLat = Latitude(latMax.planetographic(Angle::Degrees), mappingGroup,
65 baseLat = Latitude(baseLat.degrees(), equaRad, polRad,
66 Latitude::Planetocentric, Angle::Degrees);
69 if (!qFuzzyCompare(latInc.radians(), 0.0)) {
74 while (startLat < minLat) {
75 startLat = startLat.add(latInc, mappingGroup);
78 catch (IException &) {
82 while (startLat.add(latInc * -1, mappingGroup) >= minLat) {
83 startLat = startLat.add(latInc * -1, mappingGroup);
86 catch (IException &) {
95 while (endLat > maxLat) {
96 endLat = endLat.add(latInc * -1, mappingGroup);
99 catch (IException &) {
104 while (endLat.add(latInc, mappingGroup) <= maxLat) {
105 endLat = endLat.add(latInc, mappingGroup);
108 catch (IException &) {
113 minLat = Latitude(latMin.degrees(), mappingGroup,
115 maxLat = Latitude(latMax.degrees(), mappingGroup,
120 if (!qFuzzyCompare(latInc.radians(), 0.0)) {
122 baseLat - Angle(floor((baseLat - minLat) / latInc) * latInc), mappingGroup);
124 if (qFuzzyCompare(startLat.degrees(), -90.0))
125 startLat = Latitude(-90.0, mappingGroup, Angle::Degrees);
129 (
long)((maxLat - startLat) / latInc) * latInc + startLat,
131 if (qFuzzyCompare(endLat.degrees(), 90.0))
132 endLat = Latitude(90.0, mappingGroup, Angle::Degrees);
135 Longitude minLon(lonMin.degrees(), mappingGroup,
137 Longitude maxLon(lonMax.degrees(), mappingGroup,
142 if (!qFuzzyCompare(lonInc.radians(), 0.0)) {
143 startLon = Longitude(
144 baseLon - Angle(floor((baseLon - minLon) / lonInc) * lonInc));
148 (long)((maxLon - startLon) / lonInc) * lonInc + startLon;
150 if (qFuzzyCompare( (endLon + lonInc).radians(), maxLon.radians() )) {
155 if (!qFuzzyCompare( (startLat + latInc).radians(), startLat.radians() ) &&
156 !qFuzzyCompare( (startLon + lonInc).radians(), startLon.radians() )) {
158 int numCurvedLines = (int)ceil(((maxLat - minLat) / latInc) + 1);
159 numCurvedLines += (int)ceil(((maxLon - minLon) / lonInc) + 1);
161 int curvedLineDensity = density / numCurvedLines + 1;
162 Angle latRes((maxLon - minLon) / (
double)curvedLineDensity);
163 Angle lonRes((maxLat - minLat) / (
double)curvedLineDensity);
165 if (mappingGroup[
"LatitudeType"][0] ==
"Planetographic") {
167 (maxLat.planetographic() - minLat.planetographic()) / (
double)curvedLineDensity,
171 if (latRes <= Angle(0, Angle::Degrees))
172 latRes = Angle(1
E-10, Angle::Degrees);
174 if (lonRes <= Angle(0, Angle::Degrees))
175 lonRes = Angle(1
E-10, Angle::Degrees);
177 bool firstIteration =
true;
178 bool atMaxLat =
false;
179 bool atMaxLon =
false;
185 Latitude lat = minLat;
187 double previousX = 0;
188 double previousY = 0;
189 bool havePrevious =
false;
191 for(Longitude lon = minLon; lon != maxLon + latRes; lon += latRes) {
193 if (lon > maxLon && !atMaxLon) {
205 if (tproj->IsPositiveWest()) {
206 valid = tproj->SetGround(lat.degrees(), lon.positiveWest(Angle::Degrees));
209 valid = tproj->SetGround(lat.degrees(), lon.positiveEast(Angle::Degrees));
214 y = -1 * tproj->YCoord();
217 if(previousX != x || previousY != y) {
218 QGraphicsLineItem* latLine =
219 new QGraphicsLineItem(QLineF(previousX, previousY, x, y),
this);
223 pen.setCosmetic(
true);
224 latLine->setPen(pen);
229 havePrevious = valid;
243 firstIteration =
false;
249 nextLat = lat.add(latInc, mappingGroup);
251 catch (IException &) {
255 if (lat == minLat && minLat != startLat) {
257 if (startLat < minLat || startLat > maxLat) {
266 else if (lat >= maxLat) {
269 else if (nextLat > endLat) {
276 firstIteration =
true;
281 for (Longitude lon = minLon; lon != maxLon + lonInc; lon += lonInc) {
282 if (lon > endLon && lon < maxLon) {
286 if (lon > maxLon && !atMaxLon) {
291 double previousX = 0;
292 double previousY = 0;
293 bool havePrevious =
false;
295 Latitude lat = minLat;
304 if (tproj->IsPositiveWest()) {
305 double glon = tproj->Has180Domain() ? -1*lon.positiveEast(Angle::Degrees): lon.positiveWest(Angle::Degrees);
306 valid = tproj->SetGround(lat.degrees(), glon);
309 valid = tproj->SetGround(lat.degrees(), lon.positiveEast(Angle::Degrees));
314 y = -1 * tproj->YCoord();
317 if(previousX != x || previousY != y) {
318 QGraphicsLineItem* lonLine =
319 new QGraphicsLineItem(QLineF(previousX, previousY, x, y),
this);
323 pen.setCosmetic(
true);
324 lonLine->setPen(pen);
329 havePrevious = valid;
337 lat = lat.add(lonRes, mappingGroup);
342 lon = startLon - lonInc;
344 firstIteration =
false;
355 GridGraphicsItem::~GridGraphicsItem() {
359 void GridGraphicsItem::paint(QPainter *painter,
360 const QStyleOptionGraphicsItem *style,
QWidget * widget) {
364 QRectF GridGraphicsItem::boundingRect()
const {
365 return m_boundingRect;
369 QRectF GridGraphicsItem::rect()
const {
370 return m_boundingRect;
374 QRectF GridGraphicsItem::calcRect()
const {
378 sceneRect = sceneRect.united(child->boundingRect());
385 void GridGraphicsItem::setRect(QRectF newBoundingRect) {
386 if (m_boundingRect != newBoundingRect) {
387 prepareGeometryChange();
388 m_boundingRect = newBoundingRect;