Skip to content

Commit 5a163cd

Browse files
authored
happy birthday VIBes (#145)
Happy birthday VIBes! Let's draw 🎂
1 parent 3d24db5 commit 5a163cd

File tree

6 files changed

+278
-1
lines changed

6 files changed

+278
-1
lines changed

client-api/C++/examples/all_commands.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ int main()
330330

331331
cout << "3." << endl;
332332

333+
VIBES_TEST( vibes::newFigure("Happy birthday") );
334+
VIBES_TEST( vibes::drawCake(1, -1, 0, 3, "black[red]") );
335+
VIBES_TEST( vibes::drawCake(3, 1, 45., 1, "black[blue]") );
336+
VIBES_TEST( vibes::drawCake(-1, -3, -20., 2, "black[green]") );
337+
VIBES_TEST( vibes::drawCake(3, -2, -180., 2.5, "black[yellow]") );
338+
VIBES_TEST( vibes::axisAuto() );
339+
333340

334341
/* vibes::Params p2 = vibesParams("action", "draw",
335342
"figure", "fig_name",

client-api/C++/src/vibes.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,22 @@ namespace vibes
763763
fflush(channel.get());
764764
}
765765

766+
void drawCake(const double &cx, const double &cy, const double &rot, const double &length, Params params)
767+
{
768+
beginDrawingIfNeeded();
769+
Vec2d vc = { cx, cy };
770+
Params msg;
771+
msg["action"] = "draw";
772+
msg["figure"] = params.pop("figure",current_fig);
773+
msg["shape"] = (params, "type", "cake",
774+
"center", vc,
775+
"length", length,
776+
"orientation", rot);
777+
778+
fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel.get());
779+
fflush(channel.get());
780+
}
781+
766782

767783
void newGroup(const std::string &name, Params params)
768784
{

client-api/C++/src/vibes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ namespace vibes {
393393
const double &,ulb, const double &, yub,
394394
const double &,width, const double &, height, const double &, rot);
395395

396+
/// Draw a Cake at position (cx,cy)
397+
VIBES_FUNC_COLOR_PARAM_4(drawCake,const double &,cx, const double &,cy, const double &,rot, const double &,length)
398+
396399
/// @}
397400
/// @name Objects grouping and deletion
398401
/// @{

client-api/python/vibes/vibes.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,23 @@ def drawRaster(cls, filename, xlb, yub, width, height, rot=0, **kwargs):
581581
if "color" in kwargs:
582582
msg["shape"]["format"] = kwargs['color']
583583
cls._write(msg, **kwargs)
584+
585+
@classmethod
586+
def drawCake(cls, cx, cy, length, color='r', **kwargs):
587+
"""Draw a cake centered at (cx, cy) with heading <heading> and size length
588+
589+
Args:
590+
cx,cy (double): position of the Cake
591+
heading (double): heading of the vehicle in degree
592+
lenght (double): length of the vehicle
593+
594+
"""
595+
msg = {'action': 'draw',
596+
'shape': {'type': 'cake',
597+
'center': [cx, cy],
598+
'length': length,
599+
'orientation': oritentation,
600+
'format': color
601+
}
602+
}
603+
cls._write(msg, **kwargs)

viewer/vibesgraphicsitem.cpp

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ VibesGraphicsItem * VibesGraphicsItem::newWithType(const QString type)
247247
{
248248
return new VibesGraphicsRaster();
249249
}
250+
else if (type == "cake")
251+
{
252+
return new VibesGraphicsCake();
253+
}
250254
return 0;
251255
}
252256

@@ -2268,3 +2272,219 @@ bool VibesGraphicsRaster::computeProjection(int dimX, int dimY)
22682272
// Update successful
22692273
return true;
22702274
}
2275+
2276+
//
2277+
// VibesGraphicsCake
2278+
//
2279+
2280+
bool VibesGraphicsCake::parseJsonGraphics(const QJsonObject &json)
2281+
{
2282+
// Now process shape-specific properties
2283+
// (we can only update properties of a shape, but mutation into another type is not supported)
2284+
if (json.contains("type"))
2285+
{
2286+
// Retrieve type
2287+
QString type = json["type"].toString();
2288+
2289+
// JSON type for VibesGraphicsCake is "cake"
2290+
if (type == "cake")
2291+
{
2292+
if (json.contains("center") && json.contains("length") && json.contains("orientation"))
2293+
{
2294+
int center_size = json["center"].toArray().size();
2295+
if (center_size == 2 && json["length"].toDouble() > 0.)
2296+
{
2297+
// Set dimension
2298+
this->_nbDim = center_size;
2299+
2300+
// Update successful
2301+
return true;
2302+
}
2303+
}
2304+
}
2305+
}
2306+
2307+
// Unknown or empty JSON, update failed
2308+
return false;
2309+
}
2310+
2311+
bool VibesGraphicsCake::computeProjection(int dimX, int dimY)
2312+
{
2313+
const QJsonObject & json = this->_json;
2314+
2315+
QJsonArray center = json["center"].toArray();
2316+
double length = json["length"].toDouble();
2317+
double orientation = json["orientation"].toDouble();
2318+
2319+
// Get shape color (or default if not specified)
2320+
const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
2321+
const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(), QString::number(7.0*jsonValue("LineWidth").toString().toDouble()/length));
2322+
2323+
const QBrush & cake_brush = vibesDefaults.brush("#ffde85");
2324+
const QPen & cake_pen = vibesDefaults.pen("black","-", "0.1");
2325+
const QPen & text_pen = vibesDefaults.pen("#525252","-", "0.5");
2326+
2327+
const QBrush & cream_brush = vibesDefaults.brush("#fcf7e8");
2328+
2329+
const QPen & empty_pen = vibesDefaults.pen("transparent","-", jsonValue("LineWidth").toString());
2330+
2331+
2332+
Q_ASSERT(json.contains("type"));
2333+
Q_ASSERT(json["type"].toString() == "cake");
2334+
2335+
Q_ASSERT(center.size() == 2);
2336+
Q_ASSERT(this->_nbDim == center.size());
2337+
Q_ASSERT(length > 0.);
2338+
2339+
// Get center
2340+
const QPointF & centerPoint = QPointF(center[dimX].toDouble(), center[dimY].toDouble());
2341+
2342+
/* This shape is inspired by the MOOS middleware GUI (see pMarineViewer) */
2343+
2344+
// If the shape has already been drawn, it has at least one child
2345+
// Update child items if they exist
2346+
if (this->childItems().size() > 0)
2347+
{
2348+
// This cake is already perfect, please don't change it
2349+
}
2350+
// Else draw the shape for the first time
2351+
else{
2352+
// Set body shape
2353+
{
2354+
QGraphicsEllipseItem * disk = new QGraphicsEllipseItem(center[dimX].toDouble()-length/2.0, center[dimY].toDouble()-length/8.0, length, length/4.);
2355+
2356+
disk->setPen(cake_pen);
2357+
disk->setBrush(cake_brush);
2358+
disk->setTransformOriginPoint(centerPoint);
2359+
disk->setRotation(orientation);
2360+
this->addToGroup(disk);
2361+
}
2362+
2363+
{
2364+
QGraphicsRectItem * rect = new QGraphicsRectItem(center[dimX].toDouble()-length/2.0, center[dimY].toDouble(), length, length/2.);
2365+
rect->setPen(empty_pen);
2366+
rect->setBrush(cake_brush);
2367+
rect->setTransformOriginPoint(centerPoint);
2368+
rect->setRotation(orientation);
2369+
this->addToGroup(rect);
2370+
2371+
}
2372+
2373+
{
2374+
QGraphicsEllipseItem * disk = new QGraphicsEllipseItem(center[dimX].toDouble()-length/2.0, center[dimY].toDouble()+length/4., length, length/4.);
2375+
2376+
disk->setPen(empty_pen);
2377+
disk->setBrush(cream_brush);
2378+
disk->setTransformOriginPoint(centerPoint);
2379+
disk->setRotation(orientation);
2380+
this->addToGroup(disk);
2381+
}
2382+
2383+
{
2384+
QGraphicsRectItem * rect = new QGraphicsRectItem(center[dimX].toDouble()-length/2.0, center[dimY].toDouble()+length/4. + length/8., length, length/8.);
2385+
rect->setPen(empty_pen);
2386+
rect->setBrush(cream_brush);
2387+
rect->setTransformOriginPoint(centerPoint);
2388+
rect->setRotation(orientation);
2389+
this->addToGroup(rect);
2390+
}
2391+
2392+
{
2393+
QPainterPath lef_line_details;
2394+
lef_line_details.moveTo(center[dimX].toDouble()-length/2.0, center[dimY].toDouble());
2395+
lef_line_details.lineTo(center[dimX].toDouble()-length/2.0, center[dimY].toDouble()+length/2.);
2396+
2397+
QGraphicsPathItem * left_line = new QGraphicsPathItem(lef_line_details);
2398+
2399+
QPainterPath right_line_details;
2400+
right_line_details.moveTo(center[dimX].toDouble()+length/2.0, center[dimY].toDouble());
2401+
right_line_details.lineTo(center[dimX].toDouble()+length/2.0, center[dimY].toDouble()+length/2.);
2402+
2403+
QGraphicsPathItem * right_line = new QGraphicsPathItem(right_line_details);
2404+
2405+
left_line->setPen(cake_pen);
2406+
right_line->setPen(cake_pen);
2407+
2408+
left_line->setTransformOriginPoint(centerPoint);
2409+
right_line->setTransformOriginPoint(centerPoint);
2410+
2411+
left_line->setRotation(orientation);
2412+
right_line->setRotation(orientation);
2413+
2414+
this->addToGroup(left_line);
2415+
this->addToGroup(right_line);
2416+
}
2417+
2418+
{
2419+
QGraphicsEllipseItem * disk = new QGraphicsEllipseItem(center[dimX].toDouble()-length/2.0, center[dimY].toDouble()+length/2.-length/8.0, length, length/4.);
2420+
2421+
disk->setPen(cake_pen);
2422+
disk->setBrush(cream_brush);
2423+
disk->setTransformOriginPoint(centerPoint);
2424+
disk->setRotation(orientation);
2425+
this->addToGroup(disk);
2426+
}
2427+
2428+
{
2429+
QGraphicsEllipseItem * disk_1 = new QGraphicsEllipseItem(center[dimX].toDouble()-length/2.0, center[dimY].toDouble()+length/2.-length/16., length/8., length/8.);
2430+
QGraphicsEllipseItem * disk_2 = new QGraphicsEllipseItem(center[dimX].toDouble()+length/2.0-length/8., center[dimY].toDouble()+length/2.-length/16., length/8., length/8.);
2431+
QGraphicsEllipseItem * disk_3 = new QGraphicsEllipseItem(center[dimX].toDouble()-length/2.0+length/4., center[dimY].toDouble()+length/2.+length/16., length/8., length/8.);
2432+
QGraphicsEllipseItem * disk_4 = new QGraphicsEllipseItem(center[dimX].toDouble()+length/8., center[dimY].toDouble()+length/2.+length/16., length/8., length/8.);
2433+
QGraphicsEllipseItem * disk_5 = new QGraphicsEllipseItem(center[dimX].toDouble()-length/2.0+length/4., center[dimY].toDouble()+length/2.-length/8., length/8., length/8.);
2434+
QGraphicsEllipseItem * disk_6 = new QGraphicsEllipseItem(center[dimX].toDouble()+length/8., center[dimY].toDouble()+length/2.-length/8., length/8., length/8.);
2435+
2436+
QGraphicsSimpleTextItem * text = new QGraphicsSimpleTextItem("10");
2437+
text->setTransform(QTransform(1, 0, 0, -1, center[dimX].toDouble()-length/4.0-length/8.0,center[dimY].toDouble()+length));
2438+
text->setScale(0.04*length);
2439+
text->setPen(text_pen);
2440+
text->setBrush(brush);
2441+
2442+
disk_1->setPen(pen);
2443+
disk_2->setPen(pen);
2444+
disk_3->setPen(pen);
2445+
disk_4->setPen(pen);
2446+
disk_5->setPen(pen);
2447+
disk_6->setPen(pen);
2448+
2449+
disk_1->setBrush(brush);
2450+
disk_2->setBrush(brush);
2451+
disk_3->setBrush(brush);
2452+
disk_4->setBrush(brush);
2453+
disk_5->setBrush(brush);
2454+
disk_6->setBrush(brush);
2455+
2456+
disk_1->setTransformOriginPoint(centerPoint);
2457+
disk_2->setTransformOriginPoint(centerPoint);
2458+
disk_3->setTransformOriginPoint(centerPoint);
2459+
disk_4->setTransformOriginPoint(centerPoint);
2460+
disk_5->setTransformOriginPoint(centerPoint);
2461+
disk_6->setTransformOriginPoint(centerPoint);
2462+
2463+
disk_1->setRotation(orientation);
2464+
disk_2->setRotation(orientation);
2465+
disk_3->setRotation(orientation);
2466+
disk_4->setRotation(orientation);
2467+
disk_5->setRotation(orientation);
2468+
disk_6->setRotation(orientation);
2469+
2470+
this->addToGroup(disk_1);
2471+
this->addToGroup(disk_2);
2472+
this->addToGroup(disk_3);
2473+
this->addToGroup(disk_4);
2474+
2475+
if (orientation == 0)
2476+
this->addToGroup(text);
2477+
2478+
this->addToGroup(disk_5);
2479+
this->addToGroup(disk_6);
2480+
2481+
}
2482+
2483+
2484+
}
2485+
2486+
2487+
2488+
// Update successful
2489+
return true;
2490+
}

