# Library functions for the scripting library (scrib)
#
# File: scrib/lib/mesh_direct.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------
#
# --> if the name of the first proc already exists in interp, return
#
if {"" != [info commands scrib_lib_meshdirect_bolt_holes]} { return }
#
# --> procs in this file depend on the following scrib/lib/ files--source them
#     I guess we could do this by proc, too, not sure waht's best
#
if 0 {
    # none required yet
    set required_files ""
    scrib_lib_setup_source_files $required_files
}

## needs doc. is in wrong place. is misnamed.

proc scrib_lib_meshdirect_bolt_holes {d1 d2 {number_of_nodes ""} {ratio 1} {width 2} {loops ""} } {
    return [ic_meshdirect_bolt_holes $d1 $d2 $number_of_nodes $ratio $width $loops]
}


proc scrib_lib_meshdirect_remove_unclosed_loops { loops } {
    return [ic_meshdirect_remove_unclosed_loops $loops]
}

proc scrib_lib_meshdirect_get_number_of_els {curves} {
    set number_of_els 0
    foreach c $curves {
	set num_on_curve [expr [ic_geo_crv_length $c] / \
	    [lindex [ic_get_meshing_params curve $c] 1] ]
	set num_on_curve [expr round($num_on_curve)]
	set number_of_els [expr $number_of_els + $num_on_curve]
    }
    return $number_of_els
}

proc scrib_lib_meshdirect_remove_sharp_loops { loops {max_angle 60} } {
    set ret_loops ""
    foreach loop $loops {
	if {[llength $loop] == 1} {set loop [lindex $loop 0] }
	set pts ""
	set skip 0
	foreach c $loop {
	    set pts [concat $pts [ic_geo_boundary curve $c 0 1]]
	    set length [ic_geo_crv_length $c]
	    set curvature [scrib_lib_meshdirect_curve_curvature $c]
	    if {$length > 1.5} continue
	    if {$curvature < $max_angle} continue
	    set skip 1
	    break
	}
	if $skip continue
	set pt_list [lsort -unique $pts]
	foreach pt $pt_list {
	    set ccc(1) ""
	    set ccc(2) ""
	    set i 1
	    foreach c [ic_geo_incident point $pt 1] {
		if {[lsearch $loop $c] == -1} continue
		set ccc($i) $c
		incr i
	    }
	     if {[scrib_lib_feature_find_angle_between_curves $ccc(1) $ccc(2) $pt] > $max_angle} {
		set skip 1
		mess "angle for $loop at point $pt is larger than $max_angle\n"
		break
	     }
	}
	if $skip continue
	lappend ret_loops $loop

    }
    return $ret_loops
}

proc scrib_lib_meshdirect_curve_curvature {curve} {
    set tan1 [ic_geo_get_crv_tan 0 $curve]
    set tan2 [ic_geo_get_crv_tan 1 $curve]
    return [ic_vcalc angle $tan1 $tan2]
}











