TcpClient.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "TcpClient.h"
30 #include <opencv2/opencv.hpp>
31 #include <QtGui/QTransform>
32 #include <QtCore/QPointF>
33 #include <QtCore/QTime>
34 
35 TcpClient::TcpClient(QObject *parent) :
36  QTcpSocket(parent),
37  blockSize_(0)
38 {
39  connect(this, SIGNAL(readyRead()), this, SLOT(readReceivedData()));
40  connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
41  connect(this, SIGNAL(disconnected()), this, SLOT(connectionLost()));
42 }
43 
45 {
46  QDataStream in(this);
47  in.setVersion(QDataStream::Qt_4_0);
48 
49  if (blockSize_ == 0)
50  {
51  if (this->bytesAvailable() < (int)sizeof(quint16))
52  {
53  return;
54  }
55 
56  in >> blockSize_;
57  }
58 
59  if (this->bytesAvailable() < blockSize_)
60  {
61  return;
62  }
63 
64  blockSize_ = 0;
65 
67  in >> info;
68 
69  printf("---\n");
70  if(info.objDetected_.size() == 0)
71  {
72  printf("(%s) No objects detected.\n",
73  QTime::currentTime().toString("HH:mm:ss.zzz").toStdString().c_str());
74  }
75  else
76  {
77  QMultiMap<int, QSize>::const_iterator iterSizes = info.objDetectedSizes_.constBegin();
78  for(QMultiMap<int, QTransform>::const_iterator iter=info.objDetected_.constBegin();
79  iter!=info.objDetected_.constEnd();
80  ++iter)
81  {
82  // get data
83  int id = (int)iter.key();
84  float objectWidth = iterSizes.value().width();
85  float objectHeight = iterSizes.value().height();
86 
87  // Find corners Qt
88  QTransform qtHomography = iter.value();
89 
90  QPointF qtTopLeft = qtHomography.map(QPointF(0,0));
91  QPointF qtTopRight = qtHomography.map(QPointF(objectWidth,0));
92  QPointF qtBottomLeft = qtHomography.map(QPointF(0,objectHeight));
93  QPointF qtBottomRight = qtHomography.map(QPointF(objectWidth,objectHeight));
94 
95  printf("(%s) Object %d detected, Qt corners at (%f,%f) (%f,%f) (%f,%f) (%f,%f)\n",
96  QTime::currentTime().toString("HH:mm:ss.zzz").toStdString().c_str(),
97  id,
98  qtTopLeft.x(), qtTopLeft.y(),
99  qtTopRight.x(), qtTopRight.y(),
100  qtBottomLeft.x(), qtBottomLeft.y(),
101  qtBottomRight.x(), qtBottomRight.y());
102 
103  // Example with OpenCV
104  if(0)
105  {
106  // Find corners OpenCV
107  cv::Mat cvHomography(3, 3, CV_32F);
108  cvHomography.at<float>(0,0) = qtHomography.m11();
109  cvHomography.at<float>(1,0) = qtHomography.m12();
110  cvHomography.at<float>(2,0) = qtHomography.m13();
111  cvHomography.at<float>(0,1) = qtHomography.m21();
112  cvHomography.at<float>(1,1) = qtHomography.m22();
113  cvHomography.at<float>(2,1) = qtHomography.m23();
114  cvHomography.at<float>(0,2) = qtHomography.m31();
115  cvHomography.at<float>(1,2) = qtHomography.m32();
116  cvHomography.at<float>(2,2) = qtHomography.m33();
117  std::vector<cv::Point2f> inPts, outPts;
118  inPts.push_back(cv::Point2f(0,0));
119  inPts.push_back(cv::Point2f(objectWidth,0));
120  inPts.push_back(cv::Point2f(0,objectHeight));
121  inPts.push_back(cv::Point2f(objectWidth,objectHeight));
122  cv::perspectiveTransform(inPts, outPts, cvHomography);
123 
124  printf("(%s) Object %d detected, CV corners at (%f,%f) (%f,%f) (%f,%f) (%f,%f)\n",
125  QTime::currentTime().toString("HH:mm:ss.zzz").toStdString().c_str(),
126  id,
127  outPts.at(0).x, outPts.at(0).y,
128  outPts.at(1).x, outPts.at(1).y,
129  outPts.at(2).x, outPts.at(2).y,
130  outPts.at(3).x, outPts.at(3).y);
131  }
132 
133  ++iterSizes;
134  }
135  }
136 }
137 
138 void TcpClient::displayError(QAbstractSocket::SocketError socketError)
139 {
140  switch (socketError)
141  {
142  case QAbstractSocket::RemoteHostClosedError:
143  break;
144  case QAbstractSocket::HostNotFoundError:
145  printf("Tcp error: The host was not found. Please "
146  "check the host name and port settings.\n");
147  break;
148  case QAbstractSocket::ConnectionRefusedError:
149  printf("The connection was refused by the peer. "
150  "Make sure Find-Object is running, "
151  "and check that the host name and port "
152  "settings are correct.\n");
153  break;
154  default:
155  printf("The following error occurred: %s.\n", this->errorString().toStdString().c_str());
156  break;
157  }
158 }
159 
161 {
162  printf("Connection lost!\n");
163 }
164 
void displayError(QAbstractSocket::SocketError socketError)
Definition: TcpClient.cpp:138
void readReceivedData()
Definition: TcpClient.cpp:44
TcpClient(QObject *parent=0)
Definition: TcpClient.cpp:35
void connectionLost()
Definition: TcpClient.cpp:160
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
quint16 blockSize_
Definition: TcpClient.h:45


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26