View Javadoc

1   // Dihed version 0.1 Java application for calculationg the dihedral angle of two planes
2   // based on geometric algebra Cl(4,1) conformal model as implemented in
3   // the GeometricAlgebra Java package
4   // For current version of GemoetricAlgebra package
5   // Go to ... http://sinai.mech.fukui-u.ac.jp/gcj/software/KamiWaAi/index.html
6   //
7   // Copyright (C) 2003, Eckhard M.S. Hitzer 
8   //
9   // This library is free software; you can redistribute it and/or
10  // modify it under the terms of the GNU Lesser General Public
11  // License as published by the Free Software Foundation; either
12  // version 2.1 of the License, or any later version.
13  //
14  // This library is distributed in the hope that it will be useful,
15  // but WITHOUT ANY WARRANTY; without even the implied warranty of
16  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  // Lesser General Public License for more details.
18  //
19  // You should have received a copy of the GNU Lesser General Public
20  // License along with this library; if not, write to the Free Software
21  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  // Go to ... http://www.gnu.org/copyleft/lesser.html
23  // 
24  // hitzer@mech.fukui-u.ac.jp 
25  // 
26  // Department of Mechanical Engineering, Fukui University
27  // 3-9-1 Bunkyo, 910-8507 Fukui, Japan
28  // 
29  // 
30  //
31  // It the personal wish of E.M.S. Hitzer,
32  // that you never use this software for infringing human dignity: 
33  //
34  // "God created man in his own image,
35  // in the image of God he created him;
36  // male and female he created them.
37  // [The Bible, Genesis chapter 1 verse 27]
38  //
39  //
40  //
41  // sdg
42  // Datei Dihed.java
43  // by Eckhard Hitzer
44  //
45  package net.sourceforge.kamiwaai;
46  import java.awt.FlowLayout;
47  import java.awt.Font;
48  import java.awt.GridLayout;
49  import java.awt.Label;
50  import java.awt.event.ActionEvent;
51  import java.awt.event.ActionListener;
52  import java.awt.event.WindowAdapter;
53  import java.awt.event.WindowEvent;
54  
55  import javax.swing.JButton;
56  import javax.swing.JFrame;
57  import javax.swing.JLabel;
58  import javax.swing.JPanel;
59  import javax.swing.JTextField;
60  import javax.swing.SwingConstants;
61  
62  import net.sourceforge.kamiwaai.geometricalgebra.MultiVector;
63  import net.sourceforge.kamiwaai.geometricalgebra.MultiVectors;
64  import net.sourceforge.kamiwaai.geometricalgebra.PointC;
65  
66  // Fensterklasse definieren
67  public class Dihed extends JFrame
68    {
69  	public static MultiVector N    = MultiVectors.N();
70  	public static MultiVector n    = MultiVectors.n();
71  	public static MultiVector nbar = MultiVectors.nbar();
72  	public static MultiVector One  = MultiVectors.One();
73  	public static MultiVector I    = MultiVectors.I();
74  	public static MultiVector e1   = MultiVectors.e1();
75  	public static MultiVector e2   = MultiVectors.e2();
76  	public static MultiVector e3   = MultiVectors.e3();
77    JButton doitswitch;
78    JTextField A1x_F, A1y_F, A1z_F, A2x_F, A2y_F, A2z_F, A3x_F, A3y_F, A3z_F;
79    JTextField B1x_F, B1y_F, B1z_F, B2x_F, B2y_F, B2z_F, B3x_F, B3y_F, B3z_F; 
80    JTextField angled_F, angler_F, cos_F, sin_F, mx_F, my_F, mz_F;
81  
82    // der Konstruktor 
83    Dihed(String titel)
84      {
85      super(titel);
86  
87      getContentPane().setLayout(new GridLayout(12,3,5,5));
88     
89      JPanel panel1 = new JPanel();
90      panel1.setLayout(new FlowLayout());
91  
92      JPanel panel2 = new JPanel();
93      panel2.setLayout(new FlowLayout());
94  
95      JPanel panel3 = new JPanel();
96      panel3.setLayout(new FlowLayout());
97  
98      JPanel panel4 = new JPanel();
99      panel1.setLayout(new FlowLayout());
100 
101     JPanel panel5 = new JPanel();
102     panel2.setLayout(new FlowLayout());
103 
104     JPanel panel6 = new JPanel();
105     panel3.setLayout(new FlowLayout());
106 
107     JPanel panel7 = new JPanel();
108     panel3.setLayout(new FlowLayout());
109 
110     JPanel panel8 = new JPanel();
111     panel3.setLayout(new FlowLayout());
112 
113     JPanel panel9 = new JPanel();
114     panel3.setLayout(new FlowLayout());
115 
116 
117     // Plane 1 definition
118     JLabel A1x_L = new JLabel("A1x", SwingConstants.RIGHT);
119     A1x_F = new JTextField(10);
120     A1x_F.setText("0.0");
121     panel1.add(A1x_L);
122     panel1.add(A1x_F);
123 
124     JLabel A2x_L = new JLabel("A2x", SwingConstants.RIGHT);
125     A2x_F = new JTextField(10);
126     A2x_F.setText("0.0");
127     panel1.add(A2x_L);
128     panel1.add(A2x_F);
129 
130     JLabel A3x_L = new JLabel("A3x", SwingConstants.RIGHT);
131     A3x_F = new JTextField(10);
132     A3x_F.setText("0.0");
133     panel1.add(A3x_L);
134     panel1.add(A3x_F);
135 
136     
137     JLabel A1y_L = new JLabel("A1y", SwingConstants.RIGHT);
138     A1y_F = new JTextField(10);
139     A1y_F.setText("0.0");
140     panel2.add(A1y_L);
141     panel2.add(A1y_F);
142 
143     JLabel A2y_L = new JLabel("A2y", SwingConstants.RIGHT);
144     A2y_F = new JTextField(10);
145     A2y_F.setText("0.0");
146     panel2.add(A2y_L);
147     panel2.add(A2y_F);
148 
149     JLabel A3y_L = new JLabel("A3y", SwingConstants.RIGHT);
150     A3y_F = new JTextField(10);
151     A3y_F.setText("0.0");
152     panel2.add(A3y_L);
153     panel2.add(A3y_F);
154 
155     
156     JLabel A1z_L = new JLabel("A1z", SwingConstants.RIGHT);
157     A1z_F = new JTextField(10);
158     A1z_F.setText("0.0");
159     panel3.add(A1z_L);
160     panel3.add(A1z_F);
161 
162     JLabel A2z_L = new JLabel("A2z", SwingConstants.RIGHT);
163     A2z_F = new JTextField(10);
164     A2z_F.setText("0.0");
165     panel3.add(A2z_L);
166     panel3.add(A2z_F);
167 
168     JLabel A3z_L = new JLabel("A3z", SwingConstants.RIGHT);
169     A3z_F = new JTextField(10);
170     A3z_F.setText("0.0");
171     panel3.add(A3z_L);
172     panel3.add(A3z_F);
173 
174       
175     // Plane 2 definition 
176     JLabel B1x_L = new JLabel("B1x", SwingConstants.RIGHT);
177     B1x_F = new JTextField(10);
178     B1x_F.setText("0.0");
179     panel4.add(B1x_L);
180     panel4.add(B1x_F);
181 
182     JLabel B2x_L = new JLabel("B2x", SwingConstants.RIGHT);
183     B2x_F = new JTextField(10);
184     B2x_F.setText("0.0");
185     panel4.add(B2x_L);
186     panel4.add(B2x_F);
187 
188     JLabel B3x_L = new JLabel("B3x", SwingConstants.RIGHT);
189     B3x_F = new JTextField(10);
190     B3x_F.setText("0.0");
191     panel4.add(B3x_L);
192     panel4.add(B3x_F);
193 
194     
195     JLabel B1y_L = new JLabel("B1y", SwingConstants.RIGHT);
196     B1y_F = new JTextField(10);
197     B1y_F.setText("0.0");
198     panel5.add(B1y_L);
199     panel5.add(B1y_F);
200 
201     JLabel B2y_L = new JLabel("B2y", SwingConstants.RIGHT);
202     B2y_F = new JTextField(10);
203     B2y_F.setText("0.0");
204     panel5.add(B2y_L);
205     panel5.add(B2y_F);
206 
207     JLabel B3y_L = new JLabel("B3y", SwingConstants.RIGHT);
208     B3y_F = new JTextField(10);
209     B3y_F.setText("0.0");
210     panel5.add(B3y_L);
211     panel5.add(B3y_F);
212       
213 
214     JLabel B1z_L = new JLabel("B1z", SwingConstants.RIGHT);
215     B1z_F = new JTextField(10);
216     B1z_F.setText("0.0");
217     panel6.add(B1z_L);
218     panel6.add(B1z_F);
219 
220     JLabel B2z_L = new JLabel("B2z", SwingConstants.RIGHT);
221     B2z_F = new JTextField(10);
222     B2z_F.setText("0.0");
223     panel6.add(B2z_L);
224     panel6.add(B2z_F);
225 
226     JLabel B3z_L = new JLabel("B3z", SwingConstants.RIGHT);
227     B3z_F = new JTextField(10);
228     B3z_F.setText("0.0");
229     panel6.add(B3z_L);
230     panel6.add(B3z_F);
231 
232     doitswitch = new JButton("Get angle!");
233 
234     JLabel angled_L = new JLabel("Dihedral angle [deg]", SwingConstants.RIGHT);
235     angled_F = new JTextField(5);
236     //B1z_F.setText("0.0");
237     panel7.add(angled_L);
238     panel7.add(angled_F);
239 
240     JLabel angler_L = new JLabel("dh. angle [rad]", SwingConstants.RIGHT);
241     angler_F = new JTextField(5);
242     //B1z_F.setText("0.0");
243     panel7.add(angler_L);
244     panel7.add(angler_F);
245 
246     JLabel cos_L = new JLabel("cos", SwingConstants.RIGHT);
247     cos_F = new JTextField(5);
248     //B1z_F.setText("0.0");
249     panel8.add(cos_L);
250     panel8.add(cos_F);
251 
252     JLabel sin_L = new JLabel("sin", SwingConstants.RIGHT);
253     sin_F = new JTextField(5);
254     //B1z_F.setText("0.0");
255     panel8.add(sin_L);
256     panel8.add(sin_F);
257 
258     JLabel mx_L = new JLabel("Dir. of intsec.  mx", SwingConstants.RIGHT);
259     mx_F = new JTextField(5);
260     //B1z_F.setText("0.0");
261     panel9.add(mx_L);
262     panel9.add(mx_F);
263 
264     JLabel my_L = new JLabel("my", SwingConstants.RIGHT);
265     my_F = new JTextField(5);
266     //B1z_F.setText("0.0");
267     panel9.add(my_L);
268     panel9.add(my_F);
269 
270     JLabel mz_L = new JLabel("mz", SwingConstants.RIGHT);
271     mz_F = new JTextField(5);
272     //B1z_F.setText("0.0");
273     panel9.add(mz_L);
274     panel9.add(mz_F);
275 
276     Label plane1_L = new Label("Coordinates of three points on plane 1");
277     plane1_L.setFont(new Font("SansSerif",Font.BOLD,14));
278     getContentPane().add(plane1_L);
279     getContentPane().add(panel1);
280     getContentPane().add(panel2);
281     getContentPane().add(panel3);
282 
283     Label plane2_L = new Label("Coordinates of three points on plane 2");
284     plane2_L.setFont(new Font("SansSerif",Font.BOLD,14));
285     getContentPane().add(plane2_L);
286     getContentPane().add(panel4);
287     getContentPane().add(panel5);
288     getContentPane().add(panel6);
289     
290     getContentPane().add(doitswitch);
291 
292     getContentPane().add(panel7);
293     getContentPane().add(panel8);
294     getContentPane().add(panel9);
295 
296 
297       
298 
299     //JLabel l_passwort = new JLabel("Ihr Passwort: ", SwingConstants.RIGHT);
300     //JPasswordField passwort= new JPasswordField(10);
301     //passwort.setEchoChar('*');
302     //panel.add(l_passwort);
303     //panel.add(passwort);
304     //getContentPane().add("North",panel);
305 
306     //JTextArea hilfstext = new JTextArea(" Geben Sie bitte Ihren Namen \n", 3,30);
307     //hilfstext.append(" und Ihr Passwort ein.\n");
308     //hilfstext.setEditable(false);
309     //hilfstext.setFont(new Font ("Monospaced",Font.BOLD,20));
310     //hilfstext.setBackground(Color.gray);
311     //getContentPane().add("Center",hilfstext);
312 
313     // Lauscher einrichten
314     doitswitch.addActionListener(new CMyActionListener());
315     addWindowListener(new CMeinWindowLauscher());
316     }
317 
318 
319   // Ereignisbehandlung für das Fenster 
320   class CMeinWindowLauscher extends WindowAdapter
321     {
322     public void windowClosing(WindowEvent e)
323         {
324         System.exit(0);
325         }
326     }
327 
328   // Ereignisbehandlung für die Steuerelemente
329   class CMyActionListener implements ActionListener
330     {
331     public void actionPerformed(ActionEvent e)
332       {
333       double a11,a12,a13, a21,a22,a23, a31,a32,a33;
334       double b11,b12,b13, b21,b22,b23, b31,b32,b33;
335       double m1, m2, m3;
336       double angler, angled, cos, sin;
337       doitswitch.setText("Evaluate again!");
338       a11 = Double.parseDouble(A1x_F.getText());
339       a12 = Double.parseDouble(A1y_F.getText());
340       a13 = Double.parseDouble(A1z_F.getText());
341         
342       a21 = Double.parseDouble(A2x_F.getText());
343       a22 = Double.parseDouble(A2y_F.getText());
344       a23 = Double.parseDouble(A2z_F.getText());
345 
346       a31 = Double.parseDouble(A3x_F.getText());
347       a32 = Double.parseDouble(A3y_F.getText());
348       a33 = Double.parseDouble(A3z_F.getText());
349 
350       b11 = Double.parseDouble(B1x_F.getText());
351       b12 = Double.parseDouble(B1y_F.getText());
352       b13 = Double.parseDouble(B1z_F.getText());
353         
354       b21 = Double.parseDouble(B2x_F.getText());
355       b22 = Double.parseDouble(B2y_F.getText());
356       b23 = Double.parseDouble(B2z_F.getText());
357 
358       b31 = Double.parseDouble(B3x_F.getText());
359       b32 = Double.parseDouble(B3y_F.getText());
360       b33 = Double.parseDouble(B3z_F.getText());
361 
362       PointC A1 = new PointC();
363                  A1.sete1(a11);
364                  A1.sete2(a12);
365                  A1.sete3(a13);
366 
367       PointC A2 = new PointC();
368                  A2.sete1(a21);
369                  A2.sete2(a22);
370                  A2.sete3(a23);
371 
372       PointC A3 = new PointC();
373                  A3.sete1(a31);
374                  A3.sete2(a32);
375                  A3.sete3(a33);
376 
377       PointC B1 = new PointC();
378                  B1.sete1(b11);
379                  B1.sete2(b12);
380                  B1.sete3(b13);
381 
382       PointC B2 = new PointC();
383                  B2.sete1(b21);
384                  B2.sete2(b22);
385                  B2.sete3(b23);
386   
387       PointC B3 = new PointC();
388                  B3.sete1(b31);
389                  B3.sete2(b32);
390                  B3.sete3(b33);
391         
392       // Main calculation
393       //
394       MultiVector plane1 = A1.getMultiVector().OutProd(A2.getMultiVector()).OutProd(A3.getMultiVector()).OutProd(n);
395       MultiVector plane2 = B1.getMultiVector().OutProd(B2.getMultiVector()).OutProd(B3.getMultiVector()).OutProd(n);
396       MultiVector p1p2 = plane1.mult(plane2);
397       // Projecting out the dihedral angle
398       //
399       MultiVector DA = p1p2.OutProd(N).mult(N);
400       double DA0 = DA.ScProd(One);
401       MultiVector DA2 = DA.getGrade(2);
402       double DA2mag = DA2.magnitude();
403       MultiVector meet = DA2.mult(I).mult(N).multSc(-1.0);
404       MultiVector meet1 = meet.multSc(1.0/DA2mag);
405       m1 = meet1.ScProd(e1);
406       m2 = meet1.ScProd(e2);
407       m3 = meet1.ScProd(e3);
408       // This will always give an angle between 0..90 degree!
409       cos = Math.abs(DA0/Math.sqrt(DA0*DA0+DA2mag*DA2mag));
410       sin = DA2mag/Math.sqrt(DA0*DA0+DA2mag*DA2mag);
411       angler = (double) Math.atan2(sin,cos);
412       angled = (double) angler/(Math.PI)*180.0;
413 
414       // Output of results
415       //
416       angled_F.setText(Double.toString(angled).substring(0,Math.min( 7, Double.toString(angled).length() )));
417       angler_F.setText(Double.toString(angler).substring(0,Math.min(7,  Double.toString(angler).length() )));
418       cos_F.setText(Double.toString(cos).substring(0,Math.min(7,Double.toString(cos).length())));
419       sin_F.setText(Double.toString(sin).substring(0,Math.min(7,Double.toString(sin).length())));
420       mx_F.setText(Double.toString(m1).substring(0,Math.min(7,Double.toString(m1).length())));
421       my_F.setText(Double.toString(m2).substring(0,Math.min(7,Double.toString(m2).length())));
422       mz_F.setText(Double.toString(m3).substring(0,Math.min(7,Double.toString(m3).length())));
423       }
424     }
425 
426   public static void main(String[] args)
427     {
428     Dihed fenster = new Dihed("Dihedral Angle 0.1");
429     fenster.pack();
430     fenster.setSize(500,500);
431     fenster.setResizable(false);
432     fenster.show();
433     }
434   }