home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / povsrc31.zip / planes.c < prev    next >
C/C++ Source or Header  |  2000-05-13  |  13KB  |  703 lines

  1. /****************************************************************************
  2. *                planes.c
  3. *
  4. *  This module implements functions that manipulate planes.
  5. *
  6. *  from Persistence of Vision(tm) Ray Tracer
  7. *  Copyright 1996,1999 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file.
  14. *  If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by email to team-coord@povray.org or visit us on the web at
  16. *  http://www.povray.org. The latest version of POV-Ray may be found at this site.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23.  
  24. #include "frame.h"
  25. #include "povray.h"
  26. #include "vector.h"
  27. #include "povproto.h"
  28. #include "matrices.h"
  29. #include "objects.h"
  30. #include "planes.h"
  31.  
  32.  
  33.  
  34. /*****************************************************************************
  35. * Local preprocessor defines
  36. ******************************************************************************/
  37.  
  38. #define DEPTH_TOLERANCE 1.0e-6
  39.  
  40. /*****************************************************************************
  41. * Static functions
  42. ******************************************************************************/
  43.  
  44. static int   Intersect_Plane (RAY *Ray, PLANE *Plane, DBL *Depth);
  45. static int   All_Plane_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack);
  46. static int   Inside_Plane (VECTOR point, OBJECT *Object);
  47. static void  Plane_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter);
  48. static PLANE *Copy_Plane (OBJECT *Object);
  49. static void  Translate_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  50. static void  Rotate_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  51. static void  Scale_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  52. static void  Transform_Plane (OBJECT *Object, TRANSFORM *Trans);
  53. static void  Invert_Plane (OBJECT *Object);
  54. static void  Destroy_Plane (OBJECT *Object);
  55.  
  56. /*****************************************************************************
  57. * Local variables
  58. ******************************************************************************/
  59.  
  60. METHODS Plane_Methods =
  61. {
  62.   All_Plane_Intersections,
  63.   Inside_Plane, Plane_Normal, Default_UVCoord,
  64.   (COPY_METHOD)Copy_Plane,
  65.   Translate_Plane, Rotate_Plane,
  66.   Scale_Plane, Transform_Plane, Invert_Plane, Destroy_Plane
  67. };
  68.  
  69.  
  70. /*****************************************************************************
  71. *
  72. * FUNCTION
  73. *
  74. *   All_Plane_Intersections
  75. *
  76. * INPUT
  77. *   
  78. * OUTPUT
  79. *   
  80. * RETURNS
  81. *   
  82. * AUTHOR
  83. *
  84. *   POV-Ray Team
  85. *   
  86. * DESCRIPTION
  87. *
  88. *   -
  89. *
  90. * CHANGES
  91. *
  92. *   -
  93. *
  94. ******************************************************************************/
  95.  
  96. static int All_Plane_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack)
  97. {
  98.   DBL Depth;
  99.   VECTOR IPoint;
  100.  
  101.   if (Intersect_Plane(Ray, (PLANE *)Object, &Depth))
  102.   {
  103.     VEvaluateRay(IPoint, Ray->Initial, Depth, Ray->Direction);
  104.  
  105.     if (Point_In_Clip(IPoint, Object->Clip))
  106.     {
  107.       push_entry(Depth,IPoint,Object,Depth_Stack);
  108.  
  109.       return(TRUE);
  110.     }
  111.   }
  112.  
  113.   return(FALSE);
  114. }
  115.  
  116.  
  117.  
  118. /*****************************************************************************
  119. *
  120. * FUNCTION
  121. *
  122. *   Intersect_Plane
  123. *
  124. * INPUT
  125. *   
  126. * OUTPUT
  127. *   
  128. * RETURNS
  129. *   
  130. * AUTHOR
  131. *
  132. *   POV-Ray Team
  133. *   
  134. * DESCRIPTION
  135. *
  136. *   -
  137. *
  138. * CHANGES
  139. *
  140. *   -
  141. *
  142. ******************************************************************************/
  143.  
  144. static int Intersect_Plane (RAY *Ray, PLANE *Plane, DBL *Depth)
  145. {
  146.   DBL NormalDotOrigin, NormalDotDirection;
  147. #ifndef FastPlanePatch
  148.   VECTOR P, D;
  149. #endif
  150.   Increase_Counter(stats[Ray_Plane_Tests]);
  151.  
  152. #ifndef FastPlanePatch
  153.   if (Plane->Trans == NULL)
  154.   {
  155. #endif
  156.     VDot(NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  157.  
  158.     if (fabs(NormalDotDirection) < EPSILON)
  159.     {
  160.       return(FALSE);
  161.     }
  162.  
  163.     VDot(NormalDotOrigin, Plane->Normal_Vector, Ray->Initial);
  164. #ifndef FastPlanePatch
  165.   }
  166.   else
  167.   {
  168.     MInvTransPoint(P, Ray->Initial, Plane->Trans);
  169.     MInvTransDirection(D, Ray->Direction, Plane->Trans);
  170.  
  171.     VDot(NormalDotDirection, Plane->Normal_Vector, D);
  172.  
  173.     if (fabs(NormalDotDirection) < EPSILON)
  174.     {
  175.       return(FALSE);
  176.     }
  177.  
  178.     VDot(NormalDotOrigin, Plane->Normal_Vector, P);
  179.   }
  180. #endif
  181.   *Depth = -(NormalDotOrigin + Plane->Distance) / NormalDotDirection;
  182.  
  183.   if ((*Depth >= DEPTH_TOLERANCE) && (*Depth <= Max_Distance))
  184.   {
  185.     Increase_Counter(stats[Ray_Plane_Tests_Succeeded]);
  186.  
  187.     return (TRUE);
  188.   }
  189.   else
  190.   {
  191.     return (FALSE);
  192.   }
  193. }
  194.  
  195.  
  196.  
  197. /*****************************************************************************
  198. *
  199. * FUNCTION
  200. *
  201. *   Inside_Plane
  202. *
  203. * INPUT
  204. *   
  205. * OUTPUT
  206. *   
  207. * RETURNS
  208. *   
  209. * AUTHOR
  210. *
  211. *   POV-Ray Team
  212. *   
  213. * DESCRIPTION
  214. *
  215. *   -
  216. *
  217. * CHANGES
  218. *
  219. *   -
  220. *
  221. ******************************************************************************/
  222.  
  223. static int Inside_Plane (VECTOR IPoint, OBJECT *Object)
  224. {
  225.   DBL Temp;
  226. #ifndef FastPlanePatch
  227.   VECTOR P;
  228.  
  229.   if (((PLANE *)Object)->Trans == NULL)
  230.   {
  231.   #endif
  232.     VDot (Temp, IPoint, ((PLANE *)Object)->Normal_Vector);
  233. #ifndef FastPlanePatch
  234.  
  235.   }
  236.   else
  237.   {
  238.     MInvTransPoint(P, IPoint, ((PLANE *)Object)->Trans);
  239.  
  240.     VDot (Temp, P, ((PLANE *)Object)->Normal_Vector);
  241.   }
  242. #endif
  243.   return((Temp + ((PLANE *)Object)->Distance) < EPSILON);
  244. }
  245.  
  246.  
  247.  
  248. /*****************************************************************************
  249. *
  250. * FUNCTION
  251. *
  252. *   Plane_Normal
  253. *
  254. * INPUT
  255. *
  256. * OUTPUT
  257. *
  258. * RETURNS
  259. *
  260. * AUTHOR
  261. *
  262. *   POV-Ray Team
  263. *
  264. * DESCRIPTION
  265. *
  266. *   -
  267. *
  268. * CHANGES
  269. *
  270. *   -
  271. *
  272. ******************************************************************************/
  273.  
  274. static void Plane_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter)
  275. {
  276.   Assign_Vector(Result,((PLANE *)Object)->Normal_Vector);
  277. #ifndef FastPlanePatch
  278.  
  279.   if (((PLANE *)Object)->Trans != NULL)
  280.   {
  281.     MTransNormal(Result, Result, ((PLANE *)Object)->Trans);
  282.  
  283.     VNormalize(Result, Result);
  284.   }
  285.   #endif
  286. }
  287.  
  288.  
  289.  
  290. /*****************************************************************************
  291. *
  292. * FUNCTION
  293. *
  294. *   Translate_Plane
  295. *
  296. * INPUT
  297. *   
  298. * OUTPUT
  299. *   
  300. * RETURNS
  301. *   
  302. * AUTHOR
  303. *
  304. *   POV-Ray Team
  305. *   
  306. * DESCRIPTION
  307. *
  308. *   -
  309. *
  310. * CHANGES
  311. *
  312. *   -
  313. *
  314. ******************************************************************************/
  315.  
  316. static void Translate_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  317. {
  318.   VECTOR Translation;
  319.   PLANE *Plane = (PLANE *)Object;
  320.  
  321.  #ifndef FastPlanePatch
  322.  if (Plane->Trans == NULL)
  323.   {
  324. #endif
  325.     VEvaluate (Translation, ((PLANE *)Object)->Normal_Vector, Vector);
  326.  
  327.     ((PLANE *)Object)->Distance -= Translation[X] + Translation[Y] + Translation[Z];
  328.  
  329.     Compute_Plane_BBox((PLANE *)Object);
  330. #ifndef FastPlanePatch
  331.   }
  332.   else
  333.   {
  334.     Transform_Plane(Object, Trans);
  335.   }
  336. #endif
  337. }
  338.  
  339.  
  340.  
  341. /*****************************************************************************
  342. *
  343. * FUNCTION
  344. *
  345. *   Rotate_Plane
  346. *
  347. * INPUT
  348. *   
  349. * OUTPUT
  350. *   
  351. * RETURNS
  352. *   
  353. * AUTHOR
  354. *
  355. *   POV-Ray Team
  356. *   
  357. * DESCRIPTION
  358. *
  359. *   -
  360. *
  361. * CHANGES
  362. *
  363. *   -
  364. *
  365. ******************************************************************************/
  366.  
  367. static void Rotate_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  368. {
  369. #ifndef FastPlanePatch
  370.   if (((PLANE *)Object)->Trans == NULL)
  371.   {
  372. #endif
  373.     MTransDirection(((PLANE *)Object)->Normal_Vector, ((PLANE *)Object)->Normal_Vector, Trans);
  374.  
  375.     Compute_Plane_BBox(((PLANE *)Object));
  376. #ifndef FastPlanePatch
  377.   }
  378.   else
  379.   {
  380.     Transform_Plane (Object, Trans);
  381.   }
  382. #endif
  383. }
  384.  
  385.  
  386.  
  387. /*****************************************************************************
  388. *
  389. * FUNCTION
  390. *
  391. *   Scale_Plane
  392. *
  393. * INPUT
  394. *   
  395. * OUTPUT
  396. *   
  397. * RETURNS
  398. *   
  399. * AUTHOR
  400. *
  401. *   POV-Ray Team
  402. *   
  403. * DESCRIPTION
  404. *
  405. *   -
  406. *
  407. * CHANGES
  408. *
  409. *   -
  410. *
  411. ******************************************************************************/
  412.  
  413. static void Scale_Plane (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  414. {
  415.   DBL Length;
  416.   PLANE *Plane = (PLANE  *) Object;
  417.  
  418.  #ifndef FastPlanePatch
  419.  if (Plane->Trans == NULL)
  420.   {
  421. #endif
  422.     VDivEq(Plane->Normal_Vector, Vector);
  423.  
  424.     VLength(Length, ((PLANE *)Object)->Normal_Vector);
  425.  
  426.     VInverseScaleEq (((PLANE *)Object)->Normal_Vector, Length);
  427.  
  428.     ((PLANE *)Object)->Distance /= Length;
  429.  
  430.     Compute_Plane_BBox(Plane);
  431. #ifndef FastPlanePatch
  432.   }
  433.   else
  434.   {
  435.     Transform_Plane (Object, Trans);
  436.   }
  437. #endif
  438. }
  439.  
  440.  
  441.  
  442. /*****************************************************************************
  443. *
  444. * FUNCTION
  445. *
  446. *   Invert_Plane
  447. *
  448. * INPUT
  449. *
  450. * OUTPUT
  451. *
  452. * RETURNS
  453. *
  454. * AUTHOR
  455. *
  456. *   POV-Ray Team
  457. *
  458. * DESCRIPTION
  459. *
  460. *   -
  461. *
  462. * CHANGES
  463. *
  464. *   -
  465. *
  466. ******************************************************************************/
  467.  
  468. static void Invert_Plane (OBJECT *Object)
  469. {
  470.   VScaleEq(((PLANE *)Object)->Normal_Vector, -1.0);
  471.  
  472.   ((PLANE *)Object)->Distance *= -1.0;
  473. }
  474.  
  475.  
  476.  
  477. /*****************************************************************************
  478. *
  479. * FUNCTION
  480. *
  481. *   Transform_Plane
  482. *
  483. * INPUT
  484. *
  485. * OUTPUT
  486. *
  487. * RETURNS
  488. *
  489. * AUTHOR
  490. *
  491. *   POV-Ray Team
  492. *
  493. * DESCRIPTION
  494. *
  495. *   -
  496. *
  497. * CHANGES
  498. *
  499. *   -
  500. *
  501. ******************************************************************************/
  502.  
  503. static void Transform_Plane(OBJECT *Object, TRANSFORM *Trans)
  504. {
  505.   PLANE *Plane = (PLANE  *) Object;
  506. #ifndef FastPlanePatch
  507.  
  508.   if (Plane->Trans == NULL)
  509.   {
  510.     Plane->Trans = Create_Transform();
  511.   }
  512.  
  513.   Compose_Transforms(Plane->Trans, Trans);
  514. #else
  515.   VECTOR point;
  516.  
  517.   VScale( point, Plane->Normal_Vector, -Plane->Distance );
  518.  
  519.   MTransPoint( point, point, Trans );
  520.  
  521.   MTransNormal( Plane->Normal_Vector, Plane->Normal_Vector, Trans );
  522.  
  523.   VNormalizeEq( Plane->Normal_Vector );
  524.  
  525.   VDot( Plane->Distance, Plane->Normal_Vector, point );
  526.  
  527.   Plane->Distance = - Plane->Distance;
  528.  
  529. #endif
  530.   Compute_Plane_BBox(Plane);
  531. }
  532.  
  533.  
  534.  
  535. /*****************************************************************************
  536. *
  537. * FUNCTION
  538. *
  539. *   Create_Plane
  540. *
  541. * INPUT
  542. *
  543. * OUTPUT
  544. *
  545. * RETURNS
  546. *
  547. * AUTHOR
  548. *
  549. *   POV-Ray Team
  550. *   
  551. * DESCRIPTION
  552. *
  553. *   -
  554. *
  555. * CHANGES
  556. *
  557. *   -
  558. *
  559. ******************************************************************************/
  560.  
  561. PLANE *Create_Plane()
  562. {
  563.   PLANE *New;
  564.  
  565.   New = (PLANE *)POV_MALLOC(sizeof (PLANE), "plane");
  566.  
  567.   INIT_OBJECT_FIELDS(New,PLANE_OBJECT,&Plane_Methods)
  568.  
  569.   Make_Vector(New->Normal_Vector, 0.0, 1.0, 0.0);
  570.  
  571.   New ->Distance = 0.0;
  572.  
  573.  #ifndef FastPlanePatch
  574.  New->Trans = NULL;
  575. #endif
  576.   return(New);
  577. }
  578.  
  579.  
  580.  
  581. /*****************************************************************************
  582. *
  583. * FUNCTION
  584. *
  585. *   Copy_Plane
  586. *
  587. * INPUT
  588. *   
  589. * OUTPUT
  590. *   
  591. * RETURNS
  592. *   
  593. * AUTHOR
  594. *
  595. *   POV-Ray Team
  596. *   
  597. * DESCRIPTION
  598. *
  599. *   -
  600. *
  601. * CHANGES
  602. *
  603. *   -
  604. *
  605. ******************************************************************************/
  606.  
  607. static PLANE *Copy_Plane (OBJECT *Object)
  608. {
  609.   PLANE *New;
  610.  
  611.   New = Create_Plane();
  612.  
  613. #ifndef FastPlanePatch
  614.   Destroy_Transform(New->Trans);
  615. #endif
  616.   *New = *((PLANE *)Object);
  617. #ifndef FastPlanePatch
  618.  
  619.   New->Trans = Copy_Transform(((PLANE *)Object)->Trans);
  620. #endif
  621.   return(New);
  622. }
  623.  
  624.  
  625.  
  626. /*****************************************************************************
  627. *
  628. * FUNCTION
  629. *
  630. *   Destroy_Plane
  631. *
  632. * INPUT
  633. *   
  634. * OUTPUT
  635. *   
  636. * RETURNS
  637. *   
  638. * AUTHOR
  639. *
  640. *   POV-Ray Team
  641. *   
  642. * DESCRIPTION
  643. *
  644. *   -
  645. *
  646. * CHANGES
  647. *
  648. *   -
  649. *
  650. ******************************************************************************/
  651.  
  652. static void Destroy_Plane(OBJECT *Object)
  653. {
  654. #ifndef FastPlanePatch
  655.   Destroy_Transform(((PLANE *)Object)->Trans);
  656. #endif
  657.   POV_FREE(Object);
  658. }
  659.  
  660.  
  661.  
  662. /*****************************************************************************
  663. *
  664. * FUNCTION
  665. *
  666. *   Compute_Plane_BBox
  667. *
  668. * INPUT
  669. *
  670. *   Plane - Plane
  671. *   
  672. * OUTPUT
  673. *
  674. *   Plane
  675. *   
  676. * RETURNS
  677. *   
  678. * AUTHOR
  679. *
  680. *   Dieter Bayer
  681. *   
  682. * DESCRIPTION
  683. *
  684. *   Calculate the bounding box of a plane (it's always infinite).
  685. *
  686. * CHANGES
  687. *
  688. *   Aug 1994 : Creation.
  689. *
  690. ******************************************************************************/
  691.  
  692. void Compute_Plane_BBox(PLANE *Plane)
  693. {
  694.   Make_BBox(Plane->BBox, -BOUND_HUGE/2, -BOUND_HUGE/2, -BOUND_HUGE/2,
  695.     BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  696.  
  697.   if (Plane->Clip != NULL)
  698.   {
  699.     Plane->BBox = Plane->Clip->BBox;
  700.   }
  701. }
  702.  
  703.