# File : begin

if { [array get Draw_Groups "TOPOLOGY Check commands"] == "" } {
    pload TOPTEST
    pload VISUALIZATION
}

# to prevent loops limit to 16 minutes
cpulimit 1000

set rel_tol 0
set max_rel_tol_diff 0

if { [info exists imagedir] == 0 } {
   set imagedir .
}
if { [info exists test_image] == 0 } {
   set test_image photo
}

# Procedure to check equality of two reals with tolerance (relative and absolute)
help checkarea {shape area_expected tol_abs tol_rel}
proc checkarea {shape area_expected tol_abs tol_rel} {
    # compute area with half of the relative tolerance
    # to be used in comparison; 0.001 is added to avoid zero value
    set prop [uplevel sprops $shape [expr 0.5 * abs($tol_rel) + 0.001]]

    # get the value
    if { ! [regexp {Mass\s*:\s*([0-9.e+-]+)} $prop res area] } {
        puts "Error: cannot get area of the shape $shape"
        return
    }

    # compare with expected value
    checkreal "area of $shape" $area $area_expected $tol_abs $tol_rel
}

# Check if area of triangles is valid
proc CheckTriArea {shape {eps 0}} {
  upvar #0 $shape result
  set area [triarea result $eps]
  set t_area [lindex $area 0]
  set g_area [expr abs([lindex $area 1])]
  puts "area by triangles: $t_area"
  puts "area by geometry:  $g_area"
  expr ($t_area - $g_area) / $g_area * 100
}

# Check expected time
proc checktime {value expected tol_rel message} {
   set t1 [expr ${value} - ${expected}]
   set t2 [expr ${expected} * abs (${tol_rel})]

   if { abs (${t1}) <= ${t2} } {                                         
      puts "OK. ${message}, ${value} seconds, is equal to expected time - ${expected} seconds"
   } elseif {${t1} > ${t2}} {
      puts "Error. ${message}, ${value} seconds, is more than expected time - ${expected} seconds"
   } else {
      puts "Improvement. ${message}, ${value} seconds, is less than expected time - ${expected} seconds"
   }
}