Author: Kore Nordmann
Date: 2007-03-27 16:46:09 +0200 (Tue, 27 Mar 2007)
New Revision: 4779

Log:
- Added private classes for more convenient coordinate and vector 
  transformations

Added:
   trunk/Graph/src/math/rotation.php
   trunk/Graph/src/math/transformation.php
   trunk/Graph/src/math/translation.php
   trunk/Graph/tests/transformation_test.php
Modified:
   trunk/Graph/src/graph_autoload.php
   trunk/Graph/src/math/vector.php
   trunk/Graph/tests/suite.php
   trunk/Graph/tests/vector_test.php

Modified: trunk/Graph/src/graph_autoload.php
===================================================================
--- trunk/Graph/src/graph_autoload.php  2007-03-27 12:47:48 UTC (rev 4778)
+++ trunk/Graph/src/graph_autoload.php  2007-03-27 14:46:09 UTC (rev 4779)
@@ -45,6 +45,7 @@
     'ezcGraphAxisExactLabelRenderer'            => 
'Graph/renderer/axis_label_exact.php',
     'ezcGraphAxisCenteredLabelRenderer'         => 
'Graph/renderer/axis_label_centered.php',
     'ezcGraphAxisBoxedLabelRenderer'            => 
'Graph/renderer/axis_label_boxed.php',
+    'ezcGraphAxisRotatedLabelRenderer'          => 
'Graph/renderer/axis_label_rotated.php',
 
     'ezcGraphDriver'                            => 
'Graph/interfaces/driver.php',
     'ezcGraphFontRenderingException'            => 
'Graph/exceptions/font_rendering.php',
@@ -112,6 +113,9 @@
     'ezcGraphMatrixOutOfBoundingsException'     => 
'Graph/exceptions/out_of_boundings.php',
     'ezcGraphPolynom'                           => 'Graph/math/polynom.php',
     'ezcGraphBoundings'                         => 'Graph/math/boundings.php',
+    'ezcGraphTransformation'                    => 
'Graph/math/transformation.php',
+    'ezcGraphTranslation'                       => 
'Graph/math/translation.php',
+    'ezcGraphRotation'                          => 'Graph/math/rotation.php',
 
     'ezcGraphAxisStep'                          => 'Graph/structs/step.php',
     'ezcGraphCoordinate'                        => 
'Graph/structs/coordinate.php',

