$search
00001 /******************************************************************************* 00002 * Copyright (c) 2012 Stefan Profanter. All rights reserved. This program and the accompanying 00003 * materials are made available under the terms of the GNU Public License v3.0 which accompanies 00004 * this distribution, and is available at http://www.gnu.org/licenses/gpl.html 00005 * 00006 * Contributors: Stefan Profanter - initial API and implementation, Year: 2012 00007 ******************************************************************************/ 00008 package edu.tum.cs.vis.model.util; 00009 00010 import java.awt.Color; 00011 import java.io.Serializable; 00012 00013 import processing.core.PGraphics; 00014 00021 public abstract class DrawObject implements Serializable { 00025 private static final long serialVersionUID = -1917773602783043823L; 00026 00036 public static float[][] MatrixMultiply(float mat1[][], float mat2[][]) { 00037 int x = mat1.length; 00038 int y = mat2.length; 00039 float result[][] = new float[x][y]; 00040 00041 for (int i = 0; i < x; i++) { 00042 for (int j = 0; j < y - 1; j++) { 00043 for (int k = 0; k < y; k++) { 00044 00045 result[i][j] += mat1[i][k] * mat2[k][j]; 00046 } 00047 } 00048 } 00049 00050 return result; 00051 } 00052 00056 protected Vertex position[]; 00057 00061 protected Appearance appearance; 00062 00069 public DrawObject(final int numberOfEdges) { 00070 position = new Vertex[numberOfEdges]; 00071 } 00072 00081 protected void applyColor(PGraphics g, Color overrideColor) { 00082 if (appearance == null) { 00083 00084 g.noStroke(); 00085 if (overrideColor != null) 00086 g.fill(overrideColor.getRed(), overrideColor.getGreen(), overrideColor.getBlue(), 00087 overrideColor.getAlpha()); 00088 else 00089 g.fill(200, 200, 200); 00090 return; 00091 } 00092 if (appearance.getColorLine() != null) { 00093 if (overrideColor != null) 00094 g.stroke(overrideColor.getRed(), overrideColor.getGreen(), overrideColor.getBlue(), 00095 overrideColor.getAlpha()); 00096 else 00097 g.stroke(appearance.getColorLine().getRed(), appearance.getColorLine().getGreen(), 00098 appearance.getColorLine().getBlue(), appearance.getColorLine().getAlpha()); 00099 g.strokeWeight(appearance.getStrokeWeight()); 00100 } else { 00101 g.noStroke(); 00102 } 00103 00104 if (overrideColor != null) 00105 g.fill(overrideColor.getRed(), overrideColor.getGreen(), overrideColor.getBlue(), 00106 overrideColor.getAlpha()); 00107 else if (appearance.getImageReference() == null) { 00108 if (appearance.getColorFill() != null) { 00109 g.fill(appearance.getColorFill().getRed(), appearance.getColorFill().getGreen(), 00110 appearance.getColorFill().getBlue(), appearance.getColorFill().getAlpha()); 00111 } else { 00112 g.noFill(); 00113 } 00114 } else { 00115 // Has texture 00116 // Use fallback if texture isn't drawn. So fill triangles with white color 00117 g.fill(255, 255, 255, 0); 00118 } 00119 } 00120 00121 @Override 00122 public boolean equals(Object o) { 00123 if (o == this) { 00124 return true; 00125 } 00126 if (!(o instanceof DrawObject)) { 00127 return false; 00128 } 00129 00130 DrawObject t = (DrawObject) o; 00131 if (t.position.length != t.position.length) 00132 return false; 00133 int cnt = 0; 00134 int maxCnt = position.length; 00135 for (int k = 0; k < maxCnt; k++) { 00136 for (int l = 0; l < maxCnt; l++) { 00137 if (t.position[k].equals(position[l])) 00138 cnt++; 00139 } 00140 } 00141 return (cnt == maxCnt); 00142 } 00143 00147 public Appearance getAppearance() { 00148 return appearance; 00149 } 00150 00154 public Vertex[] getPosition() { 00155 return position; 00156 } 00157 00164 public void scale(float factor) { 00165 for (int v = 0; v < position.length; v++) { 00166 position[v].x *= factor; 00167 position[v].y *= factor; 00168 position[v].z *= factor; 00169 } 00170 updateCentroid(); // Recalculate centroid 00171 } 00172 00177 public void setAppearance(Appearance appearance) { 00178 this.appearance = appearance; 00179 } 00180 00187 public void setPosition(Vertex[] position) { 00188 this.position = position; 00189 updateCentroid(); 00190 } 00191 00198 public void transform(float[][] matrix) { 00199 for (int v = 0; v < position.length; v++) { 00200 float[] newPos = new float[4]; 00201 for (int row = 0; row < 4; row++) { 00202 newPos[row] = position[v].x * matrix[row][0] + position[v].y * matrix[row][1] 00203 + position[v].z * matrix[row][2] + matrix[row][3]; 00204 } 00205 position[v].x = newPos[0] / newPos[3]; 00206 position[v].y = newPos[1] / newPos[3]; 00207 position[v].z = newPos[2] / newPos[3]; 00208 } 00209 updateCentroid(); 00210 } 00211 00217 public void updateCentroid() { 00218 /* 00219 * Overridden in triangles class 00220 */ 00221 } 00222 00223 }