Tutorial Solutions
3D Clipping

  1. The situation is best visualized by drawing the projection of the line and view volume on the yz-plane.

    The view-volume plane equations are:

      left:   plane(P) =  x + left*z/near    plane(P1) =  0  plane(P2) = 4
      right:  plane(P) = -x - right*z/near   plane(P1) =  0  plane(P2) = 2
      top:    plane(P) = -y - top*z/near     plane(P1) = -2  plane(P2) = 3
      bottom: plane(P) =  y + bottom*z/near  plane(P1) =  2  plane(P2) = 3
      front:  plane(P) = -z - near           plane(P1) = -1  plane(P2) = 2
      back:   plane(P) =  z + far            plane(P1) =  4  plane(P2) = 1
    
    We can first check for a trivial accept or reject by using the outcodes for P1 and P2. The outcode bits can be produced by simply checking the sign of plane(P). Let's generates the outcode bits in the same order as the plane equations are listed above. We'll assume that a point on a bounding plane counts as being 'inside' the view volume. This produces
    out(P1) = 001010 
    out(P2) = 000000
    
    trivial accept test: out(P1) OR out(P2) = 001010 --> fails
    trivial reject test: out(P1) AND out(P2) = 000000 --> fails
    
    We can choose to clip against either the front or top plane first. Let's be smart and choose to clip against the top plane first.

    The plane equation is:
    -y - z = 0
    t = -plane(P1) / ( plane(P2) - plane(P1) ) = 2/5
    P(t) = P1 + t (P2 - P1) = (0,2,0) + 0.4 (1,-2,-3) = (0.4,1.2,-1.2)


  2. First of all, let's find the parametric equation of the projected line by projecting the points and then constructing the line. The parametric equation for P0'P1' can thus be determined as follows:
      x0' = x0*d/z0
      y0' = y0*d/z0
      z0' = d
    
      x1' = x1*d/z1
      y1' = y1*d/z1
      z1' = d
    
      P'(u) = (1-u) P0' + u P1',  u=[0,1]
    
    or equivalently
    
      x'(u) = (1-u) x0' + u x1',  u=[0,1]
      y'(u) = (1-u) y0' + u y1',  u=[0,1]
      z'(u) = d
    
    Now let's write the equation for the line P0 P1 and then project it.
      x'(t) = x(t) d / z(t)
      y'(t) = y(t) d / z(t)
      z'(t) = d
    
    Expanding this for x(t) gives
      x'(t) = d[(1-t) x0 + t x1] / [(1-t)z0 + t z1]
    
    We can express x0 and x1 in terms of x0' and x1', giving:
      x'(t) = d[(1-t) x0'z0/d + t x1'z1/d] / [(1-t)z0 + t z1]
    
    Simplifying, and writing the two terms in the numerator separately gives:
      x'(t) = (1-t) x0'z0 / [(1-t)z0 + t z1] + 
                  t x1'z1 / [(1-t)z0 + t z1]
    
    This equation can be rewritten as
      x'(a) = (1-a) x0' + a x1'
    
    where
      a = t z1 / [(1-t) z0 + t z1]
    
    Note that for t=0, a=0, and for t=1, a=1. Thus, (P0 P1)' reduces to a form equivalent to P0'P1', although the parametric parameters travel at different `speeds'.