1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2018 Esteban Tovagliari, The appleseedhq Organization
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27 //
28 
29 // Interface header.
30 #include "positionaov.h"
31 
32 // appleseed.renderer headers.
33 #include "renderer/kernel/aov/aovaccumulator.h"
34 #include "renderer/kernel/rendering/pixelcontext.h"
35 #include "renderer/kernel/shading/shadingpoint.h"
36 #include "renderer/kernel/shading/shadingresult.h"
37 #include "renderer/modeling/aov/aov.h"
38 
39 // appleseed.foundation headers.
40 #include "foundation/image/color.h"
41 #include "foundation/image/image.h"
42 #include "foundation/image/tile.h"
43 #include "foundation/utility/api/apistring.h"
44 #include "foundation/utility/api/specializedapiarrays.h"
45 #include "foundation/utility/containers/dictionary.h"
46 
47 // Standard headers.
48 #include <cstddef>
49 
50 using namespace foundation;
51 using namespace std;
52 
53 namespace renderer
54 {
55 
56 namespace
57 {
58     //
59     // Position AOV accumulator.
60     //
61 
62     class PositionAOVAccumulator
63       : public UnfilteredAOVAccumulator
64     {
65       public:
PositionAOVAccumulator(Image & image)66         explicit PositionAOVAccumulator(Image& image)
67           : UnfilteredAOVAccumulator(image)
68         {
69         }
70 
write(const PixelContext & pixel_context,const ShadingPoint & shading_point,const ShadingComponents & shading_components,const AOVComponents & aov_components,ShadingResult & shading_result)71         void write(
72             const PixelContext&         pixel_context,
73             const ShadingPoint&         shading_point,
74             const ShadingComponents&    shading_components,
75             const AOVComponents&        aov_components,
76             ShadingResult&              shading_result) override
77         {
78             const Vector2i& pi = pixel_context.get_pixel_coords();
79 
80             // Ignore samples outside the tile.
81             if (!m_cropped_tile_bbox.contains(pi))
82                 return;
83 
84             float* out =
85                 reinterpret_cast<float*>(
86                     m_tile->pixel(
87                         pi.x - m_tile_origin_x,
88                         pi.y - m_tile_origin_y));
89 
90             if (shading_point.hit_surface())
91             {
92                 const Vector3d& p = shading_point.get_point();
93                 out[0] = static_cast<float>(p[0]);
94                 out[1] = static_cast<float>(p[1]);
95                 out[2] = static_cast<float>(p[2]);
96             }
97             else
98             {
99                 out[0] = 0.0f;
100                 out[1] = 0.0f;
101                 out[2] = 0.0f;
102             }
103         }
104     };
105 
106 
107     //
108     // Position AOV.
109     //
110 
111     const char* PositionAOVModel = "position_aov";
112 
113     class PositionAOV
114       : public UnfilteredAOV
115     {
116       public:
PositionAOV(const ParamArray & params)117         explicit PositionAOV(const ParamArray& params)
118           : UnfilteredAOV("position", params)
119         {
120         }
121 
release()122         void release() override
123         {
124             delete this;
125         }
126 
get_model() const127         const char* get_model() const override
128         {
129             return PositionAOVModel;
130         }
131 
clear_image()132         void clear_image() override
133         {
134             m_image->clear(Color3f(0.0f));
135         }
136 
137       private:
create_accumulator() const138         auto_release_ptr<AOVAccumulator> create_accumulator() const override
139         {
140             return auto_release_ptr<AOVAccumulator>(
141                 new PositionAOVAccumulator(get_image()));
142         }
143     };
144 }
145 
146 
147 //
148 // PositionAOVFactory class implementation.
149 //
150 
release()151 void PositionAOVFactory::release()
152 {
153     delete this;
154 }
155 
get_model() const156 const char* PositionAOVFactory::get_model() const
157 {
158     return PositionAOVModel;
159 }
160 
get_model_metadata() const161 Dictionary PositionAOVFactory::get_model_metadata() const
162 {
163     return
164         Dictionary()
165             .insert("name", get_model())
166             .insert("label", "Position");
167 }
168 
get_input_metadata() const169 DictionaryArray PositionAOVFactory::get_input_metadata() const
170 {
171     DictionaryArray metadata;
172     return metadata;
173 }
174 
create(const ParamArray & params) const175 auto_release_ptr<AOV> PositionAOVFactory::create(const ParamArray& params) const
176 {
177     return auto_release_ptr<AOV>(new PositionAOV(params));
178 }
179 
180 }   // namespace renderer
181