Skip to content
Snippets Groups Projects
Commit 5238b987 authored by Andrew Maclean's avatar Andrew Maclean
Browse files

Simplifying viewport coordinates.

Former-commit-id: 39dcac2b
parent bbb71763
No related branches found
No related tags found
No related merge requests found
......@@ -9,22 +9,21 @@
#include <vtkSmartPointer.h>
// Select one of the following:
// #include <vtkParametricBoy.h>
// #include <vtkParametricConicSpiral.h>
// #include <vtkParametricCrossCap.h>
// #include <vtkParametricDini.h>
// #include <vtkParametricEllipsoid.h>
// #include <vtkParametricEnneper.h>
// #include <vtkParametricFigure8Klein.h>
// #include <vtkParametricKlein.h>
// #include <vtkParametricMobius.h>
// #include <vtkParametricRandomHills.h>
// #include <vtkParametricRoman.h>
// #include <vtkParametricSpline.h>
// #include <vtkParametricSuperEllipsoid.h>
// #include <vtkParametricSuperToroid.h>
#include <vtkParametricTorus.h>
//#include <vtkParametricBoy.h>
//#include <vtkParametricConicSpiral.h>
//#include <vtkParametricCrossCap.h>
//#include <vtkParametricDini.h>
//#include <vtkParametricEllipsoid.h>
//#include <vtkParametricEnneper.h>
//#include <vtkParametricFigure8Klein.h>
//#include <vtkParametricKlein.h>
//#include <vtkParametricMobius.h>
//#include <vtkParametricRandomHills.h>
//#include <vtkParametricRoman.h>
//#include <vtkParametricSpline.h>
//#include <vtkParametricSuperEllipsoid.h>
//#include <vtkParametricSuperToroid.h>
//#include <vtkParametricTorus.h>
int main(int, char* [])
{
......
......@@ -2,6 +2,7 @@
#include <vtkActor2D.h>
#include <vtkCamera.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkParametricFunctionSource.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
......@@ -28,12 +29,27 @@
#include <vtkParametricSuperEllipsoid.h>
#include <vtkParametricSuperToroid.h>
#include <vtkParametricTorus.h>
#include <vtkParametricTorus.h>
#include <array>
#include <vector>
int main(int, char* [])
{
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// Set the background color.
auto SetColor = [&colors](std::array<double, 3>& v,
std::string const& colorName) {
auto const scaleFactor = 256.0;
std::transform(std::begin(v), std::end(v), std::begin(v),
[=](double const& n) { return n / scaleFactor; });
colors->SetColor(colorName, v.data());
return;
};
std::array<double, 3> bkg{{25, 51, 102}};
SetColor(bkg, "BkgColor");
// Select one of the following (matching the selection above)
std::vector<vtkSmartPointer<vtkParametricFunction>> parametricObjects;
......@@ -115,7 +131,7 @@ int main(int, char* [])
vtkSmartPointer<vtkProperty> backProperty =
vtkSmartPointer<vtkProperty>::New();
backProperty->SetColor(1.0, 0.0, 0.0);
backProperty->SetColor(colors->GetColor3d("Red").GetData());
// Create a parametric function source, renderer, mapper, and actor
// for each object
......@@ -135,6 +151,7 @@ int main(int, char* [])
actors.push_back(vtkSmartPointer<vtkActor>::New());
actors[i]->SetMapper(mappers[i]);
actors[i]->GetProperty()->SetColor(colors->GetColor3d("White").GetData());
actors[i]->SetBackfaceProperty(backProperty);
textmappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
......@@ -146,11 +163,12 @@ int main(int, char* [])
textactors[i]->SetPosition(100, 16);
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
}
unsigned int gridDimensions = 4;
auto gridDimensions = 4;
// Need a renderer even if there is no actor
for (size_t i = parametricObjects.size(); i < gridDimensions * gridDimensions;
i++)
for (auto i = static_cast<int>(parametricObjects.size());
i < gridDimensions * gridDimensions; i++)
{
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
}
......@@ -158,34 +176,29 @@ int main(int, char* [])
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
int rendererSize = 200;
renderWindow->SetSize(
rendererSize * gridDimensions, rendererSize * gridDimensions);
renderWindow->SetSize(rendererSize * gridDimensions,
rendererSize * gridDimensions);
for (int row = 0; row < static_cast<int>(gridDimensions); row++)
for (auto row = 0; row < gridDimensions; row++)
{
for (int col = 0; col < static_cast<int>(gridDimensions); col++)
for (auto col = 0; col < gridDimensions; col++)
{
int index = row * gridDimensions + col;
// (xmin, ymin, xmax, ymax)
double viewport[4] = {static_cast<double>(col) * rendererSize /
(gridDimensions * rendererSize),
static_cast<double>(gridDimensions - (row + 1)) * rendererSize /
(gridDimensions * rendererSize),
static_cast<double>(col + 1) * rendererSize /
(gridDimensions * rendererSize),
static_cast<double>(gridDimensions - row) * rendererSize /
(gridDimensions * rendererSize)};
auto index = row * gridDimensions + col;
auto x0 = double(col) / gridDimensions;
auto y0 = double(gridDimensions - row - 1) / gridDimensions;
auto x1 = double(col + 1) / gridDimensions;
auto y1 = double(gridDimensions - row) / gridDimensions;
renderWindow->AddRenderer(renderers[index]);
renderers[index]->SetViewport(viewport);
renderers[index]->SetViewport(x0, y0, x1, y1);
if (index > static_cast<int>(parametricObjects.size() - 1))
{
continue;
}
renderers[index]->AddActor(actors[index]);
renderers[index]->AddActor(textactors[index]);
renderers[index]->SetBackground(.2, .3, .4);
renderers[index]->SetBackground(colors->GetColor3d("BkgColor").GetData());
renderers[index]->ResetCamera();
renderers[index]->GetActiveCamera()->Azimuth(30);
renderers[index]->GetActiveCamera()->Elevation(-30);
......
#include <vtkSmartPointer.h>
#include <vtkCamera.h>
#include <vtkPolyDataMapper.h>
#include <vtkTextMapper.h>
#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkCamera.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkParametricFunctionSource.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkTextProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkParametricFunctionSource.h>
#include <vtkMath.h>
#include <vtkPoints.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkTextMapper.h>
#include <vtkTextProperty.h>
#include <vtkParametricBohemianDome.h>
#include <vtkParametricBour.h>
......@@ -22,46 +23,54 @@
#include <vtkParametricPluckerConoid.h>
#include <vtkParametricPseudosphere.h>
#include <array>
#include <vector>
int main(int, char *[])
int main(int, char* [])
{
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// Set the background color.
auto SetColor = [&colors](std::array<double, 3>& v,
std::string const& colorName) {
auto const scaleFactor = 256.0;
std::transform(std::begin(v), std::end(v), std::begin(v),
[=](double const& n) { return n / scaleFactor; });
colors->SetColor(colorName, v.data());
return;
};
std::array<double, 3> bkg{{25, 51, 102}};
SetColor(bkg, "BkgColor");
// Select one of the following (matching the selection above)
std::vector<vtkSmartPointer<vtkParametricFunction> > parametricObjects;
std::vector<vtkSmartPointer<vtkParametricFunction>> parametricObjects;
parametricObjects.push_back(
vtkSmartPointer<vtkParametricBohemianDome>::New());
static_cast<vtkParametricBohemianDome*>(
parametricObjects.back().GetPointer())->SetA(5.0);
static_cast<vtkParametricBohemianDome*>(
parametricObjects.back().GetPointer())->SetB(1.0);
static_cast<vtkParametricBohemianDome*>(
parametricObjects.back().GetPointer())->SetC(2.0);
parametricObjects.push_back(
vtkSmartPointer<vtkParametricBour>::New());
static_cast<vtkParametricBohemianDome*>(parametricObjects.back().GetPointer())
->SetA(5.0);
static_cast<vtkParametricBohemianDome*>(parametricObjects.back().GetPointer())
->SetB(1.0);
static_cast<vtkParametricBohemianDome*>(parametricObjects.back().GetPointer())
->SetC(2.0);
parametricObjects.push_back(vtkSmartPointer<vtkParametricBour>::New());
parametricObjects.push_back(
vtkSmartPointer<vtkParametricCatalanMinimal>::New());
parametricObjects.push_back(
vtkSmartPointer<vtkParametricHenneberg>::New());
parametricObjects.push_back(
vtkSmartPointer<vtkParametricKuen>::New());
parametricObjects.push_back(vtkSmartPointer<vtkParametricHenneberg>::New());
parametricObjects.push_back(vtkSmartPointer<vtkParametricKuen>::New());
parametricObjects.push_back(
vtkSmartPointer<vtkParametricPluckerConoid>::New());
parametricObjects.push_back(
vtkSmartPointer<vtkParametricPseudosphere>::New());
std::vector<vtkSmartPointer<vtkParametricFunctionSource> >
std::vector<vtkSmartPointer<vtkParametricFunctionSource>>
parametricFunctionSources;
std::vector<vtkSmartPointer<vtkRenderer> >
renderers;
std::vector<vtkSmartPointer<vtkPolyDataMapper> >
mappers;
std::vector<vtkSmartPointer<vtkActor> >
actors;
std::vector<vtkSmartPointer<vtkTextMapper> >
textmappers;
std::vector<vtkSmartPointer<vtkActor2D> >
textactors;
std::vector<vtkSmartPointer<vtkRenderer>> renderers;
std::vector<vtkSmartPointer<vtkPolyDataMapper>> mappers;
std::vector<vtkSmartPointer<vtkActor>> actors;
std::vector<vtkSmartPointer<vtkTextMapper>> textmappers;
std::vector<vtkSmartPointer<vtkActor2D>> textactors;
// Create one text property for all
vtkSmartPointer<vtkTextProperty> textProperty =
......@@ -71,11 +80,11 @@ int main(int, char *[])
vtkSmartPointer<vtkProperty> backProperty =
vtkSmartPointer<vtkProperty>::New();
backProperty->SetColor(1.0, 0.0, 0.0);
backProperty->SetColor(colors->GetColor3d("Red").GetData());
// Create a parametric function source, renderer, mapper, and actor
// for each object
for(unsigned int i = 0; i < parametricObjects.size(); i++)
for (unsigned int i = 0; i < parametricObjects.size(); i++)
{
parametricFunctionSources.push_back(
vtkSmartPointer<vtkParametricFunctionSource>::New());
......@@ -88,6 +97,7 @@ int main(int, char *[])
actors.push_back(vtkSmartPointer<vtkActor>::New());
actors[i]->SetMapper(mappers[i]);
actors[i]->GetProperty()->SetColor(colors->GetColor3d("White").GetData());
actors[i]->SetBackfaceProperty(backProperty);
textmappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
......@@ -99,13 +109,13 @@ int main(int, char *[])
textactors[i]->SetPosition(100, 16);
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
}
unsigned int gridXDimensions = 4;
unsigned int gridYDimensions = 2;
auto gridDimensionsX = 4;
auto gridDimensionsY = 2;
// Need a renderer even if there is no actor
for(size_t i = parametricObjects.size();
i < gridXDimensions * gridYDimensions;
i++)
for (auto i = static_cast<int>(parametricObjects.size());
i < gridDimensionsX * gridDimensionsY; i++)
{
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
}
......@@ -113,30 +123,29 @@ int main(int, char *[])
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
int rendererSize = 200;
renderWindow->SetSize(
rendererSize*gridXDimensions, rendererSize*gridYDimensions);
renderWindow->SetSize(rendererSize * gridDimensionsX,
rendererSize * gridDimensionsY);
for(int row = 0; row < static_cast<int>(gridYDimensions); row++)
for (auto row = 0; row < gridDimensionsY; ++row)
{
for(int col = 0; col < static_cast<int>(gridXDimensions); col++)
for (auto col = 0; col < gridDimensionsX; ++col)
{
int index = row*gridXDimensions + col;
// (xmin, ymin, xmax, ymax)
double viewport[4] =
{static_cast<double>(col) * rendererSize / (gridXDimensions * rendererSize),
static_cast<double>(gridYDimensions - (row+1)) * rendererSize / (gridYDimensions * rendererSize),
static_cast<double>(col+1)*rendererSize / (gridXDimensions * rendererSize),
static_cast<double>(gridYDimensions - row) * rendererSize / (gridYDimensions * rendererSize)};
auto index = row * gridDimensionsX + col;
auto x0 = double(col) / gridDimensionsX;
auto y0 = double(gridDimensionsY - row - 1) / gridDimensionsY;
auto x1 = double(col + 1) / gridDimensionsX;
auto y1 = double(gridDimensionsY - row) / gridDimensionsY;
renderWindow->AddRenderer(renderers[index]);
renderers[index]->SetViewport(viewport);
if(index > static_cast<int>(parametricObjects.size() - 1))
renderers[index]->SetViewport(x0, y0, x1, y1);
if (index > static_cast<int>(parametricObjects.size() - 1))
{
continue;
}
renderers[index]->AddActor(actors[index]);
renderers[index]->AddActor(textactors[index]);
renderers[index]->SetBackground(.2, .3, .4);
renderers[index]->SetBackground(colors->GetColor3d("BkgColor").GetData());
renderers[index]->ResetCamera();
renderers[index]->GetActiveCamera()->Azimuth(30);
renderers[index]->GetActiveCamera()->Elevation(-30);
......
......@@ -76,7 +76,7 @@ int main(int argc, char* argv[])
colors->SetColor(colorName, v.data());
return;
};
std::array<double, 3> bkg{25, 51, 102};
std::array<double, 3> bkg{{25, 51, 102}};
SetColor(bkg, "BkgColor");
// Read a vtk file
......
......@@ -5,6 +5,9 @@ import vtk
class ParametricObjects(object):
def ParametricObjects(self):
colors = vtk.vtkNamedColors()
# Select one of the following functions.
# parametricObject = vtk.vtkParametricBoy()
# parametricObject = vtk.vtkParametricConicSpiral()
......@@ -32,6 +35,7 @@ class ParametricObjects(object):
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetDiffuseColor(colors.GetColor3d("Burlywood"))
# ------------------------------------------------------------
# Create the RenderWindow, Renderer and Interactor
......@@ -45,7 +49,7 @@ class ParametricObjects(object):
# add actors
ren.AddViewProp(actor)
ren.SetBackground(1, 1, 1) # Background color white
ren.SetBackground(colors.GetColor3d("Beige"))
# enable user interface interactor
iren.Initialize()
......
......@@ -58,6 +58,16 @@ class ParametricObjects(object):
textProperty.SetFontSize(10)
textProperty.SetJustificationToCentered()
colors = vtk.vtkNamedColors()
# Set the background color. Match those in VTKTextbook.pdf.
bkg = map(lambda x: x / 256.0, [25, 51, 102])
colors.SetColor("BkgColor", *bkg)
frontProperty = vtk.vtkProperty()
frontProperty.SetColor(colors.GetColor3d("Silver"))
backProperty = vtk.vtkProperty()
backProperty.SetColor(colors.GetColor3d("Red"))
# Create a parametric function source, renderer, mapper
# and actor for each object.
for idx, item in enumerate(parametricObjects):
......@@ -70,6 +80,8 @@ class ParametricObjects(object):
actors.append(vtk.vtkActor())
actors[idx].SetMapper(mappers[idx])
actors[idx].GetProperty().SetColor(colors.GetColor3d("White"))
actors[idx].SetBackfaceProperty(backProperty)
textmappers.append(vtk.vtkTextMapper())
textmappers[idx].SetInput(item.GetClassName())
......@@ -99,22 +111,25 @@ class ParametricObjects(object):
for row in range(gridDimensions):
for col in range(gridDimensions):
idx = row * gridDimensions + col
x0 = float(col) / gridDimensions
y0 = float(gridDimensions - row - 1) / gridDimensions
x1 = float(col + 1) / gridDimensions
y1 = float(gridDimensions - row) / gridDimensions
viewport[:] = []
viewport.append(float(col) * rendererSize / (gridDimensions * rendererSize))
viewport.append(float(gridDimensions - (row + 1)) * rendererSize / (gridDimensions * rendererSize))
viewport.append(float(col + 1) * rendererSize / (gridDimensions * rendererSize))
viewport.append(float(gridDimensions - row) * rendererSize / (gridDimensions * rendererSize))
viewport.append(x0)
viewport.append(y0)
viewport.append(x1)
viewport.append(y1)
if idx > (len(parametricObjects) - 1):
continue
renderers[idx].SetViewport(viewport)
renderWindow.AddRenderer(renderers[idx])
renderers[idx].SetViewport(viewport)
renderers[idx].AddActor(actors[idx])
renderers[idx].AddActor(textactors[idx])
renderers[idx].SetBackground(0.2, 0.3, 0.4)
renderers[idx].SetBackground(colors.GetColor3d("BkgColor"))
renderers[idx].ResetCamera()
renderers[idx].GetActiveCamera().Azimuth(30)
renderers[idx].GetActiveCamera().Elevation(-30)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment