Overte C++ Documentation
NestableTransformNode.h
1 //
2 // Created by Sabrina Shanman 2018/08/14
3 // Copyright 2018 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #ifndef hifi_NestableTransformNode_h
9 #define hifi_NestableTransformNode_h
10 
11 #include "TransformNode.h"
12 
13 #include "SpatiallyNestable.h"
14 
15 #include "RegisteredMetaTypes.h"
16 
17 template <typename T>
18 class BaseNestableTransformNode : public TransformNode {
19 public:
20  BaseNestableTransformNode(std::weak_ptr<T> spatiallyNestable, int jointIndex) :
21  _spatiallyNestable(spatiallyNestable),
22  _jointIndex(jointIndex) {
23  auto nestablePointer = _spatiallyNestable.lock();
24  if (nestablePointer) {
25  if (nestablePointer->getNestableType() != NestableType::Avatar) {
26  glm::vec3 nestableDimensions = getActualScale(nestablePointer);
27  _baseScale = glm::max(glm::vec3(0.001f), nestableDimensions);
28  }
29  }
30  }
31 
32  Transform getTransform() override {
33  std::shared_ptr<T> nestable = _spatiallyNestable.lock();
34  if (!nestable) {
35  return Transform();
36  }
37 
38  bool success;
39  Transform jointWorldTransform = nestable->getJointTransform(_jointIndex, success);
40 
41  if (!success) {
42  return Transform();
43  }
44 
45  jointWorldTransform.setScale(getActualScale(nestable) / _baseScale);
46 
47  return jointWorldTransform;
48  }
49 
50  QVariantMap toVariantMap() const override {
51  QVariantMap map;
52 
53  auto nestable = _spatiallyNestable.lock();
54  if (nestable) {
55  map["parentID"] = nestable->getID();
56  map["parentJointIndex"] = _jointIndex;
57  map["baseParentScale"] = vec3toVariant(_baseScale);
58  }
59 
60  return map;
61  }
62 
63  glm::vec3 getActualScale(const std::shared_ptr<T>& nestablePointer) const;
64 
65 protected:
66  std::weak_ptr<T> _spatiallyNestable;
67  int _jointIndex;
68  glm::vec3 _baseScale { 1.0f };
69 };
70 
71 class NestableTransformNode : public BaseNestableTransformNode<SpatiallyNestable> {
72 public:
73  NestableTransformNode(std::weak_ptr<SpatiallyNestable> spatiallyNestable, int jointIndex) : BaseNestableTransformNode(spatiallyNestable, jointIndex) {};
74 };
75 
76 #endif // hifi_NestableTransformNode_h