Overte C++ Documentation
RayPick.h
1 //
2 // Created by Sam Gondelman 7/11/2017
3 // Copyright 2017 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_RayPick_h
9 #define hifi_RayPick_h
10 
11 #include <RegisteredMetaTypes.h>
12 #include <Pick.h>
13 
14 class EntityItemID;
15 
16 class RayPickResult : public PickResult {
17 public:
18  RayPickResult() {}
19  RayPickResult(const QVariantMap& pickVariant) : PickResult(pickVariant) {}
20  RayPickResult(const IntersectionType type, const QUuid& objectID, float distance, const glm::vec3& intersection, const PickRay& searchRay, const glm::vec3& surfaceNormal = glm::vec3(NAN), const QVariantMap& extraInfo = QVariantMap()) :
21  PickResult(searchRay.toVariantMap()), extraInfo(extraInfo), objectID(objectID), intersection(intersection), surfaceNormal(surfaceNormal), type(type), distance(distance), intersects(type != NONE) {
22  }
23 
24  RayPickResult(const RayPickResult& rayPickResult) : PickResult(rayPickResult.pickVariant) {
25  type = rayPickResult.type;
26  intersects = rayPickResult.intersects;
27  objectID = rayPickResult.objectID;
28  distance = rayPickResult.distance;
29  intersection = rayPickResult.intersection;
30  surfaceNormal = rayPickResult.surfaceNormal;
31  extraInfo = rayPickResult.extraInfo;
32  }
33 
34  QVariantMap extraInfo;
35  QUuid objectID;
36  glm::vec3 intersection { NAN };
37  glm::vec3 surfaceNormal { NAN };
38  IntersectionType type { NONE };
39  float distance { FLT_MAX };
40  bool intersects { false };
41 
42  /*@jsdoc
43  * An intersection result for a ray pick.
44  *
45  * @typedef {object} RayPickResult
46  * @property {IntersectionType} type - The intersection type.
47  * @property {boolean} intersects - <code>true</code> if there's a valid intersection, <code>false</code> if there isn't.
48  * @property {Uuid} objectID - The ID of the intersected object. <code>null</code> for HUD or invalid intersections.
49  * @property {number} distance - The distance from the ray origin to the intersection point.
50  * @property {Vec3} intersection - The intersection point in world coordinates.
51  * @property {Vec3} surfaceNormal - The surface normal at the intersected point. All <code>NaN</code>s if <code>type ==
52  * Picks.INTERSECTED_HUD</code>.
53  * @property {SubmeshIntersection} extraInfo - Additional intersection details for model objects, otherwise
54  * <code>{ }</code>.
55  * @property {PickRay} searchRay - The pick ray that was used. Valid even if there is no intersection.
56  */
57  virtual QVariantMap toVariantMap() const override {
58  QVariantMap toReturn;
59  toReturn["type"] = type;
60  toReturn["intersects"] = intersects;
61  toReturn["objectID"] = objectID;
62  toReturn["distance"] = distance;
63  toReturn["intersection"] = vec3toVariant(intersection);
64  toReturn["surfaceNormal"] = vec3toVariant(surfaceNormal);
65  toReturn["searchRay"] = PickResult::toVariantMap();
66  toReturn["extraInfo"] = extraInfo;
67  return toReturn;
68  }
69 
70  bool doesIntersect() const override { return intersects; }
71  bool checkOrFilterAgainstMaxDistance(float maxDistance) override { return distance < maxDistance; }
72 
73  PickResultPointer compareAndProcessNewResult(const PickResultPointer& newRes) override {
74  auto newRayRes = std::static_pointer_cast<RayPickResult>(newRes);
75  if (newRayRes->distance < distance) {
76  return std::make_shared<RayPickResult>(*newRayRes);
77  } else {
78  return std::make_shared<RayPickResult>(*this);
79  }
80  }
81 
82 };
83 
84 class RayPick : public Pick<PickRay> {
85 
86 public:
87  RayPick(glm::vec3 position, glm::vec3 direction, const PickFilter& filter, float maxDistance, bool enabled) :
88  Pick(PickRay(position, direction), filter, maxDistance, enabled) {
89  }
90 
91  PickType getType() const override { return PickType::Ray; }
92 
93  PickRay getMathematicalPick() const override;
94 
95  PickResultPointer getDefaultResult(const QVariantMap& pickVariant) const override { return std::make_shared<RayPickResult>(pickVariant); }
96  PickResultPointer getEntityIntersection(const PickRay& pick) override;
97  PickResultPointer getAvatarIntersection(const PickRay& pick) override;
98  PickResultPointer getHUDIntersection(const PickRay& pick) override;
99  Transform getResultTransform() const override;
100 
101  // These are helper functions for projecting and intersecting rays
102  static glm::vec3 intersectRayWithEntityXYPlane(const QUuid& entityID, const glm::vec3& origin, const glm::vec3& direction);
103  static glm::vec2 projectOntoEntityXYPlane(const QUuid& entityID, const glm::vec3& worldPos, bool unNormalized = true);
104 
105  static glm::vec2 projectOntoXYPlane(const glm::vec3& worldPos, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& dimensions, const glm::vec3& registrationPoint, bool unNormalized);
106  static glm::vec2 projectOntoXZPlane(const glm::vec3& worldPos, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& dimensions, const glm::vec3& registrationPoint, bool unNoemalized);
107 
108 private:
109  static glm::vec3 intersectRayWithXYPlane(const glm::vec3& origin, const glm::vec3& direction, const glm::vec3& point, const glm::quat& rotation, const glm::vec3& registration);
110 };
111 
112 #endif // hifi_RayPick_h
Abstract ID for editing model items. Used in EntityItem JS API.
Definition: EntityItemID.h:28