image

image

private Kitware.VTK.RenderWindowControl renderWindowControl1;
vtkAxesActor axes = null;
List<vtkActor> vtkActors = new List<vtkActor>();
public Form1()
{
InitializeComponent();

renderWindowControl1 = new RenderWindowControl();
splitContainer1.Panel2.Controls.Add(renderWindowControl1);
renderWindowControl1.Show();
renderWindowControl1.Dock = DockStyle.Fill;
}

繪製線段

private void drawLine(double[] p0, double[] p1, int lineWidth, IntPtr pColor)
{
// Create a line.
vtkLineSource lineSource = vtkLineSource.New();

lineSource.SetPoint1(p0[0], p0[1], p0[2]);
lineSource.SetPoint2(p1[0], p1[1], p1[2]);

// Visualize
vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
mapper.SetInputConnection(lineSource.GetOutputPort());
vtkActor actor = vtkActor.New();
actor.SetMapper(mapper);
actor.GetProperty().SetLineWidth(lineWidth);
actor.GetProperty().SetColor(pColor);
vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
renderer.SetBackground(0.2, 0.3, 0.4);
renderer.AddActor(actor);
vtkActors.Add(actor);
renderer.ResetCamera();
}

顯示XYZ軸

private void drawAxes()
{
if (axes == null)
axes = vtkAxesActor.New();
// The axes are positioned with a user transform
vtkTransform transform = vtkTransform.New();
transform.Translate(0.0, 0.0, 0.0);
axes.SetUserTransform(transform);
// properties of the axes labels can be set as follows
// this sets the x axis label to red
// axes.GetXAxisCaptionActor2D().GetCaptionTextProperty().SetColor(1,0,0);

// the actual text of the axis label can be changed:
// axes.SetXAxisLabelText("test");
vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
renderer.AddActor(axes);
// we need to call Render() for the whole renderWindow,
// because vtkAxesActor uses an overlayed renderer for the axes label
// in total we have now two renderer
renderWindow.Render();

}

繪製兩條線

private void drawToolStripMenuItem_Click(object sender, EventArgs e)
{
// Create two points, P0 and P1
double[] p0 = new double[] { 0.5, 0.0, 0.0 };
double[] p1 = new double[] { 0.0, 0.5, 0.0 };
int lineWidth = 4;
double[] Color = new double[3] { 1.0, 0, 0 };
IntPtr pColor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * 3);
Marshal.Copy(Color, 0, pColor, 3);
drawLine(p0, p1, lineWidth, pColor);
double[] p2 = new double[] { 0.0, 0.0, 0.0 };
double[] p3 = new double[] { 0.5, 0.5, 0.0 };
lineWidth = 8;
Color[0] = 0; Color[1] = 0; Color[2] = 1.0;
Marshal.Copy(Color, 0, pColor, 3);
drawLine(p2, p3, lineWidth, pColor);
renderWindowControl1.Invalidate();
}

結果

image

===================================================

平移XYZ軸

private void UpdateAxes()
{
if (axes == null) return;
double x = hScrollBar1.Value / 100.0;
double y = hScrollBar2.Value / 100.0;
double z = hScrollBar3.Value / 100.0;
vtkTransform transform = vtkTransform.New();
transform.Translate(x, y, z);
axes.SetUserTransform(transform);
vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
renderWindow.Render();
}

加入三個軸Scrollbar

private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
label1.Text = hScrollBar1.Value.ToString();
UpdateAxes();
}

private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
{
label2.Text = hScrollBar2.Value.ToString();
UpdateAxes();
}

private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)
{
label3.Text = hScrollBar3.Value.ToString();
UpdateAxes();
}

image

image

線段對X軸旋轉

private void timerRotateX_Tick(object sender, EventArgs e)
{
vtkActors[1].RotateX(1.0);
vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
renderWindow.Render();
}

線段對Y軸旋轉

private void timerRotateY_Tick(object sender, EventArgs e)
{
vtkActors[1].RotateY(1.0);
vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
renderWindow.Render();
}

線段對Z軸旋轉

private void timerRotateZ_Tick(object sender, EventArgs e)
{
vtkActors[1].RotateZ(1.0);
vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
renderWindow.Render();
}


參考資料

http://www.vtk.org/Wiki/VTK/Examples/CSharp/GeometricObjects/Axes

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()