1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 ///
25 /// \file gf/wrapCamera.h
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/gf/camera.h"
29 #include "pxr/base/gf/frustum.h"
30 #include "pxr/base/tf/pyEnum.h"
31 
32 #include <boost/python/operators.hpp>
33 
34 #include <vector>
35 
36 using namespace boost::python;
37 
38 PXR_NAMESPACE_USING_DIRECTIVE
39 
40 namespace {
41 
42 static float
_GetHorizontalFieldOfView(const GfCamera & camera)43 _GetHorizontalFieldOfView(const GfCamera &camera) {
44     return camera.GetFieldOfView(GfCamera::FOVHorizontal);
45 }
46 
47 static float
_GetVerticalFieldOfView(const GfCamera & camera)48 _GetVerticalFieldOfView(const GfCamera &camera) {
49     return camera.GetFieldOfView(GfCamera::FOVVertical);
50 }
51 
52 // Required because CamCamera::GetClippingPlane returns const &std::vector
53 // and add_property does not allow one to specify a return_value policy.
54 static std::vector<GfVec4f>
_GetClippingPlanes(const GfCamera & camera)55 _GetClippingPlanes(const GfCamera &camera) {
56     return camera.GetClippingPlanes();
57 }
58 
_Repr(GfCamera const & self)59 static std::string _Repr(GfCamera const &self)
60 {
61     const std::string prefix = TF_PY_REPR_PREFIX + "Camera(";
62     const std::string indent(prefix.size(), ' ');
63     const std::string seperator = ",\n" + indent;
64 
65     // Use keyword args for clarity.
66     // Only supply some arguments when different from default.
67     std::vector<std::string> kwargs;
68     if (self.GetTransform() != GfMatrix4d(1.0))
69         kwargs.push_back("transform = " + TfPyRepr(self.GetTransform()));
70 
71     kwargs.push_back("projection = " + TfPyRepr(self.GetProjection()));
72     kwargs.push_back("horizontalAperture = " +
73                      TfPyRepr(self.GetHorizontalAperture()));
74     kwargs.push_back("verticalAperture = " +
75                      TfPyRepr(self.GetVerticalAperture()));
76     if (self.GetHorizontalApertureOffset() != 0.0)
77         kwargs.push_back("horizontalApertureOffset = " +
78                          TfPyRepr(self.GetHorizontalApertureOffset()));
79     if (self.GetVerticalApertureOffset() != 0.0)
80         kwargs.push_back("verticalApertureOffset = " +
81                          TfPyRepr(self.GetVerticalApertureOffset()));
82     kwargs.push_back("focalLength = " +
83                      TfPyRepr(self.GetFocalLength()));
84     if (self.GetClippingRange() != GfRange1f(1, 1000000))
85         kwargs.push_back("clippingRange = " +
86                          TfPyRepr(self.GetClippingRange()));
87     if (!self.GetClippingPlanes().empty())
88         kwargs.push_back("clippingPlanes = " +
89                          TfPyRepr(self.GetClippingPlanes()));
90     if (self.GetFStop() != 0.0)
91         kwargs.push_back("fStop = " + TfPyRepr(self.GetFStop()));
92     if (self.GetFocusDistance() != 0.0)
93         kwargs.push_back("focusDistance = " +
94                          TfPyRepr(self.GetFocusDistance()));
95 
96     return prefix + TfStringJoin(kwargs, seperator.c_str()) + ")";
97 }
98 
99 } // anonymous namespace
100 
101 void
wrapCamera()102 wrapCamera()
103 {
104     typedef GfCamera This;
105 
106     class_<This> c("Camera");
107 
108     scope s(c);
109 
110     TfPyWrapEnum<GfCamera::Projection>();
111     TfPyWrapEnum<GfCamera::FOVDirection>();
112 
113     c   .def(init<const This &>())
114         .def(init<const GfMatrix4d &, GfCamera::Projection,
115                   float, float, float, float, float,
116                   const GfRange1f &, const std::vector<GfVec4f> &,
117                   float, float>
118              ((args("transform") = GfMatrix4d(1.0),
119                args("projection") = GfCamera::Perspective,
120                args("horizontalAperture") =
121                                           GfCamera::DEFAULT_HORIZONTAL_APERTURE,
122                args("verticalAperture") = GfCamera::DEFAULT_VERTICAL_APERTURE,
123                args("horizontalApertureOffset") = 0.0,
124                args("verticalApertureOffset") = 0.0,
125                args("focalLength") = 50.0,
126                args("clippingRange") = GfRange1f(1, 1000000),
127                args("clippingPlanes") = std::vector<GfVec4f>(),
128                args("fStop") = 0.0,
129                args("focusDistance") = 0.0)))
130         .add_property("transform",
131                       &This::GetTransform,
132                       &This::SetTransform)
133         .add_property("projection",
134                       &This::GetProjection,
135                       &This::SetProjection)
136         .add_property("horizontalAperture",
137                       &This::GetHorizontalAperture,
138                       &This::SetHorizontalAperture)
139         .add_property("verticalAperture",
140                       &This::GetVerticalAperture,
141                       &This::SetVerticalAperture)
142         .add_property("horizontalApertureOffset",
143                       &This::GetHorizontalApertureOffset,
144                       &This::SetHorizontalApertureOffset)
145         .add_property("verticalApertureOffset",
146                       &This::GetVerticalApertureOffset,
147                       &This::SetVerticalApertureOffset)
148         .add_property("aspectRatio",
149                       &This::GetAspectRatio)
150         .add_property("focalLength",
151                       &This::GetFocalLength,
152                       &This::SetFocalLength)
153         .add_property("clippingRange",
154                       &This::GetClippingRange,
155                       &This::SetClippingRange)
156         .add_property("clippingPlanes",
157                       &_GetClippingPlanes,
158                       &This::SetClippingPlanes)
159         .add_property("frustum",
160                       &This::GetFrustum)
161         .add_property("fStop",
162                       &This::GetFStop,
163                       &This::SetFStop)
164         .add_property("focusDistance",
165                       &This::GetFocusDistance,
166                       &This::SetFocusDistance)
167         .add_property("horizontalFieldOfView",
168                       &_GetHorizontalFieldOfView)
169         .add_property("verticalFieldOfView",
170                       &_GetVerticalFieldOfView)
171         .def("GetFieldOfView",
172                       &This::GetFieldOfView)
173         .def("SetPerspectiveFromAspectRatioAndFieldOfView",
174                       &This::SetPerspectiveFromAspectRatioAndFieldOfView,
175              ( arg("aspectRatio"),
176                arg("fieldOfView"),
177                arg("direction"),
178                arg("horizontalAperture") = This::DEFAULT_HORIZONTAL_APERTURE) )
179         .def("SetOrthographicFromAspectRatioAndSize",
180                       &This::SetOrthographicFromAspectRatioAndSize,
181              ( arg("aspectRatio"),
182                arg("orthographicSize"),
183                arg("direction")))
184         .def("SetFromViewAndProjectionMatrix",
185                       &This::SetFromViewAndProjectionMatrix,
186              ( arg("viewMatrix"),
187                arg("projMatrix"),
188                arg("focalLength") = 50))
189         .setattr("APERTURE_UNIT",
190             This::APERTURE_UNIT)
191         .setattr("FOCAL_LENGTH_UNIT",
192             This::FOCAL_LENGTH_UNIT)
193         .setattr("DEFAULT_HORIZONTAL_APERTURE",
194             This::DEFAULT_HORIZONTAL_APERTURE)
195         .setattr("DEFAULT_VERTICAL_APERTURE",
196             This::DEFAULT_VERTICAL_APERTURE)
197 
198         .def(self == self)
199         .def(self != self)
200 
201         .def("__repr__", _Repr)
202             ;
203 }
204