[comp.graphics] Ray Tracing in 3D space

gyp@ccadfa.adfa.oz.au (Patrick Tang Guan Yaw) (03/09/90)

I am trying to do ray tracing of light through a 
cylinder coming at differenct angle to the axis
of the cylinder.  Could some one give me some
pointers?

Thanks in advance.
----
Patrick Tang Guan Yaw,		Phone	 :	+61 62 68 8882
Dept. of Mathematics,	EMAIL-ARPA/CSNET :	gyp@ccadfa.cc.adfa.oz.au
ADFA, Canberra, 2600.		UUCP	 :	..!uunet!munnari!ccadfa.oz!gyp
AUSTRALIA			ACSnet   :	gyp@ccadfa.oz

markv@gauss.Princeton.EDU (Mark VandeWettering) (03/13/90)

>I am trying to do ray tracing of light through a 
>cylinder coming at differenct angle to the axis
>of the cylinder.  Could some one give me some
>pointers?

Ray cylinder intersection is (conceptually) just as easy as hitting a sphere.
Most of the problems come from clipping the cylinder so it isn't infinite.
I can think of several ways to do this, but first let me mention that
you should consult Introduction to Ray Tracing edited by Andrew Glassner.
Articles by Pat Hanrahan and Eric Haines go over most of this stuff.


Its easist to imagine a unit cylinder fromed by rotating the line x = 1 
in the xy plane about the y axis.  The formula for this cylinder is 
x^2 + z^2 = 1.  If your ray is of the form P + t D, with P and D three
tuples, you can insert the components into the original formula and 
come up with:

	(px + t dx)^2 + (pz + t dz)^2 - 1 = 0 

or	px^2 + 2 t dx px + (t dx)^2 + pz^2 + 2 t dz pz + (t dz)^2

or	(px^2 + pz^2) + 2 t (dx px + dz pz) + t^2 (dx^2 + dz^2)

which you can then solve using the quadratic formula.  If there are 
no roots, then there is no intersection.  If there are roots, then these
give two t values along the ray.  Figure out those points using P + t D.
Now, clipping.  We wanted to have a finite cylinder, say within the cube
two units on a side centered at the origin.  Well, gosh, ignore any 
intersections that occur outside this box.  Then take the closest one.

Now, to intersect with an arbitrary cylinder, work up a world transformation
matrix that transforms points into the objects coordinate system.  Transform
the ray origin and direction, and voila.  You do need to be careful to rescale
t appropriately, but its really not all that hard.

You might instead want to implement general quadrics as a primitive, or 
choose any one of a number of different ways of doing the above.  Homogenous
coordinates might make this simpler actually.... Hmmm.... And there is a
geometric argument that can also be used to derive algorithms like this.

Think about it.  It shouldn't be that difficult.

Mark

ritter@versatc.versatec.COM (Jack Ritter) (03/17/90)

in article <14440@phoenix.Princeton.EDU>, markv@gauss.Princeton.EDU (Mark VandeWettering) says:
> 
> You might instead want to implement general quadrics as a primitive, or 
> choose any one of a number of different ways of doing the above.  Homogenous
> coordinates might make this simpler actually.... Hmmm.... And there is a
> geometric argument that can also be used to derive algorithms like this.
> 
> Think about it.  It shouldn't be that difficult.
> 
> Mark

I do general ntersections in image space, by using the general quadric locus:

  a1*x^2 + a2*y^2 + a3*z^2 + b1*x*y + b2*x*z * b3*y*z + c1*x * c2*y + c3*z +d = 0 .

This represents every class of quadric at any location/orientation/scale.

To compute the (0,1, or2) intersections in image space, substitute
the componants of the ray (Ox,Oy,Oz) -> (Rx,Ry,Rz) for the x's y's and z's
in the general locus. You will get a (very complex) 2nd degree polynomial
in t. Do the factoring and rearranging, to compute the a,b, and c coefficients
of the reduced polynomial: a*t^2 + b*t + c = 0.
Now solve this quadratic equation to get your t's. Only positive t's
are valid, usually the smallest is the 1 you want (closest intersection if there are 2).
The intersection point, from your final t, is of course (Ox,Oy,Oz) + t*(Rx,Ry,Rz).
You now have the intersection point without even knowing what kind of
quadric it is! 
  To go along with this scheme, you must instantiate your quadrics algibraically.
This method allows saddle surfaces, hyperboloids of 1 sheet, etc, in addition
to the usual spheres, cones, etc. It also allows squashed quadrics, not just
symetrical ones (eg, an elliptical cylinder).
  Then there is boundary testing. I have a scheme that desribes a CSG type
cluster of quadric surfaces. To do boundary testing, I transform the image space
intersection point into the object space of the bounding surface(s) in that cluster.
I then compare the point with the topological boundary constraint(s)
imposed on the surface by its fellow bounder(s).
  In all of this, you still never really have to know what type of quadric you have hit!!
The draw back of this general scheme is the compute time it takes to produce
the a,b,c of the t polynomial. With the conventional way of transforming the ray
back to the object's cannonical space, most of the coefficients of the quadric locus
(eg, the a's,b's,c's and d of the locus above) are zero or 1.
However, I'm happy with my method, because I do lots of ray rejection tricks, to make the
computation less (but then who doesnt?)
-- 
   Versatec, Inc.
   Jack Ritter, M.S. 1-7
   2710 Walsh Ave.
   P.O. Box 58091
   Santa Clara, CA 95052-8091
   (408)982-4332, or (408)988-2800 X 5743
   UUCP:  {ames,apple,sun,pyramid}!versatc!ritter

   --( / __ - .. ((  /
   / / -- ) . \ \ // . (
	/ ** ) // _*_ // * ..
	) (( . \ / . * ) //