source: git/Singular/LIB/surfex/Equation.java @ 2ab830

spielwiese
Last change on this file since 2ab830 was 3de2ca, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: surfex_0_90_00 git-svn-id: file:///usr/local/Singular/svn/trunk@11070 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.7 KB
Line 
1////////////////////////////////////////////////////////////////////////
2//
3// This file Equation.java is part of SURFEX.
4//
5////////////////////////////////////////////////////////////////////////
6
7////////////////////////////////////////////////////////////////////////
8//
9// SURFEX version 0.90.00
10// =================
11//
12// Saarland University at Saarbruecken, Germany
13// Department of Mathematics and Computer Science
14//
15// SURFEX on the web: www.surfex.AlgebraicSurface.net
16//
17// Authors: Oliver Labs (2001-2008), Stephan Holzer (2004-2005)
18//
19// Copyright (C) 2001-2008
20//
21//
22// *NOTICE*
23// ========
24// 
25// This program is free software; you can redistribute it and/or modify it
26// under the terms of the GNU General Public License as published by the
27// Free Software Foundation ( version 3 or later of the License ).
28//
29// See LICENCE.TXT for details.
30//
31/////////////////////////////////////////////////////////////////////////
32
33import java.awt.BorderLayout;
34import java.awt.Color;
35import java.awt.GridLayout;
36import java.awt.event.ActionEvent;
37import java.awt.event.ActionListener;
38import java.io.BufferedReader;
39import java.io.File;
40import java.io.FileWriter;
41import java.io.InputStreamReader;
42import java.io.PrintWriter;
43import java.net.URL;
44import java.net.URLConnection;
45import java.net.URLEncoder;
46
47import javax.swing.JButton;
48import javax.swing.JCheckBox;
49import javax.swing.JComboBox;
50import javax.swing.JColorChooser;
51import javax.swing.JLabel;
52import javax.swing.JPanel;
53import javax.swing.JTextField;
54
55//////////////////////////////////////////////////////////////
56//
57// class Equation
58//
59//////////////////////////////////////////////////////////////
60
61public class Equation extends JPanel {
62
63    // Anfang Variablen
64   
65    surfex surfex_;
66   
67    public int surf_no = -1;
68   
69    // GUI
70    public JTextField text;
71   
72    JPanel configPanel = new JPanel();
73   
74    JButton basePlaneButton = new JButton();
75   
76    JButton optionButton = new JButton("opts");
77   
78    JButton deletePlaneButton = new JButton("del");
79   
80    JLabel label1, label2;
81   
82    String eindeutigeEqnNr;
83   
84    JButton colorButton_i, colorButton_o;
85   
86//    JPanel labelpanel = new JPanel(new GridLayout(2, 1));
87   
88    int eqnr;
89   
90    jv4surfex jv4sx;
91   
92    JCheckBox cbox = new JCheckBox();
93    JComboBox visType = new JComboBox();
94   
95    EquationAdmin eqAdm;
96   
97    OptionButtonPane optionButtonPane;
98   
99    // zum rechnen
100//    pcalc polyCalc = new pcalc();
101   
102    // Ende Variablen
103   
104    public String getImpsurfCode(String strPoly, double[] scale) {
105        String strRichMor = "DEF=" + URLEncoder.encode(strPoly + "=0;")
106            + "&XMIN=-" + (9.0 / scale[0]) + "&XMAX=" + (9.3 / scale[0])
107            + "&YMIN=-" + (9.1 / scale[1]) + "&YMAX=" + (9.1 / scale[1])
108            + "&ZMIN=-" + (9.2 / scale[2]) + "&ZMAX=" + (9.3 / scale[2])
109            + "" + "&COARSE=8&FINE=64&FACE=512&EDGE=4096"
110            + "&TIMEOUT=1000&VERSION=3";
111        return (strRichMor);
112    }
113   
114    public boolean triangulate(String strPoly, double[] scale,
115                               String eindeutigeEqnNr) {
116        // ??? do this on the webserver!!!
117        //  System.out.println("scale:"+scale[0]+","+scale[1]+","+scale[2]);
118        String strRichMor = getImpsurfCode(strPoly, scale);
119       
120        if (surfex_.configFrame.webPro.isSelected()) {
121            try {
122                URL url = new URL(surfex_.webPrgs.surfPath.getText()
123                                  + "surfex_comp.php");
124                URLConnection connection = url.openConnection();
125                connection.setUseCaches(false);
126                connection.setDoOutput(true);
127               
128                PrintWriter out = new PrintWriter(connection.getOutputStream());
129                //        System.out.println("old subdir:"+surfex_.getCurrentProject().old_tmpsubdir);
130                out
131                    .print("prefix=surfex_tmp"
132                           + "&filelocation="
133                           + URLEncoder.encode("./" + eindeutigeEqnNr
134                                               + ".jvx")
135                           + "&tmpsubdir="
136                           + URLEncoder
137                           .encode(surfex_.getCurrentProject().old_tmpsubdir)
138                           + "&prg=impsurf" + "&prg_code_part1="
139                           + URLEncoder.encode(strRichMor)
140                           + "&prg_code_part2=");
141                out.close();
142                // read the file names
143                BufferedReader in = new BufferedReader(new InputStreamReader(
144                                                           connection.getInputStream()));
145                String inputLine;
146                inputLine = in.readLine();
147                //        System.out.println(inputLine);
148                String strFilename = "";
149                if (inputLine.equals("okay")) {
150                    //        System.out.println("it works!");
151                    strFilename = in.readLine();
152                    surfex_.getCurrentProject().old_tmpsubdir = in.readLine();
153                   
154                    System.out.println("achtung seit Version 020 geaendert damit er kompiliert - ohne test");
155                   
156                    //   eqAdm.img_filename = surfex_.webPrgs.surfPath.getText()               + strFilename;
157                } else {
158                    System.out.println("it does not work!");
159                    System.out.println(in.readLine());
160                }
161                in.close();
162            } catch (Exception ex) {
163                System.out.println("triang:" + ex.toString());
164            }
165        } else {
166            try {
167                File outputFile = new File("./impsurf.richmor");
168                FileWriter out = new FileWriter(outputFile);
169                int len = strRichMor.length();
170                for (int pos = 0; pos < len; pos++) {
171                    out.write(strRichMor.charAt(pos));
172                }
173                out.close();
174               
175                outputFile = new File("./impsurf.sh");
176                out = new FileWriter(outputFile);
177                String strCommand = "#!/bin/sh\n"
178                    + "export REQUEST_METHOD=POST\n "
179                    + "export CONTENT_LENGTH=" + (strRichMor.length())
180                    + "\n " + "./localimpsurfCV <./impsurf.richmor >./"
181                    + eindeutigeEqnNr + ".jvx";
182               
183                len = strCommand.length();
184                for (int pos = 0; pos < len; pos++) {
185                    out.write(strCommand.charAt(pos));
186                }
187                out.close();
188            } catch (Exception e2) {
189                return (false);
190            }
191           
192            try {
193                Runtime r = Runtime.getRuntime();
194                Process p;
195                p = r.exec("sh ./impsurf.sh");
196                p.waitFor();
197            } catch (Exception e1) {
198                System.out.println(e1.toString());
199                return (false);
200            }
201        }
202        return (true);
203    } // end of triangulate()
204   
205    // Konstruktor
206    Equation(int internalEqnr, int eqnr, EquationAdmin ea, jv4surfex jv4sx,
207             surfex sx) {
208       
209//      polyCalc.doPrint = false;
210        eqAdm = ea;
211        this.eqnr = eqnr;
212        this.jv4sx = jv4sx;
213        surfex_ = sx;
214        eindeutigeEqnNr = eqAdm.ProjectNumber + "_"
215            + (new Integer(eqnr)).toString();
216
217        cbox.setSelected(true);
218
219        visType.insertItemAt("normal",0);
220        visType.insertItemAt("100% transparent",1);
221        visType.insertItemAt("as clipping surface",2);
222        visType.setSelectedIndex(0);
223
224        final OptionButtonPane tempOptionButtonPane = new OptionButtonPane(
225            true, 10, 0);
226        optionButtonPane = tempOptionButtonPane;
227        basePlaneButton.setBackground(Color.green);
228        basePlaneButton.setActionCommand("basePlaneButton" + internalEqnr);
229        basePlaneButton.setToolTipText("Triangulate this surface");
230        deletePlaneButton.setActionCommand("deletePlaneButton" + internalEqnr);
231        deletePlaneButton.setToolTipText("Delete this surface");
232        deletePlaneButton.setEnabled(false);
233        optionButton.addActionListener(new ActionListener() {
234                public void actionPerformed(ActionEvent evt) {
235                    tempOptionButtonPane.setVisible(true);
236                }
237            });
238        optionButton.setToolTipText("More options for the appearence");
239        //  label1=new JLabel("Equation");
240        label1 = new JLabel("f" + eqnr);
241        text = new JTextField("");
242        setLayout(new BorderLayout());
243        //  this.add(labelpanel, BorderLayout.EAST);
244///     labelpanel.add(label1);
245        //  labelpanel.add(label2);
246        this.add(text, BorderLayout.CENTER);
247        this.add(configPanel, BorderLayout.WEST);
248        //  if(!surfex_.inAnApplet) {
249        //      configPanel.add(basePlaneButton);
250        //  }
251        configPanel.add(cbox);
252
253        configPanel.add(visType);
254
255        colorButton_i = new JButton();
256        colorButton_i.setBackground(new Color(0, 180, 240));
257        colorButton_i.setToolTipText("Select the \"inside\" color");
258        colorButton_i.addActionListener(new ActionListener() {
259                public void actionPerformed(ActionEvent evt) {
260                    colorButton_i.setBackground(JColorChooser.showDialog(null,
261                                                                         "Change surface's inside color", colorButton_i
262                                                                         .getBackground()));
263                }
264            });
265       
266        colorButton_o = new JButton();
267        colorButton_o.setBackground(new Color(240, 180, 0));
268        colorButton_o.setToolTipText("Select the \"outside\" color");
269        colorButton_o.addActionListener(new ActionListener() {
270                public void actionPerformed(ActionEvent evt) {
271                    colorButton_o.setBackground(JColorChooser.showDialog(null,
272                                                                         "Change surface's outside color", colorButton_o
273                                                                         .getBackground()));
274                }
275            });
276       
277        configPanel.add(colorButton_o);
278        configPanel.add(colorButton_i);
279//      configPanel.add(deletePlaneButton);
280        configPanel.add(optionButton);
281        configPanel.add(label1);
282    }
283   
284    //Methoden
285    public boolean isSelected() {
286        return (cbox.isSelected());
287    }
288   
289    public int getEqNo() {
290        return (eqnr);
291    }
292   
293    public void setEquationNo(int no) {
294        eqnr = no;
295        eindeutigeEqnNr = eqAdm.ProjectNumber + "_"
296            + (new Integer(eqnr)).toString();
297        label1.setText("f" + eqnr);
298    }
299   
300    public void updateActionCommands(int internalEqnr) {
301        basePlaneButton.setActionCommand("basePlaneButton" + internalEqnr);
302        deletePlaneButton.setActionCommand("deletePlaneButton" + internalEqnr);
303    }
304   
305    public void setBasePlane(boolean b) {
306        // erhaelt nur false ...
307        basePlaneButton.setBackground(Color.white);
308    }
309   
310    public void setBasePlaneDefault() {
311        basePlaneButton.setBackground(Color.green);
312        //  System.out.println("jv4sx.show()");
313        jv4sx.showDefault();
314        //  jv4sx.show();
315    }
316   
317    public void setBasePlane(boolean b, jv4surfex jv4sx, double[] scale) {
318        //  System.out.println("setBasePlane...");
319        // erhaelt nur false ...
320        triangulate(text.getText(), scale, eindeutigeEqnNr);
321        basePlaneButton.setBackground(Color.green);
322        String strtmpdir = ".";
323        if (surfex_.inAnApplet) {
324            strtmpdir = surfex_.getCurrentProject().old_tmpsubdir;
325        }
326        if (jv4sx.loadAndDisplay(strtmpdir + "/" + eindeutigeEqnNr + ".jvx")) {
327            //          System.out.println("jv show");
328            jv4sx.show();
329        } else {
330            jv4sx.showDefault();
331            //      System.out.println("jv problem");
332            //      jv4sx.loadAndDisplay("./impsurf.jvx");
333            jv4sx.show();
334        }
335    }
336   
337    public boolean isBasePlane() {
338        //  System.out.println("bg:"+basePlaneButton.getBackground());
339        return false;
340        //  if (basePlaneButton.getBackground().equals(Color.green)) {
341        //      return true;
342        //  } else {
343        //      return false;
344        //  }
345    }
346   
347    public String getPolyCode(String polyname) {
348        return ("poly " + polyname + " = "
349                + text.getText().replaceAll("-", "-1*") + ";");
350    }
351   
352    public String getSurfCode(String polyname, int surface_no, double ang[]) {
353        String str = "";
354    //  str += getPolyCode(polyname);
355        String strSurfNo = "";
356        if (surface_no > 1) {
357            strSurfNo = "" + surface_no;
358        }
359        str += "surface" + strSurfNo + " = " + polyname + ";\n";
360        str += "surface" + strSurfNo + "_red = "
361            + (colorButton_o.getBackground().getRed()) + ";\n" + "surface"
362            + strSurfNo + "_green = "
363            + (colorButton_o.getBackground().getGreen()) + ";\n" + "surface"
364            + strSurfNo + "_blue = "
365            + (colorButton_o.getBackground().getBlue()) + ";\n";
366        str += "inside" + strSurfNo + "_red = "
367            + (colorButton_i.getBackground().getRed()) + ";\n" + "inside"
368            + strSurfNo + "_green = "
369            + (colorButton_i.getBackground().getGreen()) + ";\n" + "inside"
370            + strSurfNo + "_blue = "
371            + (colorButton_i.getBackground().getBlue()) + ";\n";
372        if(visType.getSelectedIndex()==1) {
373            str += "transparence" + strSurfNo + " = 100;\n";
374        } else {
375            str += "transparence" + strSurfNo + " = "
376                + (new Integer(optionButtonPane.slider1.getValue())) + ";\n";
377        }
378       
379        //  str += "surface" + strSurfNo + "=rotate(surface" + strSurfNo + ","
380        //      + ang[0] + ",yAxis);";
381        //  str += "surface" + strSurfNo + "=rotate(surface" + strSurfNo + ","
382        //      + ang[1] + ",xAxis);";
383        //  str += "surface" + strSurfNo + "=rotate(surface" + strSurfNo + ","
384        //      + ang[2] + ",zAxis);";
385        return (str);
386    }
387   
388    public void savesurfCode(PrintWriter pw, String polyname, int surface_no,
389                             double ang[]) {
390        //  System.out.println("poly print");
391        pw.println("poly " + polyname + " = "
392                   + text.getText().replaceAll("-", "-1*") + ";");
393        String strSurfNo = "";
394        if (surface_no > 1) {
395            strSurfNo = "" + surface_no;
396        }
397        pw.println("surface" + strSurfNo + " = " + polyname + ";\n");
398        pw.println("surface" + strSurfNo + "_red = "
399                   + (colorButton_o.getBackground().getRed()) + ";\n" + "surface"
400                   + strSurfNo + "_green = "
401                   + (colorButton_o.getBackground().getGreen()) + ";\n" + "surface"
402                   + strSurfNo + "_blue = "
403                   + (colorButton_o.getBackground().getBlue()) + ";");
404        pw.println("inside" + strSurfNo + "_red = "
405                   + (colorButton_i.getBackground().getRed()) + ";\n" + "inside"
406                   + strSurfNo + "_green = "
407                   + (colorButton_i.getBackground().getGreen()) + ";\n" + "inside"
408                   + strSurfNo + "_blue = "
409                   + (colorButton_i.getBackground().getBlue()) + ";");
410        pw.println("transparence" + strSurfNo + " = 0;");
411        pw.println("surface" + strSurfNo + "=rotate(surface" + strSurfNo + ","
412                   + ang[0] + ",yAxis);");
413        pw.println("surface" + strSurfNo + "=rotate(surface" + strSurfNo + ","
414                   + ang[1] + ",xAxis);");
415        pw.println("surface" + strSurfNo + "=rotate(surface" + strSurfNo + ","
416                   + ang[2] + ",zAxis);");
417    }
418
419    /*
420    public void savePOVCode(PrintWriter pw) {
421        //  System.out.println("poly print");
422        String POVCode = "";
423        pw.println("poly");
424        pw.println("{");
425        pw.println(polyCalc.showAsPovrayCode(text.getText()));
426        //  pw.println(text.getText().substring(0,2)+",");
427        //  pw.println("<"+text.getText().substring(3,text.getText().length())+">");
428        pw.println(" pigment { rgb <"
429                   + (colorButton_o.getBackground().getRed() / 255.0) + ","
430                   + (colorButton_o.getBackground().getGreen() / 255.0) + ","
431                   + (colorButton_o.getBackground().getBlue() / 255.0) + "> }");
432       
433        pw.println("finish { specular .6 }");
434        pw.println("clipped_by {");
435        pw.println("      sphere { < 0, 0, 0 >, Radius }");
436        pw.println("}");
437        pw.println("scale scale_x");
438        pw.println("scale scale_y");
439        pw.println("scale scale_z");
440        pw.println("rotate y*degy");
441        pw.println("rotate x*degx");
442        pw.println("rotate z*degz");
443        pw.println("}");
444        }
445    */
446   
447    public void saveYourself(PrintWriter pw) {
448        pw.println("////////////////// Equation: /////////////////////////");
449        pw.println("" + eqnr + "\n");
450        pw.println(text.getText() + "\n");
451        pw.println(colorButton_i.getBackground().getRed() + "\n");
452        pw.println(colorButton_i.getBackground().getGreen() + "\n");
453        pw.println(colorButton_i.getBackground().getBlue() + "\n");
454        pw.println(colorButton_o.getBackground().getRed() + "\n");
455        pw.println(colorButton_o.getBackground().getGreen() + "\n");
456        pw.println(colorButton_o.getBackground().getBlue() + "\n");
457        pw.println(basePlaneButton.getBackground().getRed() + "\n");
458        pw.println(cbox.isSelected() + "\n");
459        pw.println(visType.getSelectedIndex() + "\n");
460        optionButtonPane.saveYourself(pw);
461    }
462   
463    public String saveYourself() {
464        String str = "";
465        str += "////////////////// Equation: /////////////////////////" + "\n";
466        str += ("" + eqnr + "\n");
467        str += text.getText() + "\n";
468        str += colorButton_i.getBackground().getRed() + "\n";
469        str += colorButton_i.getBackground().getGreen() + "\n";
470        str += colorButton_i.getBackground().getBlue() + "\n";
471        str += colorButton_o.getBackground().getRed() + "\n";
472        str += colorButton_o.getBackground().getGreen() + "\n";
473        str += colorButton_o.getBackground().getBlue() + "\n";
474        str += basePlaneButton.getBackground().getRed() + "\n";
475        str += cbox.isSelected() + "\n";
476        str += visType.getSelectedIndex() + "\n";
477        str += optionButtonPane.saveYourself();
478        return (str);
479    }
480   
481    public void updateJV4SX() {
482        //  System.out.println("updateJV4SX!");
483        String strtmpdir = ".";
484        if (surfex_.inAnApplet) {
485            strtmpdir = surfex_.getCurrentProject().old_tmpsubdir;
486        }
487        if (jv4sx.loadAndDisplay(strtmpdir + "/" + eindeutigeEqnNr + ".jvx")) {
488            //          System.out.println("jv show");
489            jv4sx.show();
490        } else {
491            //      System.out.println("jv problem");
492            //      jv4sx.loadAndDisplay("./impsurf.jvx");
493            jv4sx.showDefault();
494            jv4sx.show();
495        }
496    }
497} // end class Equation
498
Note: See TracBrowser for help on using the repository browser.