View Javadoc
1 /***************************************************************************** 2 * Virtual Mockup for Machine Vision 3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr., 4 * Carlos S. Santos. 5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP 6 * 7 * Permission is granted to redistribute and/or modify this 8 * software under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html) 16 * for more details. 17 * 18 *****************************************************************************/ 19 20 package camera3d.gizmo; 21 22 23 24 25 import javax.vecmath.*; 26 import javax.media.j3d.*; 27 import com.sun.j3d.utils.geometry.*; 28 import camera3d.test.NodeTester; 29 30 /*** 31 * @author Carlos da Silva dos Santos e Fábio de Miranda 32 * @version 1.0 33 */ 34 public class SimplePlaneGizmo extends SimpleBagGizmo { 35 36 private Vector3d tempVec3d = new Vector3d(); 37 private Vector4d plane = new Vector4d(); 38 39 private double width = 4.0; 40 private Point3d tempPt[] = new Point3d[4]; 41 private Point3d tempP3d = new Point3d(); 42 43 private Vector3d planeNormal = new Vector3d(); 44 private Vector3d planePoint = new Vector3d(); 45 private Vector3d normal = new Vector3d(); 46 private Vector3d r = new Vector3d(); 47 Vector3d s = new Vector3d(); 48 Color3f blue = new Color3f(0.f, 0.f, 0.6f); 49 50 /* 51 public SimplePlaneGizmo(Color3b color, Point3d p1, Point3d p2, Point3d p3) { 52 super(color); 53 } 54 55 public SimplePlaneGizmo(Color3d color, Point3d p1, Point3d p2, Point3d p3, double width) { 56 super(color); 57 } 58 59 60 public SimplePlaneGizmo(Point3d p1, Point3d p2, Point3d p3){ 61 } 62 63 public SimplePlaneGizmo(Color3d color, Tuple4d plane){ 64 } 65 66 public SimplePlaneGizmo(Color3d color, Tuple4d plane, double width){ 67 } 68 69 70 public SimplePlaneGizmo(Tuple4d plane){ 71 } 72 73 public SimplePlaneGizmo(Color3d color, Point3d planePoint, Vector3d planeNormal){ 74 } 75 76 */ 77 78 public SimplePlaneGizmo(Color3b color, Point3d planePoint, Vector3d planeNormal, double width){ 79 super(color); 80 this.width = width; 81 // At this point we know: 82 // The point where to center the plane 83 // Its equation 84 // We can create now a Quad Shape and Stripify it 85 buildPlane(planePoint, planeNormal); 86 if (shape==null) { 87 System.out.println("Null shape3d added to plane in plane gizmo"); 88 } 89 else { 90 this.addChild(shape); 91 } 92 93 94 } 95 96 public SimplePlaneGizmo(Point3d planePoint, Vector3d planeNormal){ 97 this(new Color3b((byte) 0, (byte)128, (byte)255), planePoint, planeNormal, 6.0); 98 } 99 100 void buildPlane(Point3d planePoint, Vector3d planeVec){ 101 //buildPlane4dFromPointAndNormal(plane, point, normal); 102 //tempP3d.set(planePoint); 103 104 for (int i = 0; i < tempPt.length; i++) { 105 tempPt[i] = new Point3d(); 106 } 107 108 // Finding 4 points close to desired point in plane 109 buildBase(planePoint, planeVec, normal, r, s); 110 // +r + s 111 addToPoint(tempPt[0], planePoint, 1.0, r, 1.0, s); 112 // +r - s 113 addToPoint(tempPt[1], planePoint, 1.0, r, -1.0, s); 114 // -r -s 115 addToPoint(tempPt[2], planePoint, -1.0, r, -1.0, s); 116 // -r + s 117 addToPoint(tempPt[3], planePoint, -1.0, r, 1.0, s); 118 119 // Now we can build a quad from 4 points and stripify it 120 GeometryInfo geoInfo = new GeometryInfo(GeometryInfo.QUAD_ARRAY); 121 geoInfo.setCoordinates(tempPt); 122 NormalGenerator generator = new NormalGenerator(); 123 generator.generateNormals(geoInfo); 124 Stripifier stripifier = new Stripifier(); 125 stripifier.stripify(geoInfo); 126 127 Appearance appearance = new Appearance(); 128 Material material = new Material(); 129 material.setLightingEnable(true); 130 material.setEmissiveColor(blue); 131 material.setAmbientColor(blue); 132 material.setDiffuseColor(blue); 133 PolygonAttributes attributes = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0.4f); 134 //attributes.setCullFace(PolygonAttributes.CULL_NONE); 135 appearance.setMaterial(material); 136 appearance.setPolygonAttributes(attributes); 137 //RenderingAttributes render = new RenderingAttributes(); 138 139 GeometryArray gArray = geoInfo.getGeometryArray(); 140 shape = new Shape3D(gArray, appearance); 141 } 142 143 public void addToPoint(Point3d result, Point3d basePoint, double f1, Vector3d v1, double f2, Vector3d v2){ 144 result.scaleAdd(f1, v1, basePoint); 145 result.scaleAdd(f2, v2, result); 146 } 147 148 /* 149 public SimplePlaneGizmo(Point3d planePoint, Vector3d planeNormal){ 150 } 151 152 */ 153 154 /*** 155 * Takes 3 points as input and builds a plane Description from it 156 */ 157 158 void buildPlane4dDescription(Vector4d plane, Point3d p1, Point3d p2, Point3d p3){ 159 debugln("Build plane from: ("+p1.x+","+p1.y+","+p1.z+") ("+p2.x+","+p2.y+","+p2.z+") ("+p3.x+","+p3.y+","+p3.z+")"); 160 // Now we must find the plane defined by the points p1, p2 e p3 161 plane.x = p1.y *(p2.z - p3.z) + p2.y *(p3.z - p1.z) + p3.y *(p1.z - p2.z) ; 162 plane.y = p1.z *(p2.x - p3.x) + p2.z *(p3.x - p1.x) + p3.z *(p1.x - p2.x); 163 plane.z = p1.x *(p2.y - p3.y) + p2.x *(p3.y - p1.y) + p3.x *(p1.y - p2.y); 164 plane.w =- ( p1.x *(p2.y *p3.z - p3.y *p2.z) + p2.x *(p3.y *p1.z - p1.y *p3.z) + p3.x *(p1.y *p2.z - p2.y *p1.z)); 165 debugln(" Resulting plane ("+plane.x+", "+plane.y+", "+plane.z+", "+plane.w+")"); 166 } 167 168 /*** 169 * Converts a plane in the form Ax + By + Cz + D to a plane represented by a normal 170 * and a point 171 * @param plane is the Ax + By + Cz + D representation of the plan 172 * @param planeNormal is the resulting plane normal (Bet you could've guessed that :) ) 173 * @param planePoint is a point belonging to the plane 174 */ 175 void buildPlanePointAndNormal(Vector4d plane, Vector3d planeNormal, Point3d planePoint){ 176 planeNormal.x = plane.x; 177 planeNormal.y = plane.y; 178 planeNormal.z = plane.z; 179 180 planePoint.x = 0.0; 181 planePoint.y = 0.0; 182 planePoint.z = -(plane.w/plane.z); 183 debugln("Building plane in point and normal form.\n From "+dbgTuple4d(plane)+"Point: "+dbgTuple3d(planePoint)+" Normal: "+dbgTuple3d(planeNormal)); 184 } 185 186 void buildPlane4dFromPointAndNormal(Vector4d result, Point3d planePoint, Vector3d planeNormal){ 187 tempVec3d.set(planeNormal); 188 //tempVec3d.normalize(); 189 result.x = tempVec3d.x; 190 result.y = tempVec3d.y; 191 result.z = tempVec3d.z; 192 double n = result.x * planePoint.x + result.y *planePoint.y + result.z * planePoint.z; 193 result.w = -n; 194 } 195 196 /*** 197 * Given points where x and z values are know to belong to a certain plane, finds y value that makes it consistent 198 */ 199 void replaceForY(Point3d point, Vector4d plane){ 200 point.y = -(plane.x*point.x + plane.z*point.z + plane.w)/plane.y; 201 } 202 203 /*** 204 * Finds another place for points at given distance of center. The new position of 205 * the point still lies along the center-point vector 206 */ 207 void movePoint(Point3d center, Point3d point, double distance){ 208 } 209 210 public void debugln(String s){ 211 } 212 213 214 String dbgTuple3d(Tuple3d tup){ 215 return " ("+tup.x+","+tup.y+","+tup.z+")"; 216 } 217 218 String dbgTuple4d(Tuple4d tup){ 219 return " ("+tup.x+","+tup.y+","+tup.z+","+tup.w+")"; 220 } 221 222 void buildBase(Point3d planePoint, Vector3d pointLine, Vector3d normal, Vector3d r, Vector3d s){ 223 normal.x = pointLine.x; 224 normal.y = pointLine.y; 225 normal.z = pointLine.z; 226 normal.normalize(); 227 228 // Pick a random point that does not belong to the plane 229 tempP3d.set(planePoint); 230 tempP3d.x = tempP3d.x + 30; 231 232 // One vector connecting a random point to a point that belongs to this plane 233 tempVec3d.sub(planePoint, tempP3d); 234 r.cross(tempVec3d, normal); 235 r.normalize(); 236 s.cross(normal, r); 237 s.normalize(); // I think this is unnecessary, but anyway 238 239 // Now normal, r and s are a base 240 } 241 242 /*** 243 * 244 */ 245 static public void main(String[] args) { 246 NodeTester tester = new NodeTester(); 247 Point3d origin = new Point3d(1.0, 1.0, 1.0); 248 Vector3d direction = new Vector3d(1.0, 1.0, 1.0); 249 SimplePlaneGizmo plane = new SimplePlaneGizmo(origin, direction); 250 tester.add(plane); 251 SimplePointGizmo point = new SimplePointGizmo(origin); 252 tester.add(point); 253 SimpleLineGizmo line = new SimpleLineGizmo(origin, direction, 2.0); 254 tester.add(line); 255 256 } 257 258 }

This page was automatically generated by Maven