PolyLineArchimedeanHelix.cxx 5.41 KB
Newer Older
Nick Thompson's avatar
Nick Thompson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt 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 <complex>
#include <vtkm/cont/DataSetBuilderExplicit.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/io/writer/VTKDataSetWriter.h>
#include <vtkm/worklet/Tube.h>

#include <vtkm/cont/ColorTable.h>
#include <vtkm/cont/CoordinateSystem.h>
#include <vtkm/cont/DataSetFieldAdd.h>
#include <vtkm/rendering/Camera.h>
#include <vtkm/rendering/CanvasRayTracer.h>
#include <vtkm/rendering/MapperRayTracer.h>
#include <vtkm/rendering/Scene.h>
#include <vtkm/rendering/View3D.h>

26
vtkm::Vec3f ArchimedeanSpiralToCartesian(vtkm::Vec3f const& p)
Nick Thompson's avatar
Nick Thompson committed
27
28
{
  // p[0] = r, p[1] = theta, p[2] = z:
29
  vtkm::Vec3f xyz;
Nick Thompson's avatar
Nick Thompson committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  auto c = std::polar(p[0], p[1]);
  xyz[0] = c.real();
  xyz[1] = c.imag();
  xyz[2] = p[2];
  return xyz;
}

void TubeThatSpiral(vtkm::FloatDefault radius, vtkm::Id numLineSegments, vtkm::Id numSides)
{
  vtkm::cont::DataSetBuilderExplicitIterative dsb;
  std::vector<vtkm::Id> ids;

  // The Archimedian spiral is defined by the equation r = a + b*theta.
  // To extend to a 3D curve, use z = t, theta = t, r = a + b t.
  vtkm::FloatDefault a = vtkm::FloatDefault(0.2);
  vtkm::FloatDefault b = vtkm::FloatDefault(0.8);
  for (vtkm::Id i = 0; i < numLineSegments; ++i)
  {
    vtkm::FloatDefault t = 4 * vtkm::FloatDefault(3.1415926) * (i + 1) /
      numLineSegments; // roughly two spins around. Doesn't need to be perfect.
    vtkm::FloatDefault r = a + b * t;
    vtkm::FloatDefault theta = t;
52
53
    vtkm::Vec3f cylindricalCoordinate{ r, theta, t };
    vtkm::Vec3f spiralSample = ArchimedeanSpiralToCartesian(cylindricalCoordinate);
Nick Thompson's avatar
Nick Thompson committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    vtkm::Id pid = dsb.AddPoint(spiralSample);
    ids.push_back(pid);
  }
  dsb.AddCell(vtkm::CELL_SHAPE_POLY_LINE, ids);

  vtkm::cont::DataSet ds = dsb.Create();

  vtkm::worklet::Tube tubeWorklet(
    /*capEnds = */ true,
    /* how smooth the cylinder is; infinitely smooth as n->infty */ numSides,
    radius);

  // You added lines, but you need to extend it to a tube.
  // This generates a new pointset, and new cell set.
68
  vtkm::cont::ArrayHandle<vtkm::Vec3f> tubePoints;
Nick Thompson's avatar
Nick Thompson committed
69
  vtkm::cont::CellSetSingleType<> tubeCells;
Nickolas Davis's avatar
Nickolas Davis committed
70
71
72
73
  tubeWorklet.Run(ds.GetCoordinateSystem().GetData().Cast<vtkm::cont::ArrayHandle<vtkm::Vec3f>>(),
                  ds.GetCellSet(),
                  tubePoints,
                  tubeCells);
Nick Thompson's avatar
Nick Thompson committed
74
75
76

  vtkm::cont::DataSet tubeDataset;
  tubeDataset.AddCoordinateSystem(vtkm::cont::CoordinateSystem("coords", tubePoints));
77
  tubeDataset.SetCellSet(tubeCells);
Nick Thompson's avatar
Nick Thompson committed
78
79
80

  vtkm::Bounds coordsBounds = tubeDataset.GetCoordinateSystem().GetBounds();

81
  vtkm::Vec3f_64 totalExtent(
Nick Thompson's avatar
Nick Thompson committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    coordsBounds.X.Length(), coordsBounds.Y.Length(), coordsBounds.Z.Length());
  vtkm::Float64 mag = vtkm::Magnitude(totalExtent);
  vtkm::Normalize(totalExtent);

  // setup a camera and point it to towards the center of the input data
  vtkm::rendering::Camera camera;
  camera.ResetToBounds(coordsBounds);

  camera.SetLookAt(totalExtent * (mag * .5f));
  camera.SetViewUp(vtkm::make_Vec(0.f, 1.f, 0.f));
  camera.SetClippingRange(1.f, 100.f);
  camera.SetFieldOfView(60.f);
  camera.SetPosition(totalExtent * (mag * 2.f));
  vtkm::cont::ColorTable colorTable("inferno");

  vtkm::rendering::Scene scene;
  vtkm::rendering::MapperRayTracer mapper;
  vtkm::rendering::CanvasRayTracer canvas(2048, 2048);
  vtkm::rendering::Color bg(0.2f, 0.2f, 0.2f, 1.0f);

102

Nick Thompson's avatar
Nick Thompson committed
103
  vtkm::cont::DataSetFieldAdd dsfa;
104
  std::vector<vtkm::FloatDefault> v(static_cast<std::size_t>(tubePoints.GetNumberOfValues()));
105
  // The first value is a cap:
Nick Thompson's avatar
Nick Thompson committed
106
107
108
109
110
111
112
  v[0] = 0;
  for (vtkm::Id i = 1; i < vtkm::Id(v.size()); i += numSides)
  {
    vtkm::FloatDefault t = 4 * vtkm::FloatDefault(3.1415926) * (i + 1) / numSides;
    vtkm::FloatDefault r = a + b * t;
    for (vtkm::Id j = i; j < i + numSides && j < vtkm::Id(v.size()); ++j)
    {
113
      v[static_cast<std::size_t>(j)] = r;
Nick Thompson's avatar
Nick Thompson committed
114
115
116
117
118
119
120
121
122
123
124
125
126
    }
  }
  // Point at the end cap should be the same color as the surroundings:
  v[v.size() - 1] = v[v.size() - 2];

  dsfa.AddPointField(tubeDataset, "Spiral Radius", v);
  scene.AddActor(vtkm::rendering::Actor(tubeDataset.GetCellSet(),
                                        tubeDataset.GetCoordinateSystem(),
                                        tubeDataset.GetField("Spiral Radius"),
                                        colorTable));
  vtkm::rendering::View3D view(scene, mapper, canvas, camera, bg);
  view.Initialize();
  view.Paint();
127
  // We can save the file as a .NetBPM:
Nick Thompson's avatar
Nick Thompson committed
128
129
  std::string output_filename = "tube_output_" + std::to_string(numSides) + "_sides.pnm";
  view.SaveAs(output_filename);
130
131
132
  // Or as a .png:
  output_filename = "tube_output_" + std::to_string(numSides) + "_sides.png";
  view.SaveAs(output_filename);
Nick Thompson's avatar
Nick Thompson committed
133
134
135
136
137
138
139
140
141
142
}



int main()
{
  // Radius of the tube:
  vtkm::FloatDefault radius = 0.5f;
  // How many segments is the tube decomposed into?
  vtkm::Id numLineSegments = 100;
143
  // As numSides->infty, the tubes becomes perfectly cylindrical:
Nick Thompson's avatar
Nick Thompson committed
144
145
  vtkm::Id numSides = 50;
  TubeThatSpiral(radius, numLineSegments, numSides);
146
  // Setting numSides = 4 makes a square around the polyline:
Nick Thompson's avatar
Nick Thompson committed
147
148
149
150
  numSides = 4;
  TubeThatSpiral(radius, numLineSegments, numSides);
  return 0;
}