金曜日, 7月 27, 2012

網模様3 : mesh textile 3


----------------------------
こないだの一連の作業(物体に三角形の網模様を作る=世間では,メッシュ生成と呼ぶらしい)が滞ってたので,再度ゴソゴソ作業.


とりあえず,小さい目標として

・小さい直方体形状を定義
・三角形のサイズパラメータ

などから,

・triangle コマンドを使い  表面の三角模様埋め(デローニ分割と呼ぶ)

を行い,

 ・tetgen を使って表面三角形から 内部に四面体敷き詰め(3次元デローニ分割)

をやろうと思う.


中長期的狙いとしては,
直方体底面 には,いくつかの まばらな 矩形領域をつくり,
デローニ分割したい.
この例は1孔だが,複数つくる.矩形でなくてもいい.


----------------------------
《直方体表面に2次元デローニ 三角形を敷き詰める》


以下のようなシェルスクリプトを作る.

#!/bin/bash

#/-----------------------------------------------------------------------
#
#                                 CreateRectangularParallelopipedSTL.sh
#                                      T.Y.   Mon. July 23, 2012
#/-----------------------------------------------------------------------

#-------------------------------------------------------
#----------  modify following parameters  --------------
#-------------------------------------------------------
####
x0=0.0
y0=0.0
z0=0.0
Lx=100.0
Ly=120.0
Lz=140.0
#dL=5.0
dL=60.0
####
FileBottom="./data/bottom.poly"
FileBottomSTL="./data/bottom.stl"
FileTop="./data/top.poly"
FileTopSTL="./data/top.stl"

FileWest="./data/west.poly"
FileWestSTL="./data/west.stl"
FileEast="./data/east.poly"
FileEastSTL="./data/east.stl"

FileSouth="./data/south.poly"
FileSouthSTL="./data/south.stl"
FileNorth="./data/north.poly"
FileNorthSTL="./data/north.stl"

FileBoxSTL="./data/BOX.stl"


#-------------------------------------------------------
#----------  compile programs  -------------------------
#-------------------------------------------------------
gfortran -O2 -Wall -o Shewchuk2STL Shewchuk2STL.f
gfortran -O2 -Wall -o TransformSTL TransformSTL.f



#-------------------------------------------------------
#----------  display parameters  -----------------------
#-------------------------------------------------------
echo " "
echo "----------------------------------------"
echo "   CreateRectangularParallelopipedSTL.sh   "
echo "----------------------------------------"
echo "x0 : "${x0}
echo "y0 : "${y0}
echo "z0 : "${z0}
echo "Lx : "${Lx}
echo "Ly : "${Ly}
echo "Lz : "${Lz}
echo "dL : "${dL}
dS=`echo ${dL} "*" ${dL} "* 0.25 * sqrt(3)"| bc`
echo "dS : "${dS}
echo " "


#-------------------------------------------------------
#----------  bottom STL creation -----------------------
#-------------------------------------------------------
vert01x=${x0}
vert01y=${y0}
vert02x=`echo ${x0}" + "${Lx} | bc`
vert02y=${y0}
vert03x=`echo ${x0}" + "${Lx} | bc`
vert03y=`echo ${y0}" + "${Ly} | bc`
vert04x=${x0}
vert04y=`echo ${y0}" + "${Ly} | bc`

echo "# bottom.poly " > ${FileBottom}
echo "4  2  0  0" >> ${FileBottom}
echo "#">> ${FileBottom}
echo "1  "${vert01x}" "${vert01y} >> ${FileBottom}
echo "2  "${vert02x}" "${vert02y} >> ${FileBottom}
echo "3  "${vert03x}" "${vert03y} >> ${FileBottom}
echo "4  "${vert04x}" "${vert04y} >> ${FileBottom}
echo "#">> ${FileBottom}
echo "4  0" >> ${FileBottom}
echo "1   1  2 ">> ${FileBottom}
echo "2   2  3 ">> ${FileBottom}
echo "3   3  4 ">> ${FileBottom}
echo "4   4  1 ">> ${FileBottom}
echo "#">> ${FileBottom}
echo "0">> ${FileBottom}

