All files / src/WebGL/shapes Cylinder.ts

64.7% Statements 11/17
100% Branches 3/3
50% Functions 2/4
64.7% Lines 11/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44    18x                     22x 22x 22x 22x                                   1x 1x 1x 1x 1x   1x      
import type { Matrix4 } from '../math';
import { Vector3 } from '../math';
let vector = new Vector3()
 
//Bounding cylinder for stick render
/** @constructor */
export class Cylinder {
  c1: Vector3
  c2: Vector3
  direction: Vector3
  radius: number
 
  constructor(c1:Vector3 = new Vector3(), c2:Vector3 = new Vector3(), radius:number = 0) {
    this.c1 = c1;
    this.c2 = c2;
    this.radius = radius;
    this.direction = new Vector3()
      .subVectors(this.c2, this.c1)
      .normalize();
  }
 
  copy(cylinder:Cylinder):Cylinder {
    this.c1.copy(cylinder.c1);
    this.c2.copy(cylinder.c2);
    this.direction.copy(cylinder.direction);
    this.radius = cylinder.radius;
    return this;
  }
 
  lengthSq():number { 
    return vector.subVectors(this.c2, this.c1).lengthSq();
  }
 
  applyMatrix4(matrix:Matrix4):Cylinder {
    this.direction.add(this.c1).applyMatrix4(matrix);
    this.c1.applyMatrix4(matrix);
    this.c2.applyMatrix4(matrix);
    this.direction.sub(this.c1).normalize();
    this.radius = this.radius * matrix.getMaxScaleOnAxis();
 
    return this;
  }
}