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