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