Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Apr 25, 2018
2 parents 2c69ccb + b6544b4 commit 8c74495
Show file tree
Hide file tree
Showing 12 changed files with 753 additions and 19 deletions.
50 changes: 50 additions & 0 deletions curve/CurveUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,56 @@ public static void InPlaceSmooth(IList<Vector3d> vertices, int iStart, int iEnd,



/// <summary>
/// smooth set of vertices using extra buffer
/// </summary>
public static void IterativeSmooth(IList<Vector3d> vertices, double alpha, int nIterations, bool bClosed)
{
IterativeSmooth(vertices, 0, vertices.Count, alpha, nIterations, bClosed);
}
/// <summary>
/// smooth set of vertices using extra buffer
/// </summary>
public static void IterativeSmooth(IList<Vector3d> vertices, int iStart, int iEnd, double alpha, int nIterations, bool bClosed, Vector3d[] buffer = null)
{
int N = vertices.Count;
if (buffer == null || buffer.Length < N)
buffer = new Vector3d[N];
if (bClosed) {
for (int iter = 0; iter < nIterations; ++iter) {
for (int ii = iStart; ii < iEnd; ++ii) {
int i = (ii % N);
int iPrev = (ii == 0) ? N - 1 : ii - 1;
int iNext = (ii + 1) % N;
Vector3d prev = vertices[iPrev], next = vertices[iNext];
Vector3d c = (prev + next) * 0.5f;
buffer[i] = (1 - alpha) * vertices[i] + (alpha) * c;
}
for (int ii = iStart; ii < iEnd; ++ii) {
int i = (ii % N);
vertices[i] = buffer[i];
}
}
} else {
for (int iter = 0; iter < nIterations; ++iter) {
for (int i = iStart; i <= iEnd; ++i) {
if (i == 0 || i >= N - 1)
continue;
Vector3d prev = vertices[i - 1], next = vertices[i + 1];
Vector3d c = (prev + next) * 0.5f;
buffer[i] = (1 - alpha) * vertices[i] + (alpha) * c;
}
for (int ii = iStart; ii < iEnd; ++ii) {
int i = (ii % N);
vertices[i] = buffer[i];
}
}
}
}




}


