Skip to content
Snippets Groups Projects
CutSphere.cpp 5.83 KiB
Newer Older
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
// $Id: CutSphere.cpp,v 1.47 2007-05-04 10:45:08 geuzaine Exp $
Christophe Geuzaine's avatar
Christophe Geuzaine committed
//
Christophe Geuzaine's avatar
Christophe Geuzaine committed
// Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
Christophe Geuzaine's avatar
Christophe Geuzaine committed
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
// 
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
// Please report all bugs and problems to <gmsh@geuz.org>.
Christophe Geuzaine's avatar
Christophe Geuzaine committed

#include <string.h>
#include "CutSphere.h"
#include "List.h"
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#if defined(HAVE_FLTK)
#include "GmshUI.h"
#include "Draw.h"
#endif


StringXNumber CutSphereOptions_Number[] = {
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  {GMSH_FULLRC, "Xc", GMSH_CutSpherePlugin::callbackX, 0.},
  {GMSH_FULLRC, "Yc", GMSH_CutSpherePlugin::callbackY, 0.},
  {GMSH_FULLRC, "Zc", GMSH_CutSpherePlugin::callbackZ, 0.},
  {GMSH_FULLRC, "R", GMSH_CutSpherePlugin::callbackR, 0.25},
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  {GMSH_FULLRC, "ExtractVolume", GMSH_CutSpherePlugin::callbackVol, 0.},
  {GMSH_FULLRC, "RecurLevel", GMSH_CutSpherePlugin::callbackRecur, 4},
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  {GMSH_FULLRC, "iView", NULL, -1.}
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  GMSH_Plugin *GMSH_RegisterCutSpherePlugin()
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    return new GMSH_CutSpherePlugin();
GMSH_CutSpherePlugin::GMSH_CutSpherePlugin()
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  ;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
void GMSH_CutSpherePlugin::draw()
{
#if defined(HAVE_FLTK)
  static GLUquadricObj *qua;
  static int first = 1;
  if(first) {
    first = 0;
    qua = gluNewQuadric();
  }
  GLint mode[2];
  glGetIntegerv(GL_POLYGON_MODE, mode);
  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  glPushMatrix();
Christophe Geuzaine's avatar
Christophe Geuzaine committed
  glColor4ubv((GLubyte *) & CTX.color.fg);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  glLineWidth(CTX.line_width);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  glTranslated(CutSphereOptions_Number[0].def,
	       CutSphereOptions_Number[1].def,
	       CutSphereOptions_Number[2].def);
  gluSphere(qua, CutSphereOptions_Number[3].def, 40, 40);
  glPopMatrix();
  glPolygonMode(GL_FRONT_AND_BACK, mode[1]);
#endif
}

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
double GMSH_CutSpherePlugin::callback(int num, int action, double value, double *opt,
				      double step, double min, double max)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  switch(action){ // configure the input field
  case 1: return step;
  case 2: return min;
  case 3: return max;
  default: break;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  }
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  *opt = value;
#if defined(HAVE_FLTK)
  DrawPlugin(draw);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#endif
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  return 0.;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
}

double GMSH_CutSpherePlugin::callbackX(int num, int action, double value)
{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  return callback(num, action, value, &CutSphereOptions_Number[0].def,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
		  CTX.lc/100., -2*CTX.lc, 2*CTX.lc);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
}

double GMSH_CutSpherePlugin::callbackY(int num, int action, double value)
{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  return callback(num, action, value, &CutSphereOptions_Number[1].def,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
		  CTX.lc/100., -2*CTX.lc, 2*CTX.lc);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
}

double GMSH_CutSpherePlugin::callbackZ(int num, int action, double value)
{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  return callback(num, action, value, &CutSphereOptions_Number[2].def,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
		  CTX.lc/100., -2*CTX.lc, 2*CTX.lc);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
}

double GMSH_CutSpherePlugin::callbackR(int num, int action, double value)
{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  return callback(num, action, value, &CutSphereOptions_Number[3].def,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
		  CTX.lc/100., 0., 2*CTX.lc);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
}

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
double GMSH_CutSpherePlugin::callbackVol(int num, int action, double value)
{
  return callback(num, action, value, &CutSphereOptions_Number[4].def,
		  1., -1., 1.);
}

double GMSH_CutSpherePlugin::callbackRecur(int num, int action, double value)
{
  return callback(num, action, value, &CutSphereOptions_Number[5].def,
		  1, 0, 10);
}

void GMSH_CutSpherePlugin::getName(char *name) const
{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  strcpy(name, "Cut Sphere");
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
void GMSH_CutSpherePlugin::getInfos(char *author, char *copyright,
                                    char *help_text) const
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  strcpy(author, "J.-F. Remacle");
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  strcpy(copyright, "DGR (www.multiphysics.com)");
  strcpy(help_text,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
         "Plugin(CutSphere) cuts the view `iView' with the\n"
	 "sphere (X-`Xc')^2 + (Y-`Yc')^2 + (Z-`Zc')^2 = `R'^2.\n"
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
	 "If `ExtractVolume' is nonzero, the plugin extracts\n"
	 "the elements inside (if `ExtractVolume' < 0) or\n"
	 "outside (if `ExtractVolume' > 0) the sphere. If\n"
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
	 "`iView' < 0, the plugin is run on the current view.\n"
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
	 "\n"
	 "Plugin(CutSphere) creates one new view.\n");
}

int GMSH_CutSpherePlugin::getNbOptions() const
{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  return sizeof(CutSphereOptions_Number) / sizeof(StringXNumber);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
StringXNumber *GMSH_CutSpherePlugin::getOption(int iopt)
  return &CutSphereOptions_Number[iopt];
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
void GMSH_CutSpherePlugin::catchErrorMessage(char *errorMessage) const
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  strcpy(errorMessage, "CutSphere failed...");
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
double GMSH_CutSpherePlugin::levelset(double x, double y, double z,
                                      double val) const
  double a = CutSphereOptions_Number[0].def;
  double b = CutSphereOptions_Number[1].def;
  double c = CutSphereOptions_Number[2].def;
  double r = CutSphereOptions_Number[3].def;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  return (x - a) * (x - a) + (y - b) * (y - b) + (z - c) * (z - c) - r * r;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
Post_View *GMSH_CutSpherePlugin::execute(Post_View * v)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  int iView = (int)CutSphereOptions_Number[6].def;
  _ref[0] = CutSphereOptions_Number[0].def;
  _ref[1] = CutSphereOptions_Number[1].def;
  _ref[2] = CutSphereOptions_Number[2].def;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  _extractVolume = (int)CutSphereOptions_Number[4].def;
  _recurLevel = (int)CutSphereOptions_Number[5].def;

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  _valueIndependent = 1;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  _valueView = -1;
  _valueTimeStep = -1;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  _orientation = GMSH_LevelsetPlugin::SPHERE;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  if(iView < 0)
    iView = v ? v->Index : 0;

  if(!List_Pointer_Test(CTX.post.list, iView)) {
    Msg(GERROR, "View[%d] does not exist", iView);
    return v;
Christophe Geuzaine's avatar
Christophe Geuzaine committed

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  Post_View *v1 = *(Post_View **)List_Pointer(CTX.post.list, iView);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed

  return GMSH_LevelsetPlugin::execute(v1);
Christophe Geuzaine's avatar
Christophe Geuzaine committed
}