use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class Cell method writeVTK.
public static void writeVTK(String filename, PolygonalMesh mesh, ArrayList<String> pointData_scalar_names, ArrayList<double[]> pointData_scalar_data, ArrayList<String> pointData_vector_names, ArrayList<ArrayList<double[]>> pointData_vector_data, ArrayList<String> cellData_scalar_names, ArrayList<double[]> cellData_scalar_data, ArrayList<String> cellData_vector_names, ArrayList<ArrayList<double[]>> cellData_vector_data) {
// this writes a surface geometry
int nPoints = mesh.numVertices();
int nCells = mesh.numFaces();
try {
PrintWriter file = new PrintWriter(new BufferedWriter(new FileWriter(filename, false)));
// header
file.println("# vtk DataFile Version 3.0");
// title
file.println("SurfaceMesh");
// data type: ASCII or BINARY
file.println("ASCII");
// dataset type: STRUCTURED_POINTS, STRUCTURED_GRID, UNSTRUCTURED_GRID, POLYDATA, RECTILINEAR_GRID, FIELD
file.println("DATASET POLYDATA");
// write points
file.println();
file.printf("POINTS %d float\n", nPoints);
for (Vertex3d v : mesh.getVertices()) {
Point3d p = v.getPosition();
file.printf("%f %f %f\n", p.x, p.y, p.z);
}
// write cells
int nCellPoints = 0;
for (Face f : mesh.getFaces()) nCellPoints = nCellPoints + f.getVertexIndices().length;
file.println();
file.printf("POLYGONS %d %d\n", nCells, nCellPoints + nCells);
for (Face f : mesh.getFaces()) {
int[] indices = f.getVertexIndices();
int nInd = indices.length;
file.printf("%d", nInd);
for (int a = 0; a < nInd; a++) file.printf(" %d", indices[a]);
file.println();
}
// write point data
writePointData(file, nPoints, pointData_scalar_names, pointData_scalar_data, pointData_vector_names, pointData_vector_data);
// write cell data
writeCellData(file, nCells, cellData_scalar_names, cellData_scalar_data, cellData_vector_names, cellData_vector_data);
// file.flush();
file.close();
} catch (Exception e) {
}
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class CollisionRenderer method createPenetrationRenderObject.
RenderObject createPenetrationRenderObject(CollisionHandler handler, ArrayList<PenetratingPoint> points, ArrayList<PenetrationRegion> regions) {
RenderObject rd = new RenderObject();
HashMap<Vertex3d, Double> depthMap = new HashMap<Vertex3d, Double>();
double maxd = 0;
for (PenetratingPoint pp : points) {
depthMap.put(pp.vertex, pp.distance);
if (pp.distance > maxd) {
maxd = pp.distance;
}
}
ScalarRange range = handler.myBehavior.myPenetrationDepthRange;
range.updateInterval(0, maxd);
float[] rgb = new float[3];
for (int i = 0; i < 256; i++) {
handler.myManager.myColorMap.getRGB(i / 255.0, rgb);
rd.addColor(rgb);
}
Point3d wpnt = new Point3d();
Vector3d wnrm = new Vector3d();
for (PenetrationRegion region : regions) {
for (Face face : region.getFaces()) {
HalfEdge he = face.firstHalfEdge();
Vertex3d v0 = he.getHead();
Vertex3d v1 = he.getNext().getHead();
Vertex3d v2 = he.getTail();
v0.getWorldPoint(wpnt);
int pi0 = rd.addPosition(wpnt);
v1.getWorldPoint(wpnt);
int pi1 = rd.addPosition(wpnt);
v2.getWorldPoint(wpnt);
int pi2 = rd.addPosition(wpnt);
int ci0 = getColorIndex(range, v0, depthMap);
int ci1 = getColorIndex(range, v1, depthMap);
int ci2 = getColorIndex(range, v2, depthMap);
face.getWorldNormal(wnrm);
int ni = rd.addNormal(wnrm);
int v0idx = rd.addVertex(pi0, ni, ci0, -1);
int v1idx = rd.addVertex(pi1, ni, ci1, -1);
int v2idx = rd.addVertex(pi2, ni, ci2, -1);
rd.addTriangle(v0idx, v1idx, v2idx);
}
}
return rd;
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class MFreeFactory method cloneFem.
public static MFreeModel3d cloneFem(MFreeModel3d model, FemModel3d fem) {
if (model == null) {
model = new MFreeModel3d();
}
HashMap<FemNode3d, MFreeNode3d> nodeMap = new HashMap<FemNode3d, MFreeNode3d>();
HashMap<MFreeNode3d, FemNode3d> nodeMapInv = new HashMap<MFreeNode3d, FemNode3d>();
ArrayList<MFreeNode3d> nodeList = new ArrayList<MFreeNode3d>();
// duplicate nodes
for (FemNode3d node : fem.getNodes()) {
MFreeNode3d mnode = new MFreeNode3d(node.getRestPosition());
// explicit node masses
mnode.setMassExplicit(true);
mnode.setMass(node.getMass());
MFreeNode3d[] deps = new MFreeNode3d[1];
deps[0] = mnode;
VectorNd coords = new VectorNd(new double[] { 1 });
mnode.setDependentNodes(deps, coords);
nodeMap.put(node, mnode);
nodeMapInv.put(mnode, node);
nodeList.add(mnode);
}
// convert surface mesh
FemMeshComp surfaceFem = fem.getSurfaceMeshComp();
PolygonalMesh mesh = (PolygonalMesh) surfaceFem.getMesh();
// build mesh
PolygonalMesh mesh2 = mesh.copy();
// index vertices
int idx = 0;
for (Vertex3d vtx : mesh.getVertices()) {
vtx.setIndex(idx++);
}
idx = 0;
for (Vertex3d vtx : mesh2.getVertices()) {
vtx.setIndex(idx++);
}
MFreeMeshComp surfaceMFree = new MFreeMeshComp(model, "surface");
surfaceMFree.setMesh(mesh2);
// manually build surface attachments
for (Vertex3d vtx : mesh.getVertices()) {
PointAttachment pa = surfaceFem.getAttachment(vtx.getIndex());
if (pa instanceof PointFem3dAttachment) {
PointFem3dAttachment pfa = (PointFem3dAttachment) pa;
FemNode[] masters = pfa.getNodes();
MFreeNode3d[] deps = new MFreeNode3d[masters.length];
VectorNd coords = new VectorNd(masters.length);
for (int j = 0; j < masters.length; j++) {
// mlist.add (new ContactMaster (masters[j], pfa.getCoordinate(j)));
deps[j] = nodeMap.get(masters[j]);
coords.set(j, pfa.getCoordinate(j));
}
surfaceMFree.setVertexAttachment(vtx.getIndex(), coords.getBuffer(), deps);
} else {
PointParticleAttachment ppa = (PointParticleAttachment) pa;
DynamicComponent[] masters = ppa.getMasters();
MFreeNode3d[] deps = new MFreeNode3d[1];
deps[0] = nodeMap.get(masters[0]);
VectorNd coords = new VectorNd(new double[] { 1 });
surfaceMFree.setVertexAttachment(vtx.getIndex(), coords.getBuffer(), deps);
}
}
// integration regions by copying elements
ArrayList<MFreeElement3d> elemList = new ArrayList<MFreeElement3d>(fem.numElements());
HashMap<FemElement3d, MFreeElement3d> elemMap = new HashMap<FemElement3d, MFreeElement3d>(fem.numElements());
for (FemElement3d elem : fem.getElements()) {
MFreeNode3d[] elemNodes = new MFreeNode3d[elem.numNodes()];
FemNode3d[] fnodes = elem.getNodes();
for (int i = 0; i < elem.numNodes(); i++) {
elemNodes[i] = nodeMap.get(fnodes[i]);
}
MFreeElement3d region = new MFreeElement3d(null, elemNodes);
// region.setAllTermsActive(true);
MFreeIntegrationPoint3d[] mpnts = new MFreeIntegrationPoint3d[elem.numIntegrationPoints()];
IntegrationData3d[] mdata = new IntegrationData3d[elem.numIntegrationPoints()];
IntegrationPoint3d[] ipnts = elem.getIntegrationPoints();
IntegrationData3d[] idata = elem.getIntegrationData();
for (int i = 0; i < ipnts.length; i++) {
Point3d pos = new Point3d();
ipnts[i].computePosition(pos, elem.getNodes());
Vector3d[] gradU = ipnts[i].getGNs();
ArrayList<Vector3d> grads = new ArrayList<Vector3d>();
for (Vector3d g : gradU) {
grads.add(g);
}
MFreeIntegrationPoint3d mpnt = MFreeIntegrationPoint3d.create(elemNodes, ipnts[i].getShapeWeights(), grads, ipnts[i].getWeight());
IntegrationData3d mdat = new IntegrationData3d();
mdat.setRestInverseJacobian(idata[i].getInvJ0(), idata[i].getDetJ0());
mpnts[i] = mpnt;
mdata[i] = mdat;
}
// set warping point
if (region.getWarpingPoint() == null) {
IntegrationPoint3d wpnt = elem.getWarpingPoint();
IntegrationData3d wdat = elem.getWarpingData();
Point3d pos = new Point3d();
wpnt.computePosition(pos, elem.getNodes());
// Vector3d [] gradU = wpnt.updateShapeGradient(wdat.getInvJ0());
Vector3d[] gradU = wpnt.getGNs();
ArrayList<Vector3d> grads = new ArrayList<Vector3d>();
for (Vector3d g : gradU) {
grads.add(g);
}
// MFreeIntegrationPoint3d mpnt =
// MFreeIntegrationPoint3d.create(pos, deps,
// wpnt.getShapeWeights(), grads, wpnt.getWeight()*wdat.getDetJ0());
MFreeIntegrationPoint3d mpnt = MFreeIntegrationPoint3d.create(elemNodes, wpnt.getShapeWeights(), grads, wpnt.getWeight());
region.setWarpingPoint(mpnt);
IntegrationData3d wdata = new IntegrationData3d();
wdata.setRestInverseJacobian(wdat.getInvJ0(), wdat.getDetJ0());
region.setWarpingPoint(mpnt, wdata);
}
region.setIntegrationPoints(mpnts, mdata);
elemList.add(region);
elemMap.put(elem, region);
}
// add everything to model
model.addNodes(nodeList);
model.addElements(elemList);
model.setSurfaceMeshComp(surfaceMFree);
// copy properties
model.setDensity(fem.getDensity());
model.setParticleDamping(fem.getParticleDamping());
model.setStiffnessDamping(fem.getStiffnessDamping());
// copy over all masses
for (FemNode3d node : fem.getNodes()) {
nodeMap.get(node).setMass(node.getMass());
}
for (FemElement3d elem : fem.getElements()) {
elemMap.get(elem).setMass(elem.getMass());
}
model.setMaterial(fem.getMaterial());
return model;
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class MFreeFactory method computeNodeRadii.
private static double[] computeNodeRadii(Point3d[] nodes, Point3d[] ipnts, MeshBase mesh, int minK, double marginScale) {
Point3d min = new Point3d(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
Point3d max = new Point3d(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
for (Point3d node : nodes) {
node.updateBounds(min, max);
}
for (Point3d ipnt : ipnts) {
ipnt.updateBounds(min, max);
}
Vector3d widths = new Vector3d(max);
widths.sub(min);
IndexedPoint3d[] inodes = new IndexedPoint3d[nodes.length];
for (int i = 0; i < nodes.length; ++i) {
inodes[i] = new IndexedPoint3d(nodes[i], i);
}
KDTree<IndexedPoint3d> kdtree = new KDTree<IndexedPoint3d>(3, Arrays.asList(inodes), new MyKdComparator());
// KPointDistanceGrid grid = new KPointDistanceGrid(minK, trans, new int[] {30, 30, 30}, widths);
// for (Point3d node : nodes) {
// grid.addPoint(node);
// }
double[] r = new double[nodes.length];
// int[] closest = new int[minK];
// double[] dists = new double[minK];
IndexedPoint3d idpnt = new IndexedPoint3d(null, 0);
for (Point3d node : nodes) {
idpnt.pnt = node;
List<IndexedPoint3d> nearest = kdtree.nearestNeighbourSearch(idpnt, minK, 0);
// check for coplanarity
int kk = minK;
while (isCoplanar(nearest)) {
kk += 1;
nearest = kdtree.nearestNeighbourSearch(idpnt, kk, 0);
}
for (IndexedPoint3d nbr : nearest) {
double rl = nbr.pnt.distance(node) * marginScale;
if (rl > r[nbr.idx]) {
r[nbr.idx] = rl;
}
}
}
for (Point3d ipnt : ipnts) {
idpnt.pnt = ipnt;
List<IndexedPoint3d> nearest = kdtree.nearestNeighbourSearch(idpnt, minK, 0);
// check for coplanarity
int kk = minK;
while (isCoplanar(nearest)) {
kk += 1;
nearest = kdtree.nearestNeighbourSearch(idpnt, kk, 0);
}
for (IndexedPoint3d nbr : nearest) {
double rl = nbr.pnt.distance(ipnt) * marginScale;
if (rl > r[nbr.idx]) {
r[nbr.idx] = rl;
}
}
}
for (Vertex3d vtx : mesh.getVertices()) {
idpnt.pnt = vtx.getWorldPoint();
List<IndexedPoint3d> nearest = kdtree.nearestNeighbourSearch(idpnt, minK, 0);
// check for coplanarity
int kk = minK;
while (isCoplanar(nearest)) {
kk += 1;
nearest = kdtree.nearestNeighbourSearch(idpnt, kk, 0);
}
for (IndexedPoint3d nbr : nearest) {
double rl = nbr.pnt.distance(idpnt.pnt) * marginScale;
if (rl > r[nbr.idx]) {
r[nbr.idx] = rl;
}
}
}
return r;
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class MFreeMeshComp 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<MFreeNode3d, 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;
MFreeNode3d node = (MFreeNode3d) 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();
}
Aggregations