/************************** END RECT *******************************/ #endregion #region parse_POLYGON /* ******************************************** POLY ***/ private bool parse_polygon() { Region region = _targetregion; Polygon poly = null; int npoints = 0; double ra, dec, x, y, z; if (region == null) return true; advance(); // Skip over 'RECT' _format = peekFormat(); if (ismore && _format != Format.Null) { advance(); } while (ismore) { if (npoints >= this._current_capacity) { _error = Error.errPolyToomanyPoints; return isok; } switch (_format) { case Format.Null: case Format.Cartesian: try { x = this.getdouble(); y = this.getdouble(); z = this.getdouble(); } catch { if (_error != Error.errEol) _error = Error.errIllegalNumber; return isok; } xs[npoints] = x; ys[npoints] = y; zs[npoints] = z; npoints++; break; case Format.J2000: case Format.Latlon: try { ra = this.getdouble(); dec = this.getdouble(); } catch { if (_error != Error.errEol) _error = Error.errIllegalNumber; return isok; } if (_format == Format.Latlon) { SpatialVector.radec2cartesian(dec, ra, out x, out y, out z); } else { SpatialVector.radec2cartesian(ra, dec, out x, out y, out z); } xs[npoints] = x; ys[npoints] = y; zs[npoints] = z; npoints++; break; default: break; } } if (npoints < 3) { _error = Error.errNot2forRect; return isok; } poly = new Polygon(region); // WARNING!!! more than one kinf of error. The other one is for // degenerate edges, two points are too close... or you can eliminate them! Polygon.Error err = poly.add(xs, ys, zs, npoints); if (err == Polygon.Error.errBowtieOrConcave) { _error = Error.errPolyBowtie; return isok; } else if (err == Polygon.Error.errZeroLength){ _error = Error.errPolyZeroLength; return isok; } return true; }
public bool addConvex(double[] x, double[] y, double[] z, int len){ // // All the input points are in x,y,z... // To make a convex hull, follow these steps: // 1. add all the points to the internal array // 2. compute the centroid // 3. make the rotation matrix, that rotates the centroid to (x,y,0) // 4. rotate the points (call xform, returns error if more than hemisphere) // 5. sort (by x,y) // 6. compute the convex hull, result is a polygon // 7. Call the polygoner to make the convex // todo: encapsulate Polygon into Chull, or use inheritance? // if (_reg == null) return true; CPoint centroid = new CPoint(); for(int i=0; i<len; i++){ addPoint(x[i], y[i], z[i]); } this.nPoints = len; this.getCentroid( centroid); this.makeRotator(centroid); if (this.xform()){ this.sort(); nrKept = this.chainHull_2D(); // output has it all // allmost like Polygon, except first point is included at end // We can now make a polygon out of it. Luckily it takes // care of the winding error // // First, move over the x,y,z points to double arrays; // double[] xa, ya, za; CPoint p; xa = new double[nrKept]; ya = new double[nrKept]; za = new double[nrKept]; for(int i=0; i<nrKept; i++){ p = (CPoint) output[i]; xa[i] = p.x_in; ya[i] = p.y_in; za[i] = p.z_in; } Polygon poly = new Polygon(this._reg); poly.add(xa, ya, za, nrKept-1); // because first connects to last } else { // // Error, points were more than a hemisphere // return false; } return true; }