/******************************************************************************* * 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.opencascade; import java.util.List; import javax.vecmath.Matrix4d; import javax.vecmath.Point3d; import org.jcae.opencascade.jni.BRepBndLib; import org.jcae.opencascade.jni.BRepGProp; import org.jcae.opencascade.jni.BRep_Tool; import org.jcae.opencascade.jni.Bnd_Box; import org.jcae.opencascade.jni.GP_Trsf; import org.jcae.opencascade.jni.GProp_GProps; import org.jcae.opencascade.jni.GProp_VelGProps; import org.jcae.opencascade.jni.Poly_Triangulation; import org.jcae.opencascade.jni.TopAbs_Orientation; import org.jcae.opencascade.jni.TopLoc_Location; import org.jcae.opencascade.jni.TopoDS_Face; import org.jcae.opencascade.jni.TopoDS_Shape; public class OCCTTool { public static double getBoundingBoxDiagonal(TopoDS_Shape shape) { double []min = new double[3]; double []max = new double[3]; double []mmm = new double[3]; double []bb = new double[6]; // Compute bounding box: //---------------------- Bnd_Box boundingBox = new Bnd_Box(); BRepBndLib.add(shape, boundingBox); boundingBox.get(bb); boundingBox.delete(); min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2]; max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5]; //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]"); // The length of the space diagonal of cuboid for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i]; double length = Math.sqrt(mmm[2]*mmm[2] + mmm[1]*mmm[1] + mmm[0]*mmm[0]); double t0 = mmm[0]*mmm[0]; double t1 = mmm[1]*mmm[1]; double t2 = mmm[2]*mmm[2]; double tol = 1.0e-6 * length; if((t0 < tol) || (t1 < tol) || (t2 < tol)) { System.out.println("Shape seems to be 2D. Unable to proceed. Aborting."); return 0; } return length; } public static double getBoundingBoxVolume(TopoDS_Shape shape) { double []min = new double[3]; double []max = new double[3]; double []mmm = new double[3]; double []bb = new double[6]; // Compute bounding box: //---------------------- Bnd_Box boundingBox = new Bnd_Box(); BRepBndLib.add(shape, boundingBox); boundingBox.get(bb); boundingBox.delete(); min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2]; max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5]; //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]"); for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i]; double vol = Math.sqrt(mmm[2]*mmm[1]*mmm[0]); return vol; } private static GProp_GProps getGProp(TopoDS_Shape shape) { GProp_GProps GSystem = null; int type = 0; if (type == 0) { GSystem = new GProp_GProps(); BRepGProp.volumeProperties(shape, GSystem,0.001); } else if (type == 1) { GSystem = new GProp_VelGProps(); BRepGProp.volumeProperties(shape, GSystem,0.001); } else if (type == 2) { GSystem = new GProp_VelGProps(); BRepGProp.volumePropertiesGK(shape, GSystem, 0.001); } return GSystem; } public static double getMass( TopoDS_Shape shape) { GProp_GProps GSystem = getGProp(shape); double mass = GSystem.mass(); //System.out.println("Mass " + mass); GSystem.delete(); return mass; } public static double[] getInertia(TopoDS_Shape shape) { GProp_GProps GSystem = getGProp(shape); double inertia[] = GSystem.matrixOfInertia(); GSystem.delete(); return inertia; } public static double[] getCentreOfMass(TopoDS_Shape shape) { GProp_GProps GSystem = getGProp(shape); double cm[] = GSystem.centreOfMass(); GSystem.delete(); return cm; } public static boolean appendToMesh (TopoDS_Face face, List meshPoints, List meshTriangles) { TopLoc_Location Location = new TopLoc_Location(); Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location); if(triangulation == null) { Location.delete(); System.out.println("Encountered empty triangulation after face"); return false; } boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED; int lastPoint = meshPoints.size() / 3; int[]triangles = triangulation.triangles(); double[]nodes = triangulation.nodes(); int nofTriangles = triangulation.nbTriangles(); int nofNodes = triangulation.nbNodes(); triangulation.delete(); if(nofTriangles < 1) { System.out.println("No triangles for mesh on face"); Location.delete(); return false; } if(nofNodes < 1) { System.out.println("No nodes for mesh on face:"); Location.delete(); return false; } for(int i = 0; i < nofTriangles; i++) { int n0, n1, n2; if (!reverse) { n0 = triangles[3 * i]; n1 = triangles[3 * i + 1]; n2 = triangles[3 * i + 2]; } else { n0 = triangles[3 * i + 2]; n1 = triangles[3 * i + 1]; n2 = triangles[3 * i]; } meshTriangles.add(n0 + lastPoint); meshTriangles.add(n1 + lastPoint); meshTriangles.add(n2 + lastPoint); } GP_Trsf transformation = Location.transformation(); Location.delete(); double d_mat[] = new double[16]; transformation.getValues(d_mat); Matrix4d mat = new Matrix4d(d_mat); for(int i = 0; i < nofNodes; i++) { Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]); mat.transform(p); meshPoints.add(p.x); meshPoints.add(p.y); meshPoints.add(p.z); } transformation.delete(); return true; } }