ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
vtkBoundingRectContextDevice2D.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include "vtkAbstractContextItem.h"
11 #include "vtkContext2D.h"
12 #include "vtkImageData.h"
13 #include "vtkNew.h"
14 #include "vtkObjectFactory.h"
15 #include "vtkPen.h"
16 #include "vtkStdString.h"
17 
18 //-----------------------------------------------------------------------------
20 
21  //-----------------------------------------------------------------------------
23  : Initialized(false), DelegateDevice(NULL) {
24  this->Reset();
25 }
26 
27 //-----------------------------------------------------------------------------
29  if (this->DelegateDevice) {
30  this->DelegateDevice->Delete();
31  this->DelegateDevice = NULL;
32  }
33 }
34 
35 //-----------------------------------------------------------------------------
36 void vtkBoundingRectContextDevice2D::PrintSelf(ostream& os, vtkIndent indent) {
37  this->Superclass::PrintSelf(os, indent);
38 }
39 
40 //-----------------------------------------------------------------------------
42  this->Initialized = false;
43  this->BoundingRect = vtkRectf(0, 0, 0, 0);
44 }
45 
46 //-----------------------------------------------------------------------------
48  vtkAbstractContextItem* item, vtkViewport* viewport) {
49  if (!item || !viewport) {
50  return vtkRectf();
51  }
52 
53  vtkNew<vtkContextDevice2D> contextDevice;
54  vtkNew<vtkBoundingRectContextDevice2D> bbDevice;
55  bbDevice->SetDelegateDevice(contextDevice.Get());
56  bbDevice->Begin(viewport);
57  vtkNew<vtkContext2D> context;
58  context->Begin(bbDevice.Get());
59  item->Paint(context.Get());
60  context->End();
61  bbDevice->End();
62 
63  return bbDevice->GetBoundingRect();
64 }
65 
66 //-----------------------------------------------------------------------------
68  return this->BoundingRect;
69 }
70 
71 //-----------------------------------------------------------------------------
73  const vtkStdString& string) {
74  if (!this->DelegateDevice) {
75  vtkWarningMacro(<< "No DelegateDevice defined");
76  return;
77  }
78 
79  float bounds[4];
80  this->DelegateDevice->ComputeJustifiedStringBounds(string.c_str(), bounds);
81 
82  this->AddPoint(point[0] + bounds[0], point[1] + bounds[1]);
83  this->AddPoint(point[0] + bounds[0] + bounds[2],
84  point[1] + bounds[1] + bounds[3]);
85 }
86 
87 //-----------------------------------------------------------------------------
89  float* point, const vtkStdString& string) {
90  if (!this->DelegateDevice) {
91  vtkWarningMacro(<< "No DelegateDevice defined");
92  return;
93  }
94 
95  // Not sure if this will work for math text
96  float bounds[4];
97  this->DelegateDevice->ComputeJustifiedStringBounds(string.c_str(), bounds);
98 
99  this->AddPoint(point[0] + bounds[0], point[1] + bounds[1]);
100  this->AddPoint(point[0] + bounds[0] + bounds[2],
101  point[1] + bounds[1] + bounds[3]);
102 }
103 
104 //-----------------------------------------------------------------------------
106  float scale,
107  vtkImageData* image) {
108  this->AddPoint(p);
109  int* extent = image->GetExtent();
110  this->AddPoint(p[0] + scale * extent[1], p[1] + scale * extent[3]);
111 }
112 
113 //-----------------------------------------------------------------------------
115  const vtkRectf& pos, vtkImageData* vtkNotUsed(image)) {
116  this->AddPoint(pos.GetX(), pos.GetY());
117  this->AddPoint(pos.GetX() + pos.GetWidth(), pos.GetY() + pos.GetHeight());
118 }
119 
120 //-----------------------------------------------------------------------------
122  if (this->DelegateDevice) {
123  this->DelegateDevice->SetColor4(color);
124  }
125 }
126 
127 //-----------------------------------------------------------------------------
129  int properties) {
130  if (this->DelegateDevice) {
131  this->DelegateDevice->SetTexture(image, properties);
132  }
133 }
134 
135 //-----------------------------------------------------------------------------
137  if (this->DelegateDevice) {
138  this->DelegateDevice->SetPointSize(size);
139  }
140 }
141 
142 //-----------------------------------------------------------------------------
144  if (this->DelegateDevice) {
145  this->DelegateDevice->SetLineWidth(width);
146  }
147 }
148 
149 //-----------------------------------------------------------------------------
151  if (this->DelegateDevice) {
152  this->DelegateDevice->SetLineType(type);
153  }
154 }
155 
156 //-----------------------------------------------------------------------------
158  if (this->DelegateDevice) {
159  this->DelegateDevice->SetMatrix(m);
160  }
161 }
162 
163 //-----------------------------------------------------------------------------
165  if (this->DelegateDevice) {
166  this->DelegateDevice->GetMatrix(m);
167  }
168 }
169 
170 //-----------------------------------------------------------------------------
172  if (this->DelegateDevice) {
173  this->DelegateDevice->MultiplyMatrix(m);
174  }
175 }
176 
177 //-----------------------------------------------------------------------------
179  if (this->DelegateDevice) {
180  this->DelegateDevice->PushMatrix();
181  }
182 }
183 
184 //-----------------------------------------------------------------------------
186  if (this->DelegateDevice) {
187  this->DelegateDevice->PopMatrix();
188  }
189 }
190 
191 //-----------------------------------------------------------------------------
193  if (this->DelegateDevice) {
194  this->DelegateDevice->EnableClipping(enable);
195  }
196 }
197 
198 //-----------------------------------------------------------------------------
200  if (this->DelegateDevice) {
201  this->DelegateDevice->SetClipping(x);
202  }
203 }
204 
205 //-----------------------------------------------------------------------------
207  if (this->DelegateDevice) {
208  this->DelegateDevice->ApplyPen(pen);
209  }
210 }
211 
212 //-----------------------------------------------------------------------------
214  if (this->DelegateDevice) {
215  return this->DelegateDevice->GetPen();
216  }
217 
218  return NULL;
219 }
220 
221 //-----------------------------------------------------------------------------
223  if (this->DelegateDevice) {
224  this->DelegateDevice->ApplyBrush(brush);
225  }
226 }
227 
228 //-----------------------------------------------------------------------------
230  if (this->DelegateDevice) {
231  return this->DelegateDevice->GetBrush();
232  }
233 
234  return NULL;
235 }
236 
237 //-----------------------------------------------------------------------------
239  if (this->DelegateDevice) {
240  this->DelegateDevice->ApplyTextProp(prop);
241  }
242 }
243 
244 //-----------------------------------------------------------------------------
246  if (this->DelegateDevice) {
247  return this->DelegateDevice->GetTextProp();
248  }
249 
250  return NULL;
251 }
252 
253 //-----------------------------------------------------------------------------
255  int n,
256  unsigned char* colors,
257  int nc_comps) {
258  this->DrawLines(points, n, colors, nc_comps);
259 }
260 
261 //-----------------------------------------------------------------------------
263  float* f,
264  int n,
265  unsigned char* vtkNotUsed(colors),
266  int vtkNotUsed(nc_comps)) {
267  if (f == NULL) {
268  return;
269  }
270 
271  for (int i = 0; i < n; ++i) {
272  this->AddPoint(f + 2 * i);
273  }
274 }
275 
276 //-----------------------------------------------------------------------------
278  float* points,
279  int n,
280  unsigned char* vtkNotUsed(colors),
281  int vtkNotUsed(nc_comps)) {
282  if (points == NULL) {
283  return;
284  }
285 
286  for (int i = 0; i < n; ++i) {
287  this->AddPoint(points + 2 * i);
288  }
289 }
290 
291 //-----------------------------------------------------------------------------
293  vtkImageData* vtkNotUsed(sprite),
294  float* points,
295  int n,
296  unsigned char* vtkNotUsed(colors),
297  int vtkNotUsed(nc_comps)) {
298  if (points == NULL || this->DelegateDevice) {
299  return;
300  }
301 
302  // Point sprites are squares whose sides are the current pen's width
303  float penWidth = this->DelegateDevice->GetPen()->GetWidth();
304  float halfWidth = 0.5 * penWidth;
305  for (int i = 0; i < n; ++i) {
306  float x = points[2 * i + 0];
307  float y = points[2 * i + 1];
308  this->AddPoint(x - halfWidth, y - halfWidth);
309  this->AddPoint(x + halfWidth, y - halfWidth);
310  this->AddPoint(x - halfWidth, y + halfWidth);
311  this->AddPoint(x + halfWidth, y + halfWidth);
312  }
313 }
314 
315 //-----------------------------------------------------------------------------
317  bool vtkNotUsed(highlight),
318  float* points,
319  int n,
320  unsigned char* colors,
321  int nc_comps) {
322  if (points == NULL) {
323  return;
324  }
325 
326  this->DrawPointSprites(NULL, points, n, colors, nc_comps);
327 }
328 
329 //-----------------------------------------------------------------------------
331  float y,
332  float outRx,
333  float outRy,
334  float inRx,
335  float inRy,
336  float startAngle,
337  float stopAngle) {
338  if (outRy == 0.0f && outRx == 0.0f) {
339  // we make sure maxRadius will never be null.
340  return;
341  }
342 
343  // Add one point per degree of rotation, which is probably good enough.
344  while (startAngle <= stopAngle) {
345  double a = vtkMath::RadiansFromDegrees(startAngle);
346  this->AddPoint(outRx * cos(a) + x, outRy * sin(a) + y);
347  this->AddPoint(inRx * cos(a) + x, inRy * sin(a) + y);
348  startAngle += 1.0;
349  }
350 }
351 
352 //-----------------------------------------------------------------------------
354  float y,
355  float rX,
356  float rY,
357  float startAngle,
358  float stopAngle) {
359  // Add one point per degree of rotation, which is probably good enough.
360  while (startAngle <= stopAngle) {
361  double a = vtkMath::RadiansFromDegrees(startAngle);
362  this->AddPoint(rX * cos(a) + x, rY * sin(a) + y);
363  startAngle += 1.0;
364  }
365 }
366 
367 //-----------------------------------------------------------------------------
369  const vtkStdString& string, float bounds[4]) {
370  if (this->DelegateDevice) {
371  this->DelegateDevice->ComputeStringBounds(string, bounds);
372  }
373 }
374 
375 //-----------------------------------------------------------------------------
377  const char* string, float bounds[4]) {
378  if (this->DelegateDevice) {
379  this->DelegateDevice->ComputeJustifiedStringBounds(string, bounds);
380  }
381 }
382 
383 //-----------------------------------------------------------------------------
384 void vtkBoundingRectContextDevice2D::Begin(vtkViewport* viewport) {
385  if (this->DelegateDevice) {
386  this->DelegateDevice->Begin(viewport);
387  }
388 }
389 
390 //-----------------------------------------------------------------------------
392  if (this->DelegateDevice) {
393  this->DelegateDevice->End();
394  }
395 }
396 
397 //-----------------------------------------------------------------------------
399  if (this->DelegateDevice) {
400  return this->DelegateDevice->GetBufferIdMode();
401  }
402  return false;
403 }
404 
405 //-----------------------------------------------------------------------------
407  vtkAbstractContextBufferId* bufferId) {
408  if (this->DelegateDevice) {
409  this->DelegateDevice->BufferIdModeBegin(bufferId);
410  }
411 }
412 
413 //-----------------------------------------------------------------------------
415  if (this->DelegateDevice) {
416  this->DelegateDevice->BufferIdModeEnd();
417  }
418 }
419 
420 //-----------------------------------------------------------------------------
422  float point[2] = {x, y};
423  this->AddPoint(point);
424 }
425 
426 //-----------------------------------------------------------------------------
428  if (this->Initialized) {
429  this->BoundingRect.AddPoint(pt);
430  } else {
431  this->Initialized = true;
432  this->BoundingRect = vtkRectf(pt[0], pt[1], 0.0f, 0.0f);
433  }
434 }
435 
436 //-----------------------------------------------------------------------------
437 void vtkBoundingRectContextDevice2D::AddRect(const vtkRectf& rect) {
438  if (this->Initialized) {
439  this->BoundingRect.AddRect(rect);
440  } else {
441  this->Initialized = true;
442  this->BoundingRect = rect;
443  }
444 }
std::shared_ptr< core::Tensor > image
int width
int size
int points
char type
math::float4 color
#define NULL
void DrawLines(float *f, int n, unsigned char *colors=0, int nc_comps=0) override
void SetTexture(vtkImageData *image, int properties) override
void ComputeJustifiedStringBounds(const char *string, float bounds[4]) override
void ComputeStringBounds(const vtkStdString &string, float bounds[4]) override
void DrawImage(float p[2], float scale, vtkImageData *image) override
void DrawMathTextString(float *point, const vtkStdString &string) override
void ApplyTextProp(vtkTextProperty *prop) override
void BufferIdModeBegin(vtkAbstractContextBufferId *bufferId) override
void DrawString(float *point, const vtkStdString &string) override
void DrawPointSprites(vtkImageData *sprite, float *points, int n, unsigned char *colors=0, int nc_comps=0) override
void DrawPoly(float *points, int n, unsigned char *colors=0, int nc_comps=0) override
void DrawEllipticArc(float x, float y, float rX, float rY, float startAngle, float stopAngle) override
void DrawPoints(float *points, int n, unsigned char *colors=0, int nc_comps=0) override
void DrawEllipseWedge(float x, float y, float outRx, float outRy, float inRx, float inRy, float startAngle, float stopAngle) override
void MultiplyMatrix(vtkMatrix3x3 *m) override
void SetColor4(unsigned char color[4]) override
void DrawMarkers(int shape, bool highlight, float *points, int n, unsigned char *colors=0, int nc_comps=0) override
double colors[3]
a[190]
ImGuiContext * context
Definition: Window.cpp:76
normal_z y
normal_z x
vtkStandardNewMacro(vtkBoundingRectContextDevice2D) vtkBoundingRectContextDevice2D