/** * Finds the node nearest to the point with specified coordinates. * @param x the x coordinate. * @param y the y coordinate. * @return the nearest node. */ public synchronized Node findNodeNearest(float x, float y) { return findNodeNearest((double)x,(double)y); }
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); }
/** * 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; }
sw.start(); for (int ifind=0; ifind<nfind; ++ifind) nfast[ifind] = tm.findNodeNearest(x[ifind],y[ifind]); sw.stop(); int sfast = (int)(nfind/sw.time());
/** * Computes nearest neighbor distances and values. * @param s1 sampling for coordinate x1. * @param s2 sampling for coordinate x2. * @param d array of distances to nearest known samples. * @param g array of nearest known sample values. */ public void computeDistancesAndValues( Sampling s1, Sampling s2, float[][] d, float[][] g) { int n1 = s1.getCount(); int n2 = s2.getCount(); for (int i2=0; i2<n2; ++i2) { float x2 = (float)s2.getValue(i2); for (int i1=0; i1<n1; ++i1) { float x1 = (float)s1.getValue(i1); TriMesh.Node node = _mesh.findNodeNearest(x1,x2); float d1 = x1-node.x(); float d2 = x2-node.y(); if (g!=null) g[i2][i1] = _f[node.index]; if (d!=null) d[i2][i1] = sqrt(d1*d1+d2*d2); } } }
assertEquals("foo",name); map = tm.getNodePropertyMap(name); n00 = tm.findNodeNearest(0.0f,0.0f); n01 = tm.findNodeNearest(0.0f,1.0f); n10 = tm.findNodeNearest(1.0f,0.0f); n11 = tm.findNodeNearest(1.0f,1.0f); assertEquals(0,((Integer)map.get(n00)).intValue()); assertEquals(1,((Integer)map.get(n01)).intValue());
data.gy = (float)s.get(2,0); } else { // otherwise, just use value of nearest node TriMesh.Node m = _mesh.findNodeNearest(xg,yg); data.f = f(m); data.gx = 0.0f;