├── .gitignore ├── LICENSE.md ├── Makefile ├── README.md └── dlaf.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | /dlaf 2 | 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (C) 2019 Michael Fogleman 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ 2 | TARGET = dlaf 3 | COMPILE_FLAGS = -std=c++14 -flto -O3 -Wall -Wextra -pedantic -Wno-unused-parameter -march=native 4 | 5 | all: $(TARGET) 6 | 7 | $(TARGET): $(TARGET).cpp 8 | $(CC) $(COMPILE_FLAGS) -o $(TARGET) $(TARGET).cpp 9 | 10 | clean: 11 | $(RM) $(TARGET) 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dlaf 2 | 3 | Diffusion-limited aggregation, fast. Diffusion-limited as f*ck. 4 | 5 | ### Features 6 | 7 | - 2D or 3D diffusion-limited aggregation 8 | - Super fast? 35 seconds to compute one million points on a single core. 9 | 10 | ### Dependencies 11 | 12 | - [boost](https://www.boost.org/) (used for its spatial index) 13 | 14 | ### Usage 15 | 16 | ```bash 17 | git clone https://github.com/fogleman/dlaf.git 18 | cd dlaf 19 | make 20 | ./dlaf > output.csv 21 | ``` 22 | 23 | ### Output Format 24 | 25 | The `parent_id` tells you which particle was joined to. It is -1 for initial seed positions. 26 | When 2D is used, Z will be zero for all points. 27 | 28 | ```bash 29 | # columns are: id, parent_id, x, y, z 30 | $ head output.csv 31 | 0,-1,0,0,0 32 | 1,0,0.934937,0.354814,0 33 | 2,0,0.0525095,-0.99862,0 34 | 3,1,0.989836,1.35331,0 35 | 4,3,1.92472,1.70826,0 36 | 5,3,0.65572,2.29584,0 37 | 6,4,2.90818,1.88937,0 38 | 7,0,-0.989205,0.146538,0 39 | 8,2,0.917631,-1.50018,0 40 | 9,5,0.832028,3.28017,0 41 | ``` 42 | 43 | ### Hooks & Parameters 44 | 45 | The code implements a standard diffusion-limited aggregation algorithm. But there are several parameters and code hooks that let you tweak its behavior. 46 | 47 | The following parameters can be set on a `Model` instance. 48 | 49 | | Parameter | Description | 50 | | --- | --- | 51 | | `AttractionDistance` | Defines how close together particles must be in order to join together. | 52 | | `ParticleSpacing` | Defines the distance between particles when they become joined together. | 53 | | `MinMoveDistance` | Defines the minimum distance that a particle will move in an iteration during its random walk. | 54 | | `Stubbornness` | Defines how many join attempts must occur before a particle will allow another particle to join to it. | 55 | | `Stickiness` | Defines the probability that a particle will allow another particle to join to it. | 56 | 57 | The following hooks allow you to define the algorithm behavior in small, well-defined functions. 58 | 59 | | Hook | Description | 60 | | --- | --- | 61 | | `RandomStartingPosition()` | Returns a starting position for a new particle to begin its random walk. | 62 | | `ShouldReset(p)` | Returns true if the particle has gone too far away and should be reset to a new random starting position. | 63 | | `ShouldJoin(p, parent)` | Returns true if the point should attach to the specified parent particle. This is only called when the point is already within the required attraction distance. If false is returned, the particle will continue its random walk instead of joining to the other particle. | 64 | | `PlaceParticle(p, parent)` | Returns the final placement position of the particle. | 65 | | `MotionVector(p)` | Returns a vector specifying the direction that the particle should move for one iteration. The distance that it will move is determined by the algorithm. | 66 | 67 | ### Rendering 68 | 69 | Rendering is left to you. This code just gives you the location of the points and their hierarchy. 70 | But here is an example rendering in 2D with one million particles: 71 | 72 | ![Example](https://i.imgur.com/Ma1hv3z.png) 73 | 74 | And here is a 3D example with 10 million particles. This image was ray traced in about 10 hours at 4096x4096px with 2048 samples per pixel. [Full resolution here.](https://www.michaelfogleman.com/static/dlaf-3d-10m-2048spp-4096px.png) 75 | 76 | ![Example](https://www.michaelfogleman.com/static/dlaf-3d-10m-2048spp-1600px.png) 77 | -------------------------------------------------------------------------------- /dlaf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | // number of dimensions (must be 2 or 3) 9 | const int D = 2; 10 | 11 | // default parameters (documented below) 12 | const double DefaultParticleSpacing = 1; 13 | const double DefaultAttractionDistance = 3; 14 | const double DefaultMinMoveDistance = 1; 15 | const int DefaultStubbornness = 0; 16 | const double DefaultStickiness = 1; 17 | 18 | // boost is used for its spatial index 19 | using BoostPoint = boost::geometry::model::point< 20 | double, D, boost::geometry::cs::cartesian>; 21 | 22 | using IndexValue = std::pair; 23 | 24 | using Index = boost::geometry::index::rtree< 25 | IndexValue, boost::geometry::index::linear<4>>; 26 | 27 | // Vector represents a point or a vector 28 | class Vector { 29 | public: 30 | Vector() : 31 | m_X(0), m_Y(0), m_Z(0) {} 32 | 33 | Vector(double x, double y) : 34 | m_X(x), m_Y(y), m_Z(0) {} 35 | 36 | Vector(double x, double y, double z) : 37 | m_X(x), m_Y(y), m_Z(z) {} 38 | 39 | double X() const { 40 | return m_X; 41 | } 42 | 43 | double Y() const { 44 | return m_Y; 45 | } 46 | 47 | double Z() const { 48 | return m_Z; 49 | } 50 | 51 | BoostPoint ToBoost() const { 52 | return BoostPoint(m_X, m_Y, m_Z); 53 | } 54 | 55 | double Length() const { 56 | return std::sqrt(m_X * m_X + m_Y * m_Y + m_Z * m_Z); 57 | } 58 | 59 | double LengthSquared() const { 60 | return m_X * m_X + m_Y * m_Y + m_Z * m_Z; 61 | } 62 | 63 | double Distance(const Vector &v) const { 64 | const double dx = m_X - v.m_X; 65 | const double dy = m_Y - v.m_Y; 66 | const double dz = m_Z - v.m_Z; 67 | return std::sqrt(dx * dx + dy * dy + dz * dz); 68 | } 69 | 70 | Vector Normalized() const { 71 | const double m = 1 / Length(); 72 | return Vector(m_X * m, m_Y * m, m_Z * m); 73 | } 74 | 75 | Vector operator+(const Vector &v) const { 76 | return Vector(m_X + v.m_X, m_Y + v.m_Y, m_Z + v.m_Z); 77 | } 78 | 79 | Vector operator-(const Vector &v) const { 80 | return Vector(m_X - v.m_X, m_Y - v.m_Y, m_Z - v.m_Z); 81 | } 82 | 83 | Vector operator*(const double a) const { 84 | return Vector(m_X * a, m_Y * a, m_Z * a); 85 | } 86 | 87 | Vector &operator+=(const Vector &v) { 88 | m_X += v.m_X; m_Y += v.m_Y; m_Z += v.m_Z; 89 | return *this; 90 | } 91 | 92 | private: 93 | double m_X; 94 | double m_Y; 95 | double m_Z; 96 | }; 97 | 98 | // Lerp linearly interpolates from a to b by distance. 99 | Vector Lerp(const Vector &a, const Vector &b, const double d) { 100 | return a + (b - a).Normalized() * d; 101 | } 102 | 103 | // Random returns a uniformly distributed random number between lo and hi 104 | double Random(const double lo = 0, const double hi = 1) { 105 | static thread_local std::mt19937 gen( 106 | std::chrono::high_resolution_clock::now().time_since_epoch().count()); 107 | std::uniform_real_distribution dist(lo, hi); 108 | return dist(gen); 109 | } 110 | 111 | // RandomInUnitSphere returns a random, uniformly distributed point inside the 112 | // unit sphere (radius = 1) 113 | Vector RandomInUnitSphere() { 114 | while (true) { 115 | const Vector p = Vector( 116 | Random(-1, 1), 117 | Random(-1, 1), 118 | D == 2 ? 0 : Random(-1, 1)); 119 | if (p.LengthSquared() < 1) { 120 | return p; 121 | } 122 | } 123 | } 124 | 125 | // Model holds all of the particles and defines their behavior. 126 | class Model { 127 | public: 128 | Model() : 129 | m_ParticleSpacing(DefaultParticleSpacing), 130 | m_AttractionDistance(DefaultAttractionDistance), 131 | m_MinMoveDistance(DefaultMinMoveDistance), 132 | m_Stubbornness(DefaultStubbornness), 133 | m_Stickiness(DefaultStickiness), 134 | m_BoundingRadius(0) {} 135 | 136 | void SetParticleSpacing(const double a) { 137 | m_ParticleSpacing = a; 138 | } 139 | 140 | void SetAttractionDistance(const double a) { 141 | m_AttractionDistance = a; 142 | } 143 | 144 | void SetMinMoveDistance(const double a) { 145 | m_MinMoveDistance = a; 146 | } 147 | 148 | void SetStubbornness(const int a) { 149 | m_Stubbornness = a; 150 | } 151 | 152 | void SetStickiness(const double a) { 153 | m_Stickiness = a; 154 | } 155 | 156 | // Add adds a new particle with the specified parent particle 157 | void Add(const Vector &p, const int parent = -1) { 158 | const int id = m_Points.size(); 159 | m_Index.insert(std::make_pair(p.ToBoost(), id)); 160 | m_Points.push_back(p); 161 | m_JoinAttempts.push_back(0); 162 | m_BoundingRadius = std::max( 163 | m_BoundingRadius, p.Length() + m_AttractionDistance); 164 | std::cout 165 | << id << "," << parent << "," 166 | << p.X() << "," << p.Y() << "," << p.Z() << std::endl; 167 | } 168 | 169 | // Nearest returns the index of the particle nearest the specified point 170 | int Nearest(const Vector &point) const { 171 | int result = -1; 172 | m_Index.query( 173 | boost::geometry::index::nearest(point.ToBoost(), 1), 174 | boost::make_function_output_iterator([&result](const auto &value) { 175 | result = value.second; 176 | })); 177 | return result; 178 | } 179 | 180 | // RandomStartingPosition returns a random point to start a new particle 181 | Vector RandomStartingPosition() const { 182 | const double d = m_BoundingRadius; 183 | return RandomInUnitSphere().Normalized() * d; 184 | } 185 | 186 | // ShouldReset returns true if the particle has gone too far away and 187 | // should be reset to a new random starting position 188 | bool ShouldReset(const Vector &p) const { 189 | return p.Length() > m_BoundingRadius * 2; 190 | } 191 | 192 | // ShouldJoin returns true if the point should attach to the specified 193 | // parent particle. This is only called when the point is already within 194 | // the required attraction distance. 195 | bool ShouldJoin(const Vector &p, const int parent) { 196 | m_JoinAttempts[parent]++; 197 | if (m_JoinAttempts[parent] < m_Stubbornness) { 198 | return false; 199 | } 200 | return Random() <= m_Stickiness; 201 | } 202 | 203 | // PlaceParticle computes the final placement of the particle. 204 | Vector PlaceParticle(const Vector &p, const int parent) const { 205 | return Lerp(m_Points[parent], p, m_ParticleSpacing); 206 | } 207 | 208 | // MotionVector returns a vector specifying the direction that the 209 | // particle should move for one iteration. The distance that it will move 210 | // is determined by the algorithm. 211 | Vector MotionVector(const Vector &p) const { 212 | return RandomInUnitSphere(); 213 | } 214 | 215 | // AddParticle diffuses one new particle and adds it to the model 216 | void AddParticle() { 217 | // compute particle starting location 218 | Vector p = RandomStartingPosition(); 219 | 220 | // do the random walk 221 | while (true) { 222 | // get distance to nearest other particle 223 | const int parent = Nearest(p); 224 | const double d = p.Distance(m_Points[parent]); 225 | 226 | // check if close enough to join 227 | if (d < m_AttractionDistance) { 228 | if (!ShouldJoin(p, parent)) { 229 | // push particle away a bit 230 | p = Lerp(m_Points[parent], p, 231 | m_AttractionDistance + m_MinMoveDistance); 232 | continue; 233 | } 234 | 235 | // adjust particle position in relation to its parent 236 | p = PlaceParticle(p, parent); 237 | 238 | // add the point 239 | Add(p, parent); 240 | return; 241 | } 242 | 243 | // move randomly 244 | const double m = std::max( 245 | m_MinMoveDistance, d - m_AttractionDistance); 246 | p += MotionVector(p).Normalized() * m; 247 | 248 | // check if particle is too far away, reset if so 249 | if (ShouldReset(p)) { 250 | p = RandomStartingPosition(); 251 | } 252 | } 253 | } 254 | 255 | private: 256 | // m_ParticleSpacing defines the distance between particles that are 257 | // joined together 258 | double m_ParticleSpacing; 259 | 260 | // m_AttractionDistance defines how close together particles must be in 261 | // order to join together 262 | double m_AttractionDistance; 263 | 264 | // m_MinMoveDistance defines the minimum distance that a particle will move 265 | // during its random walk 266 | double m_MinMoveDistance; 267 | 268 | // m_Stubbornness defines how many interactions must occur before a 269 | // particle will allow another particle to join to it. 270 | int m_Stubbornness; 271 | 272 | // m_Stickiness defines the probability that a particle will allow another 273 | // particle to join to it. 274 | double m_Stickiness; 275 | 276 | // m_BoundingRadius defines the radius of the bounding sphere that bounds 277 | // all of the particles 278 | double m_BoundingRadius; 279 | 280 | // m_Points stores the final particle positions 281 | std::vector m_Points; 282 | 283 | // m_JoinAttempts tracks how many times other particles have attempted to 284 | // join with each finalized particle 285 | std::vector m_JoinAttempts; 286 | 287 | // m_Index is the spatial index used to accelerate nearest neighbor queries 288 | Index m_Index; 289 | }; 290 | 291 | int main() { 292 | // create the model 293 | Model model; 294 | 295 | // add seed point(s) 296 | model.Add(Vector()); 297 | 298 | // { 299 | // const int n = 3600; 300 | // const double r = 1000; 301 | // for (int i = 0; i < n; i++) { 302 | // const double t = (double)i / n; 303 | // const double a = t * 2 * M_PI; 304 | // const double x = std::cos(a) * r; 305 | // const double y = std::sin(a) * r; 306 | // model.Add(Vector(x, y, 0)); 307 | // } 308 | // } 309 | 310 | // run diffusion-limited aggregation 311 | for (int i = 0; i < 100000; i++) { 312 | model.AddParticle(); 313 | } 314 | 315 | return 0; 316 | } 317 | --------------------------------------------------------------------------------