Skip to content

Latest commit

 

History

History
482 lines (299 loc) · 18.1 KB

documentation.md

File metadata and controls

482 lines (299 loc) · 18.1 KB

Table of Contents

Cell

A Cell instance defines a polygon from a Voronoi diagram and its seed.

Parameters

  • contourPoints Array Array of points
  • seed

isValid

Return if this cell is valid

Returns Boolean true if valid, false if not

getHash

Get the hash of this cell

Returns String

getSeed

Get the seed of the cell.

Returns Object should be of form {x: Number, y: Number, seedIndex: Number}

getPolygon

Get the polygon of this cell

Returns ConvexPolygon

getArea

Get the area of the cell (calls the polygon method)

Returns Number the area

intersectWithCell

Get the polygon resulting from the intersection of a voronoi cell with another cell (this second cell comes from another voronoi diagramm where a seed has been added). NOTE: This does NOT implement a standard polygon intersection algorithm, its use is stricly for the use case of voronoi cells being replaced, making it quite faster but not suitable for other cases.

Parameters

  • anotherCell Cell another cell to intersect with

Returns ConvexPolygon the polygon resulting from the intersection

hasSamePolygon

Compare if this cell has the same polygon as another cell. Read the doc of ConvexPolygon.getPolygon() for more info.

Parameters

  • otherCell
  • anotherCell Cell another cell to compare the polygon with.

Returns Boolean true if the same polygon, false if not

genarateHash

[STATIC] Get a unique hash from 2 floats. I am pretty sure these super simple stringified coordinate hashes are not the most robust, though the point is mainly to be as fast as possible.

Parameters

  • n1 Number a number, most likely the x of a coord
  • n2 Number a number, most likely the y of a coord
  • a String (probably unique enough) hash

CellCollection

A CellCollection stores Cell objects. It is the interface between the Voronoi diagram and the Cells/Polygons.

buildFromVoronoiDiagram

