Ticket #2978: m3d-fix.diff
File m3d-fix.diff, 15.8 KB (added by james.goppert@…, 10 years ago) |
---|
-
package.mo
old new 11 11 constant String OBJECT = "/de/tuberlin/uebb/modelica3d/server"; 12 12 constant String INTERFACE = "de.tuberlin.uebb.modelica3d.api"; 13 13 14 15 record State16 Connection conn;17 Context context;18 end State;19 20 21 14 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(); 23 17 24 18 parameter Integer framerate = 30; 25 19 parameter Modelica.SIunits.Time updateInterval = 1 / framerate; … … 32 26 33 27 algorithm 34 28 when terminal() then 35 if autostop then stop( state); end if;29 if autostop then stop(conn, context); end if; 36 30 end when; 37 31 end Controller; 38 32 … … 43 37 44 38 45 39 function createMaterial 46 input State state; 40 input Connection conn; 41 input Context context; 47 42 input Id id; 48 43 protected 49 44 Message msg = Message(TARGET, OBJECT, INTERFACE, "make_material"); 50 45 algorithm 51 setString(id,"material_" + String(modcount.increase_get( state.context)));46 setString(id,"material_" + String(modcount.increase_get(context))); 52 47 addString(msg, "reference", getString(id)); 53 sendMessage( state.conn, msg);48 sendMessage(conn, msg); 54 49 end createMaterial; 55 50 56 51 57 52 function applyMaterial 58 input State state; 53 input Connection conn; 54 input Context context; 59 55 input Id obj; 60 56 input Id mat; 61 57 output String res; … … 64 60 algorithm 65 61 addString(msg, "reference", getString(obj)); 66 62 addString(msg, "material", getString(mat)); 67 res := sendMessage( state.conn, msg);63 res := sendMessage(conn, msg); 68 64 end applyMaterial; 69 65 70 66 71 67 function createBox 72 input State state; 68 input Connection conn; 69 input Context context; 73 70 input Real length, width, height; 74 71 input Id id; 75 72 protected 76 73 Message msg = Message(TARGET, OBJECT, INTERFACE, "make_box"); 77 74 algorithm 78 setString(id,HeapString("box_" + String(modcount.increase_get( state.context))));75 setString(id,HeapString("box_" + String(modcount.increase_get(context)))); 79 76 addString(msg, "reference", getString(id)); 80 77 addReal(msg, "length", length); 81 78 addReal(msg, "width", width); 82 79 addReal(msg, "height", height); 83 sendMessage( state.conn, msg);80 sendMessage(conn, msg); 84 81 end createBox; 85 82 86 83 87 84 function createBoxAt 88 input State state; 85 input Connection conn; 86 input Context context; 89 87 input Real length, width, height; 90 88 input Real tx,ty,tz; 91 89 input Id id; 92 90 protected 93 91 Message msg = Message(TARGET, OBJECT, INTERFACE, "make_box"); 94 92 algorithm 95 setString(id,"box_" + String(modcount.increase_get( state.context)));93 setString(id,"box_" + String(modcount.increase_get(context))); 96 94 addString(msg, "reference", getString(id)); 97 95 addReal(msg, "length", length); 98 96 addReal(msg, "width", width); … … 100 98 addReal(msg, "tx", tx); 101 99 addReal(msg, "ty", ty); 102 100 addReal(msg, "tz", tz); 103 sendMessage( state.conn, msg);101 sendMessage(conn, msg); 104 102 end createBoxAt; 105 103 106 104 function loadFromFile 107 input State state; 105 input Connection conn; 106 input Context context; 108 107 input String fileName; 109 108 input Real tx,ty,tz; 110 109 input Id id; 111 110 protected 112 111 Message msg = Message(TARGET, OBJECT, INTERFACE, "loadFromFile"); 113 112 algorithm 114 setString(id,"file_" + String(modcount.increase_get( state.context)));113 setString(id,"file_" + String(modcount.increase_get(context))); 115 114 addString(msg, "reference", getString(id)); 116 115 addString(msg, "fileName", fileName); 117 116 addReal(msg, "tx", tx); 118 117 addReal(msg, "ty", ty); 119 118 addReal(msg, "tz", tz); 120 sendMessage( state.conn, msg);119 sendMessage(conn, msg); 121 120 end loadFromFile; 122 121 123 122 124 123 function createSphere 125 input State state; 124 input Connection conn; 125 input Context context; 126 126 input Real size; 127 127 input Id id; 128 128 protected 129 129 Message msg = Message(TARGET, OBJECT, INTERFACE, "make_sphere"); 130 130 algorithm 131 setString(id,"sphere_" + String(modcount.increase_get( state.context)));131 setString(id,"sphere_" + String(modcount.increase_get(context))); 132 132 addString(msg, "reference", getString(id)); 133 133 addReal(msg, "size", size); 134 sendMessage( state.conn, msg);134 sendMessage(conn, msg); 135 135 end createSphere; 136 136 137 137 138 138 function createCylinder 139 input State state; 139 input Connection conn; 140 input Context context; 140 141 input Real height; 141 142 input Real diameter; 142 143 input Id id; 143 144 protected 144 145 Message msg = Message(TARGET, OBJECT, INTERFACE, "make_cylinder"); 145 146 algorithm 146 setString(id,"cylinder_" + String(modcount.increase_get( state.context)));147 setString(id,"cylinder_" + String(modcount.increase_get(context))); 147 148 addString(msg, "reference", getString(id)); 148 149 addReal(msg, "height", height); 149 150 addReal(msg, "diameter", diameter); 150 sendMessage( state.conn, msg);151 sendMessage(conn, msg); 151 152 end createCylinder; 152 153 153 154 154 155 function createCylinderAt 155 input State state; 156 input Connection conn; 157 input Context context; 156 158 input Real height; 157 159 input Real diameter; 158 160 input Real x,y,z; … … 160 162 protected 161 163 Message msg = Message(TARGET, OBJECT, INTERFACE, "make_cylinder"); 162 164 algorithm 163 setString(id,"cylinder_" + String(modcount.increase_get( state.context)));165 setString(id,"cylinder_" + String(modcount.increase_get(context))); 164 166 addString(msg, "reference", getString(id)); 165 167 addReal(msg, "height", height); 166 168 addReal(msg, "diameter", diameter); 167 169 addReal(msg, "x", x); 168 170 addReal(msg, "y", y); 169 171 addReal(msg, "z", z); 170 sendMessage( state.conn, msg);172 sendMessage(conn, msg); 171 173 end createCylinderAt; 172 174 173 175 174 176 function createCone 175 input State state; 177 input Connection conn; 178 input Context context; 176 179 input Real height; 177 180 input Real diameter; 178 181 input Id id; 179 182 protected 180 183 Message msg = Message(TARGET, OBJECT, INTERFACE, "make_cone"); 181 184 algorithm 182 setString(id,"cone_" + String(modcount.increase_get( state.context)));185 setString(id,"cone_" + String(modcount.increase_get(context))); 183 186 addString(msg, "reference", getString(id)); 184 187 addReal(msg, "height", height); 185 188 addReal(msg, "diameter", diameter); 186 sendMessage( state.conn, msg);189 sendMessage(conn, msg); 187 190 end createCone; 188 191 189 192 190 193 function createConeAt 191 input State state; 194 input Connection conn; 195 input Context context; 192 196 input Real height; 193 197 input Real diameter; 194 198 input Real x,y,z; … … 196 200 protected 197 201 Message msg = Message(TARGET, OBJECT, INTERFACE, "make_cone"); 198 202 algorithm 199 setString(id,"cone_" + String(modcount.increase_get( state.context)));203 setString(id,"cone_" + String(modcount.increase_get(context))); 200 204 addString(msg, "reference", getString(id)); 201 205 addReal(msg, "height", height); 202 206 addReal(msg, "diameter", diameter); 203 207 addReal(msg, "x", x); 204 208 addReal(msg, "y", y); 205 209 addReal(msg, "z", z); 206 sendMessage( state.conn, msg);210 sendMessage(conn, msg); 207 211 end createConeAt; 208 212 209 213 210 214 function moveTo 211 input State state; 215 input Connection conn; 216 input Context context; 212 217 input Id id; 213 218 input Real p[3]; 214 219 input Real t; … … 221 226 addReal(msg, "y", p[2]); 222 227 addReal(msg, "z", p[3]); 223 228 addReal(msg, "t", t); 224 r := sendMessage( state.conn, msg);229 r := sendMessage(conn, msg); 225 230 end moveTo; 226 231 227 232 228 233 function moveZ 229 input State state; 234 input Connection conn; 235 input Context context; 230 236 input Id id; 231 237 input Real z; 232 238 input Real t; … … 237 243 addString(msg, "reference", getString(id)); 238 244 addReal(msg, "z", z); 239 245 addReal(msg, "t", t); 240 r := sendMessage( state.conn, msg);246 r := sendMessage(conn, msg); 241 247 end moveZ; 242 248 243 249 244 250 function scale 245 input State state; 251 input Connection conn; 252 input Context context; 246 253 input Id id; 247 254 input Real x,y,z; 248 255 input Real t; … … 255 262 addReal(msg, "y", y); 256 263 addReal(msg, "z", z); 257 264 addReal(msg, "t", t); 258 r := sendMessage( state.conn, msg);265 r := sendMessage(conn, msg); 259 266 end scale; 260 267 261 268 262 269 function scaleZ 263 input State state; 270 input Connection conn; 271 input Context context; 264 272 input Id id; 265 273 input Real z; 266 274 input Real t; … … 271 279 addString(msg, "reference", getString(id)); 272 280 addReal(msg, "z", z); 273 281 addReal(msg, "t", t); 274 r := sendMessage( state.conn, msg);282 r := sendMessage(conn, msg); 275 283 end scaleZ; 276 284 277 285 278 286 function setAmbientColor 279 input State state; 287 input Connection conn; 288 input Context context; 280 289 input Id id; 281 290 input Real r,g,b,a; 282 291 input Real t; … … 290 299 addReal(msg, "b", b); 291 300 addReal(msg, "a", a); 292 301 addReal(msg, "t", t); 293 res := sendMessage( state.conn, msg);302 res := sendMessage(conn, msg); 294 303 end setAmbientColor; 295 304 296 305 297 306 function setDiffuseColor 298 input State state; 307 input Connection conn; 308 input Context context; 299 309 input Id id; 300 310 input Real r,g,b,a; 301 311 input Real t; … … 309 319 addReal(msg, "b", b); 310 320 addReal(msg, "a", a); 311 321 addReal(msg, "t", t); 312 res := sendMessage( state.conn, msg);322 res := sendMessage(conn, msg); 313 323 end setDiffuseColor; 314 324 315 325 316 326 function setSpecularColor 317 input State state; 327 input Connection conn; 328 input Context context; 318 329 input Id id; 319 330 input Real r,g,b,a; 320 331 input Real t; … … 328 339 addReal(msg, "b", b); 329 340 addReal(msg, "a", a); 330 341 addReal(msg, "t", t); 331 res := sendMessage( state.conn, msg);342 res := sendMessage(conn, msg); 332 343 end setSpecularColor; 333 344 334 345 335 346 function setMatProperty 336 input State state; 347 input Connection conn; 348 input Context context; 337 349 input Id id; 338 350 input String property; 339 351 input Real value; … … 346 358 addString(msg, "prop", property); 347 359 addReal(msg, "value", value); 348 360 addReal(msg, "t", t); 349 res := sendMessage( state.conn, msg);361 res := sendMessage(conn, msg); 350 362 end setMatProperty; 351 363 352 364 353 365 function loadSceneFromFile 354 input State state; 366 input Connection conn; 367 input Context context; 355 368 input String fileName; 356 369 output String res; 357 370 protected 358 371 Message msg = Message(TARGET, OBJECT, INTERFACE, "load_scene"); 359 372 algorithm 360 373 addString(msg, "filepath", fileName); 361 res := sendMessage( state.conn, msg);374 res := sendMessage(conn, msg); 362 375 end loadSceneFromFile; 363 376 364 377 365 378 function rotate 366 input State state; 379 input Connection conn; 380 input Context context; 367 381 input Id id; 368 382 input Real[3,3] R; 369 383 input Real t; … … 381 395 end for; 382 396 383 397 addReal(msg, "t", t); 384 r := sendMessage( state.conn, msg);398 r := sendMessage(conn, msg); 385 399 end rotate; 386 400 387 401 388 402 function stop 389 input State state; 403 input Connection conn; 404 input Context context; 390 405 output String r; 391 406 protected 392 407 Message msg = Message(TARGET, OBJECT, INTERFACE, "stop"); 393 408 algorithm 394 r := sendMessage( state.conn, msg);409 r := sendMessage(conn, msg); 395 410 end stop; 396 411 397 412 … … 406 421 import Modelica.Mechanics.MultiBody.Frames; 407 422 408 423 function shapeDescrTo3D 409 input State state; 424 input Connection conn; 425 input Context context; 410 426 input String descr; 411 427 input Real length, width, height; 412 428 input Real[3] at; 413 429 input Real extra; 414 430 input Id id; 415 431 algorithm 432 416 433 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); 418 435 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); 420 437 elseif (descr == "sphere") then 421 createSphere( state, length, id);438 createSphere(conn, context, length, id); 422 439 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); 424 441 elseif (descr == "pipecylinder") then 425 442 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); 427 444 else 428 445 // not yet supported 429 446 Modelica.Utilities.Streams.print("Error: Visualization of pipecylinders has not been implemented yet!"); … … 431 448 end if; 432 449 elseif (descr == "pipe") then 433 450 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); 435 452 else 436 453 // not yet supported 437 454 Modelica.Utilities.Streams.print("Error: Visualization of pipes has not been implemented yet!"); … … 451 468 setString(id,"UNKNOWN"); 452 469 else 453 470 // 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); 455 472 end if; 456 473 457 474 end shapeDescrTo3D; … … 471 488 //initial equation 472 489 // pre(oldT) = R.T; 473 490 // pre(pos) = r + Frames.resolve1(R, r_shape); 474 initial algorithm475 if modcount.get(initContext) <> 1 then476 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;485 491 algorithm 486 492 when m3d_control.send and modcount.get(initContext) == 1 then 487 493 // check for rotation 488 494 rotated := not Modelica.Math.Matrices.isEqual(R.T, oldT); 489 495 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); 491 497 end if; 492 498 oldT := R.T; 493 499 … … 495 501 pos := r + Frames.resolve1(R, r_shape); 496 502 moved := not Modelica.Math.Vectors.isEqual(pos, pre(pos)); 497 503 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); 499 505 end if; 500 506 501 507 end when; 502 508 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 503 530 annotation ( 504 531 Icon(coordinateSystem( 505 532 preserveAspectRatio=true,