View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.demos.image;
31  
32  import javax.swing.JFrame;
33  
34  import org.openimaj.demos.Demo;
35  import org.openimaj.image.DisplayUtilities;
36  import org.openimaj.image.MBFImage;
37  import org.openimaj.image.colour.ColourSpace;
38  import org.openimaj.image.colour.RGBColour;
39  import org.openimaj.math.geometry.shape.Ellipse;
40  import org.openimaj.math.geometry.transforms.TransformUtilities;
41  
42  import Jama.Matrix;
43  
44  /**
45   * 	Demonstrates affine transforms for shapes.	
46   * 
47   *  @author Sina Samangooei (ss@ecs.soton.ac.uk)
48   *	
49   *	@created 15 Feb 2012
50   */
51  @Demo(
52  	author = "Sina Samangooei", 
53  	description = "Demonstrates affine transforms for shapes.", 
54  	keywords = { "shape", "affine", "transform" }, 
55  	title = "Affine Shape Transforms",
56  	icon = "/org/openimaj/demos/icons/image/affine-icon.png"
57  )
58  public class TestShapeTransforms {
59  	private static Runnable displayUpdater;
60  	private static JFrame frame;
61  	private static double rotation = Math.PI*2/4;
62  	private static Ellipse ellipse;
63  	private static MBFImage image;
64  
65  	/**
66  	 * 	Default main
67  	 *  @param args Command-line arguments
68  	 */
69  	public static void main(String args[]){
70  		ellipse = new Ellipse(400,400,100,50,0);
71  		image = new MBFImage(800,800,ColourSpace.RGB);
72  		frame = DisplayUtilities.display(image);
73  		displayUpdater = new Runnable(){
74  			@Override
75  			public void run() {
76  				while(true){
77  					DisplayUtilities.display(image,frame);
78  					update();
79  					try {
80  						Thread.sleep(1000/30);
81  					} catch (InterruptedException e) {
82  					}
83  				}
84  			}
85  			
86  		};
87  		Thread t = new Thread(displayUpdater);
88  		t.start();
89  	}
90  	
91  	private static void update() {
92  		rotation += Math.PI/30;
93  //		int dx = 100;
94  //		int dy = 100;
95  //		float x = (float) (Math.cos(rotation) * dx + Math.sin(rotation) * dy);
96  //		float y = (float) (-Math.sin(rotation) * dx + Math.cos(rotation) * dy);
97  		Matrix rotMat = TransformUtilities.rotationMatrixAboutPoint(rotation, ellipse.calculateCentroid().getX(), ellipse.calculateCentroid().getY());
98  //		Matrix transMat = TransformUtilities.translateMatrix(x, y);
99  		Matrix scaleMat = TransformUtilities.scaleMatrix(Math.abs(0.5 * Math.cos(rotation)) + 1, Math.abs(0.5 * Math.sin(rotation))+ 1);
100 		Matrix scaledTrans = scaleMat.times(TransformUtilities.translateMatrix(-ellipse.calculateCentroid().getX(), -ellipse.calculateCentroid().getY()));
101 		scaledTrans = TransformUtilities.translateMatrix(ellipse.calculateCentroid().getX(), ellipse.calculateCentroid().getY()).times(scaledTrans);
102 		Matrix transform = Matrix.identity(3, 3);
103 		transform = rotMat.times(transform);
104 //		transform = transMat.times(transform);
105 //		transform = scaledTrans.times(transform);
106 		image.fill(RGBColour.BLACK);
107 		image.createRenderer().drawShapeFilled(ellipse.transformAffine(transform), RGBColour.RED);
108 	}
109 	
110 }