Giter Club home page Giter Club logo

quadtrees's Introduction

QuadTrees

CircleCI

High Performance QuadTree for C# based on https://github.com/splitice/QuadTrees implementation. This fork reworked some internals to make it compatible with structs and lowlevel operations.

Usage example with classes

// The objects being stored inside the quadtree
class QTreeObject: IPointFQuadStorable {

    private PointF _rect;

    public PointF Point {
	set { _rect = value; }
	get { return _rect; }
    }

    public QTreeObject(PointF rect) { _rect = rect; }
}

// The payload for lambda operations to prevent local variables copies causing allocations 
struct Payload{ public int counter; }

QuadTreePointF<QTreeObject> qtree = new QuadTreePointF<QTreeObject>();
qtree.AddRange(new List<QTreeObject>{
	new QTreeObject(new PointF(10,10)),
	new QTreeObject(new PointF(11,11)),
	new QTreeObject(new PointF(12,12)),
	new QTreeObject(new PointF(11,11)),
	new QTreeObject(new PointF(-1000,1000))
});

var amount = qtree.ObjectCount(new RectangleF(9,9,20,20));                     // Counts entities inside the range 
var list = qtree.GetObjects(new RectangleF(9, 9, 20, 20));                     // Returns new list
qtree.GetObjects(new RectangleF(9,9,20,20), list);                             // Uses existing one  

var payload = new Payload{ counter = 0; }
qtree.GetObjects(new RectangleF(9, 9, 20, 20), (obj) => { // logic });                            // Executes a lambda for each obj inside the rectangle
qtree.GetObjects(new RectangleF(9, 9, 20, 20), ref payload, (ref payload, obj) => payload++);     // Same as above, but with passed payload to pass stuff into the lambda

Usage example with structs

// The objects being stored inside the quadtree
struct QTreeObject: IPointFQuadStorable {

    public int uniqueId;  // Used for hashing, just an example, any other hash method variant will work too .
    private PointF _rect;

    public PointF Point {
	set { _rect = value; }
	get { return _rect; }
    }

    public QTreeObjectStruct(int uniqueId, PointF rect) {
	this.uniqueId = uniqueId;
	_rect = rect;
    }

    public bool Equals(QTreeObjectStruct other) { return uniqueId == other.uniqueId; }

    public override bool Equals(object obj) { return obj is QTreeObjectStruct other && Equals(other); }

    public override int GetHashCode() { return uniqueId; }
}

// The payload for lambda operations to prevent local variables copies causing allocations 
struct Payload{ public int counter; }

QuadTreePointF<QTreeObject> qtree = new QuadTreePointF<QTreeObject>();
qtree.AddRange(new List<QTreeObject>{
	new QTreeObject(1, new PointF(10,10)),
	new QTreeObject(2, new PointF(11,11)),
	new QTreeObject(3, new PointF(12,12)),
	new QTreeObject(4, new PointF(11,11)),
	new QTreeObject(5, new PointF(-1000,1000))
});

var amount = qtree.ObjectCount(new RectangleF(9,9,20,20));                     // Counts entities inside the range 
var list = qtree.GetObjects(new RectangleF(9, 9, 20, 20));                     // Returns new list
qtree.GetObjects(new RectangleF(9,9,20,20), list);                             // Uses existing one  

var rect = new RectangleF(9, 9, 20, 20);
var count = qtree.ObjectCount(rect);
Span<QTreeObject> array = stackalloc QTreeObject[count];           // Local array of the structs being stored in the quadtree
qtree.GetObjects(rect, array);                                    // Copy structs inside the rectangle to the local array 

var payload = new Payload{ counter = 0; }
qtree.GetObjects(new RectangleF(9, 9, 20, 20), (ref QTreeObject obj) => { // logic });                            // Executes a lambda for each obj inside the rectangle
qtree.GetObjects(new RectangleF(9, 9, 20, 20), ref payload, (ref Payload payload, ref QTreeObject obj) => payload++);     // Same as above, but with passed payload to pass stuff into the lambda

License

Since version v1.0.3 licensed under the Apache License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.