1 /* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License as 4 * published by the Free Software Foundation; either version 2 of the 5 * License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, but 8 * WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOUSE. See the GNU 10 * General Public License for more details. 11 * 12 * You should have recieved a copy of the GNU General Public License 13 * along with this program; if not write to the Free Software 14 * Foundation, inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 15 * USA 16 */ 17 package j3d; 18 19 import java.awt.*; 20 import java.awt.event.ActionEvent; 21 import java.awt.event.KeyAdapter; 22 import java.awt.event.KeyEvent; 23 import java.awt.geom.Rectangle2D; 24 import javax.swing.tree.*; 25 import java.io.*; 26 import javax.swing.*; 27 28 import com.stevesoft.pat.Regex; 29 30 import gui.*; 31 /** 32 * Insert the type's description here. 33 * 34 * @author: Yuriy Mikhaylovskiy 35 */ 36 37 public class _Point extends _Geometry implements Serializable{ 38 public float x,y,z,vx,vy,vz,x2,y2,z2; 39 public Color color; 40 public boolean selected = false; 41 private JTextField tx,ty,tz,tvx,tvy,tvz; 42 private Object[] arr = new Object[2]; 43 private static Regex r_index = new Regex("^ *([0-9]+) *"); 44 private static Regex r_x = new Regex(" X *= *([-+]?[0-9]+[.]?[0-9]*(?:[eE][-+]?[0-9]+)?) *"); 45 private static Regex r_y = new Regex(" Y *= *([-+]?[0-9]+[.]?[0-9]*(?:[eE][-+]?[0-9]+)?) *"); 46 private static Regex r_z = new Regex(" Z *= *([-+]?[0-9]+[.]?[0-9]*(?:[eE][-+]?[0-9]+)?) *"); 47 48 writeObject()49 public String writeObject() { 50 String st = new String(Id); 51 st+=" \tX = " + x + "\tY = " + y + "\tZ = " + z; 52 53 return st; 54 } 55 readObject(String st)56 public void readObject(String st) { 57 if (r_index.search(st)) Id = r_index.stringMatched(1); 58 if (r_x.search(st)) x = Float.parseFloat(r_x.stringMatched(1)); 59 if (r_y.search(st)) y = Float.parseFloat(r_y.stringMatched(1)); 60 if (r_z.search(st)) z = Float.parseFloat(r_z.stringMatched(1)); 61 } 62 deselectRequiredObjects()63 public void deselectRequiredObjects() { 64 // Do nothing 65 } 66 reset(boolean do_mesh)67 public void reset(boolean do_mesh) { 68 69 x2 = x+vx; y2 = y+vy; z2 = z+vz; 70 71 }; mesh(int type, float size)72 public void mesh(int type, float size){} delete()73 public boolean delete(){return selected;} 74 _Point(boolean add)75 public _Point(boolean add) { 76 this(0,0,0,0,0,0,Color.black,add); 77 } 78 _Point(_Point p)79 public _Point(_Point p) { 80 this(p.x,p.y,p.z,p.vx,p.vy,p.vz,p.color,false); 81 } 82 _Point(float xx, float yy, float zz, float vx, float vy, float vz, Color cl,boolean add)83 public _Point(float xx, float yy, float zz, float vx, float vy, float vz, Color cl,boolean add) { 84 this.add = add; 85 x=xx; 86 y=yy; 87 z=zz; 88 this.vx = vx; 89 this.vy = vy; 90 this.vz = vz; 91 color=cl; 92 } 93 _Point(float xx, float yy, float zz, float vx, float vy, float vz, Color cl)94 public _Point(float xx, float yy, float zz, float vx, float vy, float vz, Color cl) { 95 this(xx,yy,zz,vx,vy,vz,cl,false); 96 } 97 _Point(float xx, float yy, float zz, float vx, float vy, float vz)98 public _Point(float xx, float yy, float zz, float vx, float vy, float vz) { 99 this(xx,yy,zz,vx,vy,vz,Color.black,false); 100 } 101 _Point(float xx, float yy, float zz, Color cl)102 public _Point(float xx, float yy, float zz, Color cl) { 103 this(xx,yy,zz,0f,0f,0f,cl,false); 104 } 105 _Point(float xx, float yy, float zz)106 public _Point(float xx, float yy, float zz) { 107 this(xx,yy,zz,0f,0f,0f,Color.black,false); 108 } 109 110 get_Array(Canvas3D j3d)111 public Object[] get_Array(Canvas3D j3d){ 112 this.J3D = j3d; 113 arr = new Object[2]; 114 arr[0] = new shpPoint(x,y,z,selected ? j3d.SELECTCOLOR : color); 115 arr[1] = new shpLine(x,y,z,x2,y2,z2,selected ? j3d.SELECTCOLOR : color); 116 for (int i=0; i<arr.length; i++) 117 ((shp)arr[i]).setShow(show); 118 return arr; 119 } 120 isPickPoint(int x, int y, boolean shw, boolean ogl)121 public boolean isPickPoint(int x, int y, boolean shw, boolean ogl) { 122 boolean check = false; 123 124 if (show == shw) 125 for (int i=0; i< arr.length; i++) 126 check = (((shp)arr[i]).isPickPoint(x,y,ogl) == true ? true : check); 127 128 return check; 129 } 130 isPickPoint(Rectangle2D r, boolean shw, boolean ogl)131 public boolean isPickPoint(Rectangle2D r, boolean shw, boolean ogl) { 132 boolean check = false; 133 134 if (show == shw) 135 for (int i=0; i< arr.length; i++) 136 check = (((shp)arr[i]).isPickPoint(r,ogl) == true ? true : check); 137 138 return check; 139 } 140 141 isSelected()142 public boolean isSelected(){return selected;} setSelected(boolean sel)143 public void setSelected(boolean sel){ selected=sel; } toString()144 public String toString(){ return "Point ID="+Id;} get_TreeNode()145 public MutableTreeNode get_TreeNode(){ 146 DefaultMutableTreeNode node = new DefaultMutableTreeNode(this); 147 return node; 148 } 149 transform3D(Matrix3D t)150 public void transform3D(Matrix3D t){ 151 if(!selected || processed)return; 152 float xx=x*t.xx+y*t.xy+z*t.xz+t.xo; 153 float yy=x*t.yx+y*t.yy+z*t.yz+t.yo; 154 float zz=x*t.zx+y*t.zy+z*t.zz+t.zo; 155 x=xx; y=yy; z=zz; 156 xx=x2*t.xx+y2*t.xy+z2*t.xz+t.xo; 157 yy=x2*t.yx+y2*t.yy+z2*t.yz+t.yo; 158 zz=x2*t.zx+y2*t.zy+z2*t.zz+t.zo; 159 x2=xx; y2=yy; z2=zz; 160 } 161 getEditPanel(Canvas3D j3d, PreProcessor pp)162 public JPanel getEditPanel(Canvas3D j3d, PreProcessor pp){ 163 this.J3D = j3d; 164 this.PreP = pp; 165 JPanel p = new JPanel(new BorderLayout()); 166 JPanel p1 = new JPanel(new GridLayout(3,4)); 167 JPanel p2 = new JPanel(new BorderLayout()); 168 JLabel lb = new JLabel("Edit - "+toString()); 169 // if (!add) { 170 tx = new JTextField(x+"",3); 171 ty = new JTextField(y+"",3); 172 tz = new JTextField(z+"",3); 173 tvx = new JTextField(vx+"",3); 174 tvy = new JTextField(vy+"",3); 175 tvz = new JTextField(vz+"",3); 176 /* } else 177 { 178 tx = new JTextField("",3); 179 ty = new JTextField("",3); 180 tz = new JTextField("",3); 181 tvx = new JTextField("",3); 182 tvy = new JTextField("",3); 183 tvz = new JTextField("",3); 184 }*/ 185 lb.setForeground(Color.blue); 186 JButton b_upd = new JButton(add == true?"Add":"Update"); 187 b_upd.addActionListener(new java.awt.event.ActionListener() { 188 public void actionPerformed(ActionEvent e) { 189 jButton1_actionPerformed(e); 190 } 191 }); 192 193 p.add(p1, BorderLayout.CENTER); 194 p1.add(new JLabel()); 195 p1.add(new JLabel("X")); 196 p1.add(new JLabel("Y")); 197 p1.add(new JLabel("Z")); 198 p1.add(new JLabel("Point:")); 199 p1.add(tx, null); 200 p1.add(ty, null); 201 p1.add(tz, null); 202 p1.add(new JLabel("Vector:")); 203 p1.add(tvx, null); 204 p1.add(tvy, null); 205 p1.add(tvz, null); 206 p.add(lb, BorderLayout.NORTH); 207 p.add(p2, BorderLayout.SOUTH); 208 p2.add(b_upd,null); 209 210 tx.addKeyListener(new KeyAdapter() { 211 212 public void keyPressed(KeyEvent e) { 213 checkDefaultKey(e); 214 } 215 216 public void keyTyped(KeyEvent e) { 217 char c = e.getKeyChar(); 218 219 if (c == '\n') { 220 jButton1_actionPerformed(null); 221 tx.requestFocus(); 222 tx.selectAll(); 223 } 224 else 225 if (c == ',') { 226 ty.requestFocus(); 227 ty.selectAll(); 228 e.consume(); 229 } 230 231 } 232 233 }); 234 235 ty.addKeyListener(new KeyAdapter() { 236 237 public void keyPressed(KeyEvent e) { 238 checkDefaultKey(e); 239 } 240 241 public void keyTyped(KeyEvent e) { 242 char c = e.getKeyChar(); 243 244 if (c == '\n') { 245 jButton1_actionPerformed(null); 246 tx.requestFocus(); 247 tx.selectAll(); 248 } 249 else 250 if (c == ',') { 251 tz.requestFocus(); 252 tz.selectAll(); 253 e.consume(); 254 } 255 } 256 }); 257 258 tz.addKeyListener(new KeyAdapter() { 259 260 public void keyPressed(KeyEvent e) { 261 checkDefaultKey(e); 262 } 263 264 public void keyTyped(KeyEvent e) { 265 char c = e.getKeyChar(); 266 267 if (c == '\n') { 268 jButton1_actionPerformed(null); 269 tx.requestFocus(); 270 tx.selectAll(); 271 } 272 else 273 if (c == ',') { 274 tvx.requestFocus(); 275 tvx.selectAll(); 276 e.consume(); 277 } 278 } 279 }); 280 281 tvx.addKeyListener(new KeyAdapter() { 282 283 public void keyPressed(KeyEvent e) { 284 checkDefaultKey(e); 285 } 286 287 public void keyTyped(KeyEvent e) { 288 char c = e.getKeyChar(); 289 290 if (c == '\n') { 291 jButton1_actionPerformed(null); 292 tx.requestFocus(); 293 tx.selectAll(); 294 } 295 else 296 if (c == ',') { 297 tvy.requestFocus(); 298 tvy.selectAll(); 299 e.consume(); 300 } 301 302 } 303 304 }); 305 306 tvy.addKeyListener(new KeyAdapter() { 307 308 public void keyPressed(KeyEvent e) { 309 checkDefaultKey(e); 310 } 311 312 public void keyTyped(KeyEvent e) { 313 char c = e.getKeyChar(); 314 315 if (c == '\n') { 316 jButton1_actionPerformed(null); 317 tx.requestFocus(); 318 tx.selectAll(); 319 } 320 else 321 if (c == ',') { 322 tvz.requestFocus(); 323 tvz.selectAll(); 324 e.consume(); 325 } 326 } 327 }); 328 329 tvz.addKeyListener(new KeyAdapter() { 330 331 public void keyPressed(KeyEvent e) { 332 checkDefaultKey(e); 333 } 334 335 public void keyTyped(KeyEvent e) { 336 char c = e.getKeyChar(); 337 338 if (c == '\n') { 339 jButton1_actionPerformed(null); 340 tx.requestFocus(); 341 tx.selectAll(); 342 } 343 else 344 if (c == ',') { 345 tx.requestFocus(); 346 tx.selectAll(); 347 e.consume(); 348 } 349 } 350 }); 351 352 return p; 353 } 354 355 jButton1_actionPerformed(ActionEvent e)356 void jButton1_actionPerformed(ActionEvent e) { 357 _Point tmp = this; 358 float xx1=Float.parseFloat(tx.getText()); 359 float yy1=Float.parseFloat(ty.getText()); 360 float zz1=Float.parseFloat(tz.getText()); 361 x=xx1; y=yy1; z=zz1; 362 363 vx = Float.parseFloat(tvx.getText()); 364 vy = Float.parseFloat(tvy.getText()); 365 vz = Float.parseFloat(tvz.getText()); 366 float l = (float)Math.sqrt(vx*vx+vy*vy+vz*vz); 367 if (l > 0) { 368 vx = vx / l; 369 vy = vy / l; 370 vz = vz / l; 371 } 372 373 x2 = x+vx; y2 = y+vy; z2 = z+vz; 374 375 if(add == true) { 376 try { 377 tmp = (_Point)this.clone(); 378 J3D.add3D(tmp); 379 } catch (CloneNotSupportedException e1) { 380 e1.printStackTrace(); 381 } 382 tmp.add = false; 383 tx.requestFocus(); 384 tx.selectAll(); 385 } else 386 tmp.reset(true); 387 388 J3D.tree_reset(); 389 J3D.view_reset(); 390 } 391 requestFocus()392 public void requestFocus() { 393 394 tx.requestFocus(); 395 tx.selectAll(); 396 397 } 398 get_Nodes()399 public _Node[] get_Nodes(){return null;} get_Elements()400 public _Object[] get_Elements(){return null;} distance(_Point p)401 public float distance(_Point p){ return (float)Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z)); } 402 getVector()403 public Vector3D getVector() { 404 Vector3D v = new Vector3D(); 405 406 v.x = this.x; 407 v.y = this.y; 408 v.z = this.z; 409 410 return v; 411 } 412 toLocal(Vector3D translate, Matrix3D rotate)413 public void toLocal(Vector3D translate, Matrix3D rotate) { 414 415 Vector3D temp = new Vector3D(this.x, this.y, this.z); 416 417 temp.add(temp,translate); 418 temp.mult(rotate,temp); 419 420 this.x = temp.x; 421 this.y = temp.y; 422 this.z = temp.z; 423 424 } 425 toGlobal(Vector3D translate, Matrix3D rotate)426 public void toGlobal(Vector3D translate, Matrix3D rotate) { 427 428 Vector3D temp = new Vector3D(this.x, this.y, this.z); 429 430 temp.sub(temp,translate); 431 temp.transMult(rotate,temp); 432 433 this.x = temp.x; 434 this.y = temp.y; 435 this.z = temp.z; 436 437 } 438 duplicate(Canvas3D out, boolean add)439 public _Object duplicate(Canvas3D out, boolean add){ 440 _Point o = null; 441 try { 442 o = (_Point)this.clone(); 443 } catch (CloneNotSupportedException e) { 444 e.printStackTrace(); 445 } 446 447 if (add) out.add3D(o); 448 449 return o; 450 } 451 getCenter()452 public Vector3D getCenter() { 453 Vector3D s = new Vector3D(x,y,z); 454 455 return s; 456 } 457 replaceObjectWith(_Object o, _Object replacement)458 public void replaceObjectWith(_Object o, _Object replacement) { 459 // Do nothing 460 } 461 462 } 463 464