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.Point3d;
15 import javax.vecmath.Tuple3d;
18 import vtk.vtkDataSetMapper;
21 import vtk.vtkPolyLine;
22 import vtk.vtkUnstructuredGrid;
24 public class vtkShape {
27 * Creates a grid shaped actor.
29 * @param size number of grid lines
30 * @param space distance between grid lines
31 * @param axes bitmask of axes: 1:x, 2:y, 4:z
32 * @return vtkActor representing a grid.
34 public static vtkActor createGridActor(int size, double space, int axes) {
36 if ((axes & 0x1) > 0) {
39 if ((axes & 0x2) > 0) {
42 if ((axes & 0x4) > 0) {
45 int pointCount = (size+1) * 2 * 2 * gridCount;
46 vtkPoints linePoints = new vtkPoints();
47 linePoints.SetNumberOfPoints(pointCount);
50 double max = space * (double)size * 0.5;
53 if ((axes & 0x1) > 0) {
54 for (int i = 0; i <= size; i++) {
55 double s = min + ((double)i) * space;
56 linePoints.InsertPoint(base + i*2 ,0.0, s, min);
57 linePoints.InsertPoint(base + i*2+1,0.0, s, max);
61 s = min + ((double)i) * space;
62 linePoints.InsertPoint(base + i*2 ,0.0, s, max);
63 linePoints.InsertPoint(base + i*2+1,0.0, s, min);
66 for (int i = 0; i <= size; i++) {
67 double s = min + ((double)i) * space;
68 linePoints.InsertPoint(base + i*2 , 0.0, max, s);
69 linePoints.InsertPoint(base + i*2+1, 0.0, min, s);
73 s = min + ((double)i) * space;
74 linePoints.InsertPoint(base + i*2 , 0.0, min, s);
75 linePoints.InsertPoint(base + i*2+1, 0.0, max, s);
79 if ((axes & 0x4) > 0) {
80 for (int i = 0; i <= size; i++) {
81 double s = min + ((double)i) * space;
82 linePoints.InsertPoint(base + i*2 ,s, min, 0.0);
83 linePoints.InsertPoint(base + i*2+1,s, max, 0.0);
87 s = min + ((double)i) * space;
88 linePoints.InsertPoint(base + i*2 ,s, max, 0.0);
89 linePoints.InsertPoint(base + i*2+1,s, min, 0.0);
92 for (int i = 0; i <= size; i++) {
93 double s = min + ((double)i) * space;
94 linePoints.InsertPoint(base + i*2 ,max, s, 0.0);
95 linePoints.InsertPoint(base + i*2+1,min, s, 0.0);
99 s = min + ((double)i) * space;
100 linePoints.InsertPoint(base + i*2 ,min, s, 0.0);
101 linePoints.InsertPoint(base + i*2+1,max, s, 0.0);
105 if ((axes & 0x2) > 0) {
106 for (int i = 0; i <= size; i++) {
107 double s = min + ((double)i) * space;
108 linePoints.InsertPoint(base + i*2 ,s, 0.0, min);
109 linePoints.InsertPoint(base + i*2+1,s, 0.0, max);
113 s = min + ((double)i) * space;
114 linePoints.InsertPoint(base + i*2 ,s, 0.0, max);
115 linePoints.InsertPoint(base + i*2+1,s, 0.0, min);
118 for (int i = 0; i <= size; i++) {
119 double s = min + ((double)i) * space;
120 linePoints.InsertPoint(base + i*2 ,max, 0.0, s);
121 linePoints.InsertPoint(base + i*2+1,min, 0.0, s);
125 s = min + ((double)i) * space;
126 linePoints.InsertPoint(base + i*2 ,min, 0.0, s);
127 linePoints.InsertPoint(base + i*2+1,max, 0.0, s);
133 //vtkLine aLine = new vtkLine();
134 vtkPolyLine aLine = new vtkPolyLine();
135 aLine.GetPointIds().SetNumberOfIds(pointCount);
136 for (int i = 0; i < pointCount; i++) {
137 aLine.GetPointIds().SetId(i, i);
141 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
142 aLineGrid.Allocate(pointCount, pointCount);
143 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
144 aLineGrid.SetPoints(linePoints);
145 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
146 aLineMapper.SetInputData(aLineGrid);
147 vtkActor aLineActor = new vtkActor();
148 aLineActor.SetMapper(aLineMapper);
151 aLine.GetPointIds().Delete();
154 aLineMapper.Delete();
156 aLineActor.GetProperty().SetColor(0, 0, 0);
157 aLineActor.GetProperty().Delete();
169 public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) {
170 vtkPoints linePoints = new vtkPoints();
171 linePoints.SetNumberOfPoints(2);
172 linePoints.InsertPoint(0,p1.x, p1.y, p1.z);
173 linePoints.InsertPoint(1,p2.x, p2.y, p2.z);
174 vtkLine aLine = new vtkLine();
175 aLine.GetPointIds().SetId(0, 0);
176 aLine.GetPointIds().SetId(1, 1);
177 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
178 aLineGrid.Allocate(1, 1);
179 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
180 aLineGrid.SetPoints(linePoints);
181 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
182 aLineMapper.SetInputData(aLineGrid);
183 vtkActor aLineActor = new vtkActor();
184 aLineActor.SetMapper(aLineMapper);
185 //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
188 aLine.GetPointIds().Delete();
191 aLineMapper.Delete();
196 public static vtkActor createLineActor(Tuple3d... p) {
197 vtkPoints linePoints = new vtkPoints();
198 linePoints.SetNumberOfPoints(p.length);
200 for (int i = 0; i < p.length; i++) {
202 linePoints.InsertPoint(i,p1.x, p1.y, p1.z);
205 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
206 //aLineGrid.Allocate(1, 1);
207 for (int i = 0; i< p.length -1; i++) {
208 vtkLine aLine = new vtkLine();
209 aLine.GetPointIds().SetId(0, i);
210 aLine.GetPointIds().SetId(1, i+1);
211 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
212 aLine.GetPointIds().Delete();
216 aLineGrid.SetPoints(linePoints);
217 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
218 aLineMapper.SetInputData(aLineGrid);
219 vtkActor aLineActor = new vtkActor();
220 aLineActor.SetMapper(aLineMapper);
221 //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
226 aLineMapper.Delete();