Adaptive Polygons Mk2
=====================

Functionality
-------------

This node puts a (transformed) copy of one object, called **donor**, along each
face of another object, called **recipient**. Copies of donor objects are
deformed (adapted) to match the shape and normals of recipient's face.

This node works primarily with Quads and Tris; if you ask it to work with NGons
(N > 4), it can produce weird resuls.

As an option, the node can process NGons (and Quads / Tris, optionally) in so
called "Frame / Fan" mode. In this mode, each face of recipient mesh is inset
by some amount, so it is replaced with a number of either Quads or Tris. Each
of such faces is then processed as usual.

Or one can pass recipient object through **Inset Special** node to achieve results
similar to Tissue's "Fan" or "Frame" modes.

In many cases it is good idea to pass the output of this node through **Remove
Doubles** node.

This node is based on original code taken with permission from
https://sketchesofcode.wordpress.com/2013/11/11/ by Alessandro Zomparelli
(sketchesofcode). Further features were developed after the Tissue addon by
Alessandro.

Inputs
------

This node has the following inputs:

- **VersR**. Vertices of the recipient object. This input is mandatory.
- **PolsR**. Faces of the recipient object. Please have in mind that order of
  indices in each face affect donor object rotation; for example, `[0, 1, 2,
  3]` is one rotation, `[3, 0, 1, 2]` is the same turned by 90 degrees. This
  input is mandatory. 
- **VersD**. Vertices of the donor object. This input is mandatory.
- **PolsD**. Faces of the donor object.
- **Width coeff**. Coefficient for donor object width (size along X,Y axis,
  usually). Default value is 1.0. The input expects one number per each
  recipient object face.
- **Frame width**. Width of frame for "Frame / Fan" mode. Maximum value of 1.0
  means that there will be no gap in the center of face, so this will be "Fan"
  mode. Default value is 0.5. This input is available only if **Frame mode**
  parameter is set to value other than **Do not use**.
- **Z coeff**. Coefficient for donor object size along recipient object face
  normal. Exact meaning depends on **Z Scale** parameter (see below). The
  default value is 1.0. The input expects one number per each recipient object face.
- **Z offset**. Offset of donor objects along the normal of recipient object
  face. Default value is 0.0. The input expects one number per each recipient
  object face.
- **Z Rotation**. Rotation of donor objects along the normal of recipient
  object face, in radians. Default value is 0.0. The input expects one number
  per each recipient object face.
- **PolyRotation**. Rotation of each recipient object face indexes. The default
  value is 0. For example, if recipient face definition is `[3, 4, 5, 6]`, and
  *PolyRotation* is set to 1, then the face will be interpreted as `[4, 5, 6,
  3]`. This will affect the orientation of donor object.
- **PolyMask**. Mask for recipient object faces processing. What exactly will
  be done with faces which are masked out is defined by **Mask Mode** parameter
  (see below).

Parameters
----------

This node has some number of parameters, and most of them are accessible only in the N panel:

- **Join**. If checked, then all procuced copies of donor object will be merged
  into one mesh. Unchecked by default.
- **Matching mode**. This defines how the list of donor objects is matched with list of recipient objects. Available values are:
  
   - **Match longest**. Each pair of recipient and donor objects will be
     processed. For example, if there are 2 recipient objects and 2 donor
     objects, you will have two outputs: `recipient[1] + donor[1]` and
     `recipient[2] + donor[2]`. If one of lists is shorter than another, the
     last object will be repeated as many times as necessary. For example, if
     there is 1 recipient object and 2 donor objects, you will have two
     outputs: `recipient[1] + donor[1]` and `recipient[1] + donor[2]`.
   - **Donor per face**. The list of donor objects will be treated as "one
     donor object per recipient object face". Number of outputs will be defined
     by number of recipient objects.

   The default value is **Match longest**.

- **Normal axis**. Axis of the donor object to be aligned with recipient object
  face normal. Available values are X, Y, and Z. Default value is Z.
- **Z Scale**. This defines how donor object size along recipient face normal
  is calculated. Available values are:

  - **Proportional**. Donor object size along recipient face normal is
    calculated by it's size along Z axis (or other one, defined by **Normal
    axis** parameter) multiplyed by value from **Z coeff** input.
  - **Constant**. Donor object size along recipient face normal is taken from
    **Z Coeff** input.
  - **Auto**. Calculate donor object scale along normal automatically, based on
    it's scale along two other axes. Multiply that automatically calculated
    value to the value taken from **Z Coeff** input.

  The default value is **Proportional**.

- **Use normals**. This defines how recipient object normals will be used to
  transform the donor objects. Available values are:

  - **Map**. Interpolation between recipient object's vertex normals will be
    used to deform the donor object. While this procuces smoother results, this
    gives more deformation of donor object.
  - **Face**. Recipient object's face normal will be used to orient the donor
    object. While this gives less deformation of donor object, it can give gaps
    between copies.

  The default value is **Map**.

- **Interpolate normals**. This parameter is available only if **Use normals**
  is set to **Map**. This defines the method of interpolation between recipient
  object's vertex normals. Available values are:

  - **Linear**. Linear interpolation will be used.
  - **Unit length**. Linear interpolation will be used, but the resulting
    normal will be resized to length of 1.0. This can give more smooth results
    in some cases.

  The default value is **Linear**.

