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
/* 
 * File:   CameraInfo.cpp
 * Author: Claas-Norman Ritter
 *
 * Created on 1. Februar 2009, 20:07
 */


#include "CameraInfo.h"

#include "Tools/Math/Common.h"

#include <Messages/Framework-Representations.pb.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>

using namespace naoth;
using namespace std;

double CameraInfo::getFocalLength() const
{
  double halfDiagLength = 0.5 * hypot(resolutionWidth, resolutionHeight);
  
  // senity check
  ASSERT(halfDiagLength > 0.0 && getOpeningAngleDiagonal() > 0.0);
  return halfDiagLength / tan(0.5 * getOpeningAngleDiagonal());
}

double CameraInfo::getOpeningAngleHeight() const {
  return 2.0 * atan2(static_cast<double>(resolutionHeight), getFocalLength() * 2.0);
}

double CameraInfo::getOpeningAngleWidth() const {
  return 2.0 * atan2(static_cast<double>(resolutionWidth), getFocalLength() * 2.0);
}

double CameraInfo::getOpticalCenterX() const {
  return static_cast<double>(resolutionWidth / 2);
}

double CameraInfo::getOpticalCenterY() const {
  return static_cast<double>(resolutionHeight / 2);
}

unsigned long CameraInfo::getSize() const {
  return resolutionHeight * resolutionWidth;
}

double CameraInfo::getOpeningAngleDiagonal() const {
  return params.openingAngleDiagonal;
}

void CameraInfo::print(ostream& stream) const
{
  stream << "Camera ID: " << cameraID << endl
         << "Resolution Width, Height: " << resolutionWidth << ", " << resolutionHeight << endl

         << "Opening Angle Diagonal (config): " << getOpeningAngleDiagonal() << "rad [" 
                                                << Math::toDegrees(getOpeningAngleDiagonal()) << " deg]" << endl

         << "Opening Angle Width (calculated): " << getOpeningAngleWidth() << " rad [" 
                                                 << Math::toDegrees(getOpeningAngleWidth()) << "deg]" << endl

         << "Opening Angle Height (calculated): " << getOpeningAngleHeight() << " rad [" 
                                                  << Math::toDegrees(getOpeningAngleHeight()) << "deg]" << endl

         << "Optical Center (calculated): " << getOpticalCenterX() << " Pixel, " << getOpticalCenterY() << " Pixel" << endl
         << "Focal Length (calculated): "<< getFocalLength() << " Pixel"<< endl
         /*
         << "Pixel Size: "<< params.pixelSize << " mm" << endl
         << "Focal Length: "<< params.focus << " mm" << endl
         << "Error to Center: " << params.xp << " mm, " << params.yp << " mm" << endl
         << "Radial Symmetric Error: " << params.k1 << " mm, " << params.k2 << " mm, " << params.k3 << " mm" << endl
         << "Radial Asymmetric and Tangential Error: " << params.p1 << " mm, " << params.p2 << " mm" << endl
         << "Affinity and Shearing Error: " << params.b1 << " mm, " << params.b2 << " mm" << endl
         */
         ;
}

CameraInfoParameter::CameraInfoParameter(const std::string& idName)
  :
  ParameterList("CameraInfo" + idName)
{
  PARAMETER_ANGLE_REGISTER(openingAngleDiagonal) = 72.6;

  /*
  //size of an Pixel on the chip
  PARAMETER_REGISTER(pixelSize) = 0.0036;
  //measured focus
  PARAMETER_REGISTER(focus) = 1.37;
  //moved middle point
  PARAMETER_REGISTER(xp) = 0.0;
  PARAMETER_REGISTER(yp) = 0.0;
  //radial symmetric distortion parameters
  PARAMETER_REGISTER(k1) = 0.0;
  PARAMETER_REGISTER(k2) = 0.0;
  PARAMETER_REGISTER(k3) = 0.0;
  //radial asymmetric and tangential distortion parameters
  PARAMETER_REGISTER(p1) = 0.0;
  PARAMETER_REGISTER(p2) = 0.0;
  //affinity and shearing distortion parameters
  PARAMETER_REGISTER(b1) = 0.0;
  PARAMETER_REGISTER(b2) = 0.0;
  */

  syncWithConfig();
}

void Serializer<CameraInfo>::serialize(const CameraInfo& representation, std::ostream& stream)
{
  naothmessages::CameraInfo msg;
  msg.set_resolutionwidth(static_cast<int>(representation.resolutionWidth));
  msg.set_resolutionheight(static_cast<int>(representation.resolutionHeight));
  msg.set_cameraid((naothmessages::CameraID) representation.cameraID);
  msg.set_openinganglediagonal(representation.params.openingAngleDiagonal);
  //msg.set_focus(representation.params.focus);
  //msg.set_pixelsize(representation.params.pixelSize);

  google::protobuf::io::OstreamOutputStream buf(&stream);
  msg.SerializeToZeroCopyStream(&buf);
}

void Serializer<CameraInfo>::deserialize(std::istream& stream, CameraInfo& r)
{
  naothmessages::CameraInfo msg;
  google::protobuf::io::IstreamInputStream buf(&stream);
  msg.ParseFromZeroCopyStream(&buf);
  
  r.resolutionWidth = static_cast<unsigned int>(msg.resolutionwidth());
  r.resolutionHeight = static_cast<unsigned int>(msg.resolutionheight());
  r.cameraID = (CameraInfo::CameraID) msg.cameraid();
  r.params.openingAngleDiagonal = msg.openinganglediagonal();
  //r.params.focus = msg.focus();
  //r.params.pixelSize = msg.pixelsize();
}