diff --git a/.gitignore b/.gitignore index 35dd5a667e..4f0152028d 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,5 @@ node_modules .sublimets .settings/launch.json + +.vscode diff --git a/3d-bin-packing/3d-bin-packing-tests.ts b/3d-bin-packing/3d-bin-packing-tests.ts new file mode 100644 index 0000000000..319624d597 --- /dev/null +++ b/3d-bin-packing/3d-bin-packing-tests.ts @@ -0,0 +1,50 @@ +import packer = require("3d-bin-packing"); +import samchon = require("samchon-framework"); + +function main(): void +{ + /////////////////////////// + // CONSTRUCT OBJECTS + /////////////////////////// + let wrapperArray: bws.packer.WrapperArray = new packer.WrapperArray(); + let instanceArray: bws.packer.InstanceArray = new packer.InstanceArray(); + + // Wrappers + wrapperArray.push + ( + new packer.Wrapper("Large", 1000, 40, 40, 15, 0), + new packer.Wrapper("Medium", 700, 20, 20, 10, 0), + new packer.Wrapper("Small", 500, 15, 15, 8, 0) + ); + + /////// + // Each Instance is repeated #15 + /////// + instanceArray.insert(instanceArray.end(), 15, new packer.Product("Eraser", 1, 2, 5)); + instanceArray.insert(instanceArray.end(), 15, new packer.Product("Book", 15, 30, 3)); + instanceArray.insert(instanceArray.end(), 15, new packer.Product("Drink", 3, 3, 10)); + instanceArray.insert(instanceArray.end(), 15, new packer.Product("Umbrella", 5, 5, 20)); + + // Wrappers also can be packed into another Wrapper. + instanceArray.insert(instanceArray.end(), 15, new packer.Wrapper("Notebook-Box", 2000, 30, 40, 4, 2)); + instanceArray.insert(instanceArray.end(), 15, new packer.Wrapper("Tablet-Box", 2500, 20, 28, 2, 0)); + + /////////////////////////// + // BEGINS PACKING + /////////////////////////// + // CONSTRUCT PACKER + let my_packer: bws.packer.Packer = new packer.Packer(wrapperArray, instanceArray); + + /////// + // PACK (OPTIMIZE) + let result: bws.packer.WrapperArray = my_packer.optimize(); + /////// + + /////////////////////////// + // TRACE PACKING RESULT + /////////////////////////// + let xml: samchon.library.XML = result.toXML(); + console.log(xml.toString()); +} + +main(); \ No newline at end of file diff --git a/3d-bin-packing/3d-bin-packing.d.ts b/3d-bin-packing/3d-bin-packing.d.ts new file mode 100644 index 0000000000..2461538194 --- /dev/null +++ b/3d-bin-packing/3d-bin-packing.d.ts @@ -0,0 +1,1500 @@ +// Type definitions for 3d-bin-packing +// Project: https://github.com/betterwaysystems/packer +// Definitions by: Jeongho Nam +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// +/// +/// +/// + +declare module "3d-bin-packing" +{ + export = bws.packer; +} +declare var ReactDataGrid: typeof AdazzleReactDataGrid.ReactDataGrid; +declare namespace boxologic { + /** + *

An abstract instance of boxologic.

+ * + *

{@link st_Instance} represents a physical, tangible instance of 3-dimension.

+ * + * @author Jeongho Nam + */ + abstract class Instance { + /** + * Width of the tangible instance, length on the X-axis in 3D. + */ + width: number; + /** + * Height of the tangible instance, length on the Y-axis in 3D. + */ + height: number; + /** + * Length of the tangible instance, length on the Z-axis in 3D. + */ + length: number; + /** + * Width considering layout placement. + */ + layout_width: number; + /** + * Height considering layout placement. + */ + layout_height: number; + /** + * Length considering layout placement. + */ + layout_length: number; + /** + * Volume, Width x Height x Length. + */ + volume: number; + /** + * Construct from size members. + * + * @param width Width, length on the X-axis in 3D. + * @param height Height, length on the Y-axis in 3D. + * @param length Length, length on the Z-axis in 3D. + */ + constructor(width: number, height: number, length: number); + } +} +declare namespace bws.packer { + /** + * @brief Packer, a solver of 3d bin packing with multiple wrappers. + * + * @details + *

Packer is a facade class supporting packing operations in user side. You can solve a packing problem + * by constructing Packer class with {@link WrapperArray wrappers} and {@link InstanceArray instances} to + * pack and executing {@link optimize Packer.optimize()} method.

+ * + *

In background side, deducting packing solution, those algorithms are used.

+ * + * + * @author Jeongho Nam + */ + class Packer extends samchon.protocol.Entity { + /** + * Candidate wrappers who can contain instances. + */ + protected wrapperArray: WrapperArray; + /** + * Instances trying to pack into the wrapper. + */ + protected instanceArray: InstanceArray; + /** + * Default Constructor. + */ + constructor(); + /** + * Construct from members. + * + * @param wrapperArray Candidate wrappers who can contain instances. + * @param instanceArray Instances to be packed into some wrappers. + */ + constructor(wrapperArray: WrapperArray, instanceArray: InstanceArray); + /** + * @inheritdoc + */ + construct(xml: samchon.library.XML): void; + /** + * Get wrapperArray. + */ + getWrapperArray(): WrapperArray; + /** + * Get instanceArray. + */ + getInstanceArray(): InstanceArray; + /** + *

Deduct + * + */ + optimize(): WrapperArray; + /** + * @brief Initialize sequence list (gene_array). + * + * @details + *

Deducts initial sequence list by such assumption:

+ * + *
    + *
  • Cost of larger wrapper is less than smaller one, within framework of price per volume unit.
  • + *
      + *
    • Wrapper Larger: (price: $1,000, volume: 100cm^3 -> price per volume unit: $10 / cm^3)
    • + *
    • Wrapper Smaller: (price: $700, volume: 50cm^3 -> price per volume unit: $14 / cm^3)
    • + *
    • Larger's cost is less than Smaller, within framework of price per volume unit
    • + *
    + *
+ * + *

Method {@link initGenes initGenes()} constructs {@link WrapperGroup WrapperGroups} corresponding + * with the {@link wrapperArray} and allocates {@link instanceArray instances} to a {@link WrapperGroup}, + * has the smallest cost between containbles.

+ * + *

After executing packing solution by {@link WrapperGroup.optimize WrapperGroup.optimize()}, trying to + * {@link repack re-pack} each {@link WrapperGroup} to another type of {@link Wrapper}, deducts the best + * solution between them. It's the initial sequence list of genetic algorithm.

+ * + * @return Initial sequence list. + */ + protected initGenes(): GAWrapperArray; + /** + * Try to repack each wrappers to another type. + * + * @param $wrappers Wrappers to repack. + * @return Re-packed wrappers. + */ + protected repack($wrappers: WrapperArray): WrapperArray; + /** + * @inheritdoc + */ + TAG(): string; + /** + * @inheritdoc + */ + toXML(): samchon.library.XML; + } +} +declare namespace flex { + class TabNavigator extends React.Component { + render(): JSX.Element; + private handle_change(index, event); + } + class NavigatorContent extends React.Component { + render(): JSX.Element; + } + interface TabNavigatorProps extends React.Props { + selectedIndex?: number; + style?: React.CSSProperties; + } + interface NavigatorContentProps extends React.Props { + label: string; + } +} +declare namespace boxologic { + /** + * A box, trying to pack into a {@link Pallet}. + * + * @author Bill Knechtel,
+ * Migrated and Refactored by Jeongho Nam + */ + class Box extends Instance { + /** + * Coordinate-X of the box placement in a {@link Pallet}. + */ + cox: number; + /** + * Coordinate-Y of the box placement in a {@link Pallet}. + */ + coy: number; + /** + * Coordinate-Z of the box placement in a {@link Pallet}. + */ + coz: number; + /** + * Whether the {@link Box} is packed into a {@link Pallet}. + */ + is_packed: boolean; + overlapped_boxes: std.HashSet; + /** + * Construct from an instance. + * + * @param instance An instance adapts with. + */ + constructor(instance: bws.packer.Instance); + hit_test(obj: Box): boolean; + private hit_test_single(obj); + private hit_test_point(x, y, z); + } +} +/** + *

A set of programs that calculate the best fit for boxes on a pallet migrated from language C.

+ * + *
    + *
  • Original Boxologic: https://github.com/exad/boxologic
  • + *
+ * + * @author Bill Knechtel,
+ * Migrated and Refactored by Jeongho Nam + */ +declare namespace boxologic { + /** + *

A facade class of boxologic.

+ * + *

The Boxologic class dudcts the best solution of packing boxes to a pallet.

+ * + *
    + *
  • Reference: https://github.com/exad/boxologic
  • + *
+ * + * @author Bill Knechtel,
+ * Migrated and Refactored by Jeongho Nam + */ + class Boxologic { + /** + * A Wrapper to pack instances. + */ + private wrapper; + /** + * Instances trying to put into the wrapper. + */ + private instanceArray; + /** + * Instances failed to pack by overloading. + */ + private leftInstances; + /** + * A pallet containing {@link Box boxes}. + * + * @see Wrapper + */ + private pallet; + /** + * Boxes, trying to pack into the {@link pallet}. + */ + private box_array; + /** + * Sum of all boxes' volume. + */ + private total_box_volume; + /** + *

All different lengths of {@link box_array all box} dimensions along with evaluation values.

+ * + *

In other word, the layer_map stores those entries; each {@link Boxbox}'s length on each + * axis as a key (width, height or length) and evaluation value as a value. The evaluation + * value means sum of minimum gaps between the key and other {@link Box boxes}' width, height and length + *

+ * + * + FOR i := 0 to box_array.size() + WHILE key IN width, length and height in box_array[i] + BEGIN + value := 0 + FOR j to box_array.size() + value += min + ( + abs(key - box_array[j].width), + abs(key - box_array[j].height), + abs(key - box_array[j].length) + ) + layer_map.insert({key, value}); + END + * + * + *
    + *
  • key: A dimension value
  • + *
  • value: Evaluation weight value for the corresponding key.
  • + *
+ */ + private layer_map; + /** + * {@link List} of {@link Scrapped} instances, edges of layers under construction. + * + * @see Scrapped + * @see scrap_min_z + */ + private scrap_list; + /** + * The topology {@link Scrapped}, the edge of the current layer under construction. + * + * @see Scrapped + * @see scrap_list + */ + private scrap_min_z; + /** + * Index of the current {@link box}. + */ + private cboxi; + /** + * Candidate {@link Box.layout_width layout_width} of the {@link cboxi current box}. + */ + private cbox_layout_width; + /** + * Candidate {@link Box.layout_height layout_height} of the {@link cboxi current box}. + */ + private cbox_layout_height; + /** + * Candidate {@link Box.layout_length layout_length} of the {@link cboxi current box}. + */ + private cbox_layout_length; + /** + * Current layer's key on iteration. + */ + private layer_thickness; + /** + * Previous layer's key had iterated. + */ + private pre_layer; + /** + * Key of the unevened layer in the current packing layer. + */ + private layer_in_layer; + /** + * Little Z, gotten from {@link Scrapped.cumz cumz} in {@link min_scrap_z} + */ + private lilz; + /** + * Remained (unfilled) {@link Pallet.layout_height layout_height} of the {@link pallet}. + */ + private remain_layout_height; + /** + * Remained (unfilled) {@link Pallet.layout_length layout_length} of the {@link pallet}. + */ + private remain_layout_length; + /** + * Packed (filled) {@link Pallet.layout_height layout_height} of the {@link pallet}. + */ + private packed_layout_height; + /** + * Packed {@link Pallet.vo1lume volume} of the {@lnk pallet}. + */ + private packed_volume; + private boxi; + private bboxi; + private boxx; + private boxy; + private boxz; + private bboxx; + private bboxy; + private bboxz; + private bfx; + private bfy; + private bfz; + private bbfx; + private bbfy; + private bbfz; + /** + *

Whether the packing is on progress.

+ * + *

The {@link packing} is a flag variable for terminating iterations in + * {@link iterate_orientations iterate_orientations()}, who deducts the best packing solution.

+ */ + private packing; + /** + * Whether packing a layer is done. + */ + private layer_done; + /** + * Whether the current packing layer is evened. + */ + private evened; + /** + * Whether the best solution is deducted. + */ + private packing_best; + /** + * Whether the utilization degree of pallet space is 100%. + */ + private hundred_percent; + /** + * The best orientation of the pallet, which can deduct the {@link best_solution_volume}. + */ + private best_orientation; + /** + * The best layer, which can deduct the {@link best_solution_volume}. + */ + private best_layer; + /** + * The best volume, fit the best utilization degree of the pallet space. + */ + private best_solution_volume; + /** + * Construct from a wrapper and instances. + * + * @param wrapper A Wrapper to pack instances. + * @param instanceArray Instances trying to put into the wrapper. + */ + constructor(wrapper: bws.packer.Wrapper, instanceArray: bws.packer.InstanceArray); + /** + *

Encode data

+ * + *

Encodes {@link bws.packer Packer}'s data to be suitable for the + * {@link boxologic Boxologic}'s parametric data.

+ */ + private encode(); + /** + *

Decode data

+ * + *

Decodes the Boxologic's optimization result data to be suitable for the Packer's own.

+ */ + private decode(); + private inspect_validity(); + /** + *

Pack instances to the {@link wrapper}.

+ * + *

The {@link Boxologic.pack} is an adaptor method between {@link bws.packer Packer} and + * {@link boxologic}. It encodes data from {@link bws.packer Packer}, deducts the best packing + * solution decodes the optimization result and returns it.

+ * + *

The optimization result is returned as a {@link Pair} like below:

+ *
    + *
  • first: The {@link wrapper} with packed instances.
  • + *
  • second: {@link leftInstances Left instances failed to pack} by overloading.
  • + *
+ * + * @return A pair of {@link wrapper} with packed instances and + * {@link leftInstances instances failed to pack} by overloading. + */ + pack(): std.Pair; + /** + *

Execute iterations by calling proper functions.

+ * + *

Iterations are done and parameters of the best solution are found.

+ */ + private iterate_orientations(); + /** + * Iterate a layer. + * + * @param thickness Thickness of the iterating layer. + */ + private iterate_layer(thickness); + /** + *

Construct layers.

+ * + *

Creates all possible layer heights by giving a weight value to each of them.

+ */ + private construct_layers(); + /** + *

Packs the boxes found and arranges all variables and records properly.

+ * + *

Update the linked list and the Boxlist[] array as a box is packed.

+ */ + private pack_layer(); + /** + * Find the most proper layer height by looking at the unpacked boxes and + * the remaining empty space available. + */ + private find_layer(thickness); + /** + *

Determine the gap with the samllest z value in the current layer.

+ * + *

Find the most proper boxes by looking at all six possible orientations, + * empty space given, adjacent boxes, and pallet limits.

+ * + * @param hmx Maximum available x-dimension of the current gap to be filled. + * @param hy Current layer thickness value. + * @param hmy Current layer thickness value. + * @param hz Z-dimension of the current gap to be filled. + * @param hmz Maximum available z-dimension to the current gap to be filled. + */ + private find_box(hmx, hy, hmy, hz, hmz); + /** + *

Analyzes each unpacked {@link Box box} to find the best fitting one to the empty space.

+ * + *

Used by {@link find_box find_box()} to analyze box dimensions.

+ * + * @param x index of a {@link Box box} in the {@link box_array}. + * + * @param hmx Maximum available x-dimension of the current gap to be filled. + * @param hy Current layer thickness value. + * @param hmy Current layer thickness value. + * @param hz Z-dimension of the current gap to be filled. + * @param hmz Maximum available z-dimension to the current gap to be filled. + * + * @param dim1 X-dimension of the orientation of the box being examined. + * @param dim2 Y-dimension of the orientation of the box being examined. + * @param dim3 Z-dimension of the orientation of the box being examined. + */ + private analyze_box(index, hmx, hy, hmy, hz, hmz, dim1, dim2, dim3); + /** + * After finding each box, the candidate boxes and the condition of the layer are examined. + */ + private check_found(); + /** + * After packing of each box, 100% packing condition is checked. + */ + private volume_check(); + /** + *

Find the first to be packed gap in the layer edge.

+ * + *

Determine the gap with the {@link scrap_min_z smallest z} value in the current layer.

+ */ + private find_smallest_z(); + /** + *

Determine {@link box_arrray boxes}.

+ * + *

Using the parameters found, packs the best solution found and reports.

+ */ + private report_results(); + /** + *

Determine a {@link Box}.

+ * + *

Transforms the found co-ordinate system to the one entered by the user and write them to the + * report.

+ */ + private write_box_file(); + } +} +declare namespace boxologic { + /** + * A pallet containing boxes. + * + * @author Bill Knechtel,
+ * Migrated and Refactored by Jeongho Nam + */ + class Pallet extends Instance { + /** + * Construct from a wrapper. + * + * @param wrapper A wrapper wrapping instances. + */ + constructor(wrapper: bws.packer.Wrapper); + /** + * Set placement orientation. + */ + set_orientation(orientation: number): void; + } +} +declare namespace boxologic { + /** + *

Cumulated lengths of current layer.

+ * + *

{@link Scrapped} represents an edge of the current layer under construction.

+ * + * @author Bill Knechtel,
+ * Migrated and Refactored by Jeongho Nam + */ + class Scrap { + /** + * Cumulated length on the X-axis in 3D. + */ + cumx: number; + /** + * Cumulated length on the Z-axis in 3D. + */ + cumz: number; + /** + * Default Constructor. + */ + constructor(); + /** + * Initialization Constructor. + * + * @param cumx Cumulated length on the x-axis. + * @param cumz Cumulated length on the z-axis. + */ + constructor(cumx: number, cumz: number); + } +} +declare namespace bws.packer { + /** + * Bridge of {@link Packer} for {@link InstanceForm repeated instances}. + * + * @author Jeongho Nam + */ + class PackerForm extends samchon.protocol.Entity { + /** + * Form of Instances to pack. + */ + private instanceFormArray; + /** + * Type of Wrappers to be used. + */ + private wrapperArray; + /** + * Default Constructor. + */ + constructor(); + /** + * Initializer Constructor. + * + * @param instanceFormArray Form of Instances to pack. + * @param wrapperArray Type of Wrappers to be used. + */ + constructor(instanceFormArray: InstanceFormArray, wrapperArray: WrapperArray); + construct(xml: samchon.library.XML): void; + optimize(): WrapperArray; + getInstanceFormArray(): InstanceFormArray; + getWrapperArray(): WrapperArray; + TAG(): string; + toXML(): samchon.library.XML; + toPacker(): Packer; + } + /** + * An array of {@link InstanceForm} objects. + * + * @author Jeongho Nam + */ + class InstanceFormArray extends samchon.protocol.EntityArrayCollection { + /** + * Default Constructor. + */ + constructor(); + createChild(xml: samchon.library.XML): InstanceForm; + TAG(): string; + CHILD_TAG(): string; + /** + * Convert {@link InstanceForm} objects to {@link InstanceArray}. + * + * @return An array of instance containing repeated instances in {@link InstanceForm} objects. + */ + toInstanceArray(): InstanceArray; + } + /** + *

A repeated Instance.

+ * + *

InstanceForm is an utility class for repeated {@link Instance}. It is designed for shrinking + * volume of network message I/O by storing {@link count repeated count}.

+ * + * @author Jeongho Nam + */ + class InstanceForm extends samchon.protocol.Entity { + /** + * A duplicated Instance. + */ + private instance; + /** + * Repeated count of the {@link instance}. + */ + private count; + /** + * Default Constructor. + */ + constructor(instance?: Instance, count?: number); + /** + * @inheritdoc + */ + construct(xml: samchon.library.XML): void; + private createInstance(xml); + key(): any; + getInstance(): Instance; + getCount(): number; + setCount(val: number): void; + $name: string; + $width: string; + $height: string; + $length: string; + $count: string; + /** + * @inheritdoc + */ + TAG(): string; + /** + * @inheritdoc + */ + toXML(): samchon.library.XML; + /** + *

Repeated {@link instance} to {@link InstanceArray}. + * + * @details + *

Contains the {@link instance repeated instance} to an {@link InstanceArray} to make + * {@link instance} to participate in the packing process. The returned {@link InstanceArray} will be + * registered on {@link Packer.instanceArray}. + * + * @return An array of instance containing repeated {@link instance}. + */ + toInstanceArray(): InstanceArray; + } +} +declare namespace bws.packer { + class WrapperArray extends samchon.protocol.EntityArrayCollection { + /** + * Default Constructor. + */ + constructor(); + /** + * @inheritdoc + */ + createChild(xml: samchon.library.XML): Wrapper; + /** + * Get (calculate) price. + */ + getPrice(): number; + /** + * Get (calculate) utilization rate. + */ + getUtilization(): number; + /** + * @inheritdoc + */ + TAG(): string; + /** + * @inheritdoc + */ + CHILD_TAG(): string; + } +} +declare namespace bws.packer { + class GAWrapperArray extends WrapperArray { + protected instanceArray: InstanceArray; + protected result: std.HashMap; + private price; + /** + * @brief Validity of this sequence list. + */ + private valid; + /** + * Construct from instances. + * + * @param instanceArray Instances to be wrapped. + */ + constructor(instanceArray: InstanceArray); + /** + * @brief Copy Constructor. + */ + constructor(genes: GAWrapperArray); + private constructResult(); + /** + * @brief Get optimization result. + * + * @return result map. + */ + getResult(): std.HashMap; + less(obj: GAWrapperArray): boolean; + } +} +declare namespace bws.packer { + /** + * An interface of physical 3D-instances. + * + * @author Jeongho Nam + */ + interface Instance extends samchon.protocol.IEntity { + /** + * Get name. + */ + getName(): string; + /** + * Get width, length on the X-axis in 3D. + */ + getWidth(): number; + /** + * Get height, length on the Y-axis in 3D. + */ + getHeight(): number; + /** + * Get length, length on the Z-axis in 3D. + */ + getLength(): number; + /** + * Get (calculate) volume. + * + * @return width x height x length + */ + getVolume(): number; + /** + * Set name. + */ + setName(val: string): void; + /** + * Set width, length on the X-axis in 3D. + */ + setWidth(val: number): void; + /** + * Set height, length on the Y-axis in 3D. + */ + setHeight(val: number): void; + /** + * Set length, length on the Z-axis in 3D. + */ + setLength(val: number): void; + /** + *

A type, identifier of derived class.

+ * + *

Derived types

+ *