1 //******************************************************************************
2 ///
3 /// @file core/render/tracepixel.h
4 ///
5 /// Declarations related to the basics of raytracing a pixel.
6 ///
7 /// @copyright
8 /// @parblock
9 ///
10 /// Persistence of Vision Ray Tracer ('POV-Ray') version 3.8.
11 /// Copyright 1991-2017 Persistence of Vision Raytracer Pty. Ltd.
12 ///
13 /// POV-Ray is free software: you can redistribute it and/or modify
14 /// it under the terms of the GNU Affero General Public License as
15 /// published by the Free Software Foundation, either version 3 of the
16 /// License, or (at your option) any later version.
17 ///
18 /// POV-Ray is distributed in the hope that it will be useful,
19 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
20 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 /// GNU Affero General Public License for more details.
22 ///
23 /// You should have received a copy of the GNU Affero General Public License
24 /// along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 ///
26 /// ----------------------------------------------------------------------------
27 ///
28 /// POV-Ray is based on the popular DKB raytracer version 2.12.
29 /// DKBTrace was originally written by David K. Buck.
30 /// DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
31 ///
32 /// @endparblock
33 ///
34 //******************************************************************************
35 
36 #ifndef POVRAY_CORE_TRACEPIXEL_H
37 #define POVRAY_CORE_TRACEPIXEL_H
38 
39 // Module config header file must be the first file included within POV-Ray unit header files
40 #include "core/configcore.h"
41 
42 #include <vector>
43 
44 #include "core/render/trace.h"
45 #include "core/scene/camera.h"
46 #include "core/scene/scenedata.h"
47 
48 namespace pov
49 {
50 
51 //##############################################################################
52 ///
53 /// @addtogroup PovCoreRender
54 /// @ingroup PovCore
55 ///
56 /// @{
57 
58 struct HasInteriorPointObjectCondition : public PointObjectCondition
59 {
60     virtual bool operator()(const Vector3d& point, ConstObjectPtr object) const;
61 };
62 
63 struct ContainingInteriorsPointObjectCondition : public PointObjectCondition
64 {
ContainingInteriorsPointObjectConditionContainingInteriorsPointObjectCondition65     ContainingInteriorsPointObjectCondition(RayInteriorVector& ci) : containingInteriors(ci) {}
66     virtual bool operator()(const Vector3d& point, ConstObjectPtr object) const;
67     RayInteriorVector &containingInteriors;
68 };
69 
70 class TracePixel : public Trace
71 {
72     public:
73         TracePixel(shared_ptr<SceneData> sd, const Camera* cam, TraceThreadData *td, unsigned int mtl, DBL adcb, const QualityFlags& qf,
74                    CooperateFunctor& cf, MediaFunctor& mf, RadiosityFunctor& af, bool pt = false);
75         virtual ~TracePixel();
76         void SetupCamera(const Camera& cam);
77 
78         /// Trace a pixel or sub-pixel.
79         /// @param[in]  x       X-coordinate of the (sub-)pixel's center, typically ranging from 0.5 (left) to width-0.5 (right).
80         /// @param[in]  y       Y-coordinate of the (sub-)pixel's center, typically ranging from 0.5 (top) to height-0.5 (bottom).
81         /// @param[in]  width   Horizontal size of the image in pixels.
82         /// @param[in]  height  Vertical size of the image in pixels.
83         /// @param[out] colour  Computed colour of the (sub-)pixel.
84         void operator()(DBL x, DBL y, DBL width, DBL height, RGBTColour& colour);
85     private:
86         // Focal blur data
87         class FocalBlurData
88         {
89         public:
90             FocalBlurData(const Camera& camera, TraceThreadData* threadData);
91             ~FocalBlurData();
92 
93             // Direction to focal plane.
94             DBL Focal_Distance;
95             // Array of threshold for confidence test.
96             DBL *Sample_Threshold;
97             // Array giving number of samples to take before next confidence test.
98             const int *Current_Number_Of_Samples;
99             // Array of sample locations.
100             Vector2d *Sample_Grid;
101             // Maximum amount of jitter to use.
102             DBL Max_Jitter;
103             // Vectors in the viewing plane.
104             Vector3d XPerp, YPerp;
105 
106         };
107 
108         bool useFocalBlur;
109         FocalBlurData *focalBlurData;
110 
111         bool precomputeContainingInteriors;
112         RayInteriorVector containingInteriors;
113 
114         Vector3d cameraDirection;
115         Vector3d cameraRight;
116         Vector3d cameraUp;
117         Vector3d cameraLocation;
118         /// length of current camera's 'right' vector prior to normalisation
119         DBL cameraLengthRight;
120         /// length of current camera's 'up' vector prior to normalisation
121         DBL cameraLengthUp;
122         /// aspect ratio for current camera
123         DBL aspectRatio;
124         /// camera
125         Camera camera;
126         /// scene data
127         shared_ptr<SceneData> sceneData;
128         /// thread data
129         TraceThreadData *threadData;
130 
131         /// maximum trace recursion level allowed
132         unsigned int maxTraceLevel;
133         /// adc bailout
134         DBL adcBailout;
135         /// whether this is just a pretrace, allowing some computations to be skipped
136         bool pretrace;
137 
138         /// Thread-local instances of user-defined camera functions
139         GenericScalarFunctionInstancePtr mpCameraLocationFn[3];
140         GenericScalarFunctionInstancePtr mpCameraDirectionFn[3];
141 
142         bool CreateCameraRay(Ray& ray, DBL x, DBL y, DBL width, DBL height, size_t ray_number);
143 
144         void InitRayContainerState(Ray& ray, bool compute = false);
145         void InitRayContainerStateTree(Ray& ray, BBOX_TREE *node);
146 
147         void TraceRayWithFocalBlur(RGBTColour& colour, DBL x, DBL y, DBL width, DBL height);
148         void JitterCameraRay(Ray& ray, DBL x, DBL y, size_t ray_number);
149 };
150 
151 /// @}
152 ///
153 //##############################################################################
154 
155 }
156 
157 #endif // POVRAY_CORE_TRACEPIXEL_H
158