~ubuntu-branches/ubuntu/trusty/rheolef/trusty-proposed

« back to all changes in this revision

Viewing changes to nfem/lib/geo-mayavi.cc

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2010-06-12 09:08:59 UTC
  • Revision ID: james.westby@ubuntu.com-20100612090859-8gpm2gc7j3ab43et
Tags: upstream-5.89
ImportĀ upstreamĀ versionĀ 5.89

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
///
 
2
/// This file is part of Rheolef.
 
3
///
 
4
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
 
5
///
 
6
/// Rheolef is free software; you can redistribute it and/or modify
 
7
/// it under the terms of the GNU General Public License as published by
 
8
/// the Free Software Foundation; either version 2 of the License, or
 
9
/// (at your option) any later version.
 
10
///
 
11
/// Rheolef is distributed in the hope that it will be useful,
 
12
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
/// GNU General Public License for more details.
 
15
///
 
16
/// You should have received a copy of the GNU General Public License
 
17
/// along with Rheolef; if not, write to the Free Software
 
18
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
///
 
20
/// =========================================================================
 
21
//
 
22
// visualisation of geometries using mayavi
 
23
//
 
24
// authors:
 
25
//      Pierre.Saramito@imag.fr
 
26
//
 
27
// date: 22 january 2006
 
28
//
 
29
// TODO:
 
30
//   - a mayavi Module ClipPolyData with a customizale cut plane
 
31
//   - how to cut all datas (boundfarie, ect) with the same plane in mayavi ??
 
32
//
 
33
#include "rheolef/georep.h"
 
34
#include "rheolef/geo-visu.h"
 
35
#include "rheolef/geo-visu-aux.h"
 
36
#include <sstream>
 
37
using namespace std;
 
38
 
 
39
// define when using old mayavi python library:
 
40
#undef _RHEOLEF_USE_MAYAVI_1
 
41
 
 
42
int
 
43
georep::visu_mayavi (const geo_visu_option_type& opt) const
 
