Ticket #2978: m3d-fix.diff

File m3d-fix.diff, 15.8 KB (added by james.goppert@…, 10 years ago)

m3d patch to fix state record

  • package.mo

    old new  
    1111  constant String OBJECT = "/de/tuberlin/uebb/modelica3d/server";
    1212  constant String INTERFACE = "de.tuberlin.uebb.modelica3d.api";
    1313
    14 
    15   record State
    16     Connection conn;
    17     Context context;
    18   end State;
    19 
    20 
    2114  class Controller
    22     discrete State state(conn=Connection("de.tuberlin.uebb.modelica3d.client"), context=Context());
     15    discrete Connection conn = Connection("de.tuberlin.uebb.modelica3d.client");
     16    discrete Context context = Context();
    2317
    2418    parameter Integer framerate = 30;
    2519    parameter Modelica.SIunits.Time updateInterval = 1 / framerate;
     
    3226
    3327  algorithm
    3428    when terminal() then
    35       if autostop then stop(state); end if;
     29      if autostop then stop(conn, context); end if;
    3630    end when;
    3731  end Controller;
    3832
     
    4337
    4438
    4539  function createMaterial
    46     input State state;
     40    input Connection conn;
     41    input Context context;
    4742    input Id id;
    4843  protected
    4944    Message msg = Message(TARGET, OBJECT, INTERFACE, "make_material");
    5045  algorithm
    51     setString(id,"material_" + String(modcount.increase_get(state.context)));
     46    setString(id,"material_" + String(modcount.increase_get(context)));
    5247    addString(msg, "reference", getString(id));
    53     sendMessage(state.conn, msg);
     48    sendMessage(conn, msg);
    5449  end createMaterial;
    5550
    5651
    5752  function applyMaterial
    58     input State state;
     53    input Connection conn;
     54    input Context context;
    5955    input Id obj;
    6056    input Id mat;
    6157    output String res;
     
    6460  algorithm
    6561    addString(msg, "reference", getString(obj));
    6662    addString(msg, "material", getString(mat));
    67     res := sendMessage(state.conn, msg);
     63    res := sendMessage(conn, msg);
    6864  end applyMaterial;
    6965
    7066
    7167  function createBox
    72     input State state;
     68    input Connection conn;
     69    input Context context;
    7370    input Real length, width, height;
    7471    input Id id;
    7572  protected
    7673    Message msg = Message(TARGET, OBJECT, INTERFACE, "make_box");
    7774  algorithm
    78     setString(id,HeapString("box_" + String(modcount.increase_get(state.context))));
     75    setString(id,HeapString("box_" + String(modcount.increase_get(context))));
    7976    addString(msg, "reference", getString(id));
    8077    addReal(msg, "length", length);
    8178    addReal(msg, "width", width);
    8279    addReal(msg, "height", height);
    83     sendMessage(state.conn, msg);
     80    sendMessage(conn, msg);
    8481  end createBox;
    8582
    8683
    8784  function createBoxAt
    88     input State state;
     85    input Connection conn;
     86    input Context context;
    8987    input Real length, width, height;
    9088    input Real tx,ty,tz;
    9189    input Id id;
    9290  protected
    9391    Message msg = Message(TARGET, OBJECT, INTERFACE, "make_box");
    9492  algorithm
    95     setString(id,"box_" + String(modcount.increase_get(state.context)));
     93    setString(id,"box_" + String(modcount.increase_get(context)));
    9694    addString(msg, "reference", getString(id));
    9795    addReal(msg, "length", length);
    9896    addReal(msg, "width", width);
     
    10098    addReal(msg, "tx", tx);
    10199    addReal(msg, "ty", ty);
    102100    addReal(msg, "tz", tz);
    103     sendMessage(state.conn, msg);
     101    sendMessage(conn, msg);
    104102  end createBoxAt;
    105103
    106104  function loadFromFile
    107     input State state;
     105    input Connection conn;
     106    input Context context;
    108107    input String fileName;
    109108    input Real tx,ty,tz;
    110109    input Id id;
    111110  protected
    112111    Message msg = Message(TARGET, OBJECT, INTERFACE, "loadFromFile");
    113112  algorithm
    114     setString(id,"file_" + String(modcount.increase_get(state.context)));
     113    setString(id,"file_" + String(modcount.increase_get(context)));
    115114    addString(msg, "reference", getString(id));
    116115    addString(msg, "fileName", fileName);
    117116    addReal(msg, "tx", tx);
    118117    addReal(msg, "ty", ty);
    119118    addReal(msg, "tz", tz);
    120     sendMessage(state.conn, msg);
     119    sendMessage(conn, msg);
    121120  end loadFromFile;
    122121
    123122
    124123  function createSphere
    125     input State state;
     124    input Connection conn;
     125    input Context context;
    126126    input Real size;
    127127    input Id id;
    128128  protected
    129129    Message msg = Message(TARGET, OBJECT, INTERFACE, "make_sphere");
    130130  algorithm
    131     setString(id,"sphere_" + String(modcount.increase_get(state.context)));
     131    setString(id,"sphere_" + String(modcount.increase_get(context)));
    132132    addString(msg, "reference", getString(id));
    133133    addReal(msg, "size", size);
    134     sendMessage(state.conn, msg);
     134    sendMessage(conn, msg);
    135135  end createSphere;
    136136
    137137
    138138  function createCylinder
    139     input State state;
     139    input Connection conn;
     140    input Context context;
    140141    input Real height;
    141142    input Real diameter;
    142143    input Id id;
    143144  protected
    144145    Message msg = Message(TARGET, OBJECT, INTERFACE, "make_cylinder");
    145146  algorithm
    146     setString(id,"cylinder_" + String(modcount.increase_get(state.context)));
     147    setString(id,"cylinder_" + String(modcount.increase_get(context)));
    147148    addString(msg, "reference", getString(id));
    148149    addReal(msg, "height", height);
    149150    addReal(msg, "diameter", diameter);
    150     sendMessage(state.conn, msg);
     151    sendMessage(conn, msg);
    151152  end createCylinder;
    152153
    153154
    154155  function createCylinderAt
    155     input State state;
     156    input Connection conn;
     157    input Context context;
    156158    input Real height;
    157159    input Real diameter;
    158160    input Real x,y,z;
     
    160162  protected
    161163    Message msg = Message(TARGET, OBJECT, INTERFACE, "make_cylinder");
    162164  algorithm
    163     setString(id,"cylinder_" + String(modcount.increase_get(state.context)));
     165    setString(id,"cylinder_" + String(modcount.increase_get(context)));
    164166    addString(msg, "reference", getString(id));
    165167    addReal(msg, "height", height);
    166168    addReal(msg, "diameter", diameter);
    167169    addReal(msg, "x", x);
    168170    addReal(msg, "y", y);
    169171    addReal(msg, "z", z);
    170     sendMessage(state.conn, msg);
     172    sendMessage(conn, msg);
    171173  end createCylinderAt;
    172174
    173175
    174176  function createCone
    175     input State state;
     177    input Connection conn;
     178    input Context context;
    176179    input Real height;
    177180    input Real diameter;
    178181    input Id id;
    179182  protected
    180183    Message msg = Message(TARGET, OBJECT, INTERFACE, "make_cone");
    181184  algorithm
    182     setString(id,"cone_" + String(modcount.increase_get(state.context)));
     185    setString(id,"cone_" + String(modcount.increase_get(context)));
    183186    addString(msg, "reference", getString(id));
    184187    addReal(msg, "height", height);
    185188    addReal(msg, "diameter", diameter);
    186     sendMessage(state.conn, msg);
     189    sendMessage(conn, msg);
    187190  end createCone;
    188191
    189192
    190193  function createConeAt
    191     input State state;
     194    input Connection conn;
     195    input Context context;
    192196    input Real height;
    193197    input Real diameter;
    194198    input Real x,y,z;
     
    196200  protected
    197201    Message msg = Message(TARGET, OBJECT, INTERFACE, "make_cone");
    198202  algorithm
    199     setString(id,"cone_" + String(modcount.increase_get(state.context)));
     203    setString(id,"cone_" + String(modcount.increase_get(context)));
    200204    addString(msg, "reference", getString(id));
    201205    addReal(msg, "height", height);
    202206    addReal(msg, "diameter", diameter);
    203207    addReal(msg, "x", x);
    204208    addReal(msg, "y", y);
    205209    addReal(msg, "z", z);
    206     sendMessage(state.conn, msg);
     210    sendMessage(conn, msg);
    207211  end createConeAt;
    208212
    209213
    210214  function moveTo
    211     input State state;
     215    input Connection conn;
     216    input Context context;
    212217    input Id id;
    213218    input Real p[3];
    214219    input Real t;
     
    221226    addReal(msg, "y", p[2]);
    222227    addReal(msg, "z", p[3]);
    223228    addReal(msg, "t", t);
    224     r := sendMessage(state.conn, msg);
     229    r := sendMessage(conn, msg);
    225230  end moveTo;
    226231
    227232
    228233  function moveZ
    229     input State state;
     234    input Connection conn;
     235    input Context context;
    230236    input Id id;
    231237    input Real z;
    232238    input Real t;
     
    237243    addString(msg, "reference", getString(id));
    238244    addReal(msg, "z", z);
    239245    addReal(msg, "t", t);
    240     r := sendMessage(state.conn, msg);
     246    r := sendMessage(conn, msg);
    241247  end moveZ;
    242248
    243249
    244250  function scale
    245     input State state;
     251    input Connection conn;
     252    input Context context;
    246253    input Id id;
    247254    input Real x,y,z;
    248255    input Real t;
     
    255262    addReal(msg, "y", y);
    256263    addReal(msg, "z", z);
    257264    addReal(msg, "t", t);
    258     r := sendMessage(state.conn, msg);
     265    r := sendMessage(conn, msg);
    259266  end scale;
    260267
    261268
    262269  function scaleZ
    263     input State state;
     270    input Connection conn;
     271    input Context context;
    264272    input Id id;
    265273    input Real z;
    266274    input Real t;
     
    271279    addString(msg, "reference", getString(id));
    272280    addReal(msg, "z", z);
    273281    addReal(msg, "t", t);
    274     r := sendMessage(state.conn, msg);
     282    r := sendMessage(conn, msg);
    275283  end scaleZ;
    276284
    277285
    278286  function setAmbientColor
    279     input State state;
     287    input Connection conn;
     288    input Context context;
    280289    input Id id;
    281290    input Real r,g,b,a;
    282291    input Real t;
     
    290299    addReal(msg, "b", b);
    291300    addReal(msg, "a", a);
    292301    addReal(msg, "t", t);
    293     res := sendMessage(state.conn, msg);
     302    res := sendMessage(conn, msg);
    294303  end setAmbientColor;
    295304
    296305
    297306  function setDiffuseColor
    298     input State state;
     307    input Connection conn;
     308    input Context context;
    299309    input Id id;
    300310    input Real r,g,b,a;
    301311    input Real t;
     
    309319    addReal(msg, "b", b);
    310320    addReal(msg, "a", a);
    311321    addReal(msg, "t", t);
    312     res := sendMessage(state.conn, msg);
     322    res := sendMessage(conn, msg);
    313323  end setDiffuseColor;
    314324
    315325
    316326  function setSpecularColor
    317     input State state;
     327    input Connection conn;
     328    input Context context;
    318329    input Id id;
    319330    input Real r,g,b,a;
    320331    input Real t;
     
    328339    addReal(msg, "b", b);
    329340    addReal(msg, "a", a);
    330341    addReal(msg, "t", t);
    331     res := sendMessage(state.conn, msg);
     342    res := sendMessage(conn, msg);
    332343  end setSpecularColor;
    333344
    334345
    335346  function setMatProperty
    336     input State state;
     347    input Connection conn;
     348    input Context context;
    337349    input Id id;
    338350    input String property;
    339351    input Real value;
     
    346358    addString(msg, "prop", property);
    347359    addReal(msg, "value", value);
    348360    addReal(msg, "t", t);
    349     res := sendMessage(state.conn, msg);
     361    res := sendMessage(conn, msg);
    350362  end setMatProperty;
    351363
    352364
    353365  function loadSceneFromFile
    354     input State state;
     366    input Connection conn;
     367    input Context context;
    355368    input String fileName;
    356369    output String res;
    357370  protected
    358371    Message msg = Message(TARGET, OBJECT, INTERFACE, "load_scene");
    359372  algorithm
    360373    addString(msg, "filepath", fileName);
    361     res := sendMessage(state.conn, msg);
     374    res := sendMessage(conn, msg);
    362375  end loadSceneFromFile;
    363376
    364377
    365378  function rotate
    366     input State state;
     379    input Connection conn;
     380    input Context context;
    367381    input Id id;
    368382    input Real[3,3] R;
    369383    input Real t;
     
    381395    end for;
    382396
    383397    addReal(msg, "t", t);
    384     r := sendMessage(state.conn, msg);
     398    r := sendMessage(conn, msg);
    385399  end rotate;
    386400
    387401
    388402  function stop
    389     input State state;
     403    input Connection conn;
     404    input Context context;
    390405    output String r;
    391406  protected
    392407    Message msg = Message(TARGET, OBJECT, INTERFACE, "stop");
    393408  algorithm
    394     r := sendMessage(state.conn, msg);
     409    r := sendMessage(conn, msg);
    395410  end stop;
    396411
    397412
     
    406421    import Modelica.Mechanics.MultiBody.Frames;
    407422
    408423    function shapeDescrTo3D
    409       input State state;
     424      input Connection conn;
     425      input Context context;
    410426      input String descr;
    411427      input Real length, width, height;
    412428      input Real[3] at;
    413429      input Real extra;
    414430      input Id id;
    415431    algorithm
     432
    416433      if (descr == "box") then
    417         createBoxAt(state, width, height, length, at[1], at[2], at[3], id);
     434        createBoxAt(conn, context, width, height, length, at[1], at[2], at[3], id);
    418435      elseif (descr == "cone") then
    419         createConeAt(state, length, width, at[1], at[2], at[3], id);
     436        createConeAt(conn, context, length, width, at[1], at[2], at[3], id);
    420437      elseif (descr == "sphere") then
    421         createSphere(state, length, id);
     438        createSphere(conn, context, length, id);
    422439      elseif (descr == "cylinder") then
    423         createCylinderAt(state, length, width, at[1], at[2], at[3], id);
     440        createCylinderAt(conn, context, length, width, at[1], at[2], at[3], id);
    424441      elseif (descr == "pipecylinder") then
    425442        if (Modelica.Math.isEqual(extra, 0.0)) then
    426           createCylinderAt(state, length, width, at[1], at[2], at[3], id);
     443          createCylinderAt(conn, context, length, width, at[1], at[2], at[3], id);
    427444        else
    428445          // not yet supported
    429446          Modelica.Utilities.Streams.print("Error: Visualization of pipecylinders has not been implemented yet!");
     
    431448        end if;
    432449      elseif (descr == "pipe") then
    433450        if (Modelica.Math.isEqual(extra, 0.0)) then
    434           createCylinderAt(state, length, width, at[1], at[2], at[3], id);
     451          createCylinderAt(conn, context, length, width, at[1], at[2], at[3], id);
    435452        else
    436453          // not yet supported
    437454          Modelica.Utilities.Streams.print("Error: Visualization of pipes has not been implemented yet!");
     
    451468        setString(id,"UNKNOWN");
    452469      else
    453470        // assume it is a filename
    454         loadFromFile(state, Modelica.Utilities.Files.fullPathName(ModelicaServices.ExternalReferences.loadResource(descr)), at[1], at[2], at[3], id);
     471        loadFromFile(conn, context, Modelica.Utilities.Files.fullPathName(ModelicaServices.ExternalReferences.loadResource(descr)), at[1], at[2], at[3], id);
    455472      end if;
    456473
    457474    end shapeDescrTo3D;
     
    471488  //initial equation
    472489  //  pre(oldT) = R.T;
    473490  //  pre(pos) = r + Frames.resolve1(R, r_shape);
    474   initial algorithm
    475     if modcount.get(initContext) <> 1 then
    476       shapeDescrTo3D(m3d_control.state, shapeType, length, width, height, lengthDirection, extra, id);
    477       createMaterial(m3d_control.state, mat);
    478       setAmbientColor(m3d_control.state, mat, color[1] / 255, color[2] / 255, color[3] / 255, 1.0, time);
    479       setSpecularColor(m3d_control.state, mat, specularCoefficient * (color[1] / 255),
    480                        specularCoefficient * color[2] / 255, specularCoefficient * color[3] / 255, 1.0, time);
    481 
    482       applyMaterial(m3d_control.state, id, mat);
    483       modcount.set(initContext, 1);
    484     end if;
    485491  algorithm
    486492    when m3d_control.send and modcount.get(initContext) == 1 then
    487493    // check for rotation
    488494      rotated := not Modelica.Math.Matrices.isEqual(R.T, oldT);
    489495      if noEvent(rotated) then
    490         res := rotate(m3d_control.state, id, R.T, time);
     496        res := rotate(m3d_control.conn, m3d_control.context, id, R.T, time);
    491497      end if;
    492498      oldT := R.T;
    493499
     
    495501      pos := r + Frames.resolve1(R, r_shape);
    496502      moved := not Modelica.Math.Vectors.isEqual(pos, pre(pos));
    497503      if noEvent(moved) then
    498         res := moveTo(m3d_control.state, id, pos, time);
     504        res := moveTo(m3d_control.conn, m3d_control.context, id, pos, time);
    499505      end if;
    500506
    501507    end when;
    502508
     509    if modcount.get(initContext) <> 1 then
     510     
     511      Modelica.Utilities.Streams.print( "SHAPE " + getString(id) + " " + shapeType);
     512      Modelica.Utilities.Streams.print( "length = " + String(length));
     513      Modelica.Utilities.Streams.print( "width = " + String(width));
     514      Modelica.Utilities.Streams.print( "height = " + String(height));
     515
     516     
     517      shapeDescrTo3D(m3d_control.conn, m3d_control.context, shapeType, length, width, height, lengthDirection, extra, id);
     518      createMaterial(m3d_control.conn, m3d_control.context, mat);
     519      setAmbientColor(m3d_control.conn, m3d_control.context, mat, color[1] / 255, color[2] / 255, color[3] / 255, 1.0, time);
     520      setSpecularColor(m3d_control.conn, m3d_control.context, mat, specularCoefficient * (color[1] / 255),
     521                       specularCoefficient * color[2] / 255, specularCoefficient * color[3] / 255, 1.0, time);
     522
     523      applyMaterial(m3d_control.conn, m3d_control.context, id, mat);
     524      modcount.set(initContext, 1);
     525    end if;
     526
     527   
     528   
     529
    503530    annotation (
    504531      Icon(coordinateSystem(
    505532          preserveAspectRatio=true,