use of maspack.geometry.Face in project artisynth_core by artisynth.
the class MFreeMeshComp method createPointAttachment.
public PointFem3dAttachment createPointAttachment(Point pnt) {
if (!(getMesh() instanceof PolygonalMesh)) {
return null;
}
PolygonalMesh mesh = (PolygonalMesh) getMesh();
if (!mesh.isTriangular()) {
return null;
}
// Find nearest face to the point. The vertices of this face will be used
// to find the nodes and weight for the attachment.
BVFeatureQuery query = new BVFeatureQuery();
Point3d near = new Point3d();
Vector2d uv = new Vector2d();
Face face = query.nearestFaceToPoint(near, uv, mesh, pnt.getPosition());
Vertex3d[] vtxs = face.getTriVertices();
double[] wgts = new double[] { 1 - uv.x - uv.y, uv.x, uv.y };
HashMap<FemNode, Double> nodeWeights = new HashMap<FemNode, Double>();
for (int i = 0; i < vtxs.length; i++) {
PointAttachment va = myVertexAttachments.get(vtxs[i].getIndex());
if (va instanceof PointParticleAttachment) {
PointParticleAttachment ppa = (PointParticleAttachment) va;
FemNode node = (FemNode) ppa.getParticle();
accumulateNodeWeights(node, wgts[i], nodeWeights);
} else if (va instanceof PointFem3dAttachment) {
PointFem3dAttachment pfa = (PointFem3dAttachment) va;
for (int k = 0; k < pfa.numMasters(); k++) {
FemNode node = pfa.getNodes()[k];
double w = pfa.getCoordinate(k);
accumulateNodeWeights(node, w * wgts[i], nodeWeights);
}
}
}
// Create a new PointFem3dAttachment
PointFem3dAttachment ax = new PointFem3dAttachment(pnt);
VectorNd weightVec = new VectorNd();
for (Double d : nodeWeights.values()) {
weightVec.append(d);
}
ax.setFromNodes(nodeWeights.keySet(), weightVec);
return ax;
}
use of maspack.geometry.Face in project artisynth_core by artisynth.
the class MFreeMeshComp method writeMesh.
public boolean writeMesh(PrintWriter pw, boolean nodeFormat) {
PolygonalMesh mesh = null;
if (!(getMesh() instanceof PolygonalMesh)) {
return false;
}
mesh = (PolygonalMesh) getMesh();
pw.print("[ ");
NumberFormat fmt = new NumberFormat("%.8g");
IndentingPrintWriter.addIndentation(pw, 2);
if (!nodeFormat) {
for (Vertex3d vtx : mesh.getVertices()) {
writeVertexInfo(pw, vtx, fmt);
}
}
ArrayList<Integer> nodeNums = new ArrayList<Integer>();
for (Face face : mesh.getFaces()) {
HalfEdge he0 = face.firstHalfEdge();
HalfEdge he = he0;
pw.print("f");
do {
int vidx = he.head.getIndex();
if (nodeFormat) {
PointParticleAttachment ppa = (PointParticleAttachment) getAttachment(vidx);
MFreeNode3d node = (MFreeNode3d) ppa.getParticle();
pw.print(" " + node.getNumber());
} else {
pw.print(" " + (vidx + 1));
}
he = he.getNext();
} while (he != he0);
pw.println("");
}
IndentingPrintWriter.addIndentation(pw, -2);
pw.println("]");
return true;
}
use of maspack.geometry.Face in project artisynth_core by artisynth.
the class RigidMeshComp method getDistanceGrid.
@Override
public DistanceGrid getDistanceGrid() {
if (getMesh() instanceof PolygonalMesh) {
if (mySDGrid == null) {
List<Face> faces = ((PolygonalMesh) getMesh()).getFaces();
mySDGrid = new DistanceGrid(faces, myGridMargin, myMaxGridDivisions, /*signed=*/
true);
}
} else {
mySDGrid = null;
}
return mySDGrid;
}
use of maspack.geometry.Face in project artisynth_core by artisynth.
the class StlWriter method writeMesh.
public void writeMesh(PolygonalMesh mesh) throws IOException {
NumberFormat fmt = myFmt;
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(myOstream)));
String name = mesh.getName();
if (name == null) {
name = "";
}
pw.println("solid " + name);
for (Face face : mesh.getFaces()) {
Vector3d n = face.getNormal();
if (face.isTriangle()) {
pw.println("facet normal " + fmt.format((float) n.x) + " " + fmt.format((float) n.y) + " " + fmt.format((float) n.z));
} else {
pw.println("facet normal 0 0 0");
}
pw.println(" outer loop");
for (int i = 0; i < face.numVertices(); i++) {
Point3d pos = face.getVertex(i).getPosition();
pw.println(" vertex " + fmt.format((float) pos.x) + " " + fmt.format((float) pos.y) + " " + fmt.format((float) pos.z));
}
pw.println(" endloop");
pw.println("endfacet");
}
pw.println("endsolid " + name);
pw.close();
}
Aggregations