aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tensorboard/components/vz_projector/scatterPlotVisualizerTraces.ts
blob: 57d4b0611b2253065eaabe71ef87b5e95d795a65 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Copyright 2016 The TensorFlow Authors. 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.
==============================================================================*/

import {DataSet} from './data';
import {RenderContext} from './renderContext';
import {ScatterPlotVisualizer} from './scatterPlotVisualizer';
import * as util from './util';

const RGB_NUM_ELEMENTS = 3;
const XYZ_NUM_ELEMENTS = 3;

/**
 * Renders 'traces' (polylines) that connect multiple points in the dataset
 */
export class ScatterPlotVisualizerTraces implements ScatterPlotVisualizer {
  private dataSet: DataSet;
  private scene: THREE.Scene;
  private traces: THREE.Line[];
  private tracePositionBuffer: {[trace: number]: THREE.BufferAttribute} = {};
  private traceColorBuffer: {[trace: number]: THREE.BufferAttribute} = {};

  private updateTraceIndicesInDataSet(ds: DataSet) {
    for (let i = 0; i < ds.traces.length; i++) {
      const trace = ds.traces[i];
      for (let j = 0; j < trace.pointIndices.length - 1; j++) {
        ds.points[trace.pointIndices[j]].traceIndex = i;
        ds.points[trace.pointIndices[j + 1]].traceIndex = i;
      }
    }
  }

  private createTraces(scene: THREE.Scene) {
    if (!this.dataSet || !this.dataSet.traces) {
      return;
    }

    this.updateTraceIndicesInDataSet(this.dataSet);
    this.traces = [];

    for (let i = 0; i < this.dataSet.traces.length; i++) {
      const geometry = new THREE.BufferGeometry();
      geometry.addAttribute('position', this.tracePositionBuffer[i]);
      geometry.addAttribute('color', this.traceColorBuffer[i]);

      const material = new THREE.LineBasicMaterial({
        linewidth: 1,  // unused default, overwritten by width array.
        opacity: 1.0,  // unused default, overwritten by opacity array.
        transparent: true,
        vertexColors: THREE.VertexColors
      });

      const trace = new THREE.LineSegments(geometry, material);
      trace.frustumCulled = false;
      this.traces.push(trace);
      scene.add(trace);
    }
  }

  dispose() {
    if (!this.traces) {
      return;
    }
    for (let i = 0; i < this.traces.length; i++) {
      this.scene.remove(this.traces[i]);
      this.traces[i].geometry.dispose();
    }
    this.traces = null;
    this.tracePositionBuffer = {};
    this.traceColorBuffer = {};
  }

  setScene(scene: THREE.Scene) {
    this.scene = scene;
  }

  onPointPositionsChanged(newPositions: Float32Array, dataSet: DataSet) {
    this.dataSet = dataSet;
    if (dataSet == null) {
      return;
    }

    if ((this.traces == null) ||
        (this.traces.length !== dataSet.traces.length)) {
      if (this.traces != null) {
        this.dispose();
      }
      // Set up the position buffer arrays for each trace.
      for (let i = 0; i < this.dataSet.traces.length; i++) {
        let dataTrace = this.dataSet.traces[i];
        const vertexCount = 2 * (dataTrace.pointIndices.length - 1);

        let traces = new Float32Array(vertexCount * XYZ_NUM_ELEMENTS);
        this.tracePositionBuffer[i] =
            new THREE.BufferAttribute(traces, XYZ_NUM_ELEMENTS);

        let colors = new Float32Array(vertexCount * RGB_NUM_ELEMENTS);
        this.traceColorBuffer[i] =
            new THREE.BufferAttribute(colors, RGB_NUM_ELEMENTS);
      }
    }

    for (let i = 0; i < this.dataSet.traces.length; i++) {
      const dataTrace = this.dataSet.traces[i];
      let src = 0;
      for (let j = 0; j < dataTrace.pointIndices.length - 1; j++) {
        const p1Index = dataTrace.pointIndices[j];
        const p2Index = dataTrace.pointIndices[j + 1];
        const p1 = util.vector3FromPackedArray(newPositions, p1Index);
        const p2 = util.vector3FromPackedArray(newPositions, p2Index);
        this.tracePositionBuffer[i].setXYZ(src, p1.x, p1.y, p1.z);
        this.tracePositionBuffer[i].setXYZ(src + 1, p2.x, p2.y, p2.z);
        src += 2;
      }
      this.tracePositionBuffer[i].needsUpdate = true;
    }

    if (this.traces == null) {
      this.createTraces(this.scene);
    }
  }

  onRender(renderContext: RenderContext) {
    if (this.traces == null) {
      return;
    }
    for (let i = 0; i < this.traces.length; i++) {
      this.traces[i].material.opacity = renderContext.traceOpacities[i];
      (this.traces[i].material as THREE.LineBasicMaterial).linewidth =
          renderContext.traceWidths[i];
      this.traceColorBuffer[i].array = renderContext.traceColors[i];
      this.traceColorBuffer[i].needsUpdate = true;
    }
  }

  onPickingRender(renderContext: RenderContext) {}
  onResize(newWidth: number, newHeight: number) {}
  onSetLabelAccessor(labelAccessor: (index: number) => string) {}
}