View Javadoc

1   package org.lcsim.geometry.compact.converter.lcdd.util;
2   
3   import org.jdom.DataConversionException;
4   import org.jdom.Element;
5   
6   /**
7    * 
8    * LCDD Position element.
9    * 
10   * @author tonyj
11   * @author jeremym
12   */
13  public class Position extends RefElement
14  {
15      /** Creates a new instance of Position at (0,0,0). */
16      public Position(String name)
17      {
18          super("position", name);
19          setAttribute("x", "0.0");
20          setAttribute("y", "0.0");
21          setAttribute("z", "0.0");
22          setAttribute("unit", "mm");
23      }
24      
25      public Position(String name, double x, double y, double z)
26      {
27      	super("position",name);
28      	setX(x);
29      	setY(y);
30      	setZ(z);
31      	setAttribute("unit", "mm");
32      }
33  
34      /**
35       * Creates a new instance of Position from an {@link org.jdom.Element}.
36       * @param element The XML element which must be called position.
37       */
38      public Position(String name, Element element)
39      {   
40          super("position", name);
41  
42          if (!element.getName().equals("position"))
43              throw new IllegalArgumentException("expected position element but got " + element.getName());
44  
45          try {
46              
47              double x,y,z;
48              x = y = z = 0.;
49  
50              if (element.getAttribute("x") != null)
51              {
52                  x = element.getAttribute("x").getDoubleValue();
53              }
54  
55              if (element.getAttribute("y") != null)
56              {
57                  y  = element.getAttribute("y").getDoubleValue();
58              }
59  
60              if (element.getAttribute("z") != null)
61              {
62                  z = element.getAttribute("z").getDoubleValue();
63              }
64              
65              setX(x);
66              setY(y);
67              setZ(z);
68          }
69          catch (DataConversionException except)
70          {
71              throw new RuntimeException(except);
72          }
73      }   
74  
75      public void setX(double d)
76      {
77          setAttribute("x", String.valueOf(d));
78      }
79  
80      public void setY(double d)
81      {
82          setAttribute("y", String.valueOf(d));
83      }
84  
85      public void setZ(double d)
86      {
87          setAttribute("z", String.valueOf(d));
88      }
89  
90      public double x()
91      {
92          return dim("x");
93      }
94  
95      public double y()
96      {
97          return dim("y");
98      }
99  
100     public double z()
101     {
102         return dim("z");
103     }
104 
105     private double dim(String name)
106     {
107         try {
108             return getAttribute(name).getDoubleValue();
109         }
110         catch (DataConversionException x)
111         {
112             throw new RuntimeException(x);
113         }
114     }
115 }