001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.demos.sandbox; 031 032import java.awt.Component; 033import java.awt.Dimension; 034import java.awt.GridBagLayout; 035import java.awt.GridLayout; 036import java.io.IOException; 037import java.util.List; 038 039import javax.swing.JCheckBox; 040import javax.swing.JLabel; 041import javax.swing.JPanel; 042import javax.swing.JSeparator; 043import javax.swing.JSlider; 044import javax.swing.SwingConstants; 045import javax.swing.event.ChangeEvent; 046import javax.swing.event.ChangeListener; 047 048import org.openimaj.content.animation.animator.DoubleArrayValueAnimator; 049import org.openimaj.content.animation.animator.RandomLinearDoubleValueAnimator; 050import org.openimaj.content.animation.animator.ValueAnimator; 051import org.openimaj.content.slideshow.Slide; 052import org.openimaj.content.slideshow.SlideshowApplication; 053import org.openimaj.image.FImage; 054import org.openimaj.math.geometry.line.Line2d; 055import org.openimaj.math.geometry.point.Point2d; 056import org.openimaj.math.geometry.point.PointList; 057import org.openimaj.math.geometry.point.PointListConnections; 058import org.openimaj.math.geometry.shape.PointDistributionModel; 059import org.openimaj.math.geometry.transforms.TransformUtilities; 060import org.openimaj.video.AnimatedVideo; 061import org.openimaj.video.VideoDisplay; 062 063public class PDMDemo implements Slide { 064 065 private VideoDisplay<FImage> display; 066 ValueAnimator<double[]> a; 067 volatile boolean animate = false; 068 private double[] stdev; 069 070 @Override 071 public Component getComponent(int width, int height) throws IOException { 072 // the main panel 073 final JPanel base = new JPanel(); 074 base.setOpaque(false); 075 base.setPreferredSize(new Dimension(width, height)); 076 base.setLayout(new GridBagLayout()); 077 078 final PointListConnections connections = PDMTest.loadConnections(); 079 final List<PointList> pointData = PDMTest.loadData(); 080 081 final PointDistributionModel pdm = new PointDistributionModel(pointData); 082 final int N = 5; 083 pdm.setNumComponents(N); 084 085 final JPanel sliderPanel = new JPanel(); 086 sliderPanel.setOpaque(false); 087 sliderPanel.setLayout(new GridLayout(0, 1)); 088 final JSlider[] sliders = new JSlider[N]; 089 for (int i = 0; i < sliders.length; i++) { 090 sliders[i] = new JSlider(-100, 100, 0); 091 sliderPanel.add(sliders[i]); 092 } 093 094 stdev = pdm.getStandardDeviations(3); 095 final double[] currentValue = new double[N]; 096 display = VideoDisplay.createVideoDisplay(new AnimatedVideo<FImage>(new FImage(600, 600)) { 097 098 @Override 099 protected void updateNextFrame(FImage frame) { 100 frame.fill(0f); 101 102 if (animate) { 103 System.arraycopy(a.nextValue(), 0, currentValue, 0, currentValue.length); 104 } else { 105 for (int i = 0; i < N; i++) { 106 final double v = sliders[i].getValue() * stdev[i] / 100.0; 107 currentValue[i] = v; 108 } 109 } 110 111 final PointList newShape = pdm.generateNewShape(currentValue); 112 final PointList tfShape = newShape.transform(TransformUtilities.translateMatrix(300, 300).times( 113 TransformUtilities.scaleMatrix(150, 150))); 114 115 final List<Line2d> lines = connections.getLines(tfShape); 116 frame.drawLines(lines, 1, 1f); 117 118 for (final Point2d pt : tfShape) { 119 frame.drawPoint(pt, 1f, 5); 120 } 121 122 for (int i = 0; i < N; i++) { 123 final int newVal = (int) (100.0 * currentValue[i] / stdev[i]); 124 sliders[i].setValue(newVal); 125 } 126 } 127 }, base); 128 129 final JPanel p = new JPanel(); 130 p.setOpaque(false); 131 final JCheckBox cb = new JCheckBox(); 132 cb.addChangeListener(new ChangeListener() { 133 @Override 134 public void stateChanged(ChangeEvent e) { 135 if (cb.isSelected()) { 136 a = makeAnimator(60, stdev, currentValue); 137 animate = true; 138 } else { 139 animate = false; 140 } 141 } 142 }); 143 p.add(new JLabel("animate:")); 144 p.add(cb); 145 final JSeparator vh = new JSeparator(SwingConstants.HORIZONTAL); 146 sliderPanel.add(vh); 147 sliderPanel.add(p); 148 149 final JSeparator vs = new JSeparator(SwingConstants.VERTICAL); 150 base.add(vs); 151 base.add(sliderPanel); 152 153 return base; 154 } 155 156 public static DoubleArrayValueAnimator makeAnimator(int duration, double[] maxs, double[] initial) { 157 final RandomLinearDoubleValueAnimator[] animators = new RandomLinearDoubleValueAnimator[maxs.length]; 158 159 for (int i = 0; i < maxs.length; i++) 160 animators[i] = new RandomLinearDoubleValueAnimator((-maxs[i]), maxs[i], duration, initial[i]); 161 162 return new DoubleArrayValueAnimator(animators); 163 } 164 165 @Override 166 public void close() { 167 display.close(); 168 } 169 170 public static void main(String[] args) throws IOException { 171 new SlideshowApplication(new PDMDemo(), 1024, 768); 172 } 173}