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 import javax.vecmath.*; 24 import javax.media.j3d.*; 25 import com.sun.j3d.utils.geometry.*; 26 import camera3d.*; 27 import camera3d.test.NodeTester; 28 29 /*** 30 * Gizmo for showing geometry that represent planes. 31 * @author Carlos da Silva dos Santos, Fábio de Miranda 32 */ 33 public class PlaneGizmo extends SimpleGizmo { 34 35 private Vector3d tempVec3d = new Vector3d(); 36 private Vector3d tempVec3d2 = new Vector3d(); 37 38 /*** Holds parameters fo the plane equation */ 39 private Vector4d plane = new Vector4d(); 40 41 private double width = 2.0; 42 private Point3d tempPt[] = new Point3d[4]; 43 private Point3d tempP3d = new Point3d(); 44 45 /*** Holds vector normal to plane */ 46 private Vector3d planeNormal = new Vector3d(); 47 48 /*** Holds a point that belongs to plane */ 49 private Point3d planePoint = new Point3d(); 50 51 private Vector3d normal = new Vector3d(); 52 private Vector3d r = new Vector3d(); 53 private Vector3d s = new Vector3d(); 54 private Color3f ownColor = new Color3f(0.f, 0.f, 1.0f); 55 56 private Transform3D tempT3D = new Transform3D(); 57 58 private Point3d p1 = new Point3d(); 59 private Point3d p2 = new Point3d(); 60 private Point3d p3 = new Point3d(); 61 62 /*** 63 * Creates representation of a plane that contains the three points given as input. 64 * @param pt1 first point belonging to plane 65 * @param pt2 second point belonging to plane 66 * @param pt3 third point belonging to plane 67 */ 68 public PlaneGizmo(Point3d pt1, Point3d pt2, Point3d pt3){ 69 super(); 70 this.p1.set(pt1); 71 this.p2.set(pt2); 72 this.p3.set(pt3); 73 this.width = 5.0; 74 buildPlanePointAndNormal(planePoint, planeNormal, p1, p2, p3); 75 debugln("Generated Plane point "+planePoint); 76 debugln("Generated Plane normal "+planeNormal); 77 buildPlane(planePoint, planeNormal); 78 if (shape==null) { 79 System.out.println("Null shape3d added to plane in plane gizmo"); 80 } 81 else { 82 this.addChild(shape); 83 } 84 } 85 86 public PlaneGizmo(Color3b color, Point3d p1, Point3d p2, Point3d p3){ 87 this(p1, p2, p3); 88 ownColor.set(color.get()); 89 Material m = shape.getAppearance().getMaterial(); 90 if (m!=null){ 91 m.setAmbientColor(ownColor); 92 m.setDiffuseColor(ownColor); 93 m.setEmissiveColor(ownColor); 94 } else { 95 System.out.println("m is null"); 96 } 97 } 98 99 public PlaneGizmo(Color3b color, Vector4d plane){ 100 super(); 101 ownColor.set(color.get()); 102 this.width = 5.0; 103 MathUtility.planeToPointAndNormal(plane, planeNormal, planePoint); 104 //buildPlanePointAndNormal(plane, planeNormal, planePoint); 105 //buildPlanePointAndNormal(Vector4d plane, Vector3d planeNormal, Point3d planePoint) 106 buildPlane(planePoint, planeNormal); 107 if (shape==null) { 108 System.out.println("Null shape3d added to plane in plane gizmo"); 109 } 110 else { 111 this.addChild(shape); 112 } 113 } 114 115 /* 116 public PlaneGizmo(Color3d color, Tuple4d plane, double width){ 117 } 118 */ 119 120 public PlaneGizmo(Vector4d plan){ 121 super(new Color3b((byte)56,(byte)56,(byte)200)); 122 this.plane.set(plan); 123 this.width = 5.0; 124 MathUtility.planeToPointAndNormal(plane, planeNormal, planePoint); 125 //buildPlanePointAndNormal(plane, planeNormal, planePoint); 126 //buildPlanePointAndNormal(Vector4d plane, Vector3d planeNormal, Point3d planePoint) 127 buildPlane(planePoint, planeNormal); 128 if (shape==null) { 129 System.out.println("Null shape3d added to plane in plane gizmo"); 130 } 131 else { 132 this.addChild(shape); 133 } 134 } 135 136 public PlaneGizmo(Vector4d plan, Color3b color){ 137 this(color, plan); 138 } 139 140 141 /* 142 public PlaneGizmo(Color3d color, Point3d planePoint, Vector3d planeNormal){ 143 } 144 145 */ 146 147 public PlaneGizmo(Color3b color, Point3d inPlanePoint, Vector3d inPlaneNormal, double width){ 148 super(color); 149 this.planePoint.set(inPlanePoint); 150 this.planeNormal.set(inPlaneNormal); 151 ownColor.set(color.get()); 152 this.width = width; 153 // At this point we know: 154 // The point where to center the plane 155 // Its equation 156 // We can create now a Quad Shape and Stripify it 157 buildPlane(planePoint, planeNormal); 158 if (shape==null) { 159 System.out.println("Null shape3d added to plane in plane gizmo"); 160 } 161 else { 162 this.addChild(shape); 163 } 164 } 165 166 public PlaneGizmo(Point3d planePoint, Vector3d planeNormal){ 167 this(new Color3b((byte) 0, (byte)128, (byte)127), planePoint, planeNormal, 6.0); 168 } 169 170 /*** 171 * Creates shape that represents plane. 172 * @param planePoint point that belongs to the plane 173 * @param planeVec vector normal to plane 174 */ 175 void buildPlane(Point3d planePoint, Vector3d planeVec){ 176 177 for (int i = 0; i < tempPt.length; i++) { 178 tempPt[i] = new Point3d(); 179 } 180 181 double size = width/2; 182 183 // Finding 4 points close to desired point in plane 184 // finds perpendicular vectors r and s that are contained in plane 185 buildBase(planePoint, planeVec, normal, r, s); 186 debugln("Vectors of base:"); 187 debugln("r "+r+", s "+s+", n "+normal); 188 // +r + s 189 addToPoint(tempPt[0], planePoint, size, r, size, s); 190 // +r - s 191 addToPoint(tempPt[1], planePoint, size, r, -size, s); 192 // -r -s 193 addToPoint(tempPt[2], planePoint, -size, r, -size, s); 194 // -r + s 195 addToPoint(tempPt[3], planePoint, -size, r, size, s); 196 197 for (int i = 0; i < tempPt.length; i++) { 198 debugln(i+"th Point from quad :"+tempPt[i]); 199 } 200 201 202 // Now we can build a quad from 4 points and stripify it 203 GeometryInfo geoInfo = new GeometryInfo(GeometryInfo.QUAD_ARRAY); 204 geoInfo.setCoordinates(tempPt); 205 NormalGenerator generator = new NormalGenerator(); 206 generator.generateNormals(geoInfo); 207 Stripifier stripifier = new Stripifier(); 208 stripifier.stripify(geoInfo); 209 210 Appearance appearance = new Appearance(); 211 Material material = new Material(); 212 material.setLightingEnable(true); 213 material.setEmissiveColor(ownColor); 214 material.setAmbientColor(ownColor); 215 material.setDiffuseColor(ownColor); 216 PolygonAttributes attributes = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0.4f); 217 //attributes.setCullFace(PolygonAttributes.CULL_NONE); 218 appearance.setMaterial(material); 219 appearance.setPolygonAttributes(attributes); 220 TransparencyAttributes transp = new TransparencyAttributes(TransparencyAttributes.FASTEST, 0.35f); 221 appearance.setTransparencyAttributes(transp); 222 //RenderingAttributes render = new RenderingAttributes(); 223 224 GeometryArray gArray = geoInfo.getGeometryArray(); 225 shape = new Shape3D(gArray, appearance); 226 } 227 228 public void addToPoint(Point3d result, Point3d basePoint, double f1, Vector3d v1, double f2, Vector3d v2){ 229 result.scaleAdd(f1, v1, basePoint); 230 result.scaleAdd(f2, v2, result); 231 } 232 233 234 public void buildPlane4dFromPointAndNormal(Vector4d result, Point3d planePoint, Vector3d planeNormal){ 235 tempVec3d.set(planeNormal); 236 tempVec3d.normalize(); 237 result.x = tempVec3d.x; 238 result.y = tempVec3d.y; 239 result.z = tempVec3d.z; 240 double n = result.x * planePoint.x + result.y *planePoint.y + result.z * planePoint.z; 241 result.w = -n; 242 } 243 244 /*** 245 * Given points where x and z values are know to belong to a certain plane, finds y value that makes it consistent 246 */ 247 void replaceForY(Point3d point, Vector4d plane){ 248 point.y = -(plane.x*point.x + plane.z*point.z + plane.w)/plane.y; 249 } 250 251 /*** 252 * Finds another place for points at given distance of center. The new position of 253 * the point still lies along the center-point vector 254 */ 255 void movePoint(Point3d center, Point3d point, double distance){ 256 } 257 258 public void debugln(String s){ 259 System.out.println(s); 260 } 261 262 263 String dbgTuple3d(Tuple3d tup){ 264 return " ("+tup.x+","+tup.y+","+tup.z+")"; 265 } 266 267 String dbgTuple4d(Tuple4d tup){ 268 return " ("+tup.x+","+tup.y+","+tup.z+","+tup.w+")"; 269 } 270 271 /*** 272 * Builds a base of vectors. 273 * @planePoint point belonging to plane. Input parameter. 274 * @param pointLine vector normal to plane. Input parameter. 275 * @param normal vector normal to plane and of length = 1. Output parameter. 276 * @param r vector belonging to the plane. Output parameter. 277 * @param s vector belonging to the plane and perpendicular to r. Output parameter. 278 */ 279 void buildBase(Point3d planePoint, Vector3d pointLine, Vector3d normal, Vector3d r, Vector3d s){ 280 281 normal.set(pointLine); 282 normal.normalize(); 283 284 tempP3d.set(planePoint); 285 /* 286 * Sets up a vector with rotation along 3 axes 287 */ 288 tempVec3d2.set(Math.PI/4, Math.PI/4, Math.PI/4); 289 tempT3D.setEuler(tempVec3d2); 290 tempVec3d2.set(pointLine); 291 /* 292 * Rotates a vector that was coincident to pointLine in x, y and z, to make 293 * sure it no longer coincides with that vector 294 */ 295 tempT3D.transform(tempVec3d2); 296 297 // Summing the resulting vector to a point that was set to be the same as planePoint, 298 // we get a point that doesn't belong to the line for sure 299 tempP3d.add(tempVec3d2); 300 301 // One vector connecting a random point to a point that belongs to this plane 302 tempVec3d.sub(planePoint, tempP3d); 303 r.cross(tempVec3d, normal); 304 r.normalize(); 305 s.cross(normal, r); 306 s.normalize(); // I think this is unnecessary, but anyway 307 // Now normal, r and s are a base 308 } 309 310 public String getLabel(){ 311 return this.label; 312 } 313 314 315 /*** 316 * Calculates the normal vector and a plane point for the plane that contains the 317 * points p1, p2 and p3. 318 * @param planePoint Calculated point that belongs to plane. Output parameter. 319 * @param planeNormal Calculates plane normal. Output parameter. 320 * @param p1 first point that belongs to plane. 321 * @param p2 second point that belongs to plane. 322 * @param p3 third point that belongs to plane. 323 */ 324 void buildPlanePointAndNormal(Point3d planePoint, Vector3d planeNormal, Point3d p1, Point3d p2, Point3d p3){ 325 planePoint.set(p1); 326 planePoint.add(p2); 327 planePoint.add(p3); 328 // Planepoint averages other 3 points 329 planePoint.x/=3; 330 planePoint.y/=3; 331 planePoint.z/=3; 332 tempVec3d.sub(p1, p2); 333 planeNormal.sub(p3, p2); 334 planeNormal.cross(tempVec3d, planeNormal); 335 planeNormal.normalize(); 336 this.width = 2*Math.max(p1.distance(p2), Math.max(p2.distance(p3), p1.distance(p3))); 337 } 338 339 /*** 340 * 341 */ 342 static public void main(String[] args) { 343 NodeTester tester = new NodeTester(); 344 Point3d origin = new Point3d(1.0, 1.0, 1.0); 345 Vector3d direction = new Vector3d(1.0, 1.0, 1.0); 346 PlaneGizmo plane = new PlaneGizmo(origin, direction); 347 //tester.add(plane); 348 PointGizmo point = new PointGizmo(origin); 349 //tester.add(point); 350 LineGizmo line = new LineGizmo(origin, direction, 2.0); 351 //tester.add(line); 352 353 Point3d farPoint = new Point3d(1.0, 1.0, 2.0); 354 PlaneGizmo planeGizmo = new PlaneGizmo(farPoint, direction); 355 356 Vector4d newPlane = new Vector4d(); 357 planeGizmo.buildPlane4dFromPointAndNormal(newPlane, farPoint, direction); 358 359 /*Testing the Vector4f-based constructor*/ 360 planeGizmo = new PlaneGizmo(newPlane); 361 //tester.add(planeGizmo); 362 363 Point3d p1 = new Point3d(0.0,0.0,0.0); 364 PointGizmo p1g = new PointGizmo(p1); 365 Point3d p2 = new Point3d(0.0,1.0,-1.0); 366 PointGizmo p2g = new PointGizmo(p2); 367 Point3d p3 = new Point3d(3.0,-1.0,1.0); 368 PointGizmo p3g = new PointGizmo(p3); 369 370 /*Testing the 3-point based constructor*/ 371 //PlaneGizmo planeGiz = new PlaneGizmo(new Color3b((byte)0,(byte)127,(byte)0),p1,p2,p3); 372 /*** Comentado para ficar mais fácil resolver o problema logo abaixo 373 * 374 PlaneGizmo planeGiz = new PlaneGizmo(p1,p2,p3); 375 tester.add(planeGiz); 376 tester.add(p1g); 377 tester.add(p2g); 378 tester.add(p3g); 379 */ 380 381 //Point3d x1 = new Point3d(0.0, 1.0, 0.0); 382 System.out.println("TEST TEST TEST"); 383 Point3d x1 = new Point3d(0.0000, 1.0, 0.0); 384 Point3d x2 = new Point3d(0.0, 0.0, 1.0); 385 Point3d x3 = new Point3d(0.0, 0.0, -1.0); 386 387 tester.add(new PointGizmo(x1)); 388 tester.add(new PointGizmo(x2)); 389 tester.add(new PointGizmo(x3)); 390 391 PlaneGizmo planeQ = new PlaneGizmo(x1, x2, x3); 392 tester.add(planeQ); 393 } 394 395 public Color3b getDefaultColor(){ 396 color.set(ownColor.get()); 397 return color; 398 } 399 }

This page was automatically generated by Maven