title: recalculate ray tracing/casting costs when changing size of rectangle
body: <p>I have an array of "rays" which I need to measure the costs in relation to the rectangular boxes below. The outer red box is always 1m larger than the dark green box and light green box is 10cm smaller than the dark green box. If a ray </p>
  
  <ol>
  <li>passes through the dark green box I would assign cost c </li>
  <li>ands on the dark green box I would assign cost d</li>
  <li>lands on the red area i would assign
  cost e</li>
  <li>does not intersect the dark green box and not land in red box, cost f</li>
  <li>and <code>d &lt; f &lt; c &lt; e</code></li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/AYc9j.png" rel="noreferrer"><img src="https://i.stack.imgur.com/AYc9j.png" alt="enter image description here"></a></p>
  
  <p>I currently have the following data structures and functions to calculate the cost. I am required to calculate the cost for the given rectangles (represented by 4 xy coordinates), but at the same time, find the <strong>approximate/local</strong> optimal length/width of the dark green rectangle(i.e shrink or grow the dimension by holding the closest corner of the rectangle fixed) such that the cost is minimum.</p>
  
  <p>A concrete example is the screenshot below. The smaller rectangle corresponds to the dark green box in the figure. Green lines are the rays with cost d, yellow lines cost f and the turquoise lines are the ones with cost c. If I fix the top left hand corner of the inner rectangle and reduce the width, I can reduce the turqoise rays from cost c to f.<br>
  <a href="https://i.stack.imgur.com/wfH2D.png" rel="noreferrer"><img src="https://i.stack.imgur.com/wfH2D.png" alt="enter image description here"></a></p>
  
  <p>My question is, I am stuck at how should I alter my code or change my data structure, such that I can find the best dimensions by only recomputing the affected rays (i.e without looping through all the rays again).</p>
  
  <pre><code>struct VRay{
      float range, x, y;
      enum RayType{ PASSTHROUGH, FREE, SURFACE, OCCLUDED, UNIFORM};
      RayType r;
  };
  struct VScan{
      VRay rays[401];
      int closestIdx;
      int firstIdx;
      int lastIdx;
  } vscan;
  </code></pre>
  
  <p>The function to calculate the costs:</p>
  
  <pre><code>for (int i = 0; i &lt; 401; i++){
         VRay&amp; r = vscan.rays[i];
  
         Vector2f cray(r.y, -r.x);
         bool ppBound = false;
         bool ppSurf = false;
         Vector2f vertex =  outBox.row(0);
         Vector2f vertexSurf = surface.row(0);
  
         float init = cray.dot(vertex);
         float initSurf = cray.dot(vertexSurf);
         //this part finds whether ray intersects rectangle or not 
         for (int j = 1; j &lt; 4; j++){
              Vector2f v2 = outBox.row(j);
              Vector2f vSurf =  surface.row(j);
  
              float i2 = cray.dot(v2);
              float iSurf = cray.dot(vSurf);
  
              if (i2 * init &lt; 0){
                  ppBound =  true;
              }
  
              if (iSurf * initSurf &lt; 0){
                  ppSurf = true;
              }
         }
  
         //ray does not intersect all rectangles
         if (!ppBound){
            z += log(1/100.);
            continue;
         }
  
          //ray is inside red box
          if (inPolygon(outBox, r)){
              //ray inside dark green box 
              if (inPolygon(surface, r)){
                  //ray inside light green box
                  if (inPolygon(inBox,r))
                      c  = passTCost;
                  else
                      c = surfaceCost;
              }
              else{
                  c = freeCost; //free space
              }
          }
          else if (ppSurf){
              c = passTCost; //before all boxes
          }
          else { //ray does not intersect dark green box
              z += log(1/100.);
              continue;
          }
  
          z += -(c * c)/(2 * deviation * deviation);
      }
  </code></pre>