Expand Down
56 changes: 49 additions & 7 deletions curve/CurveUtils2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,25 @@ public static void LaplacianSmoothConstrained(Polygon2d poly, double alpha, int
for (int i = 0; i < N; ++i ) {
Vector2d curpos = poly[i];
Vector2d smoothpos = (poly[(i + N - 1) % N] + poly[(i + 1) % N]) * 0.5;
bool contained = true;
if (bAllowShrink == false || bAllowGrow == false)
contained = origPoly.Contains(smoothpos);

bool do_smooth = true;
if (bAllowShrink && contained == false)
do_smooth = false;
if (bAllowGrow && contained == true)
do_smooth = false;
if (bAllowShrink == false || bAllowGrow == false) {
bool is_inside = origPoly.Contains(smoothpos);
if (is_inside == true)
do_smooth = bAllowShrink;
else
do_smooth = bAllowGrow;
}

// [RMS] this is old code...I think not correct?
//bool contained = true;
//if (bAllowShrink == false || bAllowGrow == false)
// contained = origPoly.Contains(smoothpos);
//bool do_smooth = true;
//if (bAllowShrink && contained == false)
// do_smooth = false;
//if (bAllowGrow && contained == true)
// do_smooth = false;

if ( do_smooth ) {
Vector2d newpos = beta * curpos + alpha * smoothpos;
Expand Down Expand Up @@ -246,6 +257,37 @@ public static void Split<T>(List<T> objects, out List<T> set1, out List<T> set2,



public static Polygon2d SplitToTargetLength(Polygon2d poly, double length)
{
Polygon2d result = new Polygon2d();
result.AppendVertex(poly[0]);
for (int j = 0; j < poly.VertexCount; ++j) {
int next = (j + 1) % poly.VertexCount;
double len = poly[j].Distance(poly[next]);
if (len < length) {
result.AppendVertex(poly[next]);
continue;
}

int steps = (int)Math.Ceiling(len / length);
for (int k = 1; k < steps; ++k) {
double t = (double)(k) / (double)steps;
Vector2d v = (1.0 - t) * poly[j] + (t) * poly[next];
result.AppendVertex(v);
}

if (j < poly.VertexCount - 1) {
Util.gDevAssert(poly[j].Distance(result.Vertices[result.VertexCount - 1]) > 0.0001);
result.AppendVertex(poly[next]);
}
}

return result;
}




/// <summary>
/// Remove polygons and polygon-holes smaller than minArea
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions curve/PolyLine2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,43 @@ public PolyLine2d Transform(ITransform2 xform)
}



static public PolyLine2d MakeBoxSpiral(Vector2d center, double len, double spacing)
{
double d = spacing / 2;
PolyLine2d pline = new PolyLine2d();
pline.AppendVertex(center);

Vector2d c = center;
c.x += spacing / 2;
pline.AppendVertex(c);
c.y += spacing;
pline.AppendVertex(c);
double accum = spacing / 2 + spacing;

double w = spacing / 2;
double h = spacing;

double sign = -1.0;
while (accum < len) {
w += spacing;
c.x += sign * w;
pline.AppendVertex(c);
accum += w;

h += spacing;
c.y += sign * h;
pline.AppendVertex(c);
accum += h;

sign *= -1.0;
}

return pline;
}



}


Expand Down
2 changes: 2 additions & 0 deletions geometry3Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<Compile Include="implicit\GridImplicits3d.cs" />
<Compile Include="implicit\Implicit2d.cs" />
<Compile Include="implicit\Implicit3d.cs" />
<Compile Include="implicit\ImplicitFieldSampler3d.cs" />
<Compile Include="implicit\ImplicitOperators.cs" />
<Compile Include="implicit\MarchingQuads.cs" />
<Compile Include="intersection\IntrLine3AxisAlignedBox3.cs" />
Expand Down Expand Up @@ -294,6 +295,7 @@
<Compile Include="spatial\Bitmap3.cs" />
<Compile Include="spatial\DCurveBoxTree.cs" />
<Compile Include="spatial\DCurveProjection.cs" />
<Compile Include="spatial\DenseGrid2.cs" />
<Compile Include="spatial\DenseGrid3.cs" />
<Compile Include="spatial\PointAABBTree3.cs" />
<Compile Include="spatial\DMeshAABBTree.cs" />
Expand Down
91 changes: 91 additions & 0 deletions implicit/ImplicitFieldSampler3d.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace g3
{
/// <summary>
/// Sample implicit fields into a dense grid
/// </summary>
public class ImplicitFieldSampler3d
{
public DenseGrid3f Grid;
public double CellSize;
public Vector3d GridOrigin;
public ShiftGridIndexer3 Indexer;
public AxisAlignedBox3i GridBounds;

public float BackgroundValue;


public enum CombineModes
{
DistanceMinUnion = 0
}
public CombineModes CombineMode = CombineModes.DistanceMinUnion;


public ImplicitFieldSampler3d(AxisAlignedBox3d fieldBounds, double cellSize)
{
CellSize = cellSize;
GridOrigin = fieldBounds.Min;
Indexer = new ShiftGridIndexer3(GridOrigin, CellSize);

Vector3d max = fieldBounds.Max; max += cellSize;
int ni = (int)((max.x - GridOrigin.x) / CellSize) + 1;
int nj = (int)((max.y - GridOrigin.y) / CellSize) + 1;
int nk = (int)((max.z - GridOrigin.z) / CellSize) + 1;

GridBounds = new AxisAlignedBox3i(0, 0, 0, ni, nj, nk);

BackgroundValue = (float)((ni + nj + nk) * CellSize);
Grid = new DenseGrid3f(ni, nj, nk, BackgroundValue);
}



public DenseGridTrilinearImplicit ToImplicit() {
return new DenseGridTrilinearImplicit(Grid, GridOrigin, CellSize);
}



public void Clear(float f)
{
BackgroundValue = f;
Grid.assign(BackgroundValue);
}



public void Sample(BoundedImplicitFunction3d f, double expandRadius = 0)
{
AxisAlignedBox3d bounds = f.Bounds();

Vector3d expand = expandRadius * Vector3d.One;
Vector3i gridMin = Indexer.ToGrid(bounds.Min-expand),
gridMax = Indexer.ToGrid(bounds.Max+expand) + Vector3i.One;
gridMin = GridBounds.ClampExclusive(gridMin);
gridMax = GridBounds.ClampExclusive(gridMax);

AxisAlignedBox3i gridbox = new AxisAlignedBox3i(gridMin, gridMax);
switch (CombineMode) {
case CombineModes.DistanceMinUnion:
sample_min(f, gridbox.IndicesInclusive());
break;
}
}


void sample_min(BoundedImplicitFunction3d f, IEnumerable<Vector3i> indices)
{
gParallel.ForEach(indices, (idx) => {
Vector3d v = Indexer.FromGrid(idx);
double d = f.Value(ref v);
Grid.set_min(ref idx, (float)d);
});
}

}
}
12 changes: 11 additions & 1 deletion math/AxisAlignedBox3d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public Vector3d Center {
get { return new Vector3d(0.5 * (Min.x + Max.x), 0.5 * (Min.y + Max.y), 0.5 * (Min.z + Max.z)); }
}


public static bool operator ==(AxisAlignedBox3d a, AxisAlignedBox3d b) {
return a.Min == b.Min && a.Max == b.Max;
}
Expand Down Expand Up @@ -138,6 +137,17 @@ public Vector3d Corner(int i)
return new Vector3d(x, y, z);
}

/// <summary>
/// Returns point on face/edge/corner. For each coord value neg==min, 0==center, pos==max
/// </summary>
public Vector3d Point(int xi, int yi, int zi)
{
double x = (xi < 0) ? Min.x : ((xi == 0) ? (0.5*(Min.x + Max.x)) : Max.x);
double y = (yi < 0) ? Min.y : ((yi == 0) ? (0.5*(Min.y + Max.y)) : Max.y);
double z = (zi < 0) ? Min.z : ((zi == 0) ? (0.5*(Min.z + Max.z)) : Max.z);
return new Vector3d(x, y, z);
}


// TODO
////! 0 == bottom-left, 1 = bottom-right, 2 == top-right, 3 == top-left
Expand Down
12 changes: 12 additions & 0 deletions math/AxisAlignedBox3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ public Vector3f Corner(int i)
}


