2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte Denizet
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.1-en.txt
12 package org.scilab.forge.scirenderer.implementation.g2d.motor;
14 import java.awt.Color;
15 import java.awt.Graphics2D;
16 import java.awt.Stroke;
17 import java.awt.geom.Path2D;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.HashSet;
24 import org.scilab.forge.scirenderer.tranformations.Vector3d;
25 import org.scilab.forge.scirenderer.tranformations.Vector4d;
28 * @author Calixte DENIZET
30 public class Segment extends ConvexObject implements Comparable<Segment> {
33 protected Stroke stroke;
34 protected List<ConvexObject> segmentOn;
36 public Segment(Vector3d[] vertices, Color[] colors, Stroke stroke) throws InvalidPolygonException {
37 super(vertices, colors);
38 if (vertices.length != 2) {
39 throw new InvalidPolygonException("Invalid segment: must have 2 vertices.");
44 public Segment(Vector3d[] vertices, Color[] colors) throws InvalidPolygonException {
45 this(vertices, colors, null);
48 public void setStroke(Stroke stroke) {
52 public double getLength() {
53 return vertices[0].minus(vertices[1]).getNorm();
56 public static double getLength(Vector3d[] vertices) {
57 return vertices[0].minus(vertices[1]).getNorm();
60 public void addConvexObject(ConvexObject co) {
61 if (segmentOn == null) {
62 segmentOn = new ArrayList<ConvexObject>(2);
67 public void removeConvexObject(ConvexObject co) {
68 if (segmentOn != null) {
73 public void replaceSegment(List<Segment> segs) {
74 if (segmentOn != null) {
75 for (ConvexObject co : segmentOn) {
76 Triangle t = (Triangle) co;
77 t.replaceSegment(this, segs);
82 public boolean isIn2D() {
83 return isNull(vertices[0].getZ()) && isNull(vertices[1].getZ());
86 public boolean isInFront() {
87 return isEqual(vertices[0].getZ(), -0.5) && isEqual(vertices[1].getZ(), -0.5);
91 public int compareTo(Segment o) {
96 return getPrecedence() - o.getPrecedence();
100 public boolean equals(Object o) {
101 if (o instanceof Segment) {
102 Segment s = (Segment) o;
103 return (s.vertices[0].equals(vertices[0]) && s.vertices[1].equals(vertices[1]) && s.colors[0].equals(colors[0]) && s.colors[1].equals(colors[1])) || (s.vertices[1].equals(vertices[0]) && s.vertices[0].equals(vertices[1]) && s.colors[1].equals(colors[0]) && s.colors[0].equals(colors[1]));
110 public int isBehind(ConvexObject o) {
111 if (o instanceof Triangle && ((Triangle) o).isSegmentAcross(this)) {
115 return super.isBehind(o);
119 public List<ConvexObject> breakObject(ConvexObject o) {
120 if (o instanceof Triangle) {
121 return ((Triangle) o).breakObject(this);
122 } else if (o instanceof SpritedRectangle) {
123 return ((SpritedRectangle) o).breakObject(this);
130 public List<ConvexObject> breakObject(Vector4d v) {
131 double[] vv = v.getData();
132 Vector3d np = new Vector3d(vv);
133 boolean a = isBehind(vertices[0], np, vv[3]);
134 boolean b = isBehind(vertices[1], np, vv[3]);
137 List<ConvexObject> list = new ArrayList<ConvexObject>(1);
146 double c = (vv[3] + vertices[1].scalar(np)) / v0.scalar(np);
147 Vector3d p = Vector3d.getBarycenter(vertices[0], vertices[1], c, 1 - c);
148 Color color = getColorsBarycenter(colors[0], colors[1], c, 1 - c);
149 Vector3d[] vs = null;
153 vs = new Vector3d[] {vertices[0], p};
154 cs = new Color[] {colors[0], color};
158 vs = new Vector3d[] {p, vertices[1]};
159 cs = new Color[] {color, colors[1]};
163 List<ConvexObject> list = new ArrayList<ConvexObject>(1);
164 list.add(new Segment(vs, cs, this.stroke));
167 } catch (InvalidPolygonException e) { }
172 public List<Segment> breakObject(Vector3d p, Vector3d u, Vector3d n) {
173 double c = vertices[1].minus(p).scalar(n) / v0.scalar(n);
174 if (c > 0 && !isNull(c) && c < 1 && !isEqual(c, 1)) {
175 List<Segment> list = new ArrayList<Segment>(2);
176 Vector3d q = Vector3d.getBarycenter(vertices[0], vertices[1], c, 1 - c);
177 Color color = getColorsBarycenter(colors[0], colors[1], c, 1 - c);
179 list.add(new Segment(new Vector3d[] {vertices[0], q}, new Color[] {colors[0], color}, stroke));
180 list.add(new Segment(new Vector3d[] {q, vertices[1]}, new Color[] {color, colors[1]}, stroke));
183 } catch (InvalidPolygonException e) { }
185 List<Segment> list = new ArrayList<Segment>(1);
187 list.add(new Segment(new Vector3d[] {vertices[0], vertices[1]}, new Color[] {colors[0], colors[1]}, stroke));
190 } catch (InvalidPolygonException e) { }
197 public void draw(Graphics2D g2d) {
198 if (segmentOn == null || segmentOn.isEmpty()) {
199 Path2D polyline = getProjectedPolyLine();
200 g2d.setColor(colors[0]);
201 Stroke oldStroke = g2d.getStroke();
202 if (oldStroke != stroke) {
203 g2d.setStroke(stroke);
208 if (oldStroke != stroke) {
209 g2d.setStroke(oldStroke);
217 public int hashCode() {
219 hash = Arrays.hashCode(vertices) + 19 * Arrays.hashCode(colors);
225 public String toString() {
226 return "Segment " + vertices[0].toString() + " " + vertices[1].toString() + " Precedence: " + getPrecedence();