9 #include "ControlNetVersioner.h"
11 #include <boost/numeric/ublas/symmetric.hpp>
12 #include <boost/numeric/ublas/io.hpp>
17 #include "ControlNetFileHeaderV0002.pb.h"
18 #include "ControlNetFileHeaderV0005.pb.h"
19 #include "ControlNetLogDataProtoV0001.pb.h"
20 #include "ControlPointFileEntryV0002.pb.h"
22 #include "ControlMeasure.h"
23 #include "ControlNet.h"
24 #include "ControlMeasureLogData.h"
26 #include "EndianSwapper.h"
28 #include "IException.h"
30 #include "LinearAlgebra.h"
31 #include "Longitude.h"
32 #include "NaifStatus.h"
36 #include "PvlKeyword.h"
37 #include "PvlObject.h"
38 #include "SpecialPixel.h"
39 #include "SurfacePoint.h"
43 #include <google/protobuf/io/zero_copy_stream_impl.h>
44 #include <google/protobuf/io/zero_copy_stream.h>
45 #include <google/protobuf/io/coded_stream.h>
47 using boost::numeric::ublas::symmetric_matrix;
48 using boost::numeric::ublas::upper;
49 using namespace google::protobuf::io;
61 ControlNetVersioner::ControlNetVersioner(
ControlNet *net)
62 : m_ownsPoints(false) {
89 : m_ownsPoints(true) {
90 read(netFile, progress);
225 pvlPoint +=
PvlKeyword(
"PointType",
"Constrained");
232 if ( controlPoint->
GetId().isEmpty() ) {
233 QString msg =
"Unable to write control net to PVL file. "
234 "Invalid control point has no point ID value.";
240 if ( QString::compare(controlPoint->GetChooserName(),
"Null", Qt::CaseInsensitive) != 0 ) {
241 pvlPoint +=
PvlKeyword(
"ChooserName", controlPoint->GetChooserName());
243 if ( QString::compare(controlPoint->GetDateTime(),
"Null", Qt::CaseInsensitive) != 0 ) {
244 pvlPoint +=
PvlKeyword(
"DateTime", controlPoint->GetDateTime());
246 if ( controlPoint->IsEditLocked() ) {
249 if ( controlPoint->IsIgnored() ) {
253 switch ( controlPoint->GetAprioriSurfacePointSource() ) {
254 case ControlPoint::SurfacePointSource::None:
256 case ControlPoint::SurfacePointSource::User:
257 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"User");
259 case ControlPoint::SurfacePointSource::AverageOfMeasures:
260 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"AverageOfMeasures");
262 case ControlPoint::SurfacePointSource::Reference:
263 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"Reference");
265 case ControlPoint::SurfacePointSource::Basemap:
266 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"Basemap");
268 case ControlPoint::SurfacePointSource::BundleSolution:
269 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"BundleSolution");
274 pvlPoint +=
PvlKeyword(
"AprioriXYZSourceFile",
275 controlPoint->GetAprioriSurfacePointSourceFile());
278 switch ( controlPoint->GetAprioriRadiusSource() ) {
279 case ControlPoint::RadiusSource::None:
281 case ControlPoint::RadiusSource::User:
282 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"User");
284 case ControlPoint::RadiusSource::AverageOfMeasures:
285 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"AverageOfMeasures");
287 case ControlPoint::RadiusSource::BundleSolution:
288 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"BundleSolution");
290 case ControlPoint::RadiusSource::Ellipsoid:
291 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"Ellipsoid");
293 case ControlPoint::RadiusSource::DEM:
294 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"DEM");
299 pvlPoint +=
PvlKeyword(
"AprioriRadiusSourceFile",
300 controlPoint->GetAprioriRadiusSourceFile());
304 SurfacePoint aprioriSurfacePoint = controlPoint->GetAprioriSurfacePoint();
305 if ( aprioriSurfacePoint.Valid() ) {
321 pvlPoint += aprioriX;
322 pvlPoint += aprioriY;
323 pvlPoint += aprioriZ;
325 symmetric_matrix<double, upper> aprioriCovarianceMatrix =
326 aprioriSurfacePoint.GetRectangularMatrix();
328 if ( aprioriCovarianceMatrix.size1() > 0 ) {
332 matrix +=
toString(aprioriCovarianceMatrix(0, 0));
333 matrix +=
toString(aprioriCovarianceMatrix(0, 1));
334 matrix +=
toString(aprioriCovarianceMatrix(0, 2));
335 matrix +=
toString(aprioriCovarianceMatrix(1, 1));
336 matrix +=
toString(aprioriCovarianceMatrix(1, 2));
337 matrix +=
toString(aprioriCovarianceMatrix(2, 2));
345 QString sigmas =
"AprioriLatitudeSigma = "
347 +
" <meters> AprioriLongitudeSigma = "
349 +
" <meters> AprioriRadiusSigma = "
356 if ( aprioriCovarianceMatrix(0, 0) != 0.0
357 || aprioriCovarianceMatrix(0, 1) != 0.0
358 || aprioriCovarianceMatrix(0, 2) != 0.0
359 || aprioriCovarianceMatrix(1, 1) != 0.0
360 || aprioriCovarianceMatrix(1, 2) != 0.0
361 || aprioriCovarianceMatrix(2, 2) != 0.0 ) {
371 pvlPoint +=
PvlKeyword(
"LatitudeConstrained",
"True");
375 pvlPoint +=
PvlKeyword(
"LongitudeConstrained",
"True");
379 pvlPoint +=
PvlKeyword(
"RadiusConstrained",
"True");
383 SurfacePoint adjustedSurfacePoint = controlPoint->GetAdjustedSurfacePoint();
384 if ( adjustedSurfacePoint.Valid() ) {
402 pvlPoint += adjustedX;
403 pvlPoint += adjustedY;
404 pvlPoint += adjustedZ;
406 symmetric_matrix<double, upper> adjustedCovarianceMatrix =
407 adjustedSurfacePoint.GetRectangularMatrix();
409 if ( adjustedCovarianceMatrix.size1() > 0 ) {
411 PvlKeyword matrix(
"AdjustedCovarianceMatrix");
412 matrix +=
toString(adjustedCovarianceMatrix(0, 0));
413 matrix +=
toString(adjustedCovarianceMatrix(0, 1));
414 matrix +=
toString(adjustedCovarianceMatrix(0, 2));
415 matrix +=
toString(adjustedCovarianceMatrix(1, 1));
416 matrix +=
toString(adjustedCovarianceMatrix(1, 2));
417 matrix +=
toString(adjustedCovarianceMatrix(2, 2));
423 QString sigmas =
"AdjustedLatitudeSigma = "
425 +
" <meters> AdjustedLongitudeSigma = "
427 +
" <meters> AdjustedRadiusSigma = "
434 if ( adjustedCovarianceMatrix(0, 0) != 0.0
435 || adjustedCovarianceMatrix(0, 1) != 0.0
436 || adjustedCovarianceMatrix(0, 2) != 0.0
437 || adjustedCovarianceMatrix(1, 1) != 0.0
438 || adjustedCovarianceMatrix(1, 2) != 0.0
439 || adjustedCovarianceMatrix(2, 2) != 0.0 ) {
446 for (
int j = 0; j < controlPoint->GetNumMeasures(); j++) {
447 PvlGroup pvlMeasure(
"ControlMeasure");
451 switch ( controlMeasure.GetType() ) {
453 pvlMeasure +=
PvlKeyword(
"MeasureType",
"Candidate");
456 pvlMeasure +=
PvlKeyword(
"MeasureType",
"Manual");
459 pvlMeasure +=
PvlKeyword(
"MeasureType",
"RegisteredPixel");
462 pvlMeasure +=
PvlKeyword(
"MeasureType",
"RegisteredSubPixel");
466 if (QString::compare(controlMeasure.
GetChooserName(),
"Null", Qt::CaseInsensitive) != 0) {
469 if (QString::compare(controlMeasure.
GetDateTime(),
"Null", Qt::CaseInsensitive) != 0) {
476 if ( controlMeasure.IsIgnored() ) {
480 if ( controlMeasure.GetSample() !=
Isis::Null) {
485 if ( controlMeasure.GetLine() !=
Isis::Null ) {
494 if ( controlMeasure.GetAprioriSample() !=
Isis::Null ) {
495 pvlMeasure +=
PvlKeyword(
"AprioriSample",
toString(controlMeasure.GetAprioriSample()));
498 if ( controlMeasure.GetAprioriLine() !=
Isis::Null ) {
502 if ( controlMeasure.GetSampleSigma() !=
Isis::Null ) {
507 if ( controlMeasure.GetLineSigma() !=
Isis::Null ) {
512 if ( controlMeasure.GetSampleResidual() !=
Isis::Null ) {
514 toString(controlMeasure.GetSampleResidual()),
518 if ( controlMeasure.GetLineResidual() !=
Isis::Null ) {
523 if ( controlMeasure.IsRejected() ) {
534 pvlMeasure +=
PvlKeyword(
"Reference",
"True");
556 if ( network.
hasObject(
"ProtoBuffer") ) {
559 else if ( network.
hasObject(
"ControlNetwork") ) {
563 QString msg =
"Could not determine the control network file type";
568 QString msg =
"Reading the control network [" + netFile.
name()
588 version =
toInt(controlNetwork[
"Version"][0]);
608 QString msg =
"The Pvl file version [" +
toString(version)
609 +
"] is not supported";
635 QString msg =
"Missing required header information.";
640 progress->
SetText(
"Reading Control Points...");
646 for (
int objectIndex = 0; objectIndex < network.
objects(); objectIndex ++) {
660 QString msg =
"Failed to initialize control point at index ["
687 QString msg =
"Missing required header information.";
692 progress->
SetText(
"Reading Control Points...");
698 for (
int objectIndex = 0; objectIndex < network.
objects(); objectIndex ++) {
711 QString msg =
"Failed to initialize control point at index ["
738 QString msg =
"Missing required header information.";
743 progress->
SetText(
"Reading Control Points...");
749 for (
int objectIndex = 0; objectIndex < network.
objects(); objectIndex ++) {
761 QString msg =
"Failed to initialize control point at index ["
788 QString msg =
"Missing required header information.";
793 progress->
SetText(
"Reading Control Points...");
799 for (
int objectIndex = 0; objectIndex < network.
objects(); objectIndex ++) {
810 QString msg =
"Failed to initialize control point at index ["
837 QString msg =
"Missing required header information.";
842 progress->
SetText(
"Reading Control Points...");
848 for (
int objectIndex = 0; objectIndex < network.
objects(); objectIndex ++) {
859 QString msg =
"Failed to initialize control point at index ["
884 version =
toInt(netInfo[
"Version"][0]);
897 QString msg =
"The Protobuf file version [" +
toString(version)
898 +
"] is not supported";
918 BigInt coreStartPos = protoBufferCore[
"StartByte"];
919 BigInt coreLength = protoBufferCore[
"Bytes"];
921 fstream input(netFile.
expanded().toLatin1().data(), ios::in | ios::binary);
922 if ( !input.is_open() ) {
923 QString msg =
"Failed to open protobuf file [" + netFile.
name() +
"].";
927 input.seekg(coreStartPos, ios::beg);
928 IstreamInputStream inStream(&input);
929 CodedInputStream codedInStream(&inStream);
930 codedInStream.PushLimit(coreLength);
932 codedInStream.SetTotalBytesLimit(1024 * 1024 * 512, 1024 * 1024 * 400);
935 ControlNetFileProtoV0001 protoNet;
937 if ( !protoNet.ParseFromCodedStream(&codedInStream) ) {
938 QString msg =
"Failed to read input PB file [" + netFile.
name() +
"].";
943 QString msg =
"Cannot parse binary protobuf file";
947 QString msg =
"Cannot parse binary PB file";
952 BigInt logStartPos = logDataInfo[
"StartByte"];
953 BigInt logLength = logDataInfo[
"Bytes"];
956 input.seekg(logStartPos, ios::beg);
957 IstreamInputStream logInStream(&input);
958 CodedInputStream codedLogInStream(&logInStream);
959 codedLogInStream.PushLimit(logLength);
961 codedLogInStream.SetTotalBytesLimit(1024 * 1024 * 512, 1024 * 1024 * 400);
964 ControlNetLogDataProtoV0001 protoLogData;
966 if ( !protoLogData.ParseFromCodedStream(&codedLogInStream) ) {
967 QString msg =
"Failed to read log data in protobuf file [" + netFile.
name() +
"].";
972 QString msg =
"Cannot parse binary protobuf file's log data";
979 header.
networkID = protoNet.networkid().c_str();
980 if ( protoNet.has_targetname() ) {
981 header.
targetName = protoNet.targetname().c_str();
986 header.
created = protoNet.created().c_str();
988 header.
description = protoNet.description().c_str();
989 header.
userName = protoNet.username().c_str();
993 QString msg =
"Failed to parse the header from the protobuf control network file.";
998 progress->
SetText(
"Reading Control Points...");
1004 for (
int i = 0; i < protoNet.points_size(); i++) {
1007 protoPoint(
new ControlNetFileProtoV0001_PBControlPoint(protoNet.points(i)));
1009 protoPointLogData(
new ControlNetLogDataProtoV0001_Point(protoLogData.points(i)));
1019 QString msg =
"Failed to convert version 1 protobuf control point at index ["
1020 +
toString(i) +
"] into a ControlPoint.";
1042 BigInt headerStartPos = protoBufferCore[
"HeaderStartByte"];
1043 BigInt headerLength = protoBufferCore[
"HeaderBytes"];
1045 fstream input(netFile.
expanded().toLatin1().data(), ios::in | ios::binary);
1046 if ( !input.is_open() ) {
1047 QString msg =
"Failed to open control network file" + netFile.
name();
1051 input.seekg(headerStartPos, ios::beg);
1052 streampos filePos = input.tellg();
1054 ControlNetFileHeaderV0002 protoHeader;
1056 IstreamInputStream headerInStream(&input);
1057 CodedInputStream headerCodedInStream(&headerInStream);
1059 headerCodedInStream.SetTotalBytesLimit(1024 * 1024 * 512,
1061 CodedInputStream::Limit oldLimit = headerCodedInStream.PushLimit(headerLength);
1062 if ( !protoHeader.ParseFromCodedStream(&headerCodedInStream) ) {
1063 QString msg =
"Failed to parse protobuf header from input control net file ["
1064 + netFile.
name() +
"]";
1067 headerCodedInStream.PopLimit(oldLimit);
1068 filePos += headerLength;
1071 QString msg =
"An error occured while reading the protobuf control network header.";
1078 header.
networkID = protoHeader.networkid().c_str();
1079 if ( protoHeader.has_targetname() ) {
1080 header.
targetName = protoHeader.targetname().c_str();
1085 header.
created = protoHeader.created().c_str();
1086 header.
lastModified = protoHeader.lastmodified().c_str();
1087 header.
description = protoHeader.description().c_str();
1088 header.
userName = protoHeader.username().c_str();
1092 QString msg =
"Missing required header information.";
1099 input.open(netFile.
expanded().toLatin1().data(), ios::in | ios::binary);
1100 input.seekg(filePos, ios::beg);
1101 IstreamInputStream pointInStream(&input);
1102 int numPoints = protoHeader.pointmessagesizes_size();
1105 progress->
SetText(
"Reading Control Points...");
1110 for (
int pointIndex = 0; pointIndex <
numPoints; pointIndex ++) {
1114 CodedInputStream pointCodedInStream(&pointInStream);
1115 pointCodedInStream.SetTotalBytesLimit(1024 * 1024 * 512,
1117 int pointSize = protoHeader.pointmessagesizes(pointIndex);
1118 CodedInputStream::Limit oldPointLimit = pointCodedInStream.PushLimit(pointSize);
1119 newPoint->ParseFromCodedStream(&pointCodedInStream);
1120 pointCodedInStream.PopLimit(oldPointLimit);
1123 QString msg =
"Failed to read protobuf version 2 control point at index ["
1138 QString msg =
"Failed to convert protobuf version 2 control point at index ["
1139 +
toString(pointIndex) +
"] into a ControlPoint.";
1162 BigInt headerStartPos = protoBufferCore[
"HeaderStartByte"];
1163 BigInt headerLength = protoBufferCore[
"HeaderBytes"];
1164 BigInt pointsLength = protoBufferCore[
"PointsBytes"];
1166 fstream input(netFile.
expanded().toLatin1().data(), ios::in | ios::binary);
1167 if ( !input.is_open() ) {
1168 QString msg =
"Failed to open control network file" + netFile.
name();
1172 input.seekg(headerStartPos, ios::beg);
1174 streampos filePos = input.tellg();
1176 ControlNetFileHeaderV0005 protoHeader;
1179 IstreamInputStream headerInStream(&input);
1180 CodedInputStream headerCodedInStream(&headerInStream);
1183 headerCodedInStream.SetTotalBytesLimit(1024 * 1024 * 512,
1186 CodedInputStream::Limit oldLimit = headerCodedInStream.PushLimit(headerLength);
1188 if ( !protoHeader.ParseFromCodedStream(&headerCodedInStream) ) {
1189 QString msg =
"Failed to parse protobuf header from input control net file ["
1190 + netFile.
name() +
"]";
1194 headerCodedInStream.PopLimit(oldLimit);
1196 filePos += headerLength;
1199 QString msg =
"An error occured while reading the protobuf control network header.";
1205 header.
networkID = protoHeader.networkid().c_str();
1206 if ( protoHeader.has_targetname() ) {
1207 header.
targetName = protoHeader.targetname().c_str();
1212 header.
created = protoHeader.created().c_str();
1213 header.
lastModified = protoHeader.lastmodified().c_str();
1214 header.
description = protoHeader.description().c_str();
1215 header.
userName = protoHeader.username().c_str();
1219 QString msg =
"Missing required header information.";
1226 input.open(netFile.
expanded().toLatin1().data(), ios::in | ios::binary);
1227 input.seekg(filePos, ios::beg);
1229 IstreamInputStream pointInStream(&input);
1231 BigInt numberOfPoints = 0;
1233 if ( protoBufferInfo.
hasGroup(
"ControlNetworkInfo") ) {
1236 if ( networkInfo.
hasKeyword(
"NumberOfPoints") ) {
1238 numberOfPoints = networkInfo[
"NumberOfPoints"];
1246 if (progress && numberOfPoints != 0) {
1247 progress->
SetText(
"Reading Control Points...");
1253 int pointIndex = -1;
1254 while (pointInStream.ByteCount() < pointsLength) {
1260 CodedInputStream pointCodedInStream(&pointInStream);
1261 pointCodedInStream.SetTotalBytesLimit(1024 * 1024 * 512,
1265 pointCodedInStream.ReadRaw(
reinterpret_cast<char *
>(&size),
sizeof(size));
1269 CodedInputStream::Limit oldPointLimit = pointCodedInStream.PushLimit(size);
1270 newPoint->ParseFromCodedStream(&pointCodedInStream);
1271 pointCodedInStream.PopLimit(oldPointLimit);
1274 QString msg =
"Failed to read protobuf version 2 control point at index ["
1283 if (progress && numberOfPoints != 0) {
1289 QString msg =
"Failed to convert protobuf version 2 control point at index ["
1290 +
toString(pointIndex) +
"] into a ControlPoint.";
1346 ControlPointFileEntryV0002 protoPoint = point.
pointData();
1350 controlPoint->
SetId(QString(protoPoint.id().c_str()));
1352 if ( protoPoint.has_choosername() ) {
1358 switch ( protoPoint.type() ) {
1359 case ControlPointFileEntryV0002_PointType_obsolete_Tie:
1360 case ControlPointFileEntryV0002_PointType_Free:
1363 case ControlPointFileEntryV0002_PointType_Constrained:
1366 case ControlPointFileEntryV0002_PointType_obsolete_Ground:
1367 case ControlPointFileEntryV0002_PointType_Fixed:
1371 QString msg =
"Unable to create ControlPoint [" +
toString(protoPoint.id().c_str())
1372 +
"] from file. Type enumeration [" +
toString((
int)(protoPoint.type()))
1377 controlPoint->
SetType(pointType);
1379 if ( protoPoint.has_ignore() ) {
1380 controlPoint->
SetIgnored(protoPoint.ignore());
1382 if ( protoPoint.has_jigsawrejected() ) {
1383 controlPoint->
SetRejected(protoPoint.jigsawrejected());
1387 if ( protoPoint.has_aprioriradiussource() ) {
1389 switch ( protoPoint.aprioriradiussource() ) {
1390 case ControlPointFileEntryV0002_AprioriSource_None:
1393 case ControlPointFileEntryV0002_AprioriSource_User:
1396 case ControlPointFileEntryV0002_AprioriSource_AverageOfMeasures:
1399 case ControlPointFileEntryV0002_AprioriSource_Ellipsoid:
1402 case ControlPointFileEntryV0002_AprioriSource_DEM:
1405 case ControlPointFileEntryV0002_AprioriSource_BundleSolution:
1410 QString msg =
"Unknown control point apriori radius source.";
1416 if ( protoPoint.has_aprioriradiussourcefile() ) {
1421 if ( protoPoint.has_apriorisurfpointsource() ) {
1422 switch ( protoPoint.apriorisurfpointsource() ) {
1423 case ControlPointFileEntryV0002_AprioriSource_None:
1427 case ControlPointFileEntryV0002_AprioriSource_User:
1431 case ControlPointFileEntryV0002_AprioriSource_AverageOfMeasures:
1433 ControlPoint::SurfacePointSource::AverageOfMeasures);
1436 case ControlPointFileEntryV0002_AprioriSource_Reference:
1440 case ControlPointFileEntryV0002_AprioriSource_Basemap:
1444 case ControlPointFileEntryV0002_AprioriSource_BundleSolution:
1446 ControlPoint::SurfacePointSource::BundleSolution);
1450 QString msg =
"Unknown control point aprioir surface point source.";
1456 if ( protoPoint.has_apriorisurfpointsourcefile() ) {
1458 protoPoint.apriorisurfpointsourcefile().c_str());
1461 if ( protoPoint.has_apriorix()
1462 && protoPoint.has_aprioriy()
1463 && protoPoint.has_aprioriz() ) {
1469 if ( protoPoint.aprioricovar_size() > 0 ) {
1470 symmetric_matrix<double, upper> aprioriCovarianceMatrix;
1471 aprioriCovarianceMatrix.resize(3);
1472 aprioriCovarianceMatrix.clear();
1473 aprioriCovarianceMatrix(0, 0) = protoPoint.aprioricovar(0);
1474 aprioriCovarianceMatrix(0, 1) = protoPoint.aprioricovar(1);
1475 aprioriCovarianceMatrix(0, 2) = protoPoint.aprioricovar(2);
1476 aprioriCovarianceMatrix(1, 1) = protoPoint.aprioricovar(3);
1477 aprioriCovarianceMatrix(1, 2) = protoPoint.aprioricovar(4);
1478 aprioriCovarianceMatrix(2, 2) = protoPoint.aprioricovar(5);
1487 if ( protoPoint.has_adjustedx()
1488 && protoPoint.has_adjustedy()
1489 && protoPoint.has_adjustedz() ) {
1495 if ( protoPoint.adjustedcovar_size() > 0 ) {
1496 symmetric_matrix<double, upper> adjustedCovarianceMatrix;
1497 adjustedCovarianceMatrix.resize(3);
1498 adjustedCovarianceMatrix.clear();
1499 adjustedCovarianceMatrix(0, 0) = protoPoint.adjustedcovar(0);
1500 adjustedCovarianceMatrix(0, 1) = protoPoint.adjustedcovar(1);
1501 adjustedCovarianceMatrix(0, 2) = protoPoint.adjustedcovar(2);
1502 adjustedCovarianceMatrix(1, 1) = protoPoint.adjustedcovar(3);
1503 adjustedCovarianceMatrix(1, 2) = protoPoint.adjustedcovar(4);
1504 adjustedCovarianceMatrix(2, 2) = protoPoint.adjustedcovar(5);
1511 SurfacePoint aprioriSurfacePoint = controlPoint->GetAprioriSurfacePoint();
1512 SurfacePoint adjustedSurfacePoint = controlPoint->GetAdjustedSurfacePoint();
1517 for (
int m = 0 ; m < protoPoint.measures_size(); m++) {
1521 if ( protoPoint.has_referenceindex() ) {
1526 if ( protoPoint.has_datetime() ) {
1527 controlPoint->
SetDateTime(protoPoint.datetime().c_str());
1530 if ( protoPoint.has_editlock() ) {
1534 return controlPoint;
1548 const ControlPointFileEntryV0002_Measure &measure) {
1557 switch ( measure.type() ) {
1558 case ControlPointFileEntryV0002_Measure::Candidate:
1561 case ControlPointFileEntryV0002_Measure::Manual:
1564 case ControlPointFileEntryV0002_Measure::RegisteredPixel:
1567 case ControlPointFileEntryV0002_Measure::RegisteredSubPixel:
1571 QString msg =
"Unknown control measure type.";
1575 newMeasure->
SetType(measureType);
1577 if ( measure.has_jigsawrejected() ) {
1578 newMeasure->
SetRejected(measure.jigsawrejected());
1580 if ( measure.has_ignore() ) {
1581 newMeasure->SetIgnored(measure.ignore());
1583 if ( measure.has_line() ) {
1584 newMeasure->
SetCoordinate(measure.sample(), measure.line());
1586 if ( measure.has_diameter() ) {
1589 if ( measure.has_apriorisample() ) {
1590 newMeasure->SetAprioriSample(measure.apriorisample());
1592 if ( measure.has_aprioriline() ) {
1593 newMeasure->SetAprioriLine(measure.aprioriline());
1595 if ( measure.has_samplesigma() ) {
1596 newMeasure->SetSampleSigma(measure.samplesigma());
1598 if ( measure.has_linesigma() ) {
1599 newMeasure->SetLineSigma(measure.linesigma());
1601 if ( measure.has_sampleresidual()
1602 && measure.has_lineresidual() ) {
1603 newMeasure->
SetResidual(measure.sampleresidual(), measure.lineresidual());
1606 for (
int i = 0; i < measure.log_size(); i++) {
1608 const ControlPointFileEntryV0002_Measure_MeasureLogData &protoLog = measure.log(i);
1610 if ( protoLog.has_doubledatatype() ) {
1613 if ( protoLog.has_doubledatavalue() ) {
1619 if ( measure.has_choosername() ) {
1620 newMeasure->
SetChooserName(QString(measure.choosername().c_str()));
1623 if ( measure.has_datetime() ) {
1624 newMeasure->
SetDateTime(QString(measure.datetime().c_str()));
1629 if ( measure.has_editlock() ) {
1630 newMeasure->SetEditLock(measure.editlock());
1662 const int labelBytes = 65536;
1663 fstream output(netFile.
expanded().toLatin1().data(), ios::out | ios::trunc | ios::binary);
1664 char *blankLabel =
new char[labelBytes];
1665 memset(blankLabel, 0, labelBytes);
1666 output.write(blankLabel, labelBytes);
1667 delete [] blankLabel;
1669 int numMeasures = 0;
1672 numMeasures += point->GetNumMeasures();
1675 streampos startCoreHeaderPos = output.tellp();
1679 BigInt pointByteTotal = 0;
1685 ControlNetFileHeaderV0005 protobufHeader;
1694 streampos coreHeaderSize = protobufHeader.ByteSize();
1705 BigInt pointsStartByte = (
BigInt) (startCoreHeaderPos + coreHeaderSize);
1713 PvlGroup netInfo(
"ControlNetworkInfo");
1714 netInfo.addComment(
"This group is for informational purposes only");
1715 netInfo +=
PvlKeyword(
"NetworkId", protobufHeader.networkid().c_str());
1716 netInfo +=
PvlKeyword(
"TargetName", protobufHeader.targetname().c_str());
1717 netInfo +=
PvlKeyword(
"UserName", protobufHeader.username().c_str());
1718 netInfo +=
PvlKeyword(
"Created", protobufHeader.created().c_str());
1719 netInfo +=
PvlKeyword(
"LastModified", protobufHeader.lastmodified().c_str());
1720 netInfo +=
PvlKeyword(
"Description", protobufHeader.description().c_str());
1728 output.seekp(0, ios::beg);
1735 QString msg =
"Can't write control net file";
1748 ControlNetFileHeaderV0005 protobufHeader;
1758 if ( !protobufHeader.SerializeToOstream(output) ) {
1759 QString msg =
"Failed to write output control network file.";
1776 BigInt startPos = output->tellp();
1778 ControlPointFileEntryV0002 protoPoint;
1781 if ( controlPoint->
GetId().isEmpty() ) {
1782 QString msg =
"Unbable to write first point of control net. "
1783 "Invalid control point has no point ID value.";
1787 protoPoint.set_id(controlPoint->
GetId().toLatin1().data());
1790 if ( QString::compare(controlPoint->GetChooserName(),
"Null", Qt::CaseInsensitive) != 0 ) {
1791 protoPoint.set_choosername(controlPoint->GetChooserName().toLatin1().data());
1793 if ( QString::compare(controlPoint->GetDateTime(),
"Null", Qt::CaseInsensitive) != 0 ) {
1794 protoPoint.set_datetime(controlPoint->GetDateTime().toLatin1().data());
1796 if ( controlPoint->IsEditLocked() ) {
1797 protoPoint.set_editlock(
true);
1800 ControlPointFileEntryV0002_PointType pointType;
1801 switch ( controlPoint->
GetType() ) {
1803 pointType = ControlPointFileEntryV0002_PointType_Free;
1806 pointType = ControlPointFileEntryV0002_PointType_Constrained;
1809 pointType = ControlPointFileEntryV0002_PointType_Fixed;
1812 pointType = ControlPointFileEntryV0002_PointType_Free;
1815 protoPoint.set_type(pointType);
1817 if ( controlPoint->IsIgnored() ) {
1818 protoPoint.set_ignore(
true);
1821 if ( controlPoint->IsRejected() ) {
1822 protoPoint.set_jigsawrejected(
true);
1830 protoPoint.set_apriorisurfpointsourcefile(
1831 controlPoint->GetAprioriSurfacePointSourceFile().toLatin1().data());
1835 switch ( controlPoint->GetAprioriSurfacePointSource() ) {
1836 case ControlPoint::SurfacePointSource::None:
1837 protoPoint.set_apriorisurfpointsource(ControlPointFileEntryV0002_AprioriSource_None);
1839 case ControlPoint::SurfacePointSource::User:
1840 protoPoint.set_apriorisurfpointsource(ControlPointFileEntryV0002_AprioriSource_User);
1842 case ControlPoint::SurfacePointSource::AverageOfMeasures:
1843 protoPoint.set_apriorisurfpointsource(
1844 ControlPointFileEntryV0002_AprioriSource_AverageOfMeasures);
1846 case ControlPoint::SurfacePointSource::Reference:
1847 protoPoint.set_apriorisurfpointsource(
1848 ControlPointFileEntryV0002_AprioriSource_Reference);
1850 case ControlPoint::SurfacePointSource::Basemap:
1851 protoPoint.set_apriorisurfpointsource(ControlPointFileEntryV0002_AprioriSource_Basemap);
1853 case ControlPoint::SurfacePointSource::BundleSolution:
1854 protoPoint.set_apriorisurfpointsource(
1855 ControlPointFileEntryV0002_AprioriSource_BundleSolution);
1858 protoPoint.set_apriorisurfpointsource(ControlPointFileEntryV0002_AprioriSource_None);
1863 switch ( controlPoint->GetAprioriRadiusSource() ) {
1864 case ControlPoint::RadiusSource::None:
1865 protoPoint.set_aprioriradiussource(ControlPointFileEntryV0002_AprioriSource_None);
1867 case ControlPoint::RadiusSource::User:
1868 protoPoint.set_aprioriradiussource(ControlPointFileEntryV0002_AprioriSource_User);
1870 case ControlPoint::RadiusSource::AverageOfMeasures:
1871 protoPoint.set_aprioriradiussource(
1872 ControlPointFileEntryV0002_AprioriSource_AverageOfMeasures);
1874 case ControlPoint::RadiusSource::BundleSolution:
1875 protoPoint.set_aprioriradiussource(
1876 ControlPointFileEntryV0002_AprioriSource_BundleSolution);
1878 case ControlPoint::RadiusSource::Ellipsoid:
1879 protoPoint.set_aprioriradiussource(ControlPointFileEntryV0002_AprioriSource_Ellipsoid);
1881 case ControlPoint::RadiusSource::DEM:
1882 protoPoint.set_aprioriradiussource(ControlPointFileEntryV0002_AprioriSource_DEM);
1885 protoPoint.set_aprioriradiussource(ControlPointFileEntryV0002_AprioriSource_None);
1890 protoPoint.set_aprioriradiussourcefile(
1891 controlPoint->GetAprioriRadiusSourceFile().toLatin1().data());
1894 SurfacePoint aprioriSurfacePoint = controlPoint->GetAprioriSurfacePoint();
1895 if ( aprioriSurfacePoint.Valid() ) {
1896 protoPoint.set_apriorix(aprioriSurfacePoint.GetX().
meters());
1897 protoPoint.set_aprioriy(aprioriSurfacePoint.GetY().
meters());
1898 protoPoint.set_aprioriz(aprioriSurfacePoint.GetZ().
meters());
1900 symmetric_matrix<double, upper> aprioriCovarianceMatrix =
1901 aprioriSurfacePoint.GetRectangularMatrix();
1902 if ( aprioriCovarianceMatrix.size1() > 0 &&
1907 protoPoint.add_aprioricovar(aprioriCovarianceMatrix(0, 0));
1908 protoPoint.add_aprioricovar(aprioriCovarianceMatrix(0, 1));
1909 protoPoint.add_aprioricovar(aprioriCovarianceMatrix(0, 2));
1910 protoPoint.add_aprioricovar(aprioriCovarianceMatrix(1, 1));
1911 protoPoint.add_aprioricovar(aprioriCovarianceMatrix(1, 2));
1912 protoPoint.add_aprioricovar(aprioriCovarianceMatrix(2, 2));
1927 SurfacePoint adjustedSurfacePoint = controlPoint->GetAdjustedSurfacePoint();
1928 if ( adjustedSurfacePoint.Valid() ) {
1930 protoPoint.set_adjustedx(adjustedSurfacePoint.GetX().
meters());
1931 protoPoint.set_adjustedy(adjustedSurfacePoint.GetY().
meters());
1932 protoPoint.set_adjustedz(adjustedSurfacePoint.GetZ().
meters());
1934 symmetric_matrix<double, upper> adjustedCovarianceMatrix =
1935 adjustedSurfacePoint.GetRectangularMatrix();
1936 if ( adjustedCovarianceMatrix.size1() > 0 ) {
1937 protoPoint.add_adjustedcovar(adjustedCovarianceMatrix(0, 0));
1938 protoPoint.add_adjustedcovar(adjustedCovarianceMatrix(0, 1));
1939 protoPoint.add_adjustedcovar(adjustedCovarianceMatrix(0, 2));
1940 protoPoint.add_adjustedcovar(adjustedCovarianceMatrix(1, 1));
1941 protoPoint.add_adjustedcovar(adjustedCovarianceMatrix(1, 2));
1942 protoPoint.add_adjustedcovar(adjustedCovarianceMatrix(2, 2));
1947 for (
int j = 0; j < controlPoint->GetNumMeasures(); j++) {
1951 ControlPointFileEntryV0002_Measure protoMeasure;
1955 switch ( controlMeasure.GetType() ) {
1957 protoMeasure.set_type(ControlPointFileEntryV0002_Measure_MeasureType_Candidate);
1961 protoMeasure.set_type(ControlPointFileEntryV0002_Measure_MeasureType_Manual);
1965 protoMeasure.set_type(
1966 ControlPointFileEntryV0002_Measure_MeasureType_RegisteredPixel);
1970 protoMeasure.set_type(
1971 ControlPointFileEntryV0002_Measure_MeasureType_RegisteredSubPixel);
1975 if (QString::compare(controlMeasure.
GetChooserName(),
"Null", Qt::CaseInsensitive) != 0) {
1976 protoMeasure.set_choosername(controlMeasure.
GetChooserName().toLatin1().data());
1979 if (QString::compare(controlMeasure.
GetDateTime(),
"Null", Qt::CaseInsensitive) != 0) {
1980 protoMeasure.set_datetime(controlMeasure.
GetDateTime().toLatin1().data());
1984 protoMeasure.set_editlock(
true);
1987 if ( controlMeasure.IsIgnored() ) {
1988 protoMeasure.set_ignore(
true);
1991 if ( controlMeasure.IsRejected() ) {
1992 protoMeasure.set_jigsawrejected(
true);
1995 if ( controlMeasure.GetSample() !=
Isis::Null ) {
1996 protoMeasure.set_sample(controlMeasure.GetSample());
1999 if ( controlMeasure.GetLine() !=
Isis::Null ) {
2000 protoMeasure.set_line(controlMeasure.GetLine());
2004 protoMeasure.set_diameter(controlMeasure.
GetDiameter());
2007 if ( controlMeasure.GetAprioriSample() !=
Isis::Null ) {
2008 protoMeasure.set_apriorisample(controlMeasure.GetAprioriSample());
2011 if ( controlMeasure.GetAprioriLine() !=
Isis::Null ) {
2012 protoMeasure.set_aprioriline(controlMeasure.GetAprioriLine());
2015 if ( controlMeasure.GetSampleSigma() !=
Isis::Null ) {
2016 protoMeasure.set_samplesigma(controlMeasure.GetSampleSigma());
2019 if ( controlMeasure.GetLineSigma() !=
Isis::Null ) {
2020 protoMeasure.set_linesigma(controlMeasure.GetLineSigma());
2023 if ( controlMeasure.GetSampleResidual() !=
Isis::Null ) {
2024 protoMeasure.set_sampleresidual(controlMeasure.GetSampleResidual());
2027 if ( controlMeasure.GetLineResidual() !=
Isis::Null ) {
2028 protoMeasure.set_lineresidual(controlMeasure.GetLineResidual());
2031 if ( controlMeasure.IsRejected() ) {
2032 protoMeasure.set_jigsawrejected(
true);
2036 for (
int logEntry = 0; logEntry < measureLogs.size(); logEntry ++) {
2040 ControlPointFileEntryV0002_Measure_MeasureLogData logData;
2043 logData.set_doubledatatype( (
int) log.
GetDataType() );
2047 *protoMeasure.add_log() = logData;
2050 *protoPoint.add_measures() = protoMeasure;
2053 uint32_t byteSize = protoPoint.ByteSize();
2056 byteSize = lsb.
Uint32_t(&byteSize);
2058 output->write(
reinterpret_cast<char *
>(&byteSize),
sizeof(byteSize));
2060 if ( !protoPoint.SerializeToOstream(output) ) {
2061 QString err =
"Error writing to coded protobuf stream";
2067 delete controlPoint;
2068 controlPoint = NULL;
2070 BigInt currentPos = output->tellp();
2071 BigInt byteCount = currentPos - startPos;