source: js/human_3d_alignment/src/widgets/similviewer.jsx @ 881

Last change on this file since 881 was 881, checked in by Maciej Komosinski, 5 years ago

Initial, prototype version of a javascript app to test how humans align two 3D structures

File size: 6.6 KB
Line 
1/*global Module*/
2"use strict";
3import React from 'react';
4import ReactDOM from 'react-dom';
5import Sviewer from '../visualization/sviewer';
6import { Md3dRotation, MdYoutubeSearchedFor } from 'react-icons/lib/md';
7import GenotypeParser from '../utils/genotypeparser';
8import _ from 'lodash';
9import * as THREE from 'three';
10
11/**
12 * Component for assesing genotypes matching.
13 */
14class GenoViewer extends React.Component {
15    /**
16     * Basic constructor.
17     * @param {any} props properties of Component
18     */
19    constructor(props) {
20        super(props);
21        this.props = props;
22        this.state = {
23            autorotate: false,
24            rezoom: true,
25            valid: true,
26            clicked: false
27        };
28        this.start = this.start.bind(this);
29        this.stop = this.stop.bind(this);
30        this.animate = this.animate.bind(this);
31    }
32
33    /**
34     * Method initializes all visualizations and binds their methods and events with SViewer.
35     */
36    componentDidMount() {
37        let temp = ReactDOM.findDOMNode(this.mount);
38        let temp2 = ReactDOM.findDOMNode(this.mount).parentNode;
39        this.viewer = new Sviewer(temp2.clientWidth, temp2.clientHeight, temp);
40        if (this.model) {
41            //Module.destroy(this.model);
42        }
43        this.model = GenotypeParser.getModelFromGenotype("X(X,X)");
44        this.model2 = GenotypeParser.getModelFromGenotype("XX");
45        this.viewer.loadMeshesFromModel(this.model, this.model2);
46        this.viewer.setAutoRotate(this.state.autorotate);
47        this.viewer.setRezoom(this.state.rezoom);
48        //this.elementspicker = new ElementsPicker(this.viewer.scene, this.viewer.camera.getPerspectiveCamera());
49        this.renderScene = this.viewer.renderScene.bind(this.viewer);
50        this.lastw = temp2.clientWidth;
51        this.lasth = temp2.clientHeight;
52        this.start();
53    }
54
55    /**
56     * Method stops animation of Framsticks.
57     */
58    componentWillUnmount() {
59        this.stop();
60    }
61
62    /**
63     * After update of state all visualizations are refreshed
64     */
65    componentDidUpdate() {
66        let temp = ReactDOM.findDOMNode(this.mount).parentNode;
67        this.viewer.resizeView(temp.clientWidth, temp.clientHeight);
68    }
69
70    /**
71     * Method starts animation process. Internal method, used by componentDidMount.
72     */
73    start() {
74        if (!this.frameId) {
75            this.frameId = requestAnimationFrame(this.animate);
76        }
77    }
78
79    /**
80     * Main animation loop. If any change was applied to genotype, this method loads new
81     * models and brains.
82     */
83    animate() {
84        let temp = ReactDOM.findDOMNode(this.mount).parentNode;
85        if (this.lasth != temp.clientHeight || this.lastw != temp.clientWidth) {
86            this.viewer.resizeView(temp.clientWidth, temp.clientHeight);
87            this.lastw = temp.clientWidth;
88            this.lasth = temp.clientHeight;
89        }
90       
91        this.viewer.camera.getCameraControl().update();
92        this.frameId = window.requestAnimationFrame(this.animate);
93        this.viewer.scene.render(
94            this.viewer.renderer,
95            this.viewer.camera.getPerspectiveCamera());
96    }
97
98    /**
99     * Method cancels animation.
100     */
101    stop() {
102        cancelAnimationFrame(this.frameId);
103    }
104
105    /**
106     * Turns on/off rezoom property of viewer
107     */
108    switchRezoom() {
109        this.setState({rezoom: !this.state.rezoom});
110        this.viewer.setRezoom(!this.state.rezoom);
111    }
112
113    /**
114     * Turns on/off autorotate property of viewer
115     */
116    switchAutorotate() {
117        this.setState({autorotate: !this.state.autorotate});
118        this.viewer.setAutoRotate(!this.state.autorotate);
119    }
120
121    /**
122     * Gets position of mouse
123     */
124    getMousePosition(evt){
125        let bound = this.mount.getBoundingClientRect();
126        let mousePosition = {x: 0, y: 0};
127        mousePosition.x = ((evt.clientX - bound.left) / bound.width) * 2 - 1;
128        mousePosition.y = -((evt.clientY - bound.top) / bound.height) * 2 + 1;
129        //let vector = new THREE.Vector3(mousePosition.x, mousePosition.y, 0.5);
130        let vector = new THREE.Vector2(mousePosition.x, mousePosition.y);
131        return vector;
132    }
133
134       /**
135     * Event function for using mouse within viewer.
136     * @param {any} evt object holding event data
137     */
138    handleMouseDown(evt) {
139        evt.preventDefault();
140        let mousePosition = this.getMousePosition(evt);
141        this.viewer.handleMouseDown(mousePosition);
142    }
143
144           /**
145     * Event function for using mouse within viewer.
146     * @param {any} evt object holding event data
147     */
148    handleMouseUp(evt) {
149        evt.preventDefault();
150        let mousePosition = this.getMousePosition(evt);
151        this.viewer.handleMouseUp(mousePosition);
152    }
153
154               /**
155     * Event function for using mouse within viewer.
156     * @param {any} evt object holding event data
157     */
158    handleMouseMove(evt) {
159        evt.preventDefault();
160        let mousePosition = this.getMousePosition(evt);
161        this.viewer.handleMouseMove(mousePosition);
162    }
163
164   
165     
166    render() {
167        return (
168            <table {...this.props} style={{height: '100%', width: '100%'}}>
169                <tbody>
170                    <tr onMouseDown={ev => {if (ev) ev.stopPropagation();}}>
171                        <td rowSpan={2} style={{position:'relative', verticalAlign: 'top', alignContent: 'left'}}>
172                            <div style={{height: '100%', width: '100%', position: 'absolute', transform: 'translateZ(0)'}} onMouseDown={ev => {if (ev) ev.stopPropagation();}} onTouchStart={ev => {if (ev) ev.stopPropagation();}}>
173                                <canvas ref={(mount) => {this.mount = mount;}} onMouseDown={(evt) => this.handleMouseDown(evt)} onMouseUp={(evt) => this.handleMouseUp(evt)} onMouseMove={(evt) => this.handleMouseMove(evt)} style={{position: 'absolute', zIndex: 1}}></canvas>
174                                <Md3dRotation onClick={() => this.switchAutorotate()}  style={{zIndex: 2, position: 'absolute', left: '5%', bottom: 0, width: '20%', height: '20%', maxWidth: '40px', maxHeight: '40px', minWidth: '15px', minHeight: '15px', color: this.state.autorotate ? 'green' : 'black'}} />
175                                <MdYoutubeSearchedFor onClick={() => this.switchRezoom()} style={{zIndex: 2, position: 'absolute', right: '5%', bottom: 0, width: '20%', height: '20%', maxWidth: '40px', maxHeight: '40px', minWidth: '15px', minHeight: '15px', color: this.state.rezoom ? 'green' : 'black' }} />
176                            </div>
177                        </td>
178                    </tr> 
179                </tbody>
180            </table>
181        );
182    }
183}
184
185export default GenoViewer;
Note: See TracBrowser for help on using the repository browser.