export default class Matrix {
  constructor (id, matrix, locus, resolution, orientation, measures) {
    this.dim = matrix.length;
    this.id = id;
    this.locus = locus;
    this.matrix = Matrix.to1d(matrix);
    this.measures = measures;
    this.orientation = orientation;
    this.orientationX = this.orientation.strand1 === 'coding' ? 1 : -1;
    this.orientationY = this.orientation.strand2 === 'coding' ? 1 : -1;
    this.visible = true;
    this.resolution = resolution;
  }

  /**
   * Flatten  a 2D array.
   *
   * @param {array} matrix - 2D matrix to be flattened.
   * @return {array} 1D arrays.
   */
  static flatten (matrix) {
    return matrix.reduce((a, b) => a.concat(b), []);
  }

  /**
   * Flip the raw matix's x axis in-place.
   *
   * @param {array} matrix - 2D matrix to be flipped.
   * @return {number} Orientation.
   */
  flipX () {
    Matrix.flipX(this.matrix);
    this.orientationX *= -1;
  }

  /**
   * Flip the raw matix's x axis in-place.
   *
   * @param {array} matrix - 2D matrix to be flipped.
   * @return {number} Orientation.
   */
  static flipX (matrix, dim) {
    switch (dim) {
      case '1D':
        Matrix.flipX1d(matrix);
        break;

      default:
        Matrix.flipX2d(matrix);
        break;
    }
  }

  /**
   * Flip the raw matix's x axis in-place.
   *
   * @param {array} matrix - 1D matrix to be flipped.
   * @return {number} Orientation.
   */
  static flipX1d (matrix) {
    const dims = Math.round(Math.sqrt(matrix.length));

    for (let i = 0; i < dims; i++) {
      const pos = (i * dims);

      matrix.set(matrix.slice(pos, dims).reverse(), pos);
    }
  }

  /**
   * Flip the raw matix's x axis in-place.
   *
   * @param {array} matrix - 2D matrix to be flipped.
   * @return {number} Orientation.
   */
  static flipX2d (matrix) {
    matrix.map(row => row.reverse());
  }

  /**
   * Flip the raw matix's x axis in-place.
   *
   * @param {array} matrix - 2D matrix to be flipped.
   * @return {number} Orientation.
   */
  flipY () {
    Matrix.flipY(this.matrix);
    this.orientationY *= -1;
  }

  /**
   * Flip the raw matix's y axis in-place.
   *
   * @param {array} matrix - Matrix to be flipped.
   * @return {object} Orientation.
   */
  static flipY (matrix, dim) {
    switch (dim) {
      case '1D':
        Matrix.flipY1d(matrix);
        break;

      default:
        Matrix.flipY2d(matrix);
        break;
    }
  }

  /**
   * Flip the raw matix's y axis in-place.
   *
   * @param {array} matrix - 1D-array matrix to be flipped.
   * @return {object} Orientation.
   */
  static flipY1d (matrix) {
    const dims = Math.round(Math.sqrt(matrix.length));
    const flippedMatrix = new Float32Array(matrix.length);

    for (let i = 0; i < dims; i++) {
      flippedMatrix.set(matrix.slice((i * dims), dims), (dims - 1 - i) * dims);
    }
  }

  /**
   * Flip the raw matix's y axis in-place.
   *
   * @param {array} matrix - 2D matrix to be flipped.
   * @return {object} Orientation.
   */
  static flipY2d (matrix) {
    matrix.reverse();
  }

  /**
   * Convert a 2D classic array into a 1D Float32Array.
   *
   * @param {array} matrix2d - 2D array to be converted into 1D.
   * @return  {[type]} 1D Floast32Array
   */
  static to1d (matrix2d) {
    return Float32Array.from(
      matrix2d.reduce((matrix1d, row) => matrix1d.concat(row), [])
    );
  }
}

@@ -951,344 +951,10 @@
 raw 
-matix's x axis in-place.%0A   *%0A   * @param %7Barray%7D matrix - 2D matrix to be flipped.%0A   * @return %7Bnumber%7D Orientation.%0A   */%0A  static flipX (matrix, dim) %7B%0A    switch (dim) %7B%0A      case '1D':%0A        Matrix.flipX1d(matrix);%0A        break;%0A%0A      default:%0A        Matrix.flipX2d(matrix);%0A        break;%0A    %7D%0A  %7D%0A%0A  /**%0A   * Flip the raw
+1D
  mat
@@ -1089,18 +1089,16 @@
 ic flipX
-1d
  (matrix
@@ -1259,16 +1259,26 @@
 ice(pos,
+ (i + 1) *
  dims).r
@@ -1459,661 +1459,110 @@
 /%0A  
-static flipX2d (matrix) %7B%0A    matrix.map(row =%3E row.reverse());%0A  %7D%0A%0A  /**%0A   * Flip the raw matix's x axis in-place.%0A   *%0A   * @param %7Barray%7D matrix - 2D matrix to be flipped.%0A   * @return %7Bnumber%7D Orientation.%0A   */%0A  flipY () %7B%0A    Matrix.flipY(this.matrix);%0A    this.orientationY *= -1;%0A  %7D%0A%0A  /**%0A   * Flip the raw matix's y axis in-place.%0A   *%0A   * @param %7Barray%7D matrix - Matrix to be flipped.%0A   * @return %7Bobject%7D Orientation.%0A   */%0A  static flipY (matrix, dim) %7B%0A    switch (dim) %7B%0A      case '1D':%0A        Matrix.flipY1d(matrix);%0A        break;%0A%0A      default:%0A        Matrix.flipY2d(matrix);%0A        break;%0A    %7D%0A  %7D%0A%0A  /**%0A   * Flip the raw
+flipY () %7B%0A    Matrix.flipY(this.matrix);%0A    this.orientationY *= -1;%0A  %7D%0A%0A  /**%0A   * Flip the raw 1D
  mat
@@ -1626,17 +1626,17 @@
 D-array 
-m
+M
 atrix to
@@ -1703,26 +1703,24 @@
 ic flipY
-1d
  (matrix
 ) %7B%0A    
@@ -1703,32 +1703,37 @@
 ic flipY (matrix
+, dim
 ) %7B%0A    const di
@@ -1888,32 +1888,41 @@
 ippedMatrix.set(
+%0A        
 matrix.slice((i 
@@ -1918,25 +1918,24 @@
 x.slice(
-(
 i * dims
 ), dims)
@@ -1930,31 +1930,16 @@
 dims
-), dims), (dims - 1 - i
+, (i + 1
 ) * 
@@ -1947,217 +1947,84 @@
 ims)
-;
+,
 %0A    
-%7D%0A  %7D%0A%0A  /**%0A   * Flip the raw matix's y axis in-place.%0A   *
+    (dims - 1 - i) * dims%0A      );
 %0A   
-* @param %7Barray%7D matrix - 2D matrix to be flipped.%0A   * @return %7Bobject%7D Orientation.%0A   */%0A  static flipY2d (matrix) %7B%0A    matrix.reverse(
+ %7D%0A%0A    matrix.set(flippedMatrix
 );%0A 

Remove 2D array flipping (2D arrays are not used anymore). Fix flipping code.