Commit 7e7306e4 authored by David C. Lonie's avatar David C. Lonie

Add background color/opacity settings to vtkTextProperty.

Change-Id: I583401ca6b2ee805f5ff77e26c146d5ba166450d
parent 034dc39a
......@@ -113,6 +113,10 @@ int TestGL2PSTextActor(int, char *[])
break;
}
actor->GetTextProperty()->SetColor(0.75, .2 + col * .26, .2 + row * .2);
actor->GetTextProperty()->SetBackgroundColor(0.0,
0.8 - col * .26,
.8 - row * .2);
actor->GetTextProperty()->SetBackgroundOpacity(0.25);
actor->SetPosition(x[col], y[row]);
setupTextActor(actor.GetPointer(), anchors.GetPointer());
ren->AddActor2D(actor.GetPointer());
......
......@@ -107,6 +107,10 @@ int TestGL2PSTextActor3D(int, char *[])
actor->GetTextProperty()->SetFontSize(20);
actor->GetTextProperty()->SetOrientation(45.0 * (3 * row + col));
actor->GetTextProperty()->SetColor(0.75, .2 + col * .26, .2 + row * .26);
actor->GetTextProperty()->SetBackgroundColor(0.,
1. - col * .26,
1. - row * .26);
actor->GetTextProperty()->SetBackgroundOpacity(0.25);
actor->SetPosition(x[col], y[row], 0.);
setupTextActor3D(actor.GetPointer(), anchors.GetPointer());
ren->AddActor(actor.GetPointer());
......
17db55890fe6ddfeee3bbc35df1ca551
632b71040c062eb22100ada4c81d72f3
371446ad76db1b69dd224fe70fede0de
e82e81aaa51e0e4d71ba878fffdeecf9
bff4fc5c05935949f53e50518adb32b5
b7d370e65738b172570c1d8617e85d0b
6eeee0c5b11a9d0dfd732422f40ef449
8d07293245771ce47e686dba6cca988a
......@@ -715,12 +715,12 @@ void vtkGL2PSExporter::DrawTextActor3D(vtkTextActor3D *textAct,
{
// Get path
const char *string = textAct->GetInput();
vtkNew<vtkPath> path;
vtkTextProperty *tprop = textAct->GetTextProperty();
vtkNew<vtkPath> textPath;
vtkTextRenderer *tren = vtkTextRenderer::GetInstance();
if (tren)
{
tren->StringToPath(textAct->GetTextProperty(), vtkStdString(string),
path.GetPointer());
tren->StringToPath(tprop, vtkStdString(string), textPath.GetPointer());
}
else
{
......@@ -732,18 +732,55 @@ void vtkGL2PSExporter::DrawTextActor3D(vtkTextActor3D *textAct,
// Get actor info
vtkMatrix4x4 *actorMatrix = textAct->GetMatrix();
double *actorBounds = textAct->GetBounds();
double rasterPos[3] = {(actorBounds[1] + actorBounds[0]) * 0.5,
(actorBounds[3] + actorBounds[2]) * 0.5,
(actorBounds[5] + actorBounds[4]) * 0.5};
double *dcolor = textAct->GetTextProperty()->GetColor();
unsigned char actorColor[4] = {
static_cast<unsigned char>(dcolor[0]*255),
static_cast<unsigned char>(dcolor[1]*255),
static_cast<unsigned char>(dcolor[2]*255),
static_cast<unsigned char>(textAct->GetTextProperty()->GetOpacity()*255)};
vtkGL2PSUtilities::Draw3DPath(path.GetPointer(), actorMatrix, rasterPos,
actorColor);
double textPos[3] = {(actorBounds[1] + actorBounds[0]) * 0.5,
(actorBounds[3] + actorBounds[2]) * 0.5,
(actorBounds[5] + actorBounds[4]) * 0.5};
double *fgColord = tprop->GetColor();
unsigned char fgColor[4] = {
static_cast<unsigned char>(fgColord[0] * 255),
static_cast<unsigned char>(fgColord[1] * 255),
static_cast<unsigned char>(fgColord[2] * 255),
static_cast<unsigned char>(tprop->GetOpacity() * 255)};
// Draw the background quad as a path:
if (tprop->GetBackgroundOpacity() > 0.f)
{
double *bgColord = tprop->GetBackgroundColor();
unsigned char bgColor[4] = {
static_cast<unsigned char>(bgColord[0] * 255),
static_cast<unsigned char>(bgColord[1] * 255),
static_cast<unsigned char>(bgColord[2] * 255),
static_cast<unsigned char>(tprop->GetBackgroundOpacity() * 255)};
vtkTextRenderer::Metrics metrics;
if (tren->GetMetrics(tprop, string, metrics))
{
vtkNew<vtkPath> bgPath;
bgPath->InsertNextPoint(static_cast<double>(metrics.TopLeft.GetX()),
static_cast<double>(metrics.TopLeft.GetY()),
0., vtkPath::MOVE_TO);
bgPath->InsertNextPoint(static_cast<double>(metrics.TopRight.GetX()),
static_cast<double>(metrics.TopRight.GetY()),
0., vtkPath::LINE_TO);
bgPath->InsertNextPoint(static_cast<double>(metrics.BottomRight.GetX()),
static_cast<double>(metrics.BottomRight.GetY()),
0., vtkPath::LINE_TO);
bgPath->InsertNextPoint(static_cast<double>(metrics.BottomLeft.GetX()),
static_cast<double>(metrics.BottomLeft.GetY()),
0., vtkPath::LINE_TO);
bgPath->InsertNextPoint(static_cast<double>(metrics.TopLeft.GetX()),
static_cast<double>(metrics.TopLeft.GetY()),
0., vtkPath::LINE_TO);
vtkGL2PSUtilities::Draw3DPath(bgPath.GetPointer(), actorMatrix, textPos,
bgColor);
}
}
// Draw the text path:
vtkGL2PSUtilities::Draw3DPath(textPath.GetPointer(), actorMatrix, textPos,
fgColor);
}
void vtkGL2PSExporter::DrawTextMapper(vtkTextMapper *textMap,
......@@ -855,7 +892,7 @@ void vtkGL2PSExporter::DrawViewportTextOverlay(const char *string,
glViewport(viewportPixels[0], viewportPixels[1],
viewportSpread[0], viewportSpread[1]);
vtkGL2PSUtilities::DrawString(string, tprop, textPos);
vtkGL2PSUtilities::DrawString(string, tprop, textPos, textPos[2] + 1e-6);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
......
691dadad521a920ac166ef97da4564ab
b3a108656cae9e6f01016395f5ce4e57
......@@ -26,6 +26,12 @@ vtkTextProperty::vtkTextProperty()
this->Opacity = 1.0;
this->BackgroundColor[0] = 0.0;
this->BackgroundColor[1] = 0.0;
this->BackgroundColor[2] = 0.0;
this->BackgroundOpacity = 0.0;
this->FontFamilyAsString = 0;
this->FontFile = NULL;
this->SetFontFamilyAsString( "Arial" );
......@@ -46,6 +52,7 @@ vtkTextProperty::vtkTextProperty()
this->Orientation = 0.0;
}
//----------------------------------------------------------------------------
vtkTextProperty::~vtkTextProperty()
{
this->SetFontFamilyAsString(NULL);
......@@ -63,6 +70,9 @@ void vtkTextProperty::ShallowCopy(vtkTextProperty *tprop)
this->SetColor(tprop->GetColor());
this->SetOpacity(tprop->GetOpacity());
this->SetBackgroundColor(tprop->GetBackgroundColor());
this->SetBackgroundOpacity(tprop->GetBackgroundOpacity());
this->SetFontFamilyAsString(tprop->GetFontFamilyAsString());
this->SetFontFile(tprop->GetFontFile());
this->SetFontSize(tprop->GetFontSize());
......@@ -106,6 +116,13 @@ void vtkTextProperty::PrintSelf(ostream& os, vtkIndent indent)
os << indent << "Opacity: " << this->Opacity << "\n";
os << indent << "BackgroundColor: ("
<< this->BackgroundColor[0] << ", "
<< this->BackgroundColor[1] << ", "
<< this->BackgroundColor[2] << ")\n";
os << indent << "BackgroundOpacity: " << this->BackgroundOpacity << "\n";
os << indent << "FontFamilyAsString: "
<< (this->FontFamilyAsString ? this->FontFamilyAsString : "(null)") << endl;
os << indent << "FontFile: "
......
......@@ -49,6 +49,17 @@ public:
vtkSetMacro(Opacity,double);
vtkGetMacro(Opacity,double);
// Description:
// The background color.
vtkSetVector3Macro(BackgroundColor, double);
vtkGetVector3Macro(BackgroundColor, double);
// Description:
// The background opacity. 1.0 is totally opaque and 0.0 is completely
// transparent.
vtkSetMacro(BackgroundOpacity, double);
vtkGetMacro(BackgroundOpacity, double);
// Description:
// Set/Get the font family. Supports legacy three font family system.
// If the symbolic constant VTK_FONT_FILE is returned by GetFontFamily(), the
......@@ -157,6 +168,8 @@ protected:
double Color[3];
double Opacity;
double BackgroundColor[3];
double BackgroundOpacity;
char* FontFamilyAsString;
char* FontFile;
int FontSize;
......
......@@ -42,6 +42,8 @@
#include "vtkRenderingCoreModule.h" // For export macro
#include "vtkObject.h"
#include "vtkTuple.h" // For metrics struct
#include "vtkVector.h" // For metrics struct
class vtkImageData;
class vtkPath;
......@@ -67,6 +69,31 @@ private:
class VTKRENDERINGCORE_EXPORT vtkTextRenderer: public vtkObject
{
public:
struct Metrics
{
// Description:
// Construct a Metrics object with all members initialized to 0.
Metrics()
: BoundingBox(0),
TopLeft(0), TopRight(0), BottomLeft(0), BottomRight(0)
{
}
// Description:
// The axis-aligned bounding box of the rendered text and background, in
// pixels. The origin of the bounding box is the anchor point of the data
// when considering justification. Layout is { xMin, xMax, yMin, yMax }.
vtkTuple<int, 4> BoundingBox;
// Description:
// The corners of the rendered text (or background, if applicable), in pixels.
// Uses the same origin as BoundingBox.
vtkVector2i TopLeft;
vtkVector2i TopRight;
vtkVector2i BottomLeft;
vtkVector2i BottomRight;
};
vtkTypeMacro(vtkTextRenderer, vtkObject)
virtual void PrintSelf(ostream &os, vtkIndent indent);
......@@ -133,6 +160,24 @@ public:
return this->GetBoundingBoxInternal(tprop, str, bbox, dpi, backend);
}
// Description:
// Given a text property and a string, get some metrics for the rendered
// string.
// Some rendering backends need the DPI of the target. If it is not provided,
// a DPI of 120 is assumed.
// Return true on success, false otherwise.
bool GetMetrics(vtkTextProperty *tprop, const vtkStdString &str,
Metrics &metrics, int dpi = 120, int backend = Default)
{
return this->GetMetricsInternal(tprop, str, metrics, dpi, backend);
}
bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString &str,
Metrics &metrics, int dpi = 120, int backend = Default)
{
return this->GetMetricsInternal(tprop, str, metrics, dpi, backend);
}
// Description:
// Given a text property and a string, this function initializes the
// vtkImageData *data and renders it in a vtkImageData.
......@@ -224,6 +269,12 @@ protected:
virtual bool GetBoundingBoxInternal(vtkTextProperty *tprop,
const vtkUnicodeString &str,
int bbox[4], int dpi, int backend) = 0;
virtual bool GetMetricsInternal(vtkTextProperty *tprop,
const vtkStdString &str,
Metrics &metrics, int dpi, int backend) = 0;
virtual bool GetMetricsInternal(vtkTextProperty *tprop,
const vtkUnicodeString &str,
Metrics &metrics, int dpi, int backend) = 0;
virtual bool RenderStringInternal(vtkTextProperty *tprop,
const vtkStdString &str,
vtkImageData *data, int textDims[2],
......
......@@ -112,6 +112,10 @@ int TestTextActor(int, char *[])
break;
}
actor->GetTextProperty()->SetColor(0.75, .2 + col * .26, .2 + row * .2);
actor->GetTextProperty()->SetBackgroundColor(0.25, 0.4 - col * .13,
.5 - row * .1);
actor->GetTextProperty()->SetBackgroundOpacity(1.0);
actor->SetPosition(x[col], y[row]);
setupTextActor(actor.GetPointer(), anchors.GetPointer());
ren->AddActor2D(actor.GetPointer());
......
......@@ -106,6 +106,10 @@ int TestTextActor3D(int, char *[])
actor->GetTextProperty()->SetFontSize(20);
actor->GetTextProperty()->SetOrientation(45.0 * (3 * row + col));
actor->GetTextProperty()->SetColor(0.75, .2 + col * .26, .2 + row * .26);
actor->GetTextProperty()->SetBackgroundColor(0.,
1. - col * .26,
1. - row * .26);
actor->GetTextProperty()->SetBackgroundOpacity(0.25);
actor->SetPosition(x[col], y[row], 0.);
setupTextActor3D(actor.GetPointer(), anchors.GetPointer());
ren->AddActor(actor.GetPointer());
......
371446ad76db1b69dd224fe70fede0de
4e70849a90b83bc26f39aa60905bcad4
6eeee0c5b11a9d0dfd732422f40ef449
8d07293245771ce47e686dba6cca988a
This diff is collapsed.
......@@ -25,6 +25,7 @@
#include "vtkRenderingFreeTypeModule.h" // For export macro
#include "vtkObject.h"
#include "vtkTextRenderer.h" // For Metrics struct
class vtkImageData;
class vtkPath;
......@@ -97,11 +98,20 @@ public:
// box is the anchor point described by the horizontal and vertical
// justification text property variables.
// Returns true on success, false otherwise.
// @sa GetMetrics
bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString& str,
int bbox[4]);
bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString& str,
int bbox[4]);
// Description:
// Given a text property and a string, get the metrics of the rendered string.
// Returns true on success, false otherwise.
bool GetMetrics(vtkTextProperty *tprop, const vtkStdString& str,
vtkTextRenderer::Metrics &metrics);
bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString& str,
vtkTextRenderer::Metrics &metrics);
// Description:
// Given a text property and a string, this function initializes the
// vtkImageData *data and renders it in a vtkImageData. textDims, if provided,
......@@ -195,6 +205,11 @@ protected:
// receive the text stored in str
void PrepareImageData(vtkImageData *data, int bbox[4]);
// Description:
// Draw the background quad on the image.
void RenderBackground(vtkTextProperty *tprop, vtkImageData *image,
ImageMetaData &metaData);
// Description:
// Given a text property, get the corresponding FreeType size object
// (a structure storing both a face and a specific size metric).
......
......@@ -164,6 +164,119 @@ bool vtkMathTextFreeTypeTextRenderer::GetBoundingBoxInternal(
return false;
}
//------------------------------------------------------------------------------
bool vtkMathTextFreeTypeTextRenderer::GetMetricsInternal(
vtkTextProperty *tprop, const vtkStdString &str,
vtkTextRenderer::Metrics &metrics, int dpi, int backend)
{
if (!tprop)
{
vtkErrorMacro("No text property supplied!");
return false;
}
metrics = Metrics();
if (str.empty())
{
return true;
}
if (static_cast<Backend>(backend) == Default)
{
backend = this->DefaultBackend;
}
if (static_cast<Backend>(backend) == Detect)
{
backend = static_cast<int>(this->DetectBackend(str));
}
switch (static_cast<Backend>(backend))
{
case MathText:
if (this->HasMathText)
{
if (this->MathTextUtilities->GetMetrics(tprop, str.c_str(), dpi,
metrics))
{
return true;
}
}
vtkDebugMacro("MathText unavailable. Falling back to FreeType.");
case FreeType:
{
vtkStdString cleanString(str);
this->CleanUpFreeTypeEscapes(cleanString);
// Interpret string as UTF-8, use the UTF-16 GetBoundingBox overload:
return this->FreeTypeTools->GetMetrics(
tprop, vtkUnicodeString::from_utf8(cleanString), metrics);
}
default:
vtkDebugMacro("Unrecognized backend requested: " << backend);
break;
case Detect:
vtkDebugMacro("Unhandled 'Detect' backend requested!");
break;
}
return false;
}
//------------------------------------------------------------------------------
bool vtkMathTextFreeTypeTextRenderer::GetMetricsInternal(
vtkTextProperty *tprop, const vtkUnicodeString &str,
vtkTextRenderer::Metrics &metrics, int dpi, int backend)
{
if (!tprop)
{
vtkErrorMacro("No text property supplied!");
return false;
}
metrics = Metrics();
if (str.empty())
{
return true;
}
if (static_cast<Backend>(backend) == Default)
{
backend = this->DefaultBackend;
}
if (static_cast<Backend>(backend) == Detect)
{
backend = static_cast<int>(this->DetectBackend(str));
}
switch (static_cast<Backend>(backend))
{
case MathText:
if (this->HasMathText)
{
vtkDebugMacro("Converting UTF16 to UTF8 for MathText rendering.");
if (this->MathTextUtilities->GetMetrics(tprop, str.utf8_str(), dpi,
metrics))
{
return true;
}
}
vtkDebugMacro("MathText unavailable. Falling back to FreeType.");
case FreeType:
{
vtkUnicodeString cleanString(str);
this->CleanUpFreeTypeEscapes(cleanString);
return this->FreeTypeTools->GetMetrics(tprop, cleanString, metrics);
}
default:
vtkDebugMacro("Unrecognized backend requested: " << backend);
break;
case Detect:
vtkDebugMacro("Unhandled 'Detect' backend requested!");
break;
}
return false;
}
//------------------------------------------------------------------------------
bool vtkMathTextFreeTypeTextRenderer::RenderStringInternal(
vtkTextProperty *tprop, const vtkStdString &str, vtkImageData *data,
......
......@@ -54,6 +54,10 @@ protected:
bool GetBoundingBoxInternal(vtkTextProperty *tprop,
const vtkUnicodeString &str,
int bbox[4], int dpi, int backend);
bool GetMetricsInternal(vtkTextProperty *tprop, const vtkStdString &str,
Metrics &metrics, int dpi, int backend);
bool GetMetricsInternal(vtkTextProperty *tprop, const vtkUnicodeString &str,
Metrics &metrics, int dpi, int backend);
bool RenderStringInternal(vtkTextProperty *tprop, const vtkStdString &str,
vtkImageData *data, int textDims[2], int dpi,
int backend);
......
......@@ -23,6 +23,7 @@
#include "vtkRenderingFreeTypeModule.h" // For export macro
#include "vtkObject.h"
#include "vtkTextRenderer.h" // for metrics
class vtkImageData;
class vtkPath;
......@@ -74,6 +75,12 @@ public:
virtual bool GetBoundingBox(vtkTextProperty *tprop, const char *str,
unsigned int dpi, int bbox[4]) = 0;
// Description:
// Return the metrics for the rendered str, tprop, and dpi.
virtual bool GetMetrics(vtkTextProperty *tprop, const char *str,
unsigned int dpi,
vtkTextRenderer::Metrics &metrics) = 0;
// Description:
// Render the given string @a str into the vtkImageData @a data with a
// resolution of @a dpi. textDims, will be overwritten by the pixel width and
......
......@@ -231,16 +231,16 @@ void vtkGL2PSContextDevice2D::DrawEllipticArc(float x, float y,
void vtkGL2PSContextDevice2D::DrawString(float *point,
const vtkStdString &string)
{
double p[3] = {point[0], point[1], 0.f};
vtkGL2PSUtilities::DrawString(string.c_str(), this->TextProp, p);
double p[3] = {point[0], point[1], 0.};
vtkGL2PSUtilities::DrawString(string.c_str(), this->TextProp, p, 0.);
}
//-----------------------------------------------------------------------------
void vtkGL2PSContextDevice2D::DrawString(float *point,
const vtkUnicodeString &string)
{
double p[3] = {point[0], point[1], 0.f};
vtkGL2PSUtilities::DrawString(string.utf8_str(), this->TextProp, p);
double p[3] = {point[0], point[1], 0.};
vtkGL2PSUtilities::DrawString(string.utf8_str(), this->TextProp, p, 0.);
}
//-----------------------------------------------------------------------------
......
......@@ -42,7 +42,8 @@ float vtkGL2PSUtilities::PointSizeFactor = 5.f / 7.f;
float vtkGL2PSUtilities::LineWidthFactor = 5.f / 7.f;
void vtkGL2PSUtilities::DrawString(const char *str,
vtkTextProperty *tprop, double pos[])
vtkTextProperty *tprop, double pos[3],
double backgroundDepth)
{
if (!str)
{
......@@ -58,6 +59,43 @@ void vtkGL2PSUtilities::DrawString(const char *str,
return;
}
// Draw the background if needed:
if (tprop->GetBackgroundOpacity() > 0.)
{
vtkTextRenderer::Metrics metrics;
if (tren->GetMetrics(tprop, str, metrics))
{
double bgPos[3] = { pos[0], pos[1], backgroundDepth };
vtkGL2PSUtilities::ProjectPoint(bgPos);
double bgVerts[12];
bgVerts[0] = bgPos[0] + static_cast<double>(metrics.TopLeft[0]);
bgVerts[1] = bgPos[1] + static_cast<double>(metrics.TopLeft[1]);
bgVerts[2] = bgPos[2];
bgVerts[3] = bgPos[0] + static_cast<double>(metrics.BottomLeft[0]);
bgVerts[4] = bgPos[1] + static_cast<double>(metrics.BottomLeft[1]);
bgVerts[5] = bgPos[2];
bgVerts[6] = bgPos[0] + static_cast<double>(metrics.BottomRight[0]);
bgVerts[7] = bgPos[1] + static_cast<double>(metrics.BottomRight[1]);
bgVerts[8] = bgPos[2];
bgVerts[9] = bgPos[0] + static_cast<double>(metrics.TopRight[0]);
bgVerts[10] = bgPos[1] + static_cast<double>(metrics.TopRight[1]);
bgVerts[11] = bgPos[2];
vtkGL2PSUtilities::UnprojectPoints(bgVerts, 12);
glDisable(GL_LIGHTING);
glDisableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4d(tprop->GetBackgroundColor()[0],
tprop->GetBackgroundColor()[1],
tprop->GetBackgroundColor()[2],
tprop->GetBackgroundOpacity());
glVertexPointer(3, GL_DOUBLE, 0, bgVerts);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
}
bool isMath = tren->DetectBackend(str) == vtkTextRenderer::MathText;
if (!isMath && !vtkGL2PSUtilities::TextAsPath)
{
......@@ -95,7 +133,6 @@ void vtkGL2PSUtilities::DrawString(const char *str,
static_cast<unsigned char>(rgbd[2]*255),
static_cast<unsigned char>(tprop->GetOpacity()*255)};
// Get device coordinate
double devicePos[3] = {pos[0], pos[1], pos[2]};
vtkGL2PSUtilities::ProjectPoint(devicePos);
......@@ -875,3 +912,70 @@ inline void vtkGL2PSUtilities::ProjectPoints(vtkPoints *points,
points->SetPoint(i, point);
}
}
void vtkGL2PSUtilities::UnprojectPoint(double point[4],
vtkMatrix4x4 *invTransformMatrix,
double viewportOrigin[],
double halfWidth, double halfHeight,
double zfact1, double zfact2)
{
point[0] = (point[0] - viewportOrigin[0] - halfWidth) / halfWidth;
point[1] = (point[1] - viewportOrigin[1] - halfHeight) / halfHeight;
point[2] = (point[2] - zfact2) / zfact1;
point[0] *= point[3];
point[1] *= point[3];
point[2] *= point[3];
invTransformMatrix->MultiplyPoint(point, point);
}
void vtkGL2PSUtilities::UnprojectPoints(double *points3D, vtkIdType numPoints,
vtkMatrix4x4 *actorMatrix)
{
// Build transformation matrix
double glMatrix[16];
glGetDoublev(GL_PROJECTION_MATRIX, glMatrix);
vtkNew<vtkMatrix4x4> projectionMatrix;
projectionMatrix->DeepCopy(glMatrix);
projectionMatrix->Transpose();
glGetDoublev(GL_MODELVIEW_MATRIX, glMatrix);
vtkNew<vtkMatrix4x4> modelviewMatrix;
modelviewMatrix->DeepCopy(glMatrix);
modelviewMatrix->Transpose();
vtkNew<vtkMatrix4x4> transformMatrix;
vtkMatrix4x4::Multiply4x4(projectionMatrix.GetPointer(),
modelviewMatrix.GetPointer(),
transformMatrix.GetPointer());
if (actorMatrix)
{
vtkMatrix4x4::Multiply4x4(transformMatrix.GetPointer(),
actorMatrix,
transformMatrix.GetPointer());
}
transformMatrix->Invert();
double viewport[4];
glGetDoublev(GL_VIEWPORT, viewport);
double depthRange[2];
glGetDoublev(GL_DEPTH_RANGE, depthRange);
const double halfWidth = viewport[2] * 0.5;
const double halfHeight = viewport[3] * 0.5;
const double zFactor1 = (depthRange[1] - depthRange[0]) * 0.5;
const double zFactor2 = (depthRange[1] + depthRange[0]) * 0.5;
double point[4];
for (vtkIdType i = 0; i < numPoints; ++i)
{
std::copy(points3D + (i * 3), points3D + ((i + 1) * 3), point);
point[3] = 1.0;
vtkGL2PSUtilities::UnprojectPoint(point, transformMatrix.GetPointer(),
viewport, halfWidth, halfHeight, zFactor1,
zFactor2);
std::copy(point, point + 3, points3D + (i * 3));
}
}
......@@ -43,8 +43,10 @@ public:
// Description:
// Format the text in str according to tprop and instruct GL2PS to draw it at
// world coordinate pos.
static void DrawString(const char *str, vtkTextProperty *tprop, double pos[3]);
// world coordinate pos. Background depth is the z value for the background
// quad, and should be in NDC space.
static void DrawString(const char *str, vtkTextProperty *tprop, double pos[3],
double backgroundDepth);
// Description:
// Translate the tprop's fontname into a Postscript font name.
......@@ -153,6 +155,15 @@ private:
double halfHeight, double zfact1, double zfact2);
static void ProjectPoints(vtkPoints *points,
vtkMatrix4x4 *actorMatrix = NULL);
// Description:
// Unproject the point from device coordinates into world coordinates.
// Input Z coordinate should be in NDC space.
static void UnprojectPoint(double point[4], vtkMatrix4x4 *invTransformMatrix,
double viewportOrigin[2], double halfWidth,
double halfHeight, double zfact1, double zfact2);
static void UnprojectPoints(double *points3D, vtkIdType numPoints,
vtkMatrix4x4 *actorMatrix = NULL);
};
<