triangle -pqa${dS} ${FileBottom}
#triangle -pqa0.01 ${FileBottom}

##---------------------------------
##        Shewchuk2STL.f
##---------------------------------
FileCtrl="./Shewchuk2STL.ctl"
echo "############ Shewchuk2STL.ctl" > ${FileCtrl}
echo ${FileBottom%.poly}.1.node >> ${FileCtrl}
echo ${FileBottom%.poly}.1.ele >> ${FileCtrl}
echo ${FileBottom%.poly}.stl >> ${FileCtrl}
echo "" >> ${FileCtrl}
./Shewchuk2STL



#-------------------------------------------------------
#----------  top  STL creation   -----------------------
#-------------------------------------------------------
##---------------------------------
##        TransformSTL.f
##---------------------------------
FileCtrl="./TransformSTL.ctl"
cp ${FileBottomSTL} "./tmp.stl"

echo "#### TransformSTL.ctl #####" > ${FileCtrl}
echo "./tmp.stl" >>  ${FileCtrl}
echo ${FileTopSTL} >>  ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "1   0.0  0.0 "`echo ${z0}" + "${Lz} | bc` >>   ${FileCtrl}
echo "0   0.0  0.0  0.0" >>   ${FileCtrl}
echo " " >>   ${FileCtrl}
./TransformSTL
rm -f ./tmp.stl



#-------------------------------------------------------
#----------  west STL creation   -----------------------
#-------------------------------------------------------
vert01x=${z0}
vert01y=${y0}
vert02x=`echo ${z0}" + "${Lz} | bc`
vert02y=${y0}
vert03x=`echo ${z0}" + "${Lz} | bc`
vert03y=`echo ${y0}" + "${Ly} | bc`
vert04x=${z0}
vert04y=`echo ${y0}" + "${Ly} | bc`


echo "# west.poly " > ${FileWest}
echo "4  2  0  0" >> ${FileWest}
echo "#">> ${FileWest}
echo "1  "${vert01x}" "${vert01y} >> ${FileWest}
echo "2  "${vert02x}" "${vert02y} >> ${FileWest}
echo "3  "${vert03x}" "${vert03y} >> ${FileWest}
echo "4  "${vert04x}" "${vert04y} >> ${FileWest}
echo "#">> ${FileWest}
echo "4  0" >> ${FileWest}
echo "1   1  2 ">> ${FileWest}
echo "2   2  3 ">> ${FileWest}
echo "3   3  4 ">> ${FileWest}
echo "4   4  1 ">> ${FileWest}
echo "#">> ${FileWest}
echo "0">> ${FileWest}

triangle -pqa${dS} ${FileWest}

##---------------------------------
##        Shewchuk2STL.f
##---------------------------------
FileCtrl="./Shewchuk2STL.ctl"
echo "############ Shewchuk2STL.ctl" > ${FileCtrl}
echo ${FileWest%.poly}.1.node >> ${FileCtrl}
echo ${FileWest%.poly}.1.ele >> ${FileCtrl}
echo ${FileWest%.poly}.stl >> ${FileCtrl}
echo "" >> ${FileCtrl}
./Shewchuk2STL



##---------------------------------
##        TransformSTL.f
##---------------------------------
FileCtrl="./TransformSTL.ctl"
cp ${FileWestSTL} "./tmp.stl"

echo "#### TransformSTL.ctl #####" > ${FileCtrl}
echo "./tmp.stl" >>  ${FileCtrl}
echo ${FileWestSTL} >>  ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "1   -90.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0  0.0  0.0" >>   ${FileCtrl}
echo "0   0.0  0.0  0.0" >>   ${FileCtrl}
echo " " >>   ${FileCtrl}
./TransformSTL
rm -f ./tmp.stl


#-------------------------------------------------------
#----------  east STL creation   -----------------------
#-------------------------------------------------------

##---------------------------------
##        TransformSTL.f
##---------------------------------
FileCtrl="./TransformSTL.ctl"
cp ${FileWestSTL} "./tmp.stl"

