Skip to content

Commit e448bc5

Browse files
committed
more work on matrix functions, implement matrices for PShape, addl note in
PGraphics
1 parent 42a6bbf commit e448bc5

File tree

5 files changed

+105
-16
lines changed

5 files changed

+105
-16
lines changed

core/src/processing/core/PGraphics.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@
102102
* refer to the <a href="http://processing.org/reference">reference</A>
103103
* on Processing.org for proper explanations. <b>No attempt has been made to
104104
* keep the javadoc up to date or complete.</b> It's an enormous task for
105-
* which we simply do not have the time.
105+
* which we simply do not have the time. That is, it's not something that
106+
* to be done once&mdash;it's a matter of keeping the multiple references
107+
* synchronized (to say nothing of the translation issues), while targeting
108+
* them for their separate audiences. Ouch.
106109
*/
107110
public class PGraphics extends PImage implements PConstants {
108111

@@ -3434,6 +3437,15 @@ public void resetMatrix() {
34343437
}
34353438

34363439

3440+
public void applyMatrix(PMatrix source) {
3441+
if (source instanceof PMatrix2D) {
3442+
applyMatrix((PMatrix2D) source);
3443+
} else if (source instanceof PMatrix3D) {
3444+
applyMatrix((PMatrix3D) source);
3445+
}
3446+
}
3447+
3448+
34373449
public void applyMatrix(PMatrix2D source) {
34383450
applyMatrix(source.m00, source.m01, source.m02,
34393451
source.m10, source.m11, source.m12);

core/src/processing/core/PMatrix.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public void set(float m00, float m01, float m02, float m03,
7676
/**
7777
* Multiply this matrix by another.
7878
*/
79+
public void apply(PMatrix source);
80+
7981
public void apply(PMatrix2D source);
8082

8183
public void apply(PMatrix3D source);

core/src/processing/core/PMatrix2D.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public PMatrix2D(float m00, float m01, float m02,
4444
m10, m11, m12);
4545
}
4646

47+
48+
public PMatrix2D(PMatrix matrix) {
49+
set(matrix);
50+
}
51+
4752

4853
public void reset() {
4954
set(1, 0, 0,
@@ -185,9 +190,15 @@ public void scale(float x, float y, float z) {
185190
}
186191

187192

188-
/**
189-
* Multiply this matrix by another.
190-
*/
193+
public void apply(PMatrix source) {
194+
if (source instanceof PMatrix2D) {
195+
apply((PMatrix2D) source);
196+
} else if (source instanceof PMatrix3D) {
197+
apply((PMatrix3D) source);
198+
}
199+
}
200+
201+
191202
public void apply(PMatrix2D source) {
192203
apply(source.m00, source.m01, source.m02,
193204
source.m10, source.m11, source.m12);

core/src/processing/core/PMatrix3D.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public PMatrix3D(float m00, float m01, float m02, float m03,
6464
}
6565

6666

67+
public PMatrix3D(PMatrix matrix) {
68+
set(matrix);
69+
}
70+
71+
6772
public void reset() {
6873
set(1, 0, 0, 0,
6974
0, 1, 0, 0,
@@ -258,6 +263,15 @@ public void scale(float x, float y, float z) {
258263
}
259264

260265

266+
public void apply(PMatrix source) {
267+
if (source instanceof PMatrix2D) {
268+
apply((PMatrix2D) source);
269+
} else if (source instanceof PMatrix3D) {
270+
apply((PMatrix3D) source);
271+
}
272+
}
273+
274+
261275
public void apply(PMatrix2D source) {
262276
apply(source.m00, source.m01, 0, source.m02,
263277
source.m10, source.m11, 0, source.m12,

core/src/processing/core/PShape.java

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract public class PShape implements PConstants {
3838

3939
protected int kind;
4040
//protected int drawMode;
41-
protected PMatrix3D matrix;
41+
protected PMatrix matrix;
4242

4343
// setAxis -> .x and .y to move x and y coords of origin
4444
protected float x;
@@ -199,6 +199,7 @@ public float getHeight() {
199199

200200
protected void pre(PGraphics g) {
201201
if (matrix != null) {
202+
/*
202203
boolean flat = g instanceof PGraphics2D;
203204
204205
g.pushMatrix();
@@ -211,6 +212,8 @@ protected void pre(PGraphics g) {
211212
matrix.m20, matrix.m21, matrix.m22, matrix.m23,
212213
matrix.m30, matrix.m31, matrix.m32, matrix.m33);
213214
}
215+
*/
216+
g.applyMatrix(matrix);
214217
}
215218

216219
strokeSaved = g.stroke;
@@ -394,11 +397,13 @@ protected void addName(String nom, PShape shape) {
394397

395398

396399
public void translate(float tx, float ty) {
397-
translate(tx, ty, 0);
400+
checkMatrix(2);
401+
matrix.translate(tx, ty);
398402
}
399403

404+
400405
public void translate(float tx, float ty, float tz) {
401-
checkMatrix();
406+
checkMatrix(3);
402407
matrix.translate(tx, ty, 0);
403408
}
404409

@@ -407,20 +412,25 @@ public void rotateX(float angle) {
407412
rotate(angle, 1, 0, 0);
408413
}
409414

415+
410416
public void rotateY(float angle) {
411417
rotate(angle, 0, 1, 0);
412418
}
413419

420+
414421
public void rotateZ(float angle) {
415422
rotate(angle, 0, 0, 1);
416423
}
417424

425+
418426
public void rotate(float angle) {
419-
rotateZ(angle);
427+
checkMatrix(2); // at least 2...
428+
matrix.rotate(angle);
420429
}
421430

431+
422432
public void rotate(float angle, float v0, float v1, float v2) {
423-
checkMatrix();
433+
checkMatrix(3);
424434
matrix.rotate(angle, v0, v1, v2);
425435
}
426436

@@ -429,17 +439,19 @@ public void rotate(float angle, float v0, float v1, float v2) {
429439

430440

431441
public void scale(float s) {
432-
scale(s, s, s);
442+
checkMatrix(2); // at least 2...
443+
matrix.scale(s);
433444
}
434445

435446

436447
public void scale(float sx, float sy) {
437-
scale(sx, sy, 1);
448+
checkMatrix(2);
449+
matrix.scale(sx, sy);
438450
}
439451

440452

441453
public void scale(float x, float y, float z) {
442-
checkMatrix();
454+
checkMatrix(3);
443455
matrix.scale(x, y, z);
444456
}
445457

@@ -448,24 +460,51 @@ public void scale(float x, float y, float z) {
448460

449461

450462
public void resetMatrix() {
463+
checkMatrix(2);
464+
matrix.reset();
451465
}
452466

453467

468+
public void applyMatrix(PMatrix source) {
469+
if (source instanceof PMatrix2D) {
470+
applyMatrix((PMatrix2D) source);
471+
} else if (source instanceof PMatrix3D) {
472+
applyMatrix((PMatrix3D) source);
473+
}
474+
}
475+
476+
477+
public void applyMatrix(PMatrix2D source) {
478+
applyMatrix(source.m00, source.m01, 0, source.m02,
479+
source.m10, source.m11, 0, source.m12,
480+
0, 0, 1, 0,
481+
0, 0, 0, 1);
482+
}
483+
484+
454485
public void applyMatrix(float n00, float n01, float n02,
455486
float n10, float n11, float n12) {
456-
checkMatrix();
487+
checkMatrix(2);
457488
matrix.apply(n00, n01, n02, 0,
458489
n10, n11, n12, 0,
459490
0, 0, 1, 0,
460491
0, 0, 0, 1);
461492
}
462493

463494

495+
public void apply(PMatrix3D source) {
496+
applyMatrix(source.m00, source.m01, source.m02, source.m03,
497+
source.m10, source.m11, source.m12, source.m13,
498+
source.m20, source.m21, source.m22, source.m23,
499+
source.m30, source.m31, source.m32, source.m33);
500+
}
501+
502+
464503
public void applyMatrix(float n00, float n01, float n02, float n03,
465504
float n10, float n11, float n12, float n13,
466505
float n20, float n21, float n22, float n23,
467506
float n30, float n31, float n32, float n33) {
468-
checkMatrix();
507+
checkMatrix(3);
469508
matrix.apply(n00, n01, n02, n03,
470509
n10, n11, n12, n13,
471510
n20, n21, n22, n23,
@@ -476,9 +515,20 @@ public void applyMatrix(float n00, float n01, float n02, float n03,
476515
//
477516

478517

479-
protected void checkMatrix() {
518+
/**
519+
* Make sure that the shape's matrix is 1) not null, and 2) has a matrix
520+
* that can handle <em>at least</em> the specified number of dimensions.
521+
*/
522+
protected void checkMatrix(int dimensions) {
480523
if (matrix == null) {
481-
matrix = new PMatrix3D();
524+
if (dimensions == 2) {
525+
matrix = new PMatrix2D();
526+
} else {
527+
matrix = new PMatrix3D();
528+
}
529+
} else if (dimensions == 3 && (matrix instanceof PMatrix2D)) {
530+
// time for an upgrayedd for a double dose of my pimpin'
531+
matrix = new PMatrix3D(matrix);
482532
}
483533
}
484534

0 commit comments

Comments
 (0)