/// <summary>
/// Returns point on face/edge/corner. For each coord value neg==min, 0==center, pos==max
/// </summary>
public Vector3f Point(int xi, int yi, int zi)
{
float x = (xi < 0) ? Min.x : ((xi == 0) ? (0.5f * (Min.x + Max.x)) : Max.x);
float y = (yi < 0) ? Min.y : ((yi == 0) ? (0.5f * (Min.y + Max.y)) : Max.y);
float z = (zi < 0) ? Min.z : ((zi == 0) ? (0.5f * (Min.z + Max.z)) : Max.z);
return new Vector3f(x, y, z);
}


//! value is subtracted from min and added to max
public void Expand(float fRadius)
{
Expand Down
19 changes: 19 additions & 0 deletions math/IndexUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,25 @@ public static void sort_indices(ref Index3i tri)



public static Vector3i ToGrid3Index(int idx, int nx, int ny)
{
int x = idx % nx;
int y = (idx / nx) % ny;
int z = idx / (nx * ny);
return new Vector3i(x, y, z);
}

public static int ToGrid3Linear(int i, int j, int k, int nx, int ny) {
return i + nx * (j + ny * k);
}
public static int ToGrid3Linear(Vector3i ijk, int nx, int ny) {
return ijk.x + nx * (ijk.y + ny * ijk.z);
}
public static int ToGrid3Linear(ref Vector3i ijk, int nx, int ny) {
return ijk.x + nx * (ijk.y + ny * ijk.z);
}



/// <summary>
/// Filter out invalid entries in indices[] list. Will return indices itself if
Expand Down
6 changes: 3 additions & 3 deletions mesh/MeshEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ public virtual int[] StitchUnorderedEdges(List<Index2i> EdgePairs, int group_id
/// vertex ordering must reslut in appropriate orientation (which is...??)
/// [TODO] check and fail on bad orientation
/// </summary>
public virtual int[] StitchSpan(int[] vspan1, int[] vspan2, int group_id = -1)
public virtual int[] StitchSpan(IList<int> vspan1, IList<int> vspan2, int group_id = -1)
{
int N = vspan1.Length;
if (N != vspan2.Length)
int N = vspan1.Count;
if (N != vspan2.Count)
throw new Exception("MeshEditor.StitchSpan: spans are not the same length!!");
N--;

Expand Down
Loading

0 comments on commit 8c74495

Please sign in to comment.