private IntersectionGrid<SiteWithPolynomial> siteGrid; //TODO: rename IntersectionGrid to something more generic
@Override
public void setKnownSites(Collection<VectorXYZ> siteVectors) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
sites = new ArrayList<SiteWithPolynomial>(siteVectors.size());
siteGrid = new IntersectionGrid<SiteWithPolynomial>(
new AxisAlignedBoundingBoxXZ(siteVectors).pad(CELL_SIZE/2),
CELL_SIZE, CELL_SIZE);
for (VectorXYZ siteVector : siteVectors) {
SiteWithPolynomial s = new SiteWithPolynomial(siteVector);
sites.add(s);
siteGrid.insert(s);
}
System.out.println(" time grid: " + stopWatch);
stopWatch.reset();
stopWatch.start();
/* approximate a polynomial at each site */
Map<SiteWithPolynomial, List<SiteWithPolynomial>> nearestSiteMap
= new HashMap<SiteWithPolynomial, List<SiteWithPolynomial>>();
for (SiteWithPolynomial site : sites) {
List<SiteWithPolynomial> nearestSites =
findNearestSites(site.pos.xz(), SITES_FOR_APPROX, false);
nearestSiteMap.put(site, nearestSites);
}
System.out.println(" time nearest sites: " + stopWatch);
stopWatch.reset();
stopWatch.start();
calculatePolynomials:
for (SiteWithPolynomial site : sites) {
List<SiteWithPolynomial> nearestSites =
nearestSiteMap.get(site);
RealVector vector = new ArrayRealVector(SITES_FOR_APPROX);
RealMatrix matrix = new Array2DRowRealMatrix(
SITES_FOR_APPROX, DefaultPolynomial.NUM_COEFFS);
for (int row = 0; row < SITES_FOR_APPROX; row++) {
SiteWithPolynomial nearSite = nearestSites.get(row);
DefaultPolynomial.populateMatrix(matrix, row, nearSite.pos.x, nearSite.pos.z);
vector.setEntry(row, nearSite.pos.y);
}
QRDecomposition qr = new QRDecomposition(matrix);
RealVector solution = qr.getSolver().solve(vector);
double[] coeffs = solution.toArray();
for (double coeff : coeffs) {
if (coeff > 10e3) {
continue calculatePolynomials;
}
}
site.setPolynomial(new DefaultPolynomial(coeffs));
}
System.out.println(" time polyonmials: " + stopWatch);
stopWatch.reset();
stopWatch.start();
}