/
SurfaceMeshWithMetadataProvider3DChartFragment.java
209 lines (168 loc) · 9.76 KB
/
SurfaceMeshWithMetadataProvider3DChartFragment.java
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2019. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// SurfaceMeshWithMetadataProvider3DChartFragment.java is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
//******************************************************************************
package com.scichart.examples.fragments.charts3d;
import com.scichart.charting3d.model.dataSeries.grid.UniformGridDataSeries3D;
import com.scichart.charting3d.visuals.SciChartSurface3D;
import com.scichart.charting3d.visuals.axes.NumericAxis3D;
import com.scichart.charting3d.visuals.camera.Camera3D;
import com.scichart.charting3d.visuals.renderableSeries.data.DrawMeshAs;
import com.scichart.charting3d.visuals.renderableSeries.data.GradientColorPalette;
import com.scichart.charting3d.visuals.renderableSeries.metadataProviders.ISurfaceMeshMetadataProvider3D;
import com.scichart.charting3d.visuals.renderableSeries.metadataProviders.MetadataProvider3DBase;
import com.scichart.charting3d.visuals.renderableSeries.surfaceMesh.SurfaceMeshRenderPassData3D;
import com.scichart.charting3d.visuals.renderableSeries.surfaceMesh.SurfaceMeshRenderableSeries3D;
import com.scichart.core.framework.UpdateSuspender;
import com.scichart.core.model.IntegerValues;
import com.scichart.examples.R;
import com.scichart.examples.data.DataManager;
import com.scichart.examples.fragments.base.ExampleBaseFragment;
import java.util.Collections;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import static com.scichart.drawing.utility.ColorUtil.Blue;
import static com.scichart.drawing.utility.ColorUtil.CadetBlue;
import static com.scichart.drawing.utility.ColorUtil.Cyan;
import static com.scichart.drawing.utility.ColorUtil.DarkBlue;
import static com.scichart.drawing.utility.ColorUtil.DarkRed;
import static com.scichart.drawing.utility.ColorUtil.GreenYellow;
import static com.scichart.drawing.utility.ColorUtil.IndianRed;
import static com.scichart.drawing.utility.ColorUtil.LimeGreen;
import static com.scichart.drawing.utility.ColorUtil.Red;
import static com.scichart.drawing.utility.ColorUtil.Tomato;
import static com.scichart.drawing.utility.ColorUtil.Yellow;
public class SurfaceMeshWithMetadataProvider3DChartFragment extends ExampleBaseFragment {
private static final int X_SIZE = 49, Z_SIZE = 49;
@BindView(R.id.chart3d)
SciChartSurface3D surface3d;
private final UniformGridDataSeries3D<Double, Double, Double> meshDataSeries0 = new UniformGridDataSeries3D<>(Double.class, Double.class, Double.class, X_SIZE, Z_SIZE);
private final UniformGridDataSeries3D<Double, Double, Double> meshDataSeries1 = new UniformGridDataSeries3D<>(Double.class, Double.class, Double.class, X_SIZE, Z_SIZE);
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> schedule;
@Override
protected int getLayoutId() {
return R.layout.example_single_chart3d_fragment;
}
@Override
protected void initExample() {
final Camera3D camera = sciChart3DBuilder.newCamera3D().build();
final NumericAxis3D xAxis = sciChart3DBuilder.newNumericAxis3D().withDrawMajorBands(false).withDrawLabels(false).withDrawMajorGridLines(false).withDrawMajorTicks(false).withDrawMinorGridLines(false).withDrawMinorTicks(false).withPlaneBoderThickness(0f).build();
final NumericAxis3D yAxis = sciChart3DBuilder.newNumericAxis3D().withDrawMajorBands(false).withDrawLabels(false).withDrawMajorGridLines(false).withDrawMajorTicks(false).withDrawMinorGridLines(false).withDrawMinorTicks(false).withPlaneBoderThickness(0f).build();
final NumericAxis3D zAxis = sciChart3DBuilder.newNumericAxis3D().withDrawMajorBands(false).withDrawLabels(false).withDrawMajorGridLines(false).withDrawMajorTicks(false).withDrawMinorGridLines(false).withDrawMinorTicks(false).withPlaneBoderThickness(0f).build();
for (int x = 48; x >= 24; x--) {
final double y = Math.pow(x - 23.7, 0.3);
final double y2 = Math.pow(49.5 - x, 0.3);
meshDataSeries0.updateYAt(x, 24, y);
meshDataSeries1.updateYAt(x, 24, y2 + 1.505);
}
for (int x = 24; x >= 0; x--) {
for (int z = 49; z > 25; z--) {
final double y = Math.pow(z - 23.7, 0.3);
final double y2 = Math.pow(50.5 - z, 0.3) + 1.505;
meshDataSeries0.updateYAt(x + 24, 49 - z, y);
meshDataSeries0.updateYAt(z - 1, 24 - x, y);
meshDataSeries1.updateYAt(x + 24, 49 - z, y2);
meshDataSeries1.updateYAt(z - 1, 24 - x, y2);
meshDataSeries0.updateYAt(24 - x, 49 - z, y);
meshDataSeries0.updateYAt(49 - z, 24 - x, y);
meshDataSeries1.updateYAt(24 - x, 49 - z, y2);
meshDataSeries1.updateYAt(49 - z, 24 - x, y2);
meshDataSeries0.updateYAt(x + 24, z - 1, y);
meshDataSeries0.updateYAt(z - 1, x + 24, y);
meshDataSeries1.updateYAt(x + 24, z - 1, y2);
meshDataSeries1.updateYAt(z - 1, x + 24, y2);
meshDataSeries0.updateYAt(24 - x, z - 1, y);
meshDataSeries0.updateYAt(49 - z, x + 24, y);
meshDataSeries1.updateYAt(24 - x, z - 1, y2);
meshDataSeries1.updateYAt(49 - z, x + 24, y2);
}
}
final int[] colors = new int[]{DarkBlue, Blue, CadetBlue, Cyan, LimeGreen, GreenYellow, Yellow, Tomato, IndianRed, Red, DarkRed};
final float[] stops = new float[]{0f, .1f, .2f, .3f, .4f, .5f, .6f, .7f, .8f, .9f, 1f};
final SurfaceMeshRenderableSeries3D rs0 = sciChart3DBuilder.newSurfaceMeshSeries3D()
.withDataSeries(meshDataSeries0)
.withDrawMeshAs(DrawMeshAs.SolidMesh)
.withDrawSkirt(false)
.withMeshColorPalette(new GradientColorPalette(colors, stops))
.withMetadataProvider(new SurfaceMeshMetadataProvider3D())
.build();
final SurfaceMeshRenderableSeries3D rs1 = sciChart3DBuilder.newSurfaceMeshSeries3D()
.withDataSeries(meshDataSeries1)
.withDrawMeshAs(DrawMeshAs.SolidMesh)
.withDrawSkirt(false)
.withMeshColorPalette(new GradientColorPalette(colors, stops))
.withMetadataProvider(new SurfaceMeshMetadataProvider3D())
.build();
UpdateSuspender.using(surface3d, new Runnable() {
@Override
public void run() {
surface3d.setCamera(camera);
surface3d.setXAxis(xAxis);
surface3d.setYAxis(yAxis);
surface3d.setZAxis(zAxis);
Collections.addAll(surface3d.getRenderableSeries(), rs0, rs1);
surface3d.getChartModifiers().add(sciChart3DBuilder.newModifierGroupWithDefaultModifiers().build());
}
});
schedule = scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
UpdateSuspender.using(surface3d, new Runnable() {
@Override
public void run() {
rs0.invalidateMetadata();
rs1.invalidateMetadata();
}
});
}
}, 0, 10, TimeUnit.MILLISECONDS);
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (schedule != null)
schedule.cancel(true);
}
private static class SurfaceMeshMetadataProvider3D extends MetadataProvider3DBase<SurfaceMeshRenderableSeries3D> implements ISurfaceMeshMetadataProvider3D {
SurfaceMeshMetadataProvider3D() {
super(SurfaceMeshRenderableSeries3D.class);
}
@Override
public void updateMeshColors(IntegerValues cellColors) {
final SurfaceMeshRenderPassData3D currentRenderPassData = (SurfaceMeshRenderPassData3D) renderableSeries.getCurrentRenderPassData();
final DataManager dataManager = DataManager.getInstance();
final int countX = currentRenderPassData.countX - 1;
final int countZ = currentRenderPassData.countZ - 1;
cellColors.setSize(currentRenderPassData.getPointsCount());
final int[] items = cellColors.getItemsArray();
for (int x = 0; x < countX; x++) {
for (int z = 0; z < countZ; z++) {
final int index = x * countZ + z;
final int color;
if ((x >= 20 && x <= 26 && z > 0 && z < 47) || (z >= 20 && z <= 26 && x > 0 && x < 47)) {
// need to use special transparent color definition for MetadataProvider3D for it to work
color = TRANSPARENT;
} else {
color = dataManager.getRandomColor();
}
items[index] = color;
}
}
}
}
}