Newer
Older
// Gmsh - Copyright (C) 1997-2008 C. Geuzaine, J.-F. Remacle
// See the LICENSE.txt file for license information. Please report all
// bugs and problems to <gmsh@geuz.org>.
#include <fstream>
#include <string>
#include <sstream>
#ifdef HAVE_MATH_EVAL
#include "matheval.h"
#endif
#ifdef HAVE_ANN
#include "ANN/ANN.h"
#endif
#include "Context.h"
#include "Field.h"
#include "GeoInterpolation.h"
#include "GModel.h"
#include "Message.h"

Christophe Geuzaine
committed
#if !defined(HAVE_NO_POST)
#include "OctreePost.h"
#include "PViewDataList.h"

Christophe Geuzaine
committed
#endif

Jean-François Remacle
committed
class FieldOptionDouble:public FieldOption
{
public:
double &val;
FieldOptionType get_type()
{
return FIELD_OPTION_DOUBLE;

Jean-François Remacle
committed
};
FieldOptionDouble(double &_val, bool * _status = NULL):FieldOption(_status),
val(_val) {
};

Jean-François Remacle
committed
};
void numerical_value(double v)
{
modified();
val = v;

Jean-François Remacle
committed
};
void get_text_representation(std::string & v_str)
{
std::ostringstream sstream;
sstream.precision(16);
sstream << val;
v_str = sstream.str();
}

Jean-François Remacle
committed
class FieldOptionInt:public FieldOption
{
public:
int &val;
FieldOptionType get_type()
{
return FIELD_OPTION_INT;

Jean-François Remacle
committed
};
FieldOptionInt(int &_val, bool * _status = NULL):FieldOption(_status),
val(_val) {
};

Jean-François Remacle
committed
};

Jean-François Remacle
committed
};
void get_text_representation(std::string & v_str)
{
std::ostringstream sstream;
sstream << val;
v_str = sstream.str();
}

Jean-François Remacle
committed
class FieldOptionList:public FieldOption
{
public:
std::list < int >&val;
FieldOptionType get_type()
{
return FIELD_OPTION_LIST;

Jean-François Remacle
committed
};
FieldOptionList(std::list < int >&_val, bool * _status = NULL):FieldOption(_status),
val(_val)

Jean-François Remacle
committed
};
std::list < int >&list()

Jean-François Remacle
committed
const std::list < int >&list() const
{
return val;
}
void get_text_representation(std::string & v_str)
{
std::ostringstream sstream;
sstream << "{";
for(std::list < int >::iterator it = val.begin(); it != val.end(); it++) {
if(it != val.begin())
sstream << ", ";
sstream << *it;
}
sstream << "}";
v_str = sstream.str();
}

