forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkQtLabelRenderStrategy.cxx
591 lines (521 loc) · 16.5 KB
/
vtkQtLabelRenderStrategy.cxx
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*=========================================================================
Program: Visualization Toolkit
Module: vtkQtLabelRenderStrategy.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkQtLabelRenderStrategy.h"
#include "vtkCoordinate.h"
#include "vtkImageData.h"
#include "vtkLabeledDataMapper.h"
#include "vtkLabelSizeCalculator.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPlaneSource.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkProperty2D.h"
#include "vtkQImageToImageSource.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkSmartPointer.h"
#include "vtkTextProperty.h"
#include "vtkTexture.h"
#include "vtkTexturedActor2D.h"
#include "vtkTextureMapToPlane.h"
#include "vtkTimerLog.h"
#include <QApplication>
#include <QFont>
#include <QFontMetrics>
#include <QImage>
#include <QMap>
#include <QPainter>
#include <QPair>
#include <QPixmap>
#include <QTextDocument>
#include <QTextStream>
vtkStandardNewMacro(vtkQtLabelRenderStrategy);
namespace
{
struct vtkQtLabelMapEntry
{
QString Text;
QColor Color;
QFont Font;
};
struct vtkQtLabelMapValue
{
QImage Image;
QRectF Bounds;
};
bool operator <(const vtkQtLabelMapEntry& a, const vtkQtLabelMapEntry& other)
{
if (a.Text != other.Text)
{
return a.Text < other.Text;
}
if (a.Color.red() != other.Color.red())
{
return a.Color.red() < other.Color.red();
}
if (a.Color.green() != other.Color.green())
{
return a.Color.green() < other.Color.green();
}
if (a.Color.blue() != other.Color.blue())
{
return a.Color.blue() < other.Color.blue();
}
if (a.Color.alpha() != other.Color.alpha())
{
return a.Color.alpha() < other.Color.alpha();
}
return a.Font < other.Font;
}
} // End of anonymous namespace
class vtkQtLabelRenderStrategy::Internals
{
public:
QImage* Image;
QPainter* Painter;
QMap<vtkQtLabelMapEntry, vtkQtLabelMapValue> Cache;
QFont TextPropertyToFont(vtkTextProperty* tprop)
{
QFont fontSpec(tprop->GetFontFamilyAsString());
fontSpec.setBold(tprop->GetBold());
fontSpec.setItalic(tprop->GetItalic());
fontSpec.setPixelSize(tprop->GetFontSize());
return fontSpec;
}
QColor TextPropertyToColor(double* fc, double opacity)
{
QColor textColor(
static_cast<int>(fc[0]*255),
static_cast<int>(fc[1]*255),
static_cast<int>(fc[2]*255),
static_cast<int>(opacity*255));
return textColor;
}
};
//----------------------------------------------------------------------------
vtkQtLabelRenderStrategy::vtkQtLabelRenderStrategy()
{
this->Implementation = new Internals();
this->Implementation->Image = new QImage(1, 1, QImage::Format_ARGB32_Premultiplied);
this->Implementation->Painter = new QPainter(this->Implementation->Image);
this->QImageToImage = vtkQImageToImageSource::New();
this->PlaneSource = vtkPlaneSource::New();
this->TextureMapToPlane = vtkTextureMapToPlane::New();
this->Texture = vtkTexture::New();
this->Mapper = vtkPolyDataMapper2D::New();
this->Actor = vtkTexturedActor2D::New();
this->QImageToImage->SetQImage(this->Implementation->Image);
this->PlaneSource->SetOrigin(0, 0, 0);
this->TextureMapToPlane->SetInputConnection(this->PlaneSource->GetOutputPort());
this->TextureMapToPlane->AutomaticPlaneGenerationOn();
this->TextureMapToPlane->SetSRange(0., 1.);
this->TextureMapToPlane->SetTRange(0., 1.);
this->Mapper->SetInputConnection(this->TextureMapToPlane->GetOutputPort());
this->Texture->SetInputConnection(this->QImageToImage->GetOutputPort());
this->Texture->PremultipliedAlphaOn();
this->Actor->SetTexture(this->Texture);
this->Actor->SetMapper(this->Mapper);
}
//----------------------------------------------------------------------------
vtkQtLabelRenderStrategy::~vtkQtLabelRenderStrategy()
{
delete this->Implementation->Painter;
delete this->Implementation->Image;
delete this->Implementation;
this->QImageToImage->Delete();
this->PlaneSource->Delete();
this->TextureMapToPlane->Delete();
this->Texture->Delete();
this->Mapper->Delete();
this->Actor->Delete();
}
void vtkQtLabelRenderStrategy::ReleaseGraphicsResources(vtkWindow *window)
{
this->Texture->ReleaseGraphicsResources(window);
this->Mapper->ReleaseGraphicsResources(window);
this->Actor->ReleaseGraphicsResources(window);
}
//double start_frame_time = 0;
//int start_frame_iter = 0;
//----------------------------------------------------------------------------
void vtkQtLabelRenderStrategy::StartFrame()
{
//vtkTimerLog* timer = vtkTimerLog::New();
//timer->StartTimer();
if (!this->Renderer)
{
vtkErrorMacro("Renderer must be set.");
return;
}
if (!this->Renderer->GetRenderWindow())
{
vtkErrorMacro("RenderWindow must be set.");
return;
}
int *size = this->Renderer->GetRenderWindow()->GetSize();
int width = size[0];
int height = size[1];
// If the render window is not antialiased then the text should not be
this->AntialiasText = this->Renderer->GetRenderWindow()->GetMultiSamples() > 0;
if (this->Implementation->Image->width() != width ||
this->Implementation->Image->height() != height)
{
this->Implementation->Painter->end();
delete this->Implementation->Image;
this->Implementation->Image = new QImage(width, height,
QImage::Format_ARGB32_Premultiplied);
this->Implementation->Painter->begin(this->Implementation->Image);
this->Implementation->Painter->setRenderHint(QPainter::TextAntialiasing,
this->AntialiasText);
this->Implementation->Painter->setRenderHint(QPainter::Antialiasing,
this->AntialiasText);
this->QImageToImage->SetQImage(this->Implementation->Image);
this->PlaneSource->SetPoint1(width, 0, 0);
this->PlaneSource->SetPoint2(0, height, 0);
}
this->Implementation->Image->fill(qRgba(0,0,0,0));
this->QImageToImage->Modified();
//timer->StopTimer();
//start_frame_time += timer->GetElapsedTime();
//start_frame_iter++;
//if (start_frame_iter % 10 == 0)
// {
// cerr << "StartFrame time: " << (start_frame_time / start_frame_iter) << endl;
// }
}
//double compute_bounds_time = 0;
//int compute_bounds_iter = 0;
//----------------------------------------------------------------------------
void vtkQtLabelRenderStrategy::ComputeLabelBounds(
vtkTextProperty* tprop, vtkUnicodeString label, double bds[4])
{
if (!QApplication::instance())
{
vtkErrorMacro("You must initialize a QApplication before using this class.");
return;
}
//vtkTimerLog* timer = vtkTimerLog::New();
//timer->StartTimer();
if (!tprop)
{
tprop = this->DefaultTextProperty;
}
QFont fontSpec = this->Implementation->TextPropertyToFont(tprop);
// This is the recommended Qt way of controlling text antialiasing.
if (this->AntialiasText)
{
fontSpec.setStyleStrategy(QFont::PreferAntialias);
}
else
{
fontSpec.setStyleStrategy(QFont::NoAntialias);
}
QString text = QString::fromUtf8(label.utf8_str());
QColor textColor = this->Implementation->TextPropertyToColor(tprop->GetColor(),
tprop->GetOpacity());
vtkQtLabelMapEntry key;
key.Font = fontSpec;
key.Text = text;
key.Color = textColor;
QRectF rect;
if (this->Implementation->Cache.contains(key))
{
rect = this->Implementation->Cache[key].Bounds;
}
else
{
QPainterPath path;
path.addText(0, 0, fontSpec, text);
rect = path.boundingRect();
this->Implementation->Cache[key].Bounds = rect;
}
bds[0] = 0;
bds[1] = rect.width();
bds[2] = 0;
bds[3] = rect.height();
bds[2] -= tprop->GetLineOffset();
bds[3] -= tprop->GetLineOffset();
// Take justification into account
double sz[2] = {bds[1] - bds[0], bds[3] - bds[2]};
switch (tprop->GetJustification())
{
case VTK_TEXT_LEFT:
break;
case VTK_TEXT_CENTERED:
bds[0] -= sz[0]/2;
bds[1] -= sz[0]/2;
break;
case VTK_TEXT_RIGHT:
bds[0] -= sz[0];
bds[1] -= sz[0];
break;
}
switch (tprop->GetVerticalJustification())
{
case VTK_TEXT_BOTTOM:
break;
case VTK_TEXT_CENTERED:
bds[2] -= sz[1]/2;
bds[3] -= sz[1]/2;
break;
case VTK_TEXT_TOP:
bds[2] -= sz[1];
bds[3] -= sz[1];
break;
}
//timer->StopTimer();
//compute_bounds_time += timer->GetElapsedTime();
//compute_bounds_iter++;
//if (compute_bounds_iter % 10000 == 0)
// {
// cerr << "ComputeLabelBounds time: " << (compute_bounds_time / compute_bounds_iter) << endl;
// }
}
//double render_label_time = 0;
//int render_label_iter = 0;
//----------------------------------------------------------------------------
void vtkQtLabelRenderStrategy::RenderLabel(
int x[2], vtkTextProperty* tprop, vtkUnicodeString label, int maxWidth)
{
if (!QApplication::instance())
{
vtkErrorMacro("You must initialize a QApplication before using this class.");
return;
}
//vtkTimerLog* timer = vtkTimerLog::New();
//timer->StartTimer();
// Determine if we can render the label to fit the width
QString origText = QString::fromUtf8(label.utf8_str());
QFont fontSpec = this->Implementation->TextPropertyToFont(tprop);
// This is the recommended Qt way of controlling text antialiasing.
if (this->AntialiasText)
{
fontSpec.setStyleStrategy(QFont::PreferAntialias);
}
else
{
fontSpec.setStyleStrategy(QFont::NoAntialias);
}
QFontMetrics fontMetric(fontSpec);
QString text = fontMetric.elidedText(origText, Qt::ElideRight, maxWidth);
if (origText.length() >= 8 && text.length() < 8)
{
// Too small to render.
return;
}
// Get properties from text property
double rotation = -tprop->GetOrientation();
QColor textColor = this->Implementation->TextPropertyToColor(tprop->GetColor(), tprop->GetOpacity());
int *size = this->Renderer->GetRenderWindow()->GetSize();
double h = size[1]-1;
double line_offset = tprop->GetLineOffset();
int shOff[2];
tprop->GetShadowOffset(shOff);
double sc[3];
tprop->GetShadowColor(sc);
QColor shadowColor = this->Implementation->TextPropertyToColor(sc, tprop->GetOpacity());
// Compute bounds and justification
QPainterPath path;
path.addText(0, 0, fontSpec, text);
QRectF bounds = path.boundingRect();
double delta_x = 0., delta_y = 0.;
switch( tprop->GetJustification() )
{
case VTK_TEXT_LEFT:
break;
case VTK_TEXT_CENTERED:
delta_x = -bounds.width()/2.0;
break;
case VTK_TEXT_RIGHT:
delta_x = -bounds.width();
break;
}
switch (tprop->GetVerticalJustification())
{
case VTK_TEXT_TOP:
delta_y = bounds.height() - bounds.bottom();
break;
case VTK_TEXT_CENTERED:
delta_y = bounds.height()/2.0 - bounds.bottom();
break;
case VTK_TEXT_BOTTOM:
delta_y = -bounds.bottom();
break;
}
QPainter* painter = this->Implementation->Painter;
painter->save();
painter->translate(x[0], h-x[1]);
painter->rotate(rotation);
painter->translate(delta_x, delta_y);
painter->translate(0., line_offset);
if (tprop->GetShadow())
{
painter->save();
painter->translate(shOff[0], -shOff[1]);
painter->fillPath(path, shadowColor);
painter->restore();
}
painter->fillPath(path, textColor);
painter->restore();
//timer->StopTimer();
//render_label_time += timer->GetElapsedTime();
//render_label_iter++;
//if (render_label_iter % 100 == 0)
// {
// cerr << "RenderLabel time: " << (render_label_time / render_label_iter) << endl;
// }
}
//----------------------------------------------------------------------------
void vtkQtLabelRenderStrategy::RenderLabel(
int x[2], vtkTextProperty* tprop, vtkUnicodeString label)
{
if (!QApplication::instance())
{
vtkErrorMacro("You must initialize a QApplication before using this class.");
return;
}
if (!this->Renderer)
{
vtkErrorMacro("Renderer must be set.");
return;
}
//vtkTimerLog* timer = vtkTimerLog::New();
//timer->StartTimer();
QString text = QString::fromUtf8(label.utf8_str());
QFont fontSpec = this->Implementation->TextPropertyToFont(tprop);
// This is the recommended Qt way of controlling text antialiasing.
if (this->AntialiasText)
{
fontSpec.setStyleStrategy(QFont::PreferAntialias);
}
else
{
fontSpec.setStyleStrategy(QFont::NoAntialias);
}
double rotation = -tprop->GetOrientation();
QColor textColor = this->Implementation->TextPropertyToColor(tprop->GetColor(), tprop->GetOpacity());
int shOff[2];
tprop->GetShadowOffset(shOff);
double pixelPadding = 2;
double pixelPaddingX = pixelPadding + shOff[0];
double pixelPaddingY = pixelPadding - shOff[1];
// Get image from cache
QImage* img = 0;
QRectF bounds;
vtkQtLabelMapEntry key;
key.Font = fontSpec;
key.Text = text;
key.Color = textColor;
if (this->Implementation->Cache.contains(key) && this->Implementation->Cache[key].Image.width() > 0)
{
img = &this->Implementation->Cache[key].Image;
bounds = this->Implementation->Cache[key].Bounds;
}
else
{
QPainterPath path;
path.addText(0, 0, fontSpec, text);
bounds = path.boundingRect();
this->Implementation->Cache[key].Bounds = bounds;
bounds.setWidth(bounds.width() + pixelPaddingX);
bounds.setHeight(bounds.height() + pixelPaddingY);
QTransform trans;
trans.rotate(rotation);
QRectF rotBounds = trans.mapRect(bounds);
this->Implementation->Cache[key].Image = QImage(static_cast<int>(rotBounds.width()), static_cast<int>(rotBounds.height()), QImage::Format_ARGB32_Premultiplied);
img = &this->Implementation->Cache[key].Image;
img->fill(qRgba(0,0,0,0));
QPainter p(img);
p.translate(-rotBounds.left(), -rotBounds.top());
p.rotate(rotation);
p.setRenderHint(QPainter::TextAntialiasing, this->AntialiasText);
p.setRenderHint(QPainter::Antialiasing, this->AntialiasText);
if (tprop->GetShadow())
{
p.save();
p.translate(shOff[0], -shOff[1]);
double sc[3];
tprop->GetShadowColor(sc);
QColor shadowColor = this->Implementation->TextPropertyToColor(sc, tprop->GetOpacity());
p.fillPath(path, shadowColor);
p.restore();
}
p.fillPath(path, textColor);
}
QPainter* painter = this->Implementation->Painter;
double delta_x = 0.;
switch( tprop->GetJustification() )
{
case VTK_TEXT_LEFT:
delta_x = bounds.width()/2.0;
break;
case VTK_TEXT_CENTERED:
break;
case VTK_TEXT_RIGHT:
delta_x = -bounds.width()/2.0;
break;
}
double delta_y = pixelPadding / 2.0;
switch (tprop->GetVerticalJustification())
{
case VTK_TEXT_TOP:
delta_y += bounds.height()/2.0;
break;
case VTK_TEXT_CENTERED:
break;
case VTK_TEXT_BOTTOM:
delta_y += -bounds.height()/2.0;
break;
}
int *size = this->Renderer->GetRenderWindow()->GetSize();
double h = size[1]-1;
double line_offset = tprop->GetLineOffset();
QRectF imgRect;
imgRect.setSize(img->size());
painter->save();
painter->translate(x[0], h-x[1]);
painter->translate(-imgRect.width()/2.0, -imgRect.height()/2.0);
painter->rotate(rotation);
painter->translate(delta_x, delta_y);
painter->rotate(-rotation);
painter->translate(0., line_offset);
painter->drawImage(imgRect, *img, imgRect);
painter->restore();
//timer->StopTimer();
//render_label_time += timer->GetElapsedTime();
//render_label_iter++;
//if (render_label_iter % 100 == 0)
// {
// cerr << "RenderLabel time: " << (render_label_time / render_label_iter) << endl;
// }
}
//double end_frame_time = 0;
//int end_frame_iter = 0;
//----------------------------------------------------------------------------
void vtkQtLabelRenderStrategy::EndFrame()
{
//vtkTimerLog* timer = vtkTimerLog::New();
//timer->StartTimer();
this->Actor->RenderOverlay(this->Renderer);
//timer->StopTimer();
//end_frame_time += timer->GetElapsedTime();
//end_frame_iter++;
//if (end_frame_iter % 10 == 0)
// {
// cerr << "EndFrame time: " << (end_frame_time / end_frame_iter) << endl;
// }
}
//----------------------------------------------------------------------------
void vtkQtLabelRenderStrategy::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}