vsg  1.1.0
VulkanSceneGraph library
LineSegmentIntersector.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2020 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/app/Camera.h>
16 #include <vsg/utils/Intersector.h>
17 
18 namespace vsg
19 {
20 
22  struct IndexRatio
23  {
24  uint32_t index;
25  double ratio;
26  };
27 
28  using IndexRatios = std::vector<IndexRatio>;
29 
31  class VSG_DECLSPEC LineSegmentIntersector : public Inherit<Intersector, LineSegmentIntersector>
32  {
33  public:
34  LineSegmentIntersector(const dvec3& s, const dvec3& e, ref_ptr<ArrayState> initialArrayData = {});
35  LineSegmentIntersector(const Camera& camera, int32_t x, int32_t y, ref_ptr<ArrayState> initialArrayData = {});
36 
37  class VSG_DECLSPEC Intersection : public Inherit<Object, Intersection>
38  {
39  public:
40  Intersection() {}
41  Intersection(const dvec3& in_localIntersection, const dvec3& in_worldIntersection, double in_ratio, const dmat4& in_localToWorld, const NodePath& in_nodePath, const DataList& in_arrays, const IndexRatios& in_indexRatios, uint32_t in_instanceIndex);
42 
43  dvec3 localIntersection;
44  dvec3 worldIntersection;
45  double ratio = 0.0;
46 
47  dmat4 localToWorld;
48  NodePath nodePath;
49  DataList arrays;
50  IndexRatios indexRatios;
51  uint32_t instanceIndex = 0;
52 
53  // return true if Intersection is valid
54  operator bool() const { return !nodePath.empty(); }
55  };
56 
57  using Intersections = std::vector<ref_ptr<Intersection>>;
58  Intersections intersections;
59 
60  ref_ptr<Intersection> add(const dvec3& coord, double ratio, const IndexRatios& indexRatios, uint32_t instanceIndex);
61 
62  void pushTransform(const Transform& transform) override;
63  void popTransform() override;
64 
66  bool intersects(const dsphere& bs) override;
67 
68  bool intersectDraw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount) override;
69  bool intersectDrawIndexed(uint32_t firstIndex, uint32_t indexCount, uint32_t firstInstance, uint32_t instanceCount) override;
70 
71  protected:
72  struct LineSegment
73  {
74  dvec3 start;
75  dvec3 end;
76  };
77 
78  std::vector<LineSegment> _lineSegmentStack;
79  };
80  VSG_type_name(vsg::LineSegmentIntersector);
81 
82 } // namespace vsg
Definition: Camera.h:27
Definition: Inherit.h:28
Definition: LineSegmentIntersector.h:38
LineSegmentIntersector is an Intersector subclass that provides support for computing intersections b...
Definition: LineSegmentIntersector.h:32
void pushTransform(const Transform &transform) override
clone and transform this Intersector to provide a new Intersector in local coordinates
bool intersectDraw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount) override
intersect with a vkCmdDraw primitive
bool intersectDrawIndexed(uint32_t firstIndex, uint32_t indexCount, uint32_t firstInstance, uint32_t instanceCount) override
intersect with a vkCmdDrawIndexed primitive
bool intersects(const dsphere &bs) override
check for intersection with sphere
Transform node is a pure virtual base class for positioning/scaling/rotating subgraphs.
Definition: Transform.h:22
Definition: ref_ptr.h:22
IndexRatio is a pair of index and ratio used to specify the baricentric coords of primitives that hav...
Definition: LineSegmentIntersector.h:23
Definition: LineSegmentIntersector.h:73