Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit ba844f4

Browse files
committed
refactor to be more logicly configured
1 parent 8af0721 commit ba844f4

File tree

5 files changed

+293
-186
lines changed

5 files changed

+293
-186
lines changed

Diff for: desktop/graph3d/com/support/constraintlayout/extlib/graph3d/Graph3dPanel.java

+44-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.support.constraintlayout.extlib.graph3d;
22

3+
import com.support.constraintlayout.extlib.graph3d.objects.AxisBox;
4+
import com.support.constraintlayout.extlib.graph3d.objects.Surface3D;
5+
36
import javax.swing.*;
47
import java.awt.*;
5-
import java.awt.event.ComponentAdapter;
6-
import java.awt.event.ComponentEvent;
7-
import java.awt.event.MouseAdapter;
8-
import java.awt.event.MouseEvent;
8+
import java.awt.event.*;
99
import java.awt.image.BufferedImage;
1010
import java.awt.image.DataBufferInt;
1111

1212

1313
public class Graph3dPanel extends JPanel {
1414

15-
SurfaceGen mSurfaceGen = new SurfaceGen();
15+
Scene3D mScene3D = new Scene3D();
1616
private BufferedImage mImage;
1717
private int[] mImageBuff;
1818
int mGraphType = 2;
@@ -21,15 +21,26 @@ public class Graph3dPanel extends JPanel {
2121
private float mLastTrackBallX;
2222
private float mLastTrackBallY;
2323
double mDownScreenWidth;
24+
Surface3D mSurface;
25+
AxisBox mAxisBox;
26+
float range = 20;
27+
public void buildSurface() {
28+
29+
mSurface = new Surface3D((x, y) -> {
30+
double d = Math.sqrt(x * x + y * y);
31+
return 10 * ((d == 0) ? 1f : (float) (Math.sin(d) / d));
32+
});
33+
mSurface.setRange(-range, range, -range, range);
34+
mScene3D.setObject(mSurface);
35+
mScene3D.resetCamera();
36+
mAxisBox = new AxisBox();
37+
mAxisBox.setRange(-range, range, -range, range,-2,20);
38+
mScene3D.addPostObject(mAxisBox);
39+
}
2440

2541
public Graph3dPanel() {
2642

27-
mSurfaceGen.calcSurface(-20, 20, -20, 20, true, new SurfaceGen.Function() {
28-
public float eval(float x, float y) {
29-
double d = Math.sqrt(x * x + y * y);
30-
return 10 * ((d == 0) ? 1f : (float) (Math.sin(d) / d));
31-
}
32-
});
43+
buildSurface();
3344

3445
addComponentListener(new ComponentAdapter() {
3546
@Override
@@ -53,7 +64,13 @@ public void mousePressed(MouseEvent e) {
5364
public void mouseDragged(MouseEvent e) {
5465
onMouseDrag(e);
5566
}
67+
68+
@Override
69+
public void mouseWheelMoved(MouseWheelEvent e) {
70+
onMouseWheel(e);
71+
}
5672
};
73+
addMouseWheelListener(mouseAdapter);
5774
addMouseListener(mouseAdapter);
5875
addMouseMotionListener(mouseAdapter);
5976
}
@@ -67,14 +84,14 @@ public void onSizeChanged(ComponentEvent c) {
6784
}
6885
mImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
6986
mImageBuff = ((DataBufferInt) (mImage.getRaster().getDataBuffer())).getData();
70-
mSurfaceGen.setScreenDim(width, height, mImageBuff, 0x00FFEEFF);
87+
mScene3D.setScreenDim(width, height, mImageBuff, 0x00FFEEFF);
7188
}
7289

7390
public void onMouseDown(MouseEvent ev) {
74-
mDownScreenWidth = mSurfaceGen.getScreenWidth();
91+
mDownScreenWidth = mScene3D.getScreenWidth();
7592
mLastTouchX0 = ev.getX();
7693
mLastTouchY0 = ev.getY();
77-
mSurfaceGen.trackBallDown(mLastTouchX0, mLastTouchY0);
94+
mScene3D.trackBallDown(mLastTouchX0, mLastTouchY0);
7895
mLastTrackBallX = mLastTouchX0;
7996
mLastTrackBallY = mLastTouchY0;
8097
}
@@ -88,7 +105,7 @@ public void onMouseDrag(MouseEvent ev) {
88105
float moveX = (mLastTrackBallX - tx);
89106
float moveY = (mLastTrackBallY - ty);
90107
if (moveX * moveX + moveY * moveY < 4000f) {
91-
mSurfaceGen.trackBallMove(tx, ty);
108+
mScene3D.trackBallMove(tx, ty);
92109
}
93110
mLastTrackBallX = tx;
94111
mLastTrackBallY = ty;
@@ -100,15 +117,24 @@ public void onMouseUP(MouseEvent ev) {
100117
mLastTouchY0 = Float.NaN;
101118
}
102119

120+
public void onMouseWheel(MouseEvent ev) {
121+
MouseWheelEvent we = (MouseWheelEvent)ev;
122+
range = range * (float) Math.pow(1.01,we.getWheelRotation());
123+
System.out.println(range);
124+
mSurface.setRange(-range, range, -range, range);
125+
mAxisBox.setRange(-range, range, -range, range,-2,20);
126+
mScene3D.update();
127+
repaint();
128+
}
103129
public void paintComponent(Graphics g) {
104130
int w = getWidth();
105131
int h = getHeight();
106-
if (mSurfaceGen.notSetUp()) {
132+
if (mScene3D.notSetUp()) {
107133
System.out.println("setup");
108-
mSurfaceGen.setUpMatrix(w, h);
134+
mScene3D.setUpMatrix(w, h);
109135
}
110136

111-
mSurfaceGen.render(mGraphType);
137+
mScene3D.render(mGraphType);
112138
if (mImage == null) {
113139
return;
114140
}

Diff for: desktop/graph3d/com/support/constraintlayout/extlib/graph3d/Object3D.java

+35-120
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ public class Object3D {
77
protected float[] vert;
88
protected int[] index;
99
protected float[] tVert; // the vertices transformed into screen space
10-
float mMinX, mMaxX, mMinY, mMaxY, mMinZ, mMaxZ; // bounds in x,y & z
10+
protected float mMinX, mMaxX, mMinY, mMaxY, mMinZ, mMaxZ; // bounds in x,y & z
11+
protected int mType = 2;
12+
13+
public int getType() {
14+
return mType;
15+
}
16+
17+
public void setType(int type) {
18+
this.mType = type;
19+
}
20+
1121

1222
public void transform(Matrix m) {
1323
for (int i = 0; i < vert.length; i += 3) {
1424
m.mult3(vert, i, tVert, i);
1525
}
1626
}
1727

18-
public void render(SurfaceGen s, int type, float[] zbuff, int[] img, int width, int height) {
19-
System.out.println(type);
20-
switch (type) {
28+
public void render(Scene3D s, float[] zbuff, int[] img, int width, int height) {
29+
switch (mType) {
2130
case 0:
2231
raster_height(s, zbuff, img, width, height);
2332
break;
@@ -34,45 +43,45 @@ public void render(SurfaceGen s, int type, float[] zbuff, int[] img, int width,
3443
}
3544

3645

37-
private void raster_lines(SurfaceGen s, float[] zbuff, int[] img, int w, int h) {
46+
private void raster_lines(Scene3D s, float[] zbuff, int[] img, int w, int h) {
3847
for (int i = 0; i < index.length; i += 3) {
3948
int p1 = index[i];
4049
int p2 = index[i + 1];
4150
int p3 = index[i + 2];
4251

4352
float height = (vert[p1 + 2] + vert[p3 + 2] + vert[p2 + 2]) / 3;
4453
int val = (int) (255 * Math.abs(height));
45-
SurfaceGen.triangle(zbuff, img, 0x10001 * val + 0x100 * (255 - val), w, h, tVert[p1], tVert[p1 + 1],
54+
Scene3D.triangle(zbuff, img, 0x10001 * val + 0x100 * (255 - val), w, h, tVert[p1], tVert[p1 + 1],
4655
tVert[p1 + 2], tVert[p2], tVert[p2 + 1],
4756
tVert[p2 + 2], tVert[p3], tVert[p3 + 1],
4857
tVert[p3 + 2]);
4958

50-
SurfaceGen.drawline(zbuff, img, s.lineColor, w, h,
59+
Scene3D.drawline(zbuff, img, s.lineColor, w, h,
5160
tVert[p1], tVert[p1 + 1], tVert[p1 + 2] - 0.01f,
5261
tVert[p2], tVert[p2 + 1], tVert[p2 + 2] - 0.01f);
53-
SurfaceGen.drawline(zbuff, img, s.lineColor, w, h,
62+
Scene3D.drawline(zbuff, img, s.lineColor, w, h,
5463
tVert[p1], tVert[p1 + 1], tVert[p1 + 2] - 0.01f,
5564
tVert[p3], tVert[p3 + 1], tVert[p3 + 2] - 0.01f);
5665
}
5766
}
5867

59-
void raster_height(SurfaceGen s, float[] zbuff, int[] img, int w, int h) {
68+
void raster_height(Scene3D s, float[] zbuff, int[] img, int w, int h) {
6069
for (int i = 0; i < index.length; i += 3) {
6170
int p1 = index[i];
6271
int p2 = index[i + 1];
6372
int p3 = index[i + 2];
6473
float height = (vert[p1 + 2] + vert[p3 + 2] + vert[p2 + 2]) / 3;
6574
height = (height - mMinZ) / (mMaxZ - mMinZ);
66-
int col = SurfaceGen.hsvToRgb(height, Math.abs(2 * (height - 0.5f)), (float) Math.sqrt(height));
67-
SurfaceGen.triangle(zbuff, img, col, w, h, tVert[p1], tVert[p1 + 1],
75+
int col = Scene3D.hsvToRgb(height, Math.abs(2 * (height - 0.5f)), (float) Math.sqrt(height));
76+
Scene3D.triangle(zbuff, img, col, w, h, tVert[p1], tVert[p1 + 1],
6877
tVert[p1 + 2], tVert[p2], tVert[p2 + 1],
6978
tVert[p2 + 2], tVert[p3], tVert[p3 + 1],
7079
tVert[p3 + 2]);
7180
}
7281
}
7382

7483

75-
void raster_color(SurfaceGen s, float[] zbuff, int[] img, int w, int h) {
84+
void raster_color(Scene3D s, float[] zbuff, int[] img, int w, int h) {
7685
for (int i = 0; i < index.length; i += 3) {
7786
int p1 = index[i];
7887
int p2 = index[i + 1];
@@ -85,30 +94,30 @@ void raster_color(SurfaceGen s, float[] zbuff, int[] img, int w, int h) {
8594
float bright = Math.max(0, defuse);
8695
float hue = (float) Math.sqrt(height);
8796
float sat = Math.max(0.5f, height);
88-
int col = SurfaceGen.hsvToRgb(hue, sat, bright);
89-
SurfaceGen.triangle(zbuff, img, col, w, h, tVert[p1], tVert[p1 + 1],
97+
int col = Scene3D.hsvToRgb(hue, sat, bright);
98+
Scene3D.triangle(zbuff, img, col, w, h, tVert[p1], tVert[p1 + 1],
9099
tVert[p1 + 2], tVert[p2], tVert[p2 + 1],
91100
tVert[p2 + 2], tVert[p3], tVert[p3 + 1],
92101
tVert[p3 + 2]);
93102
}
94103
}
95104

96-
void raster_outline(SurfaceGen s, float[] zBuff, int[] img, int w, int h) {
105+
void raster_outline(Scene3D s, float[] zBuff, int[] img, int w, int h) {
97106
for (int i = 0; i < index.length; i += 3) {
98107
int p1 = index[i];
99108
int p2 = index[i + 1];
100109
int p3 = index[i + 2];
101110

102-
SurfaceGen.triangle(zBuff, img, s.background, w, h,
111+
Scene3D.triangle(zBuff, img, s.background, w, h,
103112
tVert[p1], tVert[p1 + 1], tVert[p1 + 2],
104113
tVert[p2], tVert[p2 + 1], tVert[p2 + 2],
105114
tVert[p3], tVert[p3 + 1], tVert[p3 + 2]);
106115

107-
SurfaceGen.drawline(zBuff, img, s.lineColor, w, h,
116+
Scene3D.drawline(zBuff, img, s.lineColor, w, h,
108117
tVert[p1], tVert[p1 + 1], tVert[p1 + 2],
109118
tVert[p2], tVert[p2 + 1], tVert[p2 + 2]);
110119

111-
SurfaceGen.drawline(zBuff, img, s.lineColor, w, h,
120+
Scene3D.drawline(zBuff, img, s.lineColor, w, h,
112121
tVert[p1], tVert[p1 + 1], tVert[p1 + 2],
113122
tVert[p3], tVert[p3 + 1], tVert[p3 + 2]);
114123
}
@@ -120,120 +129,26 @@ public double[] center() {
120129
};
121130
return look_point;
122131
}
132+
123133
public float centerX() {
124134
return (mMaxX + mMinX) / 2;
125135
}
136+
126137
public float centerY() {
127-
return (mMaxY + mMinY) / 2;
138+
return (mMaxY + mMinY) / 2;
128139
}
140+
129141
public float rangeX() {
130-
return (mMaxX - mMinX) / 2;
142+
return (mMaxX - mMinX) / 2;
131143
}
144+
132145
public float rangeY() {
133-
return (mMaxY - mMinY) / 2;
146+
return (mMaxY - mMinY) / 2;
134147
}
148+
135149
public double size() {
136150

137151
return Math.hypot((mMaxX - mMinX), Math.hypot((mMaxY - mMinY), (mMaxZ - mMinZ))) / 2;
138152
}
139153

140-
static class Surface extends Object3D {
141-
final int SIZE = 100; // the number of point on the side total points = SIZE*SIZE
142-
private Function mFunction;
143-
private float mZoomZ = 1;
144-
145-
public interface Function {
146-
float eval(float x, float y);
147-
}
148-
149-
public Surface(boolean resetZ, Function func) {
150-
computeSurface(resetZ, func);
151-
}
152-
153-
public void computeSurface(boolean resetZ, Function func) {
154-
int n = (SIZE + 1) * (SIZE + 1);
155-
vert = new float[n * 3];
156-
tVert = new float[n * 3];
157-
index = new int[SIZE * SIZE * 6];
158-
float min_x = mMinX;
159-
float max_x = mMaxX;
160-
float min_y = mMinY;
161-
float max_y = mMaxY;
162-
float min_z = Float.MAX_VALUE;
163-
float max_z = -Float.MAX_VALUE;
164-
165-
mFunction = func;
166-
int count = 0;
167-
for (int iy = 0; iy <= SIZE; iy++) {
168-
float y = min_y + iy * (max_y - min_y) / (SIZE);
169-
for (int ix = 0; ix <= SIZE; ix++) {
170-
float x = min_x + ix * (max_x - min_x) / (SIZE);
171-
vert[count++] = x;
172-
vert[count++] = y;
173-
float z = func.eval(x, y);
174-
175-
if (Float.isNaN(z) || Float.isInfinite(z)) {
176-
float epslonX = 0.000005232f;
177-
float epslonY = 0.00000898f;
178-
z = func.eval(x + epslonX, y + epslonY);
179-
}
180-
vert[count++] = z;
181-
if (Float.isNaN(z)) {
182-
continue;
183-
}
184-
185-
if (Float.isInfinite(z)) {
186-
continue;
187-
}
188-
min_z = Math.min(z, min_z);
189-
max_z = Math.max(z, max_z);
190-
}
191-
if (resetZ) {
192-
mMinZ = min_z;
193-
mMaxZ = max_z;
194-
}
195-
}
196-
// normalize range in z
197-
float xrange = mMaxX - mMinX;
198-
float yrange = mMaxY - mMinY;
199-
float zrange = max_z - min_z;
200-
if (zrange != 0) {
201-
float xyrange = (xrange + yrange) / 2;
202-
float scalez = xyrange / zrange;
203-
204-
for (int i = 0; i < vert.length; i += 3) {
205-
float z = vert[i + 2];
206-
if (Float.isNaN(z) || Float.isInfinite(z)) {
207-
if (i > 3) {
208-
z = vert[i - 1];
209-
} else {
210-
z = vert[i + 5];
211-
}
212-
}
213-
vert[i + 2] = z * scalez * mZoomZ;
214-
}
215-
if (resetZ) {
216-
mMinZ *= scalez;
217-
mMaxZ *= scalez;
218-
}
219-
}
220-
count = 0;
221-
for (int iy = 0; iy < SIZE; iy++) {
222-
for (int ix = 0; ix < SIZE; ix++) {
223-
int p1 = 3 * (ix + iy * (SIZE + 1));
224-
int p2 = 3 * (1 + ix + iy * (SIZE + 1));
225-
int p3 = 3 * (ix + (iy + 1) * (SIZE + 1));
226-
int p4 = 3 * (1 + ix + (iy + 1) * (SIZE + 1));
227-
index[count++] = p1;
228-
index[count++] = p2;
229-
index[count++] = p3;
230-
231-
index[count++] = p4;
232-
index[count++] = p3;
233-
index[count++] = p2;
234-
}
235-
}
236-
}
237-
238-
}
239154
}

0 commit comments

Comments
 (0)