home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MODEL / ATRLIB.C < prev    next >
C/C++ Source or Header  |  1996-04-15  |  15KB  |  507 lines

  1. /*
  2.  *    アトリビュートデータ制御
  3.  *
  4.  *        Copyright T.Kobayashi    1994.7.9
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11.  
  12. #include "matrix.h"
  13. #include "vector.h"
  14. #include "reader.h"
  15. #include "matclass.h"
  16. #include "strclass.h"
  17. #include "ml.h"
  18.  
  19. #include "poly.h"
  20. #include "menu.h"
  21.  
  22. static    int        FuncAttrCurrent( int, int, DataStruct* );
  23. static    int        FuncAttrAppend( int, int, DataStruct* );
  24. static    int        FuncAttrSearch( int, int, DataStruct* );
  25. static    int        FuncAttrChange( int, int, DataStruct* );
  26. static    int        FuncAttrs( int, int, DataStruct* );
  27. static    int        FuncAttrName( int, int, DataStruct* );
  28. static    int        FuncAttrFile( int, int, DataStruct* );
  29. static    int        FuncAttrCode( int, int, DataStruct* );
  30. static    int        FuncAttrReadFile( int, int, DataStruct* );
  31. static    int        FuncAttrDelete( int, int, DataStruct* );
  32. static    int        FuncAttrPolygons( int, int, DataStruct* );
  33. static    int        FuncAttrWriteFile( int, int, DataStruct* );
  34.  
  35. static    int        FuncAttrParameter( int, int, DataStruct* );
  36. static    int        FuncAttrMapType( int, int, DataStruct* );
  37. static    int        FuncAttrMapFile( int, int, DataStruct* );
  38. static    int        FuncAttrMap( int, int, DataStruct* );
  39.  
  40. static    int        StringClassID ;
  41. static    int        ColID, AmbID, DifID, SpcID, SizeID, RefID, TraID, RfrID, AttID;
  42. static    int        GetWindID, GetViewID, GetSizeID;
  43. static    int        SetWindID, SetViewID, SetSizeID;
  44.  
  45. void    AttrLibInit()
  46. {
  47.     StringClassID = ClassName( "String" );
  48.  
  49.     NewFunction( 0,                "AttrCurrent", FuncAttrCurrent );
  50.     NewFunction( 0,                "Attrs", FuncAttrs );
  51.     NewFunction( StringClassID,    "AttrAppend", FuncAttrAppend );
  52.     NewFunction( StringClassID,    "AttrSearch", FuncAttrSearch );
  53.     NewFunction( 0,                "AttrChange", FuncAttrChange );
  54.     NewFunction( 0,                "AttrName", FuncAttrName );
  55.     NewFunction( 0,                "AttrFile", FuncAttrFile );
  56.     NewFunction( 0,                "AttrCode", FuncAttrCode );
  57.     NewFunction( StringClassID,    "AttrReadFile", FuncAttrReadFile );
  58.     NewFunction( 0,                "AttrDelete", FuncAttrDelete );
  59.     NewFunction( 0,                "AttrPolygons", FuncAttrPolygons );
  60.     NewFunction( StringClassID,    "AttrWriteFile", FuncAttrWriteFile );
  61.     ColID = NewFunction( 0,                "AttrCol", FuncAttrParameter );
  62.     AmbID = NewFunction( 0,                "AttrAmb", FuncAttrParameter );
  63.     DifID = NewFunction( 0,                "AttrDif", FuncAttrParameter );
  64.     SpcID = NewFunction( 0,                "AttrSpc", FuncAttrParameter );
  65.     SizeID = NewFunction( 0,                "AttrSize", FuncAttrParameter );
  66.     RefID = NewFunction( 0,                "AttrRef", FuncAttrParameter );
  67.     TraID = NewFunction( 0,                "AttrTra", FuncAttrParameter );
  68.     RfrID = NewFunction( 0,                "AttrRfr", FuncAttrParameter );
  69.     AttID = NewFunction( 0,                "AttrAtt", FuncAttrParameter );
  70.     NewFunction( 0,                "AttrMap", FuncAttrMapType );
  71.     NewFunction( 0,             "AttrMapFile", FuncAttrMapFile);
  72.     GetWindID = NewFunction( 0,            "GetAttrMapWind", FuncAttrMap);
  73.     GetViewID = NewFunction( 0,            "GetAttrMapView", FuncAttrMap);
  74.     GetSizeID = NewFunction( 0,            "GetAttrMapSize", FuncAttrMap);
  75.     SetWindID = NewFunction( 0,            "SetAttrMapWind", FuncAttrMap);
  76.     SetViewID = NewFunction( 0,            "SetAttrMapView", FuncAttrMap);
  77.     SetSizeID = NewFunction( 0,            "SetAttrMapSize", FuncAttrMap);
  78. }
  79.  
  80. /*    カレントアトリビュートを得る    */
  81. static    int        FuncAttrCurrent( ident, args, buf )
  82. int        ident ;
  83. int        args ;
  84. DataStruct    *buf ;
  85. {
  86.     if (args == 0) {
  87.         StackPushInt( AttrCurrent );
  88.     } else {
  89.         int oldcurrent;
  90.         ArgCheck( "AttrCurrent", args, buf, TYPE_INT, TYPE_NOASN );
  91.         oldcurrent = AttrCurrent;
  92.         if (buf[0].id.i >= 0 && buf[0].id.i <= AttrLast) {
  93.             AttrCurrent = buf[0].id.i;
  94.             if (oldcurrent != AttrCurrent) {
  95.                 DrawAttrSelect();
  96.             }
  97.             StackPushInt( oldcurrent );
  98.         } else {
  99.             StackPushInt( -1 );
  100.         }
  101.     }
  102.     return RETURN_RETURN ;
  103. }
  104.  
  105. /*    アトリビュート数を得る    */
  106. static    int        FuncAttrs( ident, args, buf )
  107. int        ident ;
  108. int        args ;
  109. DataStruct    *buf ;
  110. {
  111.     StackPushInt( AttrLast + 1 );
  112.     return RETURN_RETURN ;
  113. }
  114.  
  115. /*    アトリビュートの追加    */
  116. static    int        FuncAttrAppend( ident, args, buf )
  117. int        ident ;
  118. int        args ;
  119. DataStruct    *buf ;
  120. {
  121.     int        n ;
  122.     char *name = NULL;
  123.  
  124.     if (args <= 2) {
  125.         ArgCheck( "AttrAppend", args, buf, TYPE_OBJECT, TYPE_INT, TYPE_NOASN );
  126.     } else {
  127.         ArgCheck( "AttrAppend", args, buf, TYPE_OBJECT, TYPE_INT, TYPE_OBJECT, TYPE_NOASN );
  128.         if (ObjectCheck(&buf[2], StringClassID) == FALSE) {
  129.             ExecError( "%d番目の引数の型不正です(AttrAppend)", 3 );
  130.         }
  131.         name = ((StringClass*)buf[2].od.ptr)->str ;
  132.     }
  133.  
  134.     n = AttrAppend( ((StringClass*)buf[0].od.ptr)->str );
  135.     AttrData[n].code = buf[1].id.i ;
  136.     AttrData[n].col.r = (buf[1].id.i & 2) ? COLOR_UNIT : 0;
  137.     AttrData[n].col.g = (buf[1].id.i & 4) ? COLOR_UNIT : 0;
  138.     AttrData[n].col.b = (buf[1].id.i & 1) ? COLOR_UNIT : 0;
  139.     if (name != NULL) {
  140.         strcpy( AttrData[n].fname, name);
  141.     } else {
  142.         AttrData[n].fname[0] = '\0';
  143.     }
  144.  
  145.  
  146.     StackPushInt( n );
  147.     return RETURN_RETURN ;
  148. }
  149.  
  150. /*    アトリビュートの検索    */
  151. static    int        FuncAttrSearch( ident, args, buf )
  152. int        ident ;
  153. int        args ;
  154. DataStruct    *buf ;
  155. {
  156.     StackPushInt( AttrSearch( ((StringClass*)buf[0].od.ptr)->str ) );
  157.     return RETURN_RETURN ;
  158. }
  159.  
  160. /*    セレクトされているアトリビュートを変更する    */
  161. static    int        FuncAttrChange( ident, args, buf )
  162. int        ident ;
  163. int        args ;
  164. DataStruct    *buf ;
  165. {
  166.     ArgCheck( "AttrChange", args, buf, TYPE_INT, TYPE_NOASN );
  167.     AttrChange( buf[0].id.i );
  168.     return RETURN_VOID ;
  169. }
  170.  
  171. /*    アトリビュート名を得る    */
  172. static    int        FuncAttrName( ident, args, buf )
  173. int        ident ;
  174. int        args ;
  175. DataStruct    *buf ;
  176. {
  177.     int        n ;
  178.  
  179.     if ( args < 1 )
  180.         ExecError( "引数の数が不正です(AttrName)" );
  181.     if ( buf[0].type != TYPE_INT )
  182.         ExecError( "%d番目の引数の型不正です(AttrName)", 1 );
  183.     n = buf[0].id.i ;
  184.  
  185.     if ( args >= 2 )
  186.     {
  187.         if ( buf[1].type != TYPE_OBJECT || ObjectCheck( &buf[1], StringClassID ) == FALSE )
  188.             ExecError( "%d番目の引数の型不正です(AttrName)", 2 );
  189.         strcpy( AttrData[n].name, ((StringClass*)buf[1].od.ptr)->str );
  190.     }
  191.  
  192.     buf = StackAlloc( 1 );
  193.     StringToObject( buf, AttrData[n].name );
  194.     return RETURN_RETURN ;
  195. }
  196.  
  197. /*    アトリビュートのファイル名を得る    */
  198. static    int        FuncAttrFile( ident, args, buf )
  199. int        ident ;
  200. int        args ;
  201. DataStruct    *buf ;
  202. {
  203.     int        n ;
  204.  
  205.     if ( args == 1 )
  206.     {
  207.         ArgCheck( "AttrFile", args, buf, TYPE_INT, TYPE_NOASN );
  208.         n = buf[0].id.i ;
  209.         if (n < 0 || n > AttrLast) {
  210.             return RETURN_VOID;
  211.         }
  212.     }
  213.     else
  214.     {
  215.         ArgCheck( "AttrFile", args, buf, TYPE_INT, TYPE_OBJECT, TYPE_NOASN );
  216.         n = buf[0].id.i ;
  217.         if ( ObjectCheck( &buf[1], StringClassID ) == FALSE )
  218.             ExecError( "%d番目の引数の型不正です(ObjFile)", 2 );
  219.         if (n < 0 || n > AttrLast) {
  220.             return RETURN_VOID;
  221.         }
  222.         strcpy( AttrData[n].fname, ((StringClass*)buf[1].od.ptr)->str );
  223.     }
  224.  
  225.     buf = StackAlloc( 1 );
  226.     StringToObject( buf, AttrData[n].fname );
  227.     return RETURN_RETURN ;
  228. }
  229.  
  230. /*    アトリビュートのコードを得る    */
  231. static    int        FuncAttrCode( ident, args, buf )
  232. int        ident ;
  233. int        args ;
  234. DataStruct    *buf ;
  235. {
  236.     int        n ;
  237.  
  238.     if ( args < 1 )
  239.         ExecError( "引数の数が不正です(AttrCode)" );
  240.     if ( buf[0].type != TYPE_INT )
  241.         ExecError( "%d番目の引数の型不正です(AttrCode)", 1 );
  242.     n = buf[0].id.i ;
  243.  
  244.     if ( args >= 2 )
  245.     {
  246.         if ( buf[1].type != TYPE_INT )
  247.             ExecError( "%d番目の引数の型不正です(AttrCode)", 2 );
  248.         AttrData[n].code = buf[1].id.i ;
  249. /*
  250.         AttrData[n].col.r = (buf[1].id.i & 2) ? COLOR_UNIT : 0;
  251.         AttrData[n].col.g = (buf[1].id.i & 4) ? COLOR_UNIT : 0;
  252.         AttrData[n].col.b = (buf[1].id.i & 1) ? COLOR_UNIT : 0;
  253. */
  254.     }
  255.  
  256.     StackPushInt( AttrData[n].code );
  257.     return RETURN_RETURN ;
  258. }
  259.  
  260.  
  261. /*    アトリビュートをファイルから読み込む    */
  262. static    int        FuncAttrReadFile( ident, args, buf )
  263. int        ident ;
  264. int        args ;
  265. DataStruct    *buf ;
  266. {
  267.     int mode = FILE_APPEND;
  268.  
  269.     if (args > 1) {
  270.         ArgCheck( "AttrReadFile", args, buf, TYPE_OBJECT, TYPE_INT|TYPE_BOOLEAN,TYPE_NOASN );
  271.         mode = buf[1].id.i;
  272.     } else {
  273.         ArgCheck( "AttrReadFile", args, buf, TYPE_OBJECT, TYPE_NOASN );
  274.     }
  275.  
  276.     StackPushInt(AttrReadFile( ((StringClass*)buf[0].od.ptr)->str, mode ));
  277.     return RETURN_RETURN;
  278. }
  279.  
  280. /*    アトリビュートを削除する    */
  281. static    int        FuncAttrDelete( ident, args, buf )
  282. int        ident ;
  283. int        args ;
  284. DataStruct    *buf ;
  285. {
  286.     ArgCheck( "AttrDelete", args, buf, TYPE_INT, TYPE_NOASN );
  287.     StackPushBoolean( AttrDelete( buf[0].id.i ) );
  288.     return RETURN_RETURN ;
  289. }
  290.  
  291.  
  292. /*    ポリゴン数を数える    */
  293. static    int        FuncAttrPolygons( ident, args, buf )
  294. int        ident ;
  295. int        args ;
  296. DataStruct    *buf ;
  297. {
  298.     int n = 0;
  299.     Polygon    *poly ;
  300.     poly = PolyTop();
  301.     if ( args >= 1 )
  302.     {
  303.         int atrno;
  304.         ArgCheck( "Polygons", args, buf, TYPE_INT, TYPE_NOASN );
  305.         atrno = buf[0].id.i;
  306.         while( poly != NULL )
  307.         {
  308.             if (poly->atr == atrno) {
  309.                 n++;
  310.             }
  311.             poly = PolyNext( poly );
  312.         }
  313.     } else {
  314.         while( poly != NULL )
  315.         {
  316.             n++;
  317.             poly = PolyNext( poly );
  318.         }
  319.     }
  320.     StackPushInt( n );
  321.     return RETURN_RETURN ;
  322. }
  323.  
  324. /*    アトリビュートファイルを書き出す    */
  325. static    int        FuncAttrWriteFile( ident, args, buf )
  326. int        ident ;
  327. int        args ;
  328. DataStruct    *buf ;
  329. {
  330.     int atrno, mode;
  331.     char *fname;
  332.  
  333.     if ( args <= 2 )
  334.     {
  335.         ArgCheck( "AttrWriteFile", args, buf, TYPE_OBJECT, TYPE_INT, TYPE_NOASN );
  336.         mode = FILE_OVERWRITE ;
  337.     }
  338.     else
  339.     {
  340.         ArgCheck( "AttrWriteFile", args, buf, TYPE_OBJECT, TYPE_INT, TYPE_INT, TYPE_NOASN );
  341.         mode = buf[2].id.i ;
  342.     }
  343.     fname = ((StringClass*)buf[0].od.ptr)->str;
  344.     atrno = buf[1].id.i;
  345.     StackPushInt( AttrWriteFile( fname, atrno, mode ) );
  346.     return RETURN_RETURN ;
  347. }
  348.  
  349. /*    パラメータを設定する。    */
  350. static    int        FuncAttrParameter( ident, args, buf )
  351. int        ident ;
  352. int        args ;
  353. DataStruct    *buf ;
  354. {
  355.     int pos;
  356.     Color *c;
  357.     VectorClass *v = NULL;
  358.     if (args <= 1) {
  359.         ArgCheck( "AttrParameter", args, buf, TYPE_INT,  TYPE_NOASN );
  360.     } else {
  361.         ArgCheck( "AttrParameter", args, buf, TYPE_INT, TYPE_OBJECT, TYPE_NOASN );
  362.         if (ObjectCheck(&buf[1], ClassName("Vector")) == FALSE) {
  363.             ExecError( "1番目の引数の型が不正です(AttrParameter)" );
  364.         }
  365.         v = (VectorClass*)buf[1].od.ptr;
  366.     }
  367.     pos = buf[0].id.i;
  368.     if (pos < 0 || pos > AttrLast) {
  369.         return RETURN_VOID;
  370.     }
  371.  
  372.     if (ident == ColID)         c = &AttrData[pos].col;
  373.     else if (ident == AmbID)     c = &AttrData[pos].amb;
  374.     else if (ident == DifID)    c = &AttrData[pos].dif;
  375.     else if (ident == SpcID)    c = &AttrData[pos].spc;
  376.     else if (ident == SizeID)    c = &AttrData[pos].size;
  377.     else if (ident == RefID)    c = &AttrData[pos].ref;
  378.     else if (ident == TraID)    c = &AttrData[pos].tra;
  379.     else if (ident == RfrID)    c = &AttrData[pos].rfr;
  380.     else if (ident == AttID)    c = &AttrData[pos].att;
  381.     else assert(("Unknown Identifier", FALSE));
  382.     if (args == 1) {
  383.         v = (VectorClass*)ObjectAlloc( sizeof( VectorClass ), VectorClassID );
  384.         v->vec.x = (double)c->r / (double)(1 << COLOR_SHIFT);
  385.         v->vec.y = (double)c->g / (double)(1 << COLOR_SHIFT);
  386.         v->vec.z = (double)c->b / (double)(1 << COLOR_SHIFT);
  387.         buf = StackAlloc( 1 );
  388.         buf->type = TYPE_OBJECT ;
  389.         buf->od.ptr = (Object*)v ;
  390.         return RETURN_RETURN;
  391.     } else {
  392.         c->r = (int)(v->vec.x * (double)(1 << COLOR_SHIFT));
  393.         c->g = (int)(v->vec.y * (double)(1 << COLOR_SHIFT));
  394.         c->b = (int)(v->vec.z * (double)(1 << COLOR_SHIFT));
  395.         return RETURN_VOID;
  396.     }
  397. }
  398.  
  399.  
  400. /*    マッピングを設定する    */
  401. static    int        FuncAttrMapType( ident, args, buf )
  402. int        ident ;
  403. int        args ;
  404. DataStruct    *buf ;
  405. {
  406.     int pos;
  407.     if (args <= 1) {
  408.         ArgCheck( "AttrMap", args, buf, TYPE_INT,  TYPE_NOASN );
  409.         pos = buf[0].id.i;
  410.         if (pos < 0 || pos > AttrLast) {
  411.             return RETURN_VOID;
  412.         }
  413.         StackPushBoolean(AttrData[pos].maptype == MAP_NO ? FALSE : TRUE);
  414.         return RETURN_RETURN;
  415.     } else {
  416.         ArgCheck( "AttrMap", args, buf, TYPE_INT, TYPE_INT | TYPE_BOOLEAN, TYPE_NOASN );
  417.         pos = buf[0].id.i;
  418.         if (pos < 0 || pos > AttrLast) {
  419.             return RETURN_VOID;
  420.         }
  421.         AttrData[pos].maptype = buf[1].id.i ? MAP_COLOR : MAP_NO;
  422.         return RETURN_VOID;
  423.     }
  424. }
  425.  
  426. /*    マッピングを設定する    */
  427. static    int        FuncAttrMapFile( ident, args, buf )
  428. int        ident ;
  429. int        args ;
  430. DataStruct    *buf ;
  431. {
  432.     int        n ;
  433.  
  434.     if ( args < 1 )
  435.         ExecError( "引数の数が不正です(AttrName)" );
  436.     if ( buf[0].type != TYPE_INT )
  437.         ExecError( "%d番目の引数の型不正です(AttrName)", 1 );
  438.     n = buf[0].id.i ;
  439.     if (n < 0 || n > AttrLast) {
  440.         return RETURN_VOID;
  441.     }
  442.     if ( args >= 2 )
  443.     {
  444.         if ( buf[1].type != TYPE_OBJECT || ObjectCheck( &buf[1], StringClassID ) == FALSE )
  445.             ExecError( "%d番目の引数の型不正です(AttrName)", 2 );
  446.         strcpy( AttrData[n].colorfile, ((StringClass*)buf[1].od.ptr)->str );
  447.     }
  448.  
  449.     buf = StackAlloc( 1 );
  450.     StringToObject( buf, AttrData[n].colorfile );
  451.     return RETURN_RETURN ;
  452. }
  453.  
  454. /*    パラメータを設定する。    */
  455. static    int        FuncAttrMap( ident, args, buf )
  456. int        ident ;
  457. int        args ;
  458. DataStruct    *buf ;
  459. {
  460.     int pos;
  461.     int (*map)[2];
  462.     DataStruct *buf1, *buf2;
  463.     extern    double    ToDouble( DataStruct* );
  464.  
  465.     ArgCheck( "AttrMapView", args, buf, TYPE_INT, TYPE_ARRAY, TYPE_ARRAY, TYPE_NOASN );
  466.     if ( buf[1].ad.size < 2  || buf[2].ad.size < 2)
  467.         ExecError( "引数配列のサイズが不正です。(AttrMapView)" );
  468.     pos = buf[0].id.i;
  469.     buf1 = buf[1].ad.ary;
  470.     buf2 = buf[2].ad.ary;
  471.  
  472.     if (pos < 0 || pos > AttrLast) {
  473.         return RETURN_VOID;
  474.     }
  475.  
  476.     if (ident == GetSizeID || ident == SetSizeID)         map = AttrData[pos].mapsize;
  477.     else if (ident == GetViewID || ident == SetViewID)     map = AttrData[pos].mapview;
  478.     else if (ident == GetWindID || ident == SetWindID)    map = AttrData[pos].mapwind;
  479.     else assert(("Unknown Identifier", FALSE));
  480.  
  481.     if (ident == SetSizeID || ident == SetViewID || ident == SetWindID) {
  482.         if ((buf1[0].type & (TYPE_INT | TYPE_REAL)) == 0
  483.          || (buf1[1].type & (TYPE_INT | TYPE_REAL)) == 0
  484.          || (buf2[0].type & (TYPE_INT | TYPE_REAL)) == 0
  485.          || (buf2[1].type & (TYPE_INT | TYPE_REAL)) == 0) {
  486.             ExecError( "引数配列の型が不正です。( GetMapView )" );
  487.         }
  488.         map[0][0] = (int)(ToDouble(&buf1[0]) * 65536.0);
  489.         map[0][1] = (int)(ToDouble(&buf1[1]) * 65536.0);
  490.         map[1][0] = (int)(ToDouble(&buf2[0]) * 65536.0);
  491.         map[1][1] = (int)(ToDouble(&buf2[1]) * 65536.0);
  492.     } else {
  493.         if (buf1[0].type == TYPE_OBJECT) ObjectFree(buf1[0].od.ptr);
  494.         if (buf1[1].type == TYPE_OBJECT) ObjectFree(buf1[1].od.ptr);
  495.         if (buf2[0].type == TYPE_OBJECT) ObjectFree(buf2[0].od.ptr);
  496.         if (buf2[1].type == TYPE_OBJECT) ObjectFree(buf2[1].od.ptr);
  497.         buf1[0].type = buf1[1].type = buf2[0].type = buf2[1].type = TYPE_REAL;
  498.         buf1[0].rd.r = (double)map[0][0] / 65536.0;
  499.         buf1[1].rd.r = (double)map[0][1] / 65536.0;
  500.         buf2[0].rd.r = (double)map[1][0] / 65536.0;
  501.         buf2[1].rd.r = (double)map[1][1] / 65536.0;
  502.     }
  503.     return RETURN_VOID;
  504. }
  505.  
  506.  
  507.