@@ -306,11 +306,24 @@ void interpolateLinearGrid(NumericVector xseq, NumericVector yseq, NumericMatrix
306306 int ncol=tempmat_sky.ncol ();
307307 int nrow=tempmat_sky.nrow ();
308308
309+ // Precompute y-bracket indices for all output columns (independent of row i)
310+ std::vector<int > top_indices (myynpts, -1 ), bottom_indices (myynpts, -1 );
311+ for (int j = 1 ; j <= myynpts; j++) {
312+ double y = -0.5 +j;
313+ for (int jj = 1 ; jj < ncol; jj++) {
314+ if (myy[jj-1 ] <= y && myy[jj] >= y) {
315+ top_indices[j-1 ] = jj-1 ;
316+ bottom_indices[j-1 ] = jj;
317+ break ;
318+ }
319+ }
320+ }
321+
309322 // For each vertical row
310323 for (int i = 1 ; i <= myxnpts; i++) {
311324 // For a spline to interpolate vertically along the elements of the row
312325 double x = -0.5 +i;
313- // find the left and right index ibnto xseq
326+ // find the left and right index into xseq
314327 int left_index = -1 ;
315328 int right_index = -1 ;
316329 for (int ii = 1 ; ii < nrow; ii++) {
@@ -321,34 +334,28 @@ void interpolateLinearGrid(NumericVector xseq, NumericVector yseq, NumericMatrix
321334 }
322335 }
323336
324- // Rcpp::Rcout << "x="<<x<<" xindex="<<left_index<<" "<<right_index<<"\n";
325- // Rcpp::Rcout << "x="<<x<<" xleft="<<myx[left_index]<<" "<<myx[right_index]<<"\n";
326- int top_index = -1 ;
327- int bottom_index = -1 ;
337+ if (left_index < 0 ) continue ;
338+ const double xlambda = (x-myx[left_index])/(myx[right_index]-myx[left_index]);
339+
328340 for (int j = 1 ; j <= myynpts; j++) {
341+ int top_index = top_indices[j-1 ];
342+ int bottom_index = bottom_indices[j-1 ];
343+ if (top_index < 0 ) continue ;
344+
345+ // p1...p2
346+ // . .
347+ // . .
348+ // p3...p4
349+ double p1 = tempmat_sky (left_index,top_index);
350+ double p2 = tempmat_sky (right_index,top_index);
351+ double p3 = tempmat_sky (left_index,bottom_index);
352+ double p4 = tempmat_sky (right_index,bottom_index);
353+
329354 double y = -0.5 +j;
330- for (int jj = 1 ; jj < ncol; jj++) {
331- if (myy[jj-1 ] <= y && myy[jj] >= y) {
332- top_index = jj-1 ;
333- bottom_index = jj;
334- // p1...p2
335- // . .
336- // . .
337- // p3...p4
338- double p1 = tempmat_sky (left_index,top_index);
339- double p2 = tempmat_sky (right_index,top_index);
340- double p3 = tempmat_sky (left_index,bottom_index);
341- double p4 = tempmat_sky (right_index,bottom_index);
342-
343- double xlambda = (x-myx[left_index])/(myx[right_index]-myx[left_index]);
344- double ylambda = (y-myy[top_index])/(myy[bottom_index]-myy[top_index]);
345- double ztop = p1 * (1.0 -xlambda) + p2 * xlambda;
346- double zbottom = p3 * (1.0 -xlambda) + p4 * xlambda;
347- output (i-1 ,j-1 ) = ztop * (1.0 -ylambda) +zbottom * ylambda;
348- // Rcpp::Rcout << "y="<<y<<" yindex="<<top_index<<" "<<bottom_index<<" "<<p1<<" "<<p2<<" "<<p3<<" "<<p4<<" result="<<output(i-1,j-1)<<"\n";
349- break ;
350- }
351- }
355+ double ylambda = (y-myy[top_index])/(myy[bottom_index]-myy[top_index]);
356+ double ztop = p1 * (1.0 -xlambda) + p2 * xlambda;
357+ double zbottom = p3 * (1.0 -xlambda) + p4 * xlambda;
358+ output (i-1 ,j-1 ) = ztop * (1.0 -ylambda) +zbottom * ylambda;
352359 }
353360 }
354361}
0 commit comments