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 #include "pxr/usdImaging/usdImaging/coneAdapter.h"
25 
26 #include "pxr/usdImaging/usdImaging/delegate.h"
27 #include "pxr/usdImaging/usdImaging/implicitSurfaceMeshUtils.h"
28 #include "pxr/usdImaging/usdImaging/indexProxy.h"
29 #include "pxr/usdImaging/usdImaging/tokens.h"
30 
31 #include "pxr/imaging/hd/mesh.h"
32 #include "pxr/imaging/hd/meshTopology.h"
33 #include "pxr/imaging/hd/perfLog.h"
34 #include "pxr/imaging/hd/tokens.h"
35 
36 #include "pxr/usd/usdGeom/cone.h"
37 #include "pxr/usd/usdGeom/xformCache.h"
38 
39 #include "pxr/base/tf/type.h"
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 
TF_REGISTRY_FUNCTION(TfType)44 TF_REGISTRY_FUNCTION(TfType)
45 {
46     typedef UsdImagingConeAdapter Adapter;
47     TfType t = TfType::Define<Adapter, TfType::Bases<Adapter::BaseAdapter> >();
48     t.SetFactory< UsdImagingPrimAdapterFactory<Adapter> >();
49 }
50 
~UsdImagingConeAdapter()51 UsdImagingConeAdapter::~UsdImagingConeAdapter()
52 {
53 }
54 
55 bool
IsSupported(UsdImagingIndexProxy const * index) const56 UsdImagingConeAdapter::IsSupported(UsdImagingIndexProxy const* index) const
57 {
58     return index->IsRprimTypeSupported(HdPrimTypeTokens->mesh);
59 }
60 
61 SdfPath
Populate(UsdPrim const & prim,UsdImagingIndexProxy * index,UsdImagingInstancerContext const * instancerContext)62 UsdImagingConeAdapter::Populate(UsdPrim const& prim,
63                             UsdImagingIndexProxy* index,
64                             UsdImagingInstancerContext const* instancerContext)
65 {
66     return _AddRprim(HdPrimTypeTokens->mesh,
67                      prim, index, GetMaterialUsdPath(prim), instancerContext);
68 }
69 
70 void
TrackVariability(UsdPrim const & prim,SdfPath const & cachePath,HdDirtyBits * timeVaryingBits,UsdImagingInstancerContext const * instancerContext) const71 UsdImagingConeAdapter::TrackVariability(UsdPrim const& prim,
72                                         SdfPath const& cachePath,
73                                         HdDirtyBits* timeVaryingBits,
74                                         UsdImagingInstancerContext const*
75                                             instancerContext) const
76 {
77     BaseAdapter::TrackVariability(
78         prim, cachePath, timeVaryingBits, instancerContext);
79 
80     // Check DirtyPoints before doing variability checks, in case we can skip
81     // any of them...
82     if ((*timeVaryingBits & HdChangeTracker::DirtyPoints) == 0) {
83         _IsVarying(prim, UsdGeomTokens->height,
84                    HdChangeTracker::DirtyPoints,
85                    UsdImagingTokens->usdVaryingPrimvar,
86                    timeVaryingBits, /*inherited*/false);
87     }
88     if ((*timeVaryingBits & HdChangeTracker::DirtyPoints) == 0) {
89         _IsVarying(prim, UsdGeomTokens->radius,
90                    HdChangeTracker::DirtyPoints,
91                    UsdImagingTokens->usdVaryingPrimvar,
92                    timeVaryingBits, /*inherited*/false);
93     }
94     if ((*timeVaryingBits & HdChangeTracker::DirtyPoints) == 0) {
95         _IsVarying(prim, UsdGeomTokens->axis,
96                    HdChangeTracker::DirtyPoints,
97                    UsdImagingTokens->usdVaryingPrimvar,
98                    timeVaryingBits, /*inherited*/false);
99     }
100 }
101 
102 HdDirtyBits
ProcessPropertyChange(UsdPrim const & prim,SdfPath const & cachePath,TfToken const & propertyName)103 UsdImagingConeAdapter::ProcessPropertyChange(UsdPrim const& prim,
104                                              SdfPath const& cachePath,
105                                              TfToken const& propertyName)
106 {
107     if (propertyName == UsdGeomTokens->height ||
108         propertyName == UsdGeomTokens->radius ||
109         propertyName == UsdGeomTokens->axis) {
110         return HdChangeTracker::DirtyPoints;
111     }
112 
113     // Allow base class to handle change processing.
114     return BaseAdapter::ProcessPropertyChange(prim, cachePath, propertyName);
115 }
116 
117 /*virtual*/
118 VtValue
GetPoints(UsdPrim const & prim,UsdTimeCode time) const119 UsdImagingConeAdapter::GetPoints(UsdPrim const& prim,
120                                  UsdTimeCode time) const
121 {
122     return GetMeshPoints(prim, time);
123 }
124 
125 static GfMatrix4d
_GetImplicitGeomScaleTransform(UsdPrim const & prim,UsdTimeCode time)126 _GetImplicitGeomScaleTransform(UsdPrim const& prim, UsdTimeCode time)
127 {
128     UsdGeomCone cone(prim);
129 
130     double height = 2.0;
131     if (!cone.GetHeightAttr().Get(&height, time)) {
132         TF_WARN("Could not evaluate double-valued height attribute on prim %s",
133             prim.GetPath().GetText());
134     }
135     double radius = 1.0;
136     if (!cone.GetRadiusAttr().Get(&radius, time)) {
137         TF_WARN("Could not evaluate double-valued radius attribute on prim %s",
138             prim.GetPath().GetText());
139     }
140     TfToken axis = UsdGeomTokens->z;
141     if (!cone.GetAxisAttr().Get(&axis, time)) {
142         TF_WARN("Could not evaluate token-valued axis attribute on prim %s",
143             prim.GetPath().GetText());
144     }
145 
146     return UsdImagingGenerateConeOrCylinderTransform(height, radius, axis);
147 }
148 
149 /*static*/
150 VtValue
GetMeshPoints(UsdPrim const & prim,UsdTimeCode time)151 UsdImagingConeAdapter::GetMeshPoints(UsdPrim const& prim,
152                                      UsdTimeCode time)
153 {
154     // Return scaled points (and not that of a unit geometry)
155     VtVec3fArray points = UsdImagingGetUnitConeMeshPoints();
156     GfMatrix4d scale = _GetImplicitGeomScaleTransform(prim, time);
157     for (GfVec3f& pt : points) {
158         pt = scale.Transform(pt);
159     }
160 
161     return VtValue(points);
162 }
163 
164 /*static*/
165 VtValue
GetMeshTopology()166 UsdImagingConeAdapter::GetMeshTopology()
167 {
168     // Topology is constant and identical for all cones.
169     return VtValue(HdMeshTopology(UsdImagingGetUnitConeMeshTopology()));
170 }
171 
172 /*virtual*/
173 VtValue
GetTopology(UsdPrim const & prim,SdfPath const & cachePath,UsdTimeCode time) const174 UsdImagingConeAdapter::GetTopology(UsdPrim const& prim,
175                                    SdfPath const& cachePath,
176                                    UsdTimeCode time) const
177 {
178     TRACE_FUNCTION();
179     HF_MALLOC_TAG_FUNCTION();
180     return GetMeshTopology();
181 }
182 
183 PXR_NAMESPACE_CLOSE_SCOPE
184 
185