Commit c2f54b97 authored by David C. Lonie's avatar David C. Lonie
Browse files

Add a vtkMatplotlib module.

* Isolates python dependency away from MathText
* Adds tests for MathText functionality.

Change-Id: Ib0f87248e7f73ead5d1904b72e7053537907bfd1
parent 82e58fc1
# Check that matplotlib is available:
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import matplotlib"
RESULT_VARIABLE IMPORT_MATPLOTLIB_EXITCODE)
set(MATPLOTLIB_FOUND FALSE)
if(${IMPORT_MATPLOTLIB_EXITCODE} EQUAL 0)
set(MATPLOTLIB_FOUND TRUE)
endif()
if(NOT MATPLOTLIB_FOUND)
message(FATAL_ERROR "Matplotlib not found! ('${PYTHON_EXECUTABLE} -c \"import matplotlib\"' failed.")
endif()
set(Module_SRCS
vtkMatplotlibMathTextUtilities.cxx
${CMAKE_CURRENT_BINARY_DIR}/vtkMatplotlibObjectFactory.cxx
)
set_source_files_properties(
vtkMatplotlibObjectFactory
vtkMatplotlibMathTextUtilities
WRAP_EXCLUDE
)
# Override some rendering classes. Create object factory:
list(APPEND vtk_module_overrides "vtkMathTextUtilities")
set(vtk_module_vtkMathTextUtilities_override "vtkMatplotlibMathTextUtilities")
# Now we iterate and create that class file...
foreach(_class ${vtk_module_overrides})
set(_override ${vtk_module_${_class}_override})
set(_vtk_override_includes "${_vtk_override_includes}
#include \"${_override}.h\"")
set(_vtk_override_creates "${_vtk_override_creates}
VTK_CREATE_CREATE_FUNCTION(${_override})")
set(_vtk_override_do "${_vtk_override_do}
this->RegisterOverride(\"${_class}\",
\"${_override}\",
\"Override for ${vtk-module} module\", 1,
vtkObjectFactoryCreate${_override});")
endforeach()
# Now lets create the object factory classes
string(TOUPPER ${vtk-module} VTK-MODULE)
configure_file(${VTK_CMAKE_DIR}/vtkObjectFactory.h.in
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.h)
configure_file(${VTK_CMAKE_DIR}/vtkObjectFactory.cxx.in
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.cxx)
vtk_module_library(${vtk-module} ${Module_SRCS})
set(GL2PSTests
TestGL2PSMathTextOutput.cxx
TestGL2PSMathTextScaling.cxx
TestMathTextActor.cxx
TestMathTextActor3D.cxx
)
if(VTK_DATA_ROOT)
# add tests that require data
set(MyTests
TestContextMathTextImage.cxx
TestRenderString.cxx
TestStringToPath.cxx
${GL2PSTests}
)
endif()
# Use the testing object factory, to reduce boilerplate code in tests.
include("${vtkTestingRendering_SOURCE_DIR}/vtkTestingObjectFactory.cmake")
vtk_module_test_executable(${vtk-module}CxxTests ${Tests})
set(TestsToRun ${Tests})
remove(TestsToRun CxxTests.cxx)
# Add all the executables
foreach(test ${TestsToRun})
get_filename_component(TName ${test} NAME_WE)
if(VTK_DATA_ROOT)
if(${${TName}Error})
set(_error_threshold ${${TName}Error})
else()
set(_error_threshold 10)
endif()
add_test(NAME ${vtk-module}Cxx-${TName}
COMMAND ${vtk-module}CxxTests ${TName}
-D ${VTK_DATA_ROOT}
-T ${VTK_TEST_OUTPUT_DIR}
-V Baseline/Rendering/${TName}.png
-E ${_error_threshold})
else()
add_test(NAME MathText-${TName}
COMMAND ${vtk-module}CxxTests ${TName})
endif()
endforeach()
# The GL2PS tests produce postscript output, which cannot be compared using
# the built-in image regression framework. Instead, calculate and compare
# md5 sums using a CMake script.
# The file(MD5 ...) subcommand is not available in CMake < 2.8.7
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" GREATER 2.8 OR
("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" EQUAL 2.8 AND
"${CMAKE_PATCH_VERSION}" GREATER 7))
set(CMAKE_HAS_FILE_MD5_SUBCOMMAND TRUE)
endif()
if(CMAKE_HAS_FILE_MD5_SUBCOMMAND AND GL2PSTests AND VTK_DATA_ROOT)
foreach(test ${GL2PSTests})
get_filename_component(TName ${test} NAME_WE)
add_test(NAME ${vtk-module}Cxx-${TName}-CheckMD5sum
COMMAND ${CMAKE_COMMAND}
-DTESTFILE=${VTK_BINARY_DIR}/Testing/Temporary/${TName}.ps
-DGLOBEXP=${VTK_DATA_ROOT}/Baseline/Rendering/${TName}*.ps
-DCLEANTESTFILE=1
-P ${VTK_SOURCE_DIR}/CMake/CompareFileMD5.cmake
)
set_tests_properties("${vtk-module}Cxx-${TName}-CheckMD5sum"
PROPERTIES
DEPENDS "${vtk-module}Cxx-${TName}"
REQUIRED_FILES
"${VTK_DATA_ROOT}/Baseline/Rendering/${TName}.ps;${VTK_BINARY_DIR}/Testing/Temporary/${TName}.ps"
FAIL_REGULAR_EXPRESSION "MATCH_FAIL"
)
endforeach()
endif()
/*=========================================================================
Program: Visualization Toolkit
Module: TestContextMathTextImage.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 "vtkMathTextUtilities.h"
#include "vtkContext2D.h"
#include "vtkContextItem.h"
#include "vtkContextScene.h"
#include "vtkContextView.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTextProperty.h"
//----------------------------------------------------------------------------
class ContextMathTextImageTest : public vtkContextItem
{
public:
static ContextMathTextImageTest *New();
vtkTypeMacro(ContextMathTextImageTest, vtkContextItem);
// Paint event for the chart, called whenever the chart needs to be drawn
virtual bool Paint(vtkContext2D *painter);
};
//----------------------------------------------------------------------------
int TestContextMathTextImage(int argc, char *argv[])
{
// Set up a 2D context view, context test object and add it to the scene
vtkNew<vtkContextView> view;
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
view->GetRenderWindow()->SetSize(400, 150);
vtkNew<ContextMathTextImageTest> test;
view->GetScene()->AddItem(test.GetPointer());
view->GetRenderWindow()->SetMultiSamples(0);
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
// Make our new derived class to draw a diagram
vtkStandardNewMacro(ContextMathTextImageTest)
// This function aims to test the primitives provided by the 2D API.
bool ContextMathTextImageTest::Paint(vtkContext2D *painter)
{
painter->GetTextProp()->SetColor(0.4, 0.6, 0.7);
painter->GetTextProp()->SetFontSize(60);
painter->DrawMathTextString(20, 20, "$\\left{\\frac{-b\\pm\\sqrt{b^2-4ac}}"
"{2a}\\right}$");
return true;
}
/*=========================================================================
Program: Visualization Toolkit
Module: TestGL2PSMathTextOutput.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 "vtkContext2D.h"
#include "vtkContextItem.h"
#include "vtkContextScene.h"
#include "vtkContextView.h"
#include "vtkGL2PSExporter.h"
#include "vtkIntArray.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPath.h"
#include "vtkPen.h"
#include "vtkPoints.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTestingInteractor.h"
#include "vtkTextProperty.h"
//----------------------------------------------------------------------------
class GL2PSMathTextOutputTest : public vtkContextItem
{
public:
static GL2PSMathTextOutputTest *New();
vtkTypeMacro(GL2PSMathTextOutputTest, vtkContextItem)
// Paint event for the chart, called whenever the chart needs to be drawn
virtual bool Paint(vtkContext2D *painter);
};
//----------------------------------------------------------------------------
int TestGL2PSMathTextOutput(int, char *[])
{
// Set up a 2D context view, context test object and add it to the scene
vtkNew<vtkContextView> view;
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
view->GetRenderWindow()->SetSize(800, 160);
vtkNew<GL2PSMathTextOutputTest> test;
view->GetScene()->AddItem(test.GetPointer());
view->GetRenderWindow()->SetMultiSamples(0);
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
vtkNew<vtkGL2PSExporter> exp;
exp->SetRenderWindow(view->GetRenderWindow());
exp->SetFileFormatToPS();
exp->CompressOff();
exp->SetSortToBSP();
exp->DrawBackgroundOn();
exp->Write3DPropsAsRasterImageOff();
std::string fileprefix = vtkTestingInteractor::TempDirectory +
std::string("/TestGL2PSMathTextOutput");
exp->SetFilePrefix(fileprefix.c_str());
exp->WriteTimeStampOff(); // Otherwise hashes won't match
exp->Write();
return EXIT_SUCCESS;
}
// Make our new derived class to draw a diagram
vtkStandardNewMacro(GL2PSMathTextOutputTest)
// This function aims to test the primitives provided by the 2D API.
bool GL2PSMathTextOutputTest::Paint(vtkContext2D *painter)
{
painter->GetTextProp()->SetColor(.7, .4, .5);
painter->GetTextProp()->SetFontSize(30);
painter->DrawString(20, 100, "Bezier curve");
painter->DrawMathTextString(20, 120, "$\\mathfrak{B\\'ezier\\/curve}:$");
painter->GetTextProp()->SetFontSize(25);
painter->DrawMathTextString(40, 20, "$B_{[0,n]}(t) = \\sum_{j=0}^{n}\\/t^j"
"\\left["
"\\frac{n!}{(n-j)!}\\sum_{i=0}^{j}\\/"
"\\frac{(-1)^{i+j}P_i}{i!(j-i)!}"
"\\right] = "
"(1-t)B_{[0,n-1]}(t) + tB_{[1,n]}(t)$");
return true;
}
/*=========================================================================
Program: Visualization Toolkit
Module: TestGL2PSMathTextScaling.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 "vtkBrush.h"
#include "vtkContext2D.h"
#include "vtkContextItem.h"
#include "vtkContextScene.h"
#include "vtkContextView.h"
#include "vtkGL2PSExporter.h"
#include "vtkIntArray.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPath.h"
#include "vtkPoints.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTestingInteractor.h"
#include "vtkTextProperty.h"
//----------------------------------------------------------------------------
class GL2PSMathTextScalingTest : public vtkContextItem
{
public:
static GL2PSMathTextScalingTest *New();
vtkTypeMacro(GL2PSMathTextScalingTest, vtkContextItem)
// Paint event for the chart, called whenever the chart needs to be drawn
virtual bool Paint(vtkContext2D *painter);
};
//----------------------------------------------------------------------------
int TestGL2PSMathTextScaling(int, char *[])
{
// Set up a 2D context view, context test object and add it to the scene
vtkNew<vtkContextView> view;
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
view->GetRenderWindow()->SetSize(400, 600);
vtkNew<GL2PSMathTextScalingTest> test;
view->GetScene()->AddItem(test.GetPointer());
view->GetRenderWindow()->SetMultiSamples(0);
vtkNew<vtkGL2PSExporter> exp;
exp->SetRenderWindow(view->GetRenderWindow());
exp->SetFileFormatToPS();
exp->CompressOff();
exp->SetSortToSimple();
exp->DrawBackgroundOn();
exp->Write3DPropsAsRasterImageOff();
std::string fileprefix = vtkTestingInteractor::TempDirectory +
std::string("/TestGL2PSMathTextScaling");
exp->SetFilePrefix(fileprefix.c_str());
exp->WriteTimeStampOff(); // Otherwise hashes won't match
exp->Write();
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
// Make our new derived class to draw a diagram
vtkStandardNewMacro(GL2PSMathTextScalingTest)
// This function aims to test the primitives provided by the 2D API.
bool GL2PSMathTextScalingTest::Paint(vtkContext2D *painter)
{
painter->GetBrush()->SetColor(50, 50, 128);
painter->DrawRect(0, 0, 400, 600);
const char *str = "$\\mathrm{Text}\\/\\frac{\\pi}{\\sum_i^\\infty\\/i^{-1}}$";
painter->GetTextProp()->SetColor(.7, .4, .5);
for (int i = 0; i < 10; ++i)
{
painter->GetTextProp()->SetFontSize(5+i*1.5);
painter->DrawMathTextString(20, 600-((pow(i, 1.2)+1)*40), str);
painter->DrawString(5, 600-((pow(i, 1.2)+1)*40), "Text");
}
return true;
}
/*=========================================================================
Program: Visualization Toolkit
Module: TestStringToPath.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 "vtkMathTextActor.h"
#include "vtkCamera.h"
#include "vtkGL2PSExporter.h"
#include "vtkNew.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkTestingInteractor.h"
#include "vtkTextProperty.h"
//----------------------------------------------------------------------------
int TestMathTextActor(int, char *[])
{
vtkNew<vtkMathTextActor> actor1;
actor1->SetInput("$\\langle\\psi_i\\mid\\psi_j\\rangle = \\delta_{ij}$");
actor1->GetTextProperty()->SetFontSize(36);
actor1->GetTextProperty()->SetOrientation(0.0);
actor1->GetTextProperty()->SetColor(0.8, 0.8, 0.6);
actor1->SetPosition(0, 0);
actor1->GetTextProperty()->SetVerticalJustificationToBottom();
actor1->GetTextProperty()->SetJustificationToLeft();
vtkNew<vtkMathTextActor> actor2;
actor2->SetInput("$\\langle\\psi_i\\mid\\psi_j\\rangle = \\delta_{ij}$");
actor2->GetTextProperty()->SetFontSize(36);
actor2->SetPosition(300, 300);
actor2->GetTextProperty()->SetColor(0.7, 0.3, 0.2);
actor2->GetTextProperty()->SetVerticalJustificationToCentered();
actor2->GetTextProperty()->SetJustificationToCentered();
actor2->GetTextProperty()->SetOrientation(90.0);
vtkNew<vtkMathTextActor> actor3;
actor3->SetInput("$\\langle\\psi_i\\mid\\psi_j\\rangle = \\delta_{ij}$");
actor3->GetTextProperty()->SetFontSize(36);
actor3->SetPosition(600, 600);
actor3->GetTextProperty()->SetColor(0.6, 0.5, 0.8);
actor3->GetTextProperty()->SetVerticalJustificationToTop();
actor3->GetTextProperty()->SetJustificationToRight();
vtkNew<vtkMathTextActor> actor4;
actor4->SetInput("$\\langle\\psi_i\\mid\\psi_j\\rangle = \\delta_{ij}$");
actor4->GetTextProperty()->SetFontSize(22);
actor4->SetPosition(150, 300);
actor4->GetTextProperty()->SetColor(0.2, 0.6, 0.4);
actor4->GetTextProperty()->SetVerticalJustificationToCentered();
actor4->GetTextProperty()->SetJustificationToCentered();
actor4->GetTextProperty()->SetOrientation(45.0);
vtkNew<vtkMathTextActor> actor5;
actor5->ShallowCopy(actor4.GetPointer());
actor5->SetPosition(450, 300);
vtkNew<vtkRenderer> ren;
vtkNew<vtkRenderWindow> win;
win->AddRenderer(ren.GetPointer());
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(win.GetPointer());
ren->AddActor(actor1.GetPointer());
ren->AddActor(actor2.GetPointer());
ren->AddActor(actor3.GetPointer());
ren->AddActor(actor4.GetPointer());
ren->AddActor(actor5.GetPointer());
ren->SetBackground(0.0, 0.0, 0.0);
win->SetSize(600, 600);
win->Render();
vtkNew<vtkGL2PSExporter> exp;
exp->SetRenderWindow(win.GetPointer());
exp->SetFileFormatToPS();
exp->CompressOff();
exp->SetSortToSimple();
exp->DrawBackgroundOn();
std::string fileprefix = vtkTestingInteractor::TempDirectory +
std::string("/TestMathTextActor");
exp->SetFilePrefix(fileprefix.c_str());
exp->WriteTimeStampOff(); // Otherwise hashes won't match
exp->Write();
// Finally render the scene and compare the image to a reference image
win->SetMultiSamples(0);
win->GetInteractor()->Initialize();
win->GetInteractor()->Start();
}
/*=========================================================================
Program: Visualization Toolkit
Module: TestStringToPath.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 "vtkMathTextActor3D.h"
#include "vtkCamera.h"
#include "vtkGL2PSExporter.h"
#include "vtkNew.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkTestingInteractor.h"
#include "vtkTextProperty.h"
//----------------------------------------------------------------------------
int TestMathTextActor3D(int, char *[])
{
vtkNew<vtkMathTextActor3D> actor1;
actor1->SetInput("$\\int\\frac{1}{\\left(x+a\\right)^2}dx = -\\frac{1}{x+a}$");
actor1->GetTextProperty()->SetFontSize(36);
actor1->GetTextProperty()->SetColor(0.8, 0.8, 0.6);
actor1->SetPosition(-100, -25, 0);
actor1->RotateWXYZ(70, 0, 1, 0);
vtkNew<vtkMathTextActor3D> actor2;
actor2->SetInput("$\\langle\\psi_i\\mid\\psi_j\\rangle = \\delta_{ij}$");
actor2->GetTextProperty()->SetFontSize(40);
actor2->SetPosition(20, 0, -200);
actor2->RotateWXYZ(-70, 0, 1, 0);
actor2->GetTextProperty()->SetColor(0.7, 0.3, 0.2);
vtkNew<vtkMathTextActor3D> actor3;
actor3->SetInput("$\\frac{d}{dx}\\left(\\frac{u(x)}{v(x)}\\right) = "
"\\frac{v\\frac{du}{dx} + u\\frac{dv}{dx}}{v^2}$");
actor3->GetTextProperty()->SetFontSize(36);
actor3->SetPosition(-125, 75, -100);
actor3->RotateWXYZ(50, 1, .5, -.2);
actor3->GetTextProperty()->SetColor(0.6, 0.5, 0.8);
vtkNew<vtkMathTextActor3D> actor4;
actor4->SetInput("$\\hat{H}\\psi = \\left(-\\frac{\\hbar}{2m}\\nabla^2"
" + V(r)\\right) \\psi = \\psi\\cdot E $");
actor4->GetTextProperty()->SetFontSize(22);
actor4->SetPosition(-75, -100, 25);
actor4->RotateWXYZ(40, -.2, 1, .3);
actor4->GetTextProperty()->SetColor(0.2, 0.6, 0.4);
vtkNew<vtkMathTextActor3D> actor5;
actor5->SetInput("$\\frac{d}{dx}\\left(f(x_0)\\right) = \\lim_{h \\to 0}"
"\\frac{f(x_0 + h) - f(x_0)}{h}$");
actor5->GetTextProperty()->SetFontSize(40);
actor5->GetTextProperty()->SetColor(1, 1, 1);
actor5->SetPosition(-240, -110, -500);
actor5->RotateWXYZ(-25, 1, 0, 1);
vtkNew<vtkRenderer> ren;
vtkNew<vtkRenderWindow> win;
win->AddRenderer(ren.GetPointer());
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(win.GetPointer());
ren->AddActor(actor1.GetPointer());
ren->AddActor(actor2.GetPointer());
ren->AddActor(actor3.GetPointer());
ren->AddActor(actor4.GetPointer());
ren->AddActor(actor5.GetPointer());
ren->SetBackground(0.0, 0.0, 0.0);
ren->GetActiveCamera()->SetPosition(0, 0, 400);
ren->GetActiveCamera()->SetFocalPoint(0, 0, 0);
ren->GetActiveCamera()->SetViewUp(0, 1, 0);
win->SetSize(600, 600);
win->Render();
vtkNew<vtkGL2PSExporter> exp;
exp->SetRenderWindow(win.GetPointer());
exp->SetFileFormatToPS();
exp->CompressOff();
exp->SetSortToSimple();
exp->DrawBackgroundOn();
std::string fileprefix = vtkTestingInteractor::TempDirectory +
std::string("/TestMathTextActor3D");
exp->SetFilePrefix(fileprefix.c_str());
exp->WriteTimeStampOff(); // Otherwise hashes won't match
exp->Write();
// Finally render the scene and compare the image to a reference image