<commit_before>/**
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

goog.provide('gf.vec.Viewport');

goog.require('gf.vec.Containment');
goog.require('gf.vec.Ray');
/** @suppress {extraRequire} */
goog.require('goog.math.Size');
goog.require('goog.vec.Mat4');
goog.require('goog.vec.Vec3');
goog.require('goog.vec.Vec4');



/**
 * Viewport utility.
 *
 * @constructor
 */
gf.vec.Viewport = function() {
  /**
   * Viewport width, in px.
   * @type {number}
   */
  this.width = 100;

  /**
   * Viewport height, in px.
   * @type {number}
   */
  this.height = 100;

  /**
   * Distance to the near clipping plane.
   * @type {number}
   */
  this.near = 0.1;

  /**
   * Distance to the far clipping plane.
   * @type {number}
   */
  this.far = 100;

  /**
   * Orthographic projection matrix, useful for 2D.
   * @type {!goog.vec.Mat4.Type}
   */
  this.orthoMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Projection matrix.
   * @type {!goog.vec.Mat4.Type}
   */
  this.projMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * View matrix.
   * @type {!goog.vec.Mat4.Type}
   */
  this.viewMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Inverse view matrix.
   * @type {!goog.vec.Mat4.Type}
   */
  this.inverseViewMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Projection x View matrix.
   * @type {!goog.vec.Mat4.Type}
   */
  this.viewProjMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Inverse viewProj for fast ray casting.
   * @type {!goog.vec.Mat4.Type}
   */
  this.inverseViewProjMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Calculated world position.
   * @type {!goog.vec.Vec3.Float32}
   */
  this.position = goog.vec.Vec3.createFloat32();

  /**
   * View normal.
   * @type {!goog.vec.Vec3.Float32}
   */
  this.direction = goog.vec.Vec3.createFloat32();

  /**
   * 6 planes representing the view frustum.
   * @private
   * @type {!Array.<!goog.vec.Vec4.Float32>}
   */
  this.frustumPlanes_ = [
    goog.vec.Vec4.createFloat32(), // L
    goog.vec.Vec4.createFloat32(), // R
    goog.vec.Vec4.createFloat32(), // T
    goog.vec.Vec4.createFloat32(), // B
    goog.vec.Vec4.createFloat32(), // N
    goog.vec.Vec4.createFloat32()  // F
  ];
};


/**
 * Frustum plane index values.
 * @private
 * @enum {number}
 */
gf.vec.Viewport.FrustumPlane_ = {
  LEFT: 0,
  RIGHT: 1,
  TOP: 2,
  BOTTOM: 3,
  NEAR: 4,
  FAR: 5
};


/**
 * Resets the viewport before processing a frame.
 * @param {number} width New viewport width, in px.
 * @param {number} height New viewport height, in px.
 */
gf.vec.Viewport.prototype.reset = function(width, height) {
  if (this.width != width || this.height != height) {
    this.width = width;
    this.height = height;

    var x = 2 / this.width;
    var y = 2 / -this.height;
    goog.vec.Mat4.setFromValues(this.orthoMatrix,
        x, 0, 0, 0,
        0, y, 0, 0,
        0, 0, 1, 0,
        -1, 1, 0, 1);

    var fovy = Math.PI / 4;
    var aspect = this.width / this.height;
    goog.vec.Mat4.makePerspective(
        this.projMatrix,
        fovy, aspect,
        this.near, this.far);

    goog.vec.Mat4.multMat(
        this.projMatrix,
        this.viewMatrix,
        this.viewProjMatrix);
    goog.vec.Mat4.invert(this.viewProjMatrix, this.inverseViewProjMatrix);
  }
};


/**
 * Calculates all dependent values, assuming only the projection and view
 * matrices are up to date.
 */
