use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class FaceComponent method drawFaces.
void drawFaces(Renderer renderer, Shading shading, RenderProps props) {
boolean useVertexColors = useVertexColouring;
if (renderer.isSelecting()) {
useVertexColors = false;
}
Face face = myFace;
boolean useVertexNormals = false;
ArrayList<Vector3d> normals = null;
int[] normalIndices = null;
int normalFaceOff = -1;
if ((shading == Shading.SMOOTH || shading == Shading.METAL) && myMesh.hasNormals()) {
useVertexNormals = true;
normals = myMesh.getNormals();
normalIndices = myMesh.getNormalIndices();
normalFaceOff = myMesh.getFeatureIndexOffsets()[face.getIndex()];
}
Vector3d faceNrm = face.getNormal();
ArrayList<float[]> colors = null;
int[] colorIndices = null;
int colorFaceOff = -1;
if (useVertexColouring) {
colors = myMesh.getColors();
colorIndices = myMesh.getColorIndices();
colorFaceOff = myMesh.getFeatureIndexOffsets()[face.getIndex()];
}
renderer.beginDraw(DrawMode.TRIANGLE_FAN);
HalfEdge he = face.firstHalfEdge();
int k = 0;
do {
Vertex3d vtx = he.head;
Point3d pnt = vtx.myRenderPnt;
if (useVertexColors) {
int cidx = colorIndices[colorFaceOff + k];
if (cidx != -1) {
float[] color = colors.get(cidx);
renderer.setColor(color);
}
}
if (useVertexNormals) {
int nidx = normalIndices[normalFaceOff + k];
if (nidx != -1) {
Vector3d normal = normals.get(nidx);
renderer.setNormal(normal);
}
} else {
renderer.setNormal(faceNrm);
}
renderer.addVertex(pnt.x, pnt.y, pnt.z);
he = he.getNext();
k++;
} while (he != face.firstHalfEdge());
renderer.endDraw();
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class ColoredSphereTest method build.
@Override
public void build(String[] args) throws IOException {
super.build(args);
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);
addRenderable(fm);
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class IntersectionTester method findPenetratingFaces.
private Face[] findPenetratingFaces(ArrayList<PenetratingPoint> points) {
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)) {
faces.add(face);
}
}
}
return faces.toArray(new Face[0]);
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class IntersectionTester method prerender.
@Override
public void prerender(RenderList list) {
super.prerender(list);
if (myZPerturb0 != null) {
for (Vertex3d v : myMesh0.getVertices()) {
if (isInterior(v)) {
v.pnt.z = RandomGenerator.nextDouble(myZPerturb0.getLowerBound(), myZPerturb0.getUpperBound());
}
}
myMesh0.notifyVertexPositionsModified();
myMesh0.updateFaceNormals();
}
if (myZPerturb1 != null) {
for (Vertex3d v : myMesh1.getVertices()) {
if (isInterior(v)) {
v.pnt.z = RandomGenerator.nextDouble(myZPerturb1.getLowerBound(), myZPerturb1.getUpperBound());
}
}
myMesh1.notifyVertexPositionsModified();
myMesh1.updateFaceNormals();
}
PolygonalMesh csgMesh = null;
FunctionTimer timer = new FunctionTimer();
if (myContoursOnly) {
timer.start();
System.out.println("finding contours");
myContours = myIntersector.findContours(myMesh0, myMesh1);
System.out.println("num contours=" + myContours.size());
timer.stop();
if (myContours.size() == 0) {
return;
}
} else {
timer.start();
if (myCSGOperation == SurfaceMeshIntersector.CSG.INTERSECTION) {
myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.INSIDE, myMesh1, RegionType.INSIDE);
csgMesh = myIntersector.createCSGMesh(myContactInfo);
} else if (myCSGOperation == SurfaceMeshIntersector.CSG.UNION) {
myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.OUTSIDE, myMesh1, RegionType.OUTSIDE);
csgMesh = myIntersector.createCSGMesh(myContactInfo);
} else if (myCSGOperation == SurfaceMeshIntersector.CSG.DIFFERENCE01) {
myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.OUTSIDE, myMesh1, RegionType.INSIDE);
csgMesh = myIntersector.createCSGMesh(myContactInfo);
} else if (myCSGOperation == SurfaceMeshIntersector.CSG.DIFFERENCE10) {
myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.INSIDE, myMesh1, RegionType.OUTSIDE);
csgMesh = myIntersector.createCSGMesh(myContactInfo);
} else {
myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.INSIDE, myMesh1, RegionType.INSIDE);
}
timer.stop();
if (myContactInfo == null) {
myContours = null;
return;
}
myContours = myContactInfo.getContours();
ArrayList<PenetratingPoint> points0 = myContactInfo.getPenetratingPoints(0);
// if (points0 != null) {
// System.out.println ("num verts0= " + points0.size());
// }
// else {
// System.out.println ("num verts0= null");
// }
ArrayList<PenetratingPoint> points1 = myContactInfo.getPenetratingPoints(1);
// if (points1 != null) {
// System.out.println ("num verts1= " + points1.size());
// }
// else {
// System.out.println ("num verts1= null");
// }
ArrayList<PenetrationRegion> regions;
if (false) {
System.out.println("Regions 0:");
regions = myContactInfo.getRegions(0);
for (int i = 0; i < regions.size(); i++) {
PenetrationRegion r = regions.get(i);
int[] faceIdxs = SurfaceMeshIntersectorTest.getFaceIndices(r.getFaces());
ArraySort.sort(faceIdxs);
int[] vtxIdxs = SurfaceMeshIntersectorTest.getVertexIndices(r);
ArraySort.sort(vtxIdxs);
System.out.println(" " + i + " area=" + r.getArea() + " faces=[ " + ArraySupport.toString(faceIdxs) + " vtxs=[ " + ArraySupport.toString(vtxIdxs) + "]");
}
System.out.println("Regions 1:");
regions = myContactInfo.getRegions(1);
for (int i = 0; i < regions.size(); i++) {
PenetrationRegion r = regions.get(i);
int[] faceIdxs = SurfaceMeshIntersectorTest.getFaceIndices(r.getFaces());
ArraySort.sort(faceIdxs);
int[] vtxIdxs = SurfaceMeshIntersectorTest.getVertexIndices(r);
ArraySort.sort(vtxIdxs);
System.out.println(" " + i + " area=" + r.getArea() + " faces=[ " + ArraySupport.toString(faceIdxs) + " vtxs=[ " + ArraySupport.toString(vtxIdxs) + "]");
}
System.out.println("contours:");
RigidTransform3d T = new RigidTransform3d();
ArrayList<IntersectionContour> contours = myContactInfo.getContours();
for (int k = 0; k < contours.size(); k++) {
IntersectionContour c = contours.get(k);
c.printCornerPoints("contour " + k + " closed=" + c.isClosed(), "%20.16f", T);
}
}
if (false) {
String pfx = " ";
System.out.print(pfx + "new int[] {");
int k = 0;
Collections.sort(points0, new CPPVertexComparator());
for (PenetratingPoint cpp : points0) {
System.out.print((k++ > 0 ? ", " : " ") + cpp.vertex.getIndex());
}
System.out.println(" },");
System.out.print(pfx + "new int[] {");
k = 0;
Collections.sort(points1, new CPPVertexComparator());
for (PenetratingPoint cpp : points1) {
System.out.print((k++ > 0 ? ", " : " ") + cpp.vertex.getIndex());
}
System.out.println(" },");
}
if (myRenderCSGMesh && csgMesh != null) {
csgMesh.prerender(myRenderProps);
myCSGMesh = csgMesh;
} else {
myCSGMesh = null;
}
}
// System.out.println ("time=" + timer.result(1));
}
use of maspack.geometry.Vertex3d in project artisynth_core by artisynth.
the class RenderObjectExamples method example2.
/**
* Example showing a simplified mesh renderer
*/
public static void example2(Renderer renderer, PolygonalMesh mesh, boolean drawEdges, boolean drawVertices) {
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 (mesh.getNormals() != null) {
for (Vector3d nrm : mesh.getNormals()) {
r.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
}
nidxs = mesh.getNormalIndices();
}
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();
}
// 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];
for (int j = 0; j < pidxs.length; j++) {
vidxs[j] = r.addVertex(pidxs[j], nidxs != null ? nidxs[foff + j] : -1, cidxs != null ? cidxs[foff + j] : -1, tidxs != null ? tidxs[foff + j] : -1);
}
// triangle fan for faces, line loop for edges
r.addTriangleFan(vidxs);
if (drawEdges) {
r.addLineLoop(vidxs);
}
}
// draw mesh
// <set face color and maybe polygon offset>
// draw faces
renderer.drawTriangles(r);
if (drawEdges) {
// <set edge color>
// draw edges
renderer.drawLines(r);
}
if (drawVertices) {
// <set vertex color>
// Rather than set up a set of point primitives, which would result
// in an extra index array in corresponding VBOs, draw the vertex
// array directly with an appropriate mode.
// draw all vertices as points
renderer.drawVertices(r, DrawMode.POINTS);
// less-efficient alternative:
// for (int i=0; i<r.numVertices(); i++) {
// r.addPoint(i);
// }
// renderer.drawPoints(r);
}
}
Aggregations