forked from OpenDDS/OpenDDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShapesDialog.cpp
469 lines (398 loc) · 13.7 KB
/
ShapesDialog.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include "config.hpp"
#include "ShapesDialog.hpp"
#include <QtGui/QtGui>
#include <iostream>
#include <sstream>
#include <Circle.hpp>
#include <Square.hpp>
#include <Triangle.hpp>
#include <BouncingShapeDynamics.hpp>
#include <DDSShapeDynamics.hpp>
#include <dds/DCPS/Marked_Default_Qos.h>
#include <dds/DdsDcpsInfrastructureC.h>
#include "ace/config-all.h"
#include "ShapeTypeTypeSupportImpl.h"
#include <string>
#ifndef ACE_HAS_CPP11
#error ishapes requires C++11 or newer
#endif
using org::omg::dds::demo::ShapeType;
using org::omg::dds::demo::ShapeTypeTypeSupport_var;
using org::omg::dds::demo::ShapeTypeTypeSupportImpl;
using org::omg::dds::demo::ShapeTypeDataWriter;
using org::omg::dds::demo::ShapeTypeDataWriter_var;
using org::omg::dds::demo::ShapeTypeDataReader;
using org::omg::dds::demo::ShapeTypeDataReader_var;
static const float PI = 3.1415926535F;
static QColor color_[CN];
const char* const colorString_[] = {
"BLUE",
"RED",
"GREEN",
"ORANGE",
"YELLOW",
"MAGENTA",
"CYAN",
"GRAY",
"BLACK"
};
static const std::string circleTopicName("Circle");
static const std::string squareTopicName("Square");
static const std::string triangleTopicName("Triangle");
ShapesDialog::ShapesDialog(DDS::DomainParticipant_var participant,
const QosConfig& qosConfig,
int defaultSize)
: timer(this),
participant_(participant),
filterExpression_("(x BETWEEN %0 AND %1) AND (y BETWEEN %2 AND %3)")
{
DDS::TopicQos topic_qos;
participant->get_default_topic_qos(topic_qos);
topic_qos.history.kind = DDS::KEEP_LAST_HISTORY_QOS;
topic_qos.history.depth = 100;
// Register TypeSupport (ShapeType)
ShapeTypeTypeSupport_var ts =
new ShapeTypeTypeSupportImpl;
CORBA::String_var type_name = "ShapeType";
if (ts->register_type(participant, type_name) != DDS::RETCODE_OK) {
std::cerr << "Could not register type " << std::endl;
}
circleTopic_ =
participant->create_topic(circleTopicName.c_str(),
type_name,
topic_qos,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!circleTopic_) {
std::cerr << "Could not create topic " << circleTopicName << std::endl;
}
squareTopic_ =
participant->create_topic(squareTopicName.c_str(),
type_name,
topic_qos,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!squareTopic_) {
std::cerr << "Could not create topic " << squareTopicName << std::endl;
}
triangleTopic_ =
participant->create_topic(triangleTopicName.c_str(),
type_name,
topic_qos,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!triangleTopic_) {
std::cerr << "Could not create topic " << triangleTopicName << std::endl;
}
DDS::PublisherQos pub_qos;
participant->get_default_publisher_qos(pub_qos);
if (!qosConfig.partition_.empty()) {
pub_qos.partition.name.length(1);
pub_qos.partition.name[0] = qosConfig.partition_.c_str();
}
// Create Publisher
publisher_ =
participant->create_publisher(pub_qos,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!publisher_) {
std::cerr << "Could not create publisher " << std::endl;
}
writerQos_.setPublisher(publisher_);
if (qosConfig.xcdr1_) {
DDS::DataWriterQos default_dw_qos;
publisher_->get_default_datawriter_qos(default_dw_qos);
default_dw_qos.representation.value.length(1);
default_dw_qos.representation.value[0] = DDS::XCDR_DATA_REPRESENTATION;
publisher_->set_default_datawriter_qos(default_dw_qos);
}
DDS::SubscriberQos sub_qos;
participant->get_default_subscriber_qos(sub_qos);
if (pub_qos.partition.name.length()) {
sub_qos.partition.name = pub_qos.partition.name;
}
// Create Subscriber
subscriber_ =
participant->create_subscriber(sub_qos,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!subscriber_) {
std::cerr << "Could not create subscriber " << std::endl;
}
readerQos_.setSubscriber(subscriber_);
mainWidget.setupUi(this);
shapesWidget = new ShapesWidget(mainWidget.renderFrame);
shapesWidget->resize(mainWidget.renderFrame->size());
filterDialog_ = new FilterDialog(shapesWidget);
connect(&timer, SIGNAL(timeout()),
shapesWidget, SLOT(nextAnimationFrame()));
color_[BLUE] = QColor(0x33, 0x66, 0x99);
color_[RED] = QColor(0xCC, 0x33, 0x33);
color_[GREEN] = QColor(0x99, 0xCC, 0x66);
color_[ORANGE] = QColor(0xFF, 0x99, 0x33);
color_[YELLOW] = QColor(0xFF, 0xFF, 0x66);
color_[MAGENTA] = QColor(0xCC, 0x99, 0xCC);
color_[CYAN] = QColor(0x99, 0xCC, 0xFF);
color_[GRAY] = QColor(0x99, 0x99, 0x99);
color_[BLACK] = QColor(0x33, 0x33, 0x33);
const int index = mainWidget.colorList->findText("Orange");
mainWidget.colorList->setCurrentIndex(index);
if (defaultSize) {
mainWidget.sizeSlider->setValue(defaultSize);
}
timer.start(40);
if (!qosConfig.partition_.empty()) {
QString title = this->windowTitle();
title += (" PARTITION: " + qosConfig.partition_).c_str();
this->setWindowTitle(title);
}
}
ShapesDialog::~ShapesDialog() {
delete filterDialog_;
delete shapesWidget;
}
void
ShapesDialog::onPublishButtonClicked() {
int d = mainWidget.sizeSlider->value();
float speed = ((float)mainWidget.speedSlider->value()) / 9;
QRect rect(0, 0, d, d);
// TODO: This should be retrieved from the canvas...
QRect constr(0, 0, IS_WIDTH, IS_HEIGHT);
// QRect constr = this->geometry();
int x = constr.width() * ((float)rand() / RAND_MAX);
int y = constr.height() * ((float)rand() / RAND_MAX);
int cIdx = mainWidget.colorList->currentIndex();
int sIdx = mainWidget.wShapeList->currentIndex();
QBrush brush(color_[cIdx], Qt::SolidPattern);
// QPen pen(color_[(cIdx+1)%(CN-1)], 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
// color_[cIdx]
// QColor(33, 33, 33)
QPen pen(QColor(0xff, 0xff, 0xff), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
switch (sIdx) {
case CIRCLE: {
ShapeType shape;
shape.color = CORBA::string_dup(colorString_[cIdx]);
shape.shapesize = rect.width();
shape.x = x;
shape.y = y;
// Create DataWriter
DDS::DataWriter_var writer =
publisher_->create_datawriter(circleTopic_,
writerQos_.get_qos(),
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!writer) {
std::cerr << "Could not create data writer " << std::endl;
}
ShapeTypeDataWriter_var dw =
ShapeTypeDataWriter::_narrow(writer);
std::shared_ptr<BouncingShapeDynamics>
dynamics(new BouncingShapeDynamics(x, y, rect, constr, PI/6, speed,
shape, dw));
std::shared_ptr<Shape>
circle(new Circle(rect, dynamics, pen, brush));
shapesWidget->addShape(circle);
break;
}
case SQUARE: {
ShapeType shape;
shape.color = CORBA::string_dup(colorString_[cIdx]);
shape.shapesize = rect.width();
shape.x = x;
shape.y = y;
// Create DataWriter
DDS::DataWriter_var writer =
publisher_->create_datawriter(squareTopic_,
writerQos_.get_qos(),
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!writer) {
std::cerr << "Could not create data writer " << std::endl;
}
ShapeTypeDataWriter_var dw =
ShapeTypeDataWriter::_narrow(writer);
std::shared_ptr<BouncingShapeDynamics>
dynamics(new BouncingShapeDynamics(x, y, rect, constr, PI/6, speed,
shape, dw));
std::shared_ptr<Shape>
square(new Square(rect, dynamics, pen, brush));
shapesWidget->addShape(square);
// std::cout << "CREATE SQUARE" << std::endl;
break;
}
case TRIANGLE: {
ShapeType shape;
shape.color = CORBA::string_dup(colorString_[cIdx]);
shape.shapesize = rect.width();
shape.x = x;
shape.y = y;
// Create DataWriter
DDS::DataWriter_var writer =
publisher_->create_datawriter(triangleTopic_,
writerQos_.get_qos(),
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!writer) {
std::cerr << "Could not create data writer " << std::endl;
}
ShapeTypeDataWriter_var dw =
ShapeTypeDataWriter::_narrow(writer);
std::shared_ptr<BouncingShapeDynamics>
dynamics(new BouncingShapeDynamics(x, y, rect, constr, PI/6, speed,
shape, dw));
std::shared_ptr<Shape>
triangle(new Triangle(rect, dynamics, pen, brush));
shapesWidget->addShape(triangle);
// std::cout << "CREATE TRIANGLE" << std::endl;
break;
}
default:
break;
};
}
void
ShapesDialog::onSubscribeButtonClicked() {
const int d = mainWidget.sizeSlider->value();
QRect rect(0, 0, d, d);
QRect constr(0, 0, IS_WIDTH, IS_HEIGHT);
// QRect constr = this->geometry();
int x = static_cast<int>(constr.width() * ((float)rand() / RAND_MAX)*0.9F);
int y = static_cast<int>(constr.height() * ((float)rand() / RAND_MAX)*0.9F);
int sIdx = mainWidget.rShapeList->currentIndex();
QColor gray = QColor(0x99, 0x99, 0x99);
// QBrush brush(color_[BLACK], Qt::FDiagPattern);
QBrush brush(gray, Qt::SolidPattern);
QPen pen(QColor(0xff,0xff,0xff), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
filterParams_ = DDS::StringSeq();
if (filterDialog_->isEnabled()) {
QRect rect = filterDialog_->getFilterBounds();
std::string x0 = std::to_string(rect.x());
std::string x1 = std::to_string(rect.x() + rect.width());
std::string y0 = std::to_string(rect.y());
std::string y1 = std::to_string(rect.y() + rect.height());
filterParams_.length(4);
filterParams_[0] = x0.c_str();
filterParams_[1] = x1.c_str();
filterParams_[2] = y0.c_str();
filterParams_[3] = y1.c_str();
}
ShapeTypeDataReader_var dr;
DDS::TopicDescription_var topicDesc;
std::stringstream tname;
const std::string filter = !filterDialog_->isEnabled() ? "" :
(!filterDialog_->filterOutside() ?
"(x < %0) OR (x > %1) OR (y < %2) OR (y > %3)" :
filterExpression_) ;
if (filterDialog_->isEnabled()) {
std::cout << "creating cft with filter=" << filter << std::endl;
std::cout << "%0=" << filterParams_[0] << ", %1=" << filterParams_[1] << ", %2=" << filterParams_[2] << ", %3=" << filterParams_[3] << std::endl;
}
switch (sIdx) {
case CIRCLE: {
if (filterDialog_->isEnabled()) {
static unsigned int count = 0;
tname << "CFCircle" << ++count;
topicDesc = participant_->create_contentfilteredtopic(
tname.str().c_str(), circleTopic_, filter.c_str(), filterParams_);
}
else {
topicDesc = DDS::TopicDescription::_duplicate(circleTopic_.in());
}
break;
}
case SQUARE: {
if (filterDialog_->isEnabled()) {
static unsigned int count = 0;
tname << "CFSquare" << ++count;
topicDesc = participant_->create_contentfilteredtopic(
tname.str().c_str(), squareTopic_, filter.c_str(), filterParams_);
}
else {
topicDesc = DDS::TopicDescription::_duplicate(squareTopic_.in());
}
break;
}
case TRIANGLE: {
if (filterDialog_->isEnabled()) {
static unsigned int count = 0;
tname << "CFTriangle" << ++count;
topicDesc = participant_->create_contentfilteredtopic(
tname.str().c_str(), triangleTopic_, filter.c_str(), filterParams_);
}
else {
topicDesc = DDS::TopicDescription::_duplicate(triangleTopic_.in());
}
break;
}
default:
break;
}
DDS::DataReader_var reader =
subscriber_->create_datareader(topicDesc,
readerQos_.get_qos(),
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!reader) {
std::cerr << "Could not create data reader " << std::endl;
}
dr =
ShapeTypeDataReader::_narrow(reader);
if (!dr) {
std::cerr << "Data reader is not a ShapeTypeDataReader " << std::endl;
}
switch (sIdx) {
case CIRCLE: {
for (int i = 0; i < CN; ++i) {
std::string colorStr(colorString_[i]);
std::shared_ptr<DDSShapeDynamics>
dynamics(new DDSShapeDynamics(x, y, dr, colorStr, i));
std::shared_ptr<Shape>
circle(new Circle(rect, dynamics, pen, brush, true));
dynamics->setShape(circle);
shapesWidget->addShape(circle);
}
break;
}
case SQUARE: {
for (int i = 0; i < CN; ++i) {
std::string colorStr(colorString_[i]);
std::shared_ptr<DDSShapeDynamics>
dynamics(new DDSShapeDynamics(x, y, dr, colorStr, i));
std::shared_ptr<Shape>
square(new Square(rect, dynamics, pen, brush, true));
dynamics->setShape(square);
shapesWidget->addShape(square);
}
break;
}
case TRIANGLE: {
for (int i = 0; i < CN; ++i) {
std::string colorStr(colorString_[i]);
std::shared_ptr<DDSShapeDynamics>
dynamics(new DDSShapeDynamics(x, y, dr, colorStr, i));
std::shared_ptr<Shape>
triangle(new Triangle(rect, dynamics, pen, brush, true));
dynamics->setShape(triangle);
shapesWidget->addShape(triangle);
}
break;
}
default:
break;
}
}
void
ShapesDialog::onReaderQosButtonClicked()
{
readerQos_.setVisible(true);
}
void
ShapesDialog::onWriterQosButtonClicked()
{
writerQos_.setVisible(true);
}
void
ShapesDialog::onFilterButtonClicked()
{
filterDialog_->setVisible(true);
}