44
{
 
45
  //
 
46
  // output geometry for vtkUnstructuredGridReader
 
47
  //
 
48
  string vtk_name = opt.basename + ".vtk";
 
49
  ofstream vtk (vtk_name.c_str());
 
50
  if (opt.verbose) clog << "! file \"" << vtk_name << "\" created." << endl;
 
51
  int digits10 = numeric_limits<Float>::digits10;
 
52
  vtk << setprecision(digits10);
 
53
  put_vtk (vtk);
 
54
  vtk.close();
 
55
  //
 
56
  // plot domains
 
57
  //
 
58
  domlist_type::const_iterator iter = _domlist.begin();
 
59
  domlist_type::const_iterator last = _domlist.end();
 
60
  for (; iter != last; ++iter) {
 
61
      const domain& dom = *iter;
 
62
      string dom_name = opt.basename + "." + dom.name() + ".vtk";
 
63
      vtk.open (dom_name.c_str());
 
64
      if (opt.verbose) clog << "! file \"" << dom_name << "\" created." << endl;
 
65
      dom.put_vtk (vtk, begin_node(), end_node());
 
66
      vtk.close();
 
67
  }
 
68
  //
 
69
  // output python script for mayavi/vtk executable
 
70
  //
 
71
  string py_name = opt.basename + ".py";
 
72
  ofstream py (py_name.c_str());
 
73
  if (opt.verbose) clog << "! file \"" << py_name << "\" created." << endl;
 
74
 
 
75
  py << "#!/usr/bin/env mayavi2" << endl
 
76
     << "# This is a mayavi script for the visualization of " << opt.basename << ".vtk" << endl
 
77
     << "# automatically generated by rheolef."  << endl
 
78
     << endl
 
79
     ;
 
80
#ifdef _RHEOLEF_USE_MAYAVI_1
 
81
  // old mayavi python library
 
82
  py << "import mayavi" << endl
 
83
     << "from mayavi_geo import *" << endl
 
84
     << endl
 
85
     ;
 
86
#else
 
87
  // new mayavi2 python library
 
88
  py << "from enthought.mayavi.scripts import mayavi2" << endl
 
89
     << "mayavi2.standalone(globals())" << endl
 
90
     << "from mayavi2_rheolef import *" << endl 
 
91
     << endl
 
92
     ;
 
93
#endif
 
94
  iter = _domlist.begin();
 
95
  last = _domlist.end();
 
96
  py << "d = [ '" << opt.basename << "'";
 
97
  for (; iter != last; ++iter) {
 
98
      const domain& dom = *iter;
 
99
      py << ", '" << dom.name() << "'";
 
100
  }
 
101
  py << "]" << endl;
 
102
 
 
103
  size_t d =  dimension();
 
104
  bool has_origin = (opt.origin[0] != numeric_limits<Float>::max());
 
105
  py << "opt = {                        \\" << endl
 
106
     << "    'label'   : 1," << endl
 
107
     << "    'axis'    : 1," << endl
 
108
     << "    'color'   : '" << (opt.color ? "color" : (opt.gray ? "gray" : "black_and_white")) << "'," << endl
 
109
     << "    'stereo'  : " << opt.stereo << ",          \\" << endl
 
110
     << "    'ball'    : " << opt.ball << ",            \\" << endl
 
111
     << "    'cut'     : " << opt.cut  << ",            \\" << endl
 
112
     << "    'fill'    : " << opt.fill << ",            \\" << endl
 
113
     << "    'full'    : " << opt.full << ",            \\" << endl
 
114
     << "    'lattice' : " << opt.lattice << ",                 \\" << endl
 
115
     << "    'shrink'  : " << opt.shrink << ",          \\" << endl
 
116
     << "    'tube'    : " << opt.tube << ",            \\" << endl
 
117
     << "    'has_origin' : " << has_origin  << ",              \\" << endl
 
118
     << "    'origin'     : " << python(opt.origin) << ",               \\" << endl
 
119
     << "    'normal'     : " << python(-opt.normal) << "               \\" << endl
 
120
     << "  }" << endl
 
121
     << endl
 
122
     ;
 
123
#ifdef _RHEOLEF_USE_MAYAVI_1
 
124
  py << "v = mayavi.mayavi()" << endl
 
125
     << "mayavi_geo(v, opt, d)" << endl
 
126
     << "v.Render()" << endl
 
127
     << "v.master.wait_window()" << endl
 
128
     << endl
 
129
     ;
 
130
#else
 
131
  py << "mayavi2_geo(mayavi, opt, d)" << endl
 
132
     << endl
 
133
     ;
 
134
#endif
 
135
  //
 
136
  // run pyton
 
137
  //
 
138
  int status = 0;
 
139
  string command;
 
140
  if (opt.execute) {
 
141
      command = "LANG=C PYTHONPATH=" + string(_RHEOLEF_PKGDATADIR) + " mayavi2 " + py_name;
 
142
      if (opt.verbose) clog << "! " << command << endl;
 
143
      status = system (command.c_str());
 
144
  }
 
145
  //
 
146
  // clear vtk data
 
147
  //
 
148
  if (opt.clean) {
 
149
      command = "/bin/rm -f " + opt.basename + ".vtk " + opt.basename + ".py";
 
150
      if (opt.verbose) clog << "! " << command << endl;
 
151
      status = system (command.c_str());
 
152
      iter = _domlist.begin();
 
153
      last = _domlist.end();
 
154
      for (; iter != last; ++iter) {
 
155
          const domain& dom = *iter;
 
156
          command = "/bin/rm -f " + opt.basename + "." + dom.name() + ".vtk";
 
157
          if (opt.verbose) clog << "! " << command << endl;
 
158
          status = system (command.c_str());
 
159
      }
 
160
  }
 
161
  return status;
 
162
}