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.opencascade.vtk;
14 import java.util.List;
16 import javax.vecmath.Matrix4d;
17 import javax.vecmath.Point3d;
19 import org.jcae.opencascade.jni.BRepMesh_IncrementalMesh;
20 import org.jcae.opencascade.jni.BRep_Tool;
21 import org.jcae.opencascade.jni.GP_Trsf;
22 import org.jcae.opencascade.jni.Poly_Triangulation;
23 import org.jcae.opencascade.jni.TopAbs_Orientation;
24 import org.jcae.opencascade.jni.TopAbs_ShapeEnum;
25 import org.jcae.opencascade.jni.TopExp_Explorer;
26 import org.jcae.opencascade.jni.TopLoc_Location;
27 import org.jcae.opencascade.jni.TopoDS_Face;
28 import org.jcae.opencascade.jni.TopoDS_Shape;
29 import org.simantics.opencascade.OCCTTool;
32 import vtk.vtkAlgorithmOutput;
33 import vtk.vtkAppendPolyData;
34 import vtk.vtkAssembly;
35 import vtk.vtkCleanPolyData;
36 import vtk.vtkDataSetMapper;
37 import vtk.vtkFeatureEdges;
38 import vtk.vtkGlyph3D;
41 import vtk.vtkPolyData;
42 import vtk.vtkPolyDataMapper;
43 import vtk.vtkPolyDataNormals;
45 import vtk.vtkPropCollection;
46 import vtk.vtkProperty;
47 import vtk.vtkSphereSource;
48 import vtk.vtkTriangle;
50 public class VTKOCCTool {
51 public static vtkAssembly vtkTestAssembly() {
52 vtkAssembly assemblies = new vtkAssembly();
53 vtkPolyData partGrid = createTestPartGrid();
54 gridToAssembly(assemblies, partGrid);
58 public static vtkAssembly vtkOCCShapeToAssembly(TopoDS_Shape shape) {
59 double deflection = 0.001;
61 if (deflection <= 0.0) {
63 System.out.println("Bad value for deflection. Using: " + deflection);
66 // FIXME : leaks memory!
67 //BRepTools.clean(shape);
69 double mass = OCCTTool.getMass(shape);
72 System.out.println("Non 3D-shape detected");
73 System.out.println("The cad import features are currently limited to 3D models.");
76 double length = OCCTTool.getBoundingBoxDiagonal(shape);
77 deflection *= length; // use relative units
79 BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,deflection);
82 TopExp_Explorer expFace = new TopExp_Explorer();
84 vtkAssembly assemblies = new vtkAssembly();
85 for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) {
86 TopoDS_Face face = (TopoDS_Face) expFace.current();
87 vtkPolyData partGrid = createPartGrid(face);
92 //gridToAssembly(assemblies, partGrid, stlSurfaceData, stlEdgeData);
93 gridToAssembly(assemblies, partGrid);
99 if (faceNumber == 0) {
101 .println("Cad import: error: no surface triangulation was generated.");
108 private static void gridToAssembly(vtkAssembly assemblies, vtkPolyData partGrid, vtkAppendPolyData stlSurfaceData, vtkAppendPolyData stlEdgeData) {
110 double featureAngle = 30;
113 vtkDataSetMapper partMapper = new vtkDataSetMapper();
115 boolean computeNormals = true;
116 boolean cleanPart = true;
117 boolean mergePoints = false;
119 vtkCleanPolyData partCleaner = new vtkCleanPolyData();
122 partCleaner.SetInputData(partGrid);
124 partCleaner.PointMergingOn();
126 partCleaner.PointMergingOff();
132 vtkPolyDataNormals partNormals = new vtkPolyDataNormals();
136 partNormals.SetInputConnection(partCleaner.GetOutputPort());
138 else partNormals.SetInputData(partGrid);
140 partNormals.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called
141 partMapper.SetInputConnection(partNormals.GetOutputPort());
142 partNormals.Delete();
146 if (cleanPart) partMapper.SetInputConnection(partCleaner.GetOutputPort()); // metoda 2, ne tak pekne, viz http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681
147 else partMapper.SetInputData(partGrid); // metoda 1, ne tak pekne stinovani, viz: http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681
149 partMapper.ScalarVisibilityOn();
151 vtkActor partActor = new vtkActor();
153 partActor.SetPickable(1);
154 partActor.GetProperty().SetColor(1, 1, 0);
155 partActor.SetMapper(partMapper);
157 // EDGES OF PARTS DETECTION
158 vtkFeatureEdges partEdges = new vtkFeatureEdges();
159 if (cleanPart) partEdges.SetInputConnection(partCleaner.GetOutputPort());
160 else partEdges.SetInputData(partGrid);
161 partEdges.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called
162 partEdges.FeatureEdgesOn();
163 partEdges.BoundaryEdgesOn();
164 partEdges.NonManifoldEdgesOn();
165 partEdges.ManifoldEdgesOn();
167 vtkDataSetMapper partEdgesMapper = new vtkDataSetMapper();
168 partEdgesMapper.SetInputConnection(partEdges.GetOutputPort());
169 partEdgesMapper.SetResolveCoincidentTopologyToPolygonOffset();
170 partEdgesMapper.ScalarVisibilityOff();
172 vtkActor partEdgesActor = new vtkActor();
173 partEdgesActor.SetPickable(0);
174 partEdgesActor.GetProperty().SetColor(1, 0, 1);
175 partEdgesActor.SetMapper(partEdgesMapper);
178 // Add triangles and edges to STL structures:
179 //--------------------------------------------
180 if (cleanPart) stlSurfaceData.AddInputData(partCleaner.GetOutput());
181 else stlSurfaceData.AddInputData(partGrid);
182 stlEdgeData.AddInputData(partEdges.GetOutput());
184 assemblies.AddPart(partActor);
185 assemblies.AddPart(partEdgesActor);
189 partEdgesActor.Delete();
190 partEdgesMapper.Delete();
195 partCleaner.Delete();
198 public static void gridToAssembly(vtkAssembly assemblies, vtkPolyData partGrid) {
200 double featureAngle = 30;
203 vtkDataSetMapper partMapper = new vtkDataSetMapper();
205 boolean computeNormals = true;
206 boolean cleanPart = false;
207 boolean mergePoints = false;
209 vtkCleanPolyData partCleaner = new vtkCleanPolyData();
212 partCleaner.SetInputData(partGrid);
214 partCleaner.PointMergingOn();
216 partCleaner.PointMergingOff();
222 vtkPolyDataNormals partNormals = new vtkPolyDataNormals();
226 vtkAlgorithmOutput out = partCleaner.GetOutputPort();
227 partNormals.SetInputConnection(out);
230 else partNormals.SetInputData(partGrid);
232 partNormals.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called
233 vtkAlgorithmOutput out = partNormals.GetOutputPort();
234 partMapper.SetInputConnection(out);
236 partNormals.Delete();
241 vtkAlgorithmOutput out = partCleaner.GetOutputPort();
242 partMapper.SetInputConnection(out); // metoda 2, ne tak pekne, viz http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681
245 else partMapper.SetInputData(partGrid); // metoda 1, ne tak pekne stinovani, viz: http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681
247 partMapper.ScalarVisibilityOn();
249 vtkActor partActor = new vtkActor();
250 partActor.SetPickable(1);
251 vtkProperty prop = partActor.GetProperty();
252 prop.SetColor(1, 1, 0);
254 partActor.SetMapper(partMapper);
256 assemblies.AddPart(partActor);
259 // EDGES OF PARTS DETECTION
260 vtkFeatureEdges partEdges = new vtkFeatureEdges();
262 vtkAlgorithmOutput out = partCleaner.GetOutputPort();
263 partEdges.SetInputConnection(out);
266 else partEdges.SetInputData(partGrid);
267 // partEdges.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called
268 partEdges.FeatureEdgesOn();
269 partEdges.BoundaryEdgesOn();
270 partEdges.NonManifoldEdgesOn();
271 partEdges.ManifoldEdgesOn();
273 vtkDataSetMapper partEdgesMapper = new vtkDataSetMapper();
274 vtkAlgorithmOutput out = partEdges.GetOutputPort();
275 partEdgesMapper.SetInputConnection(out);
277 partEdgesMapper.SetResolveCoincidentTopologyToPolygonOffset();
278 partEdgesMapper.ScalarVisibilityOff();
280 vtkActor partEdgesActor = new vtkActor();
282 prop = partEdgesActor.GetProperty();
283 prop.SetColor(0, 0, 0);
284 prop.SetLineWidth(2.0);
286 partEdgesActor.SetMapper(partEdgesMapper);
287 partEdgesActor.PickableOn();
289 assemblies.AddPart(partEdgesActor);
293 EdgePointsFilter edgePoints = new EdgePointsFilter();
296 out = partEdges.GetOutputPort();
297 edgePoints.SetInputConnection(out);
300 // vtkDataSetMapper partEdgePointsMapper = new vtkDataSetMapper();
301 // out = edgePoints.GetOutputPort();
302 // partEdgePointsMapper.SetInputConnection(out);
305 // vtkVertexGlyphFilter glyphFilter = new vtkVertexGlyphFilter();
306 // out = edgePoints.GetOutputPort();
307 // glyphFilter.SetInputConnection(out);
308 // glyphFilter.Update();
311 // vtkPolyDataMapper partEdgePointsMapper = new vtkPolyDataMapper();
312 // out = glyphFilter.GetOutputPort();
313 // partEdgePointsMapper.SetInputConnection(out);
316 vtkSphereSource sphereSource = new vtkSphereSource();
317 vtkGlyph3D glyph3D = new vtkGlyph3D();
318 out = sphereSource.GetOutputPort();
319 glyph3D.SetSourceConnection(out);
322 out = edgePoints.GetOutputPort();
323 glyph3D.SetInputConnection(out);
326 //glyph3D.ScalingOff();
327 glyph3D.SetScaleFactor(0.03);
331 vtkPolyDataMapper partEdgePointsMapper = new vtkPolyDataMapper();
332 out = glyph3D.GetOutputPort();
333 partEdgePointsMapper.SetInputConnection(out);
336 vtkActor edgePointsActor = new vtkActor();
338 prop = edgePointsActor.GetProperty();
339 prop.SetColor(0, 0, 1);
340 //prop.SetPointSize(10.0);
341 //prop.SetRepresentationToPoints();
343 edgePointsActor.SetMapper(partEdgePointsMapper);
345 edgePointsActor.PickableOn();
346 assemblies.AddPart(edgePointsActor);
349 edgePointsActor.Delete();
350 partEdgePointsMapper.Delete();
351 // edgePoints.Delete();
355 partEdgesActor.Delete();
356 partEdgesMapper.Delete();
363 partCleaner.Delete();
368 public static vtkPolyData createPartGrid ( TopoDS_Face face)
370 TopLoc_Location Location = new TopLoc_Location();
372 Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location);
374 if(triangulation == null) {
376 System.out.println("Encountered empty triangulation after face");
380 boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED;
382 int[]triangles = triangulation.triangles();
383 double[]nodes = triangulation.nodes();
385 int nofTriangles = triangulation.nbTriangles();
386 int nofNodes = triangulation.nbNodes();
388 triangulation.delete();
390 if(nofTriangles < 1) {
391 System.out.println("No triangles for mesh on face");
397 System.out.println("No nodes for mesh on face:");
401 //System.out.println("v " + nofNodes + " t " +nofTriangles);
402 vtkPolyData partGrid = new vtkPolyData();
403 partGrid.Allocate(nofTriangles, nofTriangles);
405 vtkTriangle triangle = new vtkTriangle();
406 vtkIdList list = triangle.GetPointIds();
407 for(int i = 0; i < nofTriangles; i++)
411 n0 = triangles[3 * i];
412 n1 = triangles[3 * i + 1];
413 n2 = triangles[3 * i + 2];
415 n0 = triangles[3 * i + 2];
416 n1 = triangles[3 * i + 1];
417 n2 = triangles[3 * i];
423 partGrid.InsertNextCell(triangle.GetCellType(), list);
429 GP_Trsf transformation = Location.transformation();
432 double d_mat[] = new double[16];
433 double d_p[] = new double[3];
434 transformation.getValues(d_mat);
435 Matrix4d mat = new Matrix4d(d_mat);
437 vtkPoints partPoints = new vtkPoints();
439 for(int i = 0; i < nofNodes; i++) {
440 // FIXME: GP_Trsf.transform(double[]) leaks memory
442 //double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]};
443 //transformation.transforms(xyz);
444 //partPoints.InsertPoint(i, xyz);
445 Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]);
450 partPoints.InsertPoint(i, d_p);
453 transformation.delete();
455 partGrid.SetPoints(partPoints);
463 public static vtkPolyData createPartGrid ( List<Double> meshPoints, List<Integer> meshTriangles)
470 int nofTriangles = meshTriangles.size() / 3;
471 int nofNodes = meshPoints.size() /3;
474 if(nofTriangles < 1) {
475 System.out.println("No triangles for mesh on face");
480 System.out.println("No nodes for mesh on face:");
483 //System.out.println("v " + nofNodes + " t " +nofTriangles);
484 vtkPolyData partGrid = new vtkPolyData();
485 partGrid.Allocate(nofTriangles, nofTriangles);
487 vtkTriangle triangle = new vtkTriangle();
488 vtkIdList list = triangle.GetPointIds();
489 for(int i = 0; i < nofTriangles; i++)
492 n0 = meshTriangles.get(3 * i);
493 n1 = meshTriangles.get(3 * i + 1);
494 n2 = meshTriangles.get(3 * i + 2);
500 partGrid.InsertNextCell(triangle.GetCellType(), list);
508 double d_p[] = new double[3];
511 vtkPoints partPoints = new vtkPoints();
513 for(int i = 0; i < nofNodes; i++) {
515 d_p[0] = meshPoints.get(3*i);
516 d_p[1] = meshPoints.get(3*i+1);
517 d_p[2] = meshPoints.get(3*i+2);
518 partPoints.InsertPoint(i, d_p);
521 partGrid.SetPoints(partPoints);
527 private static vtkPolyData createTestPartGrid ()
531 double[] nodes = new double[(size+1)*(size+1)*3];
532 int[] triangles = new int[3 * size * size * 2];
535 for (int i = 0; i <= size; i++) {
536 for (int j = 0; j <= size; j++) {
537 int index = j * size + i;
539 double x = (double)i * es;
540 double y = (Math.sin((double)i/(double)size) + Math.sin((double)j/(double)size)) * es;
541 double z = (double)j * es;
548 for (int i = 0; i < size; i++) {
549 for (int j = 0; j < size; j++) {
550 int index = j * size + i;
553 triangles[index ] = (j * (size+1) + i );
554 triangles[index+1] = (j * (size+1) + i+1);
555 triangles[index+2] = ((j+1) * (size+1) + i );
556 triangles[index+3] = ((j+1) * (size+1) + i );
557 triangles[index+4] = ((j ) * (size+1) + i +1 );
558 triangles[index+5] = ((j+1) * (size+1) + i +1 );
562 int nofTriangles = triangles.length / 3;
563 int nofNodes = nodes.length / 3;
565 if(nofTriangles < 1) {
574 //System.out.println("v " + nofNodes + " t " +nofTriangles);
575 vtkPolyData partGrid = new vtkPolyData();
576 partGrid.Allocate(nofTriangles, nofTriangles);
578 vtkTriangle triangle = new vtkTriangle();
579 vtkIdList list = triangle.GetPointIds();
580 for(int i = 0; i < nofTriangles; i++)
583 n0 = triangles[3 * i]; n1 = triangles[3 * i + 1]; n2 = triangles[3 * i + 2]; // triangles(i).Get(n0, n1, n2);
585 // if(face.orientation() != TopAbs_Orientation.FORWARD) {
586 // int tmp = n2; n2 = n1; n1 = tmp;
592 partGrid.InsertNextCell(triangle.GetCellType(), list);
598 vtkPoints partPoints = new vtkPoints();
599 for(int i = 0; i < nofNodes; i++) {
600 double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]};
601 partPoints.InsertPoint(i, xyz);
604 partGrid.SetPoints(partPoints);
611 public static void test() {
612 TopoDS_Shape shape = null;
613 //shape = OccTriangulator.makeCylinder(new double[]{0,0,0}, new double[]{0,1,0}, 1, 1);
614 for (int t = 0; t < 5000; t++) {
615 //shape = OccTriangulator.makeCylinder(new double[]{0,0,0}, new double[]{0,1,0}, 1, 1);
618 vtkAssembly ass = VTKOCCTool.vtkOCCShapeToAssembly(shape);
619 vtkPropCollection col;
620 col = ass.GetParts();
621 for (int i = 0; i < col.GetNumberOfItems(); i++)
623 vtkProp prop = (vtkProp) col.GetItemAsObject(i);
624 //System.out.println(prop.Print());
630 else if (test == 1) {
631 //BRepTools.clean(shape);
634 vtkAssembly ass = new vtkAssembly();
636 double vol = OCCTTool.getBoundingBoxDiagonal(shape);
637 double d = 0.001 * vol;
638 BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,d);
639 TopExp_Explorer expFace = new TopExp_Explorer();
640 for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) {
641 TopoDS_Face face = (TopoDS_Face) expFace.current();
643 // TopLoc_Location Location = new TopLoc_Location();
644 // Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location);
645 // if (triangulation != null) {
647 // int[]triangles = triangulation.triangles();
648 // double[]nodes = triangulation.nodes();
649 // int nofTriangles = triangulation.nbTriangles();
650 // int nofNodes = triangulation.nbNodes();
652 // triangulation.delete();
654 // GP_Trsf transformation = Location.transformation();
655 // double d_mat[] = new double[16];
656 // transformation.getValues(d_mat);
657 // Matrix4d mat = new Matrix4d(d_mat);
658 // for(int i = 0; i < nofNodes; i++) {
659 // //double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]};
660 // Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]);
661 // //transformation.transforms(xyz);
664 // transformation.delete();
667 // Location.delete();
668 vtkPolyData data = VTKOCCTool.createPartGrid(face);
669 VTKOCCTool.gridToAssembly(ass, data);
677 vtkPropCollection col;
678 col = ass.GetParts();
679 for (int i = 0; i < col.GetNumberOfItems(); i++)
681 vtkProp prop = (vtkProp) col.GetItemAsObject(i);
682 //System.out.println(prop.Print());
687 } else if (test == 2) {
688 double[] pointStruct = new double[]{0,0,0}, dirStruct = new double[]{0,1,0};
692 double[] axe = new double[6];
694 System.arraycopy(pointStruct, 0, axe, 0, 3);
695 System.arraycopy(dirStruct, 0, axe, 3, 3);
696 org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder cyl = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder(axe, radius, height, 2 * Math.PI);
697 org.jcae.opencascade.jni.TopoDS_Shape tds = cyl.shape();
700 double vol = OCCTTool.getBoundingBoxDiagonal(tds);
701 double d = 0.001 * vol;
702 BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(tds,d);
703 // TopExp_Explorer expFace = new TopExp_Explorer();
704 // for (expFace.init(tds, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) {
705 // TopoDS_Face face = (TopoDS_Face) expFace.current();
716 System.out.println(t);