Isis 3 Programmer Reference
ControlNetDiff.cpp
1 #include "IsisDebug.h"
2 
3 #include "ControlNetDiff.h"
4 
5 #include <QMap>
6 #include <QSet>
7 #include <QString>
8 
9 #include "ControlMeasure.h"
10 #include "ControlNet.h"
11 #include "ControlNetVersioner.h"
12 #include "ControlPoint.h"
13 #include "FileName.h"
14 #include "Pvl.h"
15 #include "PvlContainer.h"
16 #include "PvlGroup.h"
17 #include "PvlKeyword.h"
18 #include "PvlObject.h"
19 
20 namespace Isis {
21 
26  init();
27  }
28 
29 
36  init();
37  addTolerances(diffFile);
38  }
39 
40 
45  delete m_tolerances;
46  m_tolerances = NULL;
47 
48  delete m_ignoreKeys;
49  m_ignoreKeys = NULL;
50  }
51 
52 
61  if (diffFile.hasGroup("Tolerances")) {
62  PvlGroup tolerances = diffFile.findGroup("Tolerances");
63  for (int i = 0; i < tolerances.keywords(); i++)
64  m_tolerances->insert(tolerances[i].name(),
65  toDouble(tolerances[i][0]));
66  }
67 
68  if (diffFile.hasGroup("IgnoreKeys")) {
69  PvlGroup ignoreKeys = diffFile.findGroup("IgnoreKeys");
70  for (int i = 0; i < ignoreKeys.keywords(); i++)
71  m_ignoreKeys->insert(ignoreKeys[i].name());
72  }
73  }
74 
75 
84  Pvl ControlNetDiff::compare(FileName &net1Name, FileName &net2Name) {
85  Pvl results;
86  PvlObject report("Differences");
87 
88  diff("Filename", net1Name.name(), net2Name.name(), report);
89 
90  ControlNetVersioner cnv1(net1Name);
91  ControlNetVersioner cnv2(net2Name);
92 
93  BigInt net1NumPts = cnv1.numPoints();
94  BigInt net2NumPts = cnv2.numPoints();
95  diff("Points", toString(net1NumPts), toString(net2NumPts), report);
96 
97  diff("NetworkId", cnv1.netId(), cnv2.netId(), report);
98  diff("TargetName", cnv1.targetName(), cnv2.targetName(), report);
99 
100  Pvl net1Pvl = cnv1.toPvl();
101  Pvl net2Pvl = cnv2.toPvl();
102 
103  PvlObject &net1Obj = net1Pvl.findObject("ControlNetwork");
104  PvlObject &net2Obj = net2Pvl.findObject("ControlNetwork");
105 
107 
108  for (int p = 0; p < net1NumPts; p++) {
109  PvlObject &point = net1Obj.object(p);
110  pointMap[point.findKeyword("PointId")[0]].insert(
111  0, point);
112  }
113 
114  for (int p = 0; p < net2NumPts; p++) {
115  PvlObject &point = net2Obj.object(p);
116  pointMap[point.findKeyword("PointId")[0]].insert(
117  1, point);
118  }
119 
120  QList<QString> pointNames = pointMap.keys();
121 
122  for (int i = 0; i < pointNames.size(); i++) {
123  QMap<int, PvlObject> idMap = pointMap[pointNames[i]];
124  if (idMap.size() == 2) {
125  compare(idMap[0], idMap[1], report);
126  }
127  else if (idMap.contains(0)) {
128  addUniquePoint("PointId", idMap[0].findKeyword("PointId")[0], "N/A", report);
129  }
130  else if (idMap.contains(1)) {
131  addUniquePoint("PointId", "N/A", idMap[1].findKeyword("PointId")[0], report);
132  }
133  }
134 
135  results.addObject(report);
136  return results;
137  }
138 
139 
149  void ControlNetDiff::compare(PvlObject &point1Pvl, PvlObject &point2Pvl, PvlObject &report) {
150  PvlObject pointReport("Point");
151 
152  QString id1 = point1Pvl.findKeyword("PointId")[0];
153  QString id2 = point2Pvl.findKeyword("PointId")[0];
154  pointReport.addKeyword(makeKeyword("PointId", id1, id2));
155 
156  int p1Measures = point1Pvl.groups();
157  int p2Measures = point2Pvl.groups();
158  diff("Measures", toString(p1Measures), toString(p2Measures), pointReport);
159 
160  compareGroups(point1Pvl, point2Pvl, pointReport);
161 
163  for (int m = 0; m < p1Measures; m++) {
164  PvlGroup &measure = point1Pvl.group(m);
165  measureMap[measure.findKeyword("SerialNumber")[0]].insert(
166  0, measure);
167  }
168 
169  for (int m = 0; m < p2Measures; m++) {
170  PvlGroup &measure = point2Pvl.group(m);
171  measureMap[measure.findKeyword("SerialNumber")[0]].insert(
172  1, measure);
173  }
174 
175  QList<QString> measureNames = measureMap.keys();
176  for (int i = 0; i < measureNames.size(); i++) {
177  QMap<int, PvlGroup> idMap = measureMap[measureNames[i]];
178  if (idMap.size() == 2) {
179  compareGroups(idMap[0], idMap[1], pointReport);
180  }
181  else if (idMap.contains(0)) {
182  addUniqueMeasure("SerialNumber", idMap[0].findKeyword("SerialNumber")[0], "N/A", pointReport);
183  }
184  else if (idMap.contains(1)) {
185  addUniqueMeasure("SerialNumber", "N/A", idMap[1].findKeyword("SerialNumber")[0], pointReport);
186  }
187  }
188 
189  if (pointReport.keywords() > 2 || pointReport.groups() > 0)
190  report.addObject(pointReport);
191  }
192 
193 
207  PvlGroup measureReport("Measure");
208  if (g1.hasKeyword("SerialNumber")) {
209  QString sn1 = g1.findKeyword("SerialNumber")[0];
210  QString sn2 = g1.findKeyword("SerialNumber")[0];
211  measureReport.addKeyword(makeKeyword("SerialNumber", sn1, sn2));
212  }
213  PvlContainer &groupReport = g1.hasKeyword("SerialNumber") ?
214  (PvlContainer &) measureReport : (PvlContainer &) report;
215 
217  for (int k = 0; k < g1.keywords(); k++)
218  keywordMap[g1[k].name()].insert(0, g1[k]);
219  for (int k = 0; k < g2.keywords(); k++)
220  keywordMap[g2[k].name()].insert(1, g2[k]);
221 
222  QList<QString> keywordNames = keywordMap.keys();
223  for (int i = 0; i < keywordNames.size(); i++) {
224  QMap<int, PvlKeyword> idMap = keywordMap[keywordNames[i]];
225  if (idMap.size() == 2) {
226  compare(idMap[0], idMap[1], groupReport);
227  }
228  else if (idMap.contains(0)) {
229  QString name = idMap[0].name();
230  if (!m_ignoreKeys->contains(name))
231  diff(name, idMap[0][0], "N/A", groupReport);
232  }
233  else if (idMap.contains(1)) {
234  QString name = idMap[1].name();
235  if (!m_ignoreKeys->contains(name))
236  diff(name, "N/A", idMap[1][0], groupReport);
237  }
238  }
239 
240  if (measureReport.keywords() > 1) report.addGroup(measureReport);
241  }
242 
243 
259  QString name = k1.name();
260  if (m_tolerances->contains(name))
261  diff(name, toDouble(k1[0]), toDouble(k2[0]), (*m_tolerances)[name], report);
262  else
263  diff(name, k1[0], k2[0], report);
264  }
265 
266 
277  void ControlNetDiff::diff(QString name, PvlObject &o1, PvlObject &o2, PvlContainer &report) {
278  QString v1 = o1[name][0];
279  QString v2 = o2[name][0];
280  diff(name, v1, v2, report);
281  }
282 
283 
295  void ControlNetDiff::diff(QString name, QString v1, QString v2, PvlContainer &report) {
296  if (!m_ignoreKeys->contains(name)) {
297  if (v1 != v2) report.addKeyword(makeKeyword(name, v1, v2));
298  }
299  }
300 
301 
311  PvlKeyword ControlNetDiff::makeKeyword(QString name, QString v1, QString v2) {
312  PvlKeyword keyword(name);
313  keyword.addValue(v1);
314  if (v1 != v2) keyword.addValue(v2);
315  return keyword;
316  }
317 
318 
332  void ControlNetDiff::diff(QString name, double v1, double v2, double tol, PvlContainer &report) {
333  if (!m_ignoreKeys->contains(name)) {
334  if (fabs(v1 - v2) > tol) report.addKeyword(makeKeyword(name, v1, v2, tol));
335  }
336  }
337 
338 
350  PvlKeyword ControlNetDiff::makeKeyword(QString name, double v1, double v2, double tol) {
351  PvlKeyword keyword(name);
352  keyword.addValue(toString(v1));
353  if (fabs(v1 - v2) > tol) {
354  keyword.addValue(toString(v2));
355  keyword.addValue(toString(tol));
356  }
357  return keyword;
358  }
359 
360 
370  void ControlNetDiff::addUniquePoint(QString label, QString v1, QString v2, PvlObject &parent) {
371  PvlObject point("Point");
372 
373  PvlKeyword keyword(label);
374  keyword.addValue(v1);
375  keyword.addValue(v2);
376  point.addKeyword(keyword);
377 
378  parent.addObject(point);
379  }
380 
381 
392  void ControlNetDiff::addUniqueMeasure(QString label, QString v1, QString v2, PvlObject &parent) {
393  PvlGroup measure("Measure");
394 
395  PvlKeyword keyword(label);
396  keyword.addValue(v1);
397  keyword.addValue(v2);
398  measure.addKeyword(keyword);
399 
400  parent.addGroup(measure);
401  }
402 
403 
410  m_tolerances = NULL;
411  m_ignoreKeys = NULL;
412 
415 
416  m_ignoreKeys->insert("DateTime");
417  }
418 }
419 
PvlObject & object(const int index)
Return the object at the specified index.
Definition: PvlObject.cpp:460
long long int BigInt
Big int.
Definition: Constants.h:65
void addUniqueMeasure(QString label, QString v1, QString v2, PvlObject &parent)
Add a new keyword for the given measure to the parent object.
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:100
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
void diff(QString name, PvlObject &o1, PvlObject &o2, PvlContainer &report)
Add a new difference keyword to the given report if the PvlObjects have different values for the keyw...
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
void compareGroups(PvlContainer &g1, PvlContainer &g2, PvlObject &report)
Compare two collections, or groupings, of PvlKeywords.
void init()
Initialize the persistent structures used to maintain the state of this instance: its ignore keys and...
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
Contains more than one keyword-value pair.
Definition: PvlContainer.h:63
File name manipulation and expansion.
Definition: FileName.h:116
Pvl toPvl()
Generates a Pvl file from the currently stored control points and header.
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:286
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:198
QMap< QString, double > * m_tolerances
The map of tolerances going from keyword name to tolerance value.
Handle various control network file format versions.
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:178
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition: PvlObject.h:222
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:164
void addObject(const PvlObject &object)
Add a PvlObject.
Definition: PvlObject.h:319
QString name() const
Returns the container name.
Definition: PvlContainer.h:77
ControlNetDiff()
Create a ControlNetDiff without any tolerances.
PvlKeyword & findKeyword(const QString &kname, FindOptions opts)
Finds a keyword in the current PvlObject, or deeper inside other PvlObjects and Pvlgroups within this...
Definition: PvlObject.cpp:148
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
A single keyword-value pair.
Definition: PvlKeyword.h:98
PvlKeyword makeKeyword(QString name, QString v1, QString v2)
Create a new keyword with the given label name and the given values.
Container for cube-like labels.
Definition: Pvl.h:135
QString targetName() const
Returns the target for the network.
PvlKeyword & findKeyword(const QString &name)
Find a keyword with a specified name.
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:423
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:114
void addUniquePoint(QString label, QString v1, QString v2, PvlObject &parent)
Add a new keyword for the given point to the parent object.
QString netId() const
Returns the ID for the network.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
virtual ~ControlNetDiff()
Destroy the ControlNetDiff.
Pvl compare(FileName &net1Name, FileName &net2Name)
Compare two Control Networks given their file names, and return their differences.
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:87
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
QSet< QString > * m_ignoreKeys
The set of keywords to ignore by name.
void addTolerances(Pvl &diffFile)
Add the given ignore keys and tolerances to the persisent collections of such values.
void addValue(QString value, QString unit="")
Adds a value with units.
Definition: PvlKeyword.cpp:268
int numPoints() const
Returns the number of points that have been read in or are ready to write out.