gf.vec.Viewport.prototype.calculate = function() {
  // Update matrices
  goog.vec.Mat4.multMat(this.projMatrix, this.viewMatrix, this.viewProjMatrix);
  goog.vec.Mat4.invert(this.viewMatrix, this.inverseViewMatrix);
  goog.vec.Mat4.invert(this.viewProjMatrix, this.inverseViewProjMatrix);

  // Calculate view frustum planes
  var vpm = this.viewProjMatrix;
  var planes = this.frustumPlanes_;<commit_msg>Adding an aspect ratio override for viewport<commit_after>/**
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

goog.provide('gf.vec.Viewport');

goog.require('gf.vec.Containment');
goog.require('gf.vec.Ray');
/** @suppress {extraRequire} */
goog.require('goog.math.Size');
goog.require('goog.vec.Mat4');
goog.require('goog.vec.Vec3');
goog.require('goog.vec.Vec4');



/**
 * Viewport utility.
 *
 * @constructor
 */
gf.vec.Viewport = function() {
  /**
   * Viewport width, in px.
   * @type {number}
   */
  this.width = 100;

  /**
   * Viewport height, in px.
   * @type {number}
   */
  this.height = 100;

  /**
   * Distance to the near clipping plane.
   * @type {number}
   */
  this.near = 0.1;

  /**
   * Distance to the far clipping plane.
   * @type {number}
   */
  this.far = 100;

  /**
   * Orthographic projection matrix, useful for 2D.
   * @type {!goog.vec.Mat4.Type}
   */
  this.orthoMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Projection matrix.
   * @type {!goog.vec.Mat4.Type}
   */
  this.projMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * View matrix.
   * @type {!goog.vec.Mat4.Type}
   */
  this.viewMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Inverse view matrix.
   * @type {!goog.vec.Mat4.Type}
   */
  this.inverseViewMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Projection x View matrix.
   * @type {!goog.vec.Mat4.Type}
   */
  this.viewProjMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Inverse viewProj for fast ray casting.
   * @type {!goog.vec.Mat4.Type}
   */
  this.inverseViewProjMatrix = goog.vec.Mat4.createFloat32Identity();

  /**
   * Calculated world position.
   * @type {!goog.vec.Vec3.Float32}
   */
  this.position = goog.vec.Vec3.createFloat32();

  /**
   * View normal.
   * @type {!goog.vec.Vec3.Float32}
   */
  this.direction = goog.vec.Vec3.createFloat32();

  /**
   * 6 planes representing the view frustum.
   * @private
   * @type {!Array.<!goog.vec.Vec4.Float32>}
   */
  this.frustumPlanes_ = [
    goog.vec.Vec4.createFloat32(), // L
    goog.vec.Vec4.createFloat32(), // R
    goog.vec.Vec4.createFloat32(), // T
    goog.vec.Vec4.createFloat32(), // B
    goog.vec.Vec4.createFloat32(), // N
    goog.vec.Vec4.createFloat32()  // F
  ];
};


/**
 * Frustum plane index values.
 * @private
 * @enum {number}
 */
gf.vec.Viewport.FrustumPlane_ = {
  LEFT: 0,
  RIGHT: 1,
  TOP: 2,
  BOTTOM: 3,
  NEAR: 4,
  FAR: 5
};


/**
 * Resets the viewport before processing a frame.
 * @param {number} width New viewport width, in px.
 * @param {number} height New viewport height, in px.
 * @param {number=} opt_aspectRatio Override the aspect ratio.
 */
gf.vec.Viewport.prototype.reset = function(width, height, opt_aspectRatio) {
  if (this.width != width || this.height != height) {
    this.width = width;
    this.height = height;

    var x = 2 / this.width;
    var y = 2 / -this.height;
    goog.vec.Mat4.setFromValues(this.orthoMatrix,
        x, 0, 0, 0,
        0, y, 0, 0,
        0, 0, 1, 0,
        -1, 1, 0, 1);

    var fovy = Math.PI / 4;
    var aspectRatio = opt_aspectRatio || (this.width / this.height);
    goog.vec.Mat4.makePerspective(
        this.projMatrix,
        fovy, aspectRatio,
        this.near, this.far);

    // TODO(benvanik): remove these - require calculate after reset
    goog.vec.Mat4.multMat(
        this.projMatrix,
        this.viewMatrix,
        this.viewProjMatrix);
    goog.vec.Mat4.invert(this.viewProjMatrix, this.inverseViewProjMatrix);
  }
};


/**
 * Calculates all dependent values, assuming only the projection and view
 * matrices are up to date.
 */
gf.vec.Viewport.prototype.calculate = function() {
  // Update matrices
  goog.vec.Mat4.multMat(this.projMatrix, this.viewMatrix, this.viewProjMatrix);
  goog.vec.Mat4.invert(this.viewMatrix, this.inverseViewMatrix);
  goog.vec.Mat4.invert(this.viewProjMatrix, this.inverseViewProjMatrix);

  // Calculate view frustum planes
  var vpm = this.viewProjMatrix;
  var planes = this.frustumPlanes_;
{'fraction_non_alphanumeric': 0.14776517678452303, 'fraction_numerical': 0.024683122081387593, 'mean_word_length': 2.817911714770798, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3673876', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3107, 'n_words': 729}