echo "#### TransformSTL.ctl #####" > ${FileCtrl}
echo "./tmp.stl" >>  ${FileCtrl}
echo ${FileEastSTL} >>  ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "1   "`echo ${x0}" + "${Lx} | bc`" 0.0  0.0 "  >>   ${FileCtrl}
echo "0   0.0  0.0  0.0" >>   ${FileCtrl}
echo " " >>   ${FileCtrl}
./TransformSTL
rm -f ./tmp.stl



#-------------------------------------------------------
#----------  south STL creation -----------------------
#-------------------------------------------------------
vert01x=${x0}
vert01y=${z0}
vert02x=`echo ${x0}" + "${Lx} | bc`
vert02y=${z0}
vert03x=`echo ${x0}" + "${Lx} | bc`
vert03y=`echo ${z0}" + "${Lz} | bc`
vert04x=${x0}
vert04y=`echo ${z0}" + "${Lz} | bc`

echo "# south.poly " > ${FileSouth}
echo "4  2  0  0" >> ${FileSouth}
echo "#">> ${FileSouth}
echo "1  "${vert01x}" "${vert01y} >> ${FileSouth}
echo "2  "${vert02x}" "${vert02y} >> ${FileSouth}
echo "3  "${vert03x}" "${vert03y} >> ${FileSouth}
echo "4  "${vert04x}" "${vert04y} >> ${FileSouth}
echo "#">> ${FileSouth}
echo "4  0" >> ${FileSouth}
echo "1   1  2 ">> ${FileSouth}
echo "2   2  3 ">> ${FileSouth}
echo "3   3  4 ">> ${FileSouth}
echo "4   4  1 ">> ${FileSouth}
echo "#">> ${FileSouth}
echo "0">> ${FileSouth}

triangle -pqa${dS} ${FileSouth}

##---------------------------------
##        Shewchuk2STL.f
##---------------------------------
FileCtrl="./Shewchuk2STL.ctl"
echo "############ Shewchuk2STL.ctl" > ${FileCtrl}
echo ${FileSouth%.poly}.1.node >> ${FileCtrl}
echo ${FileSouth%.poly}.1.ele >> ${FileCtrl}
echo ${FileSouth%.poly}.stl >> ${FileCtrl}
echo "" >> ${FileCtrl}
./Shewchuk2STL


##---------------------------------
##        TransformSTL.f
##---------------------------------
FileCtrl="./TransformSTL.ctl"
cp ${FileSouthSTL} "./tmp.stl"

echo "#### TransformSTL.ctl #####" > ${FileCtrl}
echo "./tmp.stl" >>  ${FileCtrl}
echo ${FileSouthSTL} >>  ${FileCtrl}
echo "1   90.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0  0.0  0.0" >>   ${FileCtrl}
echo "0   0.0  0.0  0.0" >>   ${FileCtrl}
echo " " >>   ${FileCtrl}
./TransformSTL
rm -f ./tmp.stl


#-------------------------------------------------------
#----------  north STL creation -----------------------
#-------------------------------------------------------
##---------------------------------
##        TransformSTL.f
##---------------------------------
FileCtrl="./TransformSTL.ctl"
cp ${FileSouthSTL} "./tmp.stl"

echo "#### TransformSTL.ctl #####" > ${FileCtrl}
echo "./tmp.stl" >>  ${FileCtrl}
echo ${FileNorthSTL} >>  ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "0   0.0" >>   ${FileCtrl}
echo "1   0.0  "`echo ${y0}" + "${Ly} | bc` "  0.0 " >>   ${FileCtrl}
echo "0   0.0  0.0  0.0" >>   ${FileCtrl}
echo " " >>   ${FileCtrl}
./TransformSTL
rm -f ./tmp.stl


#-------------------------------------------------------
#----------  merge six rectangulars  -------------------
#-------------------------------------------------------

cat ${FileBottomSTL} > ${FileBoxSTL}
cat ${FileTopSTL} >> ${FileBoxSTL}

cat ${FileSouthSTL} >> ${FileBoxSTL}
cat ${FileNorthSTL} >> ${FileBoxSTL}

cat ${FileWestSTL} >> ${FileBoxSTL}
cat ${FileEastSTL} >> ${FileBoxSTL}

