Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

SegmentOrderRenderer.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 /*
00024  * Created on Nov 15, 2006
00025  *
00026  * various ways of viewing EDL files
00027  * 
00028  */
00029 package com.meapsoft.visualizer;
00030 
00031 import java.awt.Color;
00032 import java.awt.Graphics;
00033 import java.awt.Point;
00034 import java.awt.Rectangle;
00035 import java.awt.event.ActionEvent;
00036 import java.awt.image.BufferedImage;
00037 import java.util.Vector;
00038 
00039 import javax.swing.BoxLayout;
00040 import javax.swing.JCheckBox;
00041 import javax.swing.JPanel;
00042 
00043 import com.meapsoft.DSP;
00044 import com.meapsoft.EDLFile;
00045 import com.meapsoft.FeatFile;
00046 
00052 public class SegmentOrderRenderer extends Renderer
00053 {               
00054         JCheckBox thickLines;
00055         JCheckBox multiLines;
00056         
00057         boolean thick = false;
00058         boolean multi = false;
00059 
00060         int currHeight = 0;
00061         int currWidth = 0;
00062         double currXMulti = 0.0;
00063         
00064         //amount of screen to use for top and bottom rows of blips
00065         double screenPercentage = .25;
00066         
00067         public SegmentOrderRenderer(FeatFile featFile, EDLFile eDLFile)
00068         {
00069                 super(featFile, eDLFile, "SegmentOrderMapping");
00070         }
00071         
00072         public SegmentOrderRenderer(Renderer r)
00073         {
00074                 super(r);
00075                 
00076                 //have to do this so we're not stuck with invalid
00077                 //multipliers from another renderer
00078                 updateColorMultipliers();
00079         }
00080 
00081         public void draw(BufferedImage image, int width, int height)
00082         {       
00083                 int numChunks = events.size();
00084                 
00085                 //set up scaling factors
00086                 ChunkVisInfo lastChunk = (ChunkVisInfo) events.elementAt(numChunks - 1);
00087                 double xMulti = width/(lastStartTime + lastChunk.length);
00088                 //use 1/4 of the screen for plotting blips
00089                 double scaledHeight = height * screenPercentage;
00090                 
00091                 Graphics graphics = image.getGraphics();
00092                 graphics.setColor(Color.white);//Color.lightGray);
00093                 graphics.fillRect(0,0,width,height);
00094                 
00095                 int blipHeight = (int)(scaledHeight/numFeatures);
00096 
00097                 for (int i = 0; i < numChunks; i++)
00098                 {
00099                         ChunkVisInfo cVI = (ChunkVisInfo) events.elementAt(i);
00100                         
00101                         int index = 0;
00102                         for (int j = 0; j < numFeatures; j++)
00103                         {
00104                 int ndim = elementsPerFeature[j];
00105 
00106                 // draw features in Matlab's 'axis xy' order
00107                                 // (i.e. lowest feature dimensions on the bottom)
00108                                 //for (int k = 0; k < ndim; k++)
00109                 for (int k = ndim - 1; k >= 0; k--)
00110                                 {
00111                     int[] featNum = {index};
00112                                         double featureValue = cVI.getFeatures(featNum)[0];
00113 
00114                     featureValue -= lowestFeatureValue[j];
00115                                         /*
00116                                         if (colorMapType == SQRT)
00117                                         {
00118                                                 featureValue -= lowestFeatureValue[j];
00119                                                 featureValue = Math.sqrt(featureValue);
00120                                         }
00121                                         else if (colorMapType == SQUARE)
00122                                         {
00123                                                 featureValue -= lowestFeatureValue[j];
00124                                                 featureValue *= featureValue;
00125                                         }
00126                                         */
00127                     double colorIndex = featureValue * colorMultipliers[j];
00128                                         
00129                                         //graphics.setColor(colormap.table[(int)colorIndex]);
00130                                         
00131                                         if (cVI.selected)
00132                                         {
00133                                                 graphics.setColor(colormap.table[(int)colorIndex]);
00134                                         }
00135                                         else
00136                                         {
00137                                                 Color c = colormap.table[(int)colorIndex].darker().darker().darker();
00138                                                 graphics.setColor(c);
00139                                         }
00140                                         int x1 = (int)(cVI.startTime * xMulti);
00141                                         int blipWidth = (int)((cVI.startTime + cVI.length) * xMulti);
00142                                         blipWidth = blipWidth - x1;
00143                     int y = (int)(j * blipHeight + k*blipHeight/ndim);
00144         
00145                                         //int miniBlipHeight = blipHeight/ndim;
00146                                         int miniBlipHeight = (blipHeight+ndim/2)/ndim;
00147                                         if (miniBlipHeight < 1)
00148                                                 miniBlipHeight = 1;
00149                                                 
00150                                         //draw top blip
00151                                         //if (cVI.selected)
00152                                         graphics.fillRect(x1, y, blipWidth, miniBlipHeight);
00153 
00154                                         //else
00155                                         //{
00156                                                 //this is wonky, but if we always draw here then
00157                                                 //multi-dimensional blips won't disappear when
00158                                                 //they're deselected. Their rects are so small that
00159                                                 //they just stack up and fill in all the space!
00160                                         //      if (k == 0)
00161                                         //              graphics.drawRect(x1, y, blipWidth, blipHeight);                                
00162                                         //}
00163                                         
00164                                         //draw bottom blip
00165                                         if (eDLFile != null)
00166                                         {
00167                                                 if (cVI.dstTime != -1.0)
00168                                                 {
00169                                                         int x2 = (int)(cVI.dstTime * xMulti);
00170 
00171                                                         //int y2 = (height - y) - blipHeight/ndim; 
00172                             // draw the bottom blip in the same
00173                             // orientation as the top blip
00174                             int y2 = height - j*blipHeight - (ndim-k)*blipHeight/ndim;
00175                                                         //if (cVI.selected)
00176                                                                 graphics.fillRect(x2, y2, blipWidth + 1, miniBlipHeight);
00177                                                         //else
00178                                                         //{
00179                                                         //      if (k == 0)
00180                                                         //              graphics.drawRect(x2, y2, blipWidth, blipHeight);
00181                                                         //}                                                     
00182                                                         cVI.xEDL = x2;
00183                                                         cVI.yEDL = y2;
00184                                                 }
00185                                         }
00186                                         
00187                                         index++;
00188 
00189                                         cVI.xFeat = x1;
00190                                         cVI.yFeat = y;
00191                                         cVI.width = blipWidth;
00192                                         cVI.height = blipHeight;
00193                                 }
00194                         }
00195                 }
00196                 
00197                 //now do connections, if EDL file exists
00198                 //we have to do it in its own loop, otherwise our messy chunk order
00199                 //will result in lots of overlapping drawing!
00200                 if (eDLFile != null)
00201                 {
00202                         for (int i = 0; i < numChunks; i++)
00203                         {
00204                                 ChunkVisInfo cVI = (ChunkVisInfo) events.elementAt(i);
00205                                 
00206                                 int index = 0;
00207                         
00208                                 for (int j = 0; j < numFeatures; j++)
00209                                 {
00210 /*
00211                                         for (int k = 0; k < elementsPerFeature[j]; k++)
00212                                         {
00213                                                 int[] featNum = {index};
00214                                                 double[] values = cVI.getFeatures(featNum);
00215                                                 double featureValue = values[0];
00216                                                 
00217                                                 if (colorMapType == SQRT)
00218                                                 {
00219                                                         featureValue -= lowestFeatureValue[j];
00220                                                         featureValue = Math.sqrt(featureValue);
00221                                                 }
00222                                                 else if (colorMapType == SQUARE)
00223                                                 {
00224                                                         featureValue -= lowestFeatureValue[j];
00225                                                         featureValue *= featureValue;
00226                                                 }
00227                                                 else
00228                                                         featureValue -= lowestFeatureValue[j];
00229                 
00230                                                 double colorIndex = featureValue * colorMultipliers[j];
00231         
00232                                                 graphics.setColor(colormap.table[(int)colorIndex]);
00233 */
00234 
00235                     int ndim = elementsPerFeature[j];
00236 
00237                     int[] featNum = DSP.irange(index, index+ndim-1);
00238                     double[] features = cVI.getFeatures(featNum);
00239                     features = DSP.minus(features, lowestFeatureValue[j]);
00240                     double featureValue = DSP.dot(features, features)/Math.sqrt(ndim);
00241 
00242                     featureValue = features[ndim-1];
00243                     /*
00244                     if (sqrtFeatureValues.isSelected())
00245                         featureValue = Math.sqrt(featureValue);
00246                     else if (squareFeatureValues.isSelected())
00247                         featureValue *= featureValue;
00248                     */
00249                     double colorIndex = featureValue * colorMultipliers[j];
00250                     
00251                     graphics.setColor(colormap.table[(int)colorIndex]);
00252                                         
00253                     int x1 = (int)(cVI.startTime * xMulti);
00254                     int blipWidth = (int)((cVI.startTime + cVI.length) * xMulti);
00255                     blipWidth = blipWidth - x1;
00256                     int y = (int)(j * blipHeight);
00257                     
00258                     if (cVI.dstTime != -1.0)
00259                     {
00260                         int x2 = (int)(cVI.dstTime * xMulti);
00261                         
00262                         //draw bottom blip
00263                         if (cVI.selected)
00264                         {                                                               
00265                             //draw connection
00266                             if (multi)
00267                             {
00268                                 graphics.drawLine(x1 + blipWidth/2, y + blipHeight, 
00269                                                   x2 + blipWidth/2, (height - y) - blipHeight);
00270                                 if (thick)
00271                                     graphics.drawLine((x1 + blipWidth/2)+1, y + blipHeight, 
00272                                                       (x2 + blipWidth/2)+1, (height - y) - blipHeight);
00273                             }
00274                             else
00275                             {
00276                                 if (j == numFeatures - 1)
00277                                 {
00278                                     graphics.drawLine(x1 + blipWidth/2, y + blipHeight, 
00279                                                       x2 + blipWidth/2, (height - y) - blipHeight);
00280                                     if (thick)
00281                                         graphics.drawLine((x1 + blipWidth/2)+1, y + blipHeight, 
00282                                                           (x2 + blipWidth/2)+1, (height - y) - blipHeight);
00283                                 }
00284                             }
00285                         }
00286                     }
00287 
00288                     index += ndim;
00289 
00290                                         graphics.setColor(Color.black);
00291                                         y = (blipHeight * j) + blipHeight;
00292                                         graphics.drawLine(0, y, width, y);
00293                                         graphics.drawLine(0, height - y, width, height - y);
00294                                 }
00295                         }
00296                 }
00297 
00298                 if (dragRect != null)
00299                 {
00300                         //System.out.println("drawing: " + dragRect.toString());
00301                         graphics.setColor(Color.black);
00302                         graphics.drawRect(dragRect.x, dragRect.y, dragRect.width, dragRect.height);
00303                 }
00304                 
00305                 currHeight = height;
00306                 currWidth = width;
00307                 currXMulti = xMulti;
00308         }
00309 
00310         public Vector getChunkVisInfosForPoint(Point p)
00311         {
00312                 Vector chunks = new Vector();
00313                 
00314                 int whichFeature = getFeatureNumberForPoint(p);
00315                 
00316                 if (whichFeature != -1)
00317                 {
00318                         for (int i = 0; i < events.size(); i++)
00319                         {
00320                                 ChunkVisInfo c = (ChunkVisInfo) events.elementAt(i);
00321                                 if (p.y >= 0 && p.y <= (currHeight * screenPercentage))
00322                                 {
00323                                         if (p.x >= c.xFeat && p.x <= (c.xFeat + c.width))
00324                                                 chunks.add(c);
00325                                 }
00326                                 else if (p.y <= currHeight && p.y >= currHeight - (currHeight * screenPercentage))
00327                                 {
00328                                         if (p.x >= c.xEDL && p.x <= (c.xEDL + c.width))
00329                                                 chunks.add(c);
00330                                 }
00331                                 
00332                         }
00333                 }
00334 
00335                 return chunks;
00336         }
00337         
00338         public int getFeatureNumberForPoint(Point p)
00339         {
00340                 int featureNumber = -1;
00341                 
00342                 if (featFile != null)
00343                 {
00344                         double scaledHeight = currHeight * screenPercentage;
00345                         int blipHeight = (int)(scaledHeight/numFeatures);
00346                         //this only happens when we start up if the mouse moves over display
00347                         //before file is fully loaded/drawn
00348                         if (blipHeight == 0)
00349                                 return featureNumber;
00350                                 
00351                         int whichFeature = p.y/blipHeight;      
00352 
00353                         //try top
00354                         if (whichFeature < numFeatures && whichFeature >= 0)
00355                         {
00356                                 featureNumber = whichFeature;
00357                                 return featureNumber;
00358                         }
00359                         
00360                         //try bottom
00361                         whichFeature = (currHeight - p.y)/blipHeight;
00362                         if (whichFeature < numFeatures && whichFeature >= 0)
00363                         {
00364                                 featureNumber = whichFeature;
00365                                 return featureNumber;
00366                         }
00367                 }
00368                 
00369                 return featureNumber;
00370         }
00371         
00372         public String getFeatureNameForPoint(Point p)
00373         {
00374                 String featureName = "i don't know!";
00375                 
00376                 int whichFeature = getFeatureNumberForPoint(p);
00377                 
00378                 if (whichFeature != -1)
00379                 {
00380                         String fullFeatureName = (String) featFile.featureDescriptions.elementAt(whichFeature);
00381                         //we're splitting on "." but have to use an escape sequence!
00382                         String[] chunks = fullFeatureName.split("\\.");
00383                         featureName = chunks[chunks.length-1];
00384                 }
00385                         
00386                 return featureName;     
00387         }
00388 
00389         public double getFeatureValueForPoint(Point p)
00390         {
00391                 double value = 0.0;
00392                 
00393                 int whichFeature = getFeatureNumberForPoint(p);
00394                 
00395                 if (whichFeature != -1)
00396                 {
00397                         for (int i = 0; i < events.size(); i++)
00398                         {
00399                                 ChunkVisInfo cVI = (ChunkVisInfo) events.elementAt(i);
00400                                 
00401                                 if (p.y >= 0 && p.y <= (currHeight * screenPercentage))
00402                                 {
00403                                         if (p.x >= cVI.xFeat && p.x <= cVI.xFeat + cVI.width)
00404                                         {
00405                                                 int[] featNum = {0};
00406                                                 
00407                                                 for (int j = 0; j < whichFeature; j++)
00408                                                         featNum[0] += elementsPerFeature[j];
00409                                                 value = cVI.getFeatures(featNum)[0];
00410                                                 return value;
00411                                         }
00412                                 }
00413                                 
00414                                 else if (p.y <= currHeight && p.y >= currHeight - (currHeight/4))
00415                                 {
00416                                         if (p.x >= cVI.xEDL && p.x <= cVI.xEDL + cVI.width)
00417                                         {
00418                                                 int[] featNum = {0};
00419                                                 
00420                                                 for (int j = 0; j < whichFeature; j++)
00421                                                         featNum[0] += elementsPerFeature[j];
00422                                                 value = cVI.getFeatures(featNum)[0];
00423                                                 return value;
00424                                         }
00425                                 }
00426                                 
00427                         }
00428                 }
00429                 
00430                 return value;
00431         }
00432         
00433         public void rangeFilterSelectionChanged()
00434         {
00435         }
00436         
00437         public void setDragRect(Rectangle r, boolean dS)
00438         {
00439                 dragRect = r;
00440                 dragShift = dS;
00441                 
00442                 if (!dragShift)
00443                 {
00444                         selectNone();
00445                 }
00446                 
00447                 for (int i = 0; i < events.size(); i++)
00448                 {
00449                         ChunkVisInfo cVI = (ChunkVisInfo)events.elementAt(i);
00450                         Rectangle chunkRect = null;
00451                         
00452                         if (dragRect.y < currHeight/2)
00453                         {
00454                                 chunkRect = new Rectangle(cVI.xFeat, cVI.yFeat, cVI.width, cVI.height);
00455                         }
00456                         else
00457                         {
00458                                 chunkRect = new Rectangle(cVI.xEDL, cVI.yEDL, cVI.width, cVI.height);
00459                         }
00460                         if ((dragRect.x >= chunkRect.x && 
00461                                 dragRect.x <= chunkRect.x + chunkRect.width) ||
00462                                 (dragRect.x + dragRect.width >= chunkRect.x && 
00463                                 dragRect.x + dragRect.width <= chunkRect.x + chunkRect.width) ||
00464                                 (dragRect.x <= chunkRect.x && 
00465                                 dragRect.x + dragRect.width >= chunkRect.x + chunkRect.width))
00466                                 cVI.selected = true;
00467                 }
00468                 
00469                 dragRect = null;
00470                 dragShift = false;
00471                 
00472                 drawingPanel.repaint(); 
00473         }
00474         
00475         public void actionPerformed(ActionEvent arg0)
00476         {               
00477                 super.actionPerformed(arg0);
00478                 
00479                 Object source = arg0.getSource();
00480                 
00481                 if (source == multiLines)
00482                 {
00483                         JCheckBox cb = (JCheckBox)source;
00484                         
00485                         multi = cb.isSelected();
00486                 }
00487                 if (source == thickLines)
00488                 {
00489                         JCheckBox cb = (JCheckBox)source;
00490                         
00491                         thick = cb.isSelected();
00492                 }
00493         }
00494         
00495         public JPanel buildGUI(Color bgColor)
00496         {       
00497                 JPanel panel = super.buildGUI(bgColor);
00498                 
00499                 JPanel linesPanel = new JPanel();
00500                 linesPanel.setBackground(bgColor);
00501                 linesPanel.setLayout(new BoxLayout(linesPanel, BoxLayout.Y_AXIS));
00502                 
00503                 multiLines = new JCheckBox("multi lines");
00504                 multiLines.setBackground(bgColor);
00505                 multiLines.addActionListener(this);//dPanel);
00506                 linesPanel.add(multiLines);
00507                 
00508                 thickLines = new JCheckBox("thick lines");
00509                 thickLines.setBackground(bgColor);
00510                 thickLines.addActionListener(this);//dPanel);
00511                 linesPanel.add(thickLines);
00512                 
00513                 controlsPanel.add(linesPanel);
00514                 
00515                 return panel;
00516         }
00517 
00518 
00519 }
00520 
00521 
00522 

Generated on Tue Feb 6 19:02:27 2007 for MEAPsoft by doxygen1.2.18