File: shot_qt.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (216 lines) | stat: -rw-r--r-- 9,396 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef SHOT_QT_H
#define SHOT_QT_H


/** This function read a shot from a parsed XML node.

  */

template <class ShotType>
    bool ReadShotFromQDomNode(
        ShotType &shot, /// the shot that will contain the read node
        const QDomNode &node) /// The XML node to be read
{
  typedef typename ShotType::ScalarType ScalarType;
  typedef vcg::Point3<typename ShotType::ScalarType> Point3x;
  if(QString::compare(node.nodeName(),"VCGCamera")==0)
  {
    QDomNamedNodeMap attr = node.attributes();
    if (attr.contains("BinaryData") && attr.namedItem("BinaryData").nodeValue().toInt() == 1)
    {
      Point3x tra;
      QString str = attr.namedItem("TranslationVector").nodeValue();
      QByteArray value = QByteArray::fromBase64(str.toLocal8Bit());
      memcpy(tra.V(), value.data(), sizeof(ScalarType) * 3);
      shot.Extrinsics.SetTra(-tra);

      vcg::Matrix44<ScalarType> rot;
      str = attr.namedItem("RotationMatrix").nodeValue();
      value = QByteArray::fromBase64(str.toLocal8Bit());
      memcpy(rot.V(), value.data(), sizeof(ScalarType) * 16);
      shot.Extrinsics.SetRot(rot);

      vcg::Camera<ScalarType> &cam = shot.Intrinsics;
      if (attr.contains("CameraType")) cam.cameraType = attr.namedItem("CameraType").nodeValue().toInt();
      memcpy(&cam.FocalMm, QByteArray::fromBase64(attr.namedItem("FocalMm").nodeValue().toLocal8Bit()).data(), sizeof(ScalarType));
      memcpy(&cam.ViewportPx, QByteArray::fromBase64(attr.namedItem("ViewportPx").nodeValue().toLocal8Bit()).data(), sizeof(int) * 2);
      memcpy(&cam.CenterPx, QByteArray::fromBase64(attr.namedItem("CenterPx").nodeValue().toLocal8Bit()).data(), sizeof(ScalarType)*2);
      memcpy(&cam.PixelSizeMm, QByteArray::fromBase64(attr.namedItem("PixelSizeMm").nodeValue().toLocal8Bit()).data(), sizeof(ScalarType) * 2);
      memcpy(&cam.k, QByteArray::fromBase64(attr.namedItem("LensDistortion").nodeValue().toLocal8Bit()).data(), sizeof(ScalarType) * 2);
    }
    else
    {
      Point3x tra;
      tra[0] = attr.namedItem("TranslationVector").nodeValue().section(' ', 0, 0).toDouble();
      tra[1] = attr.namedItem("TranslationVector").nodeValue().section(' ', 1, 1).toDouble();
      tra[2] = attr.namedItem("TranslationVector").nodeValue().section(' ', 2, 2).toDouble();
      shot.Extrinsics.SetTra(-tra);

      vcg::Matrix44<ScalarType> rot;
      QStringList values = attr.namedItem("RotationMatrix").nodeValue().split(" ", QString::SkipEmptyParts);
      for (int y = 0; y < 4; y++)
        for (int x = 0; x < 4; x++)
          rot[y][x] = values[x + 4 * y].toDouble();
      shot.Extrinsics.SetRot(rot);

      vcg::Camera<ScalarType> &cam = shot.Intrinsics;
      if (attr.contains("CameraType")) cam.cameraType = attr.namedItem("CameraType").nodeValue().toInt();
      cam.FocalMm = attr.namedItem("FocalMm").nodeValue().toDouble();
      cam.ViewportPx.X() = attr.namedItem("ViewportPx").nodeValue().section(' ', 0, 0).toInt();
      cam.ViewportPx.Y() = attr.namedItem("ViewportPx").nodeValue().section(' ', 1, 1).toInt();
      cam.CenterPx[0] = attr.namedItem("CenterPx").nodeValue().section(' ', 0, 0).toDouble();
      cam.CenterPx[1] = attr.namedItem("CenterPx").nodeValue().section(' ', 1, 1).toDouble();
      cam.PixelSizeMm[0] = attr.namedItem("PixelSizeMm").nodeValue().section(' ', 0, 0).toDouble();
      cam.PixelSizeMm[1] = attr.namedItem("PixelSizeMm").nodeValue().section(' ', 1, 1).toDouble();
      cam.k[0] = attr.namedItem("LensDistortion").nodeValue().section(' ', 0, 0).toDouble();
      cam.k[1] = attr.namedItem("LensDistortion").nodeValue().section(' ', 1, 1).toDouble();
    }
    // scale correction should no more exist !!!
    //    float scorr = attr.namedItem("ScaleCorr").nodeValue().toDouble();
    //    if(scorr != 0.0) {
    //      cam.PixelSizeMm[0] *= scorr;
    //      cam.PixelSizeMm[1] *= scorr;
    //    }
    return true;
  }
  return false;
}