#-------------------------------------------------------
#----------  single solid STL  -------------------------
#-------------------------------------------------------
cp ${FileBoxSTL} "./tmp.stl"
echo "solid BOX" > ${FileBoxSTL}
awk '($1 != "solid" && $1 != "endsolid"){print}' "./tmp.stl" >> ${FileBoxSTL}
#awk '($1 != "solid" ){print}' "./tmp.stl" >> ${FileBoxSTL}
echo "endsolid BOX" >> ${FileBoxSTL}
rm -f "./tmp.stl"


上記のスクリプト内にあるように,途中で2つの Fortran プログラムを使う.

で,処理すると,

こんな感じのものができる.
生成物は STL ファイル(→表示はパラビュー)

----------------------------
《直方体 表面三角形から 内部に四面体敷き詰め》

3次元デローニをつかうために tetgen を使う.

以下のように STL から poly ファイル(tetgen の条件ファイル)を生成するスクリプトを作成.

 #!/bin/bash

FileInput=./data/BOX.stl
FileOutput=./data/BOX.poly
#FileInput=./data/box_shape.stl
#FileOutput=./data/box_shape.poly

echo "------------------------------------"
echo "---------  STL2POLY3d.sh -----------"
echo "------------------------------------"
echo ""
date

nStart=`awk '($1 == "solid"){printf("%d\n", NR)}' ${FileInput}`
echo "nStart : "${nStart}

nEnd=`awk '($1 == "endsolid"){printf("%d\n", NR)}' ${FileInput}`
echo "nEnd : "${nEnd}

nRow=`wc -l ${FileInput} | awk '{printf("%d\n", $1)}' `
echo "nRow : "${nRow}

nFacet=`echo " (${nEnd} - ${nStart} - 1) / 7" | bc`
echo "nFacet : "${nFacet}

nNodes=`echo " ${nFacet} * 3" | bc`
echo "nNodes : "${nNodes}

#--------------
echo "# " > ${FileOutput}
echo "#   poly data derived from STL file" >> ${FileOutput}
echo "#   input: "${FileInput} >> ${FileOutput}
echo "#  output: "${FileOutput} >> ${FileOutput}
echo "# " >> ${FileOutput}
echo "# " >> ${FileOutput}

#--------------
echo "# node part" >> ${FileOutput}
echo ${nNodes}"  3    0   0" >> ${FileOutput}
awk '($1 == "vertex"){printf("%15.5f  %15.5f  %15.5f\n", $2, $3, $4)}' ${FileInput} > "./tmp.vert"
awk '{printf("%5.5d %15.5f  %15.5f  %15.5f\n",NR, $1, $2, $3)}' "./tmp.vert" >> ${FileOutput}
echo "# " >> ${FileOutput}
echo "# " >> ${FileOutput}


#--------------
echo "# facet part" >> ${FileOutput}
echo ${nFacet}"  1" >> ${FileOutput}

nCount=1
while [ $nCount -le ${nFacet} ] ; do
    # echo "Facet Count:"${nCount}
    nCount=`expr $nCount + 1`

    vert1=`echo "($nCount - 2) * 3 + 1" | bc`
    vert2=`echo "($nCount - 2) * 3 + 2" | bc`
    vert3=`echo "($nCount - 2) * 3 + 3" | bc`

    echo "  1   0   1 " >> ${FileOutput}
    echo "  3 "${vert1}" "${vert2}" "${vert3} >> ${FileOutput}

done
echo "# " >> ${FileOutput}
echo "# " >> ${FileOutput}

#--------------
echo "# hole part" >> ${FileOutput}
echo "  0" >> ${FileOutput}
echo "# " >> ${FileOutput}
echo "# " >> ${FileOutput}

#--------------
echo "# region part" >> ${FileOutput}
echo "  0" >> ${FileOutput}
echo "# " >> ${FileOutput}
echo "# " >> ${FileOutput}

date


重複点を許容するという想定で,STL の facet を順次 poly ファイルリストに流し込んでるだけ.

ここから処理をするために,

tetgen -pK ./data/BOX.poly


