[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model Surface.java,1.7,1.8
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2005-08-05 10:42:18
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27439 Modified Files: Surface.java Log Message: Added methods for calculating normals Index: Surface.java =================================================================== RCS file: /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/Surface.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** Surface.java 4 Aug 2005 11:11:49 -0000 1.7 --- Surface.java 5 Aug 2005 10:42:09 -0000 1.8 *************** *** 8,11 **** --- 8,12 ---- import java.io.Serializable; + import java.util.Iterator; import java.util.List; *************** *** 130,133 **** --- 131,190 ---- /** + * returns the normal to this + * @return A vector normal for this surface, null if the surface consists of only one edge + */ + public Vertex normal() { + if (edges.size() > 1) { + Iterator it = edges.iterator(); + EdgeFacade ef = EdgeFacade.getInstance(); + Edge e1 = ef.findById(((Edge)it.next()).getId()); + Edge e2 = ef.findById(((Edge)it.next()).getId()); + Vertex v1 = e1.getFrom(); + Vertex v2 = e1.getTo(); + Vertex v3 = e2.getTo(); + + if (v3.equals(v2) || v3.equals(v1)) { + v3 = e2.getFrom(); + } + Vertex v2v1 = v1.minus(v2); + Vertex v2v3 = v3.minus(v2); + return v2v1.cross(v2v3); + } + return null; + } + + /** + * Retruns the projection on the surface normal + * @param p The point to project on the normal + * @return the projection + */ + public Vertex projection(double[] p) { + if (edges.size() > 0) { + Iterator it = edges.iterator(); + Vertex from = ((Edge)it.next()).getTo(); + Vertex point = new Vertex(); + point.setX(p[0] - from.getX()); + point.setY(p[1] - from.getY()); + point.setZ(p[2] - from.getZ()); + Vertex normal = normal(); + double dot = normal.dot(point); + double length = normal.length(); + double scale = dot / (length * length); + normal.scale(scale); + return normal; + } + return null; + } + + /** + * Checks if e is in the surface + * @param e The edge + * @return True if e is in this otherwise false + */ + public boolean contains(Edge e) { + return edges.contains(e); + } + + /** * Return the hash code of the object * @return The hash |