1 /* 2 Copyright 2008-2025 3 Matthias Ehmann, 4 Carsten Miller, 5 Andreas Walter, 6 Alfred Wassermann 7 8 This file is part of JSXGraph. 9 10 JSXGraph is free software dual licensed under the GNU LGPL or MIT License. 11 12 You can redistribute it and/or modify it under the terms of the 13 14 * GNU Lesser General Public License as published by 15 the Free Software Foundation, either version 3 of the License, or 16 (at your option) any later version 17 OR 18 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 19 20 JSXGraph is distributed in the hope that it will be useful, 21 but WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 GNU Lesser General Public License for more details. 24 25 You should have received a copy of the GNU Lesser General Public License and 26 the MIT License along with JSXGraph. If not, see <https://www.gnu.org/licenses/> 27 and <https://opensource.org/licenses/MIT/>. 28 */ 29 /* 30 Some functionalities in this file were developed as part of a software project 31 with students. We would like to thank all contributors for their help: 32 33 Winter semester 2023/2024: 34 Lars Hofmann 35 Leonhard Iser 36 Vincent Kulicke 37 Laura Rinas 38 */ 39 40 /*global JXG:true, define: true*/ 41 42 import JXG from "../jxg.js"; 43 import Const from "../base/constants.js"; 44 import Coords from "../base/coords.js"; 45 import Type from "../utils/type.js"; 46 import Mat from "../math/math.js"; 47 import Geometry from "../math/geometry.js"; 48 import Numerics from "../math/numerics.js"; 49 import Env from "../utils/env.js"; 50 import GeometryElement from "../base/element.js"; 51 import Composition from "../base/composition.js"; 52 53 /** 54 * 3D view inside a JXGraph board. 55 * 56 * @class Creates a new 3D view. Do not use this constructor to create a 3D view. Use {@link JXG.Board#create} with 57 * type {@link View3D} instead. 58 * 59 * @augments JXG.GeometryElement 60 * @param {Array} parents Array consisting of lower left corner [x, y] of the view inside the board, [width, height] of the view 61 * and box size [[x1, x2], [y1,y2], [z1,z2]]. If the view's azimuth=0 and elevation=0, the 3D view will cover a rectangle with lower left corner 62 * [x,y] and side lengths [w, h] of the board. 63 */ 64 JXG.View3D = function (board, parents, attributes) { 65 this.constructor(board, attributes, Const.OBJECT_TYPE_VIEW3D, Const.OBJECT_CLASS_3D); 66 67 /** 68 * An associative array containing all geometric objects belonging to the view. 69 * Key is the id of the object and value is a reference to the object. 70 * @type Object 71 * @private 72 */ 73 this.objects = {}; 74 75 /** 76 * An array containing all the elements in the view that are sorted due to their depth order. 77 * @Type Object 78 * @private 79 */ 80 this.depthOrdered = {}; 81 82 /** 83 * TODO: why deleted? 84 * An array containing all geometric objects in this view in the order of construction. 85 * @type Array 86 * @private 87 */ 88 // this.objectsList = []; 89 90 /** 91 * An associative array / dictionary to store the objects of the board by name. The name of the object is the key and value is a reference to the object. 92 * @type Object 93 * @private 94 */ 95 this.elementsByName = {}; 96 97 /** 98 * Default axes of the 3D view, contains the axes of the view or null. 99 * 100 * @type {Object} 101 * @default null 102 */ 103 this.defaultAxes = null; 104 105 /** 106 * The Tait-Bryan angles specifying the view box orientation 107 */ 108 this.angles = { 109 az: null, 110 el: null, 111 bank: null 112 }; 113 114 /** 115 * @type {Array} 116 * The view box orientation matrix 117 */ 118 this.matrix3DRot = [ 119 [1, 0, 0, 0], 120 [0, 1, 0, 0], 121 [0, 0, 1, 0], 122 [0, 0, 0, 1] 123 ]; 124 125 // Used for z-index computation 126 this.matrix3DRotShift = [ 127 [1, 0, 0, 0], 128 [0, 1, 0, 0], 129 [0, 0, 1, 0], 130 [0, 0, 0, 1] 131 ]; 132 133 /** 134 * @type {Array} 135 * @private 136 */ 137 // 3D-to-2D transformation matrix 138 this.matrix3D = [ 139 [1, 0, 0, 0], 140 [0, 1, 0, 0], 141 [0, 0, 1, 0] 142 ]; 143 144 /** 145 * The 4×4 matrix that maps box coordinates to camera coordinates. These 146 * coordinate systems fit into the View3D coordinate atlas as follows. 147 * <ul> 148 * <li><b>World coordinates.</b> The coordinates used to specify object 149 * positions in a JSXGraph scene.</li> 150 * <li><b>Box coordinates.</b> The world coordinates translated to put the 151 * center of the view box at the origin. 152 * <li><b>Camera coordinates.</b> The coordinate system where the 153 * <code>x</code>, <code>y</code> plane is the screen, the origin is the 154 * center of the screen, and the <code>z</code> axis points out of the 155 * screen, toward the viewer. 156 * <li><b>Focal coordinates.</b> The camera coordinates translated to put 157 * the origin at the focal point, which is set back from the screen by the 158 * focal distance.</li> 159 * </ul> 160 * The <code>boxToCam</code> transformation is exposed to help 3D elements 161 * manage their 2D representations in central projection mode. To map world 162 * coordinates to focal coordinates, use the 163 * {@link JXG.View3D#worldToFocal} method. 164 * @type {Array} 165 */ 166 this.boxToCam = []; 167 168 /** 169 * @type array 170 * @private 171 */ 172 // Lower left corner [x, y] of the 3D view if elevation and azimuth are set to 0. 173 this.llftCorner = parents[0]; 174 175 /** 176 * Width and height [w, h] of the 3D view if elevation and azimuth are set to 0. 177 * @type array 178 * @private 179 */ 180 this.size = parents[1]; 181 182 /** 183 * Bounding box (cube) [[x1, x2], [y1,y2], [z1,z2]] of the 3D view 184 * @type array 185 */ 186 this.bbox3D = parents[2]; 187 188 /** 189 * The distance from the camera to the origin. In other words, the 190 * radius of the sphere where the camera sits. 191 * @type Number 192 */ 193 this.r = -1; 194 195 /** 196 * The distance from the camera to the screen. Computed automatically from 197 * the `fov` property. 198 * @type Number 199 */ 200 this.focalDist = -1; 201 202 /** 203 * Type of projection. 204 * @type String 205 */ 206 // Will be set in update(). 207 this.projectionType = 'parallel'; 208 209 /** 210 * Whether trackball navigation is currently enabled. 211 * @type String 212 */ 213 this.trackballEnabled = false; 214 215 this.timeoutAzimuth = null; 216 217 this.zIndexMin = Infinity; 218 this.zIndexMax = -Infinity; 219 220 this.id = this.board.setId(this, 'V'); 221 this.board.finalizeAdding(this); 222 this.elType = 'view3d'; 223 224 this.methodMap = Type.deepCopy(this.methodMap, { 225 // TODO 226 }); 227 }; 228 JXG.View3D.prototype = new GeometryElement(); 229 230 JXG.extend( 231 JXG.View3D.prototype, /** @lends JXG.View3D.prototype */ { 232 233 /** 234 * Creates a new 3D element of type elementType. 235 * @param {String} elementType Type of the element to be constructed given as a string e.g. 'point3d' or 'surface3d'. 236 * @param {Array} parents Array of parent elements needed to construct the element e.g. coordinates for a 3D point or two 237 * 3D points to construct a line. This highly depends on the elementType that is constructed. See the corresponding JXG.create* 238 * methods for a list of possible parameters. 239 * @param {Object} [attributes] An object containing the attributes to be set. This also depends on the elementType. 240 * Common attributes are name, visible, strokeColor. 241 * @returns {Object} Reference to the created element. This is usually a GeometryElement3D, but can be an array containing 242 * two or more elements. 243 */ 244 create: function (elementType, parents, attributes) { 245 var prefix = [], 246 el; 247 248 if (elementType.indexOf('3d') > 0) { 249 // is3D = true; 250 prefix.push(this); 251 } 252 el = this.board.create(elementType, prefix.concat(parents), attributes); 253 254 return el; 255 }, 256 257 /** 258 * Select a single or multiple elements at once. 259 * @param {String|Object|function} str The name, id or a reference to a JSXGraph 3D element in the 3D view. An object will 260 * be used as a filter to return multiple elements at once filtered by the properties of the object. 261 * @param {Boolean} onlyByIdOrName If true (default:false) elements are only filtered by their id, name or groupId. 262 * The advanced filters consisting of objects or functions are ignored. 263 * @returns {JXG.GeometryElement3D|JXG.Composition} 264 * @example 265 * // select the element with name A 266 * view.select('A'); 267 * 268 * // select all elements with strokecolor set to 'red' (but not '#ff0000') 269 * view.select({ 270 * strokeColor: 'red' 271 * }); 272 * 273 * // select all points on or below the x/y plane and make them black. 274 * view.select({ 275 * elType: 'point3d', 276 * Z: function (v) { 277 * return v <= 0; 278 * } 279 * }).setAttribute({color: 'black'}); 280 * 281 * // select all elements 282 * view.select(function (el) { 283 * return true; 284 * }); 285 */ 286 select: function (str, onlyByIdOrName) { 287 var flist, 288 olist, 289 i, 290 l, 291 s = str; 292 293 if (s === null) { 294 return s; 295 } 296 297 if (Type.isString(s) && s !== '') { 298 // It's a string, most likely an id or a name. 299 // Search by ID 300 if (Type.exists(this.objects[s])) { 301 s = this.objects[s]; 302 // Search by name 303 } else if (Type.exists(this.elementsByName[s])) { 304 s = this.elementsByName[s]; 305 // // Search by group ID 306 // } else if (Type.exists(this.groups[s])) { 307 // s = this.groups[s]; 308 } 309 310 } else if ( 311 !onlyByIdOrName && 312 (Type.isFunction(s) || (Type.isObject(s) && !Type.isFunction(s.setAttribute))) 313 ) { 314 // It's a function or an object, but not an element 315 flist = Type.filterElements(this.objectsList, s); 316 317 olist = {}; 318 l = flist.length; 319 for (i = 0; i < l; i++) { 320 olist[flist[i].id] = flist[i]; 321 } 322 s = new Composition(olist); 323 324 } else if ( 325 Type.isObject(s) && 326 Type.exists(s.id) && 327 !Type.exists(this.objects[s.id]) 328 ) { 329 // It's an element which has been deleted (and still hangs around, e.g. in an attractor list) 330 s = null; 331 } 332 333 return s; 334 }, 335 336 // set the Tait-Bryan angles to specify the current view rotation matrix 337 setAnglesFromRotation: function () { 338 var rem = this.matrix3DRot, // rotation remaining after angle extraction 339 rBank, cosBank, sinBank, 340 cosEl, sinEl, 341 cosAz, sinAz; 342 343 // extract bank by rotating the view box z axis onto the camera yz plane 344 rBank = Math.sqrt(rem[1][3] * rem[1][3] + rem[2][3] * rem[2][3]); 345 if (rBank > Mat.eps) { 346 cosBank = rem[2][3] / rBank; 347 sinBank = rem[1][3] / rBank; 348 } else { 349 // if the z axis is pointed almost exactly at the screen, we 350 // keep the current bank value 351 cosBank = Math.cos(this.angles.bank); 352 sinBank = Math.sin(this.angles.bank); 353 } 354 rem = Mat.matMatMult([ 355 [1, 0, 0, 0], 356 [0, cosBank, -sinBank, 0], 357 [0, sinBank, cosBank, 0], 358 [0, 0, 0, 1] 359 ], rem); 360 this.angles.bank = Math.atan2(sinBank, cosBank); 361 362 // extract elevation by rotating the view box z axis onto the camera 363 // y axis 364 cosEl = rem[2][3]; 365 sinEl = rem[3][3]; 366 rem = Mat.matMatMult([ 367 [1, 0, 0, 0], 368 [0, 1, 0, 0], 369 [0, 0, cosEl, sinEl], 370 [0, 0, -sinEl, cosEl] 371 ], rem); 372 this.angles.el = Math.atan2(sinEl, cosEl); 373 374 // extract azimuth 375 cosAz = -rem[1][1]; 376 sinAz = rem[3][1]; 377 this.angles.az = Math.atan2(sinAz, cosAz); 378 if (this.angles.az < 0) this.angles.az += 2 * Math.PI; 379 380 this.setSlidersFromAngles(); 381 }, 382 383 anglesHaveMoved: function () { 384 return ( 385 this._hasMoveAz || this._hasMoveEl || 386 Math.abs(this.angles.az - this.az_slide.Value()) > Mat.eps || 387 Math.abs(this.angles.el - this.el_slide.Value()) > Mat.eps || 388 Math.abs(this.angles.bank - this.bank_slide.Value()) > Mat.eps 389 ); 390 }, 391 392 getAnglesFromSliders: function () { 393 this.angles.az = this.az_slide.Value(); 394 this.angles.el = this.el_slide.Value(); 395 this.angles.bank = this.bank_slide.Value(); 396 }, 397 398 setSlidersFromAngles: function () { 399 this.az_slide.setValue(this.angles.az); 400 this.el_slide.setValue(this.angles.el); 401 this.bank_slide.setValue(this.angles.bank); 402 }, 403 404 // return the rotation matrix specified by the current Tait-Bryan angles 405 getRotationFromAngles: function () { 406 var a, e, b, f, 407 cosBank, sinBank, 408 mat = [ 409 [1, 0, 0, 0], 410 [0, 1, 0, 0], 411 [0, 0, 1, 0], 412 [0, 0, 0, 1] 413 ]; 414 415 // mat projects homogeneous 3D coords in View3D 416 // to homogeneous 2D coordinates in the board 417 a = this.angles.az; 418 e = this.angles.el; 419 b = this.angles.bank; 420 f = -Math.sin(e); 421 422 mat[1][1] = -Math.cos(a); 423 mat[1][2] = Math.sin(a); 424 mat[1][3] = 0; 425 426 mat[2][1] = f * Math.sin(a); 427 mat[2][2] = f * Math.cos(a); 428 mat[2][3] = Math.cos(e); 429 430 mat[3][1] = Math.cos(e) * Math.sin(a); 431 mat[3][2] = Math.cos(e) * Math.cos(a); 432 mat[3][3] = Math.sin(e); 433 434 cosBank = Math.cos(b); 435 sinBank = Math.sin(b); 436 mat = Mat.matMatMult([ 437 [1, 0, 0, 0], 438 [0, cosBank, sinBank, 0], 439 [0, -sinBank, cosBank, 0], 440 [0, 0, 0, 1] 441 ], mat); 442 443 return mat; 444 445 /* this code, originally from `_updateCentralProjection`, is an 446 * alternate implementation of the azimuth-elevation matrix 447 * computation above. using this implementation instead of the 448 * current one might lead to simpler code in a future refactoring 449 var a, e, up, 450 ax, ay, az, v, nrm, 451 eye, d, 452 func_sphere; 453 454 // finds the point on the unit sphere with the given azimuth and 455 // elevation, and returns its affine coordinates 456 func_sphere = function (az, el) { 457 return [ 458 Math.cos(az) * Math.cos(el), 459 -Math.sin(az) * Math.cos(el), 460 Math.sin(el) 461 ]; 462 }; 463 464 a = this.az_slide.Value() + (3 * Math.PI * 0.5); // Sphere 465 e = this.el_slide.Value(); 466 467 // create an up vector and an eye vector which are 90 degrees out of phase 468 up = func_sphere(a, e + Math.PI / 2); 469 eye = func_sphere(a, e); 470 d = [eye[0], eye[1], eye[2]]; 471 472 nrm = Mat.norm(d, 3); 473 az = [d[0] / nrm, d[1] / nrm, d[2] / nrm]; 474 475 nrm = Mat.norm(up, 3); 476 v = [up[0] / nrm, up[1] / nrm, up[2] / nrm]; 477 478 ax = Mat.crossProduct(v, az); 479 ay = Mat.crossProduct(az, ax); 480 481 this.matrix3DRot[1] = [0, ax[0], ax[1], ax[2]]; 482 this.matrix3DRot[2] = [0, ay[0], ay[1], ay[2]]; 483 this.matrix3DRot[3] = [0, az[0], az[1], az[2]]; 484 */ 485 }, 486 487 /** 488 * Project 2D point (x,y) to the virtual trackpad sphere, 489 * see Bell's virtual trackpad, and return z-component of the 490 * number. 491 * 492 * @param {Number} r 493 * @param {Number} x 494 * @param {Number} y 495 * @returns Number 496 * @private 497 */ 498 _projectToSphere: function (r, x, y) { 499 var d = Mat.hypot(x, y), 500 t, z; 501 502 if (d < r * 0.7071067811865475) { // Inside sphere 503 z = Math.sqrt(r * r - d * d); 504 } else { // On hyperbola 505 t = r / 1.414213562373095; 506 z = t * t / d; 507 } 508 return z; 509 }, 510 511 /** 512 * Determine 4x4 rotation matrix with Bell's virtual trackball. 513 * 514 * @returns {Array} 4x4 rotation matrix 515 * @private 516 */ 517 updateProjectionTrackball: function (Pref) { 518 var R = 100, 519 dx, dy, dr2, 520 p1, p2, x, y, theta, t, d, 521 c, s, n, 522 mat = [ 523 [1, 0, 0, 0], 524 [0, 1, 0, 0], 525 [0, 0, 1, 0], 526 [0, 0, 0, 1] 527 ]; 528 529 if (!Type.exists(this._trackball)) { 530 return this.matrix3DRot; 531 } 532 533 dx = this._trackball.dx; 534 dy = this._trackball.dy; 535 dr2 = dx * dx + dy * dy; 536 if (dr2 > Mat.eps) { 537 // // Method by Hanson, "The rolling ball", Graphics Gems III, p.51 538 // // Rotation axis: 539 // // n = (-dy/dr, dx/dr, 0) 540 // // Rotation angle around n: 541 // // theta = atan(dr / R) approx dr / R 542 // dr = Math.sqrt(dr2); 543 // c = R / Math.hypot(R, dr); // cos(theta) 544 // t = 1 - c; // 1 - cos(theta) 545 // s = dr / Math.hypot(R, dr); // sin(theta) 546 // n = [-dy / dr, dx / dr, 0]; 547 548 // Bell virtual trackpad, see 549 // https://opensource.apple.com/source/X11libs/X11libs-60/mesa/Mesa-7.8.2/progs/util/trackball.c.auto.html 550 // http://scv.bu.edu/documentation/presentations/visualizationworkshop08/materials/opengl/trackball.c. 551 // See also Henriksen, Sporring, Hornaek, "Virtual Trackballs revisited". 552 // 553 R = (this.size[0] * this.board.unitX + this.size[1] * this.board.unitY) * 0.25; 554 x = this._trackball.x; 555 y = this._trackball.y; 556 557 p2 = [x, y, this._projectToSphere(R, x, y)]; 558 x -= dx; 559 y -= dy; 560 p1 = [x, y, this._projectToSphere(R, x, y)]; 561 562 n = Mat.crossProduct(p1, p2); 563 d = Mat.hypot(n[0], n[1], n[2]); 564 n[0] /= d; 565 n[1] /= d; 566 n[2] /= d; 567 568 t = Geometry.distance(p2, p1, 3) / (2 * R); 569 t = (t > 1.0) ? 1.0 : t; 570 t = (t < -1.0) ? -1.0 : t; 571 theta = 2.0 * Math.asin(t); 572 c = Math.cos(theta); 573 t = 1 - c; 574 s = Math.sin(theta); 575 576 // Rotation by theta about the axis n. See equation 9.63 of 577 // 578 // Ian Richard Cole. "Modeling CPV" (thesis). Loughborough 579 // University. https://hdl.handle.net/2134/18050 580 // 581 mat[1][1] = c + n[0] * n[0] * t; 582 mat[2][1] = n[1] * n[0] * t + n[2] * s; 583 mat[3][1] = n[2] * n[0] * t - n[1] * s; 584 585 mat[1][2] = n[0] * n[1] * t - n[2] * s; 586 mat[2][2] = c + n[1] * n[1] * t; 587 mat[3][2] = n[2] * n[1] * t + n[0] * s; 588 589 mat[1][3] = n[0] * n[2] * t + n[1] * s; 590 mat[2][3] = n[1] * n[2] * t - n[0] * s; 591 mat[3][3] = c + n[2] * n[2] * t; 592 } 593 594 mat = Mat.matMatMult(mat, this.matrix3DRot); 595 return mat; 596 }, 597 598 updateAngleSliderBounds: function () { 599 var az_smax, az_smin, 600 el_smax, el_smin, el_cover, 601 el_smid, el_equiv, el_flip_equiv, 602 el_equiv_loss, el_flip_equiv_loss, el_interval_loss, 603 bank_smax, bank_smin; 604 605 // update stored trackball toggle 606 this.trackballEnabled = this.evalVisProp('trackball.enabled'); 607 608 // set slider bounds 609 if (this.trackballEnabled) { 610 this.az_slide.setMin(0); 611 this.az_slide.setMax(2 * Math.PI); 612 this.el_slide.setMin(-0.5 * Math.PI); 613 this.el_slide.setMax(0.5 * Math.PI); 614 this.bank_slide.setMin(-Math.PI); 615 this.bank_slide.setMax(Math.PI); 616 } else { 617 this.az_slide.setMin(this.visProp.az.slider.min); 618 this.az_slide.setMax(this.visProp.az.slider.max); 619 this.el_slide.setMin(this.visProp.el.slider.min); 620 this.el_slide.setMax(this.visProp.el.slider.max); 621 this.bank_slide.setMin(this.visProp.bank.slider.min); 622 this.bank_slide.setMax(this.visProp.bank.slider.max); 623 } 624 625 // get new slider bounds 626 az_smax = this.az_slide._smax; 627 az_smin = this.az_slide._smin; 628 el_smax = this.el_slide._smax; 629 el_smin = this.el_slide._smin; 630 bank_smax = this.bank_slide._smax; 631 bank_smin = this.bank_slide._smin; 632 633 // wrap and restore angle values 634 if (this.trackballEnabled) { 635 // if we're upside-down, flip the bank angle to reach the same 636 // orientation with an elevation between -pi/2 and pi/2 637 el_cover = Mat.mod(this.angles.el, 2 * Math.PI); 638 if (0.5 * Math.PI < el_cover && el_cover < 1.5 * Math.PI) { 639 this.angles.el = Math.PI - el_cover; 640 this.angles.az = Mat.wrap(this.angles.az + Math.PI, az_smin, az_smax); 641 this.angles.bank = Mat.wrap(this.angles.bank + Math.PI, bank_smin, bank_smax); 642 } 643 644 // wrap the azimuth and bank angle 645 this.angles.az = Mat.wrap(this.angles.az, az_smin, az_smax); 646 this.angles.el = Mat.wrap(this.angles.el, el_smin, el_smax); 647 this.angles.bank = Mat.wrap(this.angles.bank, bank_smin, bank_smax); 648 } else { 649 // wrap and clamp the elevation into the slider range. if 650 // flipping the elevation gets us closer to the slider interval, 651 // do that, inverting the azimuth and bank angle to compensate 652 el_interval_loss = function (t) { 653 if (t < el_smin) { 654 return el_smin - t; 655 } else if (el_smax < t) { 656 return t - el_smax; 657 } else { 658 return 0; 659 } 660 }; 661 el_smid = 0.5 * (el_smin + el_smax); 662 el_equiv = Mat.wrap( 663 this.angles.el, 664 el_smid - Math.PI, 665 el_smid + Math.PI 666 ); 667 el_flip_equiv = Mat.wrap( 668 Math.PI - this.angles.el, 669 el_smid - Math.PI, 670 el_smid + Math.PI 671 ); 672 el_equiv_loss = el_interval_loss(el_equiv); 673 el_flip_equiv_loss = el_interval_loss(el_flip_equiv); 674 if (el_equiv_loss <= el_flip_equiv_loss) { 675 this.angles.el = Mat.clamp(el_equiv, el_smin, el_smax); 676 } else { 677 this.angles.el = Mat.clamp(el_flip_equiv, el_smin, el_smax); 678 this.angles.az = Mat.wrap(this.angles.az + Math.PI, az_smin, az_smax); 679 this.angles.bank = Mat.wrap(this.angles.bank + Math.PI, bank_smin, bank_smax); 680 } 681 682 // wrap and clamp the azimuth and bank angle into the slider range 683 this.angles.az = Mat.wrapAndClamp(this.angles.az, az_smin, az_smax, 2 * Math.PI); 684 this.angles.bank = Mat.wrapAndClamp(this.angles.bank, bank_smin, bank_smax, 2 * Math.PI); 685 686 // since we're using `clamp`, angles may have changed 687 this.matrix3DRot = this.getRotationFromAngles(); 688 } 689 690 // restore slider positions 691 this.setSlidersFromAngles(); 692 }, 693 694 /** 695 * @private 696 * @returns {Array} 697 */ 698 _updateCentralProjection: function () { 699 var zf = 20, // near clip plane 700 zn = 8, // far clip plane 701 702 // See https://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/graphics_6_1_eng_web.html 703 // bbox3D is always at the world origin, i.e. T_obj is the unit matrix. 704 // All vectors contain affine coordinates and have length 3 705 // The matrices are of size 4x4. 706 r, A; 707 708 // set distance from view box center to camera 709 r = this.evalVisProp('r'); 710 if (r === 'auto') { 711 r = Mat.hypot( 712 this.bbox3D[0][0] - this.bbox3D[0][1], 713 this.bbox3D[1][0] - this.bbox3D[1][1], 714 this.bbox3D[2][0] - this.bbox3D[2][1] 715 ) * 1.01; 716 } 717 718 // compute camera transformation 719 // this.boxToCam = this.matrix3DRot.map((row) => row.slice()); 720 this.boxToCam = this.matrix3DRot.map(function (row) { return row.slice(); }); 721 this.boxToCam[3][0] = -r; 722 723 // compute focal distance and clip space transformation 724 this.focalDist = 1 / Math.tan(0.5 * this.evalVisProp('fov')); 725 A = [ 726 [0, 0, 0, -1], 727 [0, this.focalDist, 0, 0], 728 [0, 0, this.focalDist, 0], 729 [2 * zf * zn / (zn - zf), 0, 0, (zf + zn) / (zn - zf)] 730 ]; 731 732 return Mat.matMatMult(A, this.boxToCam); 733 }, 734 735 // Update 3D-to-2D transformation matrix with the actual azimuth and elevation angles. 736 update: function () { 737 var r = this.r, 738 stretch = [ 739 [1, 0, 0, 0], 740 [0, -r, 0, 0], 741 [0, 0, -r, 0], 742 [0, 0, 0, 1] 743 ], 744 mat2D, objectToClip, size, 745 dx, dy; 746 // objectsList; 747 748 if ( 749 !Type.exists(this.el_slide) || 750 !Type.exists(this.az_slide) || 751 !Type.exists(this.bank_slide) || 752 !this.needsUpdate 753 ) { 754 this.needsUpdate = false; 755 return this; 756 } 757 758 mat2D = [ 759 [1, 0, 0], 760 [0, 1, 0], 761 [0, 0, 1] 762 ]; 763 764 this.projectionType = this.evalVisProp('projection').toLowerCase(); 765 766 // override angle slider bounds when trackball navigation is enabled 767 if (this.trackballEnabled !== this.evalVisProp('trackball.enabled')) { 768 this.updateAngleSliderBounds(); 769 } 770 771 if (this._hasMoveTrackball) { 772 // The trackball has been moved since the last update, so we do 773 // trackball navigation. When the trackball is enabled, a drag 774 // event is interpreted as a trackball movement unless it's 775 // caught by something else, like point dragging. When the 776 // trackball is disabled, the trackball movement flag should 777 // never be set 778 this.matrix3DRot = this.updateProjectionTrackball(); 779 this.setAnglesFromRotation(); 780 } else if (this.anglesHaveMoved()) { 781 // The trackball hasn't been moved since the last up date, but 782 // the Tait-Bryan angles have been, so we do angle navigation 783 this.getAnglesFromSliders(); 784 this.matrix3DRot = this.getRotationFromAngles(); 785 } 786 787 /** 788 * The translation that moves the center of the view box to the origin. 789 */ 790 this.shift = [ 791 [1, 0, 0, 0], 792 [-0.5 * (this.bbox3D[0][0] + this.bbox3D[0][1]), 1, 0, 0], 793 [-0.5 * (this.bbox3D[1][0] + this.bbox3D[1][1]), 0, 1, 0], 794 [-0.5 * (this.bbox3D[2][0] + this.bbox3D[2][1]), 0, 0, 1] 795 ]; 796 797 switch (this.projectionType) { 798 case 'central': // Central projection 799 800 // Add a final transformation to scale and shift the projection 801 // on the board, usually called viewport. 802 size = 2 * 0.4; 803 mat2D[1][1] = this.size[0] / size; // w / d_x 804 mat2D[2][2] = this.size[1] / size; // h / d_y 805 mat2D[1][0] = this.llftCorner[0] + mat2D[1][1] * 0.5 * size; // llft_x 806 mat2D[2][0] = this.llftCorner[1] + mat2D[2][2] * 0.5 * size; // llft_y 807 // The transformations this.matrix3D and mat2D can not be combined at this point, 808 // since the projected vectors have to be normalized in between in project3DTo2D 809 this.viewPortTransform = mat2D; 810 811 objectToClip = this._updateCentralProjection(); 812 // this.matrix3D is a 4x4 matrix 813 this.matrix3D = Mat.matMatMult(objectToClip, this.shift); 814 break; 815 816 case 'parallel': // Parallel projection 817 default: 818 // Add a final transformation to scale and shift the projection 819 // on the board, usually called viewport. 820 dx = this.bbox3D[0][1] - this.bbox3D[0][0]; 821 dy = this.bbox3D[1][1] - this.bbox3D[1][0]; 822 mat2D[1][1] = this.size[0] / dx; // w / d_x 823 mat2D[2][2] = this.size[1] / dy; // h / d_y 824 mat2D[1][0] = this.llftCorner[0] + mat2D[1][1] * 0.5 * dx; // llft_x 825 mat2D[2][0] = this.llftCorner[1] + mat2D[2][2] * 0.5 * dy; // llft_y 826 827 // Combine all transformations, this.matrix3D is a 3x4 matrix 828 this.matrix3D = Mat.matMatMult( 829 mat2D, 830 Mat.matMatMult(Mat.matMatMult(this.matrix3DRot, stretch), this.shift).slice(0, 3) 831 ); 832 } 833 834 // Used for zIndex in dept ordering in subsequent update methods of the 835 // 3D elements and in view3d.updateRenderer 836 this.matrix3DRotShift = Mat.matMatMult(this.matrix3DRot, this.shift); 837 838 return this; 839 }, 840 841 /** 842 * Compares 3D elements according to their z-Index. 843 * @param {JXG.GeometryElement3D} a 844 * @param {JXG.GeometryElement3D} b 845 * @returns Number 846 */ 847 compareDepth: function (a, b) { 848 return a.zIndex - b.zIndex; 849 }, 850 851 updateZIndices: function() { 852 var id, el; 853 for (id in this.objects) { 854 if (this.objects.hasOwnProperty(id)) { 855 el = this.objects[id]; 856 // Update zIndex of less frequent objects line3d and polygon3d 857 // The other elements (point3d, face3d) do this in their update method. 858 if ((el.type === Const.OBJECT_TYPE_LINE3D || 859 el.type === Const.OBJECT_TYPE_POLYGON3D 860 ) && 861 Type.exists(el.element2D) && 862 el.element2D.evalVisProp('visible') 863 ) { 864 el.updateZIndex(); 865 } 866 } 867 } 868 }, 869 870 updateShaders: function() { 871 var id, el, v; 872 for (id in this.objects) { 873 if (this.objects.hasOwnProperty(id)) { 874 el = this.objects[id]; 875 if (Type.exists(el.shader)) { 876 v = el.shader(); 877 if (v < this.zIndexMin) { 878 this.zIndexMin = v; 879 } else if (v > this.zIndexMax) { 880 this.zIndexMax = v; 881 } 882 } 883 } 884 } 885 }, 886 887 updateDepthOrdering: function () { 888 var id, el, 889 i, layers, lay; 890 891 // Collect elements for depth ordering layer-wise 892 layers = this.evalVisProp('depthorder.layers'); 893 for (i = 0; i < layers.length; i++) { 894 this.depthOrdered[layers[i]] = []; 895 } 896 897 for (id in this.objects) { 898 if (this.objects.hasOwnProperty(id)) { 899 el = this.objects[id]; 900 if ((el.type === Const.OBJECT_TYPE_FACE3D || 901 el.type === Const.OBJECT_TYPE_LINE3D || 902 el.type === Const.OBJECT_TYPE_POINT3D || 903 el.type === Const.OBJECT_TYPE_POLYGON3D 904 ) && 905 Type.exists(el.element2D) && 906 el.element2D.evalVisProp('visible') 907 ) { 908 lay = el.element2D.evalVisProp('layer'); 909 if (layers.indexOf(lay) >= 0) { 910 this.depthOrdered[lay].push(el); 911 } 912 } 913 } 914 } 915 916 if (this.board.renderer && this.board.renderer.type === 'svg') { 917 for (i = 0; i < layers.length; i++) { 918 lay = layers[i]; 919 this.depthOrdered[lay].sort(this.compareDepth.bind(this)); 920 this.depthOrdered[lay].forEach((el) => this.board.renderer.setLayer(el.element2D, lay)); 921 // this.depthOrdered[lay].forEach((el) => console.log(el.zIndex)); 922 } 923 } 924 925 return this; 926 }, 927 928 updateRenderer: function () { 929 if (!this.needsUpdate) { 930 return this; 931 } 932 933 // console.time("update") 934 // Handle depth ordering 935 this.depthOrdered = {}; 936 937 if (this.shift !== undefined && this.evalVisProp('depthorder.enabled')) { 938 // Update the zIndices of certain element types. 939 // We do it here in updateRenderer, because the the elements' positions 940 // are meanwhile updated. 941 this.updateZIndices(); 942 943 this.updateShaders(); 944 945 if (this.board.renderer && this.board.renderer.type === 'svg') { 946 // For SVG we update the DOM order 947 // In canvas we sort the elements in board.updateRendererCanvas 948 this.updateDepthOrdering(); 949 } 950 } 951 // console.timeEnd("update") 952 953 this.needsUpdate = false; 954 return this; 955 }, 956 957 removeObject: function (object, saveMethod) { 958 var i; 959 960 // this.board.removeObject(object, saveMethod); 961 if (Type.isArray(object)) { 962 for (i = 0; i < object.length; i++) { 963 this.removeObject(object[i]); 964 } 965 return this; 966 } 967 968 object = this.select(object); 969 970 // // If the object which is about to be removed unknown or a string, do nothing. 971 // // it is a string if a string was given and could not be resolved to an element. 972 if (!Type.exists(object) || Type.isString(object)) { 973 return this; 974 } 975 976 try { 977 // // remove all children. 978 // for (el in object.childElements) { 979 // if (object.childElements.hasOwnProperty(el)) { 980 // object.childElements[el].board.removeObject(object.childElements[el]); 981 // } 982 // } 983 984 delete this.objects[object.id]; 985 } catch (e) { 986 JXG.debug('View3D ' + object.id + ': Could not be removed: ' + e); 987 } 988 989 // this.update(); 990 991 this.board.removeObject(object, saveMethod); 992 993 return this; 994 }, 995 996 /** 997 * Map world coordinates to focal coordinates. These coordinate systems 998 * are explained in the {@link JXG.View3D#boxToCam} matrix 999 * documentation. 1000 * 1001 * @param {Array} pWorld A world space point, in homogeneous coordinates. 1002 * @param {Boolean} [homog=true] Whether to return homogeneous coordinates. 1003 * If false, projects down to ordinary coordinates. 1004 */ 1005 worldToFocal: function (pWorld, homog = true) { 1006 var k, 1007 pView = Mat.matVecMult(this.boxToCam, Mat.matVecMult(this.shift, pWorld)); 1008 pView[3] -= pView[0] * this.focalDist; 1009 if (homog) { 1010 return pView; 1011 } else { 1012 for (k = 1; k < 4; k++) { 1013 pView[k] /= pView[0]; 1014 } 1015 return pView.slice(1, 4); 1016 } 1017 }, 1018 1019 /** 1020 * Project 3D coordinates to 2D board coordinates 1021 * The 3D coordinates are provides as three numbers x, y, z or one array of length 3. 1022 * 1023 * @param {Number|Array} x 1024 * @param {Number[]} y 1025 * @param {Number[]} z 1026 * @returns {Array} Array of length 3 containing the projection on to the board 1027 * in homogeneous user coordinates. 1028 */ 1029 project3DTo2D: function (x, y, z) { 1030 var vec, w; 1031 if (arguments.length === 3) { 1032 vec = [1, x, y, z]; 1033 } else { 1034 // Argument is an array 1035 if (x.length === 3) { 1036 // vec = [1].concat(x); 1037 vec = x.slice(); 1038 vec.unshift(1); 1039 } else { 1040 vec = x; 1041 } 1042 } 1043 1044 w = Mat.matVecMult(this.matrix3D, vec); 1045 1046 switch (this.projectionType) { 1047 case 'central': 1048 w[1] /= w[0]; 1049 w[2] /= w[0]; 1050 w[3] /= w[0]; 1051 w[0] /= w[0]; 1052 return Mat.matVecMult(this.viewPortTransform, w.slice(0, 3)); 1053 1054 case 'parallel': 1055 default: 1056 return w; 1057 } 1058 }, 1059 1060 /** 1061 * We know that v2d * w0 = mat * (1, x, y, d)^T where v2d = (1, b, c, h)^T with unknowns w0, h, x, y. 1062 * Setting R = mat^(-1) gives 1063 * 1/ w0 * (1, x, y, d)^T = R * v2d. 1064 * The first and the last row of this equation allows to determine 1/w0 and h. 1065 * 1066 * @param {Array} mat 1067 * @param {Array} v2d 1068 * @param {Number} d 1069 * @returns Array 1070 * @private 1071 */ 1072 _getW0: function (mat, v2d, d) { 1073 var R = Mat.inverse(mat), 1074 R1 = R[0][0] + v2d[1] * R[0][1] + v2d[2] * R[0][2], 1075 R2 = R[3][0] + v2d[1] * R[3][1] + v2d[2] * R[3][2], 1076 w, h, det; 1077 1078 det = d * R[0][3] - R[3][3]; 1079 w = (R2 * R[0][3] - R1 * R[3][3]) / det; 1080 h = (R2 - R1 * d) / det; 1081 return [1 / w, h]; 1082 }, 1083 1084 /** 1085 * Project a 2D coordinate to the plane defined by point "foot" 1086 * and the normal vector `normal`. 1087 * 1088 * @param {JXG.Point} point2d 1089 * @param {Array} normal Normal of plane 1090 * @param {Array} foot Foot point of plane 1091 * @returns {Array} of length 4 containing the projected 1092 * point in homogeneous coordinates. 1093 */ 1094 project2DTo3DPlane: function (point2d, normal, foot) { 1095 var mat, rhs, d, le, sol, 1096 f = foot.slice(1) || [0, 0, 0], 1097 n = normal.slice(1), 1098 v2d, w0, res; 1099 1100 le = Mat.norm(n, 3); 1101 d = Mat.innerProduct(f, n, 3) / le; 1102 1103 if (this.projectionType === 'parallel') { 1104 mat = this.matrix3D.slice(0, 3); // Copy each row by reference 1105 mat.push([0, n[0], n[1], n[2]]); 1106 1107 // 2D coordinates of point 1108 rhs = point2d.coords.usrCoords.slice(); 1109 rhs.push(d); 1110 try { 1111 // Prevent singularity in case elevation angle is zero 1112 if (mat[2][3] === 1.0) { 1113 mat[2][1] = mat[2][2] = Mat.eps * 0.001; 1114 } 1115 sol = Mat.Numerics.Gauss(mat, rhs); 1116 } catch (e) { 1117 sol = [0, NaN, NaN, NaN]; 1118 } 1119 } else { 1120 mat = this.matrix3D; 1121 1122 // 2D coordinates of point: 1123 rhs = point2d.coords.usrCoords.slice(); 1124 1125 v2d = Mat.Numerics.Gauss(this.viewPortTransform, rhs); 1126 res = this._getW0(mat, v2d, d); 1127 w0 = res[0]; 1128 rhs = [ 1129 v2d[0] * w0, 1130 v2d[1] * w0, 1131 v2d[2] * w0, 1132 res[1] * w0 1133 ]; 1134 try { 1135 // Prevent singularity in case elevation angle is zero 1136 if (mat[2][3] === 1.0) { 1137 mat[2][1] = mat[2][2] = Mat.eps * 0.001; 1138 } 1139 1140 sol = Mat.Numerics.Gauss(mat, rhs); 1141 sol[1] /= sol[0]; 1142 sol[2] /= sol[0]; 1143 sol[3] /= sol[0]; 1144 // sol[3] = d; 1145 sol[0] /= sol[0]; 1146 } catch (err) { 1147 sol = [0, NaN, NaN, NaN]; 1148 } 1149 } 1150 1151 return sol; 1152 }, 1153 1154 /** 1155 * Project a point on the screen to the nearest point, in screen 1156 * distance, on a line segment in 3d space. The inputs must be in 1157 * ordinary coordinates, but the output is in homogeneous coordinates. 1158 * 1159 * @param {Array} pScr The screen coordinates of the point to project. 1160 * @param {Array} end0 The world space coordinates of one end of the 1161 * line segment. 1162 * @param {Array} end1 The world space coordinates of the other end of 1163 * the line segment. 1164 * 1165 * @returns Homogeneous coordinates of the projection 1166 */ 1167 projectScreenToSegment: function (pScr, end0, end1) { 1168 var end0_2d = this.project3DTo2D(end0).slice(1, 3), 1169 end1_2d = this.project3DTo2D(end1).slice(1, 3), 1170 dir_2d = [ 1171 end1_2d[0] - end0_2d[0], 1172 end1_2d[1] - end0_2d[1] 1173 ], 1174 dir_2d_norm_sq = Mat.innerProduct(dir_2d, dir_2d), 1175 diff = [ 1176 pScr[0] - end0_2d[0], 1177 pScr[1] - end0_2d[1] 1178 ], 1179 s = Mat.innerProduct(diff, dir_2d) / dir_2d_norm_sq, // screen-space affine parameter 1180 mid, mid_2d, mid_diff, m, 1181 1182 t, // view-space affine parameter 1183 t_clamped, // affine parameter clamped to range 1184 t_clamped_co; 1185 1186 if (this.projectionType === 'central') { 1187 mid = [ 1188 0.5 * (end0[0] + end1[0]), 1189 0.5 * (end0[1] + end1[1]), 1190 0.5 * (end0[2] + end1[2]) 1191 ]; 1192 mid_2d = this.project3DTo2D(mid).slice(1, 3); 1193 mid_diff = [ 1194 mid_2d[0] - end0_2d[0], 1195 mid_2d[1] - end0_2d[1] 1196 ]; 1197 m = Mat.innerProduct(mid_diff, dir_2d) / dir_2d_norm_sq; 1198 1199 // the view-space affine parameter s is related to the 1200 // screen-space affine parameter t by a Möbius transformation, 1201 // which is determined by the following relations: 1202 // 1203 // s | t 1204 // ----- 1205 // 0 | 0 1206 // m | 1/2 1207 // 1 | 1 1208 // 1209 t = (1 - m) * s / ((1 - 2 * m) * s + m); 1210 } else { 1211 t = s; 1212 } 1213 1214 t_clamped = Math.min(Math.max(t, 0), 1); 1215 t_clamped_co = 1 - t_clamped; 1216 return [ 1217 1, 1218 t_clamped_co * end0[0] + t_clamped * end1[0], 1219 t_clamped_co * end0[1] + t_clamped * end1[1], 1220 t_clamped_co * end0[2] + t_clamped * end1[2] 1221 ]; 1222 }, 1223 1224 /** 1225 * Project a 2D coordinate to a new 3D position by keeping 1226 * the 3D x, y coordinates and changing only the z coordinate. 1227 * All horizontal moves of the 2D point are ignored. 1228 * 1229 * @param {JXG.Point} point2d 1230 * @param {Array} base_c3d 1231 * @returns {Array} of length 4 containing the projected 1232 * point in homogeneous coordinates. 1233 */ 1234 project2DTo3DVertical: function (point2d, base_c3d) { 1235 var pScr = point2d.coords.usrCoords.slice(1, 3), 1236 end0 = [base_c3d[1], base_c3d[2], this.bbox3D[2][0]], 1237 end1 = [base_c3d[1], base_c3d[2], this.bbox3D[2][1]]; 1238 1239 return this.projectScreenToSegment(pScr, end0, end1); 1240 }, 1241 1242 /** 1243 * Limit 3D coordinates to the bounding cube. 1244 * 1245 * @param {Array} c3d 3D coordinates [x,y,z] 1246 * @returns Array [Array, Boolean] containing [coords, corrected]. coords contains the updated 3D coordinates, 1247 * correct is true if the coords have been changed. 1248 */ 1249 project3DToCube: function (c3d) { 1250 var cube = this.bbox3D, 1251 isOut = false; 1252 1253 if (c3d[1] < cube[0][0]) { 1254 c3d[1] = cube[0][0]; 1255 isOut = true; 1256 } 1257 if (c3d[1] > cube[0][1]) { 1258 c3d[1] = cube[0][1]; 1259 isOut = true; 1260 } 1261 if (c3d[2] < cube[1][0]) { 1262 c3d[2] = cube[1][0]; 1263 isOut = true; 1264 } 1265 if (c3d[2] > cube[1][1]) { 1266 c3d[2] = cube[1][1]; 1267 isOut = true; 1268 } 1269 if (c3d[3] <= cube[2][0]) { 1270 c3d[3] = cube[2][0]; 1271 isOut = true; 1272 } 1273 if (c3d[3] >= cube[2][1]) { 1274 c3d[3] = cube[2][1]; 1275 isOut = true; 1276 } 1277 1278 return [c3d, isOut]; 1279 }, 1280 1281 /** 1282 * Intersect a ray with the bounding cube of the 3D view. 1283 * @param {Array} p 3D coordinates [w,x,y,z] 1284 * @param {Array} dir 3D direction vector of the line (array of length 3 or 4) 1285 * @param {Number} r direction of the ray (positive if r > 0, negative if r < 0). 1286 * @returns Affine ratio of the intersection of the line with the cube. 1287 */ 1288 intersectionLineCube: function (p, dir, r) { 1289 var r_n, i, r0, r1, d; 1290 1291 d = (dir.length === 3) ? dir : dir.slice(1); 1292 1293 r_n = r; 1294 for (i = 0; i < 3; i++) { 1295 if (d[i] !== 0) { 1296 r0 = (this.bbox3D[i][0] - p[i + 1]) / d[i]; 1297 r1 = (this.bbox3D[i][1] - p[i + 1]) / d[i]; 1298 if (r < 0) { 1299 r_n = Math.max(r_n, Math.min(r0, r1)); 1300 } else { 1301 r_n = Math.min(r_n, Math.max(r0, r1)); 1302 } 1303 } 1304 } 1305 return r_n; 1306 }, 1307 1308 /** 1309 * Test if coordinates are inside of the bounding cube. 1310 * @param {array} p 3D coordinates [[w],x,y,z] of a point. 1311 * @returns Boolean 1312 */ 1313 isInCube: function (p, polyhedron) { 1314 var q; 1315 if (p.length === 4) { 1316 if (p[0] === 0) { 1317 return false; 1318 } 1319 q = p.slice(1); 1320 } 1321 return ( 1322 q[0] > this.bbox3D[0][0] - Mat.eps && 1323 q[0] < this.bbox3D[0][1] + Mat.eps && 1324 q[1] > this.bbox3D[1][0] - Mat.eps && 1325 q[1] < this.bbox3D[1][1] + Mat.eps && 1326 q[2] > this.bbox3D[2][0] - Mat.eps && 1327 q[2] < this.bbox3D[2][1] + Mat.eps 1328 ); 1329 }, 1330 1331 /** 1332 * 1333 * @param {JXG.Plane3D} plane1 1334 * @param {JXG.Plane3D} plane2 1335 * @param {Number} d Right hand side of Hesse normal for plane2 (it can be adjusted) 1336 * @returns {Array} of length 2 containing the coordinates of the defining points of 1337 * of the intersection segment, or false if there is no intersection 1338 */ 1339 intersectionPlanePlane: function (plane1, plane2, d) { 1340 var ret = [false, false], 1341 p, q, r, w, 1342 dir; 1343 1344 d = d || plane2.d; 1345 1346 // Get one point of the intersection of the two planes 1347 w = Mat.crossProduct(plane1.normal.slice(1), plane2.normal.slice(1)); 1348 w.unshift(0); 1349 1350 p = Mat.Geometry.meet3Planes( 1351 plane1.normal, 1352 plane1.d, 1353 plane2.normal, 1354 d, 1355 w, 1356 0 1357 ); 1358 1359 // Get the direction of the intersecting line of the two planes 1360 dir = Mat.Geometry.meetPlanePlane( 1361 plane1.vec1, 1362 plane1.vec2, 1363 plane2.vec1, 1364 plane2.vec2 1365 ); 1366 1367 // Get the bounding points of the intersecting segment 1368 r = this.intersectionLineCube(p, dir, Infinity); 1369 q = Mat.axpy(r, dir, p); 1370 if (this.isInCube(q)) { 1371 ret[0] = q; 1372 } 1373 r = this.intersectionLineCube(p, dir, -Infinity); 1374 q = Mat.axpy(r, dir, p); 1375 if (this.isInCube(q)) { 1376 ret[1] = q; 1377 } 1378 1379 return ret; 1380 }, 1381 1382 intersectionPlaneFace: function (plane, face) { 1383 var ret = [], 1384 j, t, 1385 p, crds, 1386 p1, p2, c, 1387 f, le, x1, y1, x2, y2, 1388 dir, vec, w, 1389 mat = [], b = [], sol; 1390 1391 w = Mat.crossProduct(plane.normal.slice(1), face.normal.slice(1)); 1392 w.unshift(0); 1393 1394 // Get one point of the intersection of the two planes 1395 p = Geometry.meet3Planes( 1396 plane.normal, 1397 plane.d, 1398 face.normal, 1399 face.d, 1400 w, 1401 0 1402 ); 1403 1404 // Get the direction the intersecting line of the two planes 1405 dir = Geometry.meetPlanePlane( 1406 plane.vec1, 1407 plane.vec2, 1408 face.vec1, 1409 face.vec2 1410 ); 1411 1412 f = face.polyhedron.faces[face.faceNumber]; 1413 crds = face.polyhedron.coords; 1414 le = f.length; 1415 for (j = 1; j <= le; j++) { 1416 p1 = crds[f[j - 1]]; 1417 p2 = crds[f[j % le]]; 1418 vec = [0, p2[1] - p1[1], p2[2] - p1[2], p2[3] - p1[3]]; 1419 1420 x1 = Math.random(); 1421 y1 = Math.random(); 1422 x2 = Math.random(); 1423 y2 = Math.random(); 1424 mat = [ 1425 [x1 * dir[1] + y1 * dir[3], x1 * (-vec[1]) + y1 * (-vec[3])], 1426 [x2 * dir[2] + y2 * dir[3], x2 * (-vec[2]) + y2 * (-vec[3])] 1427 ]; 1428 b = [ 1429 x1 * (p1[1] - p[1]) + y1 * (p1[3] - p[3]), 1430 x2 * (p1[2] - p[2]) + y2 * (p1[3] - p[3]) 1431 ]; 1432 1433 sol = Numerics.Gauss(mat, b); 1434 t = sol[1]; 1435 if (t > -Mat.eps && t < 1 + Mat.eps) { 1436 c = [1, p1[1] + t * vec[1], p1[2] + t * vec[2], p1[3] + t * vec[3]]; 1437 ret.push(c); 1438 } 1439 } 1440 1441 return ret; 1442 }, 1443 1444 // TODO: 1445 // - handle non-closed polyhedra 1446 // - handle intersections in vertex, edge, plane 1447 intersectionPlanePolyhedron: function(plane, phdr) { 1448 var i, j, seg, 1449 p, first, pos, pos_akt, 1450 eps = 1e-12, 1451 points = [], 1452 x = [], 1453 y = [], 1454 z = []; 1455 1456 for (i = 0; i < phdr.numberFaces; i++) { 1457 if (phdr.def.faces[i].length < 3) { 1458 // We skip intersection with points or lines 1459 continue; 1460 } 1461 1462 // seg will be an array consisting of two points 1463 // that span the intersecting segment of the plane 1464 // and the face. 1465 seg = this.intersectionPlaneFace(plane, phdr.faces[i]); 1466 1467 // Plane intersects the face in less than 2 points 1468 if (seg.length < 2) { 1469 continue; 1470 } 1471 1472 if (seg[0].length === 4 && seg[1].length === 4) { 1473 // This test is necessary to filter out intersection lines which are 1474 // identical to intersections of axis planes (they would occur twice), 1475 // i.e. edges of bbox3d. 1476 for (j = 0; j < points.length; j++) { 1477 if ( 1478 (Geometry.distance(seg[0], points[j][0], 4) < eps && 1479 Geometry.distance(seg[1], points[j][1], 4) < eps) || 1480 (Geometry.distance(seg[0], points[j][1], 4) < eps && 1481 Geometry.distance(seg[1], points[j][0], 4) < eps) 1482 ) { 1483 break; 1484 } 1485 } 1486 if (j === points.length) { 1487 points.push(seg.slice()); 1488 } 1489 } 1490 } 1491 1492 // Handle the case that the intersection is the empty set. 1493 if (points.length === 0) { 1494 return { X: x, Y: y, Z: z }; 1495 } 1496 1497 // Concatenate the intersection points to a polygon. 1498 // If all went well, each intersection should appear 1499 // twice in the list. 1500 // __Attention:__ each face has to be planar!!! 1501 // Otherwise the algorithm will fail. 1502 first = 0; 1503 pos = first; 1504 i = 0; 1505 do { 1506 p = points[pos][i]; 1507 if (p.length === 4) { 1508 x.push(p[1]); 1509 y.push(p[2]); 1510 z.push(p[3]); 1511 } 1512 i = (i + 1) % 2; 1513 p = points[pos][i]; 1514 1515 pos_akt = pos; 1516 for (j = 0; j < points.length; j++) { 1517 if (j !== pos && Geometry.distance(p, points[j][0]) < eps) { 1518 pos = j; 1519 i = 0; 1520 break; 1521 } 1522 if (j !== pos && Geometry.distance(p, points[j][1]) < eps) { 1523 pos = j; 1524 i = 1; 1525 break; 1526 } 1527 } 1528 if (pos === pos_akt) { 1529 console.log('Error face3d intersection update: did not find next', pos, i); 1530 break; 1531 } 1532 } while (pos !== first); 1533 x.push(x[0]); 1534 y.push(y[0]); 1535 z.push(z[0]); 1536 1537 return { X: x, Y: y, Z: z }; 1538 }, 1539 1540 /** 1541 * Generate mesh for a surface / plane. 1542 * Returns array [dataX, dataY] for a JSXGraph curve's updateDataArray function. 1543 * @param {Array|Function} func 1544 * @param {Array} interval_u 1545 * @param {Array} interval_v 1546 * @returns Array 1547 * @private 1548 * 1549 * @example 1550 * var el = view.create('curve', [[], []]); 1551 * el.updateDataArray = function () { 1552 * var steps_u = this.evalVisProp('stepsu'), 1553 * steps_v = this.evalVisProp('stepsv'), 1554 * r_u = Type.evaluate(this.range_u), 1555 * r_v = Type.evaluate(this.range_v), 1556 * func, ret; 1557 * 1558 * if (this.F !== null) { 1559 * func = this.F; 1560 * } else { 1561 * func = [this.X, this.Y, this.Z]; 1562 * } 1563 * ret = this.view.getMesh(func, 1564 * r_u.concat([steps_u]), 1565 * r_v.concat([steps_v])); 1566 * 1567 * this.dataX = ret[0]; 1568 * this.dataY = ret[1]; 1569 * }; 1570 * 1571 */ 1572 getMesh: function (func, interval_u, interval_v) { 1573 var i_u, i_v, u, v, 1574 c2d, delta_u, delta_v, 1575 p = [0, 0, 0], 1576 steps_u = Type.evaluate(interval_u[2]), 1577 steps_v = Type.evaluate(interval_v[2]), 1578 dataX = [], 1579 dataY = []; 1580 1581 delta_u = (Type.evaluate(interval_u[1]) - Type.evaluate(interval_u[0])) / steps_u; 1582 delta_v = (Type.evaluate(interval_v[1]) - Type.evaluate(interval_v[0])) / steps_v; 1583 1584 for (i_u = 0; i_u <= steps_u; i_u++) { 1585 u = interval_u[0] + delta_u * i_u; 1586 for (i_v = 0; i_v <= steps_v; i_v++) { 1587 v = interval_v[0] + delta_v * i_v; 1588 if (Type.isFunction(func)) { 1589 p = func(u, v); 1590 } else { 1591 p = [func[0](u, v), func[1](u, v), func[2](u, v)]; 1592 } 1593 c2d = this.project3DTo2D(p); 1594 dataX.push(c2d[1]); 1595 dataY.push(c2d[2]); 1596 } 1597 dataX.push(NaN); 1598 dataY.push(NaN); 1599 } 1600 1601 for (i_v = 0; i_v <= steps_v; i_v++) { 1602 v = interval_v[0] + delta_v * i_v; 1603 for (i_u = 0; i_u <= steps_u; i_u++) { 1604 u = interval_u[0] + delta_u * i_u; 1605 if (Type.isFunction(func)) { 1606 p = func(u, v); 1607 } else { 1608 p = [func[0](u, v), func[1](u, v), func[2](u, v)]; 1609 } 1610 c2d = this.project3DTo2D(p); 1611 dataX.push(c2d[1]); 1612 dataY.push(c2d[2]); 1613 } 1614 dataX.push(NaN); 1615 dataY.push(NaN); 1616 } 1617 1618 return [dataX, dataY]; 1619 }, 1620 1621 /** 1622 * 1623 */ 1624 animateAzimuth: function () { 1625 var s = this.az_slide._smin, 1626 e = this.az_slide._smax, 1627 sdiff = e - s, 1628 newVal = this.az_slide.Value() + 0.1; 1629 1630 this.az_slide.position = (newVal - s) / sdiff; 1631 if (this.az_slide.position > 1) { 1632 this.az_slide.position = 0.0; 1633 } 1634 this.board._change3DView = true; 1635 this.board.update(); 1636 this.board._change3DView = false; 1637 1638 this.timeoutAzimuth = setTimeout(function () { 1639 this.animateAzimuth(); 1640 }.bind(this), 200); 1641 }, 1642 1643 /** 1644 * 1645 */ 1646 stopAzimuth: function () { 1647 clearTimeout(this.timeoutAzimuth); 1648 this.timeoutAzimuth = null; 1649 }, 1650 1651 /** 1652 * Check if vertical dragging is enabled and which action is needed. 1653 * Default is shiftKey. 1654 * 1655 * @returns Boolean 1656 * @private 1657 */ 1658 isVerticalDrag: function () { 1659 var b = this.board, 1660 key; 1661 if (!this.evalVisProp('verticaldrag.enabled')) { 1662 return false; 1663 } 1664 key = '_' + this.evalVisProp('verticaldrag.key') + 'Key'; 1665 return b[key]; 1666 }, 1667 1668 /** 1669 * Sets camera view to the given values. 1670 * 1671 * @param {Number} az Value of azimuth. 1672 * @param {Number} el Value of elevation. 1673 * @param {Number} [r] Value of radius. 1674 * 1675 * @returns {Object} Reference to the view. 1676 */ 1677 setView: function (az, el, r) { 1678 r = r || this.r; 1679 1680 this.az_slide.setValue(az); 1681 this.el_slide.setValue(el); 1682 this.r = r; 1683 this.board.update(); 1684 1685 return this; 1686 }, 1687 1688 /** 1689 * Changes view to the next view stored in the attribute `values`. 1690 * 1691 * @see View3D#values 1692 * 1693 * @returns {Object} Reference to the view. 1694 */ 1695 nextView: function () { 1696 var views = this.evalVisProp('values'), 1697 n = this.visProp._currentview; 1698 1699 n = (n + 1) % views.length; 1700 this.setCurrentView(n); 1701 1702 return this; 1703 }, 1704 1705 /** 1706 * Changes view to the previous view stored in the attribute `values`. 1707 * 1708 * @see View3D#values 1709 * 1710 * @returns {Object} Reference to the view. 1711 */ 1712 previousView: function () { 1713 var views = this.evalVisProp('values'), 1714 n = this.visProp._currentview; 1715 1716 n = (n + views.length - 1) % views.length; 1717 this.setCurrentView(n); 1718 1719 return this; 1720 }, 1721 1722 /** 1723 * Changes view to the determined view stored in the attribute `values`. 1724 * 1725 * @see View3D#values 1726 * 1727 * @param {Number} n Index of view in attribute `values`. 1728 * @returns {Object} Reference to the view. 1729 */ 1730 setCurrentView: function (n) { 1731 var views = this.evalVisProp('values'); 1732 1733 if (n < 0 || n >= views.length) { 1734 n = ((n % views.length) + views.length) % views.length; 1735 } 1736 1737 this.setView(views[n][0], views[n][1], views[n][2]); 1738 this.visProp._currentview = n; 1739 1740 return this; 1741 }, 1742 1743 /** 1744 * Controls the navigation in az direction using either the keyboard or a pointer. 1745 * 1746 * @private 1747 * 1748 * @param {event} evt either the keydown or the pointer event 1749 * @returns view 1750 */ 1751 _azEventHandler: function (evt) { 1752 var smax = this.az_slide._smax, 1753 smin = this.az_slide._smin, 1754 speed = (smax - smin) / this.board.canvasWidth * (this.evalVisProp('az.pointer.speed')), 1755 delta = evt.movementX, 1756 az = this.az_slide.Value(), 1757 el = this.el_slide.Value(); 1758 1759 // Doesn't allow navigation if another moving event is triggered 1760 if (this.board.mode === this.board.BOARD_MODE_DRAG) { 1761 return this; 1762 } 1763 1764 // Calculate new az value if keyboard events are triggered 1765 // Plus if right-button, minus if left-button 1766 if (this.evalVisProp('az.keyboard.enabled')) { 1767 if (evt.key === 'ArrowRight') { 1768 az = az + this.evalVisProp('az.keyboard.step') * Math.PI / 180; 1769 } else if (evt.key === 'ArrowLeft') { 1770 az = az - this.evalVisProp('az.keyboard.step') * Math.PI / 180; 1771 } 1772 } 1773 1774 if (this.evalVisProp('az.pointer.enabled') && (delta !== 0) && evt.key == null) { 1775 az += delta * speed; 1776 } 1777 1778 // Project the calculated az value to a usable value in the interval [smin,smax] 1779 // Use modulo if continuous is true 1780 if (this.evalVisProp('az.continuous')) { 1781 az = Mat.wrap(az, smin, smax); 1782 } else { 1783 if (az > 0) { 1784 az = Math.min(smax, az); 1785 } else if (az < 0) { 1786 az = Math.max(smin, az); 1787 } 1788 } 1789 1790 this.setView(az, el); 1791 return this; 1792 }, 1793 1794 /** 1795 * Controls the navigation in el direction using either the keyboard or a pointer. 1796 * 1797 * @private 1798 * 1799 * @param {event} evt either the keydown or the pointer event 1800 * @returns view 1801 */ 1802 _elEventHandler: function (evt) { 1803 var smax = this.el_slide._smax, 1804 smin = this.el_slide._smin, 1805 speed = (smax - smin) / this.board.canvasHeight * this.evalVisProp('el.pointer.speed'), 1806 delta = evt.movementY, 1807 az = this.az_slide.Value(), 1808 el = this.el_slide.Value(); 1809 1810 // Doesn't allow navigation if another moving event is triggered 1811 if (this.board.mode === this.board.BOARD_MODE_DRAG) { 1812 return this; 1813 } 1814 1815 // Calculate new az value if keyboard events are triggered 1816 // Plus if down-button, minus if up-button 1817 if (this.evalVisProp('el.keyboard.enabled')) { 1818 if (evt.key === 'ArrowUp') { 1819 el = el - this.evalVisProp('el.keyboard.step') * Math.PI / 180; 1820 } else if (evt.key === 'ArrowDown') { 1821 el = el + this.evalVisProp('el.keyboard.step') * Math.PI / 180; 1822 } 1823 } 1824 1825 if (this.evalVisProp('el.pointer.enabled') && (delta !== 0) && evt.key == null) { 1826 el += delta * speed; 1827 } 1828 1829 // Project the calculated el value to a usable value in the interval [smin,smax] 1830 // Use modulo if continuous is true and the trackball is disabled 1831 if (this.evalVisProp('el.continuous') && !this.trackballEnabled) { 1832 el = Mat.wrap(el, smin, smax); 1833 } else { 1834 if (el > 0) { 1835 el = Math.min(smax, el); 1836 } else if (el < 0) { 1837 el = Math.max(smin, el); 1838 } 1839 } 1840 1841 this.setView(az, el); 1842 1843 return this; 1844 }, 1845 1846 /** 1847 * Controls the navigation in bank direction using either the keyboard or a pointer. 1848 * 1849 * @private 1850 * 1851 * @param {event} evt either the keydown or the pointer event 1852 * @returns view 1853 */ 1854 _bankEventHandler: function (evt) { 1855 var smax = this.bank_slide._smax, 1856 smin = this.bank_slide._smin, 1857 step, speed, 1858 delta = evt.deltaY, 1859 bank = this.bank_slide.Value(); 1860 1861 // Doesn't allow navigation if another moving event is triggered 1862 if (this.board.mode === this.board.BOARD_MODE_DRAG) { 1863 return this; 1864 } 1865 1866 // Calculate new bank value if keyboard events are triggered 1867 // Plus if down-button, minus if up-button 1868 if (this.evalVisProp('bank.keyboard.enabled')) { 1869 step = this.evalVisProp('bank.keyboard.step') * Math.PI / 180; 1870 if (evt.key === '.' || evt.key === '<') { 1871 bank -= step; 1872 } else if (evt.key === ',' || evt.key === '>') { 1873 bank += step; 1874 } 1875 } 1876 1877 if (this.evalVisProp('bank.pointer.enabled') && (delta !== 0) && evt.key == null) { 1878 speed = (smax - smin) / this.board.canvasHeight * this.evalVisProp('bank.pointer.speed'); 1879 bank += delta * speed; 1880 1881 // prevent the pointer wheel from scrolling the page 1882 evt.preventDefault(); 1883 } 1884 1885 // Project the calculated bank value to a usable value in the interval [smin,smax] 1886 if (this.evalVisProp('bank.continuous')) { 1887 // in continuous mode, wrap value around slider range 1888 bank = Mat.wrap(bank, smin, smax); 1889 } else { 1890 // in non-continuous mode, clamp value to slider range 1891 bank = Mat.clamp(bank, smin, smax); 1892 } 1893 1894 this.bank_slide.setValue(bank); 1895 this.board.update(); 1896 return this; 1897 }, 1898 1899 /** 1900 * Controls the navigation using either virtual trackball. 1901 * 1902 * @private 1903 * 1904 * @param {event} evt either the keydown or the pointer event 1905 * @returns view 1906 */ 1907 _trackballHandler: function (evt) { 1908 var pos = this.board.getMousePosition(evt), 1909 x, y, center; 1910 1911 center = new Coords(Const.COORDS_BY_USER, [this.llftCorner[0] + this.size[0] * 0.5, this.llftCorner[1] + this.size[1] * 0.5], this.board); 1912 x = pos[0] - center.scrCoords[1]; 1913 y = pos[1] - center.scrCoords[2]; 1914 this._trackball = { 1915 dx: evt.movementX, 1916 dy: -evt.movementY, 1917 x: x, 1918 y: -y 1919 }; 1920 this.board.update(); 1921 return this; 1922 }, 1923 1924 /** 1925 * Event handler for pointer down event. Triggers handling of all 3D navigation. 1926 * 1927 * @private 1928 * @param {event} evt 1929 * @returns view 1930 */ 1931 pointerDownHandler: function (evt) { 1932 var neededButton, neededKey, target; 1933 1934 this._hasMoveAz = false; 1935 this._hasMoveEl = false; 1936 this._hasMoveBank = false; 1937 this._hasMoveTrackball = false; 1938 1939 if (this.board.mode !== this.board.BOARD_MODE_NONE) { 1940 return; 1941 } 1942 1943 this.board._change3DView = true; 1944 1945 if (this.evalVisProp('trackball.enabled')) { 1946 neededButton = this.evalVisProp('trackball.button'); 1947 neededKey = this.evalVisProp('trackball.key'); 1948 1949 // Move events for virtual trackball 1950 if ( 1951 (neededButton === -1 || neededButton === evt.button) && 1952 (neededKey === 'none' || (neededKey.indexOf('shift') > -1 && evt.shiftKey) || (neededKey.indexOf('ctrl') > -1 && evt.ctrlKey)) 1953 ) { 1954 // If outside is true then the event listener is bound to the document, otherwise to the div 1955 target = (this.evalVisProp('trackball.outside')) ? document : this.board.containerObj; 1956 Env.addEvent(target, 'pointermove', this._trackballHandler, this); 1957 this._hasMoveTrackball = true; 1958 } 1959 } else { 1960 if (this.evalVisProp('az.pointer.enabled')) { 1961 neededButton = this.evalVisProp('az.pointer.button'); 1962 neededKey = this.evalVisProp('az.pointer.key'); 1963 1964 // Move events for azimuth 1965 if ( 1966 (neededButton === -1 || neededButton === evt.button) && 1967 (neededKey === 'none' || (neededKey.indexOf('shift') > -1 && evt.shiftKey) || (neededKey.indexOf('ctrl') > -1 && evt.ctrlKey)) 1968 ) { 1969 // If outside is true then the event listener is bound to the document, otherwise to the div 1970 target = (this.evalVisProp('az.pointer.outside')) ? document : this.board.containerObj; 1971 Env.addEvent(target, 'pointermove', this._azEventHandler, this); 1972 this._hasMoveAz = true; 1973 } 1974 } 1975 1976 if (this.evalVisProp('el.pointer.enabled')) { 1977 neededButton = this.evalVisProp('el.pointer.button'); 1978 neededKey = this.evalVisProp('el.pointer.key'); 1979 1980 // Events for elevation 1981 if ( 1982 (neededButton === -1 || neededButton === evt.button) && 1983 (neededKey === 'none' || (neededKey.indexOf('shift') > -1 && evt.shiftKey) || (neededKey.indexOf('ctrl') > -1 && evt.ctrlKey)) 1984 ) { 1985 // If outside is true then the event listener is bound to the document, otherwise to the div 1986 target = (this.evalVisProp('el.pointer.outside')) ? document : this.board.containerObj; 1987 Env.addEvent(target, 'pointermove', this._elEventHandler, this); 1988 this._hasMoveEl = true; 1989 } 1990 } 1991 1992 if (this.evalVisProp('bank.pointer.enabled')) { 1993 neededButton = this.evalVisProp('bank.pointer.button'); 1994 neededKey = this.evalVisProp('bank.pointer.key'); 1995 1996 // Events for bank 1997 if ( 1998 (neededButton === -1 || neededButton === evt.button) && 1999 (neededKey === 'none' || (neededKey.indexOf('shift') > -1 && evt.shiftKey) || (neededKey.indexOf('ctrl') > -1 && evt.ctrlKey)) 2000 ) { 2001 // If `outside` is true, we bind the event listener to 2002 // the document. otherwise, we bind it to the div. we 2003 // register the event listener as active so it can 2004 // prevent the pointer wheel from scrolling the page 2005 target = (this.evalVisProp('bank.pointer.outside')) ? document : this.board.containerObj; 2006 Env.addEvent(target, 'wheel', this._bankEventHandler, this, { passive: false }); 2007 this._hasMoveBank = true; 2008 } 2009 } 2010 } 2011 Env.addEvent(document, 'pointerup', this.pointerUpHandler, this); 2012 }, 2013 2014 /** 2015 * Event handler for pointer up event. Triggers handling of all 3D navigation. 2016 * 2017 * @private 2018 * @param {event} evt 2019 * @returns view 2020 */ 2021 pointerUpHandler: function (evt) { 2022 var target; 2023 2024 if (this._hasMoveAz) { 2025 target = (this.evalVisProp('az.pointer.outside')) ? document : this.board.containerObj; 2026 Env.removeEvent(target, 'pointermove', this._azEventHandler, this); 2027 this._hasMoveAz = false; 2028 } 2029 if (this._hasMoveEl) { 2030 target = (this.evalVisProp('el.pointer.outside')) ? document : this.board.containerObj; 2031 Env.removeEvent(target, 'pointermove', this._elEventHandler, this); 2032 this._hasMoveEl = false; 2033 } 2034 if (this._hasMoveBank) { 2035 target = (this.evalVisProp('bank.pointer.outside')) ? document : this.board.containerObj; 2036 Env.removeEvent(target, 'wheel', this._bankEventHandler, this); 2037 this._hasMoveBank = false; 2038 } 2039 if (this._hasMoveTrackball) { 2040 target = (this.evalVisProp('trackball.outside')) ? document : this.board.containerObj; 2041 Env.removeEvent(target, 'pointermove', this._trackballHandler, this); 2042 this._hasMoveTrackball = false; 2043 } 2044 Env.removeEvent(document, 'pointerup', this.pointerUpHandler, this); 2045 this.board._change3DView = false; 2046 2047 } 2048 }); 2049 2050 /** 2051 * @class A View3D element provides the container and the methods to create and display 3D elements. 2052 * @pseudo 2053 * @description A View3D element provides the container and the methods to create and display 3D elements. 2054 * It is contained in a JSXGraph board. 2055 * <p> 2056 * It is advisable to disable panning of the board by setting the board attribute "pan": 2057 * <pre> 2058 * pan: {enabled: false} 2059 * </pre> 2060 * Otherwise users will not be able to rotate the scene with their fingers on a touch device. 2061 * <p> 2062 * The start position of the camera can be adjusted by the attributes {@link View3D#az}, {@link View3D#el}, and {@link View3D#bank}. 2063 * 2064 * @name View3D 2065 * @augments JXG.View3D 2066 * @constructor 2067 * @type Object 2068 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 2069 * @param {Array_Array_Array} lower,dim,cube Here, lower is an array of the form [x, y] and 2070 * dim is an array of the form [w, h]. 2071 * The arrays [x, y] and [w, h] define the 2D frame into which the 3D cube is 2072 * (roughly) projected. If the view's azimuth=0 and elevation=0, the 3D view will cover a rectangle with lower left corner 2073 * [x,y] and side lengths [w, h] of the board. 2074 * The array 'cube' is of the form [[x1, x2], [y1, y2], [z1, z2]] 2075 * which determines the coordinate ranges of the 3D cube. 2076 * 2077 * @example 2078 * var bound = [-4, 6]; 2079 * var view = board.create('view3d', 2080 * [[-4, -3], [8, 8], 2081 * [bound, bound, bound]], 2082 * { 2083 * projection: 'parallel', 2084 * trackball: {enabled:true}, 2085 * }); 2086 * 2087 * var curve = view.create('curve3d', [ 2088 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2089 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2090 * (t) => Math.sin(3 * t), 2091 * [-Math.PI, Math.PI] 2092 * ], { strokeWidth: 4 }); 2093 * 2094 * </pre><div id="JXG9b327a6c-1bd6-4e40-a502-59d024dbfd1b" class="jxgbox" style="width: 300px; height: 300px;"></div> 2095 * <script type="text/javascript"> 2096 * (function() { 2097 * var board = JXG.JSXGraph.initBoard('JXG9b327a6c-1bd6-4e40-a502-59d024dbfd1b', 2098 * {boundingbox: [-8, 8, 8,-8], pan: {enabled: false}, axis: false, showcopyright: false, shownavigation: false}); 2099 * var bound = [-4, 6]; 2100 * var view = board.create('view3d', 2101 * [[-4, -3], [8, 8], 2102 * [bound, bound, bound]], 2103 * { 2104 * projection: 'parallel', 2105 * trackball: {enabled:true}, 2106 * }); 2107 * 2108 * var curve = view.create('curve3d', [ 2109 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2110 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2111 * (t) => Math.sin(3 * t), 2112 * [-Math.PI, Math.PI] 2113 * ], { strokeWidth: 4 }); 2114 * 2115 * })(); 2116 * 2117 * </script><pre> 2118 * 2119 * @example 2120 * var bound = [-4, 6]; 2121 * var view = board.create('view3d', 2122 * [[-4, -3], [8, 8], 2123 * [bound, bound, bound]], 2124 * { 2125 * projection: 'central', 2126 * trackball: {enabled:true}, 2127 * 2128 * xPlaneRear: { visible: false }, 2129 * yPlaneRear: { visible: false } 2130 * 2131 * }); 2132 * 2133 * var curve = view.create('curve3d', [ 2134 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2135 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2136 * (t) => Math.sin(3 * t), 2137 * [-Math.PI, Math.PI] 2138 * ], { strokeWidth: 4 }); 2139 * 2140 * </pre><div id="JXG0dc2493d-fb2f-40d5-bdb8-762ba0ad2007" class="jxgbox" style="width: 300px; height: 300px;"></div> 2141 * <script type="text/javascript"> 2142 * (function() { 2143 * var board = JXG.JSXGraph.initBoard('JXG0dc2493d-fb2f-40d5-bdb8-762ba0ad2007', 2144 * {boundingbox: [-8, 8, 8,-8], axis: false, pan: {enabled: false}, showcopyright: false, shownavigation: false}); 2145 * var bound = [-4, 6]; 2146 * var view = board.create('view3d', 2147 * [[-4, -3], [8, 8], 2148 * [bound, bound, bound]], 2149 * { 2150 * projection: 'central', 2151 * trackball: {enabled:true}, 2152 * 2153 * xPlaneRear: { visible: false }, 2154 * yPlaneRear: { visible: false } 2155 * 2156 * }); 2157 * 2158 * var curve = view.create('curve3d', [ 2159 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2160 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2161 * (t) => Math.sin(3 * t), 2162 * [-Math.PI, Math.PI] 2163 * ], { strokeWidth: 4 }); 2164 * 2165 * })(); 2166 * 2167 * </script><pre> 2168 * 2169 * @example 2170 * var bound = [-4, 6]; 2171 * var view = board.create('view3d', 2172 * [[-4, -3], [8, 8], 2173 * [bound, bound, bound]], 2174 * { 2175 * projection: 'central', 2176 * trackball: {enabled:true}, 2177 * 2178 * // Main axes 2179 * axesPosition: 'border', 2180 * 2181 * // Axes at the border 2182 * xAxisBorder: { ticks3d: { ticksDistance: 2} }, 2183 * yAxisBorder: { ticks3d: { ticksDistance: 2} }, 2184 * zAxisBorder: { ticks3d: { ticksDistance: 2} }, 2185 * 2186 * // No axes on planes 2187 * xPlaneRearYAxis: {visible: false}, 2188 * xPlaneRearZAxis: {visible: false}, 2189 * yPlaneRearXAxis: {visible: false}, 2190 * yPlaneRearZAxis: {visible: false}, 2191 * zPlaneRearXAxis: {visible: false}, 2192 * zPlaneRearYAxis: {visible: false} 2193 * }); 2194 * 2195 * var curve = view.create('curve3d', [ 2196 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2197 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2198 * (t) => Math.sin(3 * t), 2199 * [-Math.PI, Math.PI] 2200 * ], { strokeWidth: 4 }); 2201 * 2202 * </pre><div id="JXG586f3551-335c-47e9-8d72-835409f6a103" class="jxgbox" style="width: 300px; height: 300px;"></div> 2203 * <script type="text/javascript"> 2204 * (function() { 2205 * var board = JXG.JSXGraph.initBoard('JXG586f3551-335c-47e9-8d72-835409f6a103', 2206 * {boundingbox: [-8, 8, 8,-8], axis: false, pan: {enabled: false}, showcopyright: false, shownavigation: false}); 2207 * var bound = [-4, 6]; 2208 * var view = board.create('view3d', 2209 * [[-4, -3], [8, 8], 2210 * [bound, bound, bound]], 2211 * { 2212 * projection: 'central', 2213 * trackball: {enabled:true}, 2214 * 2215 * // Main axes 2216 * axesPosition: 'border', 2217 * 2218 * // Axes at the border 2219 * xAxisBorder: { ticks3d: { ticksDistance: 2} }, 2220 * yAxisBorder: { ticks3d: { ticksDistance: 2} }, 2221 * zAxisBorder: { ticks3d: { ticksDistance: 2} }, 2222 * 2223 * // No axes on planes 2224 * xPlaneRearYAxis: {visible: false}, 2225 * xPlaneRearZAxis: {visible: false}, 2226 * yPlaneRearXAxis: {visible: false}, 2227 * yPlaneRearZAxis: {visible: false}, 2228 * zPlaneRearXAxis: {visible: false}, 2229 * zPlaneRearYAxis: {visible: false} 2230 * }); 2231 * 2232 * var curve = view.create('curve3d', [ 2233 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2234 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2235 * (t) => Math.sin(3 * t), 2236 * [-Math.PI, Math.PI] 2237 * ], { strokeWidth: 4 }); 2238 * 2239 * })(); 2240 * 2241 * </script><pre> 2242 * 2243 * @example 2244 * var bound = [-4, 6]; 2245 * var view = board.create('view3d', 2246 * [[-4, -3], [8, 8], 2247 * [bound, bound, bound]], 2248 * { 2249 * projection: 'central', 2250 * trackball: {enabled:true}, 2251 * 2252 * axesPosition: 'none' 2253 * }); 2254 * 2255 * var curve = view.create('curve3d', [ 2256 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2257 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2258 * (t) => Math.sin(3 * t), 2259 * [-Math.PI, Math.PI] 2260 * ], { strokeWidth: 4 }); 2261 * 2262 * </pre><div id="JXG9a9467e1-f189-4c8c-adb2-d4f49bc7fa26" class="jxgbox" style="width: 300px; height: 300px;"></div> 2263 * <script type="text/javascript"> 2264 * (function() { 2265 * var board = JXG.JSXGraph.initBoard('JXG9a9467e1-f189-4c8c-adb2-d4f49bc7fa26', 2266 * {boundingbox: [-8, 8, 8,-8], axis: false, pan: {enabled: false}, showcopyright: false, shownavigation: false}); 2267 * var bound = [-4, 6]; 2268 * var view = board.create('view3d', 2269 * [[-4, -3], [8, 8], 2270 * [bound, bound, bound]], 2271 * { 2272 * projection: 'central', 2273 * trackball: {enabled:true}, 2274 * 2275 * axesPosition: 'none' 2276 * }); 2277 * 2278 * var curve = view.create('curve3d', [ 2279 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2280 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2281 * (t) => Math.sin(3 * t), 2282 * [-Math.PI, Math.PI] 2283 * ], { strokeWidth: 4 }); 2284 * 2285 * })(); 2286 * 2287 * </script><pre> 2288 * 2289 * @example 2290 * var bound = [-4, 6]; 2291 * var view = board.create('view3d', 2292 * [[-4, -3], [8, 8], 2293 * [bound, bound, bound]], 2294 * { 2295 * projection: 'central', 2296 * trackball: {enabled:true}, 2297 * 2298 * // Main axes 2299 * axesPosition: 'border', 2300 * 2301 * // Axes at the border 2302 * xAxisBorder: { ticks3d: { ticksDistance: 2} }, 2303 * yAxisBorder: { ticks3d: { ticksDistance: 2} }, 2304 * zAxisBorder: { ticks3d: { ticksDistance: 2} }, 2305 * 2306 * xPlaneRear: { 2307 * fillColor: '#fff', 2308 * mesh3d: {visible: false} 2309 * }, 2310 * yPlaneRear: { 2311 * fillColor: '#fff', 2312 * mesh3d: {visible: false} 2313 * }, 2314 * zPlaneRear: { 2315 * fillColor: '#fff', 2316 * mesh3d: {visible: false} 2317 * }, 2318 * xPlaneFront: { 2319 * visible: true, 2320 * fillColor: '#fff', 2321 * mesh3d: {visible: false} 2322 * }, 2323 * yPlaneFront: { 2324 * visible: true, 2325 * fillColor: '#fff', 2326 * mesh3d: {visible: false} 2327 * }, 2328 * zPlaneFront: { 2329 * visible: true, 2330 * fillColor: '#fff', 2331 * mesh3d: {visible: false} 2332 * }, 2333 * 2334 * // No axes on planes 2335 * xPlaneRearYAxis: {visible: false}, 2336 * xPlaneRearZAxis: {visible: false}, 2337 * yPlaneRearXAxis: {visible: false}, 2338 * yPlaneRearZAxis: {visible: false}, 2339 * zPlaneRearXAxis: {visible: false}, 2340 * zPlaneRearYAxis: {visible: false}, 2341 * xPlaneFrontYAxis: {visible: false}, 2342 * xPlaneFrontZAxis: {visible: false}, 2343 * yPlaneFrontXAxis: {visible: false}, 2344 * yPlaneFrontZAxis: {visible: false}, 2345 * zPlaneFrontXAxis: {visible: false}, 2346 * zPlaneFrontYAxis: {visible: false} 2347 * 2348 * }); 2349 * 2350 * var curve = view.create('curve3d', [ 2351 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2352 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2353 * (t) => Math.sin(3 * t), 2354 * [-Math.PI, Math.PI] 2355 * ], { strokeWidth: 4 }); 2356 * 2357 * </pre><div id="JXGbd41a4e3-1bf7-4764-b675-98b01667103b" class="jxgbox" style="width: 300px; height: 300px;"></div> 2358 * <script type="text/javascript"> 2359 * (function() { 2360 * var board = JXG.JSXGraph.initBoard('JXGbd41a4e3-1bf7-4764-b675-98b01667103b', 2361 * {boundingbox: [-8, 8, 8,-8], axis: false, pan: {enabled: false}, showcopyright: false, shownavigation: false}); 2362 * var bound = [-4, 6]; 2363 * var view = board.create('view3d', 2364 * [[-4, -3], [8, 8], 2365 * [bound, bound, bound]], 2366 * { 2367 * projection: 'central', 2368 * trackball: {enabled:true}, 2369 * 2370 * // Main axes 2371 * axesPosition: 'border', 2372 * 2373 * // Axes at the border 2374 * xAxisBorder: { ticks3d: { ticksDistance: 2} }, 2375 * yAxisBorder: { ticks3d: { ticksDistance: 2} }, 2376 * zAxisBorder: { ticks3d: { ticksDistance: 2} }, 2377 * 2378 * xPlaneRear: { 2379 * fillColor: '#fff', 2380 * mesh3d: {visible: false} 2381 * }, 2382 * yPlaneRear: { 2383 * fillColor: '#fff', 2384 * mesh3d: {visible: false} 2385 * }, 2386 * zPlaneRear: { 2387 * fillColor: '#fff', 2388 * mesh3d: {visible: false} 2389 * }, 2390 * xPlaneFront: { 2391 * visible: true, 2392 * fillColor: '#fff', 2393 * mesh3d: {visible: false} 2394 * }, 2395 * yPlaneFront: { 2396 * visible: true, 2397 * fillColor: '#fff', 2398 * mesh3d: {visible: false} 2399 * }, 2400 * zPlaneFront: { 2401 * visible: true, 2402 * fillColor: '#fff', 2403 * mesh3d: {visible: false} 2404 * }, 2405 * 2406 * // No axes on planes 2407 * xPlaneRearYAxis: {visible: false}, 2408 * xPlaneRearZAxis: {visible: false}, 2409 * yPlaneRearXAxis: {visible: false}, 2410 * yPlaneRearZAxis: {visible: false}, 2411 * zPlaneRearXAxis: {visible: false}, 2412 * zPlaneRearYAxis: {visible: false}, 2413 * xPlaneFrontYAxis: {visible: false}, 2414 * xPlaneFrontZAxis: {visible: false}, 2415 * yPlaneFrontXAxis: {visible: false}, 2416 * yPlaneFrontZAxis: {visible: false}, 2417 * zPlaneFrontXAxis: {visible: false}, 2418 * zPlaneFrontYAxis: {visible: false} 2419 * 2420 * }); 2421 * 2422 * var curve = view.create('curve3d', [ 2423 * (t) => (2 + Math.cos(3 * t)) * Math.cos(2 * t), 2424 * (t) => (2 + Math.cos(3 * t)) * Math.sin(2 * t), 2425 * (t) => Math.sin(3 * t), 2426 * [-Math.PI, Math.PI] 2427 * ], { strokeWidth: 4 }); 2428 * })(); 2429 * 2430 * </script><pre> 2431 * 2432 * @example 2433 * var bound = [-5, 5]; 2434 * var view = board.create('view3d', 2435 * [[-6, -3], 2436 * [8, 8], 2437 * [bound, bound, bound]], 2438 * { 2439 * // Main axes 2440 * axesPosition: 'center', 2441 * xAxis: { strokeColor: 'blue', strokeWidth: 3}, 2442 * 2443 * // Planes 2444 * xPlaneRear: { fillColor: 'yellow', mesh3d: {visible: false}}, 2445 * yPlaneFront: { visible: true, fillColor: 'blue'}, 2446 * 2447 * // Axes on planes 2448 * xPlaneRearYAxis: {strokeColor: 'red'}, 2449 * xPlaneRearZAxis: {strokeColor: 'red'}, 2450 * 2451 * yPlaneFrontXAxis: {strokeColor: 'blue'}, 2452 * yPlaneFrontZAxis: {strokeColor: 'blue'}, 2453 * 2454 * zPlaneFrontXAxis: {visible: false}, 2455 * zPlaneFrontYAxis: {visible: false} 2456 * }); 2457 * 2458 * </pre><div id="JXGdd06d90e-be5d-4531-8f0b-65fc30b1a7c7" class="jxgbox" style="width: 500px; height: 500px;"></div> 2459 * <script type="text/javascript"> 2460 * (function() { 2461 * var board = JXG.JSXGraph.initBoard('JXGdd06d90e-be5d-4531-8f0b-65fc30b1a7c7', 2462 * {boundingbox: [-8, 8, 8,-8], axis: false, pan: {enabled: false}, showcopyright: false, shownavigation: false}); 2463 * var bound = [-5, 5]; 2464 * var view = board.create('view3d', 2465 * [[-6, -3], [8, 8], 2466 * [bound, bound, bound]], 2467 * { 2468 * // Main axes 2469 * axesPosition: 'center', 2470 * xAxis: { strokeColor: 'blue', strokeWidth: 3}, 2471 * // Planes 2472 * xPlaneRear: { fillColor: 'yellow', mesh3d: {visible: false}}, 2473 * yPlaneFront: { visible: true, fillColor: 'blue'}, 2474 * // Axes on planes 2475 * xPlaneRearYAxis: {strokeColor: 'red'}, 2476 * xPlaneRearZAxis: {strokeColor: 'red'}, 2477 * yPlaneFrontXAxis: {strokeColor: 'blue'}, 2478 * yPlaneFrontZAxis: {strokeColor: 'blue'}, 2479 * zPlaneFrontXAxis: {visible: false}, 2480 * zPlaneFrontYAxis: {visible: false} 2481 * }); 2482 * })(); 2483 * 2484 * </script><pre> 2485 * @example 2486 * var bound = [-5, 5]; 2487 * var view = board.create('view3d', 2488 * [[-6, -3], [8, 8], 2489 * [bound, bound, bound]], 2490 * { 2491 * projection: 'central', 2492 * az: { 2493 * slider: { 2494 * visible: true, 2495 * point1: { 2496 * pos: [5, -4] 2497 * }, 2498 * point2: { 2499 * pos: [5, 4] 2500 * }, 2501 * label: {anchorX: 'middle'} 2502 * } 2503 * }, 2504 * el: { 2505 * slider: { 2506 * visible: true, 2507 * point1: { 2508 * pos: [6, -5] 2509 * }, 2510 * point2: { 2511 * pos: [6, 3] 2512 * }, 2513 * label: {anchorX: 'middle'} 2514 * } 2515 * }, 2516 * bank: { 2517 * slider: { 2518 * visible: true, 2519 * point1: { 2520 * pos: [7, -6] 2521 * }, 2522 * point2: { 2523 * pos: [7, 2] 2524 * }, 2525 * label: {anchorX: 'middle'} 2526 * } 2527 * } 2528 * }); 2529 * 2530 * 2531 * </pre><div id="JXGe181cc55-271b-419b-84fd-622326fd1d1a" class="jxgbox" style="width: 300px; height: 300px;"></div> 2532 * <script type="text/javascript"> 2533 * (function() { 2534 * var board = JXG.JSXGraph.initBoard('JXGe181cc55-271b-419b-84fd-622326fd1d1a', 2535 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2536 * var bound = [-5, 5]; 2537 * var view = board.create('view3d', 2538 * [[-6, -3], [8, 8], 2539 * [bound, bound, bound]], 2540 * { 2541 * projection: 'central', 2542 * az: { 2543 * slider: { 2544 * visible: true, 2545 * point1: { 2546 * pos: [5, -4] 2547 * }, 2548 * point2: { 2549 * pos: [5, 4] 2550 * }, 2551 * label: {anchorX: 'middle'} 2552 * } 2553 * }, 2554 * el: { 2555 * slider: { 2556 * visible: true, 2557 * point1: { 2558 * pos: [6, -5] 2559 * }, 2560 * point2: { 2561 * pos: [6, 3] 2562 * }, 2563 * label: {anchorX: 'middle'} 2564 * } 2565 * }, 2566 * bank: { 2567 * slider: { 2568 * visible: true, 2569 * point1: { 2570 * pos: [7, -6] 2571 * }, 2572 * point2: { 2573 * pos: [7, 2] 2574 * }, 2575 * label: {anchorX: 'middle'} 2576 * } 2577 * } 2578 * }); 2579 * 2580 * 2581 * })(); 2582 * 2583 * </script><pre> 2584 * 2585 * 2586 */ 2587 JXG.createView3D = function (board, parents, attributes) { 2588 var view, attr, attr_az, attr_el, attr_bank, 2589 x, y, w, h, 2590 p1, p2, v, 2591 coords = parents[0], // llft corner 2592 size = parents[1]; // [w, h] 2593 2594 attr = Type.copyAttributes(attributes, board.options, 'view3d'); 2595 view = new JXG.View3D(board, parents, attr); 2596 view.defaultAxes = view.create('axes3d', [], attr); 2597 2598 x = coords[0]; 2599 y = coords[1]; 2600 w = size[0]; 2601 h = size[1]; 2602 2603 attr_az = Type.copyAttributes(attr, board.options, 'view3d', 'az', 'slider'); 2604 attr_az.name = 'az'; 2605 2606 attr_el = Type.copyAttributes(attr, board.options, 'view3d', 'el', 'slider'); 2607 attr_el.name = 'el'; 2608 2609 attr_bank = Type.copyAttributes(attr, board.options, 'view3d', 'bank', 'slider'); 2610 attr_bank.name = 'bank'; 2611 2612 v = Type.evaluate(attr_az.point1.pos); 2613 if (!Type.isArray(v)) { 2614 // 'auto' 2615 p1 = [x - 1, y - 2]; 2616 } else { 2617 p1 = v; 2618 } 2619 v = Type.evaluate(attr_az.point2.pos); 2620 if (!Type.isArray(v)) { 2621 // 'auto' 2622 p2 = [x + w + 1, y - 2]; 2623 } else { 2624 p2 = v; 2625 } 2626 2627 /** 2628 * Slider to adapt azimuth angle 2629 * @name JXG.View3D#az_slide 2630 * @type {Slider} 2631 */ 2632 view.az_slide = board.create( 2633 'slider', 2634 [ 2635 p1, p2, 2636 [ 2637 Type.evaluate(attr_az.min), 2638 Type.evaluate(attr_az.start), 2639 Type.evaluate(attr_az.max) 2640 ] 2641 ], 2642 attr_az 2643 ); 2644 view.inherits.push(view.az_slide); 2645 view.az_slide.elType = 'view3d_slider'; // Used in board.prepareUpdate() 2646 2647 v = Type.evaluate(attr_el.point1.pos); 2648 if (!Type.isArray(v)) { 2649 // 'auto' 2650 p1 = [x - 1, y]; 2651 } else { 2652 p1 = v; 2653 } 2654 v = Type.evaluate(attr_el.point2.pos); 2655 if (!Type.isArray(v)) { 2656 // 'auto' 2657 p2 = [x - 1, y + h]; 2658 } else { 2659 p2 = v; 2660 } 2661 2662 /** 2663 * Slider to adapt elevation angle 2664 * 2665 * @name JXG.View3D#el_slide 2666 * @type {Slider} 2667 */ 2668 view.el_slide = board.create( 2669 'slider', 2670 [ 2671 p1, p2, 2672 [ 2673 Type.evaluate(attr_el.min), 2674 Type.evaluate(attr_el.start), 2675 Type.evaluate(attr_el.max)] 2676 ], 2677 attr_el 2678 ); 2679 view.inherits.push(view.el_slide); 2680 view.el_slide.elType = 'view3d_slider'; // Used in board.prepareUpdate() 2681 2682 v = Type.evaluate(attr_bank.point1.pos); 2683 if (!Type.isArray(v)) { 2684 // 'auto' 2685 p1 = [x - 1, y + h + 2]; 2686 } else { 2687 p1 = v; 2688 } 2689 v = Type.evaluate(attr_bank.point2.pos); 2690 if (!Type.isArray(v)) { 2691 // 'auto' 2692 p2 = [x + w + 1, y + h + 2]; 2693 } else { 2694 p2 = v; 2695 } 2696 2697 /** 2698 * Slider to adjust bank angle 2699 * 2700 * @name JXG.View3D#bank_slide 2701 * @type {Slider} 2702 */ 2703 view.bank_slide = board.create( 2704 'slider', 2705 [ 2706 p1, p2, 2707 [ 2708 Type.evaluate(attr_bank.min), 2709 Type.evaluate(attr_bank.start), 2710 Type.evaluate(attr_bank.max) 2711 ] 2712 ], 2713 attr_bank 2714 ); 2715 view.inherits.push(view.bank_slide); 2716 view.bank_slide.elType = 'view3d_slider'; // Used in board.prepareUpdate() 2717 2718 // Set special infobox attributes of view3d.infobox 2719 // Using setAttribute() is not possible here, since we have to 2720 // avoid a call of board.update(). 2721 view.board.infobox.visProp = Type.merge(view.board.infobox.visProp, attr.infobox); 2722 2723 // 3d infobox: drag direction and coordinates 2724 view.board.highlightInfobox = function (x, y, el) { 2725 var d, i, c3d, foot, 2726 pre = '', 2727 brd = el.board, 2728 arr, infobox, 2729 p = null; 2730 2731 if (this.mode === this.BOARD_MODE_DRAG) { 2732 // Drag direction is only shown during dragging 2733 if (view.isVerticalDrag()) { 2734 pre = '<span style="color:black; font-size:200%">\u21C5 </span>'; 2735 } else { 2736 pre = '<span style="color:black; font-size:200%">\u21C4 </span>'; 2737 } 2738 } 2739 2740 // Search 3D parent 2741 for (i = 0; i < el.parents.length; i++) { 2742 p = brd.objects[el.parents[i]]; 2743 if (p.is3D) { 2744 break; 2745 } 2746 } 2747 2748 if (p && Type.exists(p.element2D)) { 2749 foot = [1, 0, 0, p.coords[3]]; 2750 view._w0 = Mat.innerProduct(view.matrix3D[0], foot, 4); 2751 2752 c3d = view.project2DTo3DPlane(p.element2D, [1, 0, 0, 1], foot); 2753 if (!view.isInCube(c3d)) { 2754 view.board.highlightCustomInfobox('', p); 2755 return; 2756 } 2757 d = p.evalVisProp('infoboxdigits'); 2758 infobox = view.board.infobox; 2759 if (d === 'auto') { 2760 if (infobox.useLocale()) { 2761 arr = [pre, '(', infobox.formatNumberLocale(p.X()), ' | ', infobox.formatNumberLocale(p.Y()), ' | ', infobox.formatNumberLocale(p.Z()), ')']; 2762 } else { 2763 arr = [pre, '(', Type.autoDigits(p.X()), ' | ', Type.autoDigits(p.Y()), ' | ', Type.autoDigits(p.Z()), ')']; 2764 } 2765 2766 } else { 2767 if (infobox.useLocale()) { 2768 arr = [pre, '(', infobox.formatNumberLocale(p.X(), d), ' | ', infobox.formatNumberLocale(p.Y(), d), ' | ', infobox.formatNumberLocale(p.Z(), d), ')']; 2769 } else { 2770 arr = [pre, '(', Type.toFixed(p.X(), d), ' | ', Type.toFixed(p.Y(), d), ' | ', Type.toFixed(p.Z(), d), ')']; 2771 } 2772 } 2773 view.board.highlightCustomInfobox(arr.join(''), p); 2774 } else { 2775 view.board.highlightCustomInfobox('(' + x + ', ' + y + ')', el); 2776 } 2777 }; 2778 2779 // Hack needed to enable addEvent for view3D: 2780 view.BOARD_MODE_NONE = 0x0000; 2781 2782 // Add events for the keyboard navigation 2783 Env.addEvent(board.containerObj, 'keydown', function (event) { 2784 var neededKey, 2785 catchEvt = false; 2786 2787 // this.board._change3DView = true; 2788 if (view.evalVisProp('el.keyboard.enabled') && 2789 (event.key === 'ArrowUp' || event.key === 'ArrowDown') 2790 ) { 2791 neededKey = view.evalVisProp('el.keyboard.key'); 2792 if (neededKey === 'none' || 2793 (neededKey.indexOf('shift') > -1 && event.shiftKey) || 2794 (neededKey.indexOf('ctrl') > -1 && event.ctrlKey)) { 2795 view._elEventHandler(event); 2796 catchEvt = true; 2797 } 2798 2799 } 2800 2801 if (view.evalVisProp('az.keyboard.enabled') && 2802 (event.key === 'ArrowLeft' || event.key === 'ArrowRight') 2803 ) { 2804 neededKey = view.evalVisProp('az.keyboard.key'); 2805 if (neededKey === 'none' || 2806 (neededKey.indexOf('shift') > -1 && event.shiftKey) || 2807 (neededKey.indexOf('ctrl') > -1 && event.ctrlKey) 2808 ) { 2809 view._azEventHandler(event); 2810 catchEvt = true; 2811 } 2812 } 2813 2814 if (view.evalVisProp('bank.keyboard.enabled') && (event.key === ',' || event.key === '<' || event.key === '.' || event.key === '>')) { 2815 neededKey = view.evalVisProp('bank.keyboard.key'); 2816 if (neededKey === 'none' || (neededKey.indexOf('shift') > -1 && event.shiftKey) || (neededKey.indexOf('ctrl') > -1 && event.ctrlKey)) { 2817 view._bankEventHandler(event); 2818 catchEvt = true; 2819 } 2820 } 2821 2822 if (event.key === 'PageUp') { 2823 view.nextView(); 2824 catchEvt = true; 2825 } else if (event.key === 'PageDown') { 2826 view.previousView(); 2827 catchEvt = true; 2828 } 2829 2830 if (catchEvt) { 2831 // We stop event handling only in the case if the keypress could be 2832 // used for the 3D view. If this is not done, input fields et al 2833 // can not be used any more. 2834 event.preventDefault(); 2835 } 2836 this.board._change3DView = false; 2837 2838 }, view); 2839 2840 // Add events for the pointer navigation 2841 Env.addEvent(board.containerObj, 'pointerdown', view.pointerDownHandler, view); 2842 2843 // Initialize view rotation matrix 2844 view.getAnglesFromSliders(); 2845 view.matrix3DRot = view.getRotationFromAngles(); 2846 2847 // override angle slider bounds when trackball navigation is enabled 2848 view.updateAngleSliderBounds(); 2849 2850 view.board.update(); 2851 2852 return view; 2853 }; 2854 2855 JXG.registerElement("view3d", JXG.createView3D); 2856 2857 export default JXG.View3D; 2858