use of maspack.geometry.PolygonalMesh in project artisynth_core by artisynth.
the class FemModel3dAgent method createPreviewModel.
private void createPreviewModel() {
fem = new FemModel3d();
setProperties(fem, getPrototypeComponent(myComponentType));
setProperties(myPrototype, myPrototype);
FemElementType elemType = null;
FemMeshType meshType = (FemMeshType) meshSelector.getValue();
if (elemSelector.isEnabledAll()) {
elemType = (FemElementType) elemSelector.getValue();
}
switch(meshType) {
case Grid:
{
VectorBase dims = gridDimField.getVectorValue();
int[] divs = gridDivField.getVectorValue();
FemFactory.createGrid(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
break;
}
case Tube:
{
VectorBase dims = tubeDimField.getVectorValue();
int[] divs = tubeDivField.getVectorValue();
FemFactory.createTube(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
break;
}
case Torus:
{
VectorBase dims = torusDimField.getVectorValue();
int[] divs = torusDivField.getVectorValue();
FemFactory.createTorus(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
break;
}
case Sphere:
{
int nodes = (Integer) sphereNodesField.getValue();
String meshPath;
if (nodes == SPHERE_NODE_OPTIONS[0]) {
meshPath = ArtisynthPath.getHomeRelativePath(SPHERE_54_MESH_PATH, ".");
} else if (nodes == SPHERE_NODE_OPTIONS[1]) {
meshPath = ArtisynthPath.getHomeRelativePath(SPHERE_196_MESH_PATH, ".");
} else {
EditorUtils.showError(myDisplay, "Invalid number of nodes for sphere");
return;
}
try {
TetGenReader.read(fem, 1000, meshPath + ".node", meshPath + ".ele", new Vector3d(1, 1, 1));
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case Extrusion:
{
double d = extrusDepthField.getDoubleValue();
int n = extrusLayersField.getIntValue();
String meshFileName = extrusFileField.getStringValue();
try {
PolygonalMesh mesh = new PolygonalMesh(new File(meshFileName));
FemFactory.createExtrusion(fem, elemType, n, d, 0, mesh);
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case AnsysMesh:
{
String nodeFileName = ansysNodeFileField.getStringValue();
String elemFileName = ansysElemFileField.getStringValue();
try {
AnsysReader.read(fem, nodeFileName, elemFileName, 1000, null, /*options=*/
0);
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case TetgenMesh:
{
String nodeFileName = tetgenNodeFileField.getStringValue();
String eleFileName = tetgenEleFileField.getStringValue();
try {
TetGenReader.read(fem, 1000, nodeFileName, eleFileName, new Vector3d(1, 1, 1));
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case UCDMesh:
{
String ucdFileName = ucdMeshFileField.getStringValue();
try {
UCDReader.read(fem, ucdFileName, 1000);
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
break;
}
case SurfaceMesh:
{
String objFileName = surfaceMeshFileField.getStringValue();
PolygonalMesh surfaceMesh = null;
try {
surfaceMesh = new PolygonalMesh(new File(objFileName));
} catch (Exception e) {
EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
return;
}
try {
FemFactory.createFromMesh(fem, surfaceMesh, /*quality=*/
2.0);
} catch (Exception e) {
e.printStackTrace();
EditorUtils.showError(myDisplay, "Error tessellating mesh: " + e.getMessage());
return;
}
break;
}
default:
{
throw new InternalErrorException("Unimplemented mesh type");
}
}
RigidTransform3d X = new RigidTransform3d();
X.p.set(positionField.getVectorValue());
X.R.setAxisAngle(orientationField.getAxisAngleValue());
PolygonalMesh mesh = fem.getSurfaceMesh();
RenderProps props = mesh.createRenderProps();
props.setFaceStyle(Renderer.FaceStyle.NONE);
props.setDrawEdges(true);
props.setLineColor(Color.LIGHT_GRAY);
mesh.setRenderProps(props);
mesh.setMeshToWorld(X);
mesh.setFixed(false);
mesh.setRenderBuffered(false);
if (meshPropPanel.getComponentIndex(scaleField) != -1) {
scaleField.maskValueChangeListeners(true);
scaleField.setValue(1.0);
scaleField.maskValueChangeListeners(false);
lastScale = 1.0;
}
myMain.getWorkspace().getViewerManager().addRenderable(mesh);
rotator = new Transrotator3d();
GLViewer viewer = myMain.getMain().getViewer();
rotator.setDraggerToWorld(X);
rotator.setSize(viewer.distancePerPixel(viewer.getCenter()) * viewer.getScreenWidth() / 6);
rotator.addListener(new FemModelDraggerListener());
myMain.getWorkspace().getViewerManager().addDragger(rotator);
myMain.rerender();
}
use of maspack.geometry.PolygonalMesh in project artisynth_core by artisynth.
the class FemDisplayProbe method updateVertexIndicators.
/**
* determines which vertices are inside the supplied mesh
*/
protected void updateVertexIndicators() {
vtxIndicatorMap = new HashMap<Vertex3d, Boolean>(myPlaneSurface.numVertices());
TriangleIntersector ti = new TriangleIntersector();
BVFeatureQuery query = new BVFeatureQuery();
ti.setEpsilon(myIntersectionTolerance);
if (myFem != null) {
PolygonalMesh mesh = myFem.getSurfaceMesh();
for (Vertex3d vtx : myPlaneSurface.getVertices()) {
Point3d pnt = vtx.getWorldPoint();
if (query.isInsideOrientedMesh(mesh, pnt, myIntersectionTolerance)) {
vtxIndicatorMap.put(vtx, true);
} else {
vtxIndicatorMap.put(vtx, false);
}
}
}
}
use of maspack.geometry.PolygonalMesh in project artisynth_core by artisynth.
the class MDLMeshIO method read.
/**
* Creates a PolygonalMesh based on MDL data read from a Reader. The node
* coordinate data can be scaled non-uniformly using an optional parameter
* giving scale values about the x, y, and z axes.
*
* @param reader
* the Reader which references MDL data to be read
* @param scale
* if non-null, gives scaling about the x, y, and z axes
* @return created polygonal mesh
* @throws IOException
* if this is a problem reading the file
*/
public static PolygonalMesh read(Reader reader, Vector3d scale) throws IOException {
PolygonalMesh mesh = new PolygonalMesh();
ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(reader));
rtok.wordChars("<>.:/\\");
// read top header
MDLHeader header = MDLHeader.scan(rtok);
if (!header.equals(defaultTopHeader)) {
// throw new IOException ("MDLReader: bad top header, " + header);
}
// read file name
if (rtok.nextToken() != ReaderTokenizer.TT_WORD) {
throw new IOException("MDLReader: expecting filename, got " + rtok.tokenName());
}
// read vertices header information
header = MDLHeader.scan(rtok);
if (!header.equals(defaultVerticesHeader)) {
// throw new IOException ("MDLReader: bad vertices header, " + header);
System.err.println("MDLReader: bad vertices header, no vertices read...");
return mesh;
}
// read vertices
int numVertices = rtok.scanInteger();
Point3d coords = new Point3d();
for (int i = 0; i < numVertices; i++) {
coords.x = rtok.scanNumber();
coords.y = rtok.scanNumber();
coords.z = rtok.scanNumber();
if (scale != null) {
coords.x *= scale.x;
coords.y *= scale.y;
coords.z *= scale.z;
}
mesh.addVertex(coords);
}
// read normals header information
try {
header = MDLHeader.scan(rtok);
} catch (IOException e) {
System.out.println("MDLReader: no normals read...");
return mesh;
}
if (!header.equals(defaultNormalsHeader)) {
// throw new IOException ("MDLReader: bad normals header, " + header);
System.out.println("MDLReader: no normals read...");
return mesh;
}
// read vertices
int numNormals = rtok.scanInteger();
ArrayList<Vector3d> vn = new ArrayList<Vector3d>(numNormals);
for (int i = 0; i < numNormals; i++) {
Vector3d vec = new Vector3d();
vec.x = rtok.scanNumber();
vec.y = rtok.scanNumber();
vec.z = rtok.scanNumber();
if (scale != null) {
vec.x *= scale.x;
vec.y *= scale.y;
vec.z *= scale.z;
}
vn.add(vec);
}
// read faces header information
try {
header = MDLHeader.scan(rtok);
} catch (IOException e) {
System.out.println("MDLReader: no faces read...");
return mesh;
}
if (!header.equals(defaultFacesHeader)) {
// throw new IOException ("MDLReader: bad faces header, " + header);
System.out.println("MDLReader: no faces read...");
return mesh;
}
// read vertices
int numFaces = rtok.scanInteger();
int[] vi = new int[3];
boolean faceIdxWarningGiven = false;
int[] normalIdxs = new int[numFaces * 3];
int k = 0;
for (int i = 0; i < numFaces; i++) {
int[] ni = new int[3];
vi[0] = rtok.scanInteger();
vi[1] = rtok.scanInteger();
vi[2] = rtok.scanInteger();
ni[0] = rtok.scanInteger();
ni[1] = rtok.scanInteger();
ni[2] = rtok.scanInteger();
if (!faceIdxWarningGiven && (vi[0] != ni[0] || vi[0] != ni[0] || vi[0] != ni[0])) {
System.out.println("Warning: MDL face idxs don't match normal idxs; ignoring");
faceIdxWarningGiven = true;
}
mesh.addFace(vi);
normalIdxs[k++] = ni[0];
normalIdxs[k++] = ni[1];
normalIdxs[k++] = ni[2];
}
mesh.setNormals(vn, normalIdxs);
return mesh;
}
use of maspack.geometry.PolygonalMesh in project artisynth_core by artisynth.
the class MDLMeshIO method read.
/**
* Creates a PolygonalMesh based on MDL data contained in a specified file.
* The node coordinate data can be scaled non-uniformly using an optional
* parameter giving scale values about the x, y, and z axes.
*
* @param fileName
* path name of the MDL file
* @param scale
* if non-null, gives scaling about the x, y, and z axes
* @return created polygonal mesh
* @throws IOException
* if this is a problem reading the file
*/
public static PolygonalMesh read(String fileName, Vector3d scale) throws IOException {
Reader reader = new FileReader(fileName);
PolygonalMesh mesh = read(reader, scale);
reader.close();
return mesh;
}
use of maspack.geometry.PolygonalMesh in project artisynth_core by artisynth.
the class ObjToMdl method main.
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("usage: java artisynth.models.badin.ObjToMdl inputObjMeshName outputMdlMeshName");
return;
}
try {
PolygonalMesh mesh = new PolygonalMesh(new File(args[0]));
PrintWriter pw = new PrintWriter(new File(args[1]));
MDLMeshIO.write(mesh, args[1], pw, new NumberFormat("%g"));
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Aggregations