USGS

Isis 3.0 Object Programmers' Reference

Home

ControlNetGraphicsItem.cpp
1 #include "ControlNetGraphicsItem.h"
2 
3 #include <float.h>
4 #include <iostream>
5 
6 #include <QDebug>
7 #include <QGraphicsScene>
8 
9 #include "Cube.h"
10 #include "ControlMeasure.h"
11 #include "ControlNet.h"
12 #include "ControlPoint.h"
13 #include "ControlPointGraphicsItem.h"
14 #include "FileName.h"
15 #include "IException.h"
16 #include "Latitude.h"
17 #include "Longitude.h"
18 #include "MosaicGraphicsView.h"
19 #include "MosaicSceneWidget.h"
20 #include "ProgressBar.h"
21 #include "Projection.h"
22 #include "Pvl.h"
23 #include "SerialNumberList.h"
24 #include "SurfacePoint.h"
25 #include "UniversalGroundMap.h"
26 
27 using namespace std;
28 
29 namespace Isis {
30  ControlNetGraphicsItem::ControlNetGraphicsItem(ControlNet *controlNet,
31  MosaicSceneWidget *mosaicScene) : QGraphicsObject() {
32  m_controlNet = controlNet;
33  m_mosaicScene = mosaicScene;
34  m_pointToScene = new QMap<ControlPoint *, QPair<QPointF, QPointF> >;
35  m_cubeToGroundMap = new QMap<QString, UniversalGroundMap *>;
36  m_serialNumbers = NULL;
37  mosaicScene->getScene()->addItem(this);
38 
39  buildChildren();
40 
41  connect(mosaicScene, SIGNAL(projectionChanged(Projection *)),
42  this, SLOT(buildChildren()));
43  connect(mosaicScene, SIGNAL(cubesChanged()),
44  this, SLOT(buildChildren()));
45 
46  setZValue(DBL_MAX);
47  }
48 
49 
50  ControlNetGraphicsItem::~ControlNetGraphicsItem() {
51  if(m_pointToScene) {
52  delete m_pointToScene;
53  m_pointToScene = NULL;
54  }
55 
56  if(m_cubeToGroundMap) {
57  QMapIterator<QString, UniversalGroundMap *> it(*m_cubeToGroundMap);
58 
59  while(it.hasNext()) {
60  it.next();
61 
62  if(it.value())
63  delete it.value();
64  }
65 
66  delete m_cubeToGroundMap;
67  m_cubeToGroundMap = NULL;
68  }
69  }
70 
71 
72  QRectF ControlNetGraphicsItem::boundingRect() const {
73  return QRectF();
74  }
75 
76 
77  void ControlNetGraphicsItem::paint(QPainter *painter,
78  const QStyleOptionGraphicsItem *style, QWidget * widget) {
79  }
80 
81 
82  QPair<QPointF, QPointF> ControlNetGraphicsItem::pointToScene(ControlPoint *cp)
83  {
84  Projection *proj = m_mosaicScene->getProjection();
85 
86  QPointF initial;
87  QPointF adjusted;
88 
89  QPointF initialLatLon;
90  QPointF adjustedLatLon;
91 
92  QPair<QPointF, QPointF> rememberedLoc = (*m_pointToScene)[cp];
93 
94  if(!rememberedLoc.second.isNull()) {
95  proj->SetUniversalGround(rememberedLoc.second.y(),
96  rememberedLoc.second.x());
97  adjusted = QPointF(proj->XCoord(), -1 * proj->YCoord());
98  adjustedLatLon = rememberedLoc.second;
99 
100  if(!rememberedLoc.first.isNull()) {
101  proj->SetUniversalGround(rememberedLoc.first.y(),
102  rememberedLoc.first.x());
103  initial = QPointF(proj->XCoord(), -1 * proj->YCoord());
104  initialLatLon = rememberedLoc.first;
105  }
106  }
107  else if(proj) {
108 
109  SurfacePoint adjSurfacePoint(cp->GetAdjustedSurfacePoint());
110  if(adjSurfacePoint.Valid()) {
111  if(proj->SetUniversalGround(adjSurfacePoint.GetLatitude().degrees(),
112  adjSurfacePoint.GetLongitude().degrees())) {
113  adjusted = QPointF(proj->XCoord(), -1 * proj->YCoord());
114  adjustedLatLon = QPointF(adjSurfacePoint.GetLongitude().degrees(),
115  adjSurfacePoint.GetLatitude().degrees());
116  }
117  }
118 
119  SurfacePoint apriSurfacePoint(cp->GetAprioriSurfacePoint());
120  if(apriSurfacePoint.Valid()) {
121  if(proj->SetUniversalGround(apriSurfacePoint.GetLatitude().degrees(),
122  apriSurfacePoint.GetLongitude().degrees())) {
123  initial = QPointF(proj->XCoord(), -1 * proj->YCoord());
124  initialLatLon = QPointF(apriSurfacePoint.GetLongitude().degrees(),
125  apriSurfacePoint.GetLatitude().degrees());
126  }
127  }
128 
129  // If we have adjusted and not apriori then find camera
130  // OR if we don't have an adjusted and don't have an initial we still
131  // need an initial
132  if((!adjusted.isNull() && initial.isNull()) ||
133  (adjusted.isNull() && initial.isNull())) {
134  try {
135  QString sn = cp->GetReferenceSN();
136  QString filename = snToFileName(sn);
137 
138  if(filename.size() > 0) {
139  if((*m_cubeToGroundMap)[filename] == NULL) {
140  Cube cube(FileName(filename).expanded(), "r");
141  UniversalGroundMap *groundMap = new UniversalGroundMap(cube);
142  (*m_cubeToGroundMap)[filename] = groundMap;
143  }
144 
145  if((*m_cubeToGroundMap)[filename]->SetImage(
146  cp->GetRefMeasure()->GetSample(),
147  cp->GetRefMeasure()->GetLine())) {
148  double lat = (*m_cubeToGroundMap)[filename]->UniversalLatitude();
149  double lon = (*m_cubeToGroundMap)[filename]->UniversalLongitude();
150 
151  if(proj->SetUniversalGround(lat, lon)) {
152  initial = QPointF(proj->XCoord(), -1 * proj->YCoord());
153  initialLatLon = QPointF(lon, lat);
154  }
155  }
156  }
157  }
158  catch(IException &) {
159  }
160  }
161  }
162 
163  QPair<QPointF, QPointF> result;
164  QPair<QPointF, QPointF> latLonResult;
165  if(!adjusted.isNull() && adjusted != initial) {
166  result.second = adjusted;
167  result.first = initial;
168  latLonResult.second = adjustedLatLon;
169  latLonResult.first = initialLatLon;
170  }
171  else {
172  result.second = initial;
173  latLonResult.second = initialLatLon;
174  }
175 
176  (*m_pointToScene)[cp] = latLonResult;
177 
178  return result;
179  }
180 
181 
182  QString ControlNetGraphicsItem::snToFileName(QString sn) {
183  QString result;
184 
185  if(m_serialNumbers && m_serialNumbers->size()) {
186  try {
187  result = m_serialNumbers->fileName(sn);
188  }
189  catch(IException &) {
190  }
191  }
192 
193  return result;
194  }
195 
196 
204  bool colorByMeasureCount, int maxMeasureCount,
205  bool colorByJigsawError, double maxResidualMagnitude) {
206 
207  foreach(QGraphicsItem *child, childItems()) {
208  ((ControlPointGraphicsItem *)child)->setArrowVisible(
209  visible, colorByMeasureCount, maxMeasureCount, colorByJigsawError, maxResidualMagnitude);
210  }
211  }
212 
213 
220  QList<QGraphicsItem *> children = childItems();
221  QGraphicsItem *child;
222  foreach(child, children) {
223  if(child->scene())
224  child->scene()->removeItem(child);
225 
226  delete child;
227  child = NULL;
228  }
229 
230  if(m_controlNet) {
231  const int numCp = m_controlNet->GetNumPoints();
232 
233  if(m_serialNumbers) {
234  delete m_serialNumbers;
235  }
236 
237  m_serialNumbers = new SerialNumberList;
238 
239  QStringList cubeFiles(m_mosaicScene->cubeFileNames());
240 
241  QString filename;
242  foreach(filename, cubeFiles) {
243  try {
244  m_serialNumbers->add(filename);
245  }
246  catch(IException &) {
247  }
248  }
249 
250  ProgressBar *p = (ProgressBar *)m_mosaicScene->getProgress();
251  p->setText("Calculating CP Locations");
252  p->setRange(0, numCp - 1);
253  p->setValue(0);
254  p->setVisible(true);
255 
256  for(int cpIndex = 0; cpIndex < numCp; cpIndex ++) {
257  ControlPoint *cp = m_controlNet->GetPoint(cpIndex);
258 
259  // Initial, Final
260  QPair<QPointF, QPointF> scenePoints = pointToScene(cp);
261 
262  new ControlPointGraphicsItem( scenePoints.second, scenePoints.first,
263  cp, m_serialNumbers, m_mosaicScene, this);
264 
265  p->setValue(cpIndex);
266  }
267 
268  p->setVisible(false);
269  }
270  }
271 }