とやると,以下のようなエラー


............(中略) ...................
Warning:  Point #296 is duplicated with Point #275. Ignored!
Warning:  Point #90 is duplicated with Point #72. Ignored!
Warning:  Point #297 is duplicated with Point #283. Ignored!
Warning:  Point #100 is duplicated with Point #72. Ignored!
Warning:  Point #98 is duplicated with Point #72. Ignored!
Delaunay seconds:  0
Creating surface mesh.
Recovering boundaries.
Error:  Invalid PLC! A point and a segment intersect.
  Point: 336. Segment: (62, 334).
Program stopped.


色々とチェックしてわかったが,2次元デローニ処理をするときに
以下のような不整合点をつくってしまうことが判明.




----------------------------
修正目標

CreateRectangularParallelopipedSTL.sh の入力パラメータの
dL を  triangle コマンド渡しせずに,
node 点を区切ってしまうことにすると うまく行く?
----------------------------

triangle コマンドをもう一度おさらいすると,
こんなページに行き付き,
"-Y" オプションが有効であることがわかる.


----------------------------
以下,修正版 スクリプト(作りかけ=bottom 面のみ)

#!/bin/bash

#/-----------------------------------------------------------------------
#
#                                                    samp.sh
#                                      T.Y.   Fri. July 27, 2012
#/-----------------------------------------------------------------------

#-------------------------------------------------------
#----------  modify following parameters  --------------
#-------------------------------------------------------
####
x0=0.0
y0=0.0
z0=0.0
Lx=100.0
Ly=120.0
Lz=140.0
Nx=5
Ny=6
Nz=7
dL=60.0

####
FileBottom="./data/bottom.poly"
FileBottomSTL="./data/bottom.stl"
FileTop="./data/top.poly"
FileTopSTL="./data/top.stl"

FileWest="./data/west.poly"
FileWestSTL="./data/west.stl"
FileEast="./data/east.poly"
FileEastSTL="./data/east.stl"

FileSouth="./data/south.poly"
FileSouthSTL="./data/south.stl"
FileNorth="./data/north.poly"
FileNorthSTL="./data/north.stl"

FileBoxSTL="./data/BOX.stl"


#-------------------------------------------------------
#----------  compile programs  -------------------------
#-------------------------------------------------------
gfortran -O2 -Wall -o Shewchuk2STL Shewchuk2STL.f
gfortran -O2 -Wall -o TransformSTL TransformSTL.f



#-------------------------------------------------------
#----------  display parameters  -----------------------
#-------------------------------------------------------
echo " "
echo "----------------------------------------"
echo "   CreateRectangularParallelopipedSTL.sh   "
echo "----------------------------------------"
echo "x0 : "${x0}
echo "y0 : "${y0}
echo "z0 : "${z0}
echo "Lx : "${Lx}
echo "Ly : "${Ly}
echo "Lz : "${Lz}
echo "Nx : "${Nx}
echo "Ny : "${Ny}
echo "Nz : "${Nz}
echo " "
dX=`echo " ${Lx} / ${Nx} "| bc`
dY=`echo " ${Ly} / ${Ny} "| bc`
dZ=`echo " ${Lz} / ${Nz} "| bc`
echo "dX : "${dX}
echo "dY : "${dY}
echo "dZ : "${dZ}
echo " "


#-------------------------------------------------------
#----------  bottom STL creation -----------------------
#-------------------------------------------------------
echo "# bottom.poly " > ${FileBottom}

nNodes=`echo " ( ( ${Nx} + 1 ) * 2 ) + (  ( ${Ny} - 1 ) * 2 ) "| bc`

echo "#-------------------------------">> ${FileBottom}
echo ${nNodes}"  2  0  0" >> ${FileBottom}
echo "#-------------------------------">> ${FileBottom}



nCount=0
#---- lower side ----
LoopCount=0
echo "# lower side" >> ${FileBottom}
while [ $LoopCount -lt ${Nx} ] ; do
    nCount=`expr $nCount + 1`
    LoopCount=`expr $LoopCount + 1`
    echo "Node Count:"${nCount}

    xx=`echo " ${x0} + ( ${LoopCount} - 1 ) * ${dX} " | bc`
    yy=${y0}
    echo ${nCount}" "${xx}" "${yy} >> ${FileBottom}