/// TEXALIGN VERSION
template <class ShotType>
  bool ReadShotFromOLDXML( ShotType &shot, const QDomNode &node)
{
  typedef typename ShotType::ScalarType ScalarType;

  if(QString::compare(node.nodeName(),"CamParam")==0)
  {
    QDomNamedNodeMap attr = node.attributes();
    vcg::Point3<ScalarType> tra;
    tra[0] = attr.namedItem("SimTra").nodeValue().section(' ',0,0).toDouble();
    tra[1] = attr.namedItem("SimTra").nodeValue().section(' ',1,1).toDouble();
    tra[2] = attr.namedItem("SimTra").nodeValue().section(' ',2,2).toDouble();
    shot.Extrinsics.SetTra(-tra);

    vcg::Matrix44<ScalarType> rot;
    QStringList values =  attr.namedItem("SimRot").nodeValue().split(" ", QString::SkipEmptyParts);
    for(int y = 0; y < 4; y++)
      for(int x = 0; x < 4; x++)
        rot[y][x] = values[x + 4*y].toDouble();
    shot.Extrinsics.SetRot(rot);

    vcg::Camera<ScalarType> &cam = shot.Intrinsics;
    cam.FocalMm = attr.namedItem("Focal").nodeValue().toDouble();
    cam.ViewportPx.X() = attr.namedItem("Viewport").nodeValue().section(' ',0,0).toInt();
    cam.ViewportPx.Y() = attr.namedItem("Viewport").nodeValue().section(' ',1,1).toInt();
    cam.CenterPx[0] = attr.namedItem("Center").nodeValue().section(' ',0,0).toInt();
    cam.CenterPx[1] = attr.namedItem("Center").nodeValue().section(' ',1,1).toInt();
    cam.PixelSizeMm[0] = attr.namedItem("ScaleF").nodeValue().section(' ',0,0).toDouble();
    cam.PixelSizeMm[1] = attr.namedItem("ScaleF").nodeValue().section(' ',1,1).toDouble();
    cam.k[0] = attr.namedItem("LensDist").nodeValue().section(' ',0,0).toDouble();
    cam.k[1] = attr.namedItem("LensDist").nodeValue().section(' ',1,1).toDouble();

    // scale correction
    float scorr = attr.namedItem("ScaleCorr").nodeValue().toDouble();
    if(scorr != 0.0) {
      cam.PixelSizeMm[0] *= scorr;
      cam.PixelSizeMm[1] *= scorr;
    }
    return true;
  }
  return false;
}



template <class ShotType>
    QDomElement WriteShotToQDomNode(
        const ShotType &shot, /// the shot to be written node
        QDomDocument &doc) /// The XML node to be read
{
  typedef typename ShotType::ScalarType ScalarType;

  QDomElement shotElem = doc.createElement( "VCGCamera" );
  vcg::Point3<ScalarType> tra = -(shot.Extrinsics.Tra());
  QString str = QString("%1 %2 %3 1").arg(tra[0]).arg(tra[1]).arg(tra[2]);
  shotElem.setAttribute("TranslationVector", str);
  vcg::Matrix44<ScalarType> rot = shot.Extrinsics.Rot();
  str = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 ")
    .arg(rot[0][0]).arg(rot[0][1]).arg(rot[0][2]).arg(rot[0][3])
    .arg(rot[1][0]).arg(rot[1][1]).arg(rot[1][2]).arg(rot[1][3])
    .arg(rot[2][0]).arg(rot[2][1]).arg(rot[2][2]).arg(rot[2][3])
    .arg(rot[3][0]).arg(rot[3][1]).arg(rot[3][2]).arg(rot[3][3]);
  shotElem.setAttribute( "RotationMatrix", str);

  const vcg::Camera<ScalarType> &cam = shot.Intrinsics;

  shotElem.setAttribute("CameraType", cam.cameraType);
  shotElem.setAttribute("BinaryData", 0);

  shotElem.setAttribute( "FocalMm", cam.FocalMm);

  str = QString("%1 %2").arg(cam.k[0]).arg(cam.k[1]);
  shotElem.setAttribute( "LensDistortion", str);

  str = QString("%1 %2").arg(cam.PixelSizeMm[0]).arg(cam.PixelSizeMm[1]);
  shotElem.setAttribute( "PixelSizeMm", str);

  str = QString("%1 %2").arg(cam.ViewportPx[0]).arg(cam.ViewportPx[1]);
  shotElem.setAttribute( "ViewportPx", str);

  str = QString("%1 %2").arg(cam.CenterPx[0]).arg(cam.CenterPx[1]);
  shotElem.setAttribute( "CenterPx", str);

  //scale correction should no more exist !!!
  //str = QString("%1").arg((double) 1);
  //shotElem.setAttribute( "ScaleCorr", str);
  return shotElem;
}


template <class ShotType>
QDomElement WriteShotToQDomNodeBinary(
  const ShotType &shot, /// the shot to be written node
  QDomDocument &doc) /// The XML node to be read
{
  typedef typename ShotType::ScalarType ScalarType;

  QDomElement shotElem = doc.createElement("VCGCamera");
  vcg::Point3<ScalarType> tra = -(shot.Extrinsics.Tra());

  QByteArray value = QByteArray::fromRawData((char *)tra.V(), sizeof(ScalarType) * 3).toBase64();
  shotElem.setAttribute("TranslationVector", QString(value));
  
  vcg::Matrix44<ScalarType> rot = shot.Extrinsics.Rot();
  value = QByteArray::fromRawData((char *)rot.V(), sizeof(ScalarType) * 16).toBase64();
  shotElem.setAttribute("RotationMatrix", QString(value));

  const vcg::Camera<ScalarType> &cam = shot.Intrinsics;

  shotElem.setAttribute("CameraType", cam.cameraType);

  shotElem.setAttribute("BinaryData", 1);

  value = QByteArray::fromRawData((char *)&cam.FocalMm, sizeof(ScalarType)).toBase64();
  shotElem.setAttribute("FocalMm", QString(value));

  value = QByteArray::fromRawData((char *)&cam.k, sizeof(ScalarType) * 2).toBase64();
  shotElem.setAttribute("LensDistortion", QString(value));

  value = QByteArray::fromRawData((char *)&cam.PixelSizeMm, sizeof(ScalarType) * 2).toBase64();
  shotElem.setAttribute("PixelSizeMm", QString(value));

  value = QByteArray::fromRawData((char *)&cam.ViewportPx, sizeof(int) * 2).toBase64();
  shotElem.setAttribute("ViewportPx", QString(value));

  //str = QString("%1 %2").arg(cam.CenterPx[0]).arg(cam.CenterPx[1]);
  value = QByteArray::fromRawData((char *)&cam.CenterPx, sizeof(ScalarType) * 2).toBase64();
  shotElem.setAttribute("CenterPx", QString(value));

  //scale correction should no more exist !!!
  //str = QString("%1").arg((double) 1);
  //shotElem.setAttribute( "ScaleCorr", str);
  return shotElem;
}


#endif // SHOT_QT_H