public static void demoSquare() { TriMesh tm = new TriMesh(); TriMesh.Node n0 = new TriMesh.Node(0.0f,0.0f); TriMesh.Node n1 = new TriMesh.Node(1.0f,0.0f); TriMesh.Node n2 = new TriMesh.Node(0.0f,1.0f); TriMesh.Node n3 = new TriMesh.Node(1.0f,1.0f); tm.addNode(n0); tm.addNode(n1); tm.addNode(n2); tm.addNode(n3); plot(tm); }
private void addGhostNodes(float[] x, float[] y) { int ng = x.length; for (int ig=0; ig<ng; ++ig) { float xg = x[ig]; float yg = y[ig]; TriMesh.PointLocation pl = _mesh.locatePoint(xg,yg); if (pl.isOutside()) { TriMesh.Node n = new TriMesh.Node(xg,yg); n.data = new NodeData(); n.index = -1-ig; // ghost nodes have negative indices _mesh.addNode(n); } } }
public void setScattered(float[] f, float[] x1, float[] x2) { _f = copy(f); _mesh = new TriMesh(); int n = f.length; for (int i=0; i<n; ++i) { TriMesh.Node node = new TriMesh.Node(x1[i],x2[i]); node.index = i; boolean added = _mesh.addNode(node); Check.argument(added,"samples have unique coordinates (x1,x2)"); } }
public void testSimple() { TriMesh tm = new TriMesh(); TriMesh.Node n0 = new TriMesh.Node(0.0f,0.0f); TriMesh.Node n1 = new TriMesh.Node(1.0f,0.0f); TriMesh.Node n2 = new TriMesh.Node(0.0f,1.0f); TriMesh.Node n3 = new TriMesh.Node(0.3f,0.3f); tm.addNode(n0); tm.addNode(n1); tm.addNode(n2); tm.addNode(n3); tm.removeNode(n3); tm.validate(); }
/** * Interpolates at specified sample points without using those samples. * This method implements a form of cross-validation. Differences * between the values of the specified samples and the returned * interpolated values are measures of errors for those samples. * <p> * If bounds have not been set explicitly, then this method will return * null values if the validated sample is on the convex hull of samples. * <p> * This method does not recompute gradients that may have been estimated * using the samples to be validated. Therefore, validation should be * performed without using gradients. * @param i array of indices of samples to validate. * @return array of values interpolated at validated sample points. */ public float[] validate(int[] i) { int nv = i.length; for (int iv=0; iv<nv; ++iv) _mesh.removeNode(_nodes[i[iv]]); float[] fv = new float[nv]; for (int iv=0; iv<nv; ++iv) { TriMesh.Node node = _nodes[i[iv]]; float xn = node.x(); float yn = node.y(); fv[iv] = interpolate(xn,yn); } for (int iv=0; iv<nv; ++iv) _mesh.addNode(_nodes[i[iv]]); return fv; }
public void testSquare() { TriMesh tm = new TriMesh(); TriMesh.Node n0 = new TriMesh.Node(0.0f,0.0f); TriMesh.Node n1 = new TriMesh.Node(1.0f,0.0f); TriMesh.Node n2 = new TriMesh.Node(0.0f,1.0f); TriMesh.Node n3 = new TriMesh.Node(1.0f,1.0f); tm.addNode(n0); tm.addNode(n1); tm.addNode(n2); tm.addNode(n3); tm.removeNode(n3); tm.removeNode(n2); tm.removeNode(n1); tm.removeNode(n0); tm.validate(); }
/** * Moves a node in the mesh to the specified (x,y) coordinates. * Roughly equivalent to (but potentially more efficient than) * first removing and then adding the node to the mesh at the * specified coordinates. However, if the node is not in the mesh, * then it will be moved, but not added to the mesh. Also, if the * specified coordinates are already occupied by another node in * the mesh, then the specified node is not moved. * @param node a node in the mesh. * @param x the x coordinate of the moved node. * @param y the y coordinate of the moved node. * @return true, if the node was moved; false, otherwise. */ public synchronized boolean moveNode(Node node, float x, float y) { // TODO: optimize for small movements that require no retriangulation. if (x!=node.x() || y!=node.y()) { Node nodeNearest = findNodeNearest(x,y); if (node==nodeNearest || x!=nodeNearest.x() || y!=nodeNearest.y()) { boolean nodeInMesh = removeNode(node); node.setPosition(x,y); if (nodeInMesh) { boolean addedNode = addNode(node); assert addedNode; } return true; } } return false; }
public void benchAddNode() { java.util.Random random = new java.util.Random(); for (int itest=0; itest<3; ++itest) { for (int nnode=1000; nnode<=64000; nnode*=2) { Stopwatch sw = new Stopwatch(); sw.restart(); TriMesh tm = new TriMesh(); for (int inode=0; inode<nnode; ++inode) { float x = random.nextFloat(); float y = random.nextFloat(); TriMesh.Node node = new TriMesh.Node(x,y); tm.addNode(node); } sw.stop(); System.out.println( "Added "+nnode+" nodes to make "+tm.countTris() + " tris in "+sw.time()+" seconds."); tm.validate(); } try { System.out.println("Sleeping"); Thread.sleep(5000,0); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
public void testTriListener() { TriMesh tm = new TriMesh(); tm.addNode(new TriMesh.Node(0.0f,0.0f)); tm.addNode(new TriMesh.Node(1.0f,0.0f)); tm.addNode(new TriMesh.Node(0.0f,1.0f)); TriListener tl = new TriListener(); tm.addTriListener(tl); TriMesh.Node node = new TriMesh.Node(0.1f,0.1f); tm.addNode(node); assertEquals(3,tl.countAdded()); assertEquals(1,tl.countRemoved()); tm.removeNode(node); assertEquals(4,tl.countAdded()); assertEquals(4,tl.countRemoved()); } private static class TriListener implements TriMesh.TriListener {
_mesh.removeNode(n); double asum = computeAreas((float)xn,(float)yn); _mesh.addNode(n); if (asum>0.0) { int nm = _nodeList.nnode();
boolean added = _mesh.addNode(node); Check.argument(added,"each sample has unique coordinates"); NodeData data = new NodeData();
public void testAddFindRemove() { java.util.Random random = new java.util.Random(); TriMesh tm = new TriMesh(); //int nadd = 0; //int nremove = 0; for (int niter=0; niter<1000; ++niter) { float x = random.nextFloat(); float y = random.nextFloat(); if (tm.countNodes()<10 || random.nextFloat()>0.5f) { TriMesh.Node node = new TriMesh.Node(x,y); boolean ok = tm.addNode(node); assertTrue(ok); tm.validate(); //++nadd; } else if (tm.countNodes()>0) { TriMesh.Node node = tm.findNodeNearest(x,y); assertTrue(node!=null); TriMesh.Node nodeSlow = tm.findNodeNearestSlow(x,y); assertTrue(node==nodeSlow); tm.removeNode(node); tm.validate(); //++nremove; } } //System.out.println("Nodes added/removed = "+nadd+"/"+nremove); }
float y = random.nextFloat(); TriMesh.Node node = new TriMesh.Node(x,y); tm.addNode(node);
TriMesh.Node n = new TriMesh.Node(xg,yg); n.index = -1-ig; // ghost nodes have negative indices _mesh.addNode(n); NodeData data = new NodeData(); n.data = data;
public void testNabors() { TriMesh tm = new TriMesh(); TriMesh.Node n0 = new TriMesh.Node(1.0f,0.0f); TriMesh.Node n1 = new TriMesh.Node(0.0f,1.0f); TriMesh.Node n2 = new TriMesh.Node(0.0f,0.0f); TriMesh.Node n3 = new TriMesh.Node(1.1f,1.1f); tm.addNode(n0); tm.addNode(n1); tm.addNode(n2); tm.addNode(n3); assertEquals(2,tm.getTriNabors(n0).length); assertEquals(2,tm.getTriNabors(n1).length); assertEquals(1,tm.getTriNabors(n2).length); assertEquals(1,tm.getTriNabors(n3).length); assertEquals(2,tm.getTriNabors(tm.findEdge(n0,n1)).length); assertEquals(1,tm.getTriNabors(tm.findEdge(n0,n2)).length); assertEquals(1,tm.getTriNabors(tm.findEdge(n1,n2)).length); assertEquals(1,tm.getTriNabors(tm.findEdge(n0,n3)).length); assertEquals(1,tm.getTriNabors(tm.findEdge(n1,n3)).length); assertEquals(3,tm.getEdgeNabors(n0).length); assertEquals(3,tm.getEdgeNabors(n1).length); assertEquals(2,tm.getEdgeNabors(n2).length); assertEquals(2,tm.getEdgeNabors(n3).length); assertEquals(3,tm.getNodeNabors(n0).length); assertEquals(3,tm.getNodeNabors(n1).length); assertEquals(2,tm.getNodeNabors(n2).length); assertEquals(2,tm.getNodeNabors(n3).length); }