Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Dev #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 21 additions & 39 deletions source/Acceleration/BVH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

#include "BVH.h"

#include <Core/Math.h>
#include <Geometry/Face.h>
#include <Foundation/Math.h>
#include <Foundation/Prim.h>

#include <algorithm>
#include <memory>

using Range = BVHNode::Range;
using SplitMethod = BVHAccelerator::SplitMethod;
using PrimCache = BVHAccelerator::PrimCache;

namespace
{
Expand Down Expand Up @@ -60,33 +60,12 @@ template <typename T, Index S = 256> class Stack
Index size{};
};

struct PrimCache
{
PrimCache(const Prim& prim, Index i)
: index(i)
, centroid(prim.Centroid())
, bbox(prim.Bounds())
{
}

PrimCache(const PrimCache&) = default;
PrimCache(PrimCache&&) = default;

PrimCache& operator=(const PrimCache&) = default;
PrimCache& operator=(PrimCache&&) = default;

Index index;
Vector3 centroid;
AABBox bbox;
};

std::vector<PrimCache> create_prim_cache(const std::vector<const Prim*>& prims, AABBox& centroid_bbox)
{
std::vector<PrimCache> entries;
entries.reserve(prims.size());

// fill entries and compute box

for (auto it = prims.begin(); it != prims.end(); ++it)
{
const Prim* prim = *it;
Expand Down Expand Up @@ -118,7 +97,9 @@ BVHNode::BVHNode(Range r, AABBox b)
{
}

BVHAccelerator::BVHAccelerator(const std::vector<const Prim *>& prims, Index max_prims)
BVHAccelerator::BVHAccelerator(const std::vector<const Prim *>& prims,
Index max_prims,
SplitMethod split_method)
{
auto num_prims = static_cast<Index>(prims.size());
if (num_prims == 0)
Expand All @@ -133,10 +114,10 @@ BVHAccelerator::BVHAccelerator(const std::vector<const Prim *>& prims, Index max

// create root bvh node
AABBox centroid_bbox;
std::vector<PrimCache> entries = create_prim_cache(prims, centroid_bbox);
entries_ = create_prim_cache(prims, centroid_bbox);
nodes_.emplace_back(Range{ 0, num_prims }, centroid_bbox);

Stack<BVHNode*, 256> stack;
Stack<BVHNode*, 512> stack;
stack.Push(&nodes_.back());

while (!stack.Empty())
Expand All @@ -161,13 +142,13 @@ BVHAccelerator::BVHAccelerator(const std::vector<const Prim *>& prims, Index max
const Range& node_range = node->GetRange();
const Index num_node_entries = node->NumEntries();

// BVHNode leaf can group few entries, compute union for all of them from the range
// BVHNode leaf can group few entries_, compute union for all of them from the range
if (num_node_entries <= max_prims)
{
AABBox leaf_bbox;
for (auto i = node_range.start; i < node_range.end; ++i)
{
leaf_bbox = ::Union(leaf_bbox, entries[i].bbox);
leaf_bbox = ::Union(leaf_bbox, entries_[i].bbox);
}
node->GetBBox() = leaf_bbox;
stack.Pop();
Expand All @@ -185,32 +166,33 @@ BVHAccelerator::BVHAccelerator(const std::vector<const Prim *>& prims, Index max

// partial sort along longest extent
const Index median_index = node_range.start + static_cast<Index>(0.5 * num_node_entries);
std::nth_element(entries.begin() + node_range.start,
entries.begin() + median_index,
entries.begin() + node_range.end,
centroid_cmp);
std::nth_element(entries_.begin() + node_range.start, entries_.begin() + median_index,
entries_.begin() + node_range.end,
centroid_cmp);

// split to L - R
Range lr_range[2] = { { node_range.start, median_index }, { median_index, node_range.end } };

// median for box split
AABBox lr_bbox[2];
const PrimCache& median = entries[median_index];
const PrimCache& median = entries_[median_index];
split_bbox(bbox, median.centroid, max_extent, lr_bbox);

// append children
for (Index c{}; c < 2; ++c)
{
//auto new_size = lr_range[c].end - lr_range[c].start;

nodes_.emplace_back(lr_range[c], lr_bbox[c]);
BVHNode* child = &nodes_.back();
node->SetChild(c, child);

// put on top

stack.Push(child);
}
}
}

// move all nodes to
BVHAccelerator::PrimCache::PrimCache(const Prim& prim, Index i)
: index(i)
, centroid(prim.Centroid())
, bbox(prim.Bounds())
{
}
44 changes: 29 additions & 15 deletions source/Acceleration/BVH.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//
// MIT License
//
//
// Copyright (c) 2019 Piotr Barejko
//
//
// 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
Expand All @@ -25,13 +25,11 @@
#ifndef BVH_H
#define BVH_H

#include <Core/AABBox.h>
#include <Foundation/AABBox.h>

#include <vector>
#include <memory>

class Prim;
class Face;

///
/// BVHNode
Expand All @@ -45,10 +43,8 @@ class BVHNode
Index end{INDEX_INVALID};
};

// TODO this has to be replaced by constructors that
// compute those features
BVHNode(Range r, AABBox b);

BVHNode(const BVHNode&) = default;
BVHNode(BVHNode&&) = default;

Expand Down Expand Up @@ -83,12 +79,27 @@ class BVHAccelerator
public:
enum class SplitMethod
{
MidPoint,
Median,
//SurfaceAreaHeuristic
};

BVHAccelerator(const std::vector<const Prim*>& prims, Index max_prims = 1);
struct PrimCache
{
PrimCache(const Prim& prim, Index i);

PrimCache(const PrimCache&) = default;
PrimCache(PrimCache&&) = default;

PrimCache& operator=(const PrimCache&) = default;
PrimCache& operator=(PrimCache&&) = default;

Index index;
Vector3 centroid;
AABBox bbox;
};

explicit BVHAccelerator(const std::vector<const Prim*>& prims,
Index max_prims = 1,
SplitMethod split_method = SplitMethod::Median);

BVHAccelerator(const BVHAccelerator&) = delete;
BVHAccelerator(BVHAccelerator&&) = delete;
Expand All @@ -99,12 +110,15 @@ class BVHAccelerator
Index NumNodes() const { return nodes_.size(); }
const BVHNode* Root() const { return nodes_.empty() ? nullptr : &nodes_.front(); }

Index MemoryUsage() const
const std::vector<PrimCache>& GetEntries() const
{
return sizeof(BVHNode) * nodes_.size() + sizeof(BVHAccelerator);
return entries_;
}

Index MemoryUsage() const { return sizeof(BVHNode) * nodes_.size() + sizeof(BVHAccelerator); }

private:
std::vector<PrimCache> entries_;
std::vector<BVHNode> nodes_;
};

Expand Down
2 changes: 1 addition & 1 deletion source/Acceleration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ target_sources(${PROJECT_NAME}

target_link_libraries(${PROJECT_NAME}
PUBLIC
Geometry
Foundation
)
4 changes: 2 additions & 2 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(Utils)
add_subdirectory(Core)
add_subdirectory(Foundation)
add_subdirectory(Geometry)
add_subdirectory(Acceleration)
add_subdirectory(Serialization)
Expand All @@ -16,7 +16,7 @@ target_compile_definitions(nth_element_vs_sort

target_link_libraries(nth_element_vs_sort
PRIVATE
Core
Foundation
Geometry
Serialization
Acceleration
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(Core CXX)
project(Foundation CXX)

add_library(${PROJECT_NAME} INTERFACE)

Expand Down
10 changes: 5 additions & 5 deletions source/Core/Math.h → source/Foundation/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ constexpr Real Clamp(Real val, Real mn, Real mx)

inline Vector3 Min(const Vector3 & l, const Vector3 & r)
{
return { std::min(l.x(), r.x()),
std::min(l.y(), r.y()),
std::min(l.z(), r.z()) };
return {std::min(l.x(), r.x()),
std::min(l.y(), r.y()),
std::min(l.z(), r.z())};
}

inline Vector3 Max(const Vector3 & l, const Vector3 & r)
Expand All @@ -49,14 +49,14 @@ inline Vector3 Max(const Vector3 & l, const Vector3 & r)
std::max(l.z(), r.z())};
}

inline Vector3 Clamp(const Vector3 & v, const Vector3 & mn, const Vector3 & mx)
inline Vector3 Clamp(const Vector3& v, const Vector3& mn, const Vector3& mx)
{
return {::Clamp(v.x(), mn.x(), mx.x()),
::Clamp(v.y(), mn.y(), mx.y()),
::Clamp(v.z(), mn.z(), mx.z())};
}

inline AABBox Union(const AABBox& l, const Vector3 & r)
inline AABBox Union(const AABBox& l, const Vector3& r)
{
return AABBox{::Min(l.MinPoint(), r),
::Max(l.MaxPoint(), r)};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion source/Geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ target_sources(${PROJECT_NAME}

target_link_libraries(${PROJECT_NAME}
PUBLIC
Core
Foundation
)
22 changes: 11 additions & 11 deletions source/Geometry/Face.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@
#include "Face.h"
#include "Mesh.h"

Face::Face(std::shared_ptr<Mesh> mesh, IndexArray&& vertices)
Face::Face(Mesh& mesh, IndexArray&& vertices)
: Prim()
, mesh_(std::move(mesh))
, index_(mesh_->NumFaces())
, mesh_(mesh)
, index_(mesh_.NumFaces())
{
mesh_->AppendFaceVertices(std::move(vertices));
mesh_.AppendFaceVertices(std::move(vertices));
}

const Vector3 & Face::GetPosition(Index vertex) const
{
Index vertex_offset = mesh_->offsets_[index_];
Index point_offset = mesh_->vertices_[vertex_offset + vertex];
return mesh_->positions_[point_offset];
Index vertex_offset = mesh_.offsets_[index_];
Index point_offset = mesh_.vertices_[vertex_offset + vertex];
return mesh_.positions_[point_offset];
}

Vector3 & Face::GetPosition(Index vertex)
{
Index vertex_offset = mesh_->offsets_[index_];
Index point_offset = mesh_->vertices_[vertex_offset + vertex];
return mesh_->positions_[point_offset];
Index vertex_offset = mesh_.offsets_[index_];
Index point_offset = mesh_.vertices_[vertex_offset + vertex];
return mesh_.positions_[point_offset];
}

Index Face::NumVertices() const
{
return mesh_->counts_[index_];
return mesh_.counts_[index_];
}
7 changes: 3 additions & 4 deletions source/Geometry/Face.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
#ifndef FACE_H
#define FACE_H

#include <Core/Prim.h>
#include <Foundation/Prim.h>

#include <memory>
#include <vector>

class Mesh;

Expand All @@ -43,10 +42,10 @@ class Face : public Prim
Index GetIndex() const { return index_; }

protected:
Face(std::shared_ptr<Mesh> mesh, IndexArray && vertices);
Face(Mesh& mesh, IndexArray&& vertices);

private:
std::shared_ptr<Mesh> mesh_;
Mesh& mesh_;
const Index index_{};
};

Expand Down
14 changes: 4 additions & 10 deletions source/Geometry/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@
// 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.
//

#include "Mesh.h"
#include "Face.h"

std::shared_ptr<Mesh> Mesh::Create()
{
struct enable_make_shared : public Mesh {};
return std::make_shared<enable_make_shared>();
}
//

#include "Mesh.h"
#include "Face.h"

Mesh::~Mesh()
{
Expand Down
Loading