/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.g3d.vtk.shape; import javax.vecmath.Tuple3d; import vtk.vtkActor; import vtk.vtkDataSetMapper; import vtk.vtkLine; import vtk.vtkPoints; import vtk.vtkPolyLine; import vtk.vtkUnstructuredGrid; public class vtkShape { /** * Creates a grid shaped actor. * * @param size number of grid lines * @param space distance between grid lines * @param axes bitmask of axes: 1:x, 2:y, 4:z * @return vtkActor representing a grid. */ public static vtkActor createGridActor(int size, double space, int axes) { int gridCount = 0; if ((axes & 0x1) > 0) { gridCount++; } if ((axes & 0x2) > 0) { gridCount++; } if ((axes & 0x4) > 0) { gridCount++; } int pointCount = (size+1) * 2 * 2 * gridCount; vtkPoints linePoints = new vtkPoints(); linePoints.SetNumberOfPoints(pointCount); double max = space * (double)size * 0.5; double min = -max; int base = 0; if ((axes & 0x1) > 0) { for (int i = 0; i <= size; i++) { double s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,0.0, s, min); linePoints.InsertPoint(base + i*2+1,0.0, s, max); i++; if (i > size) break; s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,0.0, s, max); linePoints.InsertPoint(base + i*2+1,0.0, s, min); } base += (size+1)*2; for (int i = 0; i <= size; i++) { double s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 , 0.0, max, s); linePoints.InsertPoint(base + i*2+1, 0.0, min, s); i++; if (i > size) break; s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 , 0.0, min, s); linePoints.InsertPoint(base + i*2+1, 0.0, max, s); } base += (size+1)*2; } if ((axes & 0x4) > 0) { for (int i = 0; i <= size; i++) { double s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,s, min, 0.0); linePoints.InsertPoint(base + i*2+1,s, max, 0.0); i++; if (i > size) break; s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,s, max, 0.0); linePoints.InsertPoint(base + i*2+1,s, min, 0.0); } base += (size+1)*2; for (int i = 0; i <= size; i++) { double s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,max, s, 0.0); linePoints.InsertPoint(base + i*2+1,min, s, 0.0); i++; if (i > size) break; s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,min, s, 0.0); linePoints.InsertPoint(base + i*2+1,max, s, 0.0); } base += (size+1)*2; } if ((axes & 0x2) > 0) { for (int i = 0; i <= size; i++) { double s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,s, 0.0, min); linePoints.InsertPoint(base + i*2+1,s, 0.0, max); i++; if (i > size) break; s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,s, 0.0, max); linePoints.InsertPoint(base + i*2+1,s, 0.0, min); } base += (size+1)*2; for (int i = 0; i <= size; i++) { double s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,max, 0.0, s); linePoints.InsertPoint(base + i*2+1,min, 0.0, s); i++; if (i > size) break; s = min + ((double)i) * space; linePoints.InsertPoint(base + i*2 ,min, 0.0, s); linePoints.InsertPoint(base + i*2+1,max, 0.0, s); } base += (size+1)*2; } //vtkLine aLine = new vtkLine(); vtkPolyLine aLine = new vtkPolyLine(); aLine.GetPointIds().SetNumberOfIds(pointCount); for (int i = 0; i < pointCount; i++) { aLine.GetPointIds().SetId(i, i); } vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid(); aLineGrid.Allocate(pointCount, pointCount); aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds()); aLineGrid.SetPoints(linePoints); vtkDataSetMapper aLineMapper = new vtkDataSetMapper(); aLineMapper.SetInputData(aLineGrid); vtkActor aLineActor = new vtkActor(); aLineActor.SetMapper(aLineMapper); linePoints.Delete(); aLine.GetPointIds().Delete(); aLine.Delete(); aLineGrid.Delete(); aLineMapper.Delete(); aLineActor.GetProperty().SetColor(0, 0, 0); aLineActor.GetProperty().Delete(); return aLineActor; } /** * Creates a line. * * @param p1 * @param p2 * @return */ public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) { vtkPoints linePoints = new vtkPoints(); linePoints.SetNumberOfPoints(2); linePoints.InsertPoint(0,p1.x, p1.y, p1.z); linePoints.InsertPoint(1,p2.x, p2.y, p2.z); vtkLine aLine = new vtkLine(); aLine.GetPointIds().SetId(0, 0); aLine.GetPointIds().SetId(1, 1); vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid(); aLineGrid.Allocate(1, 1); aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds()); aLineGrid.SetPoints(linePoints); vtkDataSetMapper aLineMapper = new vtkDataSetMapper(); aLineMapper.SetInputData(aLineGrid); vtkActor aLineActor = new vtkActor(); aLineActor.SetMapper(aLineMapper); //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1); linePoints.Delete(); aLine.GetPointIds().Delete(); aLine.Delete(); aLineGrid.Delete(); aLineMapper.Delete(); return aLineActor; } }