|
| 1 | +package android.support.constraintLayout.extlib.graph3d; |
| 2 | + |
| 3 | + |
| 4 | +import android.support.constraintLayout.extlib.graph3d.objects.AxisBox; |
| 5 | +import android.support.constraintLayout.extlib.graph3d.objects.Surface3D; |
| 6 | + |
| 7 | +public class Graph { |
| 8 | + Scene3D mScene3D = new Scene3D(); |
| 9 | + |
| 10 | + int mGraphType = 2; |
| 11 | + private float mLastTouchX0 = Float.NaN; |
| 12 | + private float mLastTouchY0; |
| 13 | + private float mLastTrackBallX; |
| 14 | + private float mLastTrackBallY; |
| 15 | + double mDownScreenWidth; |
| 16 | + Surface3D mSurface; |
| 17 | + AxisBox mAxisBox; |
| 18 | + float range = 20; |
| 19 | + float minZ = -10; |
| 20 | + float maxZ = 10; |
| 21 | + float mZoomFactor = 1; |
| 22 | + long nanoTime; |
| 23 | + float time = 0; |
| 24 | + int graphWidth; |
| 25 | + int graphHeight; |
| 26 | + ImageSupport image; |
| 27 | + |
| 28 | + public interface ImageSupport { |
| 29 | + void makeImage(int w, int h); |
| 30 | + int[] getBacking(); |
| 31 | + } |
| 32 | + |
| 33 | + public Graph(ImageSupport image) { |
| 34 | + this.image = image; |
| 35 | + mAxisBox = new AxisBox(); |
| 36 | + mAxisBox.setRange(-range, range, -range, range, minZ, maxZ); |
| 37 | + mScene3D.addPostObject(mAxisBox); |
| 38 | + buildSurface(DEFAULT); |
| 39 | + resetCamera(); |
| 40 | + } |
| 41 | + |
| 42 | + public static Surface3D DEFAULT = new Surface3D((x, y, t) -> { |
| 43 | + double d = Math.sqrt(x * x + y * y); |
| 44 | + return 0.3f * (float) (Math.cos(d) * (y * y - x * x) / (1 + d)); |
| 45 | + }); |
| 46 | + |
| 47 | + public void buildSurface(Surface3D surface3D) { |
| 48 | + mSurface = surface3D; |
| 49 | + mSurface.setRange(-range, range, -range, range, minZ, maxZ); |
| 50 | + mScene3D.setObject(mSurface); |
| 51 | + } |
| 52 | + |
| 53 | + public void resetCamera() { |
| 54 | + mScene3D.resetCamera(); |
| 55 | + } |
| 56 | + |
| 57 | + public void setStartTime() { |
| 58 | + nanoTime = System.nanoTime(); |
| 59 | + } |
| 60 | + |
| 61 | + public void tick(long now) { |
| 62 | + time += (now - nanoTime) * 1E-9f; |
| 63 | + nanoTime = now; |
| 64 | + mSurface.calcSurface(time, false); |
| 65 | + mScene3D.update(); |
| 66 | + } |
| 67 | + |
| 68 | + public void resize(int width, int height) { |
| 69 | + graphHeight = height; |
| 70 | + graphWidth = width; |
| 71 | + image.makeImage(width, height); |
| 72 | + mScene3D.setScreenDim(width, height, image.getBacking(), 0x00AAAAAA); |
| 73 | + } |
| 74 | + |
| 75 | + public void trackDown(float x, float y) { |
| 76 | + mDownScreenWidth = mScene3D.getScreenWidth(); |
| 77 | + mLastTouchX0 = x; |
| 78 | + mLastTouchY0 = y; |
| 79 | + mScene3D.trackBallDown(mLastTouchX0, mLastTouchY0); |
| 80 | + mLastTrackBallX = mLastTouchX0; |
| 81 | + mLastTrackBallY = mLastTouchY0; |
| 82 | + } |
| 83 | + |
| 84 | + public void trackDrag(float x, float y) { |
| 85 | + if (Float.isNaN(mLastTouchX0)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + float tx = x; |
| 89 | + float ty = y; |
| 90 | + float moveX = (mLastTrackBallX - tx); |
| 91 | + float moveY = (mLastTrackBallY - ty); |
| 92 | + if (moveX * moveX + moveY * moveY < 4000f) { |
| 93 | + mScene3D.trackBallMove(tx, ty); |
| 94 | + } |
| 95 | + mLastTrackBallX = tx; |
| 96 | + mLastTrackBallY = ty; |
| 97 | + } |
| 98 | + |
| 99 | + public void trackDone() { |
| 100 | + mLastTouchX0 = Float.NaN; |
| 101 | + mLastTouchY0 = Float.NaN; |
| 102 | + } |
| 103 | + |
| 104 | + public void wheel(float rotation, boolean control) { |
| 105 | + if (control) { |
| 106 | + mZoomFactor *= (float) Math.pow(1.01, rotation); |
| 107 | + mScene3D.setZoom(mZoomFactor); |
| 108 | + mScene3D.setUpMatrix(graphWidth, graphHeight); |
| 109 | + mScene3D.update(); |
| 110 | + } else { |
| 111 | + range = range * (float) Math.pow(1.01, rotation); |
| 112 | + mSurface.setArraySize(Math.min(300, (int) (range * 5))); |
| 113 | + mSurface.setRange(-range, range, -range, range, minZ, maxZ); |
| 114 | + mAxisBox.setRange(-range, range, -range, range, minZ, maxZ); |
| 115 | + mScene3D.update(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public static final Surface3D BLACK_HOLE_MERGE = new Surface3D((x, y, t) -> { |
| 120 | + float d = (float) Math.sqrt(x * x + y * y); |
| 121 | + float d2 = (float) Math.pow(x * x + y * y, 0.125); |
| 122 | + float angle = (float) Math.atan2(y, x); |
| 123 | + float s = (float) Math.sin(d + angle - t * 5); |
| 124 | + float s2 = (float) Math.sin(t); |
| 125 | + float c = (float) Math.cos(d + angle - t * 5); |
| 126 | + return (s2 * s2 + 0.1f) * d2 * 5 * (s + c) / (1 + d * d / 20); |
| 127 | + // return (float) (s*s+0.1) * (float) (Math.cos(d-time*5) *(y*y-x*x) /(1+d*d)); |
| 128 | + }); |
| 129 | + |
| 130 | + public void render() { |
| 131 | + if (mScene3D.notSetUp()) { |
| 132 | + mScene3D.setUpMatrix(graphWidth, graphHeight); |
| 133 | + } |
| 134 | + |
| 135 | + mScene3D.render(mGraphType); |
| 136 | + } |
| 137 | + |
| 138 | + public void lightMovesWithCamera(boolean doesMove) { |
| 139 | + mScene3D.mLightMovesWithCamera = doesMove; |
| 140 | + } |
| 141 | + |
| 142 | + public void setSaturation(float sat) { |
| 143 | + mSurface.mSaturation = sat; |
| 144 | + mScene3D.update(); |
| 145 | + } |
| 146 | +} |
0 commit comments