use of artisynth.core.mechmodels.PointParticleAttachment in project artisynth_core by artisynth.
the class FemMeshComp method buildNodeVertexMap.
protected void buildNodeVertexMap() {
myNodeVertexMap = new HashMap<FemNode3d, Vertex3d>();
myNumSingleAttachments = 0;
for (int i = 0; i < myVertexAttachments.size(); i++) {
PointAttachment pa = myVertexAttachments.get(i);
if (pa instanceof PointParticleAttachment) {
FemNode3d node = (FemNode3d) ((PointParticleAttachment) pa).getParticle();
myNodeVertexMap.put(node, getMesh().getVertex(i));
myNumSingleAttachments++;
} else if (pa instanceof PointFem3dAttachment) {
PointFem3dAttachment pfa = (PointFem3dAttachment) pa;
for (FemNode node : pfa.getNodes()) {
if (myNodeVertexMap.get(node) == null) {
myNodeVertexMap.put((FemNode3d) node, NO_SINGLE_VERTEX);
}
}
}
}
}
use of artisynth.core.mechmodels.PointParticleAttachment in project artisynth_core by artisynth.
the class FemMeshComp method scanMeshUsingNodeNumbers.
/**
* Old method of scanning mesh usign node numbers only.
*/
private void scanMeshUsingNodeNumbers(ReaderTokenizer rtok) throws IOException {
PolygonalMesh mesh = (PolygonalMesh) getMesh();
// nodeVertexMap is used during the construction of the mesh,
// so we build it during the construction rather then letting
// it be built in finalizeSurfaceBuild()
myNodeVertexMap = new HashMap<FemNode3d, Vertex3d>();
myNumSingleAttachments = 0;
ArrayList<Vertex3d> vtxList = new ArrayList<Vertex3d>();
rtok.nextToken();
while (rtok.tokenIsWord("f")) {
vtxList.clear();
rtok.nextToken();
while (rtok.tokenIsInteger()) {
int nnum = (int) rtok.lval;
FemNode3d node = getNodeFromNumber(rtok, nnum);
Vertex3d vtx = myNodeVertexMap.get(node);
if (vtx == null) {
vtx = new Vertex3d(new Point3d(node.getPosition()));
myNodeVertexMap.put(node, vtx);
myVertexAttachments.add(new PointParticleAttachment(node, null));
myNumSingleAttachments++;
mesh.addVertex(vtx);
}
vtxList.add(vtx);
rtok.nextToken();
}
mesh.addFace(vtxList.toArray(new Vertex3d[0]));
}
rtok.pushBack();
}
use of artisynth.core.mechmodels.PointParticleAttachment in project artisynth_core by artisynth.
the class FemMeshComp method scanMeshUsingVertexInfo.
/**
* New scan method where the vertex attachments are also scanned
*/
private void scanMeshUsingVertexInfo(ReaderTokenizer rtok) throws IOException {
PolygonalMesh mesh = (PolygonalMesh) getMesh();
ArrayList<Vertex3d> vtxList = new ArrayList<Vertex3d>();
ArrayList<FemNode> nodes = new ArrayList<FemNode>();
VectorNd weights = new VectorNd();
rtok.nextToken();
while (rtok.tokenIsWord()) {
if (rtok.sval.equals("v")) {
int nnum = rtok.scanInteger();
if (nnum == -1) {
double x = rtok.scanNumber();
double y = rtok.scanNumber();
double z = rtok.scanNumber();
mesh.addVertex(new Vertex3d(x, y, z));
myVertexAttachments.add(null);
rtok.nextToken();
} else {
PointAttachment ax;
double w = rtok.scanNumber();
rtok.nextToken();
Vertex3d vtx = new Vertex3d();
if (rtok.tokenIsInteger()) {
nodes.clear();
weights.setSize(0);
nodes.add(getNodeFromNumber(rtok, nnum));
weights.append(w);
while (rtok.tokenIsInteger()) {
nodes.add(getNodeFromNumber(rtok, (int) rtok.lval));
weights.append(rtok.scanNumber());
rtok.nextToken();
}
PointFem3dAttachment attacher = new PointFem3dAttachment();
attacher.setFromNodes(nodes, weights);
ax = attacher;
} else {
FemNode3d node = getNodeFromNumber(rtok, nnum);
ax = new PointParticleAttachment(node, null);
}
mesh.addVertex(vtx);
myVertexAttachments.add(ax);
}
} else if (rtok.sval.equals("f")) {
vtxList.clear();
rtok.nextToken();
while (rtok.tokenIsInteger()) {
int vnum = (int) rtok.lval;
if (vnum > mesh.numVertices()) {
throw new IOException("Vertex number " + vnum + " not found, " + rtok);
}
vtxList.add(mesh.getVertex(vnum - 1));
rtok.nextToken();
}
mesh.addFace(vtxList.toArray(new Vertex3d[0]));
} else {
throw new IOException("Unexpected token: " + rtok);
}
}
rtok.pushBack();
}
use of artisynth.core.mechmodels.PointParticleAttachment in project artisynth_core by artisynth.
the class FemMeshComp 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);
FemNode3d node = (FemNode3d) 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 artisynth.core.mechmodels.PointParticleAttachment in project artisynth_core by artisynth.
the class FemMeshComp method createEmbedded.
public static FemMeshComp createEmbedded(FemMeshComp surf, MeshBase mesh, FemModel3d fem) {
double reduceTol = 1e-8;
ArrayList<FemNode> nodes = new ArrayList<FemNode>();
VectorNd weights = new VectorNd();
if (surf == null) {
surf = new FemMeshComp(fem);
}
surf.setMesh(mesh);
ArrayList<Vertex3d> verts = mesh.getVertices();
surf.myVertexAttachments.clear();
for (int i = 0; i < verts.size(); i++) {
// this could works very similarly to the code that adds
// marker points into a mesh
Vertex3d vtx = verts.get(i);
// if (vtx instanceof FemMeshVertex) {
// nodes.clear();
// nodes.add(((FemMeshVertex)vtx).getPoint());
// weights.clear();
// weights.add(1.0);
// }
// else
{
FemElement3d elem = surf.myFem.findContainingElement(vtx.pnt);
Point3d newLoc = new Point3d(vtx.pnt);
if (elem == null) {
// won't use newLoc since we're not projecting vertex onto FEM
elem = surf.myFem.findNearestSurfaceElement(newLoc, vtx.pnt);
}
VectorNd coords = new VectorNd(elem.numNodes());
// first see if there's a node within reduceTol of the point,
// and if so just use that
double maxDist = Double.NEGATIVE_INFINITY;
double minDist = Double.POSITIVE_INFINITY;
FemNode3d nearestNode = null;
FemNode3d[] elemNodes = elem.getNodes();
for (int k = 0; k < elemNodes.length; k++) {
double d = vtx.pnt.distance(elemNodes[k].getPosition());
if (d > maxDist) {
maxDist = d;
}
if (d < minDist) {
minDist = d;
nearestNode = elemNodes[k];
}
}
if (minDist / maxDist <= reduceTol) {
// weight everything to the nearest node
nodes.clear();
nodes.add(nearestNode);
weights.setSize(0);
weights.append(1.0);
} else {
Vector3d c3 = new Vector3d();
boolean converged = elem.getNaturalCoordinates(c3, vtx.pnt, 1000) >= 0;
if (!converged) {
System.err.println("Warning: getNaturalCoordinatesRobust() did not converge, " + "element=" + ComponentUtils.getPathName(elem) + ", point=" + vtx.pnt);
// c3.setZero();
// XXX debugging:
// elem.getNaturalCoordinates(c3, vtx.pnt, 1000); // try again once more
}
for (int j = 0; j < elem.numNodes(); j++) {
coords.set(j, elem.getN(j, c3));
}
nodes.clear();
weights.setSize(0);
for (int k = 0; k < coords.size(); k++) {
if (Math.abs(coords.get(k)) >= reduceTol) {
nodes.add(elem.getNodes()[k]);
weights.append(coords.get(k));
}
}
}
}
if (weights.size() > 1) {
PointFem3dAttachment attacher = new PointFem3dAttachment();
attacher.setFromNodes(nodes, weights);
surf.myVertexAttachments.add(attacher);
} else if (weights.size() == 1) {
PointParticleAttachment attacher = new PointParticleAttachment(nodes.get(0), null);
surf.myVertexAttachments.add(attacher);
}
}
surf.buildNodeVertexMap();
return surf;
}
Aggregations