Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/vsg/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/nodes/InstanceDraw.h>
#include <vsg/nodes/InstanceDrawIndexed.h>
#include <vsg/nodes/InstanceNode.h>
#include <vsg/nodes/IntersectionProxy.h>
#include <vsg/nodes/InstrumentationNode.h>
#include <vsg/nodes/LOD.h>
#include <vsg/nodes/Layer.h>
Expand Down Expand Up @@ -265,6 +266,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/vk/vulkan.h>

// Input/Output header files
#include <vsg/io/ApplyVisitorReader.h>
#include <vsg/io/AsciiInput.h>
#include <vsg/io/AsciiOutput.h>
#include <vsg/io/BinaryInput.h>
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/core/ConstVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace vsg
class InstanceNode;
class InstanceDraw;
class InstanceDrawIndexed;
class IntersectionProxy;

// forward declare text classes
class Text;
Expand Down Expand Up @@ -360,6 +361,7 @@ namespace vsg
virtual void apply(const InstanceNode&);
virtual void apply(const InstanceDraw&);
virtual void apply(const InstanceDrawIndexed&);
virtual void apply(const IntersectionProxy&);

// text
virtual void apply(const Text&);
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/core/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace vsg
class InstanceNode;
class InstanceDraw;
class InstanceDrawIndexed;
class IntersectionProxy;

// forward declare text classes
class Text;
Expand Down Expand Up @@ -360,6 +361,7 @@ namespace vsg
virtual void apply(InstanceNode&);
virtual void apply(InstanceDraw&);
virtual void apply(InstanceDrawIndexed&);
virtual void apply(IntersectionProxy&);

// text
virtual void apply(Text&);
Expand Down
48 changes: 48 additions & 0 deletions include/vsg/io/ApplyVisitorReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

/* <editor-fold desc="MIT License">
Copyright(c) 2025 Chris Djali
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
</editor-fold> */

#include <vsg/io/ReaderWriter.h>

namespace vsg
{
/// Class to wrap a ReaderWriter and apply a Visitor to everything it loads
class VSG_DECLSPEC ApplyVisitorReader : public Inherit<ReaderWriter, ApplyVisitorReader>
{
public:
ApplyVisitorReader(vsg::ref_ptr<ReaderWriter> in_child, vsg::ref_ptr<Visitor> in_visitor);
ApplyVisitorReader(const ApplyVisitorReader& rhs, const CopyOp& copyop = {});

vsg::ref_ptr<ReaderWriter> child;
vsg::ref_ptr<Visitor> visitor;

void read(Input& input) override;
void write(Output& output) const override;

vsg::ref_ptr<vsg::Object> read(const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options = {}) const override;
vsg::ref_ptr<vsg::Object> read(std::istream& fin, vsg::ref_ptr<const vsg::Options> options = {}) const override;
vsg::ref_ptr<vsg::Object> read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options = {}) const override;

bool write(const vsg::Object* object, const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options = {}) const override;
bool write(const vsg::Object* object, std::ostream& fout, vsg::ref_ptr<const vsg::Options> options = {}) const override;

bool readOptions(vsg::Options& options, vsg::CommandLine& arguments) const override;

bool getFeatures(Features& features) const override;

protected:
mutable std::mutex _visitorMutex;
};
VSG_type_name(vsg::ApplyVisitorReader);

} // namespace vsg
120 changes: 120 additions & 0 deletions include/vsg/nodes/IntersectionProxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#pragma once

/* <editor-fold desc="MIT License">
Copyright(c) 2025 Chris Djali
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
</editor-fold> */

#include <vsg/nodes/Node.h>
#include <vsg/state/ArrayState.h>

namespace vsg
{
class LineSegmentIntersector;

/** IntersectionProxy wraps a node with a BVH to accelerate vsg::Intersector operations */
class VSG_DECLSPEC IntersectionProxy : public Inherit<Node, IntersectionProxy>
{
public:
IntersectionProxy(Node* in_original);
IntersectionProxy(const IntersectionProxy& rhs, const CopyOp& copyop = {});

void rebuild(ArrayState& arrayState);

bool valid() const;

void intersect(LineSegmentIntersector& lineSegmentIntersector) const;

public:
ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return IntersectionProxy::create(*this, copyop); }
int compare(const Object& rhs) const override;

template<class N, class V>
static void t_traverse(N& node, V& visitor)
{
node.original->accept(visitor);
}

void traverse(Visitor& visitor) override { t_traverse(*this, visitor); }
void traverse(ConstVisitor& visitor) const override { t_traverse(*this, visitor); }
void traverse(RecordTraversal& visitor) const override { t_traverse(*this, visitor); }

void read(Input& input) override;
void write(Output& output) const override;

ref_ptr<Node> original;

protected:
virtual ~IntersectionProxy();

struct Triangle
{
vec3 vertex0;
vec3 vertex1;
vec3 vertex2;
};
static constexpr size_t trisPerLeaf = 16;
struct Leaf
{
std::array<Triangle, trisPerLeaf> tris;
};
struct NodeRef
{
enum NodeType
{
LEAF,
INTERNAL,
INVALID
};
NodeType type;
uint32_t index;
};
struct InternalNode
{
std::array<std::pair<box, NodeRef>, 2> children;
};

std::vector<InternalNode, allocator_affinity_data<InternalNode>> internalNodes;
std::vector<Leaf, allocator_affinity_data<Leaf>> leaves;
box bounds;
NodeRef boundingVolumeHeirarchy;

struct TriangleMetadata
{
uint32_t index0;
uint32_t index1;
uint32_t index2;
uint32_t instance;
};
struct LeafMetadata
{
std::array<TriangleMetadata, trisPerLeaf> tris;
};

std::vector<LeafMetadata, allocator_affinity_data<LeafMetadata>> leafMetadata;
};
VSG_type_name(vsg::IntersectionProxy)

