use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class SDGridTest method build.
@Override
public void build(String[] args) throws IOException {
super.build(args);
PolygonalMesh mesh = new PolygonalMesh();
Vertex3d[] v = new Vertex3d[9 * 2 + 8];
Face[] f = new Face[8 * 6];
int idx = 0;
int fidx = 0;
double c = 64;
v[idx++] = mesh.addVertex(-c, -c, -c);
v[idx++] = mesh.addVertex(-c, -c, 0);
v[idx++] = mesh.addVertex(-c, -c, c);
v[idx++] = mesh.addVertex(-c, 0, -c);
v[idx++] = mesh.addVertex(-c, 0, 0);
v[idx++] = mesh.addVertex(-c, 0, c);
v[idx++] = mesh.addVertex(-c, c, -c);
v[idx++] = mesh.addVertex(-c, c, 0);
v[idx++] = mesh.addVertex(-c, c, c);
// left
f[fidx++] = mesh.addFace(v[0], v[1], v[4]);
f[fidx++] = mesh.addFace(v[1], v[2], v[4]);
f[fidx++] = mesh.addFace(v[2], v[5], v[4]);
f[fidx++] = mesh.addFace(v[5], v[8], v[4]);
f[fidx++] = mesh.addFace(v[8], v[7], v[4]);
f[fidx++] = mesh.addFace(v[7], v[6], v[4]);
f[fidx++] = mesh.addFace(v[6], v[3], v[4]);
f[fidx++] = mesh.addFace(v[3], v[0], v[4]);
v[idx++] = mesh.addVertex(0, -c, -c);
v[idx++] = mesh.addVertex(0, -c, 0);
v[idx++] = mesh.addVertex(0, -c, c);
v[idx++] = mesh.addVertex(0, 0, -c);
// test.addVertex( 0, 0, 0);
v[idx++] = mesh.addVertex(0, 0, c);
v[idx++] = mesh.addVertex(0, c, -c);
v[idx++] = mesh.addVertex(0, c, 0);
v[idx++] = mesh.addVertex(0, c, c);
// back
f[fidx++] = mesh.addFace(v[0], v[9], v[10]);
f[fidx++] = mesh.addFace(v[1], v[0], v[10]);
f[fidx++] = mesh.addFace(v[2], v[1], v[10]);
f[fidx++] = mesh.addFace(v[11], v[2], v[10]);
// top
f[fidx++] = mesh.addFace(v[2], v[11], v[13]);
f[fidx++] = mesh.addFace(v[5], v[2], v[13]);
f[fidx++] = mesh.addFace(v[8], v[5], v[13]);
f[fidx++] = mesh.addFace(v[16], v[8], v[13]);
// front
f[fidx++] = mesh.addFace(v[8], v[16], v[15]);
f[fidx++] = mesh.addFace(v[7], v[8], v[15]);
f[fidx++] = mesh.addFace(v[6], v[7], v[15]);
f[fidx++] = mesh.addFace(v[14], v[6], v[15]);
// bottom
f[fidx++] = mesh.addFace(v[3], v[6], v[12]);
f[fidx++] = mesh.addFace(v[6], v[14], v[12]);
f[fidx++] = mesh.addFace(v[0], v[3], v[12]);
f[fidx++] = mesh.addFace(v[9], v[0], v[12]);
v[idx++] = mesh.addVertex(c, -c, -c);
v[idx++] = mesh.addVertex(c, -c, 0);
v[idx++] = mesh.addVertex(c, -c, c);
v[idx++] = mesh.addVertex(c, 0, -c);
v[idx++] = mesh.addVertex(c, 0, 0);
v[idx++] = mesh.addVertex(c, 0, c);
v[idx++] = mesh.addVertex(c, c, -c);
v[idx++] = mesh.addVertex(c, c, 0);
v[idx++] = mesh.addVertex(c, c, c);
// bottom
f[fidx++] = mesh.addFace(v[17], v[9], v[12]);
f[fidx++] = mesh.addFace(v[20], v[17], v[12]);
f[fidx++] = mesh.addFace(v[23], v[20], v[12]);
f[fidx++] = mesh.addFace(v[14], v[23], v[12]);
f[fidx++] = mesh.addFace(v[9], v[17], v[10]);
f[fidx++] = mesh.addFace(v[17], v[18], v[10]);
f[fidx++] = mesh.addFace(v[18], v[19], v[10]);
f[fidx++] = mesh.addFace(v[19], v[11], v[10]);
f[fidx++] = mesh.addFace(v[11], v[19], v[13]);
f[fidx++] = mesh.addFace(v[19], v[22], v[13]);
f[fidx++] = mesh.addFace(v[22], v[25], v[13]);
f[fidx++] = mesh.addFace(v[25], v[16], v[13]);
f[fidx++] = mesh.addFace(v[16], v[25], v[15]);
f[fidx++] = mesh.addFace(v[25], v[24], v[15]);
f[fidx++] = mesh.addFace(v[24], v[23], v[15]);
f[fidx++] = mesh.addFace(v[23], v[14], v[15]);
f[fidx++] = mesh.addFace(v[25], v[22], v[21]);
f[fidx++] = mesh.addFace(v[22], v[19], v[21]);
f[fidx++] = mesh.addFace(v[19], v[18], v[21]);
f[fidx++] = mesh.addFace(v[18], v[17], v[21]);
f[fidx++] = mesh.addFace(v[17], v[20], v[21]);
f[fidx++] = mesh.addFace(v[20], v[23], v[21]);
f[fidx++] = mesh.addFace(v[23], v[24], v[21]);
f[fidx++] = mesh.addFace(v[24], v[25], v[21]);
// Move in some corner(s)
v[0].setPosition(new Point3d(0, 0, 0));
// v[25].setPosition(new Point3d(0,0,0));
int divisions = 3;
mesh = MeshFactory.subdivide(mesh, divisions);
// randomize vertex order and re-number (to test different chiralities)
ArrayList<Vertex3d> vertices = mesh.getVertices();
Collections.shuffle(vertices);
for (int i = 0; i < vertices.size(); ++i) {
vertices.get(i).setIndex(i);
}
FixedMeshBody fm = new FixedMeshBody("cube", mesh);
RenderProps.setFaceStyle(fm, FaceStyle.FRONT_AND_BACK);
addRenderable(fm);
int cells = 1 << (divisions + 2);
double margin = 1.0 / cells;
cells += 2;
sdgrid = new DistanceGrid(mesh.getFaces(), margin, cells, /*signed=*/
true);
// test a bunch of inside points
Vector3d norm = new Vector3d();
double dx = 2 * c / ((1 << (divisions + 1)));
for (double x = -c + dx; x < c; x += dx) {
for (double y = -c + dx; y < c; y += dx) {
for (double z = -c + dx; z < c; z += dx) {
double d = sdgrid.getLocalDistanceAndNormal(norm, x, y, z);
// check bottom corner
if ((x < 0 && y < 0 && z < 0)) {
// ||(x > 0 && y > 0 && z > 0)) {
if (d < 0) {
System.err.println("Point (" + x + "," + y + "," + z + ") incorrectly labelled as inside");
}
} else {
if (d > 0) {
System.err.println("Point (" + x + "," + y + "," + z + ") incorrectly labelled as outside");
}
}
}
}
}
RenderProps.setDrawEdges(fm, true);
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class SurfaceMeshCollider method findEdgeEdgeContacts.
/*
* Create an edge-edge contact for each unique pair of HalfEdges where - the
* edges are from opposite regions, - both edges are in need of edge-edge
* correction - the geometry is suitable (see EdgeEdgeContact.calculate())
*/
public ArrayList<EdgeEdgeContact> findEdgeEdgeContacts(ContactInfo cinfo) {
/* First scan the contours for interlocking triangles, */
HashMap<Vertex3d, PenetratingPoint> vertexCpps = new HashMap<Vertex3d, PenetratingPoint>();
for (PenetratingPoint cpp : cinfo.getPenetratingPoints(0)) {
vertexCpps.put(cpp.vertex, cpp);
}
for (PenetratingPoint cpp : cinfo.getPenetratingPoints(1)) {
vertexCpps.put(cpp.vertex, cpp);
}
ArrayList<EdgeEdgeContact> contacts = new ArrayList<EdgeEdgeContact>();
HashMap<PenetrationRegion, PenetrationRegion> opposingRegions = cinfo.findMatchingRegions();
for (PenetrationRegion r0 : opposingRegions.keySet()) {
PenetrationRegion r1 = opposingRegions.get(r0);
for (HalfEdge edge0 : r0.myEdges) {
if (needsEdgeEdgeCorrection(edge0, vertexCpps)) {
for (HalfEdge edge1 : r1.myEdges) {
if (needsEdgeEdgeCorrection(edge1, vertexCpps)) {
EdgeEdgeContact eec = new EdgeEdgeContact();
if (eec.calculate(this, edge0, edge1)) {
// Test the geometry to see if it is a valid edge-edge
// contact.
eec.region = r0;
contacts.add(eec);
}
}
}
}
}
}
return contacts;
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class MeshFactoryTest method testMaxMin.
public void testMaxMin() {
PolygonalMesh mesh = new PolygonalMesh();
// mesh = MeshFactory.createSphere (10, 10);
mesh = MeshFactory.createSphere(1.0, 4);
Point3d meshMax = new Point3d();
Point3d meshMin = new Point3d();
mesh.getLocalBounds(meshMin, meshMax);
for (Vertex3d v : mesh.myVertices) {
if (v.pnt.x < meshMin.x) {
throw new TestException("Mesh's minimum is incorrect");
}
if (v.pnt.x > meshMax.x) {
throw new TestException("Mesh's maximum is incorrect");
}
if (v.pnt.y < meshMin.y) {
throw new TestException("Mesh's minimum is incorrect");
}
if (v.pnt.y > meshMax.y) {
throw new TestException("Mesh's maximum is incorrect");
}
if (v.pnt.z < meshMin.z) {
throw new TestException("Mesh's minimum is incorrect");
}
if (v.pnt.z > meshMax.z) {
throw new TestException("Mesh's maximum is incorrect");
}
}
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class MayaAsciiReader method recursiveAddPolygonalMeshes.
private void recursiveAddPolygonalMeshes(Node<MayaNode> root, PolygonalMesh mesh, AffineTransform3d trans, UnitInfo units) {
// make copy so can traverse
trans = new AffineTransform3d(trans);
// children independently
MayaNode data = root.getData();
if (data instanceof MayaTransform) {
MayaTransform dtrans = (MayaTransform) data;
AffineTransform3d tu = new AffineTransform3d();
dtrans.getTransform(tu);
// convert units
tu.p.scale(dtrans.units.length.getSI() / units.length.getSI());
// only multiply if inherited
if (dtrans.inheritsTransform()) {
trans.mul(tu);
} else {
trans.set(tu);
}
} else if (data instanceof MayaMesh) {
MayaMesh mm = (MayaMesh) data;
PolygonalMesh mmesh = mm.getMesh();
if (mmesh != null) {
// transform mesh
HashMap<Vertex3d, Vertex3d> vtxMap = new HashMap<Vertex3d, Vertex3d>();
for (Vertex3d vtx : mmesh.getVertices()) {
Vertex3d nvtx = new Vertex3d(vtx.pnt);
// XXX prevent transform
nvtx.pnt.scale(mm.units.length.getSI() / units.length.getSI());
nvtx.pnt.transform(trans);
vtxMap.put(vtx, nvtx);
mesh.addVertex(nvtx);
}
for (Face face : mmesh.getFaces()) {
Vertex3d[] oface = face.getVertices();
Vertex3d[] nface = new Vertex3d[oface.length];
for (int i = 0; i < oface.length; i++) {
nface[i] = vtxMap.get(oface[i]);
}
mesh.addFace(nface);
}
}
}
for (Node<MayaNode> child : root.getChildren()) {
recursiveAddPolygonalMeshes(child, mesh, trans, units);
}
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class AmiraMeshWriter method writeMesh.
public void writeMesh(PolygonalMesh mesh) {
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(myOstream)));
if (!mesh.isTriangular()) {
throw new IllegalArgumentException("Mesh must be triangular");
}
int nVerts = mesh.numVertices();
int nFaces = mesh.numFaces();
pw.println(FILE_HEADER);
// mesh name
if (mesh.getName() != null) {
pw.println("# " + mesh.getName());
}
pw.println();
pw.println("define Nodes " + nVerts);
pw.println("define Triangles " + nFaces);
pw.println();
pw.println("Parameters {");
// pw.println(" ContentType \"HxLineSet\"");
// XXX confirm with amira about valid parameter set
pw.println("}");
pw.println();
pw.println("Nodes { float[3] Coordinates } = @1");
pw.println("Triangles { int[3] Nodes } = @2");
pw.println();
pw.println("@1 # xyz vertex coordinates");
int idx = 0;
for (Vertex3d vtx : mesh.getVertices()) {
vtx.setIndex(idx++);
pw.println(vtx.getPosition().toString(myFmt));
}
pw.println();
pw.println("@2 # triangular face indices");
for (Face face : mesh.getFaces()) {
HalfEdge he = face.firstHalfEdge();
Vertex3d vtx = he.getHead();
pw.print(vtx.getIndex());
he = he.getNext();
vtx = he.getHead();
pw.print(" " + vtx.getIndex());
he = he.getNext();
vtx = he.getHead();
pw.println(" " + vtx.getIndex());
}
// end loop through faces
pw.flush();
}
Aggregations