Jean-François Remacle
committed
class FieldOptionString:public FieldOption
{
public:
std::string & val;
virtual FieldOptionType get_type()
{
return FIELD_OPTION_STRING;

Jean-François Remacle
committed
};
FieldOptionString(std::string & _val, bool * _status = NULL):FieldOption(_status),
val(_val) {
};
std::string & string() {

Jean-François Remacle
committed
const std::string & string() const

Jean-François Remacle
committed
void get_text_representation(std::string & v_str)
{
std::ostringstream sstream;
sstream << "\"" << val << "\"";
v_str = sstream.str();
}

Jean-François Remacle
committed
class FieldOptionBool:public FieldOption
{
public:
bool & val;
FieldOptionType get_type()
{
return FIELD_OPTION_BOOL;

Jean-François Remacle
committed
};
FieldOptionBool(bool & _val, bool * _status = NULL):FieldOption(_status),
val(_val) {
};
double numerical_value() const
{
return val;

Jean-François Remacle
committed
};
void numerical_value(double v)
{
modified();
val = v;

Jean-François Remacle
committed
};
void get_text_representation(std::string & v_str)
{
std::ostringstream sstream;
sstream << val;
v_str = sstream.str();
}

Jean-François Remacle
committed
for(std::map < int, Field * >::iterator it = begin(); it != end(); it++) {
return NULL;
}
return it->second;
}
Field *FieldManager::new_field(int id, std::string type_name)
Msg::Error("Field id %i is already defined.", id);
return NULL;
}
if(map_type_name.find(type_name) == map_type_name.end()) {
Msg::Error("Unknown field type \"%s\".", type_name.c_str());
return NULL;
}
Field *f = (*map_type_name[type_name]) ();
if(!f)
return NULL;
f->id = id;
(*this)[id] = f;
int FieldManager::new_id()
{
int i = 0;
iterator it = begin();
while(1) {
i++;
while(it != end() && it->first < i)
it++;
if(it == end() || it->first != i)
break;
}
return std::max(i, 1);
int FieldManager::max_id()
{
if(!empty())
return rbegin()->first;
else
return 0;
void FieldManager::delete_field(int id)
{
iterator it = find(id);
if(it == end()) {
Msg::Error("Cannot delete field id %i, it does not exist.", id);

Jean-François Remacle
committed
class StructuredField:public Field
{
double o[3], d[3];
int n[3];
double *data;

Jean-François Remacle
committed
bool text_format;

Jean-François Remacle
committed
public:StructuredField()
{
options["FileName"] = new FieldOptionString(file_name, &update_needed);

Jean-François Remacle
committed
text_format = false;
options["TextFormat"] = new FieldOptionBool(text_format, &update_needed);
data = NULL;
}
const char *get_name()
{
return "Structured";
}

Jean-François Remacle
committed
virtual ~ StructuredField() {
if(data)
delete[]data;
}
double operator() (double x, double y, double z)
{
if(update_needed) {
error_status = false;
try {
std::ifstream input(file_name.c_str());
if(!input.is_open())
throw(1);
input.
exceptions(std::ifstream::eofbit | std::ifstream::failbit | std::
ifstream::badbit);

Jean-François Remacle
committed
if(!text_format) {
input.read((char *)o, 3 * sizeof(double));
input.read((char *)d, 3 * sizeof(double));
input.read((char *)n, 3 * sizeof(int));
int nt = n[0] * n[1] * n[2];
if(data)
delete[]data;
data = new double[nt];
input.read((char *)data, nt * sizeof(double));
}
else {
input >> o[0] >> o[1] >> o[2] >> d[0] >> d[1] >> d[2] >> n[0] >>
n[1] >> n[2];
int nt = n[0] * n[1] * n[2];
if(data)
delete[]data;
data = new double[nt];
for(int i = 0; i < nt; i++)
input >> data[i];
}
input.close();
}
catch(...) {
error_status = true;
Msg::Error("Field %i : error reading file %s", this->id,
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
file_name.c_str());
}
update_needed = false;
}
if(error_status)
return MAX_LC;
//tri-linear
int id[2][3];
double xi[3];
double xyz[3] = { x, y, z };
for(int i = 0; i < 3; i++) {
id[0][i] = (int)floor((xyz[i] - o[i]) / d[i]);
id[1][i] = id[0][i] + 1;
id[0][i] = std::max(std::min(id[0][i], n[i] - 1), 0);
id[1][i] = std::max(std::min(id[1][i], n[i] - 1), 0);
xi[i] = (xyz[i] - (o[i] + id[0][i] * d[i])) / d[i];
xi[i] = std::max(std::min(xi[i], 1.), 0.);
}
double v = 0;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++) {
v += data[id[i][0] * n[1] * n[2] + id[j][1] * n[2] + id[k][2]]
* (i * xi[0] + (1 - i) * (1 - xi[0]))
* (j * xi[1] + (1 - j) * (1 - xi[1]))
* (k * xi[2] + (1 - k) * (1 - xi[2]));
}
return v;
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}

Jean-François Remacle
committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
class UTMField:public Field
{
int field_id, zone;
double a, b, n, n2, n3, n4, n5, e, e2, e1, e12, e13, e14, J1, J2, J3, J4,
Ap, Bp, Cp, Dp, Ep, e4, e6, ep, ep2, ep4, k0, mu_fact;
public:
UTMField()
{
field_id = 1;
zone = 0;
options["IField"] = new FieldOptionInt(field_id);
options["Zone"] = new FieldOptionInt(zone);
a = 6378137; /* Equatorial Radius */
b = 6356752.3142; /* Rayon Polar Radius */
/* see http://www.uwgb.edu/dutchs/UsefulData/UTMFormulas.HTM */
n = (a - b) / (a + b);
n2 = n * n;
n3 = n * n * n;
n4 = n * n * n * n;
n5 = n * n * n * n * n;
e = sqrt(1 - b * b / a / a);
e2 = e * e;
e1 = (1 - sqrt(1 - e2)) / (1 + sqrt(1 - e2));
e12 = e1 * e1;
e13 = e1 * e1 * e1;
e14 = e1 * e1 * e1 * e1;
J1 = (3 * e1 / 2 - 27 * e13 / 32);
J2 = (21 * e12 / 16 - 55 * e14 / 32);
J3 = 151 * e13 / 96;
J4 = 1097 * e14 / 512;
Ap = a * (1 - n + (5. / 4.) * (n2 - n3) + (81. / 64.) * (n4 - n5));
Bp = -3 * a * n / 2 * (1 - n + (7. / 8.) * (n2 - n3) +
(55. / 64.) * (n4 - n5));
Cp = 14 * a * n2 / 16 * (1 - n + (3. / 4) * (n2 - n3));
Dp = -35 * a * n3 / 48 * (1 - n + 11. / 16. * (n2 - n3));
Ep = +315 * a * n4 / 51 * (1 - n);
e4 = e2 * e2;
e6 = e2 * e2 * e2;
ep = e * a / b;
ep2 = ep * ep;
ep4 = ep2 * ep2;
k0 = 0.9996;
mu_fact = 1 / (k0 * a * (1 - e2 / 4 - 3 * e4 / 64 - 5 * e6 / 256));
}
const char *get_name()
{
return "UTM";
}
double operator() (double x, double y, double z)
{
double r = sqrt(x * x + y * y + z * z);
double lon = atan2(y, x);
double lat = asin(z / r);
double meridionalarc = Ap * lat + Bp * sin(2 * lat)
+ Cp * sin(4 * lat) + Dp * sin(6 * lat) + Ep;
double slat = sin(lat);
double clat = cos(lat);
double slat2 = slat * slat;
double clat2 = clat * clat;
double clat3 = clat2 * clat;
double clat4 = clat3 * clat;
double tlat2 = slat2 / clat2;
double nu = a / sqrt(1 - e * e * slat2);
double p = lon - ((zone - 0.5) / 30 - 1) * M_PI;
double p2 = p * p;
double p3 = p * p2;
double p4 = p2 * p2;
double utm_x =
k0 * nu * clat * p + (k0 * nu * clat3 / 6) * (1 - tlat2 +
ep2 * clat2) * p3 + 5e5;
double utm_y =
meridionalarc * k0 + k0 * nu * slat * clat / 2 * p2 +
k0 * nu * slat * clat3 / 24 * (5 - tlat2 + 9 * ep2 * clat2 +
4 * ep4 * clat4) * p4;
return (*GModel::current()->getFields()->get(field_id)) (utm_x, utm_y, 0);
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
};
class LonLatField:public Field
{

Jean-François Remacle
committed
public:
LonLatField()
{
field_id = 1;
options["IField"] = new FieldOptionInt(field_id);
}
const char *get_name()
{
return "LonLat";
}
double operator() (double x, double y, double z)
{

Jean-François Remacle
committed
return (*GModel::current()->getFields()->get(field_id))
(atan2(y, x), asin(z / sqrt(x * x + y * y + z * z)), 0);
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}

Jean-François Remacle
committed
class BoxField:public Field
{
double v_in, v_out, x_min, x_max, y_min, y_max, z_min, z_max;

Jean-François Remacle
committed
public:
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
BoxField()
{
v_in = v_out = x_min = x_max = y_min = y_max = z_min = z_max = 0;
options["VIn"] = new FieldOptionDouble(v_in);
options["VOut"] = new FieldOptionDouble(v_out);
options["XMin"] = new FieldOptionDouble(x_min);
options["XMax"] = new FieldOptionDouble(x_max);
options["YMin"] = new FieldOptionDouble(y_min);
options["YMax"] = new FieldOptionDouble(y_max);
options["ZMin"] = new FieldOptionDouble(z_min);
options["ZMax"] = new FieldOptionDouble(z_max);
}
const char *get_name()
{
return "Box";
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
double operator() (double x, double y, double z)
{
return (x <= x_max && x >= x_min && y <= y_max && y >= y_min && z <= z_max
&& z >= z_min) ? v_in : v_out;
}

Jean-François Remacle
committed
class ThresholdField:public Field
{

Jean-François Remacle
committed
public:
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
const char *get_name()
{
return "Threshold";
}
ThresholdField()
{
iField = 0;
dmin = 1;
dmax = 10;
lcmin = 0.1;
lcmax = 1;
options["IField"] = new FieldOptionInt(iField);
options["DistMin"] = new FieldOptionDouble(dmin);
options["DistMax"] = new FieldOptionDouble(dmax);
options["LcMin"] = new FieldOptionDouble(lcmin);
options["LcMax"] = new FieldOptionDouble(lcmax);
}
double operator() (double x, double y, double z)
{
Field *field = GModel::current()->getFields()->get(iField);
double r = ((*field) (x, y, z) - dmin) / (dmax - dmin);
r = std::max(std::min(r, 1.), 0.);
double lc = lcmin * (1 - r) + lcmax * r;
return lc;
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}

Jean-François Remacle
committed
class GradientField:public Field
{

Jean-François Remacle
committed
public:
const char *get_name()
{
return "Gradient";
}

Jean-François Remacle
committed
GradientField():iField(0), kind(3), delta(CTX.lc / 1e4)
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
{
options["IField"] = new FieldOptionInt(iField);
options["Kind"] = new FieldOptionInt(kind);
options["Delta"] = new FieldOptionDouble(delta);
}
double operator() (double x, double y, double z)
{
Field *field = GModel::current()->getFields()->get(iField);
double gx, gy, gz;
switch (kind) {
case 0: /* x */
return ((*field) (x + delta / 2, y, z) -
(*field) (x - delta / 2, y, z)) / delta;
case 1: /* y */
return ((*field) (x, y + delta / 2, z) -
(*field) (x, y - delta / 2, z)) / delta;
case 2: /* z */
return ((*field) (x, y, z + delta / 2) -
(*field) (x, y, z - delta / 2)) / delta;
case 3: /* norm */
gx =
((*field) (x + delta / 2, y, z) -
(*field) (x - delta / 2, y, z)) / delta;
gy =
((*field) (x, y + delta / 2, z) -
(*field) (x, y - delta / 2, z)) / delta;
gz =
((*field) (x, y, z + delta / 2) -
(*field) (x, y, z - delta / 2)) / delta;
return sqrt(gx * gx + gy * gy + gz * gz);
default:
Msg::Error("Field %i : Unknown kind (%i) of gradient.", this->id,
kind);
return MAX_LC;
}
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
class CurvatureField:public Field
{
int iField;
double delta;
public:
const char *get_name()
{
return "Curvature";
}
CurvatureField():iField(0), delta(CTX.lc / 1e4)
{
options["IField"] = new FieldOptionInt(iField);
options["Delta"] = new FieldOptionDouble(delta);
}
void grad_norm(Field &f,double x,double y,double z, double *g){
g[0]=f(x+delta/2,y,z)-f(x-delta/2,y,z);
g[1]=f(x,y+delta/2,z)-f(x,y-delta/2,z);
g[2]=f(x,y,z+delta/2)-f(x,y,z-delta/2);
double n=sqrt(g[0]*g[0]+g[1]*g[1]+g[2]*g[2]);
g[0]/=n;
g[1]/=n;
g[2]/=n;
}
double operator() (double x, double y, double z)
{
Field *field = GModel::current()->getFields()->get(iField);
double grad[6][3];
grad_norm(*field,x+delta/2,y,z,grad[0]);
grad_norm(*field,x-delta/2,y,z,grad[1]);
grad_norm(*field,x,y+delta/2,z,grad[2]);
grad_norm(*field,x,y-delta/2,z,grad[3]);
grad_norm(*field,x,y,z+delta/2,grad[4]);
grad_norm(*field,x,y,z-delta/2,grad[5]);
return (grad[0][0]-grad[1][0]+grad[2][1]-grad[3][1]+grad[4][2]-grad[5][2])/delta;
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
};
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#if defined(HAVE_GSL)
#include <gsl/gsl_math.h>
#include <gsl/gsl_eigen.h>
class MaxEigenHessianField:public Field
{
int iField;
double delta;
gsl_eigen_symm_workspace *gslwork;
gsl_matrix *gslmat;
gsl_vector *eigenvalues;
public:
const char *get_name()
{
return "MaxEigenHessian";
}
MaxEigenHessianField():iField(0), delta(CTX.lc / 1e4)
{
options["IField"] = new FieldOptionInt(iField);
options["Delta"] = new FieldOptionDouble(delta);
gslwork=gsl_eigen_symm_alloc(3);
eigenvalues=gsl_vector_alloc(3);
gslmat=gsl_matrix_alloc(3,3);
}
~MaxEigenHessianField(){
gsl_eigen_symm_free(gslwork);
gsl_vector_free(eigenvalues);
gsl_matrix_free(gslmat);
}
double operator() (double x, double y, double z)
{
Field *field = GModel::current()->getFields()->get(iField);
gsl_matrix_set(gslmat,1,0,
(*field) (x + delta/2 , y+delta/2, z)
+ (*field) (x - delta/2 , y-delta/2, z)
- (*field) (x - delta/2 , y+delta/2, z)
- (*field) (x + delta/2 , y-delta/2, z));
gsl_matrix_set(gslmat,2,0,
(*field) (x + delta/2 , y, z+delta/2)
+ (*field) (x - delta/2 , y, z-delta/2)
- (*field) (x - delta/2 , y, z+delta/2)
- (*field) (x + delta/2 , y, z-delta/2));
gsl_matrix_set(gslmat,2,1,
(*field) (x, y + delta/2 , z+delta/2)
+ (*field) (x, y - delta/2 , z-delta/2)
- (*field) (x, y - delta/2 , z+delta/2)
- (*field) (x, y + delta/2 , z-delta/2));
double f=(*field)(x,y,z);
gsl_matrix_set(gslmat,0,0,
(*field) (x + delta , y, z)+ (*field) (x - delta , y, z) -2*f);
gsl_matrix_set(gslmat,1,1,
(*field) (x, y + delta, z)+ (*field) (x , y - delta, z) -2* f);
gsl_matrix_set(gslmat,2,2,
(*field) (x, y ,z + delta)+ (*field) (x , y, z - delta)-2*f);
gsl_eigen_symm(gslmat,eigenvalues,gslwork);
return std::max(
fabs(gsl_vector_get(eigenvalues,0)),
std::max(
fabs(gsl_vector_get(eigenvalues,0)),
fabs(gsl_vector_get(eigenvalues,1))))/(delta*delta);
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
};
#endif
class LaplacianField:public Field
{
int iField;
double delta;
public:
const char *get_name()
{
return "Laplacian";
}
LaplacianField():iField(0), delta(CTX.lc / 1e4)
{
options["IField"] = new FieldOptionInt(iField);
options["Delta"] = new FieldOptionDouble(delta);
}
double operator() (double x, double y, double z)
{
Field *field = GModel::current()->getFields()->get(iField);
return (
(*field) (x + delta , y, z)+ (*field) (x - delta , y, z)
+(*field) (x, y + delta , z)+ (*field) (x, y - delta , z)
+(*field) (x, y, z + delta )+ (*field) (x, y, z - delta )
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
) / (delta*delta);
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
};
class MeanField:public Field
{
int iField;
double delta;
int n;
public:
const char *get_name()
{
return "Mean";
}
MeanField():iField(0), delta(CTX.lc / 1e4)
{
options["IField"] = new FieldOptionInt(iField);
options["Delta"] = new FieldOptionDouble(delta);
//options["N"] = new FieldOptionInt(n);
}
double operator() (double x, double y, double z)
{
Field *field = GModel::current()->getFields()->get(iField);
return (
(*field) (x + delta , y, z)+ (*field) (x - delta , y, z)
+(*field) (x, y + delta , z)+ (*field) (x, y - delta , z)
+(*field) (x, y, z + delta )+ (*field) (x, y, z - delta )
+ (*field) (x , y, z)
) / 5;
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
};

Jean-François Remacle
committed
class MathEvalExpression
{
bool error_status;
std::list < Field * >*list;
int nvalues;
char **names;
double *values;
void *eval;
int *evaluators_id;
std::string function;
char *c_str_function;

Jean-François Remacle
committed
public:
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
double evaluate(double x, double y, double z)
{
if(error_status)
return MAX_LC;
for(int i = 0; i < nvalues; i++)
{
Field *f;
switch (evaluators_id[i]) {
case -1:
values[i] = x;
break;
case -2:values[i] = y;
break;
case -3:values[i] = z;
break;
default:
{
f = GModel::current()->getFields()->get(evaluators_id[i]);
values[i] = f ? (*f) (x, y, z) : MAX_LC;
}
}
}
return evaluator_evaluate(eval, nvalues, names, values);
}
MathEvalExpression() {
eval = NULL;
values = NULL;
c_str_function = NULL;
evaluators_id = NULL;
}
bool set_function(const std::string & f)
{
free_members();
error_status = false;
c_str_function = strdup(f.c_str());
eval = evaluator_create(c_str_function);
if(!eval) {
error_status = true;
return false;
}
evaluator_get_variables(eval, &names, &nvalues);
values = new double[nvalues];
evaluators_id = new int[nvalues];
for(int i = 0; i < nvalues; i++) {
int id;
if(!strcmp("x", names[i]))
evaluators_id[i] = -1;
else if(!strcmp("y", names[i]))
evaluators_id[i] = -2;
else if(!strcmp("z", names[i]))
evaluators_id[i] = -3;
else if(sscanf(names[i], "F%i", &id) == 1)
evaluators_id[i] = id;
else {
Msg::Error("Unknown matheval argument \"%s\"\n", names[i]);
error_status = true;
return false;
}
}
return true;
}
void free_members()
{
if(c_str_function)
free(c_str_function);
if(eval)
evaluator_destroy(eval);
if(values)
delete[]values;
if(evaluators_id)
delete evaluators_id;
}
~MathEvalExpression() {
free_members();
}

Jean-François Remacle
committed
class MathEvalField:public Field
{

Jean-François Remacle
committed
public:
MathEvalField()
{
options["F"] = new FieldOptionString(f, &update_needed);
}
double operator() (double x, double y, double z)
{
if(update_needed) {
if(!expr.set_function(f))
Msg::Error("Field %i : Invalid matheval expression \"%s\"\n",
this->id, f.c_str());
update_needed = false;
}
return expr.evaluate(x, y, z);
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
const char *get_name()
{
return "MathEval";
}

Jean-François Remacle
committed
class ParametricField:public Field
{
MathEvalExpression expr[3];
std::string f[3];
int ifield;

Jean-François Remacle
committed
public:
ParametricField()
{
options["IField"] = new FieldOptionInt(ifield);
options["FX"] = new FieldOptionString(f[0], &update_needed);
options["FY"] = new FieldOptionString(f[1], &update_needed);
options["FZ"] = new FieldOptionString(f[2], &update_needed);
}
double operator() (double x, double y, double z)
{
if(update_needed) {
for(int i = 0; i < 3; i++) {
if(!expr[i].set_function(f[i]))
Msg::Error("Field %i : Invalid matheval expression \"%s\"\n",
this->id, f[i].c_str());
}
update_needed = false;
}

Jean-François Remacle
committed
return (*GModel::current()->getFields()->get(ifield))
(expr[0].evaluate(x, y, z), expr[1].evaluate(x, y, z),
expr[2].evaluate(x, y, z));
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
const char *get_name()
{
return "Param";
}

Christophe Geuzaine
committed
#if !defined(HAVE_NO_POST)

Jean-François Remacle
committed
class PostViewField:public Field
{

Jean-François Remacle
committed
public:int view_index;
double operator() (double x, double y, double z)
{

Jean-François Remacle
committed
// FIXME: should test unique view num instead, but that would be slower
if(view_index < 0 || view_index >= (int)PView::list.size())
return MAX_LC;

Jean-François Remacle
committed
if(update_needed)
{
if(octree)
delete octree;
octree = new OctreePost(PView::list[view_index]);
update_needed = false;
}
double l = 0.;

Jean-François Remacle
committed
if(!octree->searchScalar(x, y, z, &l, 0)) {
// uncomment the following to try really hard to find an element
// around the point
/*
double fact[9] = {0.001, 0.0025, 0.005, 0.0075, 0.01, 0.025, 0.05, 0.075, 0.1};
for(int i = 0; i < 9; i++){
double eps = CTX.lc * fact[i];
if(octree->searchScalar(x + eps, y, z, &l, 0)) break;
if(octree->searchScalar(x - eps, y, z, &l, 0)) break;
if(octree->searchScalar(x, y + eps, z, &l, 0)) break;
if(octree->searchScalar(x, y - eps, z, &l, 0)) break;
if(octree->searchScalar(x, y, z + eps, &l, 0)) break;
if(octree->searchScalar(x, y, z - eps, &l, 0)) break;
if(octree->searchScalar(x + eps, y - eps, z - eps, &l, 0)) break;
if(octree->searchScalar(x + eps, y + eps, z - eps, &l, 0)) break;
if(octree->searchScalar(x - eps, y - eps, z - eps, &l, 0)) break;
if(octree->searchScalar(x - eps, y + eps, z - eps, &l, 0)) break;
if(octree->searchScalar(x + eps, y - eps, z + eps, &l, 0)) break;
if(octree->searchScalar(x + eps, y + eps, z + eps, &l, 0)) break;
if(octree->searchScalar(x - eps, y - eps, z + eps, &l, 0)) break;
if(octree->searchScalar(x - eps, y + eps, z + eps, &l, 0)) break;
}
*/
}
return l;
}
const char *get_name()
{
return "PostView";
}

Jean-François Remacle
committed
PostViewField() {
octree = NULL;
options["IView"] = new FieldOptionInt(view_index, &update_needed);
}

Jean-François Remacle
committed
~PostViewField() {
if(octree)
delete octree;
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}

Christophe Geuzaine
committed
#endif

Jean-François Remacle
committed
class MinField:public Field
{

Jean-François Remacle
committed
public:
MinField()
{
options["FieldsList"] = new FieldOptionList(idlist, &update_needed);
}
double operator() (double x, double y, double z)
{
double v = MAX_LC;
for(std::list < int >::iterator it = idlist.begin(); it != idlist.end();
it++) {
Field *f = (GModel::current()->getFields()->get(*it));
if(f)
v = std::min(v, (*f) (x, y, z));
}
return v;
}
FieldDialogBox *&dialog_box()
{
static FieldDialogBox *dialogBox = NULL;
return dialogBox;
}
const char *get_name()
{
return "Min";
}

Jean-François Remacle
committed
class MaxField:public Field
{
std::list < int >idlist;