done

nCount=`expr $nCount + 1`
echo "Node Count:"${nCount}
xx=`echo " ${x0} +  ${Lx} " | bc`
echo ${nCount}" "${xx}" "${yy} >> ${FileBottom}
echo "# " >> ${FileBottom}


#---- right side ----
LoopCount=0
echo "# right side" >> ${FileBottom}
while [ $LoopCount -lt ${Ny} ] ; do
    nCount=`expr $nCount + 1`
    LoopCount=`expr $LoopCount + 1`
    echo "Node Count:"${nCount}

    xx=`echo " ${x0} +  ${Lx} " | bc`
    yy=`echo " ${y0} + ( ${LoopCount} ) * ${dY} " | bc`
    echo ${nCount}" "${xx}" "${yy} >> ${FileBottom}
done
echo "# " >> ${FileBottom}



#---- upper side ----
LoopCount=0
echo "# upper side" >> ${FileBottom}
while [ $LoopCount -lt ${Nx} ] ; do
    nCount=`expr $nCount + 1`
    LoopCount=`expr $LoopCount + 1`
    echo "Node Count:"${nCount}

    xx=`echo " ( ${x0} + ${Lx} )  - ${dX} *  ( ${LoopCount}  ) " | bc`
    yy=`echo " ${y0} +  ${Ly} " | bc`
    echo ${nCount}" "${xx}" "${yy} >> ${FileBottom}
done
echo "# " >> ${FileBottom}


#---- left side ----
LoopCount=0
echo "# left side" >> ${FileBottom}
Nym1=`expr ${Ny} - 1`
#while [ $LoopCount -lt ${Ny} ] ; do
while [ $LoopCount -lt ${Nym1} ] ; do
    nCount=`expr $nCount + 1`
    LoopCount=`expr $LoopCount + 1`
    echo "Node Count:"${nCount}

    xx=${x0}
    yy=`echo " ( ${y0} + ${Ly} )  - ${dY} *  ( ${LoopCount}  ) " | bc`
    echo ${nCount}" "${xx}" "${yy} >> ${FileBottom}
done
echo "# " >> ${FileBottom}


nEdge=`expr ${nNodes} + 1`
echo "#-------------------------------">> ${FileBottom}
echo ${nNodes}"  0" >> ${FileBottom}
echo "#-------------------------------">> ${FileBottom}

LoopCount=0
Nm1=`expr ${nNodes} - 2`
while [ $LoopCount -le ${Nm1} ] ; do
#while [ $LoopCount -le ${nNodes} ] ; do
    LoopCount=`expr $LoopCount + 1`
    LC2=`expr $LoopCount + 1`
    echo ${LoopCount}" "${LoopCount}" "${LC2} >> ${FileBottom}
done

LoopCount=`expr $LoopCount + 1`
echo ${LoopCount}" "${LoopCount}" 1" >> ${FileBottom}
echo "# " >> ${FileBottom}
echo "0 " >> ${FileBottom}


dL=`echo " 0.5 * ( ${dX} + ${dY} ) " | bc`
dS=`echo ${dL} "*" ${dL} "* 0.25 * sqrt(3)"| bc`


#triangle -pqY ${FileBottom}
triangle -pqa${dS}Y ${FileBottom}


##---------------------------------
##        Shewchuk2STL.f
##---------------------------------
FileCtrl="./Shewchuk2STL.ctl"
echo "############ Shewchuk2STL.ctl" > ${FileCtrl}
echo ${FileBottom%.poly}.1.node >> ${FileCtrl}
echo ${FileBottom%.poly}.1.ele >> ${FileCtrl}
echo ${FileBottom%.poly}.stl >> ${FileCtrl}
echo "" >> ${FileCtrl}
./Shewchuk2STL

Y オプションの制御より,シェルスクリプトで,
点列を順序良く出力するループ処理がめんどくさかった.

----------------------------


----------------------------






0 件のコメント: