# This file is a tcltest script to test transforming FTI icons.
# It covers the following functionalities:
# 1. Flip icons horizontally or vertically.
# 2. Rotate icons around arbitrary angles.
# 3. Scale icons using arbitrary scale factors.

set scriptDir [file normalize [file join [file dirname [info script]] ".."]]
set auto_path [linsert $auto_path 0 $scriptDir]

package require Tk
package require poFti

package require tcltest
tcltest::configure {*}$argv
source [file join [file dirname [info script]] constraints.tcl]

namespace eval ::math::test {
    namespace import ::tcltest::*

    set vtxListIcon     [list 10 10  20 10  20 20]
    set vtxListFlipHori [list 90 10  80 10  80 20]
    set vtxListFlipVert [list 10 90  20 90  20 80]

    #
    # Test-Suite 1: Flip icons horizontally and vertically.
    #
    test math-1.1 "Flip Icon horizontally" -setup {
        set iconObj [poFti Icon new]
        set itemObj [poFti Item new "Item-Polygon" "polygon"]
        $iconObj AddItem $itemObj
        $itemObj AddVertices {*}$vtxListIcon
    } -body {
        $iconObj Flip "hori"
        set itemObj [$iconObj GetItem 0]
        $itemObj GetVertices
    } -cleanup {
        $iconObj destroy
    } -result $vtxListFlipHori

    test math-1.2 "Flip Icon vertically" -setup {
        set iconObj [poFti Icon new]
        set itemObj [poFti Item new "Item-Polygon" "polygon"]
        $iconObj AddItem $itemObj
        $itemObj AddVertices {*}$vtxListIcon
    } -body {
        $iconObj Flip "vert"
        set itemObj [$iconObj GetItem 0]
        $itemObj GetVertices
    } -cleanup {
        $iconObj destroy
    } -result $vtxListFlipVert

    test math-1.3 "Call Flip with invalid direction parameter" -setup {
        set iconObj [poFti Icon new]
        set itemObj [poFti Item new "Item-Polygon" "polygon"]
        $iconObj AddItem $itemObj
        $itemObj AddVertices {*}$vtxListIcon
    } -body {
        $iconObj Flip "invalidflip"
    } -returnCodes {
        error
    } -cleanup {
        $iconObj destroy
    } -result "Invalid flip direction \"invalidflip\" specified."

    #
    # Test-Suite 2: Rotate icons around arbitrary angles.
    #
    test math-2.1 "Rotate Icon 90 degrees around (0,0)" -setup {
        set iconObj [poFti Icon new]
        set itemObj [poFti Item new "Item-Line" "line"]
        $iconObj AddItem $itemObj
        $itemObj AddVertices 10 0  20 0
    } -body {
        $iconObj Rotate 90
        set itemObj [$iconObj GetItem 0]
        floatListsIdent [list 0 10  0 20] [$itemObj GetVertices]
    } -cleanup {
        $iconObj destroy
    } -result true

    test math-2.2 "Rotate Icon 90 degrees around its center" -setup {
        set iconObj [poFti Icon new]
        set itemObj [poFti Item new "Item-Point" "point"]
        $iconObj AddItem $itemObj
        $itemObj AddVertices 20 30
    } -body {
        $iconObj Rotate 90 20 30
        set itemObj [$iconObj GetItem 0]
        floatListsIdent [list 20 30] [$itemObj GetVertex 0]
    } -cleanup {
        $iconObj destroy
    } -result true

    #
    # Test-Suite 3: Scale icons using arbitrary scale factors.
    #
    test math-3.1 "Scale Icon by (2,3) around (0,0)" -setup {
        set iconObj [poFti Icon new]
        set itemObj [poFti Item new "Item-Line" "line"]
        $iconObj AddItem $itemObj
        $itemObj AddVertices 0 0  10 10
    } -body {
        $iconObj Scale 2 3
        set itemObj [$iconObj GetItem 0]
        floatListsIdent [list 0 0  20 30] [$itemObj GetVertices]
    } -cleanup {
        $iconObj destroy
    } -result true

    test math-3.2 "Scale Icon by (3,2) around (10,20)" -setup {
        set iconObj [poFti Icon new]
        set itemObj [poFti Item new "Item-Line" "line"]
        $iconObj AddItem $itemObj
        $itemObj AddVertices 10 20  30 40
    } -body {
        $iconObj Scale 3 2 10 20
        set itemObj [$iconObj GetItem 0]
        floatListsIdent [list 10 20  70 60] [$itemObj GetVertices]
    } -cleanup {
        $iconObj destroy
    } -result true
}

tcltest::cleanupTests
namespace delete ::math::test
