home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / irit60e2.zip / iritinit.irt < prev    next >
Text File  |  1996-02-28  |  12KB  |  365 lines

  1. iritState("EchoSource", false);
  2. #
  3. # Init file for IRIT solid modeller.
  4. #
  5. iritstate( "FloatFrmt", "%8.6lg" );
  6. iritstate( "DumpLevel", 9 );
  7.  
  8. demo = procedure():
  9.     include("demo.irt");
  10.  
  11. pause = procedure():t:
  12.     printf("Press return to continue:", nil()):
  13.     t = getline(string_type);
  14.  
  15. #
  16. # Some simple functions.
  17. #
  18. min = function(x, y):
  19.     if (x > y, return = y, return = x);
  20. max = function(x, y):
  21.     if (x > y, return = x, return = y);
  22. sqr = function(x):
  23.     return = x * x;
  24. normalize = function(x):len:
  25.     return = 1.0:
  26.     if (thisobj("x") == vector_type || thisobj("x") == point_type,
  27.     len = sqrt(x * x):
  28.     return = point(coord(x, 0) / len,
  29.                coord(x, 1) / len,
  30.                coord(x, 2) / len)):
  31.     if (thisobj("return") == numeric_type,
  32.     printf("NORMALIZE: Can handle only vectors or points, found %8.6lDg\n",
  33.            list(x)));
  34. midPoint = function(pt1, pt2):
  35.     return = (pt1 + pt2) * 0.5;
  36. InterpPoint = function(pt1, pt2, t):
  37.     return = (pt1 * (1 - t) + pt2 * t);
  38. edge2d = function( x1, y1, x2, y2 ):
  39.     return = ctlpt( E2, x1, y1 ) + ctlpt( E2, x2, y2 );
  40. edge3d = function( x1, y1, z1, x2, y2, z2 ):
  41.     return = ctlpt( E3, x1, y1, z1 ) + ctlpt( E3, x2, y2, z2 );
  42.  
  43. #
  44. # Setting a fixed normal to polygonal object.
  45. #
  46. SetNormalsInPoly = function( Pl, Normal ):V:i:
  47.    return = nil():
  48.    for ( i = 0, 1, sizeof( Pl ) - 1,
  49.        V = coord( Pl, i ):
  50.        attrib( V, "normal", Normal ):
  51.        snoc( V, return ) ):
  52.    return = poly( return, false );
  53. SetNormalsInPolyObj = function( Obj, Normal ):Pl:i:
  54.    return = SetNormalsInPoly( coord( Obj, 0 ), Normal ):
  55.    for ( i = 1, 1, sizeof( Obj ) - 1,
  56.        Pl = SetNormalsInPoly( coord( Obj, i ), Normal ):
  57.        return = return ^ Pl );
  58. SetNormalsInObjList = function( ObjList, Normal ):
  59.    return = nil();
  60. SetNormalsInObjList = function( ObjList, Normal ):Obj:i:
  61.    return = nil():
  62.    for ( i = 1, 1, sizeof( ObjList ),
  63.        Obj = nth( ObjList, i ):
  64.        if ( thisobj( "Obj" ) == list_type ,
  65.         snoc( SetNormalsInObjList( Obj, Normal ), return ),
  66.         if ( thisobj( "Obj" ) == poly_type,
  67.          snoc( SetNormalsInPolyObj( Obj, Normal ), return ),
  68.          snoc( Obj, return ) ) ) );
  69.  
  70. #
  71. # Sweep of circular cross section.
  72. #
  73. SwpCircSrf = function( AxisCrv, ScaleCrv, ScaleRefine ):
  74.     return = SwpSclSrf( circle( vector( 0, 0, 0 ), 1 ),
  75.             AxisCrv,
  76.             ScaleCrv,
  77.             off,
  78.             ScaleRefine );
  79.  
  80. #
  81. # Extractions of Control Polygon/Mesh/Points from a curve or a surface.
  82. #
  83. GetCtlPoints = function( Crv, Vecs ):i:p:
  84.     return = nil():
  85.     for ( i = 1, 1, sizeof( Crv ),
  86.     if ( Vecs,
  87.          p = coerce( coord( Crv, i - 1 ), vector_type ),
  88.          p = coerce( coord( Crv, i - 1 ), point_type ) ):
  89.     snoc( p, return )
  90.     );
  91. GetCtlPolygon = function( Crv ):
  92.     return = poly( GetCtlPoints( Crv, false ), true );
  93.  
  94. GetCtlMeshPts = function( Srf, Vecs ):l:i:j:p:rsize:csize:
  95.     return = nil():
  96.     rsize = meshsize( Srf, ROW ):
  97.     csize = meshsize( Srf, COL ):
  98.     for ( i = 1, 1, csize,
  99.         for ( j = 1, 1, rsize,
  100.          if ( Vecs,
  101.               p = coerce( coord( Srf, (i - 1) * rsize + j - 1 ),
  102.                   vector_type ),
  103.               p = coerce( coord( Srf, (i - 1) * rsize + j - 1 ),
  104.                   point_type ) ):
  105.          snoc( p, return )
  106.     )
  107.     );
  108. GetCtlMesh = function( Srf ):l:i:j:p:pl:rsize:csize:first:
  109.     first = true:
  110.     rsize = meshsize( Srf, ROW ):
  111.     csize = meshsize( Srf, COL ):
  112.     for ( i = 1, 1, rsize,
  113.     pl = nil():
  114.         for ( j = 1, 1, csize,
  115.          p = coerce( coord( Srf, (i - 1) * csize + j - 1 ), vector_type ):
  116.          snoc( p, pl )
  117.     ):
  118.     if ( first == true,
  119.          return = poly( pl, true ): first = false,
  120.          return = return + poly( pl, true ) )
  121.     ):
  122.     for ( j = 1, 1, csize,
  123.     pl = nil():
  124.         for ( i = 1, 1, rsize,
  125.          p = coerce( coord( Srf, (i - 1) * csize + j - 1 ), vector_type ):
  126.          snoc( p, pl )
  127.     ):
  128.     return = return + poly( pl, true )
  129.     );
  130.  
  131. #
  132. # Extract a network of isocurves.
  133. #
  134. GetIsoCurves = function( Srf, NumU, NumV ):domain:Umin:Umax:Vmin:Vmax:i:
  135.     return = nil():
  136.     domain = pdomain( Srf ):
  137.     Umin = nth( domain, 1 ):
  138.     Umax = nth( domain, 2 ):
  139.     Vmin = nth( domain, 3 ):
  140.     Vmax = nth( domain, 4 ):
  141.     for ( i = 0, 1, NumU,
  142.     snoc( csurface( Srf, COL, Umin + (Umax - Umin) * i / NumU ),
  143.           return ) ):
  144.     for ( i = 0, 1, NumV,
  145.     snoc( csurface( Srf, ROW, Vmin + (Vmax - Vmin) * i / NumV ),
  146.           return ) );
  147.  
  148. GetIsoCurveTubes = function( Srf, NumU, NumV, Rad ):domain:Umin:Umax:Vmin:Vmax:i:
  149.     return = nil():
  150.     domain = pdomain( Srf ):
  151.     Umin = nth( domain, 1 ):
  152.     Umax = nth( domain, 2 ):
  153.     Vmin = nth( domain, 3 ):
  154.     Vmax = nth( domain, 4 ):
  155.     for ( i = 0, 1, NumU,
  156.     snoc( SwpCircSrf( csurface( Srf, COL,
  157.                     Umin + (Umax - Umin) * i / NumU ),
  158.               Rad, 1 ),
  159.           return ) ):
  160.     for ( i = 0, 1, NumV,
  161.     snoc( SwpCircSrf( csurface( Srf, ROW,
  162.                     Vmin + (Vmax - Vmin) * i / NumV ),
  163.               Rad, 1 ),
  164.           return ) );
  165.  
  166. #
  167. # Approximate a (assumed to be) closed planar curve as a polygon
  168. # with n vertices.
  169. #
  170. CnvrtCrvToPolygon = function( Crv, n, IsPolyline ):ptl:pt:lastPt:t:t0:t1:dt:
  171.     ptl = nil():
  172.     t0 = nth( pdomain( Crv ), 1 ):
  173.     t1 = nth( pdomain( Crv ), 2 ):
  174.     if ( n < 2, n = 2 ):
  175.     dt = (t1 - t0) / (n - 1):
  176.     if ( IsPolyline == 0, t1 = t1 - dt ):
  177.     for ( t = t0, dt, t1 + dt / 2,
  178.     pt = ceval( Crv, t ):
  179.     snoc( pt, ptl )
  180.     ):
  181.     return = poly( ptl, IsPolyline );
  182.  
  183. #
  184. # Primitives in freeform surface form.
  185. #
  186. planeSrf = function( x1, y1, x2, y2 ):
  187.     return = ruledSrf( edge2d( x1, y1, x2, y1 ),
  188.                edge2d( x1, y2, x2, y2 ) );
  189. sphereSrf = function( Radius ):s45:
  190.     s45 = sin( 45 * pi / 180 ):
  191.     return = surfRev( cbspline( 3,
  192.                 list( ctlpt( P3, 1.0,  0.0, 0.0, -1.0 ),
  193.                       ctlpt( P3, s45, -s45, 0.0, -s45 ),
  194.                       ctlpt( P3, 1.0, -1.0, 0.0,  0.0 ),
  195.                       ctlpt( P3, s45, -s45, 0.0,  s45 ),
  196.                       ctlpt( P3, 1.0,  0.0, 0.0,  1.0 ) ),
  197.                 list( 0, 0, 0, 1, 1, 2, 2, 2 ) ) ) *
  198.     scale( vector( Radius, Radius, Radius ) );
  199. torusSrf = function( MRadius, mRad ):
  200.     return = SwpCircSrf( circle( vector( 0, 0, 0 ), MRadius ), mRad, 0 );
  201. coneSrf = function( Height, Radius ):
  202.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  203.               ctlpt( E3, Radius, 0.0, 0.0 ) +
  204.               ctlpt( E3, 0.0, 0.0, Height ) );
  205. cone2Srf = function( Height, Radius1, Radius2 ):
  206.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  207.               ctlpt( E3, Radius1, 0.0, 0.0 ) +
  208.               ctlpt( E3, Radius2, 0.0, Height ) +
  209.               ctlpt( E3, 0.0, 0.0, Height ) );
  210. cylinSrf = function( Height, Radius ):
  211.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  212.               ctlpt( E3, Radius, 0.0, 0.0 ) +
  213.               ctlpt( E3, Radius, 0.0, Height ) +
  214.               ctlpt( E3, 0.0, 0.0, Height ) );
  215. boxSrf = function( Width, Dpth, Height ):
  216.     return = list( coerce( planeSrf( 0, 0, Width, Dpth ), e3 ),
  217.            coerce( planeSrf( 0, 0, Width, Dpth ), e3 ) *
  218.                trans( vector( 0, 0, Height ) ),
  219.            coerce( planeSrf( 0, 0, Width, Height ), e3 ) *
  220.                rotx( 90 ),
  221.            coerce( planeSrf( 0, 0, Width, Height ), e3 ) *
  222.                rotx( 90 ) * trans( vector( 0, Dpth, 0 ) ),
  223.            coerce( planeSrf( 0, 0, Height, Dpth ), e3 ) *
  224.                roty( -90 ),
  225.            coerce( planeSrf( 0, 0, Height, Dpth ), e3 ) *
  226.                roty( -90 ) * trans( vector( Width, 0, 0 ) ) );
  227. flatSrf = function( UOrder, VOrder ):
  228.     return = sbezier( list( list( ctlpt( E3, -1.0, -1.0, 0.0 ),
  229.                   ctlpt( E3, -1.0,  1.0, 0.0 ) ),
  230.                 list( ctlpt( E3,  1.0, -1.0, 0.0 ),
  231.                   ctlpt( E3,  1.0,  1.0, 0.0 ) ) ) ):
  232.     return = sraise( sraise( return, row, UOrder ), col, VOrder );
  233.             
  234.  
  235. #
  236. # Transformations.
  237. #
  238. tx = function( r ):
  239.     return = trans( vector( r, 0, 0 ) );
  240. ty = function( r ):
  241.     return = trans( vector( 0, r, 0 ) );
  242. tz = function( r ):
  243.     return = trans( vector( 0, 0, r ) );
  244. sx = function( r ):
  245.     return = scale( vector( r, 1, 1 ) );
  246. sy = function( r ):
  247.     return = scale( vector( 1, r, 1 ) );
  248. sz = function( r ):
  249.     return = scale( vector( 1, 1, r ) );
  250. sc = function( s ):
  251.     return = scale( vector( s, s, s ) );
  252. rx = function( r ):
  253.     return = rotx( r );
  254. ry = function( r ):
  255.     return = roty( r );
  256. rz = function( r ):
  257.     return = rotz( r );
  258.  
  259. RotVec2Z = function( w ):u:v: # Rotation Trans. of w dir to Z axis.
  260.     if ( abs( coord( w, 0 ) ) > abs( coord( w, 1 ) ),
  261.      u = vector( 0, 1, 0 ),
  262.      u = vector( 1, 0, 0 ) ):
  263.     w = normalize( w ):
  264.     v = normalize( u ^ w ):
  265.     u = normalize( w ^ v ):
  266.     return =
  267.     homomat( list( list( coord( u, 0 ), coord( v, 0 ), coord( w, 0 ), 0 ),
  268.                list( coord( u, 1 ), coord( v, 1 ), coord( w, 1 ), 0 ),
  269.                list( coord( u, 2 ), coord( v, 2 ), coord( w, 2 ), 0 ),
  270.                list( 0, 0, 0, 1 ) ) );
  271. RotZ2Vec = function( w ): # Rotation Trans. of Z axis to W dir.
  272.     return = RotVec2Z( w )^-1;
  273.  
  274. #
  275. # Arrows.
  276. #
  277. arrow3d = function( Pt, Dir, Length, Width, HeadLength, HeadWidth ):
  278.     return = list( cylinSrf( Length - HeadLength, Width / 2 )
  279.             * RotZ2Vec( Dir )
  280.             * trans( Pt ),
  281.            coneSrf( HeadLength, HeadWidth )
  282.             * trans( vector( 0, 0, Length - HeadLength ) )
  283.             * RotZ2Vec( Dir )
  284.             * trans( Pt ) );
  285.  
  286. #
  287. # Emulation of view, interact and other useful viewing functions using VIEWOBJ
  288. # for the default display device.
  289. #
  290. beep = procedure():command_: # Make some noise.
  291.     command_ = "BEEP":
  292.     viewobj(command_);
  293. viewclear = procedure():command_: # Clear the screen.
  294.     command_ = "CLEAR":
  295.     viewobj(command_);
  296. viewdclear = procedure():command_: # Delayed clear screen.
  297.     command_ = "DCLEAR":
  298.     viewobj(command_);
  299. viewdisc = procedure():command_: # Disconnect from this display device
  300.     command_ = "DISCONNECT":
  301.     viewobj(command_);
  302. viewexit = procedure():command_: # Force Display device to exit.
  303.     command_ = "EXIT":
  304.     viewobj(command_);
  305. viewmsave = procedure(name):command_: # Save viewing matrix under name.
  306.     command_ = "MSAVE " + name:
  307.     viewobj(command_);
  308. viewremove = procedure(name):command_: # Remove an object from display.
  309.     command_ = "REMOVE " + name:
  310.     viewobj(command_);
  311. viewanim = procedure(TMin, TMax, Dt):command_: # Animate a sequence.
  312.     command_ = "ANIMATE " ^ TMin ^ " " ^ TMax ^ " " ^ Dt:
  313.     viewobj(command_);
  314. viewstate = procedure(state):command_: # Change state of display device.
  315.     command_ = "STATE " + state:
  316.     viewobj(command_);
  317. view = procedure(none,clear): # Emulation of old VIEW command.
  318.     if (clear != 0.0, viewdclear()):
  319.     viewobj(none);
  320. interact = procedure(none): # Emulation of old INTERACT command.
  321.     viewdclear():
  322.     viewobj(none):
  323.     pause();
  324.  
  325. #
  326. # Client communication helper functions.
  327. #
  328. clntclear = procedure(h):command_: # Clear the screen.
  329.     command_ = "CLEAR":
  330.     clntwrite(h, command_);
  331. clntdclear = procedure(h):command_: # Delayed clear screen.
  332.     command_ = "DCLEAR":
  333.     clntwrite(h, command_);
  334. clntdisc = procedure(h):command_: # Disconnect from this display device
  335.     command_ = "DISCONNECT":
  336.     clntwrite(h, command_);
  337. clntexit = procedure(h):command_: # Force Display device to exit.
  338.     command_ = "EXIT":
  339.     clntwrite(h, command_);
  340. clntgetobj = function(h, Name):command_: # Get an object from a client.
  341.     command_ = "GETOBJ " + Name:
  342.     clntwrite(h, command_):
  343.     return = clntread(h, 100);
  344. clntmsave = procedure(h, name):command_: # Save viewing matrix under name.
  345.     command_ = "MSAVE " + name:
  346.     clntwrite(h, command_);
  347. clntremove = procedure(h, name):command_: # Remove an object from display.
  348.     command_ = "REMOVE " + name:
  349.     clntwrite(h, command_);
  350. clntanim = procedure(h, TMin, TMax, Dt):command_: # Animate a sequence.
  351.     command_ = "ANIMATE " ^ TMin ^ " " ^ TMax ^ " " ^ Dt:
  352.     clntwrite(h, command_);
  353. clntstate = procedure(h, state):command_: # Change state of display device.
  354.     command_ = "STATE " + state:
  355.     clntwrite(h, command_);
  356. clntview = procedure(h, none, clear): # Emulation of old VIEW command.
  357.     if (clear != 0.0, clntclear(h)):
  358.     clntwrite(h, none);
  359. cntrintr = procedure(h, none): # Emulation of old INTERACT command.
  360.     clntclear(h):
  361.     clntwrite(h, none):
  362.     pause();
  363.  
  364. iritState("EchoSource", true);
  365.