use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class RenderObjectFactory method createFromMesh.
/**
* Creates a RenderObject from a PolygonalMesh, with one group of triangle
* primitives for faces. Non-triangular faces are replaced by a triangle
* fan.
* @param mesh mesh for which the render object is to be created
* @param flatNormals use "flat" shading normals
* @param addEdges add edge primitives
* @return created RenderObject
*/
public static RenderObject createFromMesh(PolygonalMesh mesh, boolean flatNormals, boolean addEdges) {
RenderObject r = new RenderObject();
int[] nidxs = null;
int[] cidxs = null;
int[] tidxs = null;
// add all appropriate info
for (Vertex3d vtx : mesh.getVertices()) {
Point3d pos = vtx.getPosition();
r.addPosition((float) pos.x, (float) pos.y, (float) pos.z);
}
if (!flatNormals) {
if (mesh.getNormals() != null) {
for (Vector3d nrm : mesh.getNormals()) {
r.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
}
nidxs = mesh.getNormalIndices();
}
} else {
for (Face f : mesh.getFaces()) {
Vector3d nrm = new Vector3d();
f.computeNormal(nrm);
r.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
}
}
if (mesh.getColors() != null) {
for (float[] color : mesh.getColors()) {
r.addColor(color);
}
cidxs = mesh.getColorIndices();
}
if (mesh.getTextureCoords() != null) {
for (Vector3d texCoord : mesh.getTextureCoords()) {
// according to existing MeshRenderer, we need to flip y
r.addTextureCoord((float) texCoord.x, (float) (1 - texCoord.y));
}
tidxs = mesh.getTextureIndices();
}
// keep a map of unique vertices to reduce storage requirements
HashMap<VertexIndexSet, Integer> uniqueVerts = new HashMap<>();
// build faces
int[] indexOffs = mesh.getFeatureIndexOffsets();
List<Face> faces = mesh.getFaces();
for (int i = 0; i < faces.size(); i++) {
Face f = faces.get(i);
int foff = indexOffs[f.idx];
int[] pidxs = f.getVertexIndices();
// vertex indices
int[] vidxs = new int[pidxs.length];
int nidx = f.idx;
for (int j = 0; j < pidxs.length; j++) {
if (!flatNormals) {
nidx = nidxs != null ? nidxs[foff + j] : -1;
}
// only add if unique combination
VertexIndexSet v = new VertexIndexSet(pidxs[j], nidx, cidxs != null ? cidxs[foff + j] : -1, tidxs != null ? tidxs[foff + j] : -1);
Integer vidx = uniqueVerts.get(v);
if (vidx != null) {
vidxs[j] = vidx.intValue();
} else {
vidxs[j] = r.addVertex(v.pidx, v.nidx, v.cidx, v.tidx);
uniqueVerts.put(v, vidxs[j]);
}
}
// triangle fan for faces, line loop for edges
r.addTriangleFan(vidxs);
if (addEdges) {
r.addLineLoop(vidxs);
}
}
// r.commit(); // finalize construction
return r;
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class MultiViewerTesterBase method addHalfBunny.
protected static void addHalfBunny(MultiViewer tester, PolygonalMesh bunny) {
RenderProps rprops = new RenderProps();
rprops.setFaceStyle(FaceStyle.FRONT_AND_BACK);
rprops.setShading(Shading.SMOOTH);
rprops.setBackColor(Color.MAGENTA.darker());
rprops.setSpecular(Color.WHITE);
rprops.setShininess(1000);
if (bunny != null) {
RenderObject r = new RenderObject();
// add all appropriate info
for (Vertex3d vtx : bunny.getVertices()) {
Point3d pos = vtx.getPosition();
r.addPosition((float) pos.x, (float) pos.y, (float) pos.z);
}
for (Vector3d nrm : bunny.getNormals()) {
r.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
}
int[] nidxs = bunny.getNormalIndices();
// left
r.createTriangleGroup();
// right
r.createTriangleGroup();
// build faces
List<Face> faces = bunny.getFaces();
int[] indexOffs = bunny.getFeatureIndexOffsets();
Vector3d centroid = new Vector3d();
for (int i = 0; i < faces.size(); i++) {
Face f = faces.get(i);
int foff = indexOffs[f.idx];
int[] pidxs = f.getVertexIndices();
// vertex indices
int[] vidxs = new int[pidxs.length];
for (int j = 0; j < pidxs.length; j++) {
// only add if unique combination
vidxs[j] = r.addVertex(pidxs[j], nidxs[foff + j], -1, -1);
}
// triangle fan for faces
f.computeCentroid(centroid);
if (centroid.x < centroid.y) {
r.triangleGroup(0);
} else {
r.triangleGroup(1);
}
r.addTriangleFan(vidxs);
}
MultiTriangleGroupWrapper rbunny = new MultiTriangleGroupWrapper(r);
rbunny.setTransform(new RigidTransform3d(new Vector3d(1.3, 1.3, 0.5), AxisAngle.IDENTITY));
rbunny.setRenderProps(rprops);
rbunny.setFaceColors(Color.RED, Color.BLUE);
tester.addRenderable(rbunny);
}
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class ColouredSphereTest method addContent.
@Override
protected void addContent(MultiViewer mv) {
PolygonalMesh mesh = MeshFactory.createOctahedralSphere(1, 4);
HueColorMap map = new HueColorMap();
mesh.setVertexColoringEnabled();
for (int i = 0; i < mesh.numVertices(); ++i) {
// hsv interpolation of colors based on height (-1 to 1)
Vertex3d vtx = mesh.getVertex(i);
double pos = vtx.getPosition().z;
Color c = map.getColor((pos + 1) / 2);
mesh.setColor(i, c);
}
RenderProps rprops = new RenderProps();
rprops.setShading(Shading.SMOOTH);
rprops.setShininess(128);
rprops.setSpecular(Color.WHITE);
mesh.setRenderProps(rprops);
// FixedMeshBody fm = new FixedMeshBody (mesh);
mv.addRenderable(mesh);
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class IntersectionTester method computePenetratingFaceArea.
double computePenetratingFaceArea(ArrayList<PenetratingPoint> points) {
double area = 0;
HashSet<Vertex3d> vertices = new HashSet<Vertex3d>();
for (PenetratingPoint p : points) {
vertices.add(p.vertex);
}
HashSet<Face> faces = new HashSet<Face>();
for (PenetratingPoint p : points) {
Vertex3d vtx = p.vertex;
Iterator<HalfEdge> it = vtx.getIncidentHalfEdges();
while (it.hasNext()) {
HalfEdge he = it.next();
Face face = he.getFace();
if (!faces.contains(face) && faceIsPenetrating(face, vertices)) {
area += face.computeArea();
System.out.println(" adding face " + face.getIndex() + " " + area);
faces.add(face);
}
}
}
return area;
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class PolygonalMeshTest method squareTest.
public void squareTest() {
PolygonalMesh mesh = new PolygonalMesh();
Vertex3d vt0 = new Vertex3d(new Point3d(0, 0, 0));
Vertex3d vt1 = new Vertex3d(new Point3d(1, 0, 0));
Vertex3d vt2 = new Vertex3d(new Point3d(1, 1, 0));
Vertex3d vt3 = new Vertex3d(new Point3d(0, 1, 0));
mesh.addVertex(vt0);
mesh.addVertex(vt1);
mesh.addVertex(vt2);
mesh.addVertex(vt3);
mesh.addFace(new int[] { 0, 1, 3 });
mesh.addFace(new int[] { 1, 2, 3 });
mesh.checkConsistency();
}
Aggregations