Isis 3 Programmer Reference
ControlNetDiff.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include "IsisDebug.h"
10 
11 #include "ControlNetDiff.h"
12 
13 #include <QMap>
14 #include <QSet>
15 #include <QString>
16 
17 #include "ControlMeasure.h"
18 #include "ControlNet.h"
19 #include "ControlNetVersioner.h"
20 #include "ControlPoint.h"
21 #include "FileName.h"
22 #include "Pvl.h"
23 #include "PvlContainer.h"
24 #include "PvlGroup.h"
25 #include "PvlKeyword.h"
26 #include "PvlObject.h"
27 
28 namespace Isis {
29 
34  init();
35  }
36 
37 
44  init();
45  addTolerances(diffFile);
46  }
47 
48 
53  delete m_tolerances;
54  m_tolerances = NULL;
55 
56  delete m_ignoreKeys;
57  m_ignoreKeys = NULL;
58  }
59 
60 
69  if (diffFile.hasGroup("Tolerances")) {
70  PvlGroup tolerances = diffFile.findGroup("Tolerances");
71  for (int i = 0; i < tolerances.keywords(); i++)
72  m_tolerances->insert(tolerances[i].name(),
73  toDouble(tolerances[i][0]));
74  }
75 
76  if (diffFile.hasGroup("IgnoreKeys")) {
77  PvlGroup ignoreKeys = diffFile.findGroup("IgnoreKeys");
78  for (int i = 0; i < ignoreKeys.keywords(); i++)
79  m_ignoreKeys->insert(ignoreKeys[i].name());
80  }
81  }
82 
83 
92  Pvl ControlNetDiff::compare(FileName &net1Name, FileName &net2Name) {
93  Pvl results;
94  PvlObject report("Differences");
95 
96  diff("Filename", net1Name.name(), net2Name.name(), report);
97 
98  ControlNetVersioner cnv1(net1Name);
99  ControlNetVersioner cnv2(net2Name);
100 
101  BigInt net1NumPts = cnv1.numPoints();
102  BigInt net2NumPts = cnv2.numPoints();
103  diff("Points", toString(net1NumPts), toString(net2NumPts), report);
104 
105  diff("NetworkId", cnv1.netId(), cnv2.netId(), report);
106  diff("TargetName", cnv1.targetName(), cnv2.targetName(), report);
107 
108  Pvl net1Pvl = cnv1.toPvl();
109  Pvl net2Pvl = cnv2.toPvl();
110 
111  PvlObject &net1Obj = net1Pvl.findObject("ControlNetwork");
112  PvlObject &net2Obj = net2Pvl.findObject("ControlNetwork");
113 
115 
116  for (int p = 0; p < net1NumPts; p++) {
117  PvlObject &point = net1Obj.object(p);
118  pointMap[point.findKeyword("PointId")[0]].insert(
119  0, point);
120  }
121 
122  for (int p = 0; p < net2NumPts; p++) {
123  PvlObject &point = net2Obj.object(p);
124  pointMap[point.findKeyword("PointId")[0]].insert(
125  1, point);
126  }
127 
128  QList<QString> pointNames = pointMap.keys();
129 
130  for (int i = 0; i < pointNames.size(); i++) {
131  QMap<int, PvlObject> idMap = pointMap[pointNames[i]];
132  if (idMap.size() == 2) {
133  compare(idMap[0], idMap[1], report);
134  }
135  else if (idMap.contains(0)) {
136  addUniquePoint("PointId", idMap[0].findKeyword("PointId")[0], "N/A", report);
137  }
138  else if (idMap.contains(1)) {
139  addUniquePoint("PointId", "N/A", idMap[1].findKeyword("PointId")[0], report);
140  }
141  }
142 
143  results.addObject(report);
144  return results;
145  }
146 
147 
157  void ControlNetDiff::compare(PvlObject &point1Pvl, PvlObject &point2Pvl, PvlObject &report) {
158  PvlObject pointReport("Point");
159 
160  QString id1 = point1Pvl.findKeyword("PointId")[0];
161  QString id2 = point2Pvl.findKeyword("PointId")[0];
162  pointReport.addKeyword(makeKeyword("PointId", id1, id2));
163 
164  int p1Measures = point1Pvl.groups();
165  int p2Measures = point2Pvl.groups();
166  diff("Measures", toString(p1Measures), toString(p2Measures), pointReport);
167 
168  compareGroups(point1Pvl, point2Pvl, pointReport);
169 
171  for (int m = 0; m < p1Measures; m++) {
172  PvlGroup &measure = point1Pvl.group(m);
173  measureMap[measure.findKeyword("SerialNumber")[0]].insert(
174  0, measure);
175  }
176 
177  for (int m = 0; m < p2Measures; m++) {
178  PvlGroup &measure = point2Pvl.group(m);
179  measureMap[measure.findKeyword("SerialNumber")[0]].insert(
180  1, measure);
181  }
182 
183  QList<QString> measureNames = measureMap.keys();
184  for (int i = 0; i < measureNames.size(); i++) {
185  QMap<int, PvlGroup> idMap = measureMap[measureNames[i]];
186  if (idMap.size() == 2) {
187  compareGroups(idMap[0], idMap[1], pointReport);
188  }
189  else if (idMap.contains(0)) {
190  addUniqueMeasure("SerialNumber", idMap[0].findKeyword("SerialNumber")[0], "N/A", pointReport);
191  }
192  else if (idMap.contains(1)) {
193  addUniqueMeasure("SerialNumber", "N/A", idMap[1].findKeyword("SerialNumber")[0], pointReport);
194  }
195  }
196 
197  if (pointReport.keywords() > 2 || pointReport.groups() > 0)
198  report.addObject(pointReport);
199  }
200 
201 
215  PvlGroup measureReport("Measure");
216  if (g1.hasKeyword("SerialNumber")) {
217  QString sn1 = g1.findKeyword("SerialNumber")[0];
218  QString sn2 = g1.findKeyword("SerialNumber")[0];
219  measureReport.addKeyword(makeKeyword("SerialNumber", sn1, sn2));
220  }
221  PvlContainer &groupReport = g1.hasKeyword("SerialNumber") ?
222  (PvlContainer &) measureReport : (PvlContainer &) report;
223 
225  for (int k = 0; k < g1.keywords(); k++)
226  keywordMap[g1[k].name()].insert(0, g1[k]);
227  for (int k = 0; k < g2.keywords(); k++)
228  keywordMap[g2[k].name()].insert(1, g2[k]);
229 
230  QList<QString> keywordNames = keywordMap.keys();
231  for (int i = 0; i < keywordNames.size(); i++) {
232  QMap<int, PvlKeyword> idMap = keywordMap[keywordNames[i]];
233  if (idMap.size() == 2) {
234  compare(idMap[0], idMap[1], groupReport);
235  }
236  else if (idMap.contains(0)) {
237  QString name = idMap[0].name();
238  if (!m_ignoreKeys->contains(name))
239  diff(name, idMap[0][0], "N/A", groupReport);
240  }
241  else if (idMap.contains(1)) {
242  QString name = idMap[1].name();
243  if (!m_ignoreKeys->contains(name))
244  diff(name, "N/A", idMap[1][0], groupReport);
245  }
246  }
247 
248  if (measureReport.keywords() > 1) report.addGroup(measureReport);
249  }
250 
251 
267  QString name = k1.name();
268  if (m_tolerances->contains(name))
269  diff(name, toDouble(k1[0]), toDouble(k2[0]), (*m_tolerances)[name], report);
270  else
271  diff(name, k1[0], k2[0], report);
272  }
273 
274 
285  void ControlNetDiff::diff(QString name, PvlObject &o1, PvlObject &o2, PvlContainer &report) {
286  QString v1 = o1[name][0];
287  QString v2 = o2[name][0];
288  diff(name, v1, v2, report);
289  }
290 
291 
303  void ControlNetDiff::diff(QString name, QString v1, QString v2, PvlContainer &report) {
304  if (!m_ignoreKeys->contains(name)) {
305  if (v1 != v2) report.addKeyword(makeKeyword(name, v1, v2));
306  }
307  }
308 
309 
319  PvlKeyword ControlNetDiff::makeKeyword(QString name, QString v1, QString v2) {
320  PvlKeyword keyword(name);
321  keyword.addValue(v1);
322  if (v1 != v2) keyword.addValue(v2);
323  return keyword;
324  }
325 
326 
340  void ControlNetDiff::diff(QString name, double v1, double v2, double tol, PvlContainer &report) {
341  if (!m_ignoreKeys->contains(name)) {
342  if (fabs(v1 - v2) > tol) report.addKeyword(makeKeyword(name, v1, v2, tol));
343  }
344  }
345 
346 
358  PvlKeyword ControlNetDiff::makeKeyword(QString name, double v1, double v2, double tol) {
359  PvlKeyword keyword(name);
360  keyword.addValue(toString(v1));
361  if (fabs(v1 - v2) > tol) {
362  keyword.addValue(toString(v2));
363  keyword.addValue(toString(tol));
364  }
365  return keyword;
366  }
367 
368 
378  void ControlNetDiff::addUniquePoint(QString label, QString v1, QString v2, PvlObject &parent) {
379  PvlObject point("Point");
380 
381  PvlKeyword keyword(label);
382  keyword.addValue(v1);
383  keyword.addValue(v2);
384  point.addKeyword(keyword);
385 
386  parent.addObject(point);
387  }
388 
389 
400  void ControlNetDiff::addUniqueMeasure(QString label, QString v1, QString v2, PvlObject &parent) {
401  PvlGroup measure("Measure");
402 
403  PvlKeyword keyword(label);
404  keyword.addValue(v1);
405  keyword.addValue(v2);
406  measure.addKeyword(keyword);
407 
408  parent.addGroup(measure);
409  }
410 
411 
418  m_tolerances = NULL;
419  m_ignoreKeys = NULL;
420 
423 
424  m_ignoreKeys->insert("DateTime");
425  }
426 }
Isis::ControlNetDiff::m_ignoreKeys
QSet< QString > * m_ignoreKeys
The set of keywords to ignore by name.
Definition: ControlNetDiff.h:87
Isis::PvlKeyword::name
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:98
Isis::PvlObject::findGroup
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:129
Isis::PvlObject::group
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:452
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
QList< QString >
Isis::ControlNetDiff::addUniquePoint
void addUniquePoint(QString label, QString v1, QString v2, PvlObject &parent)
Add a new keyword for the given point to the parent object.
Definition: ControlNetDiff.cpp:378
Isis::FileName::name
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:162
Isis::ControlNetDiff::diff
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...
Definition: ControlNetDiff.cpp:285
Isis::PvlContainer::addKeyword
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
Definition: PvlContainer.cpp:202
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::ControlNetDiff::compare
Pvl compare(FileName &net1Name, FileName &net2Name)
Compare two Control Networks given their file names, and return their differences.
Definition: ControlNetDiff.cpp:92
QSet< QString >
Isis::PvlKeyword::addValue
void addValue(QString value, QString unit="")
Adds a value with units.
Definition: PvlKeyword.cpp:252
Isis::PvlObject::groups
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:75
Isis::ControlNetDiff::m_tolerances
QMap< QString, double > * m_tolerances
The map of tolerances going from keyword name to tolerance value.
Definition: ControlNetDiff.h:84
Isis::PvlObject::hasGroup
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition: PvlObject.h:210
Isis::ControlNetDiff::addTolerances
void addTolerances(Pvl &diffFile)
Add the given ignore keys and tolerances to the persisent collections of such values.
Definition: ControlNetDiff.cpp:68
Isis::PvlContainer::hasKeyword
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
Definition: PvlContainer.cpp:159
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::PvlObject::addObject
void addObject(const PvlObject &object)
Add a PvlObject.
Definition: PvlObject.h:307
Isis::PvlObject::object
PvlObject & object(const int index)
Return the object at the specified index.
Definition: PvlObject.cpp:489
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::ControlNetDiff::makeKeyword
PvlKeyword makeKeyword(QString name, QString v1, QString v2)
Create a new keyword with the given label name and the given values.
Definition: ControlNetDiff.cpp:319
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::ControlNetDiff::ControlNetDiff
ControlNetDiff()
Create a ControlNetDiff without any tolerances.
Definition: ControlNetDiff.cpp:33
Isis::ControlNetVersioner::netId
QString netId() const
Returns the ID for the network.
Definition: ControlNetVersioner.cpp:114
Isis::BigInt
long long int BigInt
Big int.
Definition: Constants.h:49
Isis::PvlObject::findObject
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:274
Isis::ControlNetDiff::compareGroups
void compareGroups(PvlContainer &g1, PvlContainer &g2, PvlObject &report)
Compare two collections, or groupings, of PvlKeywords.
Definition: ControlNetDiff.cpp:214
Isis::PvlContainer::name
QString name() const
Returns the container name.
Definition: PvlContainer.h:63
Isis::ControlNetDiff::~ControlNetDiff
virtual ~ControlNetDiff()
Destroy the ControlNetDiff.
Definition: ControlNetDiff.cpp:52
Isis::ControlNetVersioner::targetName
QString targetName() const
Returns the target for the network.
Definition: ControlNetVersioner.cpp:124
Isis::ControlNetVersioner::numPoints
int numPoints() const
Returns the number of points that have been read in or are ready to write out.
Definition: ControlNetVersioner.cpp:174
Isis::PvlObject::addGroup
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:186
Isis::toDouble
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:149
Isis::ControlNetDiff::addUniqueMeasure
void addUniqueMeasure(QString label, QString v1, QString v2, PvlObject &parent)
Add a new keyword for the given measure to the parent object.
Definition: ControlNetDiff.cpp:400
Isis::ControlNetVersioner::toPvl
Pvl toPvl()
Generates a Pvl file from the currently stored control points and header.
Definition: ControlNetVersioner.cpp:202
QMap
This is free and unencumbered software released into the public domain.
Definition: CubeIoHandler.h:22
Isis::PvlContainer::keywords
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:86
Isis::PvlObject::findKeyword
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:177
Isis::PvlContainer::findKeyword
PvlKeyword & findKeyword(const QString &name)
Find a keyword with a specified name.
Definition: PvlContainer.cpp:62
Isis::PvlContainer
Contains more than one keyword-value pair.
Definition: PvlContainer.h:49
Isis::ControlNetDiff::init
void init()
Initialize the persistent structures used to maintain the state of this instance: its ignore keys and...
Definition: ControlNetDiff.cpp:417
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::ControlNetVersioner
Handle various control network file format versions.
Definition: ControlNetVersioner.h:412