use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class FemMeshComp method collectEdgeVertices.
private Vertex3d[] collectEdgeVertices(HashMap<EdgeDesc, Vertex3d[]> edgeVtxMap, Vertex3d v0, Vertex3d v1, FemNode3d n0, FemNode3d n1, FemElement3d elem, int res) {
int numv = res + 1;
// numv is total number of vertices along the edge, including
// the end vertices. The number of intermediate vertices is numv-2
Vertex3d[] vtxs = edgeVtxMap.get(new EdgeDesc(v0, v1));
if (vtxs == null) {
vtxs = new Vertex3d[numv];
vtxs[0] = v0;
vtxs[numv - 1] = v1;
// create vertices along edge
for (int i = 1; i < numv - 1; i++) {
double s1 = i / (double) res;
vtxs[i] = createVertex(1 - s1, s1, 0, elem, n0, n1, null);
}
edgeVtxMap.put(new EdgeDesc(v0, v1), vtxs);
return vtxs;
} else if (vtxs[0] == v0) {
return vtxs;
} else {
// vertices in the wrong order; return a reversed list of them.
Vertex3d[] vtxsRev = new Vertex3d[numv];
for (int i = 0; i < numv; i++) {
vtxsRev[i] = vtxs[numv - 1 - i];
}
return vtxsRev;
}
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class FemMeshComp method createFineSurface.
protected void createFineSurface(int resolution, ElementFilter efilter) {
// build from nodes/element filter
createSurface(efilter);
if (resolution < 2) {
// if resolution < 2, just return regular surface
return;
}
// Note: can no longer rely on the surface mesh consisting of only
// FemMeshVertex
// PolygonalMesh baseMesh = myFem.getSurfaceMesh();
// since previously built
PolygonalMesh baseMesh = (PolygonalMesh) getMesh();
ArrayList<Face> baseFaces = baseMesh.getFaces();
ArrayList<Vertex3d> baseVertices = baseMesh.getVertices();
ArrayList<PointAttachment> baseAttachments = myVertexAttachments;
// num vertices along the edge of each sub face
int numv = resolution + 1;
initializeSurfaceBuild();
HashMap<EdgeDesc, Vertex3d[]> edgeVtxMap = new HashMap<EdgeDesc, Vertex3d[]>();
// get newly empty mesh
PolygonalMesh surfMesh = (PolygonalMesh) getMesh();
for (Vertex3d vtx : baseVertices) {
FemNode3d node = getNodeForVertex(baseAttachments.get(vtx.getIndex()));
createVertex(node, vtx);
// myNodeVertexMap.put (node, newVtx);
}
System.out.println("num base faces: " + baseFaces.size());
for (int k = 0; k < baseFaces.size(); k++) {
Face face = baseFaces.get(k);
// store sub vertices for the face in the upper triangular half of
// subv.
MeshFactory.VertexSet subv = new MeshFactory.VertexSet(numv);
FemElement3d elem = getFaceElement(face);
if (elem == null) {
continue;
}
HalfEdge he = face.firstHalfEdge();
Vertex3d v0 = (Vertex3d) he.getHead();
FemNode3d n0 = getNodeForVertex(baseAttachments.get(v0.getIndex()));
he = he.getNext();
Vertex3d v1 = (Vertex3d) he.getHead();
FemNode3d n1 = getNodeForVertex(baseAttachments.get(v1.getIndex()));
he = he.getNext();
Vertex3d v2 = (Vertex3d) he.getHead();
FemNode3d n2 = getNodeForVertex(baseAttachments.get(v2.getIndex()));
subv.set(0, 0, getVertex(v0.getIndex()));
subv.set(0, numv - 1, getVertex(v2.getIndex()));
subv.set(numv - 1, numv - 1, getVertex(v1.getIndex()));
Vertex3d[] vtxs01 = collectEdgeVertices(edgeVtxMap, v0, v1, n0, n1, elem, resolution);
for (int i = 1; i < numv - 1; i++) {
subv.set(i, i, vtxs01[i]);
}
Vertex3d[] vtxs02 = collectEdgeVertices(edgeVtxMap, v0, v2, n0, n2, elem, resolution);
for (int j = 1; j < numv - 1; j++) {
subv.set(0, j, vtxs02[j]);
}
Vertex3d[] vtxs21 = collectEdgeVertices(edgeVtxMap, v2, v1, n2, n1, elem, resolution);
for (int i = 1; i < numv - 1; i++) {
subv.set(i, numv - 1, vtxs21[i]);
}
for (int i = 1; i < numv - 1; i++) {
for (int j = i + 1; j < numv - 1; j++) {
double s1 = i / (double) resolution;
double s0 = 1 - j / (double) resolution;
Vertex3d vtx = createVertex(s0, s1, 1 - s0 - s1, elem, n0, n1, n2);
subv.set(i, j, vtx);
}
}
subv.check();
for (int i = 0; i < resolution; i++) {
for (int j = i; j < resolution; j++) {
surfMesh.addFace(subv.get(i, j), subv.get(i + 1, j + 1), subv.get(i, j + 1));
if (i != j) {
surfMesh.addFace(subv.get(i, j), subv.get(i + 1, j), subv.get(i + 1, j + 1));
}
}
}
}
finalizeSurfaceBuild();
}
use of maspack.geometry.Vertex3d 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.Vertex3d 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.Vertex3d in project artisynth_core by artisynth.
the class MFreeMeshComp 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 {
MFreeNode3d node = (MFreeNode3d) 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();
}
Aggregations