VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
picker.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4//
5#pragma once
6
7#include <optional>
8#include <unordered_map>
9#include <vector>
10
11#include "core/math/vector3.h"
13
14namespace visutwin::canvas
15{
16 class CameraComponent;
17 class Engine;
18 class MeshInstance;
19 class Scene;
20
21 class Picker
22 {
23 public:
24 Picker(Engine* app, int width, int height, bool depth = false);
25
26 void resize(int width, int height);
27
28 void prepare(CameraComponent* camera, Scene* scene, const std::vector<int>& layers = {});
29
30 std::vector<MeshInstance*> getSelection(int x, int y, int width = 1, int height = 1) const;
31 MeshInstance* getSelectionSingle(int x, int y) const;
32 std::optional<Vector3> getWorldPoint(int x, int y) const;
33
34 int width() const { return _width; }
35 int height() const { return _height; }
36
37 private:
38 struct Candidate
39 {
40 MeshInstance* meshInstance = nullptr;
41 float minX = 0.0f;
42 float minY = 0.0f;
43 float maxX = 0.0f;
44 float maxY = 0.0f;
45 float distanceSq = 0.0f;
46 BoundingSphere bounds;
47 };
48
49 struct Rect
50 {
51 int x = 0;
52 int y = 0;
53 int width = 1;
54 int height = 1;
55 };
56
57 bool projectPoint(const Vector3& worldPos, float& outX, float& outY) const;
58 bool buildRay(int x, int y, Vector3& outOrigin, Vector3& outDirection) const;
59 Rect sanitizeRect(int x, int y, int width, int height) const;
60 bool isLayerAllowed(const std::vector<int>& objectLayers) const;
61
62 Engine* _app = nullptr;
63 CameraComponent* _camera = nullptr;
64 Scene* _scene = nullptr;
65 bool _depth = false;
66 int _width = 1;
67 int _height = 1;
68 std::vector<int> _layers;
69 std::vector<Candidate> _candidates;
70 std::unordered_map<MeshInstance*, size_t> _candidateIndex;
71 };
72}
Bounding sphere defined by center and radius for intersection and containment tests.
Central application orchestrator managing scenes, rendering, input, and resource loading.
Definition engine.h:38
Renderable instance of a Mesh with its own material, transform node, and optional GPU instancing.
void prepare(CameraComponent *camera, Scene *scene, const std::vector< int > &layers={})
Definition picker.cpp:36
MeshInstance * getSelectionSingle(int x, int y) const
Definition picker.cpp:157
Picker(Engine *app, int width, int height, bool depth=false)
Definition picker.cpp:24
int width() const
Definition picker.h:34
std::vector< MeshInstance * > getSelection(int x, int y, int width=1, int height=1) const
Definition picker.cpp:118
std::optional< Vector3 > getWorldPoint(int x, int y) const
Definition picker.cpp:163
void resize(int width, int height)
Definition picker.cpp:30
int height() const
Definition picker.h:35
Container for the scene graph, lighting environment, fog, skybox, and layer composition.
Definition scene.h:29