use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class PolygonalMeshTest method compareMeshes.
private void compareMeshes(PolygonalMesh mesh, PolygonalMesh comp) {
if (mesh.numVertices() != mesh.numVertices()) {
throw new TestException("mesh has " + mesh.numVertices() + " vertices, expecting " + comp.numVertices());
}
for (int i = 0; i < mesh.numVertices(); i++) {
Vertex3d vr = mesh.getVertices().get(i);
Vertex3d vc = comp.getVertices().get(i);
if (!vr.pnt.epsilonEquals(vc.pnt, 1e-10)) {
throw new TestException("mesh vertex " + i + " is " + vr.pnt + ", expecting " + vc.pnt);
}
}
if (mesh.numFaces() != mesh.numFaces()) {
throw new TestException("mesh has " + mesh.numFaces() + " faces, expecting " + comp.numFaces());
}
for (int i = 0; i < mesh.numFaces(); i++) {
Face fr = mesh.getFaces().get(i);
Face fc = comp.getFaces().get(i);
int[] ridxs = fr.getVertexIndices();
int[] cidxs = fc.getVertexIndices();
if (!compareIdxs(ridxs, cidxs)) {
throw new TestException("mesh face " + i + " has indices " + idxsToStr(ridxs) + ", expecting " + idxsToStr(cidxs));
}
}
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class MayaAsciiReader method recursiveAddPolylines.
private void recursiveAddPolylines(Node<MayaNode> root, PolylineMesh mesh, AffineTransform3d trans, UnitInfo units, Pattern pregex) {
// 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());
if (dtrans.inheritsTransform()) {
trans.mul(tu);
} else {
trans.set(tu);
}
} else if (data instanceof MayaNurbsCurve) {
MayaNurbsCurve mnc = (MayaNurbsCurve) data;
if (pregex == null || pregex.matcher(mnc.getName()).matches()) {
Polyline line = new Polyline(mnc.curve);
if (line != null) {
// transform line
for (Vertex3d vtx : line.getVertices()) {
vtx.pnt.scale(mnc.units.length.getSI() / units.length.getSI());
vtx.pnt.transform(trans);
}
mesh.addLine(line);
}
}
}
for (Node<MayaNode> child : root.getChildren()) {
recursiveAddPolylines(child, mesh, trans, units, pregex);
}
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class OffWriter method writeMesh.
public void writeMesh(PolygonalMesh mesh) throws IOException {
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(myOstream)));
pw.println("OFF");
pw.printf("%d %d %d\n", mesh.numVertices(), mesh.numFaces(), 0);
pw.flush();
int[] oldIdxs = new int[mesh.numVertices()];
int idx = 0;
for (Vertex3d vtx : mesh.getVertices()) {
Point3d pnt = vtx.getPosition();
pw.println(myFmt.format(pnt.x) + " " + myFmt.format(pnt.y) + " " + myFmt.format(pnt.z));
// protect vertex indices numbers
oldIdxs[idx] = vtx.getIndex();
vtx.setIndex(idx);
idx++;
}
for (Face face : mesh.getFaces()) {
int nf = face.numVertices();
pw.print(nf);
for (int j = 0; j < face.numVertices(); j++) {
pw.print(" " + face.getVertex(j).getIndex());
}
pw.println();
}
pw.flush();
// restore vertex indices
idx = 0;
for (Vertex3d vtx : mesh.getVertices()) {
vtx.setIndex(oldIdxs[idx]);
idx++;
}
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class AmiraMeshWriter method writeMesh.
public void writeMesh(PolylineMesh mesh) {
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(myOstream)));
int nLineSize = 0;
for (Polyline line : mesh.getLines()) {
int nlineVerts = line.numVertices();
if (nlineVerts > 0) {
nLineSize += nlineVerts + 1;
}
}
int nVerts = mesh.numVertices();
pw.println(FILE_HEADER);
// mesh name
if (mesh.getName() != null) {
pw.println("# " + mesh.getName());
}
pw.println();
pw.println("define Lines " + nLineSize);
pw.println("define Vertices " + nVerts);
pw.println();
pw.println("Parameters {");
pw.println(" ContentType \"HxLineSet\"");
pw.println("}");
pw.println();
pw.println("Vertices { float[3] Coordinates } = @1");
pw.println("Lines { int LineIdx } = @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 # line indices, terminating with -1");
for (Polyline line : mesh.getLines()) {
idx = 0;
Vertex3d[] vtxs = line.getVertices();
if (vtxs.length > 0) {
pw.print(vtxs[0].getIndex());
for (int i = 1; i < vtxs.length; i++) {
if ((i % maxLineIndexSize) == 0) {
pw.println();
} else {
pw.print(" ");
}
pw.print(vtxs[i].getIndex());
}
if ((vtxs.length % maxLineIndexSize) == 0) {
pw.println();
} else {
pw.print(" ");
}
pw.println(-1);
}
// end if non-null line
}
// end loop through lines
pw.flush();
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class GtsReader method readMesh.
public PolygonalMesh readMesh(PolygonalMesh mesh) throws IOException {
if (mesh == null) {
mesh = new PolygonalMesh();
} else {
mesh.clear();
}
ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(new InputStreamReader(myIstream)));
rtok.commentChar('!');
rtok.commentChar('#');
rtok.eolIsSignificant(true);
int numVerts = rtok.scanInteger();
int numEdges = rtok.scanInteger();
int numFaces = rtok.scanInteger();
while (rtok.nextToken() != ReaderTokenizer.TT_EOL) ;
ArrayList<Edge> edgeList = new ArrayList<Edge>();
for (int i = 0; i < numVerts; i++) {
double x = rtok.scanNumber();
double y = rtok.scanNumber();
double z = rtok.scanNumber();
mesh.addVertex(new Point3d(x, y, z));
while (rtok.nextToken() != ReaderTokenizer.TT_EOL) ;
}
ArrayList<Vertex3d> verts = mesh.getVertices();
for (int i = 0; i < numEdges; i++) {
int idx1 = rtok.scanInteger() - 1;
int idx2 = rtok.scanInteger() - 1;
if (idx1 < 0 || idx1 >= numVerts) {
System.out.println("Error: vertex " + idx1 + " out of range, " + rtok);
System.exit(1);
}
if (idx2 < 0 || idx2 >= numVerts) {
System.out.println("Error: vertex " + idx2 + " out of range, " + rtok);
System.exit(1);
}
edgeList.add(new Edge(verts.get(idx1), verts.get(idx2)));
while (rtok.nextToken() != ReaderTokenizer.TT_EOL) ;
}
for (int i = 0; i < numFaces; i++) {
ArrayList<Edge> edges = new ArrayList<Edge>();
while (rtok.nextToken() == ReaderTokenizer.TT_NUMBER) {
if (!rtok.tokenIsInteger()) {
System.out.println("Error: edge index expected, " + rtok);
System.exit(1);
}
int idx = (int) rtok.lval - 1;
if (idx < 0 || idx >= edgeList.size()) {
System.out.println("Error: edge index " + idx + " out of range, " + rtok);
System.exit(1);
}
edges.add(edgeList.get(idx));
}
int[] idxs = getFaceIndices(edges);
if (idxs == null) {
System.out.println("Error: face edges are not adjacent, " + rtok);
}
mesh.addFace(idxs);
rtok.pushBack();
while (rtok.nextToken() != ReaderTokenizer.TT_EOL) ;
}
return mesh;
}
Aggregations