- **Coordinates**. This defines the method of calculation of donor object's
  coordinates along two axes orthogonal to recipient's face normal. In any
  case, the location is defined by transforming some area of XOY plane (or
  other plane, according to **Normal axis** paramter), called *source area*, to
  the recipient object face. The question is what is the source area. The
  available values are:

  - **Bounds**. The source area is defined as follows:

    - For Quad recipient object faces, the bounding rectangle of donor object is taken.
    - For Tris recipient object faces, the bounding triangle of donor object is
      taken. The "bounding triangle" is defined as the smallest triangle with
      equal sides, which covers all donor vertices while having bottom side
      parallel to X axis (or other axis according to **Normal axis** parameter).
  
  - **As Is**. The source area is defined as follows:

    - For Quad faces, the `[-1/2; 1/2] x [-1/2; 1/2]` unit square is taken.
    - For Tris faces, the unit triangle is taken, i.e. the triangle with all
      sides equal to 1 with center at (0,0,0).

  Note that by definition of **Bounds** mode, the donor object always lies
  within the *source area*.

  The **As Is** mode allows one to manually transform the donor object before
  passing it to this node; interesting results may be achieved by making the
  donor object smaller than *source area*, or bigger than it, or even outside
  of it.

  The default value is **Bounds**.

- **Frame mode**. This defines when to apply "Frame / Fan" mode. The available values are:

  - **Do not use**. Frame / fan mode will not be used.
  - **NGons only**. Frame / fan mode will be used for NGons (n > 4) only. Other
    faces will be processed in simple replacement mode.
  - **NGons and Quads**. Frame / fan mode will be used for NGons and Quads
    (i.e. n >= 4) only. Tris will be processed in simple replacement mode.
  - **Always**. Frame / fan mode will be used for all faces.

  The default value is **Do not use**.

  Note that "Frame / Fan" mode makes either several Quads (when FrameWidth <
  1.0) or several Tris (when FrameWidth == 1.0) out of each recipient face. How
  exactly these Quads and Tris will be processed is defined by **Faces mode**
  parameter.

- **Faces mode**. This defines how deformations of donor object will be
  calculated for Quad and Tris recipient faces. Available values are:

  - **Quads / Tris Auto**. For Quad faces, the linear transformation will be
    used. For Tris faces, the barycentric transformation will be used to
    transform source triangle into the recipient triangle. This method gives
    good and smooth results for both Quads and Tris.
  - **Quads Always**. In this mode, Tris faces are processed as if they were
    (degenerated) Quads with third and fourth vertices coinciding. Such
    transformation can make one corner of donor object sharper than others, and
    in some cases produce weird results for Tris. But such results can be
    interesting in some cases. Note that at the moment the Tissue addon always
    uses this mode.

  The default value is **Quads / Tris Auto**.

- **Mask mode**. This defines what to do with recipient objectfaces excluded by the
  **PolyMask** input. Available values are:

  - **Skip**. Such faces will be skipped completely, i.e. will not produce any
    vertices and faces.
  - **As Is**. Such faces will be output as they were, i.e. one face will be
    output for each recipient face.

  The default value is **Skip**.

- **NGons**. This defines what to do with NGon recipient object faces (i.e.
  faces with number of vertices more than 4). Available values are:

  - **As Quads**. Such faces will be processed as if they were quads; only
    first three and the last vertex of the NGon will be used to form a Quad.
    This can give weird results for such faces. 
  - **Skip**. Such faces will be skipped completely, i.e. will not produce any
    vertices and faces.
  - **As Is**. Such faces will be output as they were, i.e. one face will be
    output for each recipient face.

  The default value is **As Quads**.

Outputs
-------

This node hsa the following outputs:

- **Vertices**
- **Polygons**

The outputs will contain one object, if **Join** flag is checked, or one object
per recipient object face, otherwise.

Examples of usage
-----------------

Example of **Z coeff** input usage:

.. image:: https://user-images.githubusercontent.com/284644/68081971-5473a700-fe38-11e9-8f8a-dbd204bafadd.png

Demonstration of how this node works with Tris recipient faces by default (in **Quads / Tris Auto** mode):

.. image:: https://user-images.githubusercontent.com/284644/68081972-5473a700-fe38-11e9-8604-018e7b59996d.png

The same setup but with **Faces mode** set to **Quads Always**:

.. image:: https://user-images.githubusercontent.com/284644/68081973-5473a700-fe38-11e9-89f6-8e4b4330772a.png

In some cases iterative application can give interesting results:

.. image:: https://user-images.githubusercontent.com/284644/68075234-ee027080-fdc6-11e9-8192-61d0917d45f7.png

An example of **Frame** mode:

.. image:: https://user-images.githubusercontent.com/284644/68528852-d7ee3600-0319-11ea-81ba-14bdd6e36a8e.png

The same setup with **FrameWidth** set to 1.0 - the processing switches to **Fan** mode:

.. image:: https://user-images.githubusercontent.com/284644/68528834-b68d4a00-0319-11ea-89d7-5857c886d423.png

You can also find some more examples `in the development thread <https://github.com/nortikin/sverchok/pull/2651>`_.

