/** * Locates a point. */ private PointLocation locatePoint(double x, double y) { // If no tris yet, search the node list for an exact match. // Here, we use unperturbed node coordinates. if (_troot==null) { if (_nroot!=null) { Node node = _nroot; do { if (x==node.x() && y==node.y()) return new PointLocation(node); node = node._next; } while (node!=_nroot); } return new PointLocation(null,false); } // Otherwise, find a good tri in which to begin the recursive search. Node nmin = _nroot; double dmin = distanceSquared(nmin,x,y); for (Node n:_sampledNodes) { double d = distanceSquared(n,x,y); if (d<dmin) { dmin = d; nmin = n; } } Tri tri = nmin._tri; return locatePoint(tri,x,y); }
/** * 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); } } }
/** * 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; }
/** * 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; }
/** * Called when bounds on (x,y) coordinates in mesh may have changed. */ private void updateMinMax() { if (_mesh==null) return; _xmin = Float.MAX_VALUE; _ymin = Float.MAX_VALUE; _xmax = -Float.MAX_VALUE; _ymax = -Float.MAX_VALUE; TriMesh.NodeIterator ni = _mesh.getNodes(); while (ni.hasNext()) { TriMesh.Node node = ni.next(); float x = node.x(); float y = node.y(); if (x<_xmin) _xmin = x; if (y<_ymin) _ymin = y; if (x>_xmax) _xmax = x; if (y>_ymax) _ymax = y; } }
@Override public String toString() { return "("+x()+","+y()+")"; }
private float y(TriMesh.Node node) { if (_orientation==Orientation.XRIGHT_YUP) { return node.y(); } else { return node.x(); } }
private float x(TriMesh.Node node) { if (_orientation==Orientation.XRIGHT_YUP) { return node.x(); } else { return node.y(); } }