/
CalabiYau.jsx
167 lines (148 loc) · 5.26 KB
/
CalabiYau.jsx
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React from "react";
import withStyles from "@material-ui/core/styles/withStyles";
import Card from "components/Card/Card.jsx";
import GridContainer from "components/Grid/GridContainer.jsx";
import GridItem from "components/Grid/GridItem.jsx";
import CustomDropdown from "components/CustomDropdown/CustomDropdown.jsx";
import * as math from "mathjs";
import * as THREE from "three";
import * as d3 from "d3";
import Frame from "./common/Frame.jsx";
import { createStats} from "./common/threeUtil";
import World from "./common/world.js"
// Monkey patch to the world
World.prototype.addCalabiYau = function(n, a) {
function coordinate(x, y, n, k1, k2, a) {
const z1 = math.multiply(
math.exp(math.complex(0, 2*math.PI*k1/n)),
math.pow(math.cos(math.complex(x, y)), 2/n)
);
const z2 = math.multiply(
math.exp(math.complex(0, 2*math.PI*k2/n)),
math.pow(math.sin(math.complex(x, y)), 2/n)
);
return new THREE.Vector3(z1.re, z2.re, z1.im*math.cos(a) + z2.im*math.sin(a));
}
const dx = math.PI/10;
const dy = math.PI/10;
d3.cross(d3.range(n), d3.range(n)).forEach(k => {
d3.range(0, math.PI/2, dx).forEach(x => {
d3.range(-math.PI/2, math.PI/2, dy).forEach(y => {
const data = [
{"x": x, "y": y },
{"x": x+dx, "y": y },
{"x": x+dx, "y": y+dy},
{"x": x, "y": y+dy},
];
this.addNormalRect(
...data.map(d => coordinate(d.x, d.y, n, k[0], k[1], a))
// d3.interpolateSinebow( ( (k[0]+k[1]) / n) % 1)
);
});
});
})
};
class CalabiYau extends React.Component {
constructor(props) {
super(props);
this.state = {
exponent: 6,
projection: 0.4
};
}
componentDidMount() {
this.setState({
size: {
width: this.refs.webgl.offsetWidth,
height: window.innerHeight - 70 - 2*15,
}
}, this.drawCalabiYau);
}
drawCalabiYau() {
// Performance monitor
const stats = createStats("Stats-output");
// Create a new world!
const world = new World("calabiyau", this.state.size);
this.setState({
world: world
});
world.addCamera([7, 0, 0], [0, 0, 1], world.scene.position,
this.state.size.width/this.state.size.height);
world.addTrackball();
// Calabi-Yau Manifold
world.addCalabiYau(this.state.exponent, this.state.projection);
// Animation loop
const animate = function () {
stats.update();
world.updateControls();
// Animate
requestAnimationFrame(animate);
world.render();
};
animate();
}
updateCalabiYau() {
// Remove
this.state.world.removeAll();
// new Calabi-Yau Manifold
this.state.world.addCalabiYau(this.state.exponent, this.state.projection);
}
handleChange = name => value => {
this.setState({
[name]: value,
}, this.updateCalabiYau);
}
render() {
const { classes } = this.props;
return (
<Frame title="Calabi-Yau Manifold">
<GridContainer>
<GridItem sm={9} xs={12}>
{/* left */}
<Card className={classes.card}>
<div id="Stats-output" className={classes.stats}></div>
<div id="calabiyau" ref="webgl"></div>
</Card>
</GridItem>
<GridItem sm={3} xs={12} className={classes.rightContainer}>
{/* right */}
<CustomDropdown
buttonText={`Exponent: ${this.state.exponent}`}
buttonProps={{
color: "transparent"
}}
dropdownList={d3.range(2, 9).map(d => (
<div onClick={() => this.handleChange("exponent")(d)}>{d}</div>
))}
/>
<CustomDropdown
buttonText={`Projection: ${this.state.projection}`}
buttonProps={{
color: "transparent"
}}
dropdownList={d3.range(0, 2 * math.PI, 0.4).map(d => (
<div onClick={() => this.handleChange("projection")(Math.round(10 * d) / 10)}>{Math.round(10 * d) / 10}</div>
))}
/>
</GridItem>
</GridContainer>
</Frame>
);
}
}
const calabiYauStyle = {
card: {
width: `calc(100% - ${2*15}px)`,
height: `${window.innerHeight - 70 - 2*15}px`,
margin: "15px",
},
stats: {
position: "absolute",
right: 0,
bottom: 0,
},
rightContainer: {
padding: "15px",
},
};
export default withStyles(calabiYauStyle)(CalabiYau);