Skip to content
Snippets Groups Projects
CutSphere.cpp 5.09 KiB
Newer Older
Christophe Geuzaine's avatar
Christophe Geuzaine committed
// Gmsh - Copyright (C) 1997-2017 C. Geuzaine, J.-F. Remacle
Christophe Geuzaine's avatar
Christophe Geuzaine committed
//
Christophe Geuzaine's avatar
Christophe Geuzaine committed
// See the LICENSE.txt file for license information. Please report all
Christophe Geuzaine's avatar
Christophe Geuzaine committed
// bugs and problems to the public mailing list <gmsh@onelab.info>.
Christophe Geuzaine's avatar
Christophe Geuzaine committed

#include <string.h>
#include "GmshConfig.h"
#include "CutSphere.h"
#include "Context.h"

#include "drawContext.h"
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#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},
  {GMSH_FULLRC, "TargetError", GMSH_CutSpherePlugin::callbackTarget, 0.},
  {GMSH_FULLRC, "View", NULL, -1.}
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  GMSH_Plugin *GMSH_RegisterCutSpherePlugin()
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    return new GMSH_CutSpherePlugin();
void GMSH_CutSpherePlugin::draw(void *context)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  GLint mode[2];
  glGetIntegerv(GL_POLYGON_MODE, mode);
  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  glColor4ubv((GLubyte *) & CTX::instance()->color.fg);
  glLineWidth((float)CTX::instance()->lineWidth);
  drawContext *ctx = (drawContext*)context;
  ctx->drawSphere(CutSphereOptions_Number[3].def,
                  CutSphereOptions_Number[0].def,
                  CutSphereOptions_Number[1].def,
                  CutSphereOptions_Number[2].def, 40, 40, 1);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  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,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
                                      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;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  GMSH_Plugin::setDrawFunction(draw);
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
pp  
Christophe Geuzaine committed
                  CTX::instance()->lc / 100., - 2 * CTX::instance()->lc,
                  2 * CTX::instance()->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,
                  CTX::instance()->lc / 100., -2 * CTX::instance()->lc,
                  2 * CTX::instance()->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
pp  
Christophe Geuzaine committed
                  CTX::instance()->lc / 100., -2 * CTX::instance()->lc,
                  2 * CTX::instance()->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,
                  CTX::instance()->lc / 100., 0., 2 * CTX::instance()->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,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
                  1., -1., 1.);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
}

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

double GMSH_CutSpherePlugin::callbackTarget(int num, int action, double value)
{
  return callback(num, action, value, &CutSphereOptions_Number[6].def,
                  0.01, 0., 1.);
}

std::string GMSH_CutSpherePlugin::getHelp() const
  return "Plugin(CutSphere) cuts the view `View' with the "
    "sphere (X-`Xc')^2 + (Y-`Yc')^2 + (Z-`Zc')^2 = `R'^2.\n\n"
    "If `ExtractVolume' is nonzero, the plugin extracts "
    "the elements inside (if `ExtractVolume' < 0) or "
    "outside (if `ExtractVolume' > 0) the sphere.\n\n"
    "If `View' < 0, the plugin is run on the current view.\n\n"
    "Plugin(CutSphere) creates one new view.";
}

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
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
PView *GMSH_CutSpherePlugin::execute(PView *v)
  int iView = (int)CutSphereOptions_Number[7].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;
  _targetError = CutSphereOptions_Number[6].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
  PView *v1 = getView(iView, v);
  if(!v1) return v;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed

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