2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2010 - DIGITEO - Pierre Lando
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 package org.scilab.modules.renderer.JoGLView.axes;
14 import org.scilab.forge.scirenderer.Canvas;
15 import org.scilab.forge.scirenderer.DrawingTools;
16 import org.scilab.forge.scirenderer.SciRendererException;
17 import org.scilab.forge.scirenderer.clipping.ClippingPlane;
18 import org.scilab.forge.scirenderer.shapes.appearance.Appearance;
19 import org.scilab.forge.scirenderer.shapes.geometry.Geometry.FaceCullingMode;
20 import org.scilab.forge.scirenderer.tranformations.DegenerateMatrixException;
21 import org.scilab.forge.scirenderer.tranformations.Transformation;
22 import org.scilab.forge.scirenderer.tranformations.TransformationFactory;
23 import org.scilab.forge.scirenderer.tranformations.TransformationStack;
24 import org.scilab.forge.scirenderer.tranformations.Vector3d;
25 import org.scilab.forge.scirenderer.tranformations.Vector4d;
26 import org.scilab.modules.graphic_objects.axes.Axes;
27 import org.scilab.modules.graphic_objects.axes.Box;
28 import org.scilab.modules.graphic_objects.contouredObject.Line;
29 import org.scilab.modules.graphic_objects.graphicObject.ClippableProperty;
30 import org.scilab.modules.graphic_objects.graphicObject.ClippableProperty.ClipStateType;
31 import org.scilab.modules.graphic_objects.figure.ColorMap;
32 import org.scilab.modules.renderer.JoGLView.DrawerVisitor;
33 import org.scilab.modules.renderer.JoGLView.axes.ruler.AxesRulerDrawer;
34 import org.scilab.modules.renderer.JoGLView.label.AxisLabelPositioner;
35 import org.scilab.modules.renderer.JoGLView.label.LabelManager;
36 import org.scilab.modules.renderer.JoGLView.label.LabelPositioner;
37 import org.scilab.modules.renderer.JoGLView.label.TitlePositioner;
38 import org.scilab.modules.renderer.JoGLView.label.YAxisLabelPositioner;
39 import org.scilab.modules.renderer.JoGLView.util.ColorFactory;
41 import java.awt.geom.Rectangle2D;
42 import java.util.HashMap;
47 * AxesDrawer are used by {@see DrawerVisitor} to draw {@see Axes}.
49 * @author Pierre Lando
51 public class AxesDrawer {
52 private static final double DEFAULT_THETA = 270.0;
53 private static final Line.LineType HIDDEN_BORDER_PATTERN = Line.LineType.DASH;
55 /** An epsilon value used to move the clipping planes in order to prevent strict clipping. */
56 private static final double CLIPPING_EPSILON = 1e-5;
58 private final DrawerVisitor visitor;
59 private final Geometries geometries;
61 private final AxesRulerDrawer rulerDrawer;
63 /** The front face culling mode. */
64 private FaceCullingMode frontFaceCullingMode;
66 /** The back face culling mode. */
67 private FaceCullingMode backFaceCullingMode;
69 /** The label manager. */
70 private final LabelManager labelManager;
72 /** The x-axis label positioner. */
73 private AxisLabelPositioner xAxisLabelPositioner;
75 /** The y-axis label positioner. */
76 private AxisLabelPositioner yAxisLabelPositioner;
78 /** The z-axis label positioner. */
79 private AxisLabelPositioner zAxisLabelPositioner;
81 /** The title positioner. */
82 private TitlePositioner titlePositioner;
85 * The current reversed bounds. Used by the functions converting
86 * between object and box coordinates.
88 private double[] reversedBounds;
90 /** The current reversed bounds intervals. */
91 private double[] reversedBoundsIntervals;
93 /** The current projection (from object to window coordinates) used when drawing objects. */
94 private Transformation currentProjection;
96 /** The current data scale and translate transformation. */
97 private Transformation currentDataTransformation;
99 /** The set of object to window coordinate projections associated to all the Axes drawn by this drawer. */
100 private final Map<String, Transformation> projectionMap = new HashMap<String, Transformation>();
102 /** The set of object (in 2d view mode) to window coordinate projections associated to all the Axes drawn by this drawer. */
103 private final Map<String, Transformation> projection2dViewMap = new HashMap<String, Transformation>();
105 /** This is a __MAP__ */
106 private final Map<String, Transformation> sceneProjectionMap = new HashMap<String, Transformation>();
109 * Default constructor.
110 * @param visitor the parent {@see DrawerVisitor}.
112 public AxesDrawer(DrawerVisitor visitor) {
113 this.visitor = visitor;
114 this.labelManager = visitor.getLabelManager();
115 this.geometries = new Geometries(visitor.getCanvas());
116 this.rulerDrawer = new AxesRulerDrawer(visitor.getCanvas());
118 this.xAxisLabelPositioner = new AxisLabelPositioner();
119 this.yAxisLabelPositioner = new YAxisLabelPositioner();
120 this.zAxisLabelPositioner = new AxisLabelPositioner();
121 this.titlePositioner = new TitlePositioner();
123 reversedBounds = new double[6];
124 reversedBoundsIntervals = new double[3];
128 * Draw the given {@see Axes}.
129 * @param axes {@see Axes} to draw.
130 * @throws org.scilab.forge.scirenderer.SciRendererException if the draw fail.
132 public void draw(Axes axes) throws SciRendererException {
133 DrawingTools drawingTools = visitor.getDrawingTools();
134 Canvas canvas = visitor.getCanvas();
135 ColorMap colorMap = visitor.getColorMap();
136 TransformationStack modelViewStack = drawingTools.getTransformationManager().getModelViewStack();
137 TransformationStack projectionStack = drawingTools.getTransformationManager().getProjectionStack();
139 // Axes are drawn on top of everything.
140 drawingTools.clearDepthBuffer();
143 Transformation zoneProjection = computeZoneProjection(axes);
144 projectionStack.push(zoneProjection);
146 // Set box projection.
147 Transformation transformation = computeBoxTransformation(axes, canvas, false);
148 Transformation transformation2dView = computeBoxTransformation(axes, canvas, true);
149 modelViewStack.pushRightMultiply(transformation);
151 /* Compute the data scale and translate transformation. */
152 Transformation dataTransformation = computeDataTransformation(axes);
154 currentDataTransformation = dataTransformation;
156 /* Compute the object to window coordinates projection. */
157 currentProjection = zoneProjection.rightTimes(transformation);
158 currentProjection = currentProjection.rightTimes(dataTransformation);
160 sceneProjectionMap.put(axes.getIdentifier(), currentProjection);
161 Transformation windowTrans = drawingTools.getTransformationManager().getWindowTransformation().getInverseTransformation();
162 currentProjection = windowTrans.rightTimes(currentProjection);
164 /* Update the projection maps with the resulting projections. */
165 addProjection(axes.getIdentifier(), currentProjection);
167 /* 2d view projection, to do: optimize computation */
168 currentProjection = zoneProjection.rightTimes(transformation2dView);
169 currentProjection = currentProjection.rightTimes(dataTransformation);
170 currentProjection = windowTrans.rightTimes(currentProjection);
172 addProjection2dView(axes.getIdentifier(), currentProjection);
175 * Draw the axes background.
177 drawBackground(axes, drawingTools, colorMap);
180 * Mirror the transformation such that the corner with the maximum Z value was (-1, -1, -1).
183 Transformation cubeOrientation = computeCubeMirroring(transformation);
184 modelViewStack.pushRightMultiply(cubeOrientation);
185 drawBox(axes, drawingTools, colorMap);
186 modelViewStack.pop();
188 // Ruler are drawn in box coordinate.
189 rulerDrawer.drawRuler(axes, this, colorMap, drawingTools);
191 /* Compute reversed bounds. */
192 computeReversedBounds(axes);
195 * Scale and translate for data fitting.
198 modelViewStack.pushRightMultiply(dataTransformation);
200 /* Compute the front and back culling modes */
201 computeFaceCullingModes(axes);
203 visitor.askAcceptVisitor(axes.getChildren());
204 modelViewStack.pop();
206 // Reset transformation stacks.
207 modelViewStack.pop();
208 projectionStack.pop();
212 * Draw the axes background.
213 * @param axes the {@see Axes}
214 * @param drawingTools the {@see DrawingTools} to use.
215 * @param colorMap the current {@see ColorMap}
216 * @throws org.scilab.forge.scirenderer.SciRendererException if the draw fail.
218 private void drawBackground(Axes axes, DrawingTools drawingTools, ColorMap colorMap) throws SciRendererException {
219 if (axes.getFilled()) {
220 Appearance appearance = new Appearance();
221 appearance.setFillColor(ColorFactory.createColor(colorMap, axes.getBackground()));
222 drawingTools.draw(geometries.getCubeGeometry(), appearance);
223 drawingTools.clearDepthBuffer();
228 * Draw the box of the given {@see Axes}
229 * @param axes the given {@see Axes}.
230 * @param drawingTools the {@see DrawingTools} to use.
231 * @param colorMap the current {@see ColorMap}.
232 * @throws org.scilab.forge.scirenderer.SciRendererException if the draw fail.
234 private void drawBox(Axes axes, DrawingTools drawingTools, ColorMap colorMap) throws SciRendererException {
235 Box.BoxType boxed = axes.getBox().getBox();
236 if (boxed != Box.BoxType.OFF) {
237 Appearance appearance = new Appearance();
240 * Draw hidden part of box.
242 appearance.setLineColor(ColorFactory.createColor(colorMap, axes.getHiddenAxisColor()));
243 appearance.setLineWidth(axes.getLineThickness().floatValue());
244 appearance.setLinePattern(HIDDEN_BORDER_PATTERN.asPattern());
245 drawingTools.draw(geometries.getHiddenBoxBorderGeometry(), appearance);
248 if (boxed != Box.BoxType.HIDDEN_AXES) {
253 appearance.setLineColor(ColorFactory.createColor(colorMap, axes.getLineColor()));
254 appearance.setLineWidth(axes.getLineThickness().floatValue());
255 appearance.setLinePattern(axes.getLine().getLineStyle().asPattern());
256 drawingTools.draw(geometries.getBoxBorderGeometry(), appearance);
259 if (boxed != Box.BoxType.BACK_HALF) {
261 * Draw front part of box.
263 drawingTools.draw(geometries.getFrontBoxBorderGeometry(), appearance);
271 * Compute the mirroring matrix needed to have the [-1; -1; -1] point projected with the maximal Z value.
272 * @param transformation the current transformation.
273 * @return the mirroring matrix needed to have the [-1; -1; -1] point projected with the maximal Z value.
275 private Transformation computeCubeMirroring(Transformation transformation) {
276 double[] matrix = transformation.getMatrix();
278 return TransformationFactory.getScaleTransformation(
279 matrix[2] < 0 ? 1 : -1,
280 matrix[6] < 0 ? 1 : -1,
281 matrix[10] < 0 ? 1 : -1
283 } catch (DegenerateMatrixException e) {
284 // Should never happen.
285 return TransformationFactory.getIdentity();
291 * Compute zone where the axes is draw. In normalised window coordinate.
292 * @param axes the given {@see axes}.
293 * @return the zone where the axes is draw.
295 private Rectangle2D computeZone(Axes axes) {
296 Double[] axesBounds = axes.getAxesBounds();
297 Double[] margins = axes.getMargins();
300 double x = (axesBounds[0] + axesBounds[2] * margins[0]) * 2 - 1;
301 double y = (1.0 - axesBounds[1] - axesBounds[3] * (1.0 - margins[3])) * 2 - 1;
302 double w = (1 - margins[0] - margins[1]) * axesBounds[2];
303 double h = (1 - margins[2] - margins[3]) * axesBounds[3];
305 if (axes.getIsoview()) {
306 double minSize = Math.min(w, h);
313 return new Rectangle2D.Double(x, y, w, h);
318 * Compute the projection for the given axes.
319 * @param axes the given axes.
320 * @return the projection matrix.
321 * @throws DegenerateMatrixException if axes represent a nul area.
323 private Transformation computeZoneProjection(Axes axes) throws DegenerateMatrixException {
324 Rectangle2D zone = computeZone(axes);
325 Transformation zoneTranslation = TransformationFactory.getTranslateTransformation(zone.getMaxX(), zone.getMaxY(), 0);
327 // We scale by 0.5 in Z to allow ruler to be drawn.
328 Transformation zoneScale = TransformationFactory.getScaleTransformation(zone.getWidth(), zone.getHeight(), .5);
330 return zoneTranslation.rightTimes(zoneScale);
334 * Compute data transformation for the given {@see Axes}.
336 * The data transformation is applied to data to fit in axes box.
338 * @param axes the given {@see Axes}
339 * @return data transformation.
340 * @throws DegenerateMatrixException if data bounds are not corrects.
342 private Transformation computeDataTransformation(Axes axes) throws DegenerateMatrixException {
343 Double[] bounds = axes.getDisplayedBounds();
345 // Reverse data if needed.
346 Transformation transformation = TransformationFactory.getScaleTransformation(
347 axes.getAxes()[0].getReverse() ? 1 : -1,
348 axes.getAxes()[1].getReverse() ? 1 : -1,
349 axes.getAxes()[2].getReverse() ? 1 : -1
353 Transformation scaleTransformation = TransformationFactory.getScaleTransformation(
354 2.0 / (bounds[1] - bounds[0]),
355 2.0 / (bounds[3] - bounds[2]),
356 2.0 / (bounds[5] - bounds[4])
358 transformation = transformation.rightTimes(scaleTransformation);
362 Transformation translateTransformation = TransformationFactory.getTranslateTransformation(
363 -(bounds[0] + bounds[1]) / 2.0,
364 -(bounds[2] + bounds[3]) / 2.0,
365 -(bounds[4] + bounds[5]) / 2.0
367 transformation = transformation.rightTimes(translateTransformation);
368 return transformation;
373 * Compute box transformation for the given axes.
375 * The box transformation is applied to the axes box to fit in the canvas.
377 * @param axes the given {@see Axes}.
378 * @param canvas the current {@see Canvas}.
379 * @param use2dView specifies whether the default 2d view rotation angles must be used (true) or the given Axes' ones (false).
380 * @return box transformation for the given axes.
381 * @throws DegenerateMatrixException if data bounds are incorrect or canvas with or length are zero.
383 private Transformation computeBoxTransformation(Axes axes, Canvas canvas, boolean use2dView) throws DegenerateMatrixException {
384 Double[] bounds = axes.getDisplayedBounds();
394 Transformation transformation;
396 // Set zone aspect ratio.
397 Rectangle2D zone = computeZone(axes);
398 double axesRatio = zone.getWidth() / zone.getHeight();
399 transformation = TransformationFactory.getPreferredAspectRatioTransformation(canvas.getDimension(), axesRatio);
404 theta = 2 * DEFAULT_THETA;
406 alpha = -axes.getRotationAngles()[0];
407 theta = DEFAULT_THETA + axes.getRotationAngles()[1];
410 Transformation alphaRotation = TransformationFactory.getRotationTransformation(alpha, 1.0, 0.0, 0.0);
411 transformation = transformation.rightTimes(alphaRotation);
412 Transformation thetaRotation = TransformationFactory.getRotationTransformation(theta, 0.0, 0.0, 1.0);
413 transformation = transformation.rightTimes(thetaRotation);
415 // If there is no cube scaling, we must take into account the distribution of data.
416 if (!axes.getCubeScaling()) {
417 tmpX = (bounds[1] - bounds[0]);
418 tmpY = (bounds[3] - bounds[2]);
419 tmpZ = (bounds[5] - bounds[4]);
422 * Here, we should divide the values by their maximum.
423 * But the next operation will automatically.
425 Transformation cubeScale = TransformationFactory.getScaleTransformation(tmpX, tmpY, tmpZ);
426 transformation = transformation.rightTimes(cubeScale);
429 // Compute bounds of projected data.
430 double[] matrix = transformation.getMatrix();
431 tmpX = 1 / (Math.abs(matrix[0]) + Math.abs(matrix[4]) + Math.abs(matrix[8]));
432 tmpY = 1 / (Math.abs(matrix[1]) + Math.abs(matrix[5]) + Math.abs(matrix[9]));
433 tmpZ = 1 / (Math.abs(matrix[2]) + Math.abs(matrix[6]) + Math.abs(matrix[10]));
435 // Scale projected data to fit in the cube.
436 Transformation isoScale;
437 if (axes.getIsoview()) {
438 Double[] axesBounds = axes.getAxesBounds();
439 double minScale = Math.min(tmpX * axesBounds[2], tmpY * axesBounds[3]);
440 isoScale = TransformationFactory.getScaleTransformation(minScale / axesBounds[2], minScale / axesBounds[3], tmpZ);
442 isoScale = TransformationFactory.getScaleTransformation(tmpX, tmpY, tmpZ);
444 transformation = transformation.leftTimes(isoScale);
446 return transformation;
451 * Computes and sets the reversed bounds of the given Axes.
452 * @param axes the given {@see Axes}.
454 private void computeReversedBounds(Axes axes) {
455 Double[] currentBounds = axes.getDisplayedBounds();
458 if (axes.getAxes()[0].getReverse()) {
459 reversedBounds[0] = currentBounds[1];
460 reversedBounds[1] = currentBounds[0];
462 reversedBounds[0] = currentBounds[0];
463 reversedBounds[1] = currentBounds[1];
466 if (axes.getAxes()[1].getReverse()) {
467 reversedBounds[2] = currentBounds[3];
468 reversedBounds[3] = currentBounds[2];
470 reversedBounds[2] = currentBounds[2];
471 reversedBounds[3] = currentBounds[3];
474 if (axes.getAxes()[2].getReverse()) {
475 reversedBounds[4] = currentBounds[5];
476 reversedBounds[5] = currentBounds[4];
478 reversedBounds[4] = currentBounds[4];
479 reversedBounds[5] = currentBounds[5];
483 * Interval values are set to 1 when bounds are equal to avoid divides by 0
484 * in the object to box coordinates conversion function.
486 if (reversedBounds[1] == reversedBounds[0]) {
487 reversedBoundsIntervals[0] = 1.0;
489 reversedBoundsIntervals[0] = reversedBounds[1] - reversedBounds[0];
492 if (reversedBounds[3] == reversedBounds[2]) {
493 reversedBoundsIntervals[1] = 1.0;
495 reversedBoundsIntervals[1] = reversedBounds[3] - reversedBounds[2];
498 if (reversedBounds[5] == reversedBounds[4]) {
499 reversedBoundsIntervals[2] = 1.0;
501 reversedBoundsIntervals[2] = reversedBounds[5] - reversedBounds[4];
507 * Computes the culling modes respectively corresponding to front and back faces
508 * of the given Axes' child objects as a function of its {X,Y,Z} reverse properties.
509 * It must be called by draw prior to rendering any child object.
510 * @param axes the given {@see Axes}.
512 private void computeFaceCullingModes(Axes axes) {
513 if (axes.getAxes()[0].getReverse() ^ axes.getAxes()[1].getReverse() ^ axes.getAxes()[2].getReverse()) {
515 this.frontFaceCullingMode = FaceCullingMode.CW;
516 this.backFaceCullingMode = FaceCullingMode.CCW;
519 this.frontFaceCullingMode = FaceCullingMode.CCW;
520 this.backFaceCullingMode = FaceCullingMode.CW;
525 * Converts a point from object coordinates to box coordinates (used when drawing axis rulers).
526 * @param point the point in object coordinates.
527 * @return the point in box coordinates.
529 public Vector3d getBoxCoordinates(Vector3d point) {
530 double[] dataCoordinates = new double[3];
532 dataCoordinates[0] = 1 - 2.0 * (point.getX() - reversedBounds[0]) / reversedBoundsIntervals[0];
533 dataCoordinates[1] = 1 - 2.0 * (point.getY() - reversedBounds[2]) / reversedBoundsIntervals[1];
534 dataCoordinates[2] = 1 - 2.0 * (point.getZ() - reversedBounds[4]) / reversedBoundsIntervals[2];
536 return new Vector3d(dataCoordinates);
540 * Converts a point from box coordinates (used when drawing axis rulers) to object coordinates.
541 * @param point the point in box coordinates.
542 * @return the point in object coordinates.
544 public Vector3d getObjectCoordinates(Vector3d point) {
545 double[] objectCoordinates = new double[3];
547 objectCoordinates[0] = 0.5 * (1.0 - point.getX()) * (reversedBounds[1] - reversedBounds[0]) + reversedBounds[0];
548 objectCoordinates[1] = 0.5 * (1.0 - point.getY()) * (reversedBounds[3] - reversedBounds[2]) + reversedBounds[2];
549 objectCoordinates[2] = 0.5 * (1.0 - point.getZ()) * (reversedBounds[5] - reversedBounds[4]) + reversedBounds[4];
551 return new Vector3d(objectCoordinates);
555 * Computes and return the object to window coordinate projection corresponding to the given Axes object.
556 * @param axes the given Axes.
557 * @param drawingTools the drawing tools.
558 * @param canvas the current canvas.
559 * @param use2dView specifies whether the default 2d view rotation angles must be used (true) or the given Axes' ones (false).
560 * @return the projection
562 private Transformation computeProjection(Axes axes, DrawingTools drawingTools, Canvas canvas, boolean use2dView) {
563 Transformation projection;
566 /* Compute the zone projection. */
567 Transformation zoneProjection = computeZoneProjection(axes);
569 /* Compute the box transformation. */
570 Transformation transformation = computeBoxTransformation(axes, canvas, use2dView);
572 /* Compute the data scale and translate transformation. */
573 Transformation dataTransformation = computeDataTransformation(axes);
575 /* Compute the object to window coordinates projection. */
576 projection = zoneProjection.rightTimes(transformation);
577 projection = projection.rightTimes(dataTransformation);
579 Transformation windowTrans = drawingTools.getTransformationManager().getWindowTransformation().getInverseTransformation();
580 projection = windowTrans.rightTimes(projection);
581 } catch (DegenerateMatrixException e) {
582 return TransformationFactory.getIdentity();
589 * Returns the current projection from object to window coordinates.
590 * @return the projection.
592 public Transformation getProjection() {
593 return currentProjection;
597 * Returns the current data scale and translate transformation.
598 * @return the data transformation.
600 public Transformation getDataTransformation() {
601 return currentDataTransformation;
605 * Adds the projection from object to window coordinates corresponding to a given Axes object
606 * to the projection map.
607 * @param axesId the identifier of the given Axes.
608 * @param projection the corresponding projection.
610 public synchronized void addProjection(String axesId, Transformation projection) {
611 projectionMap.put(axesId, projection);
615 * Returns the projection from object to window coordinates corresponding
616 * to a given Axes object.
617 * @param id the identifier of the given Axes.
618 * @return the projection.
620 public Transformation getProjection(String id) {
621 return projectionMap.get(id);
625 * Removes the object to window coordinate projection corresponding to a given Axes from
626 * the projection map.
627 * @param axesId the identifier of the given Axes.
629 public void removeProjection(String axesId) {
630 projectionMap.remove(axesId);
634 * Adds the projection from object (in 2d view mode) to window coordinates corresponding to a given Axes object
635 * to the projection map.
636 * @param axesId the identifier of the given Axes.
637 * @param projection the corresponding projection.
639 public synchronized void addProjection2dView(String axesId, Transformation projection) {
640 projection2dViewMap.put(axesId, projection);
644 * Returns the projection from object (in 2d view mode) to window coordinates corresponding
645 * to a given Axes object.
646 * @param id the identifier of the given Axes.
647 * @return the projection.
649 public Transformation getProjection2dView(String id) {
650 return projection2dViewMap.get(id);
653 public Transformation getSceneProjection(String id) {
654 return sceneProjectionMap.get(id);
658 * Removes the object (in 2d view mode) to window coordinate projection corresponding to a given Axes from
659 * the projection map.
660 * @param axesId the identifier of the given Axes.
662 public void removeProjection2dView(String axesId) {
663 projection2dViewMap.remove(axesId);
667 * Updates both the projection from object to window coordinates and the related
668 * object (in 2d view mode) to window coordinates projection for the given Axes object.
669 * @param axes the given Axes.
671 public static void updateAxesTransformation(Axes axes) {
672 DrawerVisitor currentVisitor = DrawerVisitor.getVisitor(axes.getParentFigure());
673 AxesDrawer axesDrawer = currentVisitor.getAxesDrawer();
675 Transformation transformation = axesDrawer.getProjection(axes.getIdentifier());
677 /* The projection must be updated */
678 if (transformation == null) {
679 Transformation projection = axesDrawer.computeProjection(axes, currentVisitor.getDrawingTools(), currentVisitor.getCanvas(), false);
681 axesDrawer.addProjection(axes.getIdentifier(), projection);
684 Transformation transformation2dView = axesDrawer.getProjection2dView(axes.getIdentifier());
686 /* The projection must be updated */
687 if (transformation2dView == null) {
688 Transformation projection2dView = axesDrawer.computeProjection(axes, currentVisitor.getDrawingTools(), currentVisitor.getCanvas(), true);
690 axesDrawer.addProjection2dView(axes.getIdentifier(), projection2dView);
695 * Computes and returns the coordinates of a point projected onto the default 2d view plane.
696 * To compute them, the point is projected using the object to window coordinate projection, then
697 * unprojected using the object to window coordinate projection corresponding to the default 2d view
698 * (which uses the default camera rotation angles).
699 * To do: optimize by using the already computed 3d view projection.
700 * @param axes the given Axes.
701 * @param coordinates the object (x,y,z) coordinates to project onto the 2d view plane (3-element array).
702 * @returns the 2d view coordinates (3-element array).
704 public static double[] compute2dViewCoordinates(Axes axes, double[] coordinates) {
705 DrawerVisitor currentVisitor;
706 AxesDrawer axesDrawer;
707 Transformation projection;
708 Transformation projection2d;
710 currentVisitor = DrawerVisitor.getVisitor(axes.getParentFigure());
712 Vector3d point = new Vector3d(coordinates);
714 if (currentVisitor != null) {
715 axesDrawer = currentVisitor.getAxesDrawer();
717 projection = axesDrawer.computeProjection(axes, currentVisitor.getDrawingTools(), currentVisitor.getCanvas(), false);
718 projection2d = axesDrawer.computeProjection(axes, currentVisitor.getDrawingTools(), currentVisitor.getCanvas(), true);
720 point = projection.project(point);
721 point = projection2d.unproject(point);
724 return new double[] {point.getX(), point.getY(), point.getZ()};
728 * Computes and returns the pixel coordinates from a point's coordinates expressed in the default
729 * 2d view coordinate frame, using the given Axes. The returned pixel coordinates are expressed
730 * in the AWT's 2d coordinate frame.
731 * @param axes the given Axes.
732 * @param coordinates the 2d view coordinates (3-element array: x, y, z).
733 * @returns the pixel coordinates (2-element array: x, y).
735 public static double[] computePixelFrom2dViewCoordinates(Axes axes, double[] coordinates) {
736 DrawerVisitor currentVisitor;
737 AxesDrawer axesDrawer;
739 double[] coords2dView = new double[] {0.0, 0.0, 0.0};
741 currentVisitor = DrawerVisitor.getVisitor(axes.getParentFigure());
743 if (currentVisitor != null) {
746 axesDrawer = currentVisitor.getAxesDrawer();
747 height = currentVisitor.getCanvas().getHeight();
749 Transformation projection2d = axesDrawer.getProjection2dView(axes.getIdentifier());
751 Vector3d point = new Vector3d(coordinates);
752 point = projection2d.project(point);
754 /* Convert the window coordinates to pixel coordinates, only y changes due to the differing y-axis convention */
755 coords2dView[0] = point.getX();
756 coords2dView[1] = height - point.getY();
763 * Computes and returns the coordinates of a point projected onto the default 2d view plane
764 * from its pixel coordinates, using the given Axes. Pixel coordinates are expressed in
765 * the AWT's 2d coordinate frame.
766 * The returned point's z component is set to 0, as we only have x and y as an input.
767 * @param axes the given Axes.
768 * @param coordinates the pixel coordinates (2-element array: x, y).
769 * @return coordinates the 2d view coordinates (3-element array: x, y, z).
771 public static double[] compute2dViewFromPixelCoordinates(Axes axes, double[] coordinates) {
772 DrawerVisitor currentVisitor;
773 AxesDrawer axesDrawer;
775 double[] coords2dView = new double[] {0.0, 0.0, 0.0};
777 currentVisitor = DrawerVisitor.getVisitor(axes.getParentFigure());
779 if (currentVisitor != null) {
782 axesDrawer = currentVisitor.getAxesDrawer();
783 height = currentVisitor.getCanvas().getHeight();
785 /* Convert the pixel coordinates to window coordinates, only y changes due to the differing y-axis convention */
786 Vector3d point = new Vector3d(coordinates[0], height - coordinates[1], 0.0);
788 Transformation projection2d = axesDrawer.getProjection2dView(axes.getIdentifier());
789 point = projection2d.unproject(point);
790 coords2dView = point.getData();
797 * Un-project the given point from AWT coordinate to given axes coordinate.
798 * @param axes returned coordinate are relative to this axes.
799 * @param point un-projected point.
800 * @return The un-projected point.
802 public static Vector3d unProject(Axes axes, Vector3d point) {
803 DrawerVisitor currentVisitor = DrawerVisitor.getVisitor(axes.getParentFigure());
805 if (currentVisitor != null) {
806 AxesDrawer axesDrawer = currentVisitor.getAxesDrawer();
807 double height = currentVisitor.getCanvas().getHeight() - 1;
809 Transformation projection2d = axesDrawer.getProjection(axes.getIdentifier());
810 return projection2d.unproject(new Vector3d(point.getX(), height - point.getY(), point.getZ()));
812 return new Vector3d(0, 0, 0);
817 * Computes and returns the viewing area corresponding to the given Axes object.
818 * The viewing area is described by the (x, y) coordinates of the Axes box's upper-left corner
819 * and the Axes box's dimensions (width and height), all values are in pixel.
820 * The 2d coordinate frame in which the area is expressed uses the AWT convention:
821 * upper-left window corner at (0, 0), y-axis pointing downwards).
822 * @param axes the given Axes.
823 * @return the Axes' viewing area (4-element array: x, y, width, height).
825 public static double[] getViewingArea(Axes axes) {
826 DrawerVisitor currentVisitor;
828 double[] viewingArea = new double[] {0.0, 0.0, 0.0, 0.0};
830 currentVisitor = DrawerVisitor.getVisitor(axes.getParentFigure());
832 if (currentVisitor != null) {
837 AxesDrawer axesDrawer = currentVisitor.getAxesDrawer();
839 width = (double) currentVisitor.getCanvas().getWidth();
840 height = (double) currentVisitor.getCanvas().getHeight();
842 Rectangle2D axesZone = axesDrawer.computeZone(axes);
844 /* Compute the upper-left point's y coordinate */
845 upperLeftY = axesZone.getY() + axesZone.getHeight() * 2.0;
847 /* Convert from normalized coordinates to 2D pixel coordinates */
848 viewingArea[0] = (axesZone.getX() + 1.0) * 0.5 * width;
849 viewingArea[1] = (1.0 - upperLeftY) * 0.5 * height;
850 viewingArea[2] = axesZone.getWidth() * width;
851 viewingArea[3] = axesZone.getHeight() * height;
858 * Returns the culling mode corresponding to front faces.
859 * @return the front face culling mode.
861 public FaceCullingMode getFrontFaceCullingMode() {
862 return this.frontFaceCullingMode;
866 * Returns the culling mode corresponding to back faces.
867 * @return the back face culling mode.
869 public FaceCullingMode getBackFaceCullingMode() {
870 return this.backFaceCullingMode;
874 * Enables clipping for the given {@link ClippableProperty}, which describes
875 * the clipping state of a clippable graphic object.
876 * Depending on the object's clip state property, clipping can be either
877 * disabled (OFF), performed against the parent Axes' box planes (CLIPGRF),
878 * or performed against the planes defined by the object's clip box.
879 * To do: find a better way to compute the clipping planes' offsets as the current one
880 * may lead to problems when the interval between the min and max bounds is too small.
881 * @param parentAxes the clipped object's parent Axes.
882 * @param clipProperty the clipping property of a clippable object.
884 public void enableClipping(Axes parentAxes, ClippableProperty clipProperty) {
885 DrawingTools drawingTools = visitor.getDrawingTools();
887 if (clipProperty.getClipState() != ClipStateType.OFF) {
889 Vector4d[] equations = null;
891 /* Stores the (xmin,xmax) ,(ymin,ymax) and (zmin,zmax) clipping bounds */
892 double[] clipBounds = new double[6];
894 /* The offsets used for the x, y and z planes in order to avoid strict clipping */
895 double[] offsets = new double[3];
897 if (clipProperty.getClipState() == ClipStateType.CLIPGRF) {
899 * All the clipping planes are set as clipping is performed
900 * against the Axes box planes.
903 Double[] bounds = parentAxes.getDisplayedBounds();
905 for (int i = 0; i < numPlanes; i++) {
906 clipBounds[i] = bounds[i];
909 offsets[0] = CLIPPING_EPSILON * (bounds[1] - bounds[0]);
910 offsets[1] = CLIPPING_EPSILON * (bounds[3] - bounds[2]);
911 offsets[2] = CLIPPING_EPSILON * (bounds[5] - bounds[4]);
912 } else if (clipProperty.getClipState() == ClipStateType.ON) {
914 * The clip box property defines values only for the x and y axes,
915 * we therefore set only the x and y clipping planes.
918 Double[] clipBox = clipProperty.getClipBox();
920 /* The clip box stores the upper-left point coordinates. */
921 clipBounds[0] = clipBox[0];
922 clipBounds[1] = clipBox[0] + clipBox[2];
923 clipBounds[2] = clipBox[1] - clipBox[3];
924 clipBounds[3] = clipBox[1];
926 Double[] bounds = parentAxes.getMaximalDisplayedBounds();
929 * The logarithmic scale must be applied to clip bounds values.
930 * If any of them are invalid, we set them to the displayed
931 * left bounds (xmin or ymin).
933 if (parentAxes.getXAxisLogFlag()) {
934 if (clipBounds[0] <= 0.0) {
935 clipBounds[0] = bounds[0];
937 clipBounds[0] = Math.log10(clipBounds[0]);
940 if (clipBounds[1] <= 0.0) {
941 clipBounds[1] = bounds[0];
943 clipBounds[1] = Math.log10(clipBounds[1]);
947 if (parentAxes.getYAxisLogFlag()) {
948 if (clipBounds[2] <= 0.0) {
949 clipBounds[2] = bounds[2];
951 clipBounds[2] = Math.log10(clipBounds[2]);
954 if (clipBounds[3] <= 0.0) {
955 clipBounds[3] = bounds[2];
957 clipBounds[3] = Math.log10(clipBounds[3]);
961 offsets[0] = CLIPPING_EPSILON * (clipBounds[1] - clipBounds[0]);
962 offsets[1] = CLIPPING_EPSILON * (clipBounds[3] - clipBounds[2]);
965 equations = new Vector4d[numPlanes];
967 equations[0] = new Vector4d(+1, 0, 0, -clipBounds[0] + offsets[0]);
968 equations[1] = new Vector4d(-1, 0, 0, +clipBounds[1] + offsets[0]);
969 equations[2] = new Vector4d(0, +1, 0, -clipBounds[2] + offsets[1]);
970 equations[3] = new Vector4d(0, -1, 0, +clipBounds[3] + offsets[1]);
972 /* If clipping is performed against the Axes box, the z plane equations must be initialized. */
973 if (numPlanes == 6) {
974 equations[4] = new Vector4d(0, 0, +1, -clipBounds[4] + offsets[2]);
975 equations[5] = new Vector4d(0, 0, -1, +clipBounds[5] + offsets[2]);
978 Transformation currentTransformation = drawingTools.getTransformationManager().getTransformation();
979 for (int i = 0 ; i < numPlanes; i++) {
980 ClippingPlane plane = drawingTools.getClippingManager().getClippingPlane(i);
981 plane.setTransformation(currentTransformation);
982 plane.setEquation(equations[i]);
983 plane.setEnable(true);
989 * Disables clipping for the given {@link ClippableProperty}.
990 * @param clipProperty the clip property for which clipping is disabled.
992 public void disableClipping(ClippableProperty clipProperty) {
995 if (clipProperty.getClipState() == ClipStateType.CLIPGRF) {
997 } else if (clipProperty.getClipState() == ClipStateType.ON) {
1001 for (int i = 0 ; i < numPlanes; i++) {
1002 ClippingPlane plane = visitor.getDrawingTools().getClippingManager().getClippingPlane(i);
1003 plane.setEnable(false);
1006 visitor.getDrawingTools().getClippingManager().disableClipping();
1010 * Returns the x-axis label positioner.
1011 * @return the x-axis label positioner.
1013 public AxisLabelPositioner getXAxisLabelPositioner() {
1014 return this.xAxisLabelPositioner;
1018 * Returns the y-axis label positioner.
1019 * @return the y-axis label positioner.
1021 public AxisLabelPositioner getYAxisLabelPositioner() {
1022 return this.yAxisLabelPositioner;
1026 * Returns the z-axis label positioner.
1027 * @return the z-axis label positioner.
1029 public AxisLabelPositioner getZAxisLabelPositioner() {
1030 return this.zAxisLabelPositioner;
1034 * Returns the title positioner.
1035 * @return the title positioner.
1037 public LabelPositioner getTitlePositioner() {
1038 return this.titlePositioner;
1041 public void disposeAll() {
1042 this.rulerDrawer.disposeAll();
1043 this.projectionMap.clear();
1044 this.projection2dViewMap.clear();
1045 this.sceneProjectionMap.clear();
1048 public void update(String id, int property) {
1049 this.rulerDrawer.update(id, property);
1052 public void dispose(String id) {
1053 this.rulerDrawer.dispose(id);
1054 projectionMap.remove(id);
1055 projection2dViewMap.remove(id);
1056 sceneProjectionMap.remove(id);