viewer/vibesgraphicsitem.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ class VibesGraphicsItem
140140
VibesGraphicsTextType,
141141
// Do not remove the following value! It signals the end of VibesGraphicsItem types
142142
VibesGraphicsLastType,
143-
VibesGraphicsRasterType
143+
VibesGraphicsRasterType,
144+
// Happy birthday
145+
VibesGraphicsCakeType
144146
};
145147
// Constructor
146148
VibesGraphicsItem(QGraphicsItem * qGraphicsItem);
@@ -436,4 +438,13 @@ class VibesGraphicsRaster : public QGraphicsItemGroup, public VibesGraphicsItem
436438
bool computeProjection(int dimX, int dimY);
437439
};
438440

441+
class VibesGraphicsCake : public QGraphicsItemGroup, public VibesGraphicsItem
442+
{
443+
VIBES_GRAPHICS_ITEM(VibesGraphicsCake, QGraphicsItemGroup)
444+
VIBES_GEOMETRY_CHANGING_PROPERTIES("center","length","orientation")
445+
protected:
446+
bool parseJsonGraphics(const QJsonObject &json);
447+
bool computeProjection(int dimX, int dimY);
448+
};
449+
439450
#endif // VIBESGRAPHICSITEM_H

0 commit comments

Comments
 (0)