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

Reformatting and making the images look nice.

parent 9825fc22
No related branches found
No related tags found
No related merge requests found
#include <vtkSmartPointer.h>
#include <vtkPlanes.h>
#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkCamera.h>
#include <vtkSphereSource.h>
#include <vtkHull.h>
#include <vtkNamedColors.h>
#include <vtkPlanes.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTextMapper.h>
#include <vtkTextProperty.h>
#include <string>
#include <vector>
int main(int, char *[])
int main(int, char* [])
{
// one way
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// These are the two methods we will use.
std::vector<std::string> titles{"Using frustum planes", "Using bounds"};
std::vector<vtkSmartPointer<vtkPlanes>> planes;
for (auto i = 0; i < titles.size(); ++i)
{
planes.push_back(vtkSmartPointer<vtkPlanes>::New());
}
// Using frustum planes.
vtkSmartPointer<vtkCamera> camera =
vtkSmartPointer<vtkCamera>::New();
double planesArray[24];
camera->GetFrustumPlanes(1, planesArray);
planes[0]->SetFrustumPlanes(planesArray);
vtkSmartPointer<vtkPlanes> planes =
vtkSmartPointer<vtkPlanes>::New();
planes->SetFrustumPlanes(planesArray);
}
// another way
{
// Using bounds.
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
double bounds[6];
sphereSource->GetOutput()->GetBounds(bounds);
planes[1]->SetBounds(bounds);
vtkSmartPointer<vtkPlanes> planes =
vtkSmartPointer<vtkPlanes>::New();
planes->SetBounds(bounds);
// At this point we have the planes created by both of the methods above.
// You can do whatever you want with them.
// For visualisation we will produce an n-sided convex hull
// and visualise it.
// Create a common text property.
vtkSmartPointer<vtkTextProperty> textProperty =
vtkSmartPointer<vtkTextProperty>::New();
textProperty->SetFontSize(16);
textProperty->SetJustificationToCentered();
// Create the render window and interactor.
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->SetWindowName("Planes");
vtkSmartPointer<vtkRenderWindowInteractor> iRen =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iRen->SetRenderWindow(renWin);
std::vector<vtkSmartPointer<vtkHull>> hulls;
std::vector<vtkSmartPointer<vtkPolyData>> pds;
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;
for (auto i = 0; i < titles.size(); ++i)
{
hulls.push_back(vtkSmartPointer<vtkHull>::New());
hulls[i]->SetPlanes(planes[i]);
pds.push_back(vtkSmartPointer<vtkPolyData>::New());
// To generate the convex hull we supply a vtkPolyData object and a bounding
// box.
// We define the bounding box to be where we expect the resulting polyhedron
// to lie.
// Make it a generous fit as it is only used to create the initial
// polygons that are eventually clipped.
hulls[i]->GenerateHull(pds[i], -200, 200, -200, 200, -200, 200);
mappers.push_back(vtkSmartPointer<vtkPolyDataMapper>::New());
mappers[i]->SetInputData(pds[i]);
actors.push_back(vtkSmartPointer<vtkActor>::New());
actors[i]->SetMapper(mappers[i]);
actors[i]->GetProperty()->SetColor(
colors->GetColor3d("Moccasin").GetData());
actors[i]->GetProperty()->SetSpecular(0.8);
actors[i]->GetProperty()->SetSpecularPower(30);
textMappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
textMappers[i]->SetInput(titles[i].c_str());
textMappers[i]->SetTextProperty(textProperty);
textActors.push_back(vtkSmartPointer<vtkActor2D>::New());
textActors[i]->SetMapper(textMappers[i]);
textActors[i]->SetPosition(120, 16);
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
renderers[i]->AddActor(actors[i]);
renderers[i]->AddViewProp(textActors[i]);
renWin->AddRenderer(renderers[i]);
}
// Setup the viewports
auto xGridDimensions = 2;
auto yGridDimensions = 1;
auto rendererSize = 300;
renWin->SetSize(rendererSize * xGridDimensions,
rendererSize * yGridDimensions);
for (auto row = 0; row < yGridDimensions; ++row)
{
for (auto col = 0; col < xGridDimensions; ++col)
{
auto index = row * xGridDimensions + col;
// (xmin, ymin, xmax, ymax)
double viewport[4] = {
static_cast<double>(col) / xGridDimensions,
static_cast<double>(yGridDimensions - (row + 1)) / yGridDimensions,
static_cast<double>(col + 1) / xGridDimensions,
static_cast<double>(yGridDimensions - row) / yGridDimensions};
if (index > (actors.size() - 1))
{
// Add a renderer even if there is no actor.
// This makes the render window background all the same color.
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
ren->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
ren->SetViewport(viewport);
renWin->AddRenderer(ren);
continue;
}
renderers[index]->SetViewport(viewport);
renderers[index]->SetBackground(
colors->GetColor3d("DarkSlateGray").GetData());
renderers[index]->ResetCamera();
renderers[index]->GetActiveCamera()->Azimuth(30);
renderers[index]->GetActiveCamera()->Elevation(-30);
renderers[index]->ResetCameraClippingRange();
}
}
iRen->Initialize();
renWin->Render();
iRen->Start();
return EXIT_SUCCESS;
}
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkPlatonicSolidSource.h>
......@@ -20,22 +21,25 @@ int main(int, char* [])
vtkSmartPointer<vtkNamedColors>::New();
// Each face has a different cell scalar
// Here we create a lookup table with a different colour
// for each face. The colors have been carefully
// chosen so that adjacent cells are colored distinctly.
vtkSmartPointer<vtkLookupTable> lut =
vtkSmartPointer<vtkLookupTable>::New();
lut->SetNumberOfTableValues(20);
lut->SetTableRange(0.0, 19.0);
lut->Build();
lut->SetTableValue(0, 0, 0, 0);
lut->SetTableValue(0, 0.1, 0.1, 0.1);
lut->SetTableValue(1, 0, 0, 1);
lut->SetTableValue(2, 0, 1, 0);
lut->SetTableValue(3, 0, 1, 1);
lut->SetTableValue(4, 1, 0, 0);
lut->SetTableValue(5, 1, 0, 1);
lut->SetTableValue(6, 1, 1, 0);
lut->SetTableValue(7, 1, 1, 1);
lut->SetTableValue(8, 0.7, 0.7, 0.7);
lut->SetTableValue(9, 0, 0, 0.7);
lut->SetTableValue(10, 0, 0.7, 0);
lut->SetTableValue(7, 0.9, 0.7, 0.9);
lut->SetTableValue(8, 0.5, 0.5, 0.5);
lut->SetTableValue(9, 0.0, 0.0, 0.7);
lut->SetTableValue(10, 0.5, 0.7, 0.5);
lut->SetTableValue(11, 0, 0.7, 0.7);
lut->SetTableValue(12, 0.7, 0, 0);
lut->SetTableValue(13, 0.7, 0, 0.7);
......@@ -48,8 +52,8 @@ int main(int, char* [])
std::vector<vtkSmartPointer<vtkPolyDataMapper>> mappers;
std::vector<vtkSmartPointer<vtkActor>> actors;
std::vector<vtkSmartPointer<vtkTextMapper>> textmappers;
std::vector<vtkSmartPointer<vtkActor2D>> textactors;
std::vector<vtkSmartPointer<vtkTextMapper>> textMappers;
std::vector<vtkSmartPointer<vtkActor2D>> textActors;
std::vector<vtkSmartPointer<vtkRenderer>> renderers;
// Create a common text property.
......@@ -83,17 +87,17 @@ int main(int, char* [])
actors.push_back(vtkSmartPointer<vtkActor>::New());
actors[i]->SetMapper(mappers[i]);
textmappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
textmappers[i]->SetInput(names[i].c_str());
textmappers[i]->SetTextProperty(textProperty);
textMappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
textMappers[i]->SetInput(names[i].c_str());
textMappers[i]->SetTextProperty(textProperty);
textactors.push_back(vtkSmartPointer<vtkActor2D>::New());
textactors[i]->SetMapper(textmappers[i]);
textactors[i]->SetPosition(120, 16);
textActors.push_back(vtkSmartPointer<vtkActor2D>::New());
textActors[i]->SetMapper(textMappers[i]);
textActors[i]->SetPosition(120, 16);
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
renderers[i]->AddActor(actors[i]);
renderers[i]->AddViewProp(textactors[i]);
renderers[i]->AddViewProp(textActors[i]);
renWin->AddRenderer(renderers[i]);
}
......@@ -121,7 +125,7 @@ int main(int, char* [])
// Add a renderer even if there is no actor.
// This makes the render window background all the same color.
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
ren->SetBackground(colors->GetColor3d("SaddleBrown").GetData());
ren->SetBackground(colors->GetColor3d("SlateGray").GetData());
ren->SetViewport(viewport);
renWin->AddRenderer(ren);
continue;
......@@ -129,10 +133,10 @@ int main(int, char* [])
renderers[index]->SetViewport(viewport);
renderers[index]->SetBackground(
colors->GetColor3d("SaddleBrown").GetData());
colors->GetColor3d("SlateGray").GetData());
renderers[index]->ResetCamera();
// renderers[index]->GetActiveCamera()->Azimuth(30);
// renderers[index]->GetActiveCamera()->Elevation(-30);
renderers[index]->GetActiveCamera()->Azimuth(4.5);
renderers[index]->GetActiveCamera()->Elevation(-18);
renderers[index]->ResetCameraClippingRange();
}
}
......
### Description
Display all five Platonic solids in a grid.
Platonic solids are regular, convex polyhedrons. They are constructed by congruent (identical in shape and size) regular (all angles equal and all sides equal) polygonal faces with the same number of faces meeting at each vertex.
Five solids satisfy the above criteria:
| Figure | Tetrahedron | Octahedron | Cube | Icosahedron | Dodecahedron|
| :------: | :------: | :------: | :------: | :------: | :------: |
| Faces | 4 | 8 | 6 | 20 | 12|
| Vertices | 4 | 6 (2 × 3) | 8| 12 (4 × 3) | 20 (8 + 4 × 3) |
......@@ -33,6 +33,11 @@ def main():
# For visualisation we will produce an n-sided convex hull
# and visualise it.
# Create a common text property.
textProperty = vtk.vtkTextProperty()
textProperty.SetFontSize(16)
textProperty.SetJustificationToCentered()
renWin = vtk.vtkRenderWindow()
renWin.SetSize(600, 600)
renWin.SetWindowName("Planes")
......@@ -73,6 +78,8 @@ def main():
textMappers.append(vtk.vtkTextMapper())
textMappers[i].SetInput(titles[i])
textMappers[i].SetTextProperty(textProperty)
textActors.append(vtk.vtkActor2D())
textActors[i].SetMapper(textMappers[i])
textActors[i].SetPosition(100, 10)
......
### Description
Display all five Platonic solids in a grid.
Platonic solids are regular, convex polyhedrons. They are constructed by congruent (identical in shape and size) regular (all angles equal and all sides equal) polygonal faces with the same number of faces meeting at each vertex.
Five solids satisfy the above criteria:
| Figure | Tetrahedron | Octahedron | Cube | Icosahedron | Dodecahedron|
| :------: | :------: | :------: | :------: | :------: | :------: |
| Faces | 4 | 8 | 6 | 20 | 12|
| Vertices | 4 | 6 (2 × 3) | 8| 12 (4 × 3) | 20 (8 + 4 × 3) |
......@@ -7,23 +7,24 @@ def main():
colors = vtk.vtkNamedColors()
# Each face has a different cell scalar
# So create a lookup table with a different colour
# for each face.
# Here we create a lookup table with a different colour
# for each face. The colors have been carefully
# chosen so that adjacent cells are colored distinctly.
lut = vtk.vtkLookupTable()
lut.SetNumberOfTableValues(20)
lut.SetTableRange(0.0, 19.0)
lut.Build()
lut.SetTableValue(0, 0, 0, 0)
lut.SetTableValue(0, 0.1, 0.1, 0.1)
lut.SetTableValue(1, 0, 0, 1)
lut.SetTableValue(2, 0, 1, 0)
lut.SetTableValue(3, 0, 1, 1)
lut.SetTableValue(4, 1, 0, 0)
lut.SetTableValue(5, 1, 0, 1)
lut.SetTableValue(6, 1, 1, 0)
lut.SetTableValue(7, 1, 1, 1)
lut.SetTableValue(8, 0.7, 0.7, 0.7)
lut.SetTableValue(9, 0, 0, 0.7)
lut.SetTableValue(10, 0, 0.7, 0)
lut.SetTableValue(7, 0.9, 0.7, 0.9)
lut.SetTableValue(8, 0.5, 0.5, 0.5)
lut.SetTableValue(9, 0.0, 0.0, 0.7)
lut.SetTableValue(10, 0.5, 0.7, 0.5)
lut.SetTableValue(11, 0, 0.7, 0.7)
lut.SetTableValue(12, 0.7, 0, 0)
lut.SetTableValue(13, 0.7, 0, 0.7)
......@@ -36,8 +37,8 @@ def main():
mappers = list()
actors = list()
textmappers = list()
textactors = list()
textMappers = list()
textActors = list()
renderers = list()
# Create a common text property.
......@@ -69,17 +70,17 @@ def main():
actors.append(vtk.vtkActor())
actors[i].SetMapper(mappers[i])
textmappers.append(vtk.vtkTextMapper())
textmappers[i].SetInput(names[i])
textmappers[i].SetTextProperty(textProperty)
textMappers.append(vtk.vtkTextMapper())
textMappers[i].SetInput(names[i])
textMappers[i].SetTextProperty(textProperty)
textactors.append(vtk.vtkActor2D())
textactors[i].SetMapper(textmappers[i])
textactors[i].SetPosition(120, 16)
textActors.append(vtk.vtkActor2D())
textActors[i].SetMapper(textMappers[i])
textActors[i].SetPosition(120, 16)
renderers.append(vtk.vtkRenderer())
renderers[i].AddActor(actors[i])
renderers[i].AddViewProp(textactors[i])
renderers[i].AddViewProp(textActors[i])
renWin.AddRenderer(renderers[i])
......@@ -102,16 +103,16 @@ def main():
# Add a renderer even if there is no actor.
# This makes the render window background all the same color.
ren = vtk.vtkRenderer()
ren.SetBackground(colors.GetColor3d("SaddleBrown"))
ren.SetBackground(colors.GetColor3d("SlateGray"))
ren.SetViewport(viewport)
renWin.AddRenderer(ren)
continue
renderers[index].SetViewport(viewport)
renderers[index].SetBackground(colors.GetColor3d("SaddleBrown"))
renderers[index].SetBackground(colors.GetColor3d("SlateGray"))
renderers[index].ResetCamera()
# renderers[index].GetActiveCamera().Azimuth(30)
# renderers[index].GetActiveCamera().Elevation(-30)
renderers[index].GetActiveCamera().Azimuth(4.5)
renderers[index].GetActiveCamera().Elevation(-18)
renderers[index].ResetCameraClippingRange()
iRen.Initialize()
......
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