mesh optimization improvements

This commit is contained in:
Joachim Schoeberl 2009-11-16 08:18:00 +00:00
parent 8ebf21f600
commit 83e8b1ec53
12 changed files with 1093 additions and 699 deletions

View File

@ -351,7 +351,8 @@ namespace netgen
{
c = ac;
r = ar;
invr = 1.0/r;
cxx = cyy = czz = 0.5 / r;
cxy = cxz = cyz = 0;
cx = - c(0) / r;
@ -377,6 +378,7 @@ namespace netgen
c(2) = coeffs.Elem(3);
r = coeffs.Elem(4);
invr = 1.0/r;
cxx = cyy = czz = 0.5 / r;
cxy = cxz = cyz = 0;
cx = - c(0) / r;
@ -412,6 +414,10 @@ namespace netgen
}
double Sphere :: CalcFunctionValue (const Point<3> & point) const
{
return 0.5* (invr * Abs2 (point-c) - r);
}
int Sphere :: IsIdentic (const Surface & s2, int & inv, double eps) const

View File

@ -122,7 +122,7 @@ namespace netgen
///
Point<3> c;
///
double r;
double r, invr;
public:
///
Sphere (const Point<3> & ac, double ar);
@ -135,6 +135,8 @@ namespace netgen
virtual Primitive * Copy () const;
virtual void Transform (Transformation<3> & trans);
virtual double CalcFunctionValue (const Point<3> & point) const;
virtual int IsIdentic (const Surface & s2, int & inv, double eps) const;

View File

@ -221,16 +221,6 @@ namespace netgen
static void MeshSurface (CSGeometry & geom, Mesh & mesh)
{
/*
Point3d pmin, pmax;
mesh.GetBox(pmin, pmax);
cout << "box = " << pmin << " - " << pmax << endl;
cout << "localhbox = "
<< mesh.LocalHFunction().GetBoundingBox().PMin() << " - "
<< mesh.LocalHFunction().GetBoundingBox().PMax()
<< endl;
*/
const char * savetask = multithread.task;
multithread.task = "Surface meshing";
@ -429,7 +419,7 @@ namespace netgen
double eps = 1e-8 * geom.MaxSize();
for (PointIndex pi = PointIndex::BASE; pi < noldp+PointIndex::BASE; pi++)
{
//if(surf->PointOnSurface(mesh[pi]))
// if(surf->PointOnSurface(mesh[pi]))
meshing.AddPoint (mesh[pi], pi, NULL,
(surf->PointOnSurface(mesh[pi], eps) != 0));
}
@ -511,18 +501,23 @@ namespace netgen
if (segments.Size())
{
// surface was meshed, not copied
static int timer = NgProfiler::CreateTimer ("total surface mesh optimization");
NgProfiler::RegionTimer reg (timer);
PrintMessage (2, "Optimize Surface");
for (int i = 1; i <= mparam.optsteps2d; i++)
{
if (multithread.terminate) return;
{
MeshOptimize2dSurfaces meshopt(geom);
meshopt.SetFaceIndex (k);
meshopt.SetImproveEdges (0);
meshopt.SetMetricWeight (mparam.elsizeweight);
meshopt.SetWriteStatus (0);
meshopt.EdgeSwapping (mesh, (i > mparam.optsteps2d/2));
}

View File

@ -689,7 +689,7 @@ namespace netgen
case UNION:
{
int in1, in2, strin1, strin2;
Solid * tansol1, * tansol2;
Solid * tansol1 = 0, * tansol2 = 0;
s1 -> RecTangentialSolid (p, tansol1, surfids, in1, strin1, eps);
s2 -> RecTangentialSolid (p, tansol2, surfids, in2, strin2, eps);
@ -703,6 +703,11 @@ namespace netgen
else if (tansol2)
tansol = tansol2;
}
else
{
delete tansol1;
delete tansol2;
}
in = (in1 || in2);
strin = (strin1 || strin2);
break;

View File

@ -76,6 +76,10 @@ namespace netgen
CalcSpecialPoints (const CSGeometry & ageometry,
Array<MeshPoint> & apoints)
{
static int timer = NgProfiler::CreateTimer ("CSG: find special points");
NgProfiler::RegionTimer reg (timer);
geometry = &ageometry;
points = &apoints;
@ -231,7 +235,7 @@ namespace netgen
// explicit solution for planes only and at most one quadratic
if (numprim <= 5)
{
int nplane = 0, nquad = 0, quadi = -1;
int nplane = 0, nquad = 0, quadi = -1, nsphere = 0;
const QuadraticSurface *qsurf = 0, *qsurfi;
for (int i = 0; i < numprim; i++)
@ -247,6 +251,9 @@ namespace netgen
quadi = i;
qsurf = qsurfi;
}
if (dynamic_cast<const Sphere*> (qsurfi))
nsphere++;
}
/*
@ -374,6 +381,82 @@ namespace netgen
return;
}
if (nsphere == numprim) // && calccp == false)
{
Array<Point<3> > pts;
Array<int> surfids;
for (int k1 = 0; k1 < numprim; k1++)
for (int k2 = 0; k2 < k1; k2++)
for (int k3 = 0; k3 < k2; k3++)
{
ComputeCrossPoints (dynamic_cast<const Sphere*> (geometry->GetSurface(locsurf[k1])),
dynamic_cast<const Sphere*> (geometry->GetSurface(locsurf[k2])),
dynamic_cast<const Sphere*> (geometry->GetSurface(locsurf[k3])),
pts);
for (int j = 0; j < pts.Size(); j++)
if (Dist (pts[j], box.Center()) < box.Diam()/2)
{
Solid * tansol;
sol -> TangentialSolid (pts[j], tansol, surfids, 1e-9*size);
if(!tansol)
continue;
bool ok1 = false, ok2 = false, ok3 = false;
int rep1 = geometry->GetSurfaceClassRepresentant(locsurf[k1]);
int rep2 = geometry->GetSurfaceClassRepresentant(locsurf[k2]);
int rep3 = geometry->GetSurfaceClassRepresentant(locsurf[k3]);
for(int jj=0; jj<surfids.Size(); jj++)
{
int actrep = geometry->GetSurfaceClassRepresentant(surfids[jj]);
if(actrep == rep1) ok1 = true;
if(actrep == rep2) ok2 = true;
if(actrep == rep3) ok3 = true;
}
if (tansol && ok1 && ok2 && ok3)
// if (sol -> IsIn (pts[j], 1e-6*size) && !sol->IsStrictIn (pts[j], 1e-6*size))
{
if (AddPoint (pts[j], layer))
(*testout) << "cross point found, 1: " << pts[j] << endl;
}
delete tansol;
}
}
for (int k1 = 0; k1 < numprim; k1++)
for (int k2 = 0; k2 < k1; k2++)
{
ComputeExtremalPoints (dynamic_cast<const Sphere*> (geometry->GetSurface(locsurf[k1])),
dynamic_cast<const Sphere*> (geometry->GetSurface(locsurf[k2])),
pts);
for (int j = 0; j < pts.Size(); j++)
if (Dist (pts[j], box.Center()) < box.Diam()/2)
{
Solid * tansol;
sol -> TangentialSolid (pts[j], tansol, surfids, 1e-9*size);
if (tansol)
// sol -> IsIn (pts[j], 1e-6*size) && !sol->IsStrictIn (pts[j], 1e-6*size) )
{
if (AddPoint (pts[j], layer))
(*testout) << "extremal point found, spheres: " << pts[j] << endl;
}
delete tansol;
}
}
return;
}
}
@ -1143,6 +1226,83 @@ namespace netgen
void SpecialPointCalculation ::
ComputeCrossPoints (const Sphere * sphere1,
const Sphere * sphere2,
const Sphere * sphere3,
Array<Point<3> > & pts)
{
Mat<2,3> mat;
Mat<3,2> inv;
Vec<2> rhs;
Vec<3> sol, t;
Point<3> p0(0,0,0);
pts.SetSize (0);
Point<3> c1 = sphere1 -> Center();
Point<3> c2 = sphere2 -> Center();
Point<3> c3 = sphere3 -> Center();
double r1 = sphere1 -> Radius();
double r2 = sphere2 -> Radius();
double r3 = sphere3 -> Radius();
Vec<3> a1 = c2-c1;
double b1 = 0.5 * (sqr(r1) - sqr(r2) - Abs2(Vec<3> (c1)) + Abs2(Vec<3> (c2)) );
Vec<3> a2 = c3-c1;
double b2 = 0.5 * (sqr(r1) - sqr(r3) - Abs2(Vec<3> (c1)) + Abs2(Vec<3> (c3)) );
for (int j = 0; j < 3; j++)
{
mat(0,j) = a1(j);
mat(1,j) = a2(j);
}
rhs(0) = b1;
rhs(1) = b2;
CalcInverse (mat, inv);
sol = inv * rhs;
t = Cross (mat.Row(0), mat.Row(1));
if (t.Length() > 1e-8)
{
Point<3> p (sol);
// quadratic on p + s t = 0
double quad_a;
Vec<3> quad_b;
Mat<3> quad_c;
quad_a = sphere1 -> CalcFunctionValue(p);
sphere1 -> CalcGradient (p, quad_b);
sphere1 -> CalcHesse (p, quad_c);
double a, b, c;
a = quad_a;
b = quad_b * t;
c = 0.5 * t * (quad_c * t);
// a + s b + s^2 c = 0;
double disc = b*b-4*a*c;
if (disc > 1e-10 * fabs (b))
{
disc = sqrt (disc);
double s1 = (-b-disc) / (2*c);
double s2 = (-b+disc) / (2*c);
pts.Append (p + s1 * t);
pts.Append (p + s2 * t);
}
}
}
@ -1238,6 +1398,138 @@ namespace netgen
void SpecialPointCalculation ::
ComputeExtremalPoints (const Sphere * sphere1,
const Sphere * sphere2,
Array<Point<3> > & pts)
{
// 3 equations:
// surf1 = 0 <===> |x-c1|^2 - r1^2 = 0;
// surf2 = 0 <===> |x-c2|^2 - r2^2 = 0;
// (grad 1 x grad 2)(i) = 0 <====> (x-p1) x (p1-p2) . e_i = 0;
pts.SetSize (0);
Point<3> c1 = sphere1 -> Center();
Point<3> c2 = sphere2 -> Center();
double r1 = sphere1 -> Radius();
double r2 = sphere2 -> Radius();
/*
*testout << "\n\ncompute extremalpoint, sphere-sphere" << endl;
*testout << "c1 = " << c1 << ", r1 = " << r1 << endl;
*testout << "c2 = " << c2 << ", r2 = " << r2 << endl;
*testout << "dist = " << Abs (c2-c1) << ", r1+r2 = " << r1+r2 << endl;
*/
Vec<3> v12 = c2 - c1;
Vec<3> a1, a2;
double b1, b2;
// eqn: ai . x = bi
a1 = v12;
b1 = 0.5 * (sqr(r1) - sqr(r2) - Abs2(Vec<3> (c1)) + Abs2(Vec<3> (c2)) );
int dir = 0;
for (int j = 1; j < 3; j++)
if (fabs (v12(j)) > v12(dir))
dir = j;
// *testout << "dir = " << dir << endl;
Vec<3> ei = 0.0;
ei(dir) = 1;
a2 = Cross (v12, ei);
b2 = Vec<3>(c1) * a2;
Point<3> p0 (0,0,0);
double quad_a;
Vec<3> quad_b;
Mat<3> quad_c;
quad_a = sphere1 -> CalcFunctionValue(p0);
sphere1 -> CalcGradient (p0, quad_b);
sphere1 -> CalcHesse (p0, quad_c);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
quad_c(i,j) *= 0.5;
// find line of two linear equations:
Vec<2> rhs;
Vec<3> sol;
Mat<2,3> mat;
for (int j = 0; j < 3; j++)
{
mat(0,j) = a1(j);
mat(1,j) = a2(j);
}
rhs(0) = b1;
rhs(1) = b2;
// *testout << "mat = " << endl << mat << endl;
// *testout << "rhs = " << endl << rhs << endl;
Vec<3> t = Cross (a1, a2);
if (Abs2(t) > 0)
{
mat.Solve (rhs, sol);
/*
*testout << "sol = " << endl << sol << endl;
*testout << "a * sol = " << mat * sol << endl;
*testout << "c1-sol = " << Abs (Vec<3>(c1)-sol) << endl;
*testout << "c2-sol = " << Abs (Vec<3>(c2)-sol) << endl;
*/
// solve quadratic equation along line sol + alpha t ....
double a = quad_a + quad_b * sol + sol * (quad_c * sol);
double b = quad_b * t + 2 * (sol * (quad_c * t));
double c = t * (quad_c * t);
// solve a + b alpha + c alpha^2:
if (fabs (c) > 1e-32)
{
double disc = sqr (0.5*b/c) - a/c;
if (disc > 0)
{
disc = sqrt (disc);
double alpha1 = -0.5*b/c + disc;
double alpha2 = -0.5*b/c - disc;
pts.Append (Point<3> (sol+alpha1*t));
pts.Append (Point<3> (sol+alpha2*t));
// *testout << "pts = " << endl << pts << endl;
/*
cout << "sol1 = " << sol + alpha1 * t
<< ", sol2 = " << sol + alpha2 * t << endl;
*/
}
}
}
}
/*
bool SpecialPointCalculation :: ExtremalPointPossible (const Surface * f1,
const Surface * f2,
@ -1445,11 +1737,11 @@ namespace netgen
if (tlo->GetLayer() != apoints[i].GetLayer())
continue;
Solid * locsol;
sol -> TangentialSolid (p, locsol, surfind, ideps*geomsize);
rep_surfind.SetSize (surfind.Size());
int num_indep_surfs = 0;
@ -1470,6 +1762,7 @@ namespace netgen
#endif
if (!locsol) continue;
// get all surface indices,
if (surf)

View File

@ -161,6 +161,11 @@ namespace netgen
const QuadraticSurface * quadric,
Array<Point<3> > & pts);
void ComputeExtremalPoints (const Sphere * sphere1,
const Sphere * sphere2,
Array<Point<3> > & pts);
void ComputeCrossPoints (const Plane * plane1,
const Plane * plane2,
const Plane * plane3,
@ -170,6 +175,11 @@ namespace netgen
const Plane * plane2,
const QuadraticSurface * quadratic,
Array<Point<3> > & pts);
void ComputeCrossPoints (const Sphere * sphere1,
const Sphere * sphere2,
const Sphere * sphere3,
Array<Point<3> > & pts);
};
}

View File

@ -94,8 +94,9 @@ namespace netgen
if (mgi)
cpointsearchtree.Insert (p, pi);
pointsearchtree.Insert (p, pi);
if (pointonsurface)
pointsearchtree.Insert (p, pi);
return pi;
}
@ -287,7 +288,9 @@ namespace netgen
Array<INDEX> & lindex,
double xh)
{
// baselineindex += 1-lines.Begin();
static int timer = NgProfiler::CreateTimer ("adfront2::GetLocals");
NgProfiler::RegionTimer reg (timer);
int pstind;
Point<3> midp, p0;
@ -296,13 +299,12 @@ namespace netgen
p0 = points[pstind].P();
loclines.Append(lines[baselineindex].L());
lindex.Append(baselineindex); // +1-lines.Begin());
lindex.Append(baselineindex);
static Array<int> nearlines;
nearlines.SetSize(0);
static Array<int> nearpoints;
nearpoints.SetSize(0);
ArrayMem<int, 1000> nearlines(0);
ArrayMem<int, 1000> nearpoints(0);
// dominating costs !!
linesearchtree.GetIntersecting (p0 - Vec3d(xh, xh, xh),
p0 + Vec3d(xh, xh, xh),
nearlines);
@ -310,12 +312,11 @@ namespace netgen
pointsearchtree.GetIntersecting (p0 - Vec3d(xh, xh, xh),
p0 + Vec3d(xh, xh, xh),
nearpoints);
for (int ii = 1; ii <= nearlines.Size(); ii++)
for (int ii = 0; ii < nearlines.Size(); ii++)
{
int i = nearlines.Get(ii);
if (lines[i].Valid() && i != baselineindex) // + 1-lines.Begin())
int i = nearlines[ii];
if (lines[i].Valid() && i != baselineindex)
{
loclines.Append(lines[i].L());
lindex.Append(i);

File diff suppressed because it is too large Load Diff

View File

@ -896,7 +896,6 @@ namespace netgen
}
}
if (strcmp (str, "volumeelements") == 0)
{
infile >> n;

View File

@ -196,6 +196,14 @@ namespace netgen
MESHING2_RESULT Meshing2 :: GenerateMesh (Mesh & mesh, double gh, int facenr)
{
static int timer = NgProfiler::CreateTimer ("surface meshing");
static int timer1 = NgProfiler::CreateTimer ("surface meshing1");
static int timer2 = NgProfiler::CreateTimer ("surface meshing2");
static int timer3 = NgProfiler::CreateTimer ("surface meshing3");
NgProfiler::RegionTimer reg (timer);
Array<int> pindex, lindex;
Array<int> delpoints, dellines;
@ -205,7 +213,6 @@ namespace netgen
Array<Element2d> locelements;
int z1, z2, oldnp(-1);
SurfaceElementIndex sei;
bool found;
int rulenr(-1);
int globind;
@ -254,7 +261,9 @@ namespace netgen
double totalarea = Area ();
double meshedarea = 0;
// search tree for surface elements:
/*
for (sei = 0; sei < mesh.GetNSE(); sei++)
{
const Element2d & sel = mesh[sei];
@ -269,18 +278,45 @@ namespace netgen
box.Add ( mesh[sel[2]] );
surfeltree.Insert (box, sei);
}
double trigarea = Cross ( mesh[sel[1]]-mesh[sel[0]],
mesh[sel[2]]-mesh[sel[0]] ).Length() / 2;
if (sel.GetNP() == 4)
trigarea += Cross (Vec3d (mesh.Point (sel.PNum(1)),
mesh.Point (sel.PNum(3))),
Vec3d (mesh.Point (sel.PNum(1)),
mesh.Point (sel.PNum(4)))).Length() / 2;;
meshedarea += trigarea;
}
*/
Array<SurfaceElementIndex> seia;
mesh.GetSurfaceElementsOfFace (facenr, seia);
for (int i = 0; i < seia.Size(); i++)
{
const Element2d & sel = mesh[seia[i]];
if (sel.IsDeleted()) continue;
Box<3> box;
box.Set ( mesh[sel[0]] );
box.Add ( mesh[sel[1]] );
box.Add ( mesh[sel[2]] );
surfeltree.Insert (box, i);
}
if (totalarea > 0 || maxarea > 0)
for (SurfaceElementIndex sei = 0; sei < mesh.GetNSE(); sei++)
{
const Element2d & sel = mesh[sei];
if (sel.IsDeleted()) continue;
double trigarea = Cross ( mesh[sel[1]]-mesh[sel[0]],
mesh[sel[2]]-mesh[sel[0]] ).Length() / 2;
if (sel.GetNP() == 4)
trigarea += Cross (Vec3d (mesh.Point (sel.PNum(1)),
mesh.Point (sel.PNum(3))),
Vec3d (mesh.Point (sel.PNum(1)),
mesh.Point (sel.PNum(4)))).Length() / 2;;
meshedarea += trigarea;
}
const char * savetask = multithread.task;
@ -293,8 +329,12 @@ namespace netgen
double meshedarea_before = meshedarea;
while (!adfront ->Empty() && !multithread.terminate)
{
NgProfiler::RegionTimer reg1 (timer1);
if (multithread.terminate)
throw NgException ("Meshing stopped");
@ -370,6 +410,10 @@ namespace netgen
adfront ->GetLocals (baselineindex, locpoints, mpgeominfo, loclines,
pindex, lindex, 2*hinner);
NgProfiler::RegionTimer reg2 (timer2);
//(*testout) << "h for locals: " << 2*hinner << endl;
@ -707,6 +751,9 @@ namespace netgen
}
}
NgProfiler::RegionTimer reg3 (timer3);
for (int i = 1; i <= locelements.Size() && found; i++)
{
const Element2d & el = locelements.Get(i);
@ -1451,14 +1498,15 @@ namespace netgen
PrintMessage (3, "Surface meshing done");
adfront->PrintOpenSegments (*testout);
multithread.task = savetask;
// cout << "surfeltree.depth = " << surfeltree.Tree().Depth() << endl;
EndMesh ();
if (!adfront->Empty())
return MESHING2_GIVEUP;

View File

@ -232,7 +232,8 @@ class MeshPoint : public Point<3>
#endif
public:
MeshPoint () : layer(1), singular(0.), type(INNERPOINT)
MeshPoint ()
// : layer(1), singular(0.), type(INNERPOINT) // would unnecessarily initialize large arrays !
{
#ifdef PARALLEL
isghost = 0;

View File

@ -667,14 +667,15 @@ namespace netgen
}
static int timer = NgProfiler::CreateTimer ("MeshSmoothing 2D");
NgProfiler::RegionTimer reg (timer);
static int timer1 = NgProfiler::CreateTimer ("MeshSmoothing 2D start");
NgProfiler::RegionTimer reg (timer);
NgProfiler::StartTimer (timer1);
CheckMeshApproximation (mesh);
SurfaceElementIndex sei;
// SurfaceElementIndex sei;
Array<SurfaceElementIndex> seia;
mesh.GetSurfaceElementsOfFace (faceindex, seia);
@ -694,14 +695,17 @@ namespace netgen
PointGeomInfo ngi;
Point3d origp;
Vec3d n1, n2;
Vector x(2), xedge(1);
Array<MeshPoint, PointIndex::BASE> savepoints(mesh.GetNP());
uselocalh = mparam.uselocalh;
Array<int, PointIndex::BASE> nelementsonpoint(mesh.GetNP());
Array<int, PointIndex::BASE> nelementsonpoint(mesh.GetNP());
nelementsonpoint = 0;
for (int i = 0; i < seia.Size(); i++)
{
@ -711,6 +715,7 @@ namespace netgen
}
TABLE<SurfaceElementIndex,PointIndex::BASE> elementsonpoint(nelementsonpoint);
for (int i = 0; i < seia.Size(); i++)
{
const Element2d & el = mesh[seia[i]];
@ -718,6 +723,7 @@ namespace netgen
elementsonpoint.Add (el[j], seia[i]);
}
loch = mparam.maxh;
locmetricweight = metricweight;
meshthis = this;
@ -813,6 +819,10 @@ namespace netgen
}
int cnt = 0;
NgProfiler::StopTimer (timer1);
for (PointIndex pi = PointIndex::BASE; pi < mesh.GetNP()+PointIndex::BASE; pi++)
if (mesh[pi].Type() == SURFACEPOINT)
{
@ -825,9 +835,10 @@ namespace netgen
printeddot = 1;
PrintDot (plotchar);
}
if (elementsonpoint[pi].Size() == 0)
continue;
sp1 = mesh[pi];
@ -850,7 +861,7 @@ namespace netgen
for (int j = 0; j < elementsonpoint[pi].Size(); j++)
{
sei = elementsonpoint[pi][j];
SurfaceElementIndex sei = elementsonpoint[pi][j];
const Element2d & bel = mesh[sei];
surfi = mesh.GetFaceDescriptor(bel.GetIndex()).SurfNr();