Builds the collection of cell using a voronoi diagram object from the Javascript-Voronoi library by Gorhill ( http://bit.ly/2FoapPI )

Parameters

  • vd

getCellHashes

Get the list of hashes (ID) of cells in the collection

Returns Array all the hashes

getCell

Get the cell of this collection that has this hash

Parameters

  • hash String the unique hash of the cell

Returns Cell a Cell instance of null if hash not found

referenceSeed

Store the hash of a given position of a seed, meaning, store a reference to a cell. This seed is the one added to the 'another' diagram, when we introduce a pixel as a seed. For instance, the seed-only cell collection does not have a reference to a seed.

Parameters

  • x Number the x position of the seed to reference
  • y Number the y position of the seed to reference

getReferencedSeedCell

Get the cell that was referenced by referenceSeed()

Returns Cell

getStolenAreaInfo

When comparing a seed-only CellCollection with a seed-and-one-pixel CellCollection with getStolenAreaInfo(), we get a list of original cell index (from the seed-only collection) as well as the area ratio that comes from them to build the pixel-based cell

Parameters

  • anotherCellCollection CellCollection a cell collection with the original seeds + 1 pixel as a seed

Returns Array list of {seedIndex: Number, weight: Number}

ConvexPolygon

ConvexPolygon is a simple approach of a polygon, it's actually a simple approcah of what is a convex polygon and is mainly made to be used in the context of a polygon representing a cell of a Voronoi diagram. Here, a polygon is a list of 2D points that represent a convex polygon, with the list of point being no closed (= the last point is not a repetition of the first). The list of points describing a polygon can not be modified later.

Parameters

  • points Array can be an array of [x, y] (both being of type Number) or it can be an array of {x: Number, y: Number}. Depending on what is the source of points, both exist. Note: there is no integrity checking that the given list of point represent an actually convex polygon.

isValid

Get if the polygon is valid

Returns Boolean [description]

getArea

Get the area of a this polygon. If never computed, computes it and stores it

Returns Number the area

getCellReplacementIntersection

This is reliable ONLY in the context of Voronoi cells! This is NOT a generally valid way to find the intersection polygon between 2 convex polygons! For this context only, we believe it's much faster tho.

Parameters

  • anotherPolygon Polygon another polygon the get the intersection with.

getHull

Get the Array of vertices. This is a reference and the point should not be modified!

Returns Array the points of the hull

isSame

Compare with another polygon and tells if it's the same. Predicate: polygons are convex + the first point of the list starts at noon and the following are going clock-wise.

Parameters

Returns Boolean true is the same, false if not

removeDuplicateVertices

[STATIC] Removes the duplicates of a hull so that a polygon hull does not have twice the same [x, y] points;

Parameters

  • polygonPoints Array an array of [x, y]

Returns Array an array of [x, y], with duplicates removed

getPolygonCenter

Get the center coordinate of a polygon by averaging all the pointd of the hull

Parameters

  • polygonPoints Array an array of [x, y]

Returns Array the center as [x, y]

orderPolygonPoints

A list of polygon points representing a convex hull are not always listed in a clock-wise of ccw order, by default, we cannot count on it. This methods reorder the vertices of the in a clock-wise fashion, starting by the one that at noon or immediately after. Note: this is necessary to compute the area and to compare two polygons.

Parameters

  • polygonPoints Array an array of [x, y]

Returns Array an array of [x, y]

Interpolator

The Interpolator is the API provider of natninter.

onProgress

Define a callback for the progress of making the map and the progress of making the output image It will be called with 2 args: the progress in percentage (Number), and a description string

Parameters

cleanSeeds

Removes all the seeds

addSeed

Add a seed to the interpolator. Note that the seed is not copied, only its reference is. This is convenient for updating the value of the seed from the outside and ecompute the interpolation without having to recompute the whole weight map.

Parameters

  • seed Object of form {x: Number, y: Number, value: Number}

addSeeds

Add an array of seed

Parameters

  • seedArr Array array of seeds, where each seed is of form {x: Number, y: Number, value: Number}

setOutputSize

Define the size of the output image

Parameters

  • width Number width of the output image
  • height Number height of the output image

hasAllSeedsInside

Check if all the seeds are inside the output area defined with setOutputSize()

Returns Boolean true if all are inside, false if at leas one is out

generateMap

Compute the sampling map. Automatically called by the update method when the map was never computed or when a seed have been added since. Though this method is not private and can be called to force recomputing the map. Note: if you have already computed a map for the exact same seed positions and output size, you can avoid recomputing it and use getMap and setMap.

getMap

Get the sampling map object

Parameters

  • m

setMap

When you don't want to recompute the sampling map with computeMap() and reuse the exact same seed position and output size (of course, seed values can change)

Parameters

  • map Array an already existing sampling map

isAtSeedPosition

is the given position at the position of a seed?

Parameters

  • i Number position along x axis
  • j Number position along y axis

Returns Boolean -1 if not, of the index of the seed if yes

_generateSeedCells

[PRIVATE] Generate the voronoi diagram where sites are only seeds

generateImage

Generate the output image as a floating points 1D array representing a 2D (1band) image.

Returns Object of form {_data: Float32Array, _metadata: {width: Number, height: Number}}

VectorTools

VectorTools is not instanciable and provide some static functions for computing things about vectors.

crossProduct

performs a cross product between v1 and v2.

Parameters

  • v1 Array a vector [x, y, z]. To use with 2D vectors, just use [x, y, 0]
  • v2 Array a vector [x, y, z]. To use with 2D vectors, just use [x, y, 0]
  • normalize Boolean will force normalization of the output vector if true (default: false) (optional, default false)

Returns Array a vector [x, y, z], result of the cross product

dotProduct

Perform the dot product of two vectors. They need to be of the same dimension but they can be 2D, 3D or aother. Note: If v1 and v2 are normalize, the dot product is also the cosine

Parameters

  • v1 Array a vector [x, y] or [x, y, z]
  • v2 Array a vector [x, y] or [x, y, z]

Returns Number the dot product

normalize

Normalizes a 3D vector

Parameters

  • v Array 3D vector to normalize

Returns Array the normalized 3D vector

getNorm

return the norm (length) of a vector [x, y, z]

Parameters

  • v Array 3D vector to get the norm of

Returns Number the norm

rotate

rotate the vector v using the rotation matrix m.

Parameters

  • v Array 3D vector
  • m Array matrix [[a, b, c], [d, e, f], [g, h, i]]

Returns Array the rotated vector [ax + by + cz, dx + ey + fz, gx + hy + iz]

getAnglePoints

Compute the angle p1p2p3 in radians. Does not give the sign, just absolute angle!

Parameters

  • p1 Array a 3D point [x, y, z]
  • p2 Array a 3D point [x, y, z]
  • p3 Array a 3D point [x, y, z]

Returns Number [description]

vector2DCrossing

Checks if the 2D vector u crosses the 2D vector v. The vector u goes from point u1 to point u2 and vector v goes from point v1 to point v2. Based on Gavin from SO http://bit.ly/2oNn741 reimplemented in JS

Parameters

  • u1 Array first point of the u vector as [x, y]
  • u2 Array second point of the u vector as [x, y]
  • v1 Array first point of the v vector as [x, y]
  • v2 Array second point of the v vector as [x, y]

Returns (Array | null) crossing point as [x, y] or null if vector don't cross.

pointToSegmentDistance

Get the distance between the 2D point p and a 2D segment u (defined by its points u1 and u2) Stolen from SO http://bit.ly/2oQG3yX

Parameters

  • p Array a point as [x, y]
  • u1 Array first point of the u vector as [x, y]
  • u2 Array second point of the u vector as [x, y]

Returns Number the distance