• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef CORE_UTIL_PICKING_H
16 #define CORE_UTIL_PICKING_H
17 
18 #include <3d/util/intf_picking.h>
19 #include <core/namespace.h>
20 
21 CORE_BEGIN_NAMESPACE()
22 class IEcs;
23 class IEngine;
24 CORE_END_NAMESPACE()
25 
26 CORE3D_BEGIN_NAMESPACE()
27 class IJointMatricesComponentManager;
28 class IMeshComponentManager;
29 class ITransformComponentManager;
30 class IRenderMeshComponentManager;
31 class IWorldMatrixComponentManager;
32 struct CameraComponent;
33 struct MeshComponent;
34 
35 class Picking : public IPicking {
36 public:
37     Picking() = default;
38     ~Picking() override = default;
39     BASE_NS::Math::Vec3 ScreenToWorld(
40         CORE_NS::IEcs const& ecs, CORE_NS::Entity cameraEntity, BASE_NS::Math::Vec3 screenCoordinate) const override;
41 
42     BASE_NS::Math::Vec3 WorldToScreen(
43         CORE_NS::IEcs const& ecs, CORE_NS::Entity cameraEntity, BASE_NS::Math::Vec3 worldCoordinate) const override;
44 
45     BASE_NS::vector<RayCastResult> RayCast(CORE_NS::IEcs const& ecs, const BASE_NS::Math::Vec3& start,
46         const BASE_NS::Math::Vec3& direction) const override;
47     BASE_NS::vector<RayCastResult> RayCastFromCamera(
48         CORE_NS::IEcs const& ecs, CORE_NS::Entity camera, const BASE_NS::Math::Vec2& screenPos) const override;
49 
50     MinAndMax GetWorldAABB(const BASE_NS::Math::Mat4X4& world, const BASE_NS::Math::Vec3& aabbMin,
51         const BASE_NS::Math::Vec3& aabbMax) const override;
52 
53     MinAndMax GetWorldMatrixComponentAABB(CORE_NS::Entity entity, bool isRecursive, CORE_NS::IEcs& ecs) const override;
54 
55     MinAndMax GetTransformComponentAABB(CORE_NS::Entity entity, bool isRecursive, CORE_NS::IEcs& ecs) const override;
56 
57     // IInterface
58     const IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
59     IInterface* GetInterface(const BASE_NS::Uid& uid) override;
60 
61     void Ref() override;
62     void Unref() override;
63 
64 protected:
65     BASE_NS::Math::Mat4X4 GetCameraViewToProjectionMatrix(const CameraComponent& cameraComponent) const;
66 
67     constexpr bool IntersectAabb(const BASE_NS::Math::Vec3 aabbMin, const BASE_NS::Math::Vec3 aabbMax,
68         const BASE_NS::Math::Vec3 start, const BASE_NS::Math::Vec3 invDirection) const;
69 
70     // Calculates AABB using WorldMatrixComponent.
71     void UpdateRecursiveAABB(const IRenderMeshComponentManager& renderMeshComponentManager,
72         const IWorldMatrixComponentManager& worldMatrixComponentManager,
73         const IJointMatricesComponentManager& jointMatricesComponentManager, const IMeshComponentManager& meshManager,
74         const ISceneNode& sceneNode, bool isRecursive, MinAndMax& mamInOut) const;
75 
76     // Calculates AABB using TransformComponent.
77     void UpdateRecursiveAABB(const IRenderMeshComponentManager& renderMeshComponentManager,
78         const ITransformComponentManager& transformComponentManager, const IMeshComponentManager& meshManager,
79         const ISceneNode& sceneNode, const BASE_NS::Math::Mat4X4& parentWorld, bool isRecursive,
80         MinAndMax& mamInOut) const;
81 
82     RayCastResult HitTestNode(ISceneNode& node, const MeshComponent& mesh, const BASE_NS::Math::Mat4X4& matrix,
83         const BASE_NS::Math::Vec3& start, const BASE_NS::Math::Vec3& invDir) const;
84 };
85 CORE3D_END_NAMESPACE()
86 
87 #endif // CORE_UTIL_PICKING_H