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.io.BufferedReader;
033import java.io.File;
034import java.io.FileReader;
035import java.io.IOException;
036import java.util.ArrayList;
037import java.util.List;
038
039import org.openimaj.content.animation.animator.ForwardBackwardLoopingValueAnimator;
040import org.openimaj.content.animation.animator.LinearDoubleValueAnimator;
041import org.openimaj.content.animation.animator.ValueAnimator;
042import org.openimaj.image.DisplayUtilities;
043import org.openimaj.image.FImage;
044import org.openimaj.math.geometry.point.Point2dImpl;
045import org.openimaj.math.geometry.point.PointList;
046import org.openimaj.math.geometry.shape.PointDistributionModel;
047import org.openimaj.math.geometry.transforms.TransformUtilities;
048import org.openimaj.video.AnimatedVideo;
049import org.openimaj.video.VideoDisplay;
050
051public class PDMPlayground3 {
052        static PointList readPts(File file) throws IOException {
053                PointList pl = new PointList();
054                BufferedReader br = new BufferedReader(new FileReader(file));
055
056                br.readLine();
057                br.readLine();
058                br.readLine();
059
060                String line;
061                while ((line = br.readLine()) != null) {
062                        if (!line.startsWith("}") && line.trim().length() > 0) {
063                                String[] parts = line.split("\\s+");
064
065                                float x = Float.parseFloat(parts[0].trim());
066                                float y = Float.parseFloat(parts[1].trim());
067
068                                pl.points.add(new Point2dImpl(x, y));
069                        }
070                }
071                br.close();
072
073                return pl;
074        }
075
076        /**
077         * @param args
078         * @throws IOException
079         */
080        public static void main(String[] args) throws IOException {
081                File dir = new File("/Users/jsh2/Downloads/am_tools/points/");
082
083                File[] fileList = new File[] {
084                                new File(dir, "107_0764.pts"),
085                                new File(dir, "107_0766.pts"),
086                                new File(dir, "107_0779.pts"),
087                                new File(dir, "107_0780.pts"),
088                                new File(dir, "107_0781.pts"),
089                                new File(dir, "107_0782.pts"),
090                                new File(dir, "107_0784.pts"),
091                                new File(dir, "107_0785.pts"),
092                                new File(dir, "107_0786.pts"),
093                                new File(dir, "107_0787.pts"),
094                                new File(dir, "107_0788.pts"),
095                                new File(dir, "107_0789.pts"),
096                                new File(dir, "107_0790.pts"),
097                                new File(dir, "107_0791.pts"),
098                                new File(dir, "107_0792.pts")
099                };
100
101                List<PointList> pls = new ArrayList<PointList>();
102                for (File f : fileList) {
103                        pls.add(readPts(f));
104                }
105
106                final PointDistributionModel pdm = new PointDistributionModel(pls);
107
108                FImage img = new FImage(200,200);
109                img.drawPoints(pdm.getMean().transform(TransformUtilities.translateMatrix(100, 100).times(TransformUtilities.scaleMatrix(50, 50))), 1f, 1);
110                DisplayUtilities.display(img);
111
112                pdm.setNumComponents(20);
113
114                final double sd = pdm.getStandardDeviations(3.0)[0];
115                
116                VideoDisplay.createVideoDisplay(new AnimatedVideo<FImage>(new FImage(200,200)) {
117                        ValueAnimator<Double> a = ForwardBackwardLoopingValueAnimator.loop(new LinearDoubleValueAnimator(-sd, sd, 60)); 
118
119                        @Override
120                        protected void updateNextFrame(FImage frame) {
121                                frame.fill(0f);
122
123                                //PointList newShape = pdm.generateNewShape( new double[] {a.nextValue()} );
124                                PointList newShape = pdm.generateNewShape( new double[] {-sd} );
125                                frame.drawPoints(newShape.transform(TransformUtilities.translateMatrix(100, 100).times(TransformUtilities.scaleMatrix(50, 50))), 1f, 1);
126                        }
127                });             
128        }
129}