1 /* 2 Copyright 2008-2024 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 /*global JXG:true, define: true*/ 30 31 /** 32 * Create axes and rear and front walls of the 33 * view3d bounding box bbox3D. 34 */ 35 import JXG from "../jxg.js"; 36 import Type from "../utils/type.js"; 37 38 /** 39 * @class This element creates the axis and plane elements of a 3D view. 40 * @pseudo 41 * @description This element "axes3d" is used to create 42 * <ul> 43 * <li> 3D coordinate axes (either "axesPosition:'border'" or "axesPosition:'center'") 44 * <li> A point3d "O" (origin) if "axesPosition:'center'" 45 * <li> Rear and front planes in all three directions of the view3d element. 46 * <li> Coordinate axes on the rear and front planes 47 * </ul> 48 * 49 * @name Axes3D 50 * @constructor 51 * @type Object 52 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 53 * 54 */ 55 JXG.createAxes3D = function (board, parents, attributes) { 56 var view = parents[0], 57 directions = ["x", "y", "z"], 58 suffixAxis = "Axis", 59 sides = ["Rear", "Front"], 60 rear = [0, 0, 0], // x, y, z 61 front = [0, 0, 0], // x, y, z 62 i, j, k, i1, i2, attr, pos, 63 dir, dir1, len, 64 from, to, vec1, vec2, 65 range1, range2, 66 na, na_parent, 67 ticks_attr, 68 axes = {}; 69 70 if (Type.exists(view.bbox3D)) { 71 for (i = 0; i < directions.length; i++) { 72 rear[i] = view.bbox3D[i][0]; 73 front[i] = view.bbox3D[i][1]; 74 } 75 } else { 76 for (i = 0; i < directions.length; i++) { 77 rear[i] = parents[1][i]; 78 front[i] = parents[2][1]; 79 } 80 } 81 82 // Main 3D axes 83 attr = Type.copyAttributes(attributes, board.options, "axes3d"); 84 // Position of the main axes can not be changed during run time 85 pos = attr.axesposition; 86 87 for (i = 0; i < directions.length; i++) { 88 // Run through ['x', 'y', 'z'] 89 dir = directions[i]; 90 na = dir + suffixAxis; 91 92 if (pos === "center") { 93 // Axes centered 94 from = [0, 0, 0]; 95 to = [0, 0, 0]; 96 to[i] = front[i]; 97 axes[na] = view.create("axis3d", [from, to], attr[na.toLowerCase()]); 98 axes[na].view = view; 99 } else if (pos === 'border') { 100 // Axes bordered 101 na += "Border"; 102 from = rear.slice(); 103 to = front.slice(); 104 if (dir === 'z') { 105 from[1] = front[1]; 106 to[0] = rear[0]; 107 } else if (dir === 'x') { 108 from = [rear[0], front[1], rear[2]]; 109 to = [front[0], front[1], rear[2]]; 110 } else { 111 from = [front[0], rear[1], rear[2]]; 112 to = [front[0], front[1], rear[2]]; 113 } 114 to[i] = front[i]; 115 // attr[na.toLowerCase()].lastArrow = false; 116 axes[na] = view.create("axis3d", [from, to], attr[na.toLowerCase()]); 117 axes[na].view = view; 118 119 ticks_attr = attr[na.toLowerCase()].ticks3d; 120 ticks_attr.element3D = true; // Needed to avoid update during change of view 121 len = front[i] - rear[i]; 122 if (dir === 'x') { 123 axes[na + "Ticks"] = view.create("ticks3d", [from, [1, 0, 0], len, [0, 1, 0]], ticks_attr); 124 } else if (dir === 'y') { 125 axes[na + "Ticks"] = view.create("ticks3d", [from, [0, 1, 0], len, [1, 0, 0]], ticks_attr); 126 } else { 127 axes[na + "Ticks"] = view.create("ticks3d", [from, [0, 0, 1], len, [0, 1, 0]], ticks_attr); 128 } 129 axes[na + "Ticks"].view = view; 130 } 131 } 132 133 if (pos === 'center') { 134 // Origin (2D point) 135 axes.O = view.create( 136 "intersection", 137 [axes[directions[0] + suffixAxis], axes[directions[1] + suffixAxis]], 138 { 139 name: "", 140 visible: false, 141 withLabel: false 142 } 143 ); 144 axes.O.view = view; 145 } else { 146 axes.O = null; 147 } 148 149 // Front and rear planes 150 for (i = 0; i < directions.length; i++) { 151 // Run through ['x', 'y', 'z'] 152 i1 = (i + 1) % 3; 153 i2 = (i + 2) % 3; 154 155 dir = directions[i]; 156 for (j = 0; j < sides.length; j++) { 157 // Run through ['Rear', 'Front'] 158 159 from = [0, 0, 0]; 160 from[i] = j === 0 ? rear[i] : front[i]; 161 vec1 = [0, 0, 0]; 162 vec2 = [0, 0, 0]; 163 vec1[i1] = 1; 164 vec2[i2] = 1; 165 range1 = [rear[i1], front[i1]]; 166 range2 = [rear[i2], front[i2]]; 167 na = dir + "Plane" + sides[j]; 168 attr = Type.copyAttributes(attributes, board.options, "axes3d", na); 169 axes[na] = view.create("plane3d", [from, vec1, vec2, range1, range2], attr); 170 axes[na].elType = "axisplane3d"; 171 } 172 } 173 174 // Axes on front and rear planes 175 for (i = 0; i < directions.length; i++) { 176 // Run through ['x', 'y', 'z'] 177 dir = directions[i]; 178 for (j = 0; j < sides.length; j++) { 179 for (k = 1; k <= 2; k++) { 180 i1 = (i + k) % 3; 181 dir1 = directions[i1]; 182 na = dir + "Plane" + sides[j] + dir1.toUpperCase() + "Axis"; 183 na_parent = dir + "Plane" + sides[j]; 184 185 from = [0, 0, 0]; 186 to = [0, 0, 0]; 187 from[i] = to[i] = j === 0 ? rear[i] : front[i]; 188 189 from[i1] = rear[i1]; 190 to[i1] = front[i1]; 191 192 attr = Type.copyAttributes(attributes, board.options, "axes3d", na); 193 axes[na] = view.create("axis3d", [from, to], attr); 194 axes[na].view = view; 195 axes[na_parent].addChild(axes[na]); 196 axes[na_parent].element2D.inherits.push(axes[na]); // TODO: Access of element2D is not nice 197 } 198 } 199 } 200 201 return axes; 202 }; 203 JXG.registerElement("axes3d", JXG.createAxes3D); 204 205 /** 206 * @class This element creates a 3D axis. 207 * @pseudo 208 * @description Simple element 3d axis as used with "axesPosition:center". No ticks and no label (yet). 209 * <p> 210 * At the time being, the input arrays are NOT dynamic, i.e. can not be given as functions. 211 * 212 * @name Axis3D 213 * @augments Arrow 214 * @constructor 215 * @type Object 216 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 217 * @param {Array_Array} start,end Two arrays of length 3 for the start point and the end point of the axis. 218 * 219 */ 220 JXG.createAxis3D = function (board, parents, attributes) { 221 var view = parents[0], 222 attr, 223 start = parents[1], 224 end = parents[2], 225 el_start, 226 el_end, 227 el; 228 229 // Use 2D points to create axis 230 attr = Type.copyAttributes(attributes.point1, board.options, "axis3d", "point1"); 231 attr.element3D = true; // Needed to avoid update during change of view 232 el_start = view.create( 233 "point", 234 [ 235 (function (xx, yy, zz) { 236 return function () { 237 return view.project3DTo2D(xx, yy, zz)[1]; 238 }; 239 })(start[0], start[1], start[2]), 240 (function (xx, yy, zz) { 241 return function () { 242 return view.project3DTo2D(xx, yy, zz)[2]; 243 }; 244 })(start[0], start[1], start[2]) 245 ], 246 attr 247 ); 248 249 attr = Type.copyAttributes(attributes.point2, board.options, "axis3d", "point2"); 250 attr.element3D = true; // Needed to avoid update during change of view 251 el_end = view.create( 252 "point", 253 [ 254 (function (xx, yy, zz) { 255 return function () { 256 return view.project3DTo2D(xx, yy, zz)[1]; 257 }; 258 })(end[0], end[1], end[2]), 259 (function (xx, yy, zz) { 260 return function () { 261 return view.project3DTo2D(xx, yy, zz)[2]; 262 }; 263 })(end[0], end[1], end[2]) 264 ], 265 attr 266 ); 267 268 attr = Type.copyAttributes(attributes, board.options, "axis3d"); 269 attr.element3D = true; // Needed to avoid update during change of view 270 el = view.create("arrow", [el_start, el_end], attr); 271 272 return el; 273 }; 274 JXG.registerElement("axis3d", JXG.createAxis3D); 275 276 /** 277 * @class This element creates a 3D (rectangular) mesh. 278 * @pseudo 279 * @description Create a (rectangular) mesh - i.e. grid lines - on a plane3D element. 280 * <p> 281 * At the time being, the mesh is not connected to the plane. The connecting element is simply the 282 * parameter point. 283 * 284 * @name Mesh3D 285 * @augments Curve 286 * @constructor 287 * @type Object 288 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 289 * @param {Array_Array_Array_Array_Array_Number} point,direction1,range1,direction2,range2,[stepWidth=1] point is an array of length 3 290 * determining the starting point of the grid. direction1 and direction2 are arrays of length 3 for the directions of the grid. 291 * range1 and range2 (arrays of length 2) give the respective ranges. stepWidth is the increment of the grid lines. 292 * All parameters can be supplied as functions returning an appropriate data type. 293 * 294 */ 295 JXG.createMesh3D = function (board, parents, attr) { 296 var view = parents[0], 297 point = parents[1], 298 dir1 = parents[2], 299 range1 = parents[3], 300 dir2 = parents[4], 301 range2 = parents[5], 302 step = parents[6] || 1, 303 el; 304 305 attr.element3D = true; // Needed to avoid update during change of view 306 el = view.create("curve", [[], []], attr); 307 308 el.point = point; 309 el.direction1 = dir1; 310 el.range1 = range1; 311 el.direction2 = dir2; 312 el.range2 = range2; 313 el.step = step; 314 315 /** 316 * @ignore 317 */ 318 el.updateDataArray = function () { 319 var range1 = Type.evaluate(this.range1), 320 range2 = Type.evaluate(this.range2), 321 s1 = range1[0], 322 e1 = range1[1], 323 s2 = range2[0], 324 e2 = range2[1], 325 l1, l2, res, i, 326 v1 = [0, 0, 0], 327 v2 = [0, 0, 0], 328 step = Type.evaluate(this.step), 329 q = [0, 0, 0]; 330 331 this.dataX = []; 332 this.dataY = []; 333 334 if (Type.isFunction(this.point)) { 335 q = this.point().slice(1); 336 } else { 337 for (i = 0; i < 3; i++) { 338 q[i] = Type.evaluate(this.point[i]); 339 } 340 } 341 for (i = 0; i < 3; i++) { 342 v1[i] = Type.evaluate(this.direction1[i]); 343 v2[i] = Type.evaluate(this.direction2[i]); 344 } 345 l1 = JXG.Math.norm(v1, 3); 346 l2 = JXG.Math.norm(v2, 3); 347 for (i = 0; i < 3; i++) { 348 v1[i] /= l1; 349 v2[i] /= l2; 350 } 351 352 // sol = Mat.Geometry.getPlaneBounds(v1, v2, q, s1, e1); 353 // if (sol !== null) { 354 // s1 = sol[0]; 355 // e1 = sol[1]; 356 // s2 = sol[2]; 357 // e2 = sol[3]; 358 // } 359 360 res = view.getMesh( 361 [ 362 function (u, v) { 363 return q[0] + u * v1[0] + v * v2[0]; 364 }, 365 function (u, v) { 366 return q[1] + u * v1[1] + v * v2[1]; 367 }, 368 function (u, v) { 369 return q[2] + u * v1[2] + v * v2[2]; 370 } 371 ], 372 [Math.ceil(s1), Math.floor(e1), (Math.ceil(e1) - Math.floor(s1)) / step], 373 [Math.ceil(s2), Math.floor(e2), (Math.ceil(e2) - Math.floor(s2)) / step] 374 ); 375 this.dataX = res[0]; 376 this.dataY = res[1]; 377 }; 378 379 return el; 380 }; 381 382 JXG.registerElement("mesh3d", JXG.createMesh3D); 383