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

Commit 8af0721

Browse files
committed
factor out Object from SurfaceGen
1 parent c5de825 commit 8af0721

File tree

4 files changed

+283
-245
lines changed

4 files changed

+283
-245
lines changed

desktop/graph3d/com/support/constraintlayout/extlib/graph3d/Graph3dPanel.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ public class Graph3dPanel extends JPanel {
2424

2525
public Graph3dPanel() {
2626

27-
mSurfaceGen.calcSurface(-20, 20, -20, 20, true, (x, y) -> {
28-
double d = Math.sqrt(x * x + y * y);
29-
return 10 * ((d == 0) ? 1f : (float) (Math.sin(d) / d));
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+
}
3032
});
3133

3234
addComponentListener(new ComponentAdapter() {
@@ -65,7 +67,7 @@ public void onSizeChanged(ComponentEvent c) {
6567
}
6668
mImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
6769
mImageBuff = ((DataBufferInt) (mImage.getRaster().getDataBuffer())).getData();
68-
mSurfaceGen.setScreenDim(width, height, mImageBuff, 0x00FFFFFF);
70+
mSurfaceGen.setScreenDim(width, height, mImageBuff, 0x00FFEEFF);
6971
}
7072

7173
public void onMouseDown(MouseEvent ev) {
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
package com.support.constraintlayout.extlib.graph3d;
2+
3+
/**
4+
* This represents 3d Object in this system
5+
*/
6+
public class Object3D {
7+
protected float[] vert;
8+
protected int[] index;
9+
protected float[] tVert; // the vertices transformed into screen space
10+
float mMinX, mMaxX, mMinY, mMaxY, mMinZ, mMaxZ; // bounds in x,y & z
11+
12+
public void transform(Matrix m) {
13+
for (int i = 0; i < vert.length; i += 3) {
14+
m.mult3(vert, i, tVert, i);
15+
}
16+
}
17+
18+
public void render(SurfaceGen s, int type, float[] zbuff, int[] img, int width, int height) {
19+
System.out.println(type);
20+
switch (type) {
21+
case 0:
22+
raster_height(s, zbuff, img, width, height);
23+
break;
24+
case 1:
25+
raster_outline(s, zbuff, img, width, height);
26+
break;
27+
case 2:
28+
raster_color(s, zbuff, img, width, height);
29+
break;
30+
case 3:
31+
raster_lines(s, zbuff, img, width, height);
32+
break;
33+
}
34+
}
35+
36+
37+
private void raster_lines(SurfaceGen s, float[] zbuff, int[] img, int w, int h) {
38+
for (int i = 0; i < index.length; i += 3) {
39+
int p1 = index[i];
40+
int p2 = index[i + 1];
41+
int p3 = index[i + 2];
42+
43+
float height = (vert[p1 + 2] + vert[p3 + 2] + vert[p2 + 2]) / 3;
44+
int val = (int) (255 * Math.abs(height));
45+
SurfaceGen.triangle(zbuff, img, 0x10001 * val + 0x100 * (255 - val), w, h, tVert[p1], tVert[p1 + 1],
46+
tVert[p1 + 2], tVert[p2], tVert[p2 + 1],
47+
tVert[p2 + 2], tVert[p3], tVert[p3 + 1],
48+
tVert[p3 + 2]);
49+
50+
SurfaceGen.drawline(zbuff, img, s.lineColor, w, h,
51+
tVert[p1], tVert[p1 + 1], tVert[p1 + 2] - 0.01f,
52+
tVert[p2], tVert[p2 + 1], tVert[p2 + 2] - 0.01f);
53+
SurfaceGen.drawline(zbuff, img, s.lineColor, w, h,
54+
tVert[p1], tVert[p1 + 1], tVert[p1 + 2] - 0.01f,
55+
tVert[p3], tVert[p3 + 1], tVert[p3 + 2] - 0.01f);
56+
}
57+
}
58+
59+
void raster_height(SurfaceGen s, float[] zbuff, int[] img, int w, int h) {
60+
for (int i = 0; i < index.length; i += 3) {
61+
int p1 = index[i];
62+
int p2 = index[i + 1];
63+
int p3 = index[i + 2];
64+
float height = (vert[p1 + 2] + vert[p3 + 2] + vert[p2 + 2]) / 3;
65+
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],
68+
tVert[p1 + 2], tVert[p2], tVert[p2 + 1],
69+
tVert[p2 + 2], tVert[p3], tVert[p3 + 1],
70+
tVert[p3 + 2]);
71+
}
72+
}
73+
74+
75+
void raster_color(SurfaceGen s, float[] zbuff, int[] img, int w, int h) {
76+
for (int i = 0; i < index.length; i += 3) {
77+
int p1 = index[i];
78+
int p2 = index[i + 1];
79+
int p3 = index[i + 2];
80+
81+
VectorUtil.triangleNormal(tVert, p1, p2, p3, s.tmpVec);
82+
float defuse = VectorUtil.dot(s.tmpVec, s.light);
83+
float height = (vert[p1 + 2] + vert[p3 + 2] + vert[p2 + 2]) / 3;
84+
height = (height - mMinZ) / (mMaxZ - mMinZ);
85+
float bright = Math.max(0, defuse);
86+
float hue = (float) Math.sqrt(height);
87+
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],
90+
tVert[p1 + 2], tVert[p2], tVert[p2 + 1],
91+
tVert[p2 + 2], tVert[p3], tVert[p3 + 1],
92+
tVert[p3 + 2]);
93+
}
94+
}
95+
96+
void raster_outline(SurfaceGen s, float[] zBuff, int[] img, int w, int h) {
97+
for (int i = 0; i < index.length; i += 3) {
98+
int p1 = index[i];
99+
int p2 = index[i + 1];
100+
int p3 = index[i + 2];
101+
102+
SurfaceGen.triangle(zBuff, img, s.background, w, h,
103+
tVert[p1], tVert[p1 + 1], tVert[p1 + 2],
104+
tVert[p2], tVert[p2 + 1], tVert[p2 + 2],
105+
tVert[p3], tVert[p3 + 1], tVert[p3 + 2]);
106+
107+
SurfaceGen.drawline(zBuff, img, s.lineColor, w, h,
108+
tVert[p1], tVert[p1 + 1], tVert[p1 + 2],
109+
tVert[p2], tVert[p2 + 1], tVert[p2 + 2]);
110+
111+
SurfaceGen.drawline(zBuff, img, s.lineColor, w, h,
112+
tVert[p1], tVert[p1 + 1], tVert[p1 + 2],
113+
tVert[p3], tVert[p3 + 1], tVert[p3 + 2]);
114+
}
115+
}
116+
117+
public double[] center() {
118+
double[] look_point = {
119+
(mMinX + mMaxX) / 2, (mMinY + mMaxY) / 2, (mMinZ + mMaxZ) / 2
120+
};
121+
return look_point;
122+
}
123+
public float centerX() {
124+
return (mMaxX + mMinX) / 2;
125+
}
126+
public float centerY() {
127+
return (mMaxY + mMinY) / 2;
128+
}
129+
public float rangeX() {
130+
return (mMaxX - mMinX) / 2;
131+
}
132+
public float rangeY() {
133+
return (mMaxY - mMinY) / 2;
134+
}
135+
public double size() {
136+
137+
return Math.hypot((mMaxX - mMinX), Math.hypot((mMaxY - mMinY), (mMaxZ - mMinZ))) / 2;
138+
}
139+
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+
}
239+
}

0 commit comments

Comments
 (0)