<commit_before>module microtome.slicer {

  // TODO Move to threed?
  export const Z_DOWN = new THREE.Vector3(0, 0, -1000000);
  export const Z_UP = new THREE.Vector3(0, 0, 1000000);
  export const ORIGIN = new THREE.Vector3(0, 0, 0);
  export const POS_X = new THREE.Vector3(1, 0, 0);
  export const NEG_X = new THREE.Vector3(-1, 0, 0);
  export const POS_Y = new THREE.Vector3(0, 1, 0);
  export const NEG_Y = new THREE.Vector3(0, -1, 0);
  export const POS_Z = new THREE.Vector3(0, 0, 1);
  export const NEG_Z = new THREE.Vector3(0, 0, -1);

  export const FAR_Z_PADDING: number = 1.0;
  export const CAMERA_NEAR: number = 1.0;
  export const SLICER_BACKGROUND_Z = -0.1;

  /**
  Advanced slicer supporting shelling, support pattern generation,
  hollow shells, etc.
  */
  export class AdvancedSlicer {

    // Slice camera
    private sliceCamera: THREE.OrthographicCamera = null;
    // z-shell camera
    private zShellCamera: THREE.OrthographicCamera = null;

    // Final composition target where final image is composed
    private finalCompositeTarget: THREE.WebGLRenderTarget = null;

    // Because of how we slice, we can't use stencil buffer
    private maskTarget: THREE.WebGLRenderTarget = null;

    // // Depth target
    // private depthTarget: THREE.WebGLRenderTarget = null;

    // Temp target 1
    private tempTarget1: THREE.WebGLRenderTarget = null;

    // Temp target 2
    private tempTarget2: THREE.WebGLRenderTarget = null;

    // Temp target 2
    private zshellTarget: THREE.WebGLRenderTarget = null;

    // Cache canvas width for dirty checking
    private lastWidth: number = -1;
    // Cache canvas height for dirty checking
    private lastHeight: number = -1;

    // In order for a shell to be fully connected, min pixel
    // count is 3 in x or y.
    private static _MIN_SHELL_PIXELS = 3;

    private sliceBackground: THREE.Mesh;

    private raftDilatePixels = 0;

    private shellErodePixels = 0;

    // Materials ---------------------------------------------------------------------------------

    private erodeDialateMaterial = three_d.CoreMaterialsFactory.erodeOrDialateMaterial.clone();

    private erodeDialateMaterialUniforms = new three_d.ErodeDialateShaderUniforms(
      new three_d.IntegerUniform(1),
      new three_d.IntegerUniform(0),
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    private copyMaterial = three_d.CoreMaterialsFactory.copyMaterial.clone();

    private copyMaterialUniforms = new three_d.CopyShaderUniforms(
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    private xorMaterial = three_d.CoreMaterialsFactory.xorMaterial.clone();
    private xorMaterialUniforms = new three_d.BoolOpShaderUniforms(
      new three_d.TextureUniform(null),
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    private orMaterial = three_d.CoreMaterialsFactory.orMaterial.clone();
    private orMaterialUniforms = new three_d.BoolOpShaderUniforms(
      new three_d.TextureUniform(null),
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    private intersectionTestMaterial = three_d.CoreMaterialsFactory.intersectionMaterial.clone();

    private intersectionMaterialUniforms = new three_d.IntersectionShaderUniforms(
      new three_d.FloatUniform(0)
    );

    private sliceMaterial = three_d.CoreMaterialsFactory.sliceMaterial.clone();

    private sliceMaterialUniforms = new three_d.SliceShaderUniforms(
      new three_d.FloatUniform(0),
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    /**
    *
    */
    constructor(
      private scene: microtome.three_d.PrinterScene,
      public pixelWidthMM: number,
      public pixelHeightMM: number,
      public raftThickness: number,
      public raftOffset: number,
      public shellInset: number,
      private renderer?: THREE.WebGLRenderer) {
      var planeGeom: THREE.PlaneGeometry = new THREE.PlaneGeometry(1.0, 1.0);
      var planeMaterial = microtome.three_d.CoreMaterialsFactory.whiteMaterial.clone();
      planeMaterial.side = THREE.DoubleSide;
      this.sliceBackground = new THREE.Mesh(planeGeom, planeMaterial);
      this.sliceBackground.position.z = SLICER_BACKGROUND_Z;
      this.sliceBackground.visible = false;
      this.scene.add(this.sliceBackground);
      this.sliceCamera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5);
      this.zShellCamera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5);
      this.erodeDialateMaterial.uniforms = this.erodeDialateMaterialUniforms;
      this.copyMaterial.uniforms = this.copyMaterialUniforms;
      this.xorMaterial.uniforms = this.xorMaterialUniforms;
      this.orMaterial.uniforms = this.orMaterialUniforms;
      this.intersectionTestMaterial.uniforms = this.intersectionMaterialUniforms;
      this.sliceMaterial.uniforms = this.sliceMaterialUniforms;
    }

    /**
    * Slice the scene at the given z offset in mm.
    */
    sliceAt(z: number) {
      this.render(z);
    }

    /***
    * Slice to an image
    * Returns a dataurl of the image
    */
    sliceAtToImage(z: number): String {
      this.render(z);
      return this.renderer.domElement.toDataURL();
    }

    private render(z: number) {
      try {
        let dirty = this.prepareRender();
        if (z <= this.raftThickness) {
          this.renderRaftSlice();
        } else {
          this.renderSlice(z);
        }
      } finally {
        // Set everything back to normal if stuff goes south
        this.resetScene();
      }
    }

    private renderRaftSlice() {
      this.scene.printVolume.visible = false
      // Set model color to white,
      this.scene.overrideMaterial = microtome.three_d.CoreMaterialsFactory.flatWhiteMaterial;
      // Hide slice background if present, render to temp target 1;
      this.sliceBackground.visible = false;
      this.renderer.render(this.scene, this.sliceCamera, this.tempTarget2, true);

      // Hide objects, show slice background
      this.scene.hidePrintObjects();
      this.sliceBackground.visible = true;<commit_msg>Added units to name, and mimetype to generated data url<commit_after>module microtome.slicer {

  // TODO Move to threed?
  export const Z_DOWN = new THREE.Vector3(0, 0, -1000000);
  export const Z_UP = new THREE.Vector3(0, 0, 1000000);
  export const ORIGIN = new THREE.Vector3(0, 0, 0);
  export const POS_X = new THREE.Vector3(1, 0, 0);
  export const NEG_X = new THREE.Vector3(-1, 0, 0);
  export const POS_Y = new THREE.Vector3(0, 1, 0);
  export const NEG_Y = new THREE.Vector3(0, -1, 0);
  export const POS_Z = new THREE.Vector3(0, 0, 1);
  export const NEG_Z = new THREE.Vector3(0, 0, -1);

  export const FAR_Z_PADDING: number = 1.0;
  export const CAMERA_NEAR: number = 1.0;
  export const SLICER_BACKGROUND_Z = -0.1;

  /**
  Advanced slicer supporting shelling, support pattern generation,
  hollow shells, etc.
  */
  export class AdvancedSlicer {

    // Slice camera
    private sliceCamera: THREE.OrthographicCamera = null;
    // z-shell camera
    private zShellCamera: THREE.OrthographicCamera = null;

    // Final composition target where final image is composed
    private finalCompositeTarget: THREE.WebGLRenderTarget = null;

    // Because of how we slice, we can't use stencil buffer
    private maskTarget: THREE.WebGLRenderTarget = null;

    // // Depth target
    // private depthTarget: THREE.WebGLRenderTarget = null;

    // Temp target 1
    private tempTarget1: THREE.WebGLRenderTarget = null;

    // Temp target 2
    private tempTarget2: THREE.WebGLRenderTarget = null;

    // Temp target 2
    private zshellTarget: THREE.WebGLRenderTarget = null;

    // Cache canvas width for dirty checking
    private lastWidth: number = -1;
    // Cache canvas height for dirty checking
    private lastHeight: number = -1;

    // In order for a shell to be fully connected, min pixel
    // count is 3 in x or y.
    private static _MIN_SHELL_PIXELS = 3;

    private sliceBackground: THREE.Mesh;

    private raftDilatePixels = 0;

    private shellErodePixels = 0;

    // Materials ---------------------------------------------------------------------------------

    private erodeDialateMaterial = three_d.CoreMaterialsFactory.erodeOrDialateMaterial.clone();

    private erodeDialateMaterialUniforms = new three_d.ErodeDialateShaderUniforms(
      new three_d.IntegerUniform(1),
      new three_d.IntegerUniform(0),
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    private copyMaterial = three_d.CoreMaterialsFactory.copyMaterial.clone();

    private copyMaterialUniforms = new three_d.CopyShaderUniforms(
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    private xorMaterial = three_d.CoreMaterialsFactory.xorMaterial.clone();
    private xorMaterialUniforms = new three_d.BoolOpShaderUniforms(
      new three_d.TextureUniform(null),
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    private orMaterial = three_d.CoreMaterialsFactory.orMaterial.clone();
    private orMaterialUniforms = new three_d.BoolOpShaderUniforms(
      new three_d.TextureUniform(null),
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    private intersectionTestMaterial = three_d.CoreMaterialsFactory.intersectionMaterial.clone();

    private intersectionMaterialUniforms = new three_d.IntersectionShaderUniforms(
      new three_d.FloatUniform(0)
    );

    private sliceMaterial = three_d.CoreMaterialsFactory.sliceMaterial.clone();

    private sliceMaterialUniforms = new three_d.SliceShaderUniforms(
      new three_d.FloatUniform(0),
      new three_d.TextureUniform(null),
      new three_d.IntegerUniform(0),
      new three_d.IntegerUniform(0));

    /**
    *
    */
    constructor(
      private scene: microtome.three_d.PrinterScene,
      public pixelWidthMM: number,
      public pixelHeightMM: number,
      public raftThicknessMM: number,
      public raftOffset: number,
      public shellInset: number,
      private renderer: THREE.WebGLRenderer) {
      var planeGeom: THREE.PlaneGeometry = new THREE.PlaneGeometry(1.0, 1.0);
      var planeMaterial = microtome.three_d.CoreMaterialsFactory.whiteMaterial.clone();
      planeMaterial.side = THREE.DoubleSide;
      this.sliceBackground = new THREE.Mesh(planeGeom, planeMaterial);
      this.sliceBackground.position.z = SLICER_BACKGROUND_Z;
      this.sliceBackground.visible = false;
      this.scene.add(this.sliceBackground);
      this.sliceCamera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5);
      this.zShellCamera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5);
      this.erodeDialateMaterial.uniforms = this.erodeDialateMaterialUniforms;
      this.copyMaterial.uniforms = this.copyMaterialUniforms;
      this.xorMaterial.uniforms = this.xorMaterialUniforms;
      this.orMaterial.uniforms = this.orMaterialUniforms;
      this.intersectionTestMaterial.uniforms = this.intersectionMaterialUniforms;
      this.sliceMaterial.uniforms = this.sliceMaterialUniforms;
    }

    /**
    * Slice the scene at the given z offset in mm.
    */
    sliceAt(z: number) {
      this.render(z);
    }

    /***
    * Slice to an image
    * Returns a dataurl of the image
    */
    sliceAtToImage(z: number): String {
      this.render(z);
      return this.renderer.domElement.toDataURL("image/png");
    }

    private render(z: number) {
      try {
        let dirty = this.prepareRender();
        if (z <= this.raftThicknessMM) {
          this.renderRaftSlice();
        } else {
          this.renderSlice(z);
        }
      } finally {
        // Set everything back to normal if stuff goes south
        this.resetScene();
      }
    }

    private renderRaftSlice() {
      this.scene.printVolume.visible = false
      // Set model color to white,
      this.scene.overrideMaterial = microtome.three_d.CoreMaterialsFactory.flatWhiteMaterial;
      // Hide slice background if present, render to temp target 1;
      this.sliceBackground.visible = false;
      this.renderer.render(this.scene, this.sliceCamera, this.tempTarget2, true);

      // Hide objects, show slice background
      this.scene.hidePrintObjects();
      this.sliceBackground.visible = true;
{'fraction_non_alphanumeric': 0.10578290409227366, 'fraction_numerical': 0.015958287249170484, 'mean_word_length': 3.0098194488438392, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4407935', 'n_tokens_mistral': 3963, 'n_tokens_neox': 3714, 'n_words': 910}