class VSG_DECLSPEC IntersectionOptimizeVisitor : public Inherit<Visitor, IntersectionOptimizeVisitor>
{
public:
using ArrayStateStack = std::vector<ref_ptr<ArrayState>>;

IntersectionOptimizeVisitor(ref_ptr<ArrayState> initialArrayState = {});

void apply(Node& node) override;

void apply(StateGroup& stateGroup) override;

protected:
ArrayStateStack arrayStateStack;
};
VSG_type_name(vsg::IntersectionOptimizeVisitor)
} // namespace vsg
2 changes: 2 additions & 0 deletions include/vsg/utils/Intersector.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace vsg

Intersector(ref_ptr<ArrayState> initialArrayState = {});

virtual void reset(ref_ptr<ArrayState> initialArrayState = {});

//
// handle traverse of the scene graph
//
Expand Down
11 changes: 10 additions & 1 deletion include/vsg/utils/LineSegmentIntersector.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ namespace vsg
LineSegmentIntersector(const dvec3& s, const dvec3& e, ref_ptr<ArrayState> initialArrayData = {});
LineSegmentIntersector(const Camera& camera, int32_t x, int32_t y, ref_ptr<ArrayState> initialArrayData = {});

void reset(ref_ptr<ArrayState> initialArrayData = {}) override;
virtual void reset(const dvec3& s, const dvec3& e, ref_ptr<ArrayState> initialArrayData = {});
virtual void reset(const Camera& camera, int32_t x, int32_t y, ref_ptr<ArrayState> initialArrayData = {});

class VSG_DECLSPEC Intersection : public Inherit<Object, Intersection>
{
public:
Expand All @@ -59,6 +63,8 @@ namespace vsg

ref_ptr<Intersection> add(const dvec3& coord, double ratio, const IndexRatios& indexRatios, uint32_t instanceIndex);

void apply(const IntersectionProxy& intersectionproxy) override;

void pushTransform(const Transform& transform) override;
void popTransform() override;

Expand All @@ -68,13 +74,16 @@ namespace vsg
bool intersectDraw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount) override;
bool intersectDrawIndexed(uint32_t firstIndex, uint32_t indexCount, uint32_t firstInstance, uint32_t instanceCount) override;

protected:
struct LineSegment
{
dvec3 start;
dvec3 end;
};

const LineSegment& lineSegment() { return _lineSegmentStack.back(); }

protected:

std::vector<LineSegment> _lineSegmentStack;
};
VSG_type_name(vsg::LineSegmentIntersector);
Expand Down
4 changes: 4 additions & 0 deletions include/vsg/utils/PolytopeIntersector.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace vsg
/// create intersector for a polytope with window space dimensions, projected into world coords using the Camera's projection and view matrices.
PolytopeIntersector(const Camera& camera, double xMin, double yMin, double xMax, double yMax, ref_ptr<ArrayState> initialArrayData = {});

void reset(ref_ptr<ArrayState> initialArrayData = {}) override;
virtual void reset(const Polytope& in_polytope, ref_ptr<ArrayState> initialArrayData = {});
virtual void reset(const Camera& camera, double xMin, double yMin, double xMax, double yMax, ref_ptr<ArrayState> initialArrayData = {});

class VSG_DECLSPEC Intersection : public Inherit<Object, Intersection>
{
public:
Expand Down
2 changes: 2 additions & 0 deletions src/vsg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ set(SOURCES
nodes/InstanceNode.cpp
nodes/InstanceDraw.cpp
nodes/InstanceDrawIndexed.cpp
nodes/IntersectionProxy.cpp

lighting/Light.cpp
lighting/AmbientLight.cpp
Expand Down Expand Up @@ -153,6 +154,7 @@ set(SOURCES
io/read.cpp
io/write.cpp
io/mem_stream.cpp
io/ApplyVisitorReader.cpp

text/CpuLayoutTechnique.cpp
text/GpuLayoutTechnique.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/vsg/core/ConstVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ void ConstVisitor::apply(const InstanceDrawIndexed& value)
{
apply(static_cast<const Command&>(value));
}
void ConstVisitor::apply(const IntersectionProxy& value)
{
apply(static_cast<const Node&>(value));
}

////////////////////////////////////////////////////////////////////////////////
//
Expand Down
4 changes: 4 additions & 0 deletions src/vsg/core/Visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ void Visitor::apply(InstanceDrawIndexed& value)
{
apply(static_cast<Command&>(value));
}
void Visitor::apply(IntersectionProxy& value)
{
apply(static_cast<Node&>(value));
}

////////////////////////////////////////////////////////////////////////////////
//
Expand Down
Loading
Loading