| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- /****************************************************************************
- * planes.c
- *
- * This module implements functions that manipulate planes.
- *
- * from Persistence of Vision(tm) Ray Tracer
- * Copyright 1996,1999 Persistence of Vision Team
- *---------------------------------------------------------------------------
- * NOTICE: This source code file is provided so that users may experiment
- * with enhancements to POV-Ray and to port the software to platforms other
- * than those supported by the POV-Ray Team. There are strict rules under
- * which you are permitted to use this file. The rules are in the file
- * named POVLEGAL.DOC which should be distributed with this file.
- * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
- * Team Coordinator by email to team-coord@povray.org or visit us on the web at
- * http://www.povray.org. The latest version of POV-Ray may be found at this site.
- *
- * This program is based on the popular DKB raytracer version 2.12.
- * DKBTrace was originally written by David K. Buck.
- * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
- *
- *****************************************************************************/
- #include "frame.h"
- #include "povray.h"
- #include "vector.h"
- #include "povproto.h"
- #include "matrices.h"
- #include "objects.h"
- #include "planes.h"
- /*****************************************************************************
- * Local preprocessor defines
- ******************************************************************************/
- #define DEPTH_TOLERANCE 1.0e-6
- /*****************************************************************************
- * Static functions
- ******************************************************************************/
- static int Intersect_Plane (RAY *Ray, PLANE *Plane, DBL *Depth);
- static int All_Plane_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack);
- static int Inside_Plane (VECTOR point, OBJECT *Object);
- static void Plane_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter);
- static PLANE *Copy_Plane (OBJECT *Object);
- static void Translate_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
- static void Rotate_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
- static void Scale_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
- static void Transform_Plane (OBJECT *Object, TRANSFORM *Trans);
- static void Invert_Plane (OBJECT *Object);
- static void Destroy_Plane (OBJECT *Object);
- /*****************************************************************************
- * Local variables
- ******************************************************************************/
- METHODS Plane_Methods =
- {
- All_Plane_Intersections,
- Inside_Plane, Plane_Normal,
- (COPY_METHOD)Copy_Plane,
- Translate_Plane, Rotate_Plane,
- Scale_Plane, Transform_Plane, Invert_Plane, Destroy_Plane
- };
- /*****************************************************************************
- *
- * FUNCTION
- *
- * All_Plane_Intersections
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static int All_Plane_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack)
- {
- DBL Depth;
- VECTOR IPoint;
- if (Intersect_Plane(Ray, (PLANE *)Object, &Depth))
- {
- VEvaluateRay(IPoint, Ray->Initial, Depth, Ray->Direction);
- if (Point_In_Clip(IPoint, Object->Clip))
- {
- push_entry(Depth,IPoint,Object,Depth_Stack);
- return(TRUE);
- }
- }
- return(FALSE);
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Intersect_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static int Intersect_Plane (RAY *Ray, PLANE *Plane, DBL *Depth)
- {
- DBL NormalDotOrigin, NormalDotDirection;
- VECTOR P, D;
- Increase_Counter(stats[Ray_Plane_Tests]);
- if (Plane->Trans == NULL)
- {
- VDot(NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
- if (fabs(NormalDotDirection) < EPSILON)
- {
- return(FALSE);
- }
- VDot(NormalDotOrigin, Plane->Normal_Vector, Ray->Initial);
- }
- else
- {
- MInvTransPoint(P, Ray->Initial, Plane->Trans);
- MInvTransDirection(D, Ray->Direction, Plane->Trans);
- VDot(NormalDotDirection, Plane->Normal_Vector, D);
- if (fabs(NormalDotDirection) < EPSILON)
- {
- return(FALSE);
- }
- VDot(NormalDotOrigin, Plane->Normal_Vector, P);
- }
- *Depth = -(NormalDotOrigin + Plane->Distance) / NormalDotDirection;
- if ((*Depth >= DEPTH_TOLERANCE) && (*Depth <= Max_Distance))
- {
- Increase_Counter(stats[Ray_Plane_Tests_Succeeded]);
- return (TRUE);
- }
- else
- {
- return (FALSE);
- }
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Inside_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static int Inside_Plane (VECTOR IPoint, OBJECT *Object)
- {
- DBL Temp;
- VECTOR P;
- if (((PLANE *)Object)->Trans == NULL)
- {
- VDot (Temp, IPoint, ((PLANE *)Object)->Normal_Vector);
- }
- else
- {
- MInvTransPoint(P, IPoint, ((PLANE *)Object)->Trans);
- VDot (Temp, P, ((PLANE *)Object)->Normal_Vector);
- }
- return((Temp + ((PLANE *)Object)->Distance) < EPSILON);
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Plane_Normal
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static void Plane_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter)
- {
- Assign_Vector(Result,((PLANE *)Object)->Normal_Vector);
- if (((PLANE *)Object)->Trans != NULL)
- {
- MTransNormal(Result, Result, ((PLANE *)Object)->Trans);
- VNormalize(Result, Result);
- }
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Translate_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static void Translate_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
- {
- VECTOR Translation;
- PLANE *Plane = (PLANE *)Object;
- if (Plane->Trans == NULL)
- {
- VEvaluate (Translation, ((PLANE *)Object)->Normal_Vector, Vector);
- ((PLANE *)Object)->Distance -= Translation[X] + Translation[Y] + Translation[Z];
- Compute_Plane_BBox((PLANE *)Object);
- }
- else
- {
- Transform_Plane(Object, Trans);
- }
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Rotate_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static void Rotate_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
- {
- if (((PLANE *)Object)->Trans == NULL)
- {
- MTransDirection(((PLANE *)Object)->Normal_Vector, ((PLANE *)Object)->Normal_Vector, Trans);
- Compute_Plane_BBox(((PLANE *)Object));
- }
- else
- {
- Transform_Plane (Object, Trans);
- }
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Scale_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static void Scale_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
- {
- DBL Length;
- PLANE *Plane = (PLANE *) Object;
- if (Plane->Trans == NULL)
- {
- VDivEq(Plane->Normal_Vector, Vector);
- VLength(Length, ((PLANE *)Object)->Normal_Vector);
- VInverseScaleEq (((PLANE *)Object)->Normal_Vector, Length);
- ((PLANE *)Object)->Distance /= Length;
- Compute_Plane_BBox(Plane);
- }
- else
- {
- Transform_Plane (Object, Trans);
- }
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Invert_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static void Invert_Plane (OBJECT *Object)
- {
- VScaleEq(((PLANE *)Object)->Normal_Vector, -1.0);
- ((PLANE *)Object)->Distance *= -1.0;
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Transform_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static void Transform_Plane(OBJECT *Object, TRANSFORM *Trans)
- {
- PLANE *Plane = (PLANE *) Object;
- if (Plane->Trans == NULL)
- {
- Plane->Trans = Create_Transform();
- }
- Compose_Transforms(Plane->Trans, Trans);
- Compute_Plane_BBox(Plane);
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Create_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- PLANE *Create_Plane()
- {
- PLANE *New;
- New = (PLANE *)POV_MALLOC(sizeof (PLANE), "plane");
- INIT_OBJECT_FIELDS(New,PLANE_OBJECT,&Plane_Methods)
- Make_Vector(New->Normal_Vector, 0.0, 1.0, 0.0);
- New ->Distance = 0.0;
- New->Trans = NULL;
- return(New);
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Copy_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static PLANE *Copy_Plane (OBJECT *Object)
- {
- PLANE *New;
- New = Create_Plane();
- Destroy_Transform(New->Trans);
- *New = *((PLANE *)Object);
- New->Trans = Copy_Transform(((PLANE *)Object)->Trans);
- return(New);
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Destroy_Plane
- *
- * INPUT
- *
- * OUTPUT
- *
- * RETURNS
- *
- * AUTHOR
- *
- * POV-Ray Team
- *
- * DESCRIPTION
- *
- * -
- *
- * CHANGES
- *
- * -
- *
- ******************************************************************************/
- static void Destroy_Plane(OBJECT *Object)
- {
- Destroy_Transform(((PLANE *)Object)->Trans);
- POV_FREE(Object);
- }
- /*****************************************************************************
- *
- * FUNCTION
- *
- * Compute_Plane_BBox
- *
- * INPUT
- *
- * Plane - Plane
- *
- * OUTPUT
- *
- * Plane
- *
- * RETURNS
- *
- * AUTHOR
- *
- * Dieter Bayer
- *
- * DESCRIPTION
- *
- * Calculate the bounding box of a plane (it's always infinite).
- *
- * CHANGES
- *
- * Aug 1994 : Creation.
- *
- ******************************************************************************/
- void Compute_Plane_BBox(PLANE *Plane)
- {
- Make_BBox(Plane->BBox, -BOUND_HUGE/2, -BOUND_HUGE/2, -BOUND_HUGE/2,
- BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
- if (Plane->Clip != NULL)
- {
- Plane->BBox = Plane->Clip->BBox;
- }
- }
|