1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.shape;
14 import javax.vecmath.Tuple3d;
17 import vtk.vtkDataSetMapper;
20 import vtk.vtkPolyLine;
21 import vtk.vtkUnstructuredGrid;
23 public class vtkShape {
26 * Creates a grid shaped actor.
28 * @param size number of grid lines
29 * @param space distance between grid lines
30 * @param axes bitmask of axes: 1:x, 2:y, 4:z
31 * @return vtkActor representing a grid.
33 public static vtkActor createGridActor(int size, double space, int axes) {
35 if ((axes & 0x1) > 0) {
38 if ((axes & 0x2) > 0) {
41 if ((axes & 0x4) > 0) {
44 int pointCount = (size+1) * 2 * 2 * gridCount;
45 vtkPoints linePoints = new vtkPoints();
46 linePoints.SetNumberOfPoints(pointCount);
49 double max = space * (double)size * 0.5;
52 if ((axes & 0x1) > 0) {
53 for (int i = 0; i <= size; i++) {
54 double s = min + ((double)i) * space;
55 linePoints.InsertPoint(base + i*2 ,0.0, s, min);
56 linePoints.InsertPoint(base + i*2+1,0.0, s, max);
60 s = min + ((double)i) * space;
61 linePoints.InsertPoint(base + i*2 ,0.0, s, max);
62 linePoints.InsertPoint(base + i*2+1,0.0, s, min);
65 for (int i = 0; i <= size; i++) {
66 double s = min + ((double)i) * space;
67 linePoints.InsertPoint(base + i*2 , 0.0, max, s);
68 linePoints.InsertPoint(base + i*2+1, 0.0, min, s);
72 s = min + ((double)i) * space;
73 linePoints.InsertPoint(base + i*2 , 0.0, min, s);
74 linePoints.InsertPoint(base + i*2+1, 0.0, max, s);
78 if ((axes & 0x4) > 0) {
79 for (int i = 0; i <= size; i++) {
80 double s = min + ((double)i) * space;
81 linePoints.InsertPoint(base + i*2 ,s, min, 0.0);
82 linePoints.InsertPoint(base + i*2+1,s, max, 0.0);
86 s = min + ((double)i) * space;
87 linePoints.InsertPoint(base + i*2 ,s, max, 0.0);
88 linePoints.InsertPoint(base + i*2+1,s, min, 0.0);
91 for (int i = 0; i <= size; i++) {
92 double s = min + ((double)i) * space;
93 linePoints.InsertPoint(base + i*2 ,max, s, 0.0);
94 linePoints.InsertPoint(base + i*2+1,min, s, 0.0);
98 s = min + ((double)i) * space;
99 linePoints.InsertPoint(base + i*2 ,min, s, 0.0);
100 linePoints.InsertPoint(base + i*2+1,max, s, 0.0);
104 if ((axes & 0x2) > 0) {
105 for (int i = 0; i <= size; i++) {
106 double s = min + ((double)i) * space;
107 linePoints.InsertPoint(base + i*2 ,s, 0.0, min);
108 linePoints.InsertPoint(base + i*2+1,s, 0.0, max);
112 s = min + ((double)i) * space;
113 linePoints.InsertPoint(base + i*2 ,s, 0.0, max);
114 linePoints.InsertPoint(base + i*2+1,s, 0.0, min);
117 for (int i = 0; i <= size; i++) {
118 double s = min + ((double)i) * space;
119 linePoints.InsertPoint(base + i*2 ,max, 0.0, s);
120 linePoints.InsertPoint(base + i*2+1,min, 0.0, s);
124 s = min + ((double)i) * space;
125 linePoints.InsertPoint(base + i*2 ,min, 0.0, s);
126 linePoints.InsertPoint(base + i*2+1,max, 0.0, s);
132 //vtkLine aLine = new vtkLine();
133 vtkPolyLine aLine = new vtkPolyLine();
134 aLine.GetPointIds().SetNumberOfIds(pointCount);
135 for (int i = 0; i < pointCount; i++) {
136 aLine.GetPointIds().SetId(i, i);
140 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
141 aLineGrid.Allocate(pointCount, pointCount);
142 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
143 aLineGrid.SetPoints(linePoints);
144 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
145 aLineMapper.SetInputData(aLineGrid);
146 vtkActor aLineActor = new vtkActor();
147 aLineActor.SetMapper(aLineMapper);
150 aLine.GetPointIds().Delete();
153 aLineMapper.Delete();
155 aLineActor.GetProperty().SetColor(0, 0, 0);
156 aLineActor.GetProperty().Delete();
168 public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) {
169 vtkPoints linePoints = new vtkPoints();
170 linePoints.SetNumberOfPoints(2);
171 linePoints.InsertPoint(0,p1.x, p1.y, p1.z);
172 linePoints.InsertPoint(1,p2.x, p2.y, p2.z);
173 vtkLine aLine = new vtkLine();
174 aLine.GetPointIds().SetId(0, 0);
175 aLine.GetPointIds().SetId(1, 1);
176 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
177 aLineGrid.Allocate(1, 1);
178 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
179 aLineGrid.SetPoints(linePoints);
180 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
181 aLineMapper.SetInputData(aLineGrid);
182 vtkActor aLineActor = new vtkActor();
183 aLineActor.SetMapper(aLineMapper);
184 //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
187 aLine.GetPointIds().Delete();
190 aLineMapper.Delete();
195 public static vtkActor createLineActor(Tuple3d... p) {
196 vtkPoints linePoints = new vtkPoints();
197 linePoints.SetNumberOfPoints(p.length);
199 for (int i = 0; i < p.length; i++) {
201 linePoints.InsertPoint(i,p1.x, p1.y, p1.z);
204 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
205 //aLineGrid.Allocate(1, 1);
206 for (int i = 0; i< p.length -1; i++) {
207 vtkLine aLine = new vtkLine();
208 aLine.GetPointIds().SetId(0, i);
209 aLine.GetPointIds().SetId(1, i+1);
210 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
211 aLine.GetPointIds().Delete();
215 aLineGrid.SetPoints(linePoints);
216 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
217 aLineMapper.SetInputData(aLineGrid);
218 vtkActor aLineActor = new vtkActor();
219 aLineActor.SetMapper(aLineMapper);
220 //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
225 aLineMapper.Delete();