Added: trunk/Graph/src/math/rotation.php
===================================================================
--- trunk/Graph/src/math/rotation.php   2007-03-27 12:47:48 UTC (rev 4778)
+++ trunk/Graph/src/math/rotation.php   2007-03-27 14:46:09 UTC (rev 4779)
@@ -0,0 +1,67 @@
+<?php
+/**
+ * File containing the ezcGraphRotation class
+ *
+ * @package Graph
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @access private
+ */
+/**
+ * Class to create rotation matrices from given rotation and center point
+ *
+ * @package Graph
+ * @access private
+ */
+class ezcGraphRotation extends ezcGraphTransformation
+{
+    /**
+     * Rotation in degrees
+     * 
+     * @var float
+     */
+    protected $rotation;
+
+    /**
+     * Center point 
+     * 
+     * @var ezcGraphCoordinate
+     */
+    protected $center;
+
+    /**
+     * Construct rotation matrix from rotation (in degrees) and optional 
+     * center point.
+     * 
+     * @param int $rotation 
+     * @param ezcGraphCoordinate $center 
+     * @return ezcGraphTransformation
+     */
+    public function __construct( $rotation = 0, ezcGraphCoordinate $center = 
null )
+    {
+        $this->rotation = (float) $rotation;
+
+        if ( $center === null )
+        {
+            $clockwiseRotation = deg2rad( $rotation );
+            $rotationMatrixArray = array( 
+                array( cos( $clockwiseRotation ), -sin( $clockwiseRotation ), 
0 ),
+                array( sin( $clockwiseRotation ), cos( $clockwiseRotation ), 0 
),
+                array( 0, 0, 1 ),
+            );
+
+            return parent::__construct( $rotationMatrixArray );
+        }
+
+        parent::__construct();
+
+        $this->center = $center;
+
+        $this->multiply( new ezcGraphTranslation( $center->x, $center->y ) );
+        $this->multiply( new ezcGraphRotation( $rotation ) );
+        $this->multiply( new ezcGraphTranslation( -$center->x, -$center->y ) );
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/math/rotation.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/Graph/src/math/transformation.php
===================================================================
--- trunk/Graph/src/math/transformation.php     2007-03-27 12:47:48 UTC (rev 
4778)
+++ trunk/Graph/src/math/transformation.php     2007-03-27 14:46:09 UTC (rev 
4779)
@@ -0,0 +1,86 @@
+<?php
+/**
+ * File containing the ezcGraphTransformation class
+ *
+ * @package Graph
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @access private
+ */
+/**
+ * Class defining transformations like scaling and rotation by a 
+ * 3x3 transformation matrix for |R^2
+ *
+ * @package Graph
+ * @access private
+ */
+class ezcGraphTransformation extends ezcGraphMatrix
+{
+    /**
+     * Constructor
+     *
+     * Creates a matrix with 3x3 dimensions. Optionally accepts an array to 
+     * define the initial matrix values. If no array is given an identity 
+     * matrix is created.
+     * 
+     * @param int $rows
+     * @param int $columns
+     * @param array $values
+     * @return void
+     */
+    public function __construct( array $values = null )
+    {
+        parent::__construct( 3, 3, $values );
+    }
+
+    /**
+     * Multiplies two matrices
+     *
+     * Multiply current matrix with another matrix and returns the result 
+     * matrix.
+     *
+     * @param ezcGraphMatrix $matrix Second factor
+     * @returns ezcGraphMatrix Result matrix
+     */
+    public function multiply( ezcGraphMatrix $matrix ) 
+    {
+        $mColumns = $matrix->columns();
+
+        // We want to ensure, that the matrix stays 3x3
+        if ( ( $this->columns !== $matrix->rows() ) &&
+             ( $this->rows !== $mColumns ) )
+        {
+            throw new ezcGraphMatrixInvalidDimensionsException( 
$this->columns, $this->rows, $mColumns, $matrix->rows() );
+        }
+
+        $result = parent::multiply( $matrix );
+
+        // The matrix dimensions stay the same, so that we can modify $this.
+        for ( $i = 0; $i < $this->rows; ++$i ) 
+        {
+            for ( $j = 0; $j < $mColumns; ++$j ) 
+            {
+                $this->set( $i, $j, $result->get( $i, $j ) );
+            }
+        }
+
+        return $this;
+    }
+
+    /**
+     * Transform a coordinate with the current transformation matrix.
+     * 
+     * @param ezcGraphCoordinate $coordinate 
+     * @return ezcGraphCoordinate
+     */
+    public function transformCoordinate( ezcGraphCoordinate $coordinate )
+    {
+        $vector = new ezcGraphMatrix( 3, 1, array( array( $coordinate->x ), 
array( $coordinate->y ), array( 1 ) ) );
+        $vector = parent::multiply( $vector );
+
+        return new ezcGraphCoordinate( $vector->get( 0, 0 ), $vector->get( 1, 
0 ) );
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/math/transformation.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/Graph/src/math/translation.php
===================================================================
--- trunk/Graph/src/math/translation.php        2007-03-27 12:47:48 UTC (rev 
4778)
+++ trunk/Graph/src/math/translation.php        2007-03-27 14:46:09 UTC (rev 
4779)
@@ -0,0 +1,29 @@
+<?php
+/**
+ * File containing the ezcGraphTranslation class
+ *
+ * @package Graph
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @access private
+ */
+/**
+ * Class creating translation matrices from given movements
+ *
+ * @package Graph
+ * @access private
+ */
+class ezcGraphTranslation extends ezcGraphTransformation
+{
+    public function __construct( $x = 0, $y = 0 )
+    {
+        parent::__construct( array( 
+            array( 1, 0, $x ),
+            array( 0, 1, $y ),
+            array( 0, 0, 1 ),
+        ) );
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/math/translation.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/Graph/src/math/vector.php
===================================================================
--- trunk/Graph/src/math/vector.php     2007-03-27 12:47:48 UTC (rev 4778)
+++ trunk/Graph/src/math/vector.php     2007-03-27 14:46:09 UTC (rev 4779)
@@ -165,6 +165,22 @@
     {
         return new ezcGraphVector( $coordinate->x, $coordinate->y );
     }
+
+    /**
+     * Transform vector using transformation matrix
+     * 
+     * @param ezcGraphTransformation $transformation 
+     * @return ezcGraphVector
+     */
+    public function transform( ezcGraphTransformation $transformation )
+    {
+        $result = $transformation->transformCoordinate( $this );
+
+        $this->x = $result->x;
+        $this->y = $result->y;
+
+        return $this;
+    }
 }
 
 ?>

Modified: trunk/Graph/tests/suite.php
===================================================================
--- trunk/Graph/tests/suite.php 2007-03-27 12:47:48 UTC (rev 4778)
+++ trunk/Graph/tests/suite.php 2007-03-27 14:46:09 UTC (rev 4779)
@@ -44,6 +44,7 @@
 require_once 'struct_test.php';
 require_once 'text_test.php';
 require_once 'tools_test.php';
+require_once 'transformation_test.php';
 require_once 'vector_test.php';
 
 /**
@@ -91,6 +92,7 @@
         $this->addTest( ezcGraphSvgDriverTest::suite() );
         $this->addTest( ezcGraphTextTest::suite() );
         $this->addTest( ezcGraphToolsTest::suite() );
+        $this->addTest( ezcGraphTransformationTest::suite() );
         $this->addTest( ezcGraphVectorTest::suite() );
     }
 

Added: trunk/Graph/tests/transformation_test.php
===================================================================
--- trunk/Graph/tests/transformation_test.php   2007-03-27 12:47:48 UTC (rev 
4778)
+++ trunk/Graph/tests/transformation_test.php   2007-03-27 14:46:09 UTC (rev 
4779)
@@ -0,0 +1,143 @@
+<?php
+/**
+ * ezcGraphTransformationTest 
+ * 
+ * @package Graph
+ * @version //autogen//
+ * @subpackage Tests
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Tests for ezcGraph class.
+ * 
+ * @package ImageAnalysis
+ * @subpackage Tests
+ */
+class ezcGraphTransformationTest extends ezcTestCase
+{
+       public static function suite()
+       {
+               return new PHPUnit_Framework_TestSuite( 
"ezcGraphTransformationTest" );
+       }
+
+    public function testCreateTransformation()
+    {
+        $transformation = new ezcGraphTransformation();
+        $matrix = new ezcGraphMatrix( 3, 3 );
+
+        $this->assertEquals(
+            $this->getAttribute( $matrix, 'matrix' ),
+            $this->getAttribute( $transformation, 'matrix' ),
+            'Transformation matrices are not aequivalent',
+            .0001
+        );
+    }
+
+    public function testCreateTransformation2()
+    {
+        $transformation = new ezcGraphTransformation(
+            array(
+                array( 2, 1, 0 ),
+                array( 2, 1, 0 ),
+                array( 2, 1, 0 ),
+            )
+        );
+
+        $matrix = new ezcGraphMatrix( 3, 3,
+            array(
+                array( 2, 1, 0 ),
+                array( 2, 1, 0 ),
+                array( 2, 1, 0 ),
+            )
+        );
+
+        $this->assertEquals(
+            $this->getAttribute( $matrix, 'matrix' ),
+            $this->getAttribute( $transformation, 'matrix' ),
+            'Transformation matrices are not aequivalent',
+            .0001
+        );
+    }
+
+    public function testCreateTranslation()
+    {
+        $transformation = new ezcGraphTranslation( 5, 5 );
+
+        $matrix = new ezcGraphMatrix( 3, 3,
+            array(
+                array( 1, 0, 5 ),
+                array( 0, 1, 5 ),
+                array( 0, 0, 1 ),
+            )
+        );
+
+        $this->assertEquals(
+            $this->getAttribute( $matrix, 'matrix' ),
+            $this->getAttribute( $transformation, 'matrix' ),
+            'Transformation matrices are not aequivalent',
+            .0001
+        );
+    }
+
+    public function testTranslateCoordinate()
+    {
+        $transformation = new ezcGraphTranslation( 5, 5 );
+
+        $testCoordinate = new ezcGraphCoordinate( 0, 0 );
+        $testCoordinate = $transformation->transformCoordinate( 
$testCoordinate );
+
+        $this->assertEquals(
+            new ezcGraphCoordinate( 5, 5 ),
+            $testCoordinate,
+            'Transformation of coordinate has wrong result.',
+            .0001
+        );
+    }
+
+    public function testCreateRotation()
+    {
+        $transformation = new ezcGraphRotation( 90 );
+
+        $matrix = new ezcGraphMatrix( 3, 3,
+            array(
+                array( 0, -1, 0 ),
+                array( 1, 0, 0 ),
+                array( 0, 0, 1 ),
+            )
+        );
+
+        $this->assertEquals(
+            $this->getAttribute( $matrix, 'matrix' ),
+            $this->getAttribute( $transformation, 'matrix' ),
+            'Transformation matrices are not aequivalent',
+            .0001
+        );
+    }
+
+    public function testCreateTranslatedRotation()
+    {
+        $transformation = new ezcGraphRotation( 90, new ezcGraphCoordinate( 
10, 10 ) );
+
+        $matrix = new ezcGraphMatrix( 3, 3,
+            array(
+                array( 0, 1, 0 ),
+                array( -1, 0, 0 ),
+                array( 0, 0, 1 ),
+            )
+        );
+
+        $testCoordinate = new ezcGraphCoordinate( 0, 0 );
+        $testCoordinate = $transformation->transformCoordinate( 
$testCoordinate );
+
+        $this->assertEquals(
+            new ezcGraphCoordinate( 20, 0 ),
+            $testCoordinate,
+            'Transformation matrices are not aequivalent',
+            .0001
+        );
+    }
+}
+
+?>


Property changes on: trunk/Graph/tests/transformation_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/Graph/tests/vector_test.php
===================================================================
--- trunk/Graph/tests/vector_test.php   2007-03-27 12:47:48 UTC (rev 4778)
+++ trunk/Graph/tests/vector_test.php   2007-03-27 14:46:09 UTC (rev 4779)
@@ -271,6 +271,26 @@
             'Result should be the vector itself'
         );
     }
+
+    public function testVectorTransform()
+    {
+        $vector = new ezcGraphVector( 0, 0 );
+
+        $result = $vector->transform( new ezcGraphRotation( -90, new 
ezcGraphCoordinate( 15, 15 ) ) );
+
+        $this->assertEquals(
+            $vector,
+            new ezcGraphVector( 0, 30 ),
+            'Vector transformation does not have the expected result',
+            .0001
+        );
+
+        $this->assertEquals(
+            $result,
+            $vector,
+            'Result should be the vector itself'
+        );
+    }
 }
 
 ?>

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to