summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/force/QuadTree.lua
blob: 3e1620dc21f70815b5900720024f9bb8b2dc2a0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
-- Copyright 2011 by Jannis Pohlmann
-- Copyright 2012 by Till Tantau
--
-- This file may be distributed an/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information

-- @release $Header$


--- An implementation of a quad trees.
--
-- The class QuadTree provides methods form handling quadtrees.
--

local QuadTree = {
  -- Subclasses
  Particle = {},
  Cell = {}
}
QuadTree.__index = QuadTree

-- Namespace:
require("pgf.gd.force").QuadTree = QuadTree

-- Imports:
local Vector = require "pgf.gd.deprecated.Vector"
local lib = require "pgf.gd.lib"


--- Creates a new quad tree.
--
-- @return A newly-allocated quad tree.
--
function QuadTree.new(x, y, width, height, max_particles)
  local tree = {
    root_cell = QuadTree.Cell.new(x, y, width, height, max_particles)
  }
  setmetatable(tree, QuadTree)
  return tree
end



--- Inserts a particle
--
-- @param param A particle of type QuadTree.Particle
--
function QuadTree:insert(particle)
  self.root_cell:insert(particle)
end



--- Computes the interactions of a particle with other cells
--
-- @param particle A particle
-- @param test_func A test function, which on input of a cubical cell and a particle should
--                  decide whether the cubical cell should be inserted into the result
-- @param cells An optional array of cells, to which the found cells will be added
--
-- @return The cells array or a new array, if it was empty.
--
function QuadTree:findInteractionCells(particle, test_func, cells)
  local test_func = test_func or function (cell, particle) return true end
  cells = cells or {}

  self.root_cell:findInteractionCells(particle, test_func, cells)

  return cells
end




--- Particle subclass
QuadTree.Particle.__index = QuadTree.Particle



--- Creates a new particle.
--
-- @return A newly-allocated particle.
--
function QuadTree.Particle.new(pos, mass)
  local particle = {
    pos = pos:copy(),
    mass = mass or 1,
    subparticles = {},
  }
  setmetatable(particle, QuadTree.Particle)
  return particle
end



--- A cell of a quadtree
--
-- TT: Why is it called "cubical", by the way?!

QuadTree.Cell.__index = QuadTree.Cell



--- Creates a new cubicle cell.
--
-- @return a newly-allocated cubicle cell.
--
function QuadTree.Cell.new(x, y, width, height, max_particles)
  local cell = {
    x = x,
    y = y,
    width = width,
    height = height,
    max_particles = max_particles or 1,
    subcells = {},
    particles = {},
    center_of_mass = nil,
    mass = 0,
  }
  setmetatable(cell, QuadTree.Cell)
  return cell
end



function QuadTree.Cell:containsParticle(particle)
  return particle.pos.x >= self.x and particle.pos.x <= self.x + self.width
     and particle.pos.y >= self.y and particle.pos.y <= self.y + self.height
end



function QuadTree.Cell:findSubcell(particle)
  return lib.find(self.subcells, function (cell)
    return cell:containsParticle(particle)
  end)
end



function QuadTree.Cell:createSubcells()
  assert(type(self.subcells) == 'table' and #self.subcells == 0)
  assert(type(self.particles) == 'table' and #self.particles <= self.max_particles)

  if #self.subcells == 0 then
    for _,x in ipairs({self.x, self.x + self.width/2}) do
      for _,y in ipairs({self.y, self.y + self.height/2}) do
        local cell = QuadTree.Cell.new(x, y, self.width/2, self.height/2, self.max_particles)
        table.insert(self.subcells, cell)
      end
    end
  end
end



function QuadTree.Cell:insert(particle)
  -- check if we have a particle with the exact same position already
  local existing = lib.find(self.particles, function (other)
    return other.pos:equals(particle.pos)
  end)

  if existing then
    -- we already have a particle at the same position; splitting the cell
    -- up makes no sense; instead we add the new particle as a
    -- subparticle of the existing one
    table.insert(existing.subparticles, particle)
  else
    if #self.subcells == 0 and #self.particles < self.max_particles then
      table.insert(self.particles, particle)
    else
      if #self.subcells == 0 then
        self:createSubcells()
      end

      -- move particles to the new subcells
      for _,existing in ipairs(self.particles) do
        local cell = self:findSubcell(existing)
        assert(cell, 'failed to find a cell for particle ' .. tostring(existing.pos))
        cell:insert(existing)
      end

      self.particles = {}

      local cell = self:findSubcell(particle)
      assert(cell)
      cell:insert(particle)
    end
  end

  self:updateMass()
  self:updateCenterOfMass()

  assert(self.mass)
  assert(self.center_of_mass)
end



function QuadTree.Cell:updateMass()
  -- reset mass to zero
  self.mass = 0

  if #self.subcells == 0 then
    -- the mass is the number of particles of the cell
    for _,particle in ipairs(self.particles) do
      self.mass = self.mass + particle.mass
      for _,subparticle in ipairs(particle.subparticles) do
        self.mass = self.mass + subparticle.mass
      end
    end
  else
    -- the mass is the sum of the masses of the subcells
    for _,subcell in ipairs(self.subcells) do
      self.mass = self.mass + subcell.mass
    end
  end
end



function QuadTree.Cell:updateCenterOfMass()
  -- reset center of mass, assuming the cell is empty
  self.center_of_mass = nil

  if #self.subcells == 0 then
    -- the center of mass is the average position of the particles
    -- weighted by their masses
    self.center_of_mass = Vector.new (2)
    for _,p in ipairs(self.particles) do
      for _,sp in ipairs(p.subparticles) do
        self.center_of_mass = self.center_of_mass:plus(sp.pos:timesScalar(sp.mass))
      end
      self.center_of_mass = self.center_of_mass:plus(p.pos:timesScalar(p.mass))
    end
    self.center_of_mass = self.center_of_mass:dividedByScalar(self.mass)
  else
    -- the center of mass is the average of the weighted centers of mass
    -- of the subcells
    self.center_of_mass = Vector.new(2)
    for _,sc in ipairs(self.subcells) do
      if sc.center_of_mass then
        self.center_of_mass = self.center_of_mass:plus(sc.center_of_mass:timesScalar(sc.mass))
      else
        assert(sc.mass == 0)
      end
    end
    self.center_of_mass = self.center_of_mass:dividedByScalar(self.mass)
  end
end



function QuadTree.Cell:findInteractionCells(particle, test_func, cells)
  if #self.subcells == 0 or test_func(self, particle) then
    table.insert(cells, self)
  else
    for _,subcell in ipairs(self.subcells) do
      subcell:findInteractionCells(particle, test_func, cells)
    end
  end
end


function QuadTree.Cell:__tostring()
  return '((' .. self.x .. ', ' .. self.y .. ') '
      .. 'to (' .. self.x + self.width .. ', ' .. self.y + self.height .. '))'
      .. (self.particle and ' => ' .. self.particle.name or '')
      .. (self.center_of_mass and ' mass ' .. self.mass .. ' at ' .. tostring(self.center_of_mass) or '